The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Jann Horn <jannh@google.com>,
	 linux-kernel@vger.kernel.org,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 2/2] proc: call task_vsize() without exec_update_lock held
Date: Thu, 26 Nov 2026 12:45:38 +0100	[thread overview]
Message-ID: <20261126-work-exec-update_lock-leftovers-v1-2-1024b26792bc@kernel.org> (raw)
In-Reply-To: <20261126-work-exec-update_lock-leftovers-v1-0-1024b26792bc@kernel.org>

Since commit 6650527444da ("proc: protect ptrace_may_access() with
exec_update_lock (part 1)") do_task_stat() holds the exec_update_lock of
the task from the access check until the last field is printed. In
between it calls task_vsize() and without an MMU that takes the mmap
lock to add up the vmas:

  locks held by stat-reader/46:
   #0: (&p->lock){+.+.}-{4:4}, at: seq_read_iter+0x38/0x32a
   #1: (&sig->exec_update_lock){++++}-{4:4}, at: do_task_stat+0x50/0xd04
   #2: (&mm->mmap_lock){++++}-{4:4}, at: task_vsize+0x5e/0xd2

So a read of /proc/<pid>/stat waits for the mmap lock with
exec_update_lock held. And without an MMU the mmap lock can be held for
a long time. A private mapping of a file is read into memory with it
held for writing:

  locks held by holder/47:
   #0: (&mm->mmap_lock){++++}-{4:4}, at: vm_mmap_pgoff+0x56/0x136
   #1: (nommu_region_sem){+.+.}-{4:4}, at: do_mmap+0x21e/0xb02
   #2: (&sb->s_type->i_mutex_key#7){.+.+}-{4:4}, at: blkdev_read_iter+0xa6/0x1c0

The lock is only needed for the access check and for what is read from
the task depending on it. The mm is pinned and everything else is shown
to everyone anyway. So drop the lock once that is done and call
task_vsize() afterwards.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/proc/array.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/proc/array.c b/fs/proc/array.c
index f6f75d206762a..3ff6f2455e5e4 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -493,7 +493,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
 	mm = get_task_mm(task);
 	if (mm) {
-		vsize = task_vsize(mm);
 		/*
 		 * esp and eip are intentionally zeroed out.  There is no
 		 * non-racy way to read them without freezing the task.
@@ -543,6 +542,17 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	if (permitted && (!whole || num_threads < 2))
 		wchan = !task_is_running(task);
 
+	/*
+	 * What depends on the access check has been read from the task or
+	 * comes from the mm that was pinned along with it. Don't hold
+	 * exec_update_lock for the rest, task_vsize() takes the mmap lock
+	 * without an MMU.
+	 */
+	up_read(&task->signal->exec_update_lock);
+
+	if (mm)
+		vsize = task_vsize(mm);
+
 	scoped_guard(rcu) {
 		scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
 			cmin_flt = sig->cmin_flt;
@@ -662,7 +672,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 		seq_puts(m, " 0");
 
 	seq_putc(m, '\n');
-	up_read(&task->signal->exec_update_lock);
 	if (mm)
 		mmput(mm);
 	return 0;

-- 
2.53.0


      parent reply	other threads:[~2026-11-26 11:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 11:45 [PATCH 0/2] exec: two more sleepers in the way of exec Christian Brauner
2026-11-26 11:45 ` [PATCH 1/2] perf: look up address filter files without ctx->mutex held Christian Brauner
2026-11-26 15:24   ` Peter Zijlstra
2026-11-26 11:45 ` Christian Brauner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126-work-exec-update_lock-leftovers-v1-2-1024b26792bc@kernel.org \
    --to=brauner@kernel.org \
    --cc=acme@kernel.org \
    --cc=jannh@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox