Skip to content

Commit 0d83b8a

Browse files
calebsanderaxboe
authored andcommitted
io_uring: introduce io_cache_free() helper
Add a helper function io_cache_free() that returns an allocation to a io_alloc_cache, falling back on kfree() if the io_alloc_cache is full. This is the inverse of io_cache_alloc(), which takes an allocation from an io_alloc_cache and falls back on kmalloc() if the cache is empty. Convert 4 callers to use the helper. Signed-off-by: Caleb Sander Mateos <[email protected]> Suggested-by: Li Zetao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent fe21a45 commit 0d83b8a

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

io_uring/alloc_cache.h

+6
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ static inline void *io_cache_alloc(struct io_alloc_cache *cache, gfp_t gfp)
6868
return io_cache_alloc_new(cache, gfp);
6969
}
7070

71+
static inline void io_cache_free(struct io_alloc_cache *cache, void *obj)
72+
{
73+
if (!io_alloc_cache_put(cache, obj))
74+
kfree(obj);
75+
}
76+
7177
#endif

io_uring/futex.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ static void __io_futex_complete(struct io_kiocb *req, io_tw_token_t tw)
5353

5454
static void io_futex_complete(struct io_kiocb *req, io_tw_token_t tw)
5555
{
56-
struct io_futex_data *ifd = req->async_data;
5756
struct io_ring_ctx *ctx = req->ctx;
5857

5958
io_tw_lock(ctx, tw);
60-
if (!io_alloc_cache_put(&ctx->futex_cache, ifd))
61-
kfree(ifd);
59+
io_cache_free(&ctx->futex_cache, req->async_data);
6260
__io_futex_complete(req, tw);
6361
}
6462

io_uring/io_uring.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,7 @@ static void io_free_batch_list(struct io_ring_ctx *ctx,
14221422

14231423
if (apoll->double_poll)
14241424
kfree(apoll->double_poll);
1425-
if (!io_alloc_cache_put(&ctx->apoll_cache, apoll))
1426-
kfree(apoll);
1425+
io_cache_free(&ctx->apoll_cache, apoll);
14271426
req->flags &= ~REQ_F_POLLED;
14281427
}
14291428
if (req->flags & IO_REQ_LINK_FLAGS)

io_uring/rsrc.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ static struct io_mapped_ubuf *io_alloc_imu(struct io_ring_ctx *ctx,
124124

125125
static void io_free_imu(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu)
126126
{
127-
if (imu->nr_bvecs > IO_CACHED_BVECS_SEGS ||
128-
!io_alloc_cache_put(&ctx->imu_cache, imu))
127+
if (imu->nr_bvecs <= IO_CACHED_BVECS_SEGS)
128+
io_cache_free(&ctx->imu_cache, imu);
129+
else
129130
kvfree(imu);
130131
}
131132

@@ -487,12 +488,6 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
487488
return IOU_OK;
488489
}
489490

490-
static void io_free_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
491-
{
492-
if (!io_alloc_cache_put(&ctx->node_cache, node))
493-
kfree(node);
494-
}
495-
496491
void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
497492
{
498493
if (node->tag)
@@ -510,7 +505,7 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
510505
break;
511506
}
512507

513-
io_free_node(ctx, node);
508+
io_cache_free(&ctx->node_cache, node);
514509
}
515510

516511
int io_sqe_files_unregister(struct io_ring_ctx *ctx)
@@ -835,7 +830,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
835830
if (ret) {
836831
if (imu)
837832
io_free_imu(ctx, imu);
838-
io_free_node(ctx, node);
833+
io_cache_free(&ctx->node_cache, node);
839834
node = ERR_PTR(ret);
840835
}
841836
kvfree(pages);

0 commit comments

Comments
 (0)