Commit 6b7898eb authored by Jens Axboe's avatar Jens Axboe

io_uring: fix imbalanced sqo_mm accounting

We do the initial accounting of locked_vm and pinned_vm before we have
setup ctx->sqo_mm, which means we can end up having not accounted the
memory at setup time, but still decrement it when we exit. This causes
an imbalance in the accounting.

Setup ctx->sqo_mm earlier in io_uring_create(), before we do the first
accounting of mm->{locked,pinned}_vm. This also unifies the state
grabbing for the ctx, and eliminates a failure case in
io_sq_offload_start().

Fixes: f74441e6 ("io_uring: account locked memory before potential error case")
Reported-by: default avatarRobert M. Muncrief <rmuncrief@humanavance.com>
Reported-by: default avatarNiklas Schnelle <schnelle@linux.ibm.com>
Tested-by: default avatarNiklas Schnelle <schnelle@linux.ibm.com>
Tested-by: default avatarRobert M. Muncrief <rmuncrief@humanavance.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 84216315
...@@ -7447,9 +7447,6 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, ...@@ -7447,9 +7447,6 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
{ {
int ret; int ret;
mmgrab(current->mm);
ctx->sqo_mm = current->mm;
if (ctx->flags & IORING_SETUP_SQPOLL) { if (ctx->flags & IORING_SETUP_SQPOLL) {
ret = -EPERM; ret = -EPERM;
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
...@@ -7494,10 +7491,6 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, ...@@ -7494,10 +7491,6 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
return 0; return 0;
err: err:
io_finish_async(ctx); io_finish_async(ctx);
if (ctx->sqo_mm) {
mmdrop(ctx->sqo_mm);
ctx->sqo_mm = NULL;
}
return ret; return ret;
} }
...@@ -8547,6 +8540,9 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, ...@@ -8547,6 +8540,9 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p,
ctx->user = user; ctx->user = user;
ctx->creds = get_current_cred(); ctx->creds = get_current_cred();
mmgrab(current->mm);
ctx->sqo_mm = current->mm;
/* /*
* Account memory _before_ installing the file descriptor. Once * Account memory _before_ installing the file descriptor. Once
* the descriptor is installed, it can get closed at any time. Also * the descriptor is installed, it can get closed at any time. Also
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment