From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DB0D27C162 for ; Mon, 23 Feb 2026 14:10:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771855849; cv=none; b=j04rSG8Eecaug7yOOTBa0I99fQbVt0R+xLYl+272izr3e48A4vDLLlti54SoPsEr2CQzndNU3h3MvSWSl7arIjM27HoMxD6swjafjBHTQFSxgE5Qtu+JZLGB7/EPNhVla+z1Ev9iZOQc82CDU1hsTkMAuclj9fm8xasRj9N3UHg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771855849; c=relaxed/simple; bh=sAmpA/t65W41B0yOtP1AfdLRml3XOiZTMLNfCBu9Qh8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O/sDa3BKk1y7LH4E85PffVdGFavKktIZmmVSqZd5BTWI99iMuEtj9APTpx57m6kn3iS6thmbQ7+sWWGC3B9P+KTvnkkj7d24vFZRtB+RQov06CfY7hrP7gYaRGBeJwZM/FjRMlI9CJxh7SI5SYEZtDjREUFRDDkYL3IQJOD3zIE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZImoh4UA; arc=none smtp.client-ip=209.85.221.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZImoh4UA" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-43770c94dfaso4367183f8f.2 for ; Mon, 23 Feb 2026 06:10:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771855846; x=1772460646; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mVZ7NiAv763L4Pu2O9i5WPJsIVLKf0ckp/pKmOLKCtc=; b=ZImoh4UA5kIT/83oCOu/JP4M7CQ+p95h7qMhcZM5zUmjsFbc1feVXUAZjPzej80VUe JL4NvIJwuCD0wjr7ozXJsMQLBuE4IV+Mvj51RV9HYpnJhtsnwjfIwvyLf0Pf7mlFE6Qk ETRi+N1rh72szpWOYsqMb3AZfZ+KJ8L3em/2sD7RRU2HaU0pGb7JxDEsWFj2PiUwc7n+ pdta91h721CWsyEYgeU93eg0kDN/dOg7kpyqf49jzMAAbAHNrIlxsmVH+ZNoPNhgJ5wD mZD71+JjKl73LomBBkt9rAb2x5pP6O+JA8sqjkK3xXOKUeEMW4uXDVOniigt4kh/z2AL 6g2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771855846; x=1772460646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=mVZ7NiAv763L4Pu2O9i5WPJsIVLKf0ckp/pKmOLKCtc=; b=SjaOyCk4vdE8aulNWJQ//wO8wHv2t7ulBBcu+jgY5hk2u5VneJrp0gel+hxSMEzf2K fsAbqgulEK6BTS0/xkOw+um4ACHoK4WtsvFw2R/AH+EB40K5q8ZreBmC4MJBZeh/9je0 yBufBQiC1wCq1a1qpUhCUDIf8CoyXJ10cGSnSS8DLdIAzmkUKIwCqg3SwQCdW7wXxXT8 W2Etmhpbp2DuC4D75AiHsyCw8u74RmgryUNkbjlQheX/5oxnosePvE57xJeU0vp34kKT raFnFVSa3BoaudBOdZpBlr6kJINauWHHJeFWt9E5ysSajRzYiNufI2BiHiJeE11x6kgT Hyzw== X-Gm-Message-State: AOJu0Yw+i6Pc0gyCxKs6Vz/CguF0B8kibiOjSXKp+kMtGGSQR9nUNAmK LqOSh/3MdlIrePDdDx8sGcAW0nsx/BdW3kU9RAE0cfA0IK9p5PoCiEotbqXwAg== X-Gm-Gg: ATEYQzycRPQIy0DrOZk9BL76QSbfyOZzn+995Rvc0O/ZqcUehcx52tXyYvaJT6vVl53 BaeL3y1qhCNWbbSlzSksv511oqfTIjf7ot2VkOOqpRi21g3cGJuicxEnZli8jofrzaFUnM+aIqg +xHneQ+raf0cGQN1Xl9sgMMRA7HzxZ5gHQL/opHDzthFV0uDvira5n0XwPWLqle6+QWqBHNAUb6 DL+dfeYZzsxvNZuCEDF1SPkM9LBdgWQc5GL8weQyRANtJniz1fovuRkqwitUNP/8ti+eqn3AyQy IhjuTCfWrpe32T4vrAqpFz7rsGC88s9ar5rj7pRsaql4AQqOx+O9FEopAm0fjh1vfDQCfjV+2NP vsVbNVxn5Ep2vO/QAbfidMpxGDxikIxjN8R5KZ+ig4NDolRunnaCUKVnEpX+hU0Ez1qadMZvHhI wnYBTtmmNbV9ZaPPIfKVlmDijTiJjErJtjoKanOKfr0UBytO1QfBjZiMh9uTZH+X4JxNOB25ReK M1V/9HVNA== X-Received: by 2002:a05:6000:2283:b0:437:81b0:6650 with SMTP id ffacd0b85a97d-4396f189ffbmr17327631f8f.56.1771855845857; Mon, 23 Feb 2026 06:10:45 -0800 (PST) Received: from 127.com ([2620:10d:c092:600::1:36ea]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43970bf9feasm19464640f8f.6.2026.02.23.06.10.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Feb 2026 06:10:44 -0800 (PST) From: Pavel Begunkov To: io-uring@vger.kernel.org Cc: asml.silence@gmail.com, bpf@vger.kernel.org, axboe@kernel.dk, Alexei Starovoitov Subject: [PATCH v9 04/10] io_uring/bpf-ops: implement bpf ops registration Date: Mon, 23 Feb 2026 14:10:15 +0000 Message-ID: <2aafe124e44c0acf5d3b412abe74101ca2c0aa29.1771855761.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- include/linux/io_uring_types.h | 5 ++ io_uring/bpf-ops.c | 91 +++++++++++++++++++++++++++++++++- io_uring/bpf-ops.h | 8 +++ io_uring/io_uring.c | 1 + 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index cceac329fcfd..976d85f82f86 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -8,6 +8,9 @@ #include #include +struct iou_loop_params; +struct io_uring_bpf_ops; + enum { /* * A hint to not wake right away but delay until there are enough of @@ -481,6 +484,8 @@ struct io_ring_ctx { DECLARE_HASHTABLE(napi_ht, 4); #endif + struct io_uring_bpf_ops *bpf_ops; + /* * Protection for resize vs mmap races - both the mmap and resize * side will need to grab this lock, to prevent either side from diff --git a/io_uring/bpf-ops.c b/io_uring/bpf-ops.c index 17518f4ecca9..1ffe7ba73b89 100644 --- a/io_uring/bpf-ops.c +++ b/io_uring/bpf-ops.c @@ -5,10 +5,12 @@ #include "io_uring.h" #include "register.h" +#include "loop.h" #include "memmap.h" #include "bpf-ops.h" #include "loop.h" +static DEFINE_MUTEX(io_bpf_ctrl_mutex); static const struct btf_type *loop_params_type; __bpf_kfunc_start_defs(); @@ -143,16 +145,103 @@ 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_bpf_ops *uops = udata; + struct io_uring_bpf_ops *ops = kdata; + + switch (moff) { + case offsetof(struct io_uring_bpf_ops, ring_fd): + ops->ring_fd = uops->ring_fd; + return 1; + } + return 0; +} + +static int io_install_bpf(struct io_ring_ctx *ctx, struct io_uring_bpf_ops *ops) +{ + if (ctx->flags & (IORING_SETUP_SQPOLL | IORING_SETUP_IOPOLL)) + return -EOPNOTSUPP; + if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN)) + return -EOPNOTSUPP; + + if (ctx->bpf_ops) + return -EBUSY; + if (WARN_ON_ONCE(!ops->loop_step)) + return -EINVAL; + + ops->priv = ctx; + ctx->bpf_ops = ops; + ctx->loop_step = ops->loop_step; return 0; } static int bpf_io_reg(void *kdata, struct bpf_link *link) { - return -EOPNOTSUPP; + struct io_uring_bpf_ops *ops = kdata; + struct io_ring_ctx *ctx; + struct file *file; + int ret = -EBUSY; + + 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, &io_bpf_ctrl_mutex) { + guard(mutex)(&ctx->uring_lock); + ret = io_install_bpf(ctx, ops); + } + + fput(file); + return ret; +} + +static void io_eject_bpf(struct io_ring_ctx *ctx) +{ + struct io_uring_bpf_ops *ops = ctx->bpf_ops; + + if (WARN_ON_ONCE(!ops)) + return; + if (WARN_ON_ONCE(ops->priv != ctx)) + return; + + ops->priv = NULL; + ctx->bpf_ops = NULL; + ctx->loop_step = NULL; } static void bpf_io_unreg(void *kdata, struct bpf_link *link) { + struct io_uring_bpf_ops *ops = kdata; + struct io_ring_ctx *ctx; + + guard(mutex)(&io_bpf_ctrl_mutex); + ctx = ops->priv; + if (ctx) { + guard(mutex)(&ctx->uring_lock); + if (WARN_ON_ONCE(ctx->bpf_ops != ops)) + return; + + io_eject_bpf(ctx); + } +} + +void io_unregister_bpf_ops(struct io_ring_ctx *ctx) +{ + /* + * ->bpf_ops is write protected by io_bpf_ctrl_mutex and uring_lock, + * and read protected by either. Try to avoid taking the global lock + * for rings that never had any bpf installed. + */ + scoped_guard(mutex, &ctx->uring_lock) { + if (!ctx->bpf_ops) + return; + } + + guard(mutex)(&io_bpf_ctrl_mutex); + guard(mutex)(&ctx->uring_lock); + if (ctx->bpf_ops) + io_eject_bpf(ctx); } static struct bpf_struct_ops bpf_ring_ops = { diff --git a/io_uring/bpf-ops.h b/io_uring/bpf-ops.h index b9e589ad519a..b39b3fd3acda 100644 --- a/io_uring/bpf-ops.h +++ b/io_uring/bpf-ops.h @@ -17,4 +17,12 @@ struct io_uring_bpf_ops { void *priv; }; +#ifdef CONFIG_IO_URING_BPF_OPS +void io_unregister_bpf_ops(struct io_ring_ctx *ctx); +#else +static inline void io_unregister_bpf_ops(struct io_ring_ctx *ctx) +{ +} +#endif + #endif /* IOU_BPF_OPS_H */ diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 548ea5a080a0..b154dac396d3 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2154,6 +2154,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_unregister_bpf_ops(ctx); io_sq_thread_finish(ctx); mutex_lock(&ctx->uring_lock); -- 2.53.0