Skip to content

Commit 98f3763

Browse files
isilenceaxboe
authored andcommitted
io_uring/bpf-ops: implement bpf ops registration
Implement BPF struct ops registration. It's registered off the BPF path, and can be removed by BPF as well as io_uring. To protect it, introduce a global lock synchronising registration. ctx->uring_lock can be nested under it. ctx->bpf_ops is write protected by both locks and so it's safe to read it under either of them. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://patch.msgid.link/1f46bffd76008de49cbafa2ad77d348810a4f69e.1772109579.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 8908192 commit 98f3763

4 files changed

Lines changed: 104 additions & 2 deletions

File tree

include/linux/io_uring_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <linux/llist.h>
99
#include <uapi/linux/io_uring.h>
1010

11+
struct iou_loop_params;
12+
struct io_uring_bpf_ops;
13+
1114
enum {
1215
/*
1316
* A hint to not wake right away but delay until there are enough of
@@ -488,6 +491,8 @@ struct io_ring_ctx {
488491
DECLARE_HASHTABLE(napi_ht, 4);
489492
#endif
490493

494+
struct io_uring_bpf_ops *bpf_ops;
495+
491496
/*
492497
* Protection for resize vs mmap races - both the mmap and resize
493498
* side will need to grab this lock, to prevent either side from

io_uring/bpf-ops.c

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

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

12+
static DEFINE_MUTEX(io_bpf_ctrl_mutex);
1213
static const struct btf_type *loop_params_type;
1314

1415
__bpf_kfunc_start_defs();
@@ -143,16 +144,103 @@ static int bpf_io_init_member(const struct btf_type *t,
143144
const struct btf_member *member,
144145
void *kdata, const void *udata)
145146
{
147+
u32 moff = __btf_member_bit_offset(t, member) / 8;
148+
const struct io_uring_bpf_ops *uops = udata;
149+
struct io_uring_bpf_ops *ops = kdata;
150+
151+
switch (moff) {
152+
case offsetof(struct io_uring_bpf_ops, ring_fd):
153+
ops->ring_fd = uops->ring_fd;
154+
return 1;
155+
}
156+
return 0;
157+
}
158+
159+
static int io_install_bpf(struct io_ring_ctx *ctx, struct io_uring_bpf_ops *ops)
160+
{
161+
if (ctx->flags & (IORING_SETUP_SQPOLL | IORING_SETUP_IOPOLL))
162+
return -EOPNOTSUPP;
163+
if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
164+
return -EOPNOTSUPP;
165+
166+
if (ctx->bpf_ops)
167+
return -EBUSY;
168+
if (WARN_ON_ONCE(!ops->loop_step))
169+
return -EINVAL;
170+
171+
ops->priv = ctx;
172+
ctx->bpf_ops = ops;
173+
ctx->loop_step = ops->loop_step;
146174
return 0;
147175
}
148176

149177
static int bpf_io_reg(void *kdata, struct bpf_link *link)
150178
{
151-
return -EOPNOTSUPP;
179+
struct io_uring_bpf_ops *ops = kdata;
180+
struct io_ring_ctx *ctx;
181+
struct file *file;
182+
int ret = -EBUSY;
183+
184+
file = io_uring_register_get_file(ops->ring_fd, false);
185+
if (IS_ERR(file))
186+
return PTR_ERR(file);
187+
ctx = file->private_data;
188+
189+
scoped_guard(mutex, &io_bpf_ctrl_mutex) {
190+
guard(mutex)(&ctx->uring_lock);
191+
ret = io_install_bpf(ctx, ops);
192+
}
193+
194+
fput(file);
195+
return ret;
196+
}
197+
198+
static void io_eject_bpf(struct io_ring_ctx *ctx)
199+
{
200+
struct io_uring_bpf_ops *ops = ctx->bpf_ops;
201+
202+
if (WARN_ON_ONCE(!ops))
203+
return;
204+
if (WARN_ON_ONCE(ops->priv != ctx))
205+
return;
206+
207+
ops->priv = NULL;
208+
ctx->bpf_ops = NULL;
209+
ctx->loop_step = NULL;
152210
}
153211

154212
static void bpf_io_unreg(void *kdata, struct bpf_link *link)
155213
{
214+
struct io_uring_bpf_ops *ops = kdata;
215+
struct io_ring_ctx *ctx;
216+
217+
guard(mutex)(&io_bpf_ctrl_mutex);
218+
ctx = ops->priv;
219+
if (ctx) {
220+
guard(mutex)(&ctx->uring_lock);
221+
if (WARN_ON_ONCE(ctx->bpf_ops != ops))
222+
return;
223+
224+
io_eject_bpf(ctx);
225+
}
226+
}
227+
228+
void io_unregister_bpf_ops(struct io_ring_ctx *ctx)
229+
{
230+
/*
231+
* ->bpf_ops is write protected by io_bpf_ctrl_mutex and uring_lock,
232+
* and read protected by either. Try to avoid taking the global lock
233+
* for rings that never had any bpf installed.
234+
*/
235+
scoped_guard(mutex, &ctx->uring_lock) {
236+
if (!ctx->bpf_ops)
237+
return;
238+
}
239+
240+
guard(mutex)(&io_bpf_ctrl_mutex);
241+
guard(mutex)(&ctx->uring_lock);
242+
if (ctx->bpf_ops)
243+
io_eject_bpf(ctx);
156244
}
157245

158246
static struct bpf_struct_ops bpf_ring_ops = {

io_uring/bpf-ops.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ struct io_uring_bpf_ops {
1717
void *priv;
1818
};
1919

20+
#ifdef CONFIG_IO_URING_BPF_OPS
21+
void io_unregister_bpf_ops(struct io_ring_ctx *ctx);
22+
#else
23+
static inline void io_unregister_bpf_ops(struct io_ring_ctx *ctx)
24+
{
25+
}
26+
#endif
27+
2028
#endif /* IOU_BPF_OPS_H */

io_uring/io_uring.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@ static __cold void io_req_caches_free(struct io_ring_ctx *ctx)
21482148

21492149
static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
21502150
{
2151+
io_unregister_bpf_ops(ctx);
21512152
io_sq_thread_finish(ctx);
21522153

21532154
mutex_lock(&ctx->uring_lock);

0 commit comments

Comments
 (0)