Allow BPF to implement the loop_step callback to overwrite the main loop logic. As described in the patch introducing the callback, it receives iou_loop_params as an argument, which BPF can directly use to control the loop. Signed-off-by: Pavel Begunkov --- io_uring/bpf-ops.c | 36 ++++++++++++++++++++++++++++++++++++ io_uring/bpf-ops.h | 4 ++++ 2 files changed, 40 insertions(+) diff --git a/io_uring/bpf-ops.c b/io_uring/bpf-ops.c index a89d1dea60c7..7db07eda5a48 100644 --- a/io_uring/bpf-ops.c +++ b/io_uring/bpf-ops.c @@ -1,11 +1,22 @@ #include #include +#include #include "io_uring.h" #include "register.h" #include "bpf-ops.h" +#include "loop.h" + +static const struct btf_type *loop_params_type; + +static int io_bpf_ops__loop_step(struct io_ring_ctx *ctx, + struct iou_loop_params *lp) +{ + return IOU_LOOP_STOP; +} static struct io_uring_bpf_ops io_bpf_ops_stubs = { + .loop_step = io_bpf_ops__loop_step, }; static bool bpf_io_is_valid_access(int off, int size, @@ -27,6 +38,14 @@ static int bpf_io_btf_struct_access(struct bpf_verifier_log *log, const struct bpf_reg_state *reg, int off, int size) { + const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); + + if (t == loop_params_type) { + if (off >= offsetof(struct iou_loop_params, cq_wait_idx) && + off + size <= offsetofend(struct iou_loop_params, cq_wait_idx)) + return SCALAR_VALUE; + } + return -EACCES; } @@ -36,8 +55,25 @@ static const struct bpf_verifier_ops bpf_io_verifier_ops = { .btf_struct_access = bpf_io_btf_struct_access, }; +static const struct btf_type * +io_lookup_struct_type(struct btf *btf, const char *name) +{ + s32 type_id; + + type_id = btf_find_by_name_kind(btf, name, BTF_KIND_STRUCT); + if (type_id < 0) + return NULL; + return btf_type_by_id(btf, type_id); +} + static int bpf_io_init(struct btf *btf) { + loop_params_type = io_lookup_struct_type(btf, "iou_loop_params"); + if (!loop_params_type) { + pr_err("io_uring: Failed to locate iou_loop_params\n"); + return -EINVAL; + } + return 0; } diff --git a/io_uring/bpf-ops.h b/io_uring/bpf-ops.h index a6756b391387..e8a08ae2df0a 100644 --- a/io_uring/bpf-ops.h +++ b/io_uring/bpf-ops.h @@ -5,6 +5,10 @@ #include struct io_uring_bpf_ops { + int (*loop_step)(struct io_ring_ctx *ctx, struct iou_loop_params *lp); + + __u32 ring_fd; + void *priv; }; #endif /* IOU_BPF_OPS_H */ -- 2.52.0