From: Hou Tao <houtao1@huawei.com> hulk inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10302 -------------------------------- Add tools/lib/nds with the NDS userspace API. The library registers device memory, submits asynchronous P2P I/O, and harvests completion events through the nds_p2p UAPI. Signed-off-by: Hou Tao <houtao1@huawei.com> Signed-off-by: Zizhi Wo <wozizhi@huawei.com> --- tools/include/uapi/linux/nds_p2p.h | 135 +++++ tools/lib/nds/Makefile | 16 + tools/lib/nds/file_p2p/.gitignore | 6 + tools/lib/nds/file_p2p/Makefile | 36 ++ tools/lib/nds/file_p2p/nds_api.c | 365 ++++++++++++++ tools/lib/nds/file_p2p/nds_api.h | 233 +++++++++ tools/lib/nds/file_p2p/nds_api_internal.h | 12 + tools/lib/nds/file_p2p/p2p_common.c | 74 +++ tools/lib/nds/file_p2p/p2p_common.h | 14 + tools/lib/nds/file_p2p/py_nds_api.c | 577 ++++++++++++++++++++++ tools/lib/nds/file_p2p/setup.py | 21 + 11 files changed, 1489 insertions(+) create mode 100644 tools/include/uapi/linux/nds_p2p.h create mode 100644 tools/lib/nds/Makefile create mode 100644 tools/lib/nds/file_p2p/.gitignore create mode 100644 tools/lib/nds/file_p2p/Makefile create mode 100644 tools/lib/nds/file_p2p/nds_api.c create mode 100644 tools/lib/nds/file_p2p/nds_api.h create mode 100644 tools/lib/nds/file_p2p/nds_api_internal.h create mode 100644 tools/lib/nds/file_p2p/p2p_common.c create mode 100644 tools/lib/nds/file_p2p/p2p_common.h create mode 100644 tools/lib/nds/file_p2p/py_nds_api.c create mode 100644 tools/lib/nds/file_p2p/setup.py diff --git a/tools/include/uapi/linux/nds_p2p.h b/tools/include/uapi/linux/nds_p2p.h new file mode 100644 index 000000000000..6b376ee447b5 --- /dev/null +++ b/tools/include/uapi/linux/nds_p2p.h @@ -0,0 +1,135 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef NDS_P2P_H_ +#define NDS_P2P_H_ + +#include <linux/types.h> + +/* + * UAPI v0.2: PCI P2PDMA + asynchronous VFS I/O. + * + * All clients open /dev/p2p_device. An fd learns its provider from the first + * registered or one-shot buffer. Later buffers on that fd must use the same + * provider. No request or device-node name carries a PCI device id. + */ +/* GET_INFO uses bits 15:8 for major and bits 7:0 for minor. */ +#define P2P_UAPI_VERSION_MAJOR 0U +#define P2P_UAPI_VERSION_MINOR 2U +#define P2P_UAPI_VERSION ((P2P_UAPI_VERSION_MAJOR << 8) | P2P_UAPI_VERSION_MINOR) + +struct p2p_iov { + __u64 addr; /* HBM addr */ + __u32 size; + __u32 reserved; /* must be zero */ +}; + +#define P2P_IO_F_REGISTERED_MEM (1U << 0) +#define P2P_IO_F_MASK P2P_IO_F_REGISTERED_MEM + +#define P2P_IO_READ 0U +#define P2P_IO_WRITE 1U + +/* One logical I/O moves at most this many bytes (the kernel MAX_RW_COUNT). */ +#define P2P_MAX_IO_BYTES 0x7ffff000U + +/* + * Fixed-size request; iov is a userspace pointer. One batch item is one + * logical I/O at byte offset @offset in @file_fd, covering all iov segments + * and completing as one event carrying user_data. A transfer shorter than the + * iov total completes with -EIO. + */ +struct p2p_io_param { + unsigned int op; /* P2P_IO_READ / P2P_IO_WRITE */ + unsigned int flags; /* P2P_IO_F_* */ + int file_fd; /* regular file or block device; no O_APPEND */ + int host_pid; /* caller TGID for one-shot; 0 if registered */ + __s32 mem_fd; /* ignored unless P2P_IO_F_REGISTERED_MEM is set */ + __u32 mem_reserved; /* must be zero */ + __u64 user_data; /* echo'd in the completion event */ + __u64 offset; /* byte offset in file_fd */ + __u64 iov; /* userspace pointer to struct p2p_iov[] */ + unsigned int iov_nr; + unsigned int reserved; /* must be zero */ + __u64 reserved2[3]; /* must be zero */ +}; + +#define P2P_MAX_IO_NR 1024U + +/* + * Submit independent p2p_io_param records in array order. The ioctl stops at + * the first item that cannot be accepted and returns the accepted prefix, or + * a negative errno when the first item fails. Each accepted item produces one + * completion event. The ioctl may sleep in the filesystem. + */ +struct p2p_io_batch_param { + __u64 items; /* userspace pointer to p2p_io_param[] */ + __u32 nr; + __u32 reserved; +}; + +struct p2p_io_event { + __u64 user_data; + __s64 res; /* 0 on success or -errno */ + __u64 reserved[4]; +}; + +struct p2p_getevents_param { + __s32 min_nr; + __s32 max_nr; + __s64 timeout_ns; /* <0 infinite, 0 nonblock, >0 relative */ + __u64 events; /* userspace pointer to struct p2p_io_event[] */ +}; + +struct p2p_mem_range { + __u64 addr; + __u64 size; +}; + +#define P2P_MEM_MAX_RANGES 8192U +#define P2P_MEM_MAX_TOTAL_RANGES 65536U + +/* + * Add non-overlapping VA ranges to the fd's table. Input overlap is -EINVAL, + * existing overlap is -EEXIST, and exceeding the per-fd limit is -E2BIG. + * The update is atomic. Only the opener's thread group can change the table. + */ +struct p2p_mem_register_param { + __u64 ranges; /* userspace pointer to struct p2p_mem_range[] */ + __u32 nr; /* 1..P2P_MEM_MAX_RANGES */ + __u32 flags; /* must be zero */ + __u64 reserved[4]; /* must be zero */ +}; + +/* + * Remove exact registered ranges and wait for their users before unpinning. + * A missing start is -ENOENT. A size mismatch or duplicate range is -EINVAL. + * The update is atomic. + */ +struct p2p_mem_unregister_param { + __u64 ranges; /* userspace pointer to struct p2p_mem_range[] */ + __u32 nr; /* 1..P2P_MEM_MAX_RANGES */ + __u32 flags; /* must be zero */ + __u64 reserved[4]; /* must be zero */ +}; + +/* Validate a file, directory or block-device fd's filesystem and P2PDMA queue. */ +struct p2p_check_fd_param { + __s32 fd; + __u32 flags; /* must be zero */ + __u64 reserved; /* must be zero */ +}; + +/* Output only. Feature flags and reserved fields are zero. */ +struct p2p_device_info { + __u32 version; /* major in bits 15:8, minor in bits 7:0; upper bits zero */ + __u32 flags; + __u64 reserved[3]; +}; + +#define IOCTL_GET_INFO _IOR('k', 1, struct p2p_device_info) +#define IOCTL_CHECK_FD _IOW('k', 2, struct p2p_check_fd_param) +#define IOCTL_REGISTER_MEM _IOW('k', 3, struct p2p_mem_register_param) +#define IOCTL_UNREGISTER_MEM _IOW('k', 4, struct p2p_mem_unregister_param) +#define IOCTL_SUBMIT_IO _IOWR('k', 5, struct p2p_io_batch_param) +#define IOCTL_GET_IO_EVENTS _IOWR('k', 6, struct p2p_getevents_param) + +#endif diff --git a/tools/lib/nds/Makefile b/tools/lib/nds/Makefile new file mode 100644 index 000000000000..f738ad37da37 --- /dev/null +++ b/tools/lib/nds/Makefile @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# NDS userspace library (tools/lib/nds) + +.PHONY: all clean lib binding + +all: lib + +lib: + $(MAKE) -C file_p2p + +binding: + $(MAKE) -C file_p2p binding + +clean: + $(MAKE) -C file_p2p clean diff --git a/tools/lib/nds/file_p2p/.gitignore b/tools/lib/nds/file_p2p/.gitignore new file mode 100644 index 000000000000..bc2c3dee3296 --- /dev/null +++ b/tools/lib/nds/file_p2p/.gitignore @@ -0,0 +1,6 @@ +__pycache__/ +build/ +nds*.so +libnds.so* +.*.d +/nds_example diff --git a/tools/lib/nds/file_p2p/Makefile b/tools/lib/nds/file_p2p/Makefile new file mode 100644 index 000000000000..b94ee7e1bfc0 --- /dev/null +++ b/tools/lib/nds/file_p2p/Makefile @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0 +CC ?= cc +CFLAGS ?= -O2 -Wall -Wextra -Werror -g -ggdb -fno-omit-frame-pointer +UAPI_DIR ?= $(abspath ../../../include/uapi) +CPPFLAGS += -I. -I.. -I$(UAPI_DIR) + +P2P_UAPI_H := $(UAPI_DIR)/linux/nds_p2p.h + +UAPI_MAJOR := $(shell awk '/^#define P2P_UAPI_VERSION_MAJOR/{print $$3+0}' $(P2P_UAPI_H)) +UAPI_MINOR := $(shell awk '/^#define P2P_UAPI_VERSION_MINOR/{print $$3+0}' $(P2P_UAPI_H)) +LIB_VERSION := $(UAPI_MAJOR).$(UAPI_MINOR) +NDS_LIB := libnds.so.$(LIB_VERSION) +NDS_SONAME := libnds.so.$(LIB_VERSION) + +NDS_SRCS := nds_api.c p2p_common.c +NDS_OBJS := $(NDS_SRCS:.c=.o) +NDS_DEPS := $(addprefix .,$(NDS_OBJS:.o=.d)) + +.PHONY: all binding clean + +all: $(NDS_LIB) + +binding: + python3 setup.py build_ext --inplace + +$(NDS_LIB): $(NDS_OBJS) $(P2P_UAPI_H) + $(CC) -shared -Wl,-soname,$(NDS_SONAME) -o $@ $(NDS_OBJS) -lpthread + ln -sf libnds.so.$(LIB_VERSION) libnds.so + +%.o: %.c + $(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -MMD -MP -MT $@ -MF .$*.d -c -o $@ $< + +-include $(NDS_DEPS) + +clean: + rm -f libnds.so libnds.so.* $(NDS_OBJS) $(NDS_DEPS) diff --git a/tools/lib/nds/file_p2p/nds_api.c b/tools/lib/nds/file_p2p/nds_api.c new file mode 100644 index 000000000000..58efc34add4e --- /dev/null +++ b/tools/lib/nds/file_p2p/nds_api.c @@ -0,0 +1,365 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * NDS — a GDS-aligned userspace API for NPU HBM ↔ NVMe P2P I/O. + * + * NDS uses the common UAPI helpers for device open and is the sole userspace + * API on top of the kernel UAPI (linux/nds_p2p.h). + */ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <errno.h> +#include <stddef.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <time.h> +#include <limits.h> +#include <stdint.h> + +#include "nds_api_internal.h" +#include <linux/nds_p2p.h> +#include "p2p_common.h" + +_Static_assert(sizeof(struct nds_io_vec) == sizeof(struct p2p_iov), + "nds_io_vec must match p2p_iov for zero-copy submission"); +_Static_assert(NDS_IO_MAX_IO_CNT <= P2P_MAX_IO_NR, + "NDS submit size must fit the kernel batch ioctl"); +_Static_assert(_Alignof(struct nds_io_vec) == _Alignof(struct p2p_iov), + "nds_io_vec alignment must match p2p_iov"); +_Static_assert(offsetof(struct nds_io_vec, buf_addr) == + offsetof(struct p2p_iov, addr) && + offsetof(struct nds_io_vec, buf_len) == + offsetof(struct p2p_iov, size) && + offsetof(struct nds_io_vec, reserved) == + offsetof(struct p2p_iov, reserved), + "nds_io_vec field layout must match p2p_iov"); +_Static_assert(sizeof(struct nds_io_event) == sizeof(struct p2p_io_event), + "nds_io_event must match p2p_io_event for zero-copy getevents"); +_Static_assert(sizeof(struct nds_io_event) == 48, + "nds_io_event / p2p_io_event are user_data + res + reserved[4]"); +_Static_assert(offsetof(struct nds_io_event, user_data) == + offsetof(struct p2p_io_event, user_data) && + offsetof(struct nds_io_event, res) == + offsetof(struct p2p_io_event, res) && + offsetof(struct nds_io_event, reserved) == + offsetof(struct p2p_io_event, reserved), + "nds_io_event field layout must match p2p_io_event"); + +_Static_assert(sizeof(struct nds_mem_range) == sizeof(struct p2p_mem_range) && + offsetof(struct nds_mem_range, addr) == + offsetof(struct p2p_mem_range, addr) && + offsetof(struct nds_mem_range, size) == + offsetof(struct p2p_mem_range, size), + "nds_mem_range must match p2p_mem_range for zero-copy registration"); +_Static_assert(NDS_MEM_MAX_RANGES <= P2P_MEM_MAX_RANGES, + "NDS range count must fit the kernel registration ioctl"); + +static struct nds_state { + int book_fd; +} g_state = { + .book_fd = -1, +}; + +static int is_init(void) +{ + return g_state.book_fd >= 0; +} + +/* ------------------------------------------------------------------ */ +/* Public NDS API */ +/* ------------------------------------------------------------------ */ + +int nds_init(struct nds_init_param *param) +{ + /* Not thread-safe: see nds_api.h. Concurrent init is undefined. */ + if (!param || param->flags || param->reserved1 || param->reserved2 || + is_init() || param->reserved3[0] || param->reserved3[1] || + param->reserved3[2] || param->reserved3[3]) + return -EINVAL; + + g_state.book_fd = p2p_open_dev(¶m->version); + if (g_state.book_fd < 0) + return g_state.book_fd; + return 0; +} + +int nds_register_fs(const struct nds_fs_desc *desc) +{ + uint32_t i; + + if (!desc || !desc->fs_fd || !desc->fs_fd_cnt || desc->reserved) + return -EINVAL; + for (i = 0; i < desc->fs_fd_cnt; i++) { + struct p2p_check_fd_param check = { .fd = desc->fs_fd[i] }; + + if (ioctl(g_state.book_fd, IOCTL_CHECK_FD, &check) < 0) + return -errno; + } + return 0; +} + +int nds_unregister_fs(const struct nds_fs_desc *desc) +{ + (void)desc; + return 0; +} + +int nds_exit(void) +{ + if (is_init()) { + close(g_state.book_fd); + g_state.book_fd = -1; + } + return 0; +} + +static int nds_mem_desc_valid(const struct nds_mem_desc *desc) +{ + return desc && desc->ranges && desc->nr && + desc->nr <= NDS_MEM_MAX_RANGES && !desc->flags && + !desc->reserved[0] && !desc->reserved[1] && + !desc->reserved[2] && !desc->reserved[3]; +} + +int nds_register_mem(const struct nds_mem_desc *desc) +{ + struct p2p_mem_register_param param = { 0 }; + + if (!is_init() || !nds_mem_desc_valid(desc)) + return -EINVAL; + param.ranges = (uint64_t)(uintptr_t)desc->ranges; + param.nr = desc->nr; + if (ioctl(g_state.book_fd, IOCTL_REGISTER_MEM, ¶m) < 0) + return -errno; + return 0; +} + +int nds_unregister_mem(const struct nds_mem_desc *desc) +{ + struct p2p_mem_unregister_param param = { 0 }; + + if (!is_init() || !nds_mem_desc_valid(desc)) + return -EINVAL; + param.ranges = (uint64_t)(uintptr_t)desc->ranges; + param.nr = desc->nr; + if (ioctl(g_state.book_fd, IOCTL_UNREGISTER_MEM, ¶m) < 0) + return -errno; + return 0; +} + +int nds_io_new_ctx(const struct nds_io_ctx_param *param, struct nds_io_ctx **ctx) +{ + struct nds_io_ctx *c; + int fd; + + if (!param || !ctx) + return -EINVAL; + if (!param->max_io_cnt || param->max_io_cnt > NDS_IO_MAX_IO_CNT || + param->flags || + param->reserved) + return -EINVAL; + + c = calloc(1, sizeof(*c)); + if (!c) + return -ENOMEM; + + fd = p2p_open_dev(NULL); + if (fd < 0) { + free(c); + return fd; + } + c->p2p_fd = fd; + *ctx = c; + return 0; +} + +int nds_io_destroy_ctx(struct nds_io_ctx *ctx) +{ + int close_err = 0; + + if (!ctx) + return -EINVAL; + /* + * Catches a closed-but-not-yet-freed ctx. A second destroy after + * free() is still use-after-free (caller bug / UB). + */ + if (ctx->p2p_fd < 0) + return -EINVAL; + + /* The final close waits for outstanding I/O and releases its events. */ + if (close(ctx->p2p_fd) < 0) { + close_err = -errno; + fprintf(stderr, "nds: destroy_ctx close failed %d\n", close_err); + } + ctx->p2p_fd = -1; + free(ctx); + return close_err; +} + +static int validate_nds_iov(const struct nds_io_cb *cb, unsigned long *io_size) +{ + if (!cb->iov || !cb->iov_cnt || cb->iov_cnt > NDS_IO_MAX_IOV) + return -EINVAL; + return p2p_get_iov_size((const struct p2p_iov *)cb->iov, cb->iov_cnt, io_size); +} + +static int nds_iocb_to_io_param(const struct nds_io_cb *cb, int file_fd, struct p2p_io_param *io) +{ + unsigned long io_size; + int err; + + if (cb->opcode > NDS_IO_OP_PWRITE || (cb->rw_flags & ~NDS_IO_F_MASK)) + return -EOPNOTSUPP; + + err = validate_nds_iov(cb, &io_size); + if (err) + return err; + if (io_size > P2P_MAX_IO_BYTES) + return -EINVAL; + if (cb->offset > ULONG_MAX || io_size > ULONG_MAX - (unsigned long)cb->offset) + return -EOVERFLOW; + + if (cb->rw_flags & NDS_IO_F_REGISTERED_MEM) { + if (cb->host_pid) + return -EINVAL; + } else if (cb->host_pid < 0) { + return -EINVAL; + } + + memset(io, 0, sizeof(*io)); + io->op = (cb->opcode == NDS_IO_OP_PWRITE) ? P2P_IO_WRITE : P2P_IO_READ; + io->file_fd = file_fd; + io->user_data = cb->user_data; + io->offset = cb->offset; + io->iov = (uint64_t)(uintptr_t)cb->iov; + io->iov_nr = cb->iov_cnt; + + if (cb->rw_flags & NDS_IO_F_REGISTERED_MEM) { + io->flags = P2P_IO_F_REGISTERED_MEM; + io->host_pid = 0; + io->mem_fd = g_state.book_fd; + } else { + io->flags = 0; + io->host_pid = cb->host_pid ? cb->host_pid : getpid(); + io->mem_fd = -1; + } + + return 0; +} + +static int nds_build_one_iocb(const struct nds_io_cb *cb, struct p2p_io_param *param) +{ + int file_fd = cb->obj.fd; + int ret; + + if (cb->reserved[0] || cb->reserved[1] || cb->obj.reserved || + file_fd < 0) + return -EINVAL; + + ret = nds_iocb_to_io_param(cb, file_fd, param); + if (ret) + fprintf(stderr, "nds: translate iocb failed %d\n", ret); + return ret; +} + +int nds_io_submit(struct nds_io_ctx *ctx, int nr, + const struct nds_io_cb *iocb) +{ + struct p2p_io_batch_param batch = { 0 }; + struct p2p_io_param *items; + unsigned int built = 0; + int build_err = 0; + int ret; + + if (!ctx || !iocb) + return -EINVAL; + if (nr < 0) + return -EINVAL; + if (!nr) + return 0; + if ((unsigned int)nr > NDS_IO_MAX_IO_CNT) + return -EINVAL; + + items = calloc((size_t)nr, sizeof(*items)); + if (!items) + return -ENOMEM; + + /* + * Build the valid prefix in iocb order, then submit its independent + * requests through one ioctl. Translation and kernel acceptance are + * both fail-stop: return the accepted prefix, or -errno if item zero + * fails. + */ + while (built < (unsigned int)nr) { + build_err = nds_build_one_iocb(&iocb[built], &items[built]); + if (build_err) + break; + built++; + } + if (!built) { + ret = build_err; + goto out; + } + + batch.nr = built; + batch.items = (uint64_t)(uintptr_t)items; + ret = ioctl(ctx->p2p_fd, IOCTL_SUBMIT_IO, &batch); + if (ret < 0) { + ret = -errno; + fprintf(stderr, "nds: submit ioctl failed %d\n", ret); + } else if (!ret) { + ret = -EIO; + } + +out: + free(items); + return ret; +} + +int nds_io_getevents(struct nds_io_ctx *ctx, int min_nr, + int nr, struct nds_io_event *events, + struct timespec *timeout) +{ + struct p2p_getevents_param param = { 0 }; + int ret; + + if (!ctx) + return -EINVAL; + if (min_nr < 0 || nr < 0 || min_nr > nr) + return -EINVAL; + if (!nr) + return 0; + if (!events) + return -EINVAL; + + /* + * nds_io_event is binary-identical to p2p_io_event (48B); pass the + * caller's buffer straight to the ioctl — no intermediate copy. + */ + param.min_nr = min_nr; + param.max_nr = nr; + if (timeout) { + if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || + timeout->tv_nsec >= 1000000000L || + (uint64_t)timeout->tv_sec > + (uint64_t)(INT64_MAX - timeout->tv_nsec) / + UINT64_C(1000000000)) + return -EINVAL; + param.timeout_ns = (__s64)timeout->tv_sec * 1000000000LL + + timeout->tv_nsec; + } else { + param.timeout_ns = -1; + } + param.events = (uint64_t)(uintptr_t)events; + + ret = ioctl(ctx->p2p_fd, IOCTL_GET_IO_EVENTS, ¶m); + if (ret < 0) { + ret = -errno; + fprintf(stderr, "nds: getevents ioctl failed %d\n", ret); + return ret; + } + return ret; +} diff --git a/tools/lib/nds/file_p2p/nds_api.h b/tools/lib/nds/file_p2p/nds_api.h new file mode 100644 index 000000000000..07e12aa8af38 --- /dev/null +++ b/tools/lib/nds/file_p2p/nds_api.h @@ -0,0 +1,233 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NDS_API_H__ +#define __NDS_API_H__ + +#include <stdint.h> +#include <sys/types.h> +#include <time.h> /* struct timespec for nds_io_getevents */ + +/* + * NDS — userspace API for NPU HBM ↔ NVMe P2P I/O (GDS-like, async). + * + * Typical usage: + * + * 1. Initialize NDS, then register each filesystem as it becomes available: + * struct nds_init_param ip = { 0 }; + * int fds[] = { file_fd }; + * struct nds_fs_desc fs = { + * .fs_fd = fds, + * .fs_fd_cnt = 1, + * }; + * nds_init(&ip); + * assert(ip.version == P2P_UAPI_VERSION); + * nds_register_fs(&fs); + * Call nds_register_fs() again to add filesystems after initialization. + * Each device fd learns its NPU from the first buffer used with that fd. + * + * 2. (Optional) Register one or more HBM / CMB ranges: + * struct nds_mem_desc mem = { .ranges = ranges, .nr = nr }; + * nds_register_mem(&mem); + * Later I/Os that use those VAs set NDS_IO_F_REGISTERED_MEM; the kernel + * translates the vectors using the process-wide registration table. + * + * 3. Create an I/O context and submit work (Linux-AIO style): + * struct nds_io_ctx *ctx; + * nds_io_new_ctx(&(struct nds_io_ctx_param){ .max_io_cnt = N }, &ctx); + * struct nds_io_vec iov = { + * .buf_addr = ..., + * .buf_len = ..., + * }; + * + * struct nds_io_cb cb = { + * .opcode = NDS_IO_OP_PREAD, + * .obj = { .fd = file_fd }, + * .offset = ..., + * .iov = &iov, + * .iov_cnt = 1, + * .rw_flags = 0, // or NDS_IO_F_REGISTERED_MEM + * .host_pid = 0, // must be 0 with REGISTERED_MEM + * .user_data = cookie, // echoed in completion events + * }; + * nds_io_submit(ctx, 1, &cb); + * nds_io_getevents(ctx, 1, 1, events, NULL); + * + * 4. Tear down: destroy ctx → unregister mem → nds_exit(). + * + * Conventions: + * - All functions return 0 / positive count on success, or -errno on failure. + * - I/O is async only (no synchronous pread/pwrite helpers). + * - Targets are open file or block-device FDs. Paths are not accepted. + * - Offsets / lengths / buffer addresses must be sector-aligned (512 B); + * a regular-file write also needs filesystem-block alignment, and one + * logical I/O is limited to P2P_MAX_IO_BYTES. + * - reserved fields must be zero. + */ + +#define NDS_IO_MAX_IO_CNT 1024u +#define NDS_IO_MAX_IOV 65536u + +#define NDS_MEM_MAX_RANGES 8192u + +struct nds_io_ctx; + +struct nds_init_param { + uint32_t flags; /* must be 0 */ + uint32_t reserved1; /* must be 0 */ + uint32_t version; /* output: kernel UAPI version on success */ + uint32_t reserved2; /* must be 0 */ + uint64_t reserved3[4]; /* must be zero when unused */ +}; + +/* File, directory or block-device fds for filesystem and queue validation. */ +struct nds_fs_desc { + int32_t *fs_fd; + uint32_t fs_fd_cnt; + uint32_t reserved; +}; + +struct nds_mem_range { + uint64_t addr; + uint64_t size; +}; + +struct nds_mem_desc { + const struct nds_mem_range *ranges; + uint32_t nr; + uint32_t flags; /* must be 0 */ + uint64_t reserved[4]; /* must be zero */ +}; + +/* max_io_cnt: accepts 1..NDS_IO_MAX_IO_CNT as a queue-size hint but does + * not enforce an outstanding-I/O budget in the library. flags and reserved + * must be zero. */ +struct nds_io_ctx_param { + uint32_t max_io_cnt; + uint32_t flags; + uint64_t reserved; +}; + +/* I/O target: an open file or block-device fd. */ +struct nds_io_obj { + uint64_t reserved; /* must be zero */ + union { + uint64_t _data; + int32_t fd; /* open file / block-device fd */ + }; +}; + +#define NDS_IO_OP_PREAD 0u /* read one or more buffers */ +#define NDS_IO_OP_PWRITE 1u /* write one or more buffers */ + +/* Buffer was previously registered with nds_register_mem(). */ +#define NDS_IO_F_REGISTERED_MEM (1u << 0) +#define NDS_IO_F_MASK NDS_IO_F_REGISTERED_MEM + +struct nds_io_vec { + uint64_t buf_addr; + uint32_t buf_len; + uint32_t reserved; /* must be zero; layout matches p2p_iov */ +}; + +/* + * One async I/O request. Fill an array of these and pass to nds_io_submit(). + * + * iov / iov_cnt describe the HBM (or CMB) VA windows for this transfer. + * With NDS_IO_F_REGISTERED_MEM, iov[0] selects a region previously passed to + * nds_register_mem(); the kernel requires every vector to fit that + * range. Otherwise buffers are one-shot. + * + * offset is the byte offset in obj.fd; the kernel maps it to the underlying + * blocks (no FIEMAP / fallocate step in userspace). Filesystem alignment, + * CPU-access fallbacks and short transfers report errors in completion events. + * Callers must exclusively manage files and keep mappings stable through I/O. + */ +struct nds_io_cb { + uint32_t opcode; /* NDS_IO_OP_* */ + uint32_t rw_flags; /* NDS_IO_F_* bitmask */ + struct nds_io_obj obj; + uint64_t offset; /* byte offset in the target object */ + uint64_t user_data; /* opaque cookie, returned in events */ + const struct nds_io_vec *iov; + uint32_t iov_cnt; + /* + * For one-shot I/O, use 0 or the PID of the calling process. + * Must be 0 when NDS_IO_F_REGISTERED_MEM is set. + */ + int32_t host_pid; + uint64_t reserved[2]; /* must be zero */ +}; + +/* One completion. Layout matches kernel struct p2p_io_event (48B). + * res is success(==0) or -errno; reserved must be zero. */ +struct nds_io_event { + uint64_t user_data; + int64_t res; + uint64_t reserved[4]; +}; + +/* + * Process-wide initialization and teardown via /dev/p2p_device. + * Call nds_init() once from one thread; it is not thread-safe or re-entrant. + * Do not call nds_exit() concurrently with any NDS operation. + */ +int nds_init(struct nds_init_param *param); +int nds_exit(void); + +int nds_register_fs(const struct nds_fs_desc *desc); +int nds_unregister_fs(const struct nds_fs_desc *desc); + +/* + * Register ranges in one or more calls. One registered I/O may use any + * registered ranges. Each vector must fit in one range. An unregistered + * address or a vector that crosses a range boundary returns -ERANGE. + * + * Supply 1..NDS_MEM_MAX_RANGES per call. Flags and reserved fields must be zero. + * Ranges may be unsorted. Empty or overlapping input ranges return -EINVAL. + * Overlap with an existing registration returns -EEXIST. + * Unregister any subset of exact (addr, size) pairs in any order. The kernel + * waits for I/O using those ranges before releasing them. Keep allocations + * live until unregister or nds_exit returns. + */ +int nds_register_mem(const struct nds_mem_desc *desc); +int nds_unregister_mem(const struct nds_mem_desc *desc); + +/* + * Create / destroy a per-queue I/O context. Destroy closes the fd, waits for + * outstanding I/O and frees @ctx. The caller owns the pointer until destroy; + * double destroy (or any use after destroy) is undefined behavior. Do not call + * nds_io_getevents concurrently with destroy on the same ctx: the caller must + * stop all other uses of the context before destroy. + */ +int nds_io_new_ctx(const struct nds_io_ctx_param *param, + struct nds_io_ctx **ctx); +int nds_io_destroy_ctx(struct nds_io_ctx *ctx); + +/* + * Submit nr control blocks. Returns the number accepted, or -errno. + * + * Each iocb is one logical I/O: its iov[] is scatter-gather on a single + * obj.fd / offset and completes as one event (user_data). Different iocbs + * may target different files; the library submits the valid prefix as + * independent IOCTL_SUBMIT_IO items without cross-iocb merging. + * + * Fail-stop like Linux io_submit: on a mid-batch failure return the count + * of iocbs already accepted (positive); the failed iocb's errno is not + * returned. If nothing was accepted, return -errno. Accepted I/Os are not + * rolled back — reap them with nds_io_getevents(). + */ +int nds_io_submit(struct nds_io_ctx *ctx, int nr, + const struct nds_io_cb *iocb); + +/* + * Wait for at least min_nr completions, up to nr. + * timeout NULL = wait forever; {0,0} = non-blocking. + * On success returns the number of events filled (>= 0); on failure -errno. + * When no I/Os are outstanding, returns 0 immediately even if min_nr > 0 + * (does not block until timeout like Linux AIO on an empty ctx). + * Must not run concurrently with nds_io_destroy_ctx on the same ctx. + */ +int nds_io_getevents(struct nds_io_ctx *ctx, int min_nr, + int nr, struct nds_io_event *events, + struct timespec *timeout); + +#endif diff --git a/tools/lib/nds/file_p2p/nds_api_internal.h b/tools/lib/nds/file_p2p/nds_api_internal.h new file mode 100644 index 000000000000..793642b978a7 --- /dev/null +++ b/tools/lib/nds/file_p2p/nds_api_internal.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef NDS_API_INTERNAL_H_ +#define NDS_API_INTERNAL_H_ + +#include "nds_api.h" + +/* Private layout shared by the library bindings and white-box tests. */ +struct nds_io_ctx { + int p2p_fd; +}; + +#endif diff --git a/tools/lib/nds/file_p2p/p2p_common.c b/tools/lib/nds/file_p2p/p2p_common.c new file mode 100644 index 000000000000..32be2548fb69 --- /dev/null +++ b/tools/lib/nds/file_p2p/p2p_common.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> +#include <stdint.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/ioctl.h> + +#include <linux/nds_p2p.h> +#include "p2p_common.h" + +#define P2P_SECTOR_SIZE 512U +#define P2P_DEV_PATH "/dev/p2p_device" + +int p2p_open_dev(uint32_t *version_out) +{ + struct p2p_device_info info = { 0 }; + int dev_fd; + int err; + + dev_fd = open(P2P_DEV_PATH, O_RDWR | O_CLOEXEC); + if (dev_fd < 0) { + err = errno == ENOENT ? -ENODEV : -errno; + fprintf(stderr, "open %s failed, errno: %d\n", P2P_DEV_PATH, err); + return err; + } + + if (ioctl(dev_fd, IOCTL_GET_INFO, &info) < 0) { + err = errno == ENOTTY ? -EPROTONOSUPPORT : -errno; + goto close_fd; + } + if (info.version != P2P_UAPI_VERSION) { + fprintf(stderr, "%s: UAPI version %u.%u does not match expected %u.%u\n", + P2P_DEV_PATH, (info.version >> 8) & 0xffU, info.version & 0xffU, + P2P_UAPI_VERSION_MAJOR, P2P_UAPI_VERSION_MINOR); + err = -EPROTONOSUPPORT; + goto close_fd; + } + if (version_out) + *version_out = info.version; + return dev_fd; + +close_fd: + close(dev_fd); + return err; +} + +int p2p_get_iov_size(const struct p2p_iov *iov, unsigned int iov_nr, unsigned long *size_out) +{ + unsigned long size = 0; + unsigned int i; + + if (!iov || !iov_nr) + return -EINVAL; + + for (i = 0; i < iov_nr; i++) { + if (!iov[i].size || iov[i].reserved || + ((iov[i].addr | iov[i].size) & (P2P_SECTOR_SIZE - 1)) || + UINT64_MAX - iov[i].addr < iov[i].size || ULONG_MAX - size < iov[i].size) + return -EINVAL; + size += iov[i].size; + } + + *size_out = size; + return 0; +} diff --git a/tools/lib/nds/file_p2p/p2p_common.h b/tools/lib/nds/file_p2p/p2p_common.h new file mode 100644 index 000000000000..7288eeca6157 --- /dev/null +++ b/tools/lib/nds/file_p2p/p2p_common.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __P2P_COMMON_H__ +#define __P2P_COMMON_H__ + +#include <stdint.h> + +#include <linux/nds_p2p.h> + +int p2p_open_dev(uint32_t *version_out); + +/* Sum iov[].size with sector-alignment checks. Returns 0 or -errno. */ +int p2p_get_iov_size(const struct p2p_iov *iov, unsigned int iov_nr, unsigned long *size_out); + +#endif diff --git a/tools/lib/nds/file_p2p/py_nds_api.c b/tools/lib/nds/file_p2p/py_nds_api.c new file mode 100644 index 000000000000..4888b0533e3b --- /dev/null +++ b/tools/lib/nds/file_p2p/py_nds_api.c @@ -0,0 +1,577 @@ +// SPDX-License-Identifier: GPL-2.0 +#define PY_SSIZE_T_CLEAN +#include <Python.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <math.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <time.h> + +#include "nds_api_internal.h" +#include <linux/nds_p2p.h> + +/* + * Opaque ctx wrapper: Python holds the pointer as a PyCapsule named + * "nds_io_ctx". + */ +static const char *nds_ctx_capsule_name = "nds_io_ctx"; + +static void nds_ctx_capsule_destructor(PyObject *capsule) +{ + struct nds_io_ctx *ctx = PyCapsule_GetPointer(capsule, nds_ctx_capsule_name); + + if (ctx) + nds_io_destroy_ctx(ctx); +} + +static struct nds_io_ctx *nds_ctx_from_obj(PyObject *obj) +{ + struct nds_io_ctx *ctx; + + if (!PyCapsule_CheckExact(obj)) { + PyErr_SetString(PyExc_TypeError, "ctx must be an nds_io_ctx capsule"); + return NULL; + } + ctx = PyCapsule_GetPointer(obj, nds_ctx_capsule_name); + if (!ctx) + PyErr_SetString(PyExc_ValueError, "invalid or destroyed nds_io_ctx"); + return ctx; +} + +static int parse_nds_iov(PyObject *obj, struct nds_io_vec **iov_out, + uint32_t *iov_cnt_out) +{ + struct nds_io_vec *iov; + PyObject *seq; + Py_ssize_t count; + Py_ssize_t i; + + seq = PySequence_Fast(obj, "iov must be a sequence of (buf_addr, buf_len)"); + if (!seq) + return -1; + count = PySequence_Fast_GET_SIZE(seq); + if (count <= 0 || count > NDS_IO_MAX_IOV) { + Py_DECREF(seq); + PyErr_SetString(PyExc_ValueError, "iov count out of range"); + return -1; + } + iov = calloc((size_t)count, sizeof(*iov)); + if (!iov) { + Py_DECREF(seq); + PyErr_NoMemory(); + return -1; + } + for (i = 0; i < count; i++) { + unsigned long long addr; + unsigned int len; + + if (!PyArg_ParseTuple(PySequence_Fast_GET_ITEM(seq, i), "KI", + &addr, &len)) { + free(iov); + Py_DECREF(seq); + return -1; + } + iov[i].buf_addr = addr; + iov[i].buf_len = len; + } + Py_DECREF(seq); + *iov_out = iov; + *iov_cnt_out = (uint32_t)count; + return 0; +} + +static int py_parse_fs_desc(PyObject *fd_seq, struct nds_fs_desc *desc, + int32_t **fds_out) +{ + PyObject *seq = NULL; + int32_t *fds = NULL; + Py_ssize_t count; + Py_ssize_t i; + + seq = PySequence_Fast(fd_seq, "fs_fds must be a sequence of ints"); + if (!seq) + return -1; + count = PySequence_Fast_GET_SIZE(seq); + if (count < 0 || count > UINT32_MAX) { + Py_DECREF(seq); + PyErr_SetString(PyExc_ValueError, "fs_fds count out of range"); + return -1; + } + fds = calloc((size_t)(count ? count : 1), sizeof(*fds)); + if (!fds) { + Py_DECREF(seq); + PyErr_NoMemory(); + return -1; + } + for (i = 0; i < count; i++) { + long v = PyLong_AsLong(PySequence_Fast_GET_ITEM(seq, i)); + + if (PyErr_Occurred() || v < 0 || v > INT32_MAX) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ValueError, + "invalid fd in fs_fds"); + free(fds); + Py_DECREF(seq); + return -1; + } + fds[i] = (int32_t)v; + } + Py_DECREF(seq); + + desc->fs_fd = fds; + desc->fs_fd_cnt = (uint32_t)count; + desc->reserved = 0; + *fds_out = fds; + return 0; +} + +static PyObject *py_nds_init(PyObject *Py_UNUSED(self), PyObject *args) +{ + struct nds_init_param param = { }; + int ret; + + if (!PyArg_ParseTuple(args, "")) + return NULL; + + Py_BEGIN_ALLOW_THREADS + ret = nds_init(¶m); + Py_END_ALLOW_THREADS + return PyLong_FromLong(ret); +} + +static PyObject *py_nds_register_fs(PyObject *Py_UNUSED(self), PyObject *args) +{ + struct nds_fs_desc desc = { }; + PyObject *fd_seq; + int32_t *fds; + int ret; + + if (!PyArg_ParseTuple(args, "O", &fd_seq)) + return NULL; + if (py_parse_fs_desc(fd_seq, &desc, &fds)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + ret = nds_register_fs(&desc); + Py_END_ALLOW_THREADS + free(fds); + return PyLong_FromLong(ret); +} + +static PyObject *py_nds_unregister_fs(PyObject *Py_UNUSED(self), PyObject *args) +{ + PyObject *ignored; + + if (!PyArg_ParseTuple(args, "O", &ignored)) + return NULL; + return PyLong_FromLong(nds_unregister_fs(NULL)); +} + +static PyObject *py_nds_exit(PyObject *Py_UNUSED(self), + PyObject *Py_UNUSED(ignored)) +{ + int ret; + + Py_BEGIN_ALLOW_THREADS + ret = nds_exit(); + Py_END_ALLOW_THREADS + return PyLong_FromLong(ret); +} + +static int parse_nds_mem_ranges(PyObject *obj, struct nds_mem_range **ranges_out, + uint32_t *nr_out) +{ + struct nds_mem_range *ranges; + PyObject *seq; + Py_ssize_t count; + Py_ssize_t i; + + seq = PySequence_Fast(obj, "ranges must be a sequence of (addr, size)"); + if (!seq) + return -1; + count = PySequence_Fast_GET_SIZE(seq); + if (count <= 0 || count > NDS_MEM_MAX_RANGES) { + Py_DECREF(seq); + PyErr_SetString(PyExc_ValueError, "range count out of range"); + return -1; + } + ranges = calloc((size_t)count, sizeof(*ranges)); + if (!ranges) { + Py_DECREF(seq); + PyErr_NoMemory(); + return -1; + } + for (i = 0; i < count; i++) { + unsigned long long addr; + unsigned long long size; + + if (!PyArg_ParseTuple(PySequence_Fast_GET_ITEM(seq, i), "KK", + &addr, &size)) { + free(ranges); + Py_DECREF(seq); + return -1; + } + ranges[i].addr = addr; + ranges[i].size = size; + } + Py_DECREF(seq); + *ranges_out = ranges; + *nr_out = (uint32_t)count; + return 0; +} + +static PyObject *py_nds_register_mem(PyObject *Py_UNUSED(self), PyObject *args) +{ + struct nds_mem_range *ranges; + PyObject *ranges_py; + uint32_t nr; + int flags = 0; + int ret; + + if (!PyArg_ParseTuple(args, "O|i", &ranges_py, &flags)) + return NULL; + if (parse_nds_mem_ranges(ranges_py, &ranges, &nr)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + ret = nds_register_mem(&(struct nds_mem_desc){ + .ranges = ranges, .nr = nr, .flags = flags, + }); + Py_END_ALLOW_THREADS + free(ranges); + return PyLong_FromLong(ret); +} + +static PyObject *py_nds_unregister_mem(PyObject *Py_UNUSED(self), PyObject *args) +{ + struct nds_mem_range *ranges; + PyObject *ranges_py; + uint32_t nr; + int flags = 0; + int ret; + + if (!PyArg_ParseTuple(args, "O|i", &ranges_py, &flags)) + return NULL; + if (parse_nds_mem_ranges(ranges_py, &ranges, &nr)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + ret = nds_unregister_mem(&(struct nds_mem_desc){ + .ranges = ranges, .nr = nr, .flags = flags, + }); + Py_END_ALLOW_THREADS + free(ranges); + return PyLong_FromLong(ret); +} + +static PyObject *py_nds_io_new_ctx(PyObject *Py_UNUSED(self), PyObject *args) +{ + struct nds_io_ctx_param param = { }; + struct nds_io_ctx *ctx = NULL; + unsigned int max_io_cnt; + unsigned int flags = 0; + int ret; + + if (!PyArg_ParseTuple(args, "I|I", &max_io_cnt, &flags)) + return NULL; + param.max_io_cnt = max_io_cnt; + param.flags = flags; + + Py_BEGIN_ALLOW_THREADS + ret = nds_io_new_ctx(¶m, &ctx); + Py_END_ALLOW_THREADS + if (ret) + return PyLong_FromLong(ret); + + return PyCapsule_New(ctx, nds_ctx_capsule_name, nds_ctx_capsule_destructor); +} + +static PyObject *py_nds_io_destroy_ctx(PyObject *Py_UNUSED(self), PyObject *args) +{ + PyObject *capsule; + struct nds_io_ctx *ctx; + int ret; + + if (!PyArg_ParseTuple(args, "O", &capsule)) + return NULL; + ctx = nds_ctx_from_obj(capsule); + if (!ctx) + return NULL; + + /* Disable auto-destroy; this call owns the teardown. */ + if (PyCapsule_SetDestructor(capsule, NULL) < 0) + return NULL; + + Py_BEGIN_ALLOW_THREADS + ret = nds_io_destroy_ctx(ctx); + Py_END_ALLOW_THREADS + + /* Invalidate capsule so later use fails cleanly. */ + if (PyCapsule_SetName(capsule, "nds_io_ctx_destroyed") < 0) + return NULL; + return PyLong_FromLong(ret); +} + +static PyObject *py_nds_io_dup_fd_for_test(PyObject *Py_UNUSED(self), + PyObject *args) +{ + PyObject *capsule; + struct nds_io_ctx *ctx; + int fd; + + if (!PyArg_ParseTuple(args, "O", &capsule)) + return NULL; + ctx = nds_ctx_from_obj(capsule); + if (!ctx) + return NULL; + + fd = fcntl(ctx->p2p_fd, F_DUPFD_CLOEXEC, 0); + return PyLong_FromLong(fd < 0 ? -errno : fd); +} + +static PyObject *py_nds_io_submit(PyObject *Py_UNUSED(self), PyObject *args) +{ + PyObject *capsule; + PyObject *iocb_seq; + PyObject *seq; + struct nds_io_ctx *ctx; + struct nds_io_cb *cbs = NULL; + struct nds_io_vec **iov = NULL; + Py_ssize_t count; + Py_ssize_t i; + int ret; + + if (!PyArg_ParseTuple(args, "OO", &capsule, &iocb_seq)) + return NULL; + ctx = nds_ctx_from_obj(capsule); + if (!ctx) + return NULL; + + seq = PySequence_Fast(iocb_seq, "iocbs must be a sequence"); + if (!seq) + return NULL; + count = PySequence_Fast_GET_SIZE(seq); + if (count < 0 || count > INT_MAX) { + Py_DECREF(seq); + PyErr_SetString(PyExc_ValueError, "iocb count out of range"); + return NULL; + } + if (!count) { + Py_DECREF(seq); + return PyLong_FromLong(0); + } + + cbs = calloc((size_t)count, sizeof(*cbs)); + iov = calloc((size_t)count, sizeof(*iov)); + if (!cbs || !iov) { + free(cbs); + free(iov); + Py_DECREF(seq); + return PyErr_NoMemory(); + } + + for (i = 0; i < count; i++) { + PyObject *item = PySequence_Fast_GET_ITEM(seq, i); + PyObject *fd_py; + PyObject *iov_py; + unsigned int opcode; + unsigned int rw_flags = 0; + unsigned long long offset; + unsigned long long user_data = 0; + long long fd; + int host_pid = 0; + + /* + * iocb tuple: + * (opcode, fd, offset, iov + * [, rw_flags=0, host_pid=0, user_data=0]) + * iov: sequence of (buf_addr, buf_len) + */ + if (!PyArg_ParseTuple(item, "IOKO|IiK", + &opcode, &fd_py, &offset, &iov_py, + &rw_flags, &host_pid, + &user_data)) { + goto parse_error; + } + fd = PyLong_AsLongLong(fd_py); + if (PyErr_Occurred()) + goto parse_error; + if (fd < 0 || fd > INT32_MAX) { + PyErr_SetString(PyExc_ValueError, "fd out of range"); + goto parse_error; + } + if (parse_nds_iov(iov_py, &iov[i], &cbs[i].iov_cnt)) + goto parse_error; + cbs[i].opcode = opcode; + cbs[i].rw_flags = rw_flags; + cbs[i].obj.fd = (int32_t)fd; + cbs[i].offset = offset; + cbs[i].user_data = user_data; + cbs[i].iov = iov[i]; + cbs[i].host_pid = (int32_t)host_pid; + } + Py_DECREF(seq); + + Py_BEGIN_ALLOW_THREADS + ret = nds_io_submit(ctx, (int)count, cbs); + Py_END_ALLOW_THREADS + + for (i = 0; i < count; i++) + free(iov[i]); + free(iov); + free(cbs); + return PyLong_FromLong(ret); + +parse_error: + for (i = 0; i < count; i++) + free(iov[i]); + free(iov); + free(cbs); + Py_DECREF(seq); + return NULL; +} + +static PyObject *py_nds_io_getevents(PyObject *Py_UNUSED(self), PyObject *args) +{ + PyObject *capsule; + struct nds_io_ctx *ctx; + struct nds_io_event *events = NULL; + struct timespec ts_storage; + struct timespec *timeout = NULL; + int min_nr; + int nr; + double timeout_sec = -1.0; + PyObject *list; + PyThreadState *thread_state; + int ret; + int i; + + if (!PyArg_ParseTuple(args, "Oii|d", &capsule, &min_nr, &nr, &timeout_sec)) + return NULL; + ctx = nds_ctx_from_obj(capsule); + if (!ctx) + return NULL; + if (min_nr < 0 || nr < 0 || min_nr > nr) + return PyLong_FromLong(-EINVAL); + if (!nr) + return PyList_New(0); + if (!isfinite(timeout_sec) || + (timeout_sec >= 0.0 && + timeout_sec > (double)INT64_MAX / 1000000000.0)) + return PyLong_FromLong(-EINVAL); + + events = calloc((size_t)nr, sizeof(*events)); + if (!events) + return PyErr_NoMemory(); + + if (timeout_sec >= 0.0) { + ts_storage.tv_sec = (time_t)timeout_sec; + ts_storage.tv_nsec = + (long)((timeout_sec - (double)ts_storage.tv_sec) * + 1000000000.0); + timeout = &ts_storage; + } + + thread_state = PyEval_SaveThread(); + ret = nds_io_getevents(ctx, min_nr, nr, events, timeout); + PyEval_RestoreThread(thread_state); + + if (ret < 0) { + free(events); + return PyLong_FromLong(ret); + } + + list = PyList_New(ret); + if (!list) { + free(events); + return NULL; + } + for (i = 0; i < ret; i++) { + PyObject *tup; + + tup = Py_BuildValue("(KL)", + (unsigned long long)events[i].user_data, + (long long)events[i].res); + if (!tup) { + Py_DECREF(list); + free(events); + return NULL; + } + PyList_SET_ITEM(list, i, tup); + } + free(events); + return list; +} + +static PyMethodDef NdsMethods[] = { + { "init", py_nds_init, METH_VARARGS, + "init() -> int\n" + "Open /dev/p2p_device. Each fd learns its NPU from its first buffer.\n" }, + { "exit", py_nds_exit, METH_NOARGS, + "exit() -> int\n" }, + { "register_fs", py_nds_register_fs, METH_VARARGS, + "register_fs(fs_fds) -> int\n" + "fs_fds: file/block fds to validate; no state is stored\n" }, + { "unregister_fs", py_nds_unregister_fs, METH_VARARGS, + "unregister_fs(fs_fds) -> int\n" + "Compatibility no-op.\n" }, + { "register_mem", py_nds_register_mem, METH_VARARGS, + "register_mem(ranges[, flags=0]) -> int\n" + "ranges: sequence of (addr, size) to add to the registration table\n" }, + { "unregister_mem", py_nds_unregister_mem, METH_VARARGS, + "unregister_mem(ranges[, flags=0]) -> int\n" + "ranges: any subset of exact registered (addr, size) pairs, in any order.\n" }, + { "io_new_ctx", py_nds_io_new_ctx, METH_VARARGS, + "io_new_ctx(max_io_cnt[, flags=0]) -> ctx capsule | negative errno\n" }, + { "io_destroy_ctx", py_nds_io_destroy_ctx, METH_VARARGS, + "io_destroy_ctx(ctx) -> int\n" }, + { "_io_dup_fd_for_test", py_nds_io_dup_fd_for_test, METH_VARARGS, + "Internal test hook: duplicate the context fd.\n" }, + { "io_submit", py_nds_io_submit, METH_VARARGS, + "io_submit(ctx, iocbs) -> int\n\n" + "iocb: (opcode, fd, offset, iov" + "[, rw_flags=0, host_pid=0, user_data=0])\n" + "iov: sequence of (buf_addr, buf_len)\n" }, + { "io_getevents", py_nds_io_getevents, METH_VARARGS, + "io_getevents(ctx, min_nr, nr[, timeout_sec=-1]) -> list|int\n\n" + "On success returns [(user_data, res), ...].\n" + "On failure returns negative errno.\n" + "timeout_sec must be finite; < 0 waits forever and 0 is non-blocking.\n" }, + { NULL, NULL, 0, NULL } +}; + +static struct PyModuleDef nds_module = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "nds", + .m_doc = "NDS GDS-aligned P2P API", + .m_size = -1, + .m_methods = NdsMethods, +}; + +PyMODINIT_FUNC PyInit_nds(void) +{ + PyObject *module; + + module = PyModule_Create(&nds_module); + if (!module) + return NULL; + + if (PyModule_AddIntConstant(module, "P2P_UAPI_VERSION", P2P_UAPI_VERSION) || + PyModule_AddIntConstant(module, "NDS_IO_MAX_IO_CNT", + NDS_IO_MAX_IO_CNT) || + PyModule_AddIntConstant(module, "NDS_IO_MAX_IOV", NDS_IO_MAX_IOV) || + PyModule_AddIntConstant(module, "NDS_MEM_MAX_RANGES", + NDS_MEM_MAX_RANGES) || + PyModule_AddIntConstant(module, "NDS_IO_OP_PREAD", NDS_IO_OP_PREAD) || + PyModule_AddIntConstant(module, "NDS_IO_OP_PWRITE", NDS_IO_OP_PWRITE) || + PyModule_AddIntConstant(module, "NDS_IO_F_REGISTERED_MEM", + NDS_IO_F_REGISTERED_MEM)) { + Py_DECREF(module); + return NULL; + } + return module; +} diff --git a/tools/lib/nds/file_p2p/setup.py b/tools/lib/nds/file_p2p/setup.py new file mode 100644 index 000000000000..82549bcb384e --- /dev/null +++ b/tools/lib/nds/file_p2p/setup.py @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0 +from setuptools import setup, Extension + +nds_module = Extension( + "nds", + sources=["nds_api.c", "py_nds_api.c", "p2p_common.c"], + depends=[ + "nds_api.h", + "nds_api_internal.h", + "../../../include/uapi/linux/nds_p2p.h", + "p2p_common.h", + ], + extra_compile_args=["-Wall", "-Wextra", "-pthread", "-I../../../include/uapi"], + extra_link_args=["-pthread"], +) + +setup( + name="nds", + version="1.0", + ext_modules=[nds_module], +) -- 2.52.0