]> Gentwo Git Trees - linux/.git/commitdiff
io_uring/zcrx: reverse ifq refcount
authorDavid Wei <dw@davidwei.uk>
Tue, 4 Nov 2025 22:44:58 +0000 (14:44 -0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 6 Nov 2025 23:23:21 +0000 (16:23 -0700)
Add a refcount to struct io_zcrx_ifq to reverse the refcounting
relationship i.e. rings now reference ifqs instead. As a result of this,
remove ctx->refs that an ifq holds on a ring via the page pool memory
provider.

This ref ifq->refs is held by internal users of an ifq, namely rings and
the page pool memory provider associated with an ifq. This is needed to
keep the ifq around until the page pool is destroyed.

Since ifqs now no longer hold refs to ring ctx, there isn't a need to
split the cleanup of ifqs into two: io_shutdown_zcrx_ifqs() in
io_ring_exit_work() while waiting for ctx->refs to drop to 0, and
io_unregister_zcrx_ifqs() after. Remove io_shutdown_zcrx_ifqs().

Signed-off-by: David Wei <dw@davidwei.uk>
Co-developed-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Reviewed-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
io_uring/io_uring.c
io_uring/zcrx.c
io_uring/zcrx.h

index 85081d7453b70f876f54210c6e163925f62c86a9..e493d4358ab7a41eeb2370a361ef1a2107b6bd65 100644 (file)
@@ -2988,11 +2988,6 @@ static __cold void io_ring_exit_work(struct work_struct *work)
                        io_cqring_overflow_kill(ctx);
                        mutex_unlock(&ctx->uring_lock);
                }
-               if (!xa_empty(&ctx->zcrx_ctxs)) {
-                       mutex_lock(&ctx->uring_lock);
-                       io_shutdown_zcrx_ifqs(ctx);
-                       mutex_unlock(&ctx->uring_lock);
-               }
 
                if (ctx->flags & IORING_SETUP_DEFER_TASKRUN)
                        io_move_task_work_from_local(ctx);
index b3f3d55d2f63bdd65ed090e26f9357f04289cdbe..5752ff9a103f67e55c037cd707ca15672b18ec93 100644 (file)
@@ -479,9 +479,9 @@ static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx)
                return NULL;
 
        ifq->if_rxq = -1;
-       ifq->ctx = ctx;
        spin_lock_init(&ifq->rq_lock);
        mutex_init(&ifq->pp_lock);
+       refcount_set(&ifq->refs, 1);
        return ifq;
 }
 
@@ -537,6 +537,12 @@ static void io_zcrx_ifq_free(struct io_zcrx_ifq *ifq)
        kfree(ifq);
 }
 
+static void io_put_zcrx_ifq(struct io_zcrx_ifq *ifq)
+{
+       if (refcount_dec_and_test(&ifq->refs))
+               io_zcrx_ifq_free(ifq);
+}
+
 struct io_mapped_region *io_zcrx_get_region(struct io_ring_ctx *ctx,
                                            unsigned int id)
 {
@@ -592,6 +598,7 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx,
        ifq = io_zcrx_ifq_alloc(ctx);
        if (!ifq)
                return -ENOMEM;
+
        if (ctx->user) {
                get_uid(ctx->user);
                ifq->user = ctx->user;
@@ -714,19 +721,6 @@ static void io_zcrx_scrub(struct io_zcrx_ifq *ifq)
        }
 }
 
-void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)
-{
-       struct io_zcrx_ifq *ifq;
-       unsigned long index;
-
-       lockdep_assert_held(&ctx->uring_lock);
-
-       xa_for_each(&ctx->zcrx_ctxs, index, ifq) {
-               io_zcrx_scrub(ifq);
-               io_close_queue(ifq);
-       }
-}
-
 void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)
 {
        struct io_zcrx_ifq *ifq;
@@ -743,7 +737,10 @@ void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)
                }
                if (!ifq)
                        break;
-               io_zcrx_ifq_free(ifq);
+
+               io_close_queue(ifq);
+               io_zcrx_scrub(ifq);
+               io_put_zcrx_ifq(ifq);
        }
 
        xa_destroy(&ctx->zcrx_ctxs);
@@ -894,15 +891,13 @@ static int io_pp_zc_init(struct page_pool *pp)
        if (ret)
                return ret;
 
-       percpu_ref_get(&ifq->ctx->refs);
+       refcount_inc(&ifq->refs);
        return 0;
 }
 
 static void io_pp_zc_destroy(struct page_pool *pp)
 {
-       struct io_zcrx_ifq *ifq = io_pp_to_ifq(pp);
-
-       percpu_ref_put(&ifq->ctx->refs);
+       io_put_zcrx_ifq(io_pp_to_ifq(pp));
 }
 
 static int io_pp_nl_fill(void *mp_priv, struct sk_buff *rsp,
index 8d828dc9b0e416f67bbb74eba1dd4068dff2e6c5..45e3e71448ffac60185247befc2a5ac228d64356 100644 (file)
@@ -39,7 +39,6 @@ struct io_zcrx_area {
 };
 
 struct io_zcrx_ifq {
-       struct io_ring_ctx              *ctx;
        struct io_zcrx_area             *area;
        unsigned                        niov_shift;
        struct user_struct              *user;
@@ -55,6 +54,7 @@ struct io_zcrx_ifq {
        struct device                   *dev;
        struct net_device               *netdev;
        netdevice_tracker               netdev_tracker;
+       refcount_t                      refs;
 
        /*
         * Page pool and net configuration lock, can be taken deeper in the
@@ -70,7 +70,6 @@ int io_zcrx_return_bufs(struct io_ring_ctx *ctx,
 int io_register_zcrx_ifq(struct io_ring_ctx *ctx,
                         struct io_uring_zcrx_ifq_reg __user *arg);
 void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx);
-void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx);
 int io_zcrx_recv(struct io_kiocb *req, struct io_zcrx_ifq *ifq,
                 struct socket *sock, unsigned int flags,
                 unsigned issue_flags, unsigned int *len);
@@ -85,9 +84,6 @@ static inline int io_register_zcrx_ifq(struct io_ring_ctx *ctx,
 static inline void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)
 {
 }
-static inline void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)
-{
-}
 static inline int io_zcrx_recv(struct io_kiocb *req, struct io_zcrx_ifq *ifq,
                               struct socket *sock, unsigned int flags,
                               unsigned issue_flags, unsigned int *len)