Skip to content

Commit fd6fab2

Browse files
committed
io_uring: retry bulk slab allocs as single allocs
I've seen cases where bulk alloc fails, since the bulk alloc API is all-or-nothing - either we get the number we ask for, or it returns 0 as number of entries. If we fail a batch bulk alloc, retry a "normal" kmem_cache_alloc() and just use that instead of failing with -EAGAIN. While in there, ensure we use GFP_KERNEL. That was an oversight in the original code, when we switched away from GFP_ATOMIC. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 8c83878 commit fd6fab2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

fs/io_uring.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,33 @@ static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
399399
static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
400400
struct io_submit_state *state)
401401
{
402+
gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
402403
struct io_kiocb *req;
403404

404405
if (!percpu_ref_tryget(&ctx->refs))
405406
return NULL;
406407

407408
if (!state) {
408-
req = kmem_cache_alloc(req_cachep, __GFP_NOWARN);
409+
req = kmem_cache_alloc(req_cachep, gfp);
409410
if (unlikely(!req))
410411
goto out;
411412
} else if (!state->free_reqs) {
412413
size_t sz;
413414
int ret;
414415

415416
sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
416-
ret = kmem_cache_alloc_bulk(req_cachep, __GFP_NOWARN, sz,
417-
state->reqs);
418-
if (unlikely(ret <= 0))
419-
goto out;
417+
ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
418+
419+
/*
420+
* Bulk alloc is all-or-nothing. If we fail to get a batch,
421+
* retry single alloc to be on the safe side.
422+
*/
423+
if (unlikely(ret <= 0)) {
424+
state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
425+
if (!state->reqs[0])
426+
goto out;
427+
ret = 1;
428+
}
420429
state->free_reqs = ret - 1;
421430
state->cur_req = 1;
422431
req = state->reqs[0];

0 commit comments

Comments
 (0)