Skip to content

Commit 8908192

Browse files
isilenceaxboe
authored andcommitted
io_uring/bpf-ops: add kfunc helpers
Add two kfuncs that should cover most of the needs: 1. bpf_io_uring_submit_sqes(), which allows to submit io_uring requests. It mirrors the normal user space submission path and follows all related io_uring_enter(2) rules. i.e. SQEs are taken from the SQ according to head/tail values. In case of IORING_SETUP_SQ_REWIND, it'll submit first N entries. 2. bpf_io_uring_get_region() returns a pointer to the specified region, where io_uring regions are kernel-userspace shared chunks of memory. It takes the size as an argument, which should be a load time constant. There are 3 types of regions: - IOU_REGION_SQ returns the submission queue. - IOU_REGION_CQ stores the CQ, SQ/CQ headers and the sqarray. In other words, it gives same memory that would normally be mmap'ed with IORING_FEAT_SINGLE_MMAP enabled IORING_OFF_SQ_RING. - IOU_REGION_MEM represents the memory / parameter region. It can be used to store request indirect parameters and for kernel - user communication. It intentionally provides a thin but flexible API and expects BPF programs to implement CQ/SQ header parsing, CQ walking, etc. That mirrors how the normal user space works with rings and should help to minimise kernel / kfunc helpers changes while introducing new generic io_uring features. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://patch.msgid.link/967bcc10e94c796eb273998621551b2a21848cde.1772109579.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d0e437b commit 8908192

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

io_uring/bpf-ops.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,58 @@
55

66
#include "io_uring.h"
77
#include "register.h"
8+
#include "memmap.h"
89
#include "bpf-ops.h"
910
#include "loop.h"
1011

1112
static const struct btf_type *loop_params_type;
1213

14+
__bpf_kfunc_start_defs();
15+
16+
__bpf_kfunc int bpf_io_uring_submit_sqes(struct io_ring_ctx *ctx, u32 nr)
17+
{
18+
return io_submit_sqes(ctx, nr);
19+
}
20+
21+
__bpf_kfunc
22+
__u8 *bpf_io_uring_get_region(struct io_ring_ctx *ctx, __u32 region_id,
23+
const size_t rdwr_buf_size)
24+
{
25+
struct io_mapped_region *r;
26+
27+
lockdep_assert_held(&ctx->uring_lock);
28+
29+
switch (region_id) {
30+
case IOU_REGION_MEM:
31+
r = &ctx->param_region;
32+
break;
33+
case IOU_REGION_CQ:
34+
r = &ctx->ring_region;
35+
break;
36+
case IOU_REGION_SQ:
37+
r = &ctx->sq_region;
38+
break;
39+
default:
40+
return NULL;
41+
}
42+
43+
if (unlikely(rdwr_buf_size > io_region_size(r)))
44+
return NULL;
45+
return io_region_get_ptr(r);
46+
}
47+
48+
__bpf_kfunc_end_defs();
49+
50+
BTF_KFUNCS_START(io_uring_kfunc_set)
51+
BTF_ID_FLAGS(func, bpf_io_uring_submit_sqes, KF_SLEEPABLE);
52+
BTF_ID_FLAGS(func, bpf_io_uring_get_region, KF_RET_NULL);
53+
BTF_KFUNCS_END(io_uring_kfunc_set)
54+
55+
static const struct btf_kfunc_id_set bpf_io_uring_kfunc_set = {
56+
.owner = THIS_MODULE,
57+
.set = &io_uring_kfunc_set,
58+
};
59+
1360
static int io_bpf_ops__loop_step(struct io_ring_ctx *ctx,
1461
struct iou_loop_params *lp)
1562
{
@@ -68,12 +115,20 @@ io_lookup_struct_type(struct btf *btf, const char *name)
68115

69116
static int bpf_io_init(struct btf *btf)
70117
{
118+
int ret;
119+
71120
loop_params_type = io_lookup_struct_type(btf, "iou_loop_params");
72121
if (!loop_params_type) {
73122
pr_err("io_uring: Failed to locate iou_loop_params\n");
74123
return -EINVAL;
75124
}
76125

126+
ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
127+
&bpf_io_uring_kfunc_set);
128+
if (ret) {
129+
pr_err("io_uring: Failed to register kfuncs (%d)\n", ret);
130+
return ret;
131+
}
77132
return 0;
78133
}
79134

io_uring/bpf-ops.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
#include <linux/io_uring_types.h>
66

7+
enum {
8+
IOU_REGION_MEM,
9+
IOU_REGION_CQ,
10+
IOU_REGION_SQ,
11+
};
12+
713
struct io_uring_bpf_ops {
814
int (*loop_step)(struct io_ring_ctx *ctx, struct iou_loop_params *lp);
915

0 commit comments

Comments
 (0)