From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com, Martin KaFai Lau <martin.lau@linux.dev>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC v2 3/5] io_uring/bpf: implement struct_ops registration
Date: Fri, 6 Jun 2025 14:58:00 +0100 [thread overview]
Message-ID: <f43e5d4e5e1797312ef3ee7986f4447bddac1d3c.1749214572.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1749214572.git.asml.silence@gmail.com>
Add ring_fd to the struct_ops and implement [un]registration.
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
io_uring/bpf.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-
io_uring/bpf.h | 3 +++
2 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/io_uring/bpf.c b/io_uring/bpf.c
index 3096c54e4fb3..0f82acf09959 100644
--- a/io_uring/bpf.c
+++ b/io_uring/bpf.c
@@ -3,6 +3,8 @@
#include "bpf.h"
#include "register.h"
+DEFINE_MUTEX(io_bpf_ctrl_mutex);
+
static struct io_uring_ops io_bpf_ops_stubs = {
};
@@ -50,20 +52,83 @@ static int bpf_io_init_member(const struct btf_type *t,
const struct btf_member *member,
void *kdata, const void *udata)
{
+ u32 moff = __btf_member_bit_offset(t, member) / 8;
+ const struct io_uring_ops *uops = udata;
+ struct io_uring_ops *ops = kdata;
+
+ switch (moff) {
+ case offsetof(struct io_uring_ops, ring_fd):
+ ops->ring_fd = uops->ring_fd;
+ return 1;
+ }
+ return 0;
+}
+
+static int io_register_bpf_ops(struct io_ring_ctx *ctx, struct io_uring_ops *ops)
+{
+ if (ctx->bpf_ops)
+ return -EBUSY;
+ if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
+ return -EOPNOTSUPP;
+
+ percpu_ref_get(&ctx->refs);
+ ops->ctx = ctx;
+ ctx->bpf_ops = ops;
return 0;
}
static int bpf_io_reg(void *kdata, struct bpf_link *link)
{
- return -EOPNOTSUPP;
+ struct io_uring_ops *ops = kdata;
+ struct io_ring_ctx *ctx;
+ struct file *file;
+ int ret;
+
+ file = io_uring_register_get_file(ops->ring_fd, false);
+ if (IS_ERR(file))
+ return PTR_ERR(file);
+
+ ctx = file->private_data;
+ scoped_guard(mutex, &ctx->uring_lock)
+ ret = io_register_bpf_ops(ctx, ops);
+
+ fput(file);
+ return ret;
}
static void bpf_io_unreg(void *kdata, struct bpf_link *link)
{
+ struct io_uring_ops *ops = kdata;
+ struct io_ring_ctx *ctx;
+
+ guard(mutex)(&io_bpf_ctrl_mutex);
+
+ ctx = ops->ctx;
+ ops->ctx = NULL;
+
+ if (ctx) {
+ scoped_guard(mutex, &ctx->uring_lock) {
+ if (ctx->bpf_ops == ops)
+ ctx->bpf_ops = NULL;
+ }
+ percpu_ref_put(&ctx->refs);
+ }
}
void io_unregister_bpf_ops(struct io_ring_ctx *ctx)
{
+ struct io_uring_ops *ops;
+
+ guard(mutex)(&io_bpf_ctrl_mutex);
+ guard(mutex)(&ctx->uring_lock);
+
+ ops = ctx->bpf_ops;
+ ctx->bpf_ops = NULL;
+
+ if (ops && ops->ctx) {
+ percpu_ref_put(&ctx->refs);
+ ops->ctx = NULL;
+ }
}
static struct bpf_struct_ops bpf_io_uring_ops = {
diff --git a/io_uring/bpf.h b/io_uring/bpf.h
index a61c489d306b..4b147540d006 100644
--- a/io_uring/bpf.h
+++ b/io_uring/bpf.h
@@ -8,6 +8,9 @@
#include "io_uring.h"
struct io_uring_ops {
+ __u32 ring_fd;
+
+ struct io_ring_ctx *ctx;
};
static inline bool io_bpf_attached(struct io_ring_ctx *ctx)
--
2.49.0
next prev parent reply other threads:[~2025-06-06 13:56 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-06 13:57 [RFC v2 0/5] BPF controlled io_uring Pavel Begunkov
2025-06-06 13:57 ` [RFC v2 1/5] io_uring: add struct for state controlling cqwait Pavel Begunkov
2025-06-06 13:57 ` [RFC v2 2/5] io_uring/bpf: add stubs for bpf struct_ops Pavel Begunkov
2025-06-06 14:25 ` Jens Axboe
2025-06-06 14:28 ` Jens Axboe
2025-06-06 13:58 ` Pavel Begunkov [this message]
2025-06-06 14:57 ` [RFC v2 3/5] io_uring/bpf: implement struct_ops registration Jens Axboe
2025-06-06 20:00 ` Pavel Begunkov
2025-06-06 21:07 ` Jens Axboe
2025-06-06 13:58 ` [RFC v2 4/5] io_uring/bpf: add handle events callback Pavel Begunkov
2025-06-12 2:28 ` Alexei Starovoitov
2025-06-12 9:33 ` Pavel Begunkov
2025-06-12 14:07 ` Jens Axboe
2025-06-06 13:58 ` [RFC v2 5/5] io_uring/bpf: add basic kfunc helpers Pavel Begunkov
2025-06-12 2:47 ` Alexei Starovoitov
2025-06-12 13:26 ` Pavel Begunkov
2025-06-12 14:06 ` Jens Axboe
2025-06-13 0:25 ` Alexei Starovoitov
2025-06-13 16:12 ` Pavel Begunkov
2025-06-13 19:51 ` Alexei Starovoitov
2025-06-16 20:34 ` Pavel Begunkov
2025-06-06 14:38 ` [RFC v2 0/5] BPF controlled io_uring Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f43e5d4e5e1797312ef3ee7986f4447bddac1d3c.1749214572.git.asml.silence@gmail.com \
--to=asml.silence@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox