Skip to content

Commit 6b69c4a

Browse files
committed
io_uring/kbuf: protect io_buffer_list teardown with a reference
No functional changes in this patch, just in preparation for being able to keep the buffer list alive outside of the ctx->uring_lock. Cc: [email protected] # v6.4+ Signed-off-by: Jens Axboe <[email protected]>
1 parent 3b80cff commit 6b69c4a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

io_uring/kbuf.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx,
6161
* always under the ->uring_lock, but the RCU lookup from mmap does.
6262
*/
6363
bl->bgid = bgid;
64+
atomic_set(&bl->refs, 1);
6465
return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
6566
}
6667

@@ -265,6 +266,14 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx,
265266
return i;
266267
}
267268

269+
static void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
270+
{
271+
if (atomic_dec_and_test(&bl->refs)) {
272+
__io_remove_buffers(ctx, bl, -1U);
273+
kfree_rcu(bl, rcu);
274+
}
275+
}
276+
268277
void io_destroy_buffers(struct io_ring_ctx *ctx)
269278
{
270279
struct io_buffer_list *bl;
@@ -274,8 +283,7 @@ void io_destroy_buffers(struct io_ring_ctx *ctx)
274283

275284
xa_for_each(&ctx->io_bl_xa, index, bl) {
276285
xa_erase(&ctx->io_bl_xa, bl->bgid);
277-
__io_remove_buffers(ctx, bl, -1U);
278-
kfree_rcu(bl, rcu);
286+
io_put_bl(ctx, bl);
279287
}
280288

281289
/*
@@ -680,9 +688,8 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
680688
if (!bl->is_buf_ring)
681689
return -EINVAL;
682690

683-
__io_remove_buffers(ctx, bl, -1U);
684691
xa_erase(&ctx->io_bl_xa, bl->bgid);
685-
kfree_rcu(bl, rcu);
692+
io_put_bl(ctx, bl);
686693
return 0;
687694
}
688695

io_uring/kbuf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct io_buffer_list {
2525
__u16 head;
2626
__u16 mask;
2727

28+
atomic_t refs;
29+
2830
/* ring mapped provided buffers */
2931
__u8 is_buf_ring;
3032
/* ring mapped provided buffers, but mmap'ed by application */

0 commit comments

Comments
 (0)