Add some basic helpers and definitions for implementing bpf struct_ops. There are no callbaack yet, and registration will always fail. Signed-off-by: Pavel Begunkov --- include/linux/io_uring_types.h | 4 ++ io_uring/Kconfig | 5 ++ io_uring/Makefile | 1 + io_uring/bpf.c | 93 ++++++++++++++++++++++++++++++++++ io_uring/bpf.h | 21 ++++++++ io_uring/io_uring.c | 2 + 6 files changed, 126 insertions(+) create mode 100644 io_uring/bpf.c create mode 100644 io_uring/bpf.h diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index ef1af730193a..43432a06d177 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -8,6 +8,8 @@ #include #include +struct io_uring_ops; + enum { /* * A hint to not wake right away but delay until there are enough of @@ -276,6 +278,8 @@ struct io_ring_ctx { struct io_rings *rings; struct percpu_ref refs; + struct io_uring_ops *bpf_ops; + clockid_t clockid; enum tk_offsets clock_offset; diff --git a/io_uring/Kconfig b/io_uring/Kconfig index 4b949c42c0bf..b4dad9b74544 100644 --- a/io_uring/Kconfig +++ b/io_uring/Kconfig @@ -9,3 +9,8 @@ config IO_URING_ZCRX depends on PAGE_POOL depends on INET depends on NET_RX_BUSY_POLL + +config IO_URING_BPF + def_bool y + depends on IO_URING + depends on BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF diff --git a/io_uring/Makefile b/io_uring/Makefile index bc4e4a3fa0a5..35eeeaf64489 100644 --- a/io_uring/Makefile +++ b/io_uring/Makefile @@ -22,3 +22,4 @@ obj-$(CONFIG_NET_RX_BUSY_POLL) += napi.o obj-$(CONFIG_NET) += net.o cmd_net.o obj-$(CONFIG_PROC_FS) += fdinfo.o obj-$(CONFIG_IO_URING_MOCK_FILE) += mock_file.o +obj-$(CONFIG_IO_URING_BPF) += bpf.o diff --git a/io_uring/bpf.c b/io_uring/bpf.c new file mode 100644 index 000000000000..4cb5d25c9247 --- /dev/null +++ b/io_uring/bpf.c @@ -0,0 +1,93 @@ +#include + +#include "bpf.h" +#include "register.h" + +static struct io_uring_ops io_bpf_ops_stubs = { +}; + +static bool bpf_io_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + if (type != BPF_READ) + return false; + if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS) + return false; + if (off % size != 0) + return false; + + return btf_ctx_access(off, size, type, prog, info); +} + +static int bpf_io_btf_struct_access(struct bpf_verifier_log *log, + const struct bpf_reg_state *reg, int off, + int size) +{ + return -EACCES; +} + +static const struct bpf_verifier_ops bpf_io_verifier_ops = { + .get_func_proto = bpf_base_func_proto, + .is_valid_access = bpf_io_is_valid_access, + .btf_struct_access = bpf_io_btf_struct_access, +}; + +static int bpf_io_init(struct btf *btf) +{ + return 0; +} + +static int bpf_io_check_member(const struct btf_type *t, + const struct btf_member *member, + const struct bpf_prog *prog) +{ + return 0; +} + +static int bpf_io_init_member(const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + return 0; +} + +static int bpf_io_reg(void *kdata, struct bpf_link *link) +{ + return -EOPNOTSUPP; +} + +static void bpf_io_unreg(void *kdata, struct bpf_link *link) +{ +} + +void io_unregister_bpf(struct io_ring_ctx *ctx) +{ +} + +static struct bpf_struct_ops bpf_io_uring_ops = { + .verifier_ops = &bpf_io_verifier_ops, + .reg = bpf_io_reg, + .unreg = bpf_io_unreg, + .check_member = bpf_io_check_member, + .init_member = bpf_io_init_member, + .init = bpf_io_init, + .cfi_stubs = &io_bpf_ops_stubs, + .name = "io_uring_ops", + .owner = THIS_MODULE, +}; + +static int __init io_uring_bpf_init(void) +{ + int ret; + + ret = register_bpf_struct_ops(&bpf_io_uring_ops, io_uring_ops); + if (ret) { + pr_err("io_uring: Failed to register struct_ops (%d)\n", ret); + return ret; + } + + return 0; +} +__initcall(io_uring_bpf_init); diff --git a/io_uring/bpf.h b/io_uring/bpf.h new file mode 100644 index 000000000000..34a51a57103d --- /dev/null +++ b/io_uring/bpf.h @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef IOU_BPF_H +#define IOU_BPF_H + +#include +#include + +#include "io_uring.h" + +struct io_uring_ops { +}; + +#ifdef CONFIG_IO_URING_BPF +void io_unregister_bpf(struct io_ring_ctx *ctx); +#else +static inline void io_unregister_bpf(struct io_ring_ctx *ctx) +{ +} +#endif + +#endif diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 29f34fbcbb01..5b80987ebb2c 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -99,6 +99,7 @@ #include "msg_ring.h" #include "memmap.h" #include "zcrx.h" +#include "bpf.h" #include "timeout.h" #include "poll.h" @@ -2830,6 +2831,7 @@ static __cold void io_req_caches_free(struct io_ring_ctx *ctx) static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) { io_sq_thread_finish(ctx); + io_unregister_bpf(ctx); mutex_lock(&ctx->uring_lock); io_sqe_buffers_unregister(ctx); -- 2.49.0