Skip to content

Commit b4a72c0

Browse files
wlukowiczaxboe
authored andcommitted
io_uring: fix memory leak when removing provided buffers
When removing provided buffers, io_buffer structs are not being disposed of, leading to a memory leak. They can't be freed individually, because they are allocated in page-sized groups. They need to be added to some free list instead, such as io_buffers_cache. All callers already hold the lock protecting it, apart from when destroying buffers, so had to extend the lock there. Fixes: cc3cec8 ("io_uring: speedup provided buffer handling") Signed-off-by: Wojciech Lukowicz <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent c0921e5 commit b4a72c0

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

io_uring/io_uring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2789,8 +2789,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
27892789
io_eventfd_unregister(ctx);
27902790
io_alloc_cache_free(&ctx->apoll_cache, io_apoll_cache_free);
27912791
io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
2792-
mutex_unlock(&ctx->uring_lock);
27932792
io_destroy_buffers(ctx);
2793+
mutex_unlock(&ctx->uring_lock);
27942794
if (ctx->sq_creds)
27952795
put_cred(ctx->sq_creds);
27962796
if (ctx->submitter_task)

io_uring/kbuf.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,14 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx,
228228
return i;
229229
}
230230

231+
/* protects io_buffers_cache */
232+
lockdep_assert_held(&ctx->uring_lock);
233+
231234
while (!list_empty(&bl->buf_list)) {
232235
struct io_buffer *nxt;
233236

234237
nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
235-
list_del(&nxt->list);
238+
list_move(&nxt->list, &ctx->io_buffers_cache);
236239
if (++i == nbufs)
237240
return i;
238241
cond_resched();

0 commit comments

Comments
 (0)