]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
io_uring: count allocated requests
authorPavel Begunkov <asml.silence@gmail.com>
Fri, 9 May 2025 11:12:53 +0000 (12:12 +0100)
committerJens Axboe <axboe@kernel.dk>
Fri, 9 May 2025 14:01:02 +0000 (08:01 -0600)
Keep track of the number requests a ring currently has allocated (and
not freed), it'll be needed in the next patch.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/c8f8308294dc2a1cb8925d984d937d4fc14ab5d4.1746788718.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/io_uring_types.h
io_uring/io_uring.c

index 7e23e993280eeefd0d25b647dd1f46169eed1b96..73b289b48280db5d709edd7e028593575ed7b7b4 100644 (file)
@@ -435,6 +435,7 @@ struct io_ring_ctx {
 
        /* protected by ->completion_lock */
        unsigned                        evfd_last_cq_tail;
+       unsigned                        nr_req_allocated;
 
        /*
         * Protection for resize vs mmap races - both the mmap and resize
index 6efecb46c828c012a13fbc2590b27590e9c62c15..714b66ab34b0eff928b1ea0da10f3336438950f4 100644 (file)
@@ -957,6 +957,8 @@ __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
        }
 
        percpu_ref_get_many(&ctx->refs, ret);
+       ctx->nr_req_allocated += ret;
+
        while (ret--) {
                struct io_kiocb *req = reqs[ret];
 
@@ -2694,8 +2696,10 @@ static void io_req_caches_free(struct io_ring_ctx *ctx)
                kmem_cache_free(req_cachep, req);
                nr++;
        }
-       if (nr)
+       if (nr) {
+               ctx->nr_req_allocated -= nr;
                percpu_ref_put_many(&ctx->refs, nr);
+       }
        mutex_unlock(&ctx->uring_lock);
 }
 
@@ -2732,6 +2736,9 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
        percpu_ref_exit(&ctx->refs);
        free_uid(ctx->user);
        io_req_caches_free(ctx);
+
+       WARN_ON_ONCE(ctx->nr_req_allocated);
+
        if (ctx->hash_map)
                io_wq_put_hash(ctx->hash_map);
        io_napi_free(ctx);