Skip to content

Commit bfc0aa7

Browse files
committed
io_uring/rsrc: add reference count to struct io_mapped_ubuf
Currently there's a single ring owner of a mapped buffer, and hence the reference count will always be 1 when it's torn down and freed. However, in preparation for being able to link io_mapped_ubuf to different spots, add a reference count to manage the lifetime of it. Signed-off-by: Jens Axboe <[email protected]>
1 parent 021b153 commit bfc0aa7

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

io_uring/rsrc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
116116

117117
*slot = NULL;
118118
if (imu != &dummy_ubuf) {
119+
if (!refcount_dec_and_test(&imu->refs))
120+
return;
119121
for (i = 0; i < imu->nr_bvecs; i++)
120122
unpin_user_page(imu->bvec[i].bv_page);
121123
if (imu->acct_pages)
@@ -990,6 +992,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
990992
imu->folio_shift = data.folio_shift;
991993
imu->folio_mask = ~((1UL << data.folio_shift) - 1);
992994
}
995+
refcount_set(&imu->refs, 1);
993996
off = (unsigned long) iov->iov_base & ~imu->folio_mask;
994997
*pimu = imu;
995998
ret = 0;

io_uring/rsrc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct io_mapped_ubuf {
4747
unsigned int folio_shift;
4848
unsigned long acct_pages;
4949
unsigned long folio_mask;
50+
refcount_t refs;
5051
struct bio_vec bvec[] __counted_by(nr_bvecs);
5152
};
5253

0 commit comments

Comments
 (0)