1 From 6b69c4ab4f685327d9e10caf0d84217ba23a8c4b Mon Sep 17 00:00:00 2001
2 From: Jens Axboe <axboe@kernel.dk>
3 Date: Fri, 15 Mar 2024 16:12:51 -0600
4 Subject: io_uring/kbuf: protect io_buffer_list teardown with a reference
6 From: Jens Axboe <axboe@kernel.dk>
8 commit 6b69c4ab4f685327d9e10caf0d84217ba23a8c4b upstream.
10 No functional changes in this patch, just in preparation for being able
11 to keep the buffer list alive outside of the ctx->uring_lock.
13 Cc: stable@vger.kernel.org # v6.4+
14 Signed-off-by: Jens Axboe <axboe@kernel.dk>
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
17 io_uring/kbuf.c | 15 +++++++++++----
18 io_uring/kbuf.h | 2 ++
19 2 files changed, 13 insertions(+), 4 deletions(-)
23 @@ -59,6 +59,7 @@ static int io_buffer_add_list(struct io_
24 * always under the ->uring_lock, but the RCU lookup from mmap does.
27 + atomic_set(&bl->refs, 1);
28 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
31 @@ -272,6 +273,14 @@ static int __io_remove_buffers(struct io
35 +static void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
37 + if (atomic_dec_and_test(&bl->refs)) {
38 + __io_remove_buffers(ctx, bl, -1U);
43 void io_destroy_buffers(struct io_ring_ctx *ctx)
45 struct io_buffer_list *bl;
46 @@ -279,8 +288,7 @@ void io_destroy_buffers(struct io_ring_c
48 xa_for_each(&ctx->io_bl_xa, index, bl) {
49 xa_erase(&ctx->io_bl_xa, bl->bgid);
50 - __io_remove_buffers(ctx, bl, -1U);
55 while (!list_empty(&ctx->io_buffers_pages)) {
56 @@ -676,9 +684,8 @@ int io_unregister_pbuf_ring(struct io_ri
60 - __io_remove_buffers(ctx, bl, -1U);
61 xa_erase(&ctx->io_bl_xa, bl->bgid);
69 @@ -25,6 +25,8 @@ struct io_buffer_list {
75 /* ring mapped provided buffers */
77 /* ring mapped provided buffers, but mmap'ed by application */