Skip to contentSkip to conversation

memory accounting fixes

PATCH Applied Pavel Begunkov 2 participants 4 messages Jul 18, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover memory accounting fixes

Two small memory accounting fixes for 5.9

Pavel Begunkov (2):
io_uring: don't miscount pinned memory
io_uring: return locked and pinned page accounting

 fs/io_uring.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
Signature · 1 line
2.24.0
#1 lore raw

PATCH 1/2 io_uring: don't miscount pinned memory

io_sqe_buffer_unregister() uses cxt->sqo_mm for memory accounting, but io_ring_ctx_free() drops ->sqo_mm before leaving pinned_vm over-accounted. Postpone mm cleanup for when it's not needed anymore.

Fixes 309758254ea62 ("io_uring: report pinned memory usage")
---
 fs/io_uring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/io_uring.c +1 -1
index 644e5727389c..e535152fefab 100644
@@ -7655,12 +7655,12 @@ static void io_destroy_buffers(struct io_ring_ctx *ctx)
 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
 {
 	io_finish_async(ctx);
+	io_sqe_buffer_unregister(ctx);
 	if (ctx->sqo_mm) {
 		mmdrop(ctx->sqo_mm);
 		ctx->sqo_mm = NULL;
 	}
 
-	io_sqe_buffer_unregister(ctx);
 	io_sqe_files_unregister(ctx);
 	io_eventfd_unregister(ctx);
 	io_destroy_buffers(ctx);
Signature · 1 line
2.24.0
#2 lore raw

PATCH 2/2 io_uring: return locked and pinned page accounting

In reply to Pavel Begunkov

Locked and pinned memory accounting in io_{,un}account_mem() depends on having ->sqo_mm, which is NULL after a recent change for non SQPOLL'ed io_ring. That disables the accounting.

Return ->sqo_mm initialisation back, and do __io_sq_thread_acquire_mm() based on IORING_SETUP_SQPOLL flag.

Fixes 8eb06d7e8dd85 ("io_uring: fix missing ->mm on exit")
---
 fs/io_uring.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
fs/io_uring.c +5 -4
index e535152fefab..57e1f26b6a6b 100644
@@ -980,7 +980,8 @@ static void io_sq_thread_drop_mm(void)
 static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
 {
 	if (!current->mm) {
-		if (unlikely(!ctx->sqo_mm || !mmget_not_zero(ctx->sqo_mm)))
+		if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
+			     !mmget_not_zero(ctx->sqo_mm)))
 			return -EFAULT;
 		kthread_use_mm(ctx->sqo_mm);
 	}
@@ -7244,10 +7245,10 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 {
 	int ret;
 
-	if (ctx->flags & IORING_SETUP_SQPOLL) {
-		mmgrab(current->mm);
-		ctx->sqo_mm = current->mm;
+	mmgrab(current->mm);
+	ctx->sqo_mm = current->mm;
 
+	if (ctx->flags & IORING_SETUP_SQPOLL) {
 		ret = -EPERM;
 		if (!capable(CAP_SYS_ADMIN))
 			goto err;
Signature · 1 line
2.24.0
maintainer Applied
#3 lore raw
In reply to Pavel Begunkov
Quoted 8 lines · Pavel Begunkov

Two small memory accounting fixes for 5.9

Pavel Begunkov (2):
io_uring: don't miscount pinned memory
io_uring: return locked and pinned page accounting

 fs/io_uring.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Thanks, applied.

Signature · 1 line
Jens Axboe