Skip to content

Commit 309fc03

Browse files
committed
io_uring: account user memory freed when exit has been queued
We currently account the memory after the exit work has been run, but that leaves a gap where a process has closed its ring and until the memory has been accounted as freed. If the memlocked ulimit is borderline, then that can introduce spurious setup errors returning -ENOMEM because the free work hasn't been run yet. Account this as freed when we close the ring, as not to expose a tiny gap where setting up a new ring can fail. Fixes: 85faa7b ("io_uring: punt final io_ring_ctx wait-and-free to workqueue") Cc: [email protected] # v5.7 Signed-off-by: Jens Axboe <[email protected]>
1 parent 667e57d commit 309fc03

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

fs/io_uring.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7351,9 +7351,6 @@ static void io_ring_ctx_free(struct io_ring_ctx *ctx)
73517351
io_mem_free(ctx->sq_sqes);
73527352

73537353
percpu_ref_exit(&ctx->refs);
7354-
if (ctx->account_mem)
7355-
io_unaccount_mem(ctx->user,
7356-
ring_pages(ctx->sq_entries, ctx->cq_entries));
73577354
free_uid(ctx->user);
73587355
put_cred(ctx->creds);
73597356
kfree(ctx->cancel_hash);
@@ -7438,6 +7435,16 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
74387435
if (ctx->rings)
74397436
io_cqring_overflow_flush(ctx, true);
74407437
idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
7438+
7439+
/*
7440+
* Do this upfront, so we won't have a grace period where the ring
7441+
* is closed but resources aren't reaped yet. This can cause
7442+
* spurious failure in setting up a new ring.
7443+
*/
7444+
if (ctx->account_mem)
7445+
io_unaccount_mem(ctx->user,
7446+
ring_pages(ctx->sq_entries, ctx->cq_entries));
7447+
74417448
INIT_WORK(&ctx->exit_work, io_ring_exit_work);
74427449
queue_work(system_wq, &ctx->exit_work);
74437450
}

0 commit comments

Comments
 (0)