This is a note to let you know that I've just added the patch titled io_uring: stash ctx task reference for SQPOLL to the 5.9-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: io_uring-stash-ctx-task-reference-for-sqpoll.patch and it can be found in the queue-5.9 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Thu Oct 29 01:19:08 PM CET 2020 From: Jens Axboe <axboe@xxxxxxxxx> Date: Mon, 14 Sep 2020 10:45:53 -0600 Subject: io_uring: stash ctx task reference for SQPOLL From: Jens Axboe <axboe@xxxxxxxxx> commit 2aede0e417db846793c276c7a1bbf7262c8349b0 upstream. We can grab a reference to the task instead of stashing away the task files_struct. This is doable without creating a circular reference between the ring fd and the task itself. Reviewed-by: Pavel Begunkov <asml.silence@xxxxxxxxx> Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/io_uring.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -265,7 +265,16 @@ struct io_ring_ctx { /* IO offload */ struct io_wq *io_wq; struct task_struct *sqo_thread; /* if using sq thread polling */ - struct mm_struct *sqo_mm; + + /* + * For SQPOLL usage - we hold a reference to the parent task, so we + * have access to the ->files + */ + struct task_struct *sqo_task; + + /* Only used for accounting purposes */ + struct mm_struct *mm_account; + wait_queue_head_t sqo_wait; /* @@ -969,9 +978,10 @@ static int __io_sq_thread_acquire_mm(str { if (!current->mm) { if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) || - !mmget_not_zero(ctx->sqo_mm))) + !ctx->sqo_task->mm || + !mmget_not_zero(ctx->sqo_task->mm))) return -EFAULT; - kthread_use_mm(ctx->sqo_mm); + kthread_use_mm(ctx->sqo_task->mm); } return 0; @@ -7591,11 +7601,11 @@ static void io_unaccount_mem(struct io_r if (ctx->limit_mem) __io_unaccount_mem(ctx->user, nr_pages); - if (ctx->sqo_mm) { + if (ctx->mm_account) { if (acct == ACCT_LOCKED) - ctx->sqo_mm->locked_vm -= nr_pages; + ctx->mm_account->locked_vm -= nr_pages; else if (acct == ACCT_PINNED) - atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm); + atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); } } @@ -7610,11 +7620,11 @@ static int io_account_mem(struct io_ring return ret; } - if (ctx->sqo_mm) { + if (ctx->mm_account) { if (acct == ACCT_LOCKED) - ctx->sqo_mm->locked_vm += nr_pages; + ctx->mm_account->locked_vm += nr_pages; else if (acct == ACCT_PINNED) - atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm); + atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); } return 0; @@ -7918,9 +7928,12 @@ static void io_ring_ctx_free(struct io_r { io_finish_async(ctx); io_sqe_buffer_unregister(ctx); - if (ctx->sqo_mm) { - mmdrop(ctx->sqo_mm); - ctx->sqo_mm = NULL; + + if (ctx->sqo_task) { + put_task_struct(ctx->sqo_task); + ctx->sqo_task = NULL; + mmdrop(ctx->mm_account); + ctx->mm_account = NULL; } io_sqe_files_unregister(ctx); @@ -8665,8 +8678,16 @@ static int io_uring_create(unsigned entr ctx->user = user; ctx->creds = get_current_cred(); + ctx->sqo_task = get_task_struct(current); + + /* + * This is just grabbed for accounting purposes. When a process exits, + * the mm is exited and dropped before the files, hence we need to hang + * on to this mm purely for the purposes of being able to unaccount + * memory (locked/pinned vm). It's not used for anything else. + */ mmgrab(current->mm); - ctx->sqo_mm = current->mm; + ctx->mm_account = current->mm; /* * Account memory _before_ installing the file descriptor. Once Patches currently in stable-queue which might be from axboe@xxxxxxxxx are queue-5.9/io_uring-convert-advanced-xarray-uses-to-the-normal-api.patch queue-5.9/io_uring-reference-nsproxy-for-file-table-commands.patch queue-5.9/io_uring-stash-ctx-task-reference-for-sqpoll.patch queue-5.9/io_uring-return-cancelation-status-from-poll-timeout-files-handlers.patch queue-5.9/io_uring-fix-xarray-usage-in-io_uring_add_task_file.patch queue-5.9/io_uring-fix-use-of-xarray-in-__io_uring_files_cancel.patch queue-5.9/io-wq-fix-use-after-free-in-io_wq_worker_running.patch queue-5.9/io_uring-don-t-rely-on-weak-files-references.patch queue-5.9/io_uring-allow-timeout-poll-files-killing-to-take-task-into-account.patch queue-5.9/io_uring-move-dropping-of-files-into-separate-helper.patch queue-5.9/io_uring-unconditionally-grab-req-task.patch queue-5.9/io_uring-enable-task-files-specific-overflow-flushing.patch queue-5.9/io_wq-make-io_wqe-lock-a-raw_spinlock_t.patch queue-5.9/io_uring-no-need-to-call-xa_destroy-on-empty-xarray.patch