* [PATCH] acct: don't take the mmap lock to get the size of the mm
@ 2026-11-24 20:29 Christian Brauner
0 siblings, 0 replies; only message in thread
From: Christian Brauner @ 2026-11-24 20:29 UTC (permalink / raw)
To: linux-mm, David Hildenbrand; +Cc: linux-kernel, Christian Brauner (Amutable)
acct_collect() takes the mmap lock of the mm for the last thread of a
process and walks all of its vmas to add up their sizes. It does that
for every process that exits on a kernel with CONFIG_BSD_PROCESS_ACCT
whether anyone accounts or not.
The mmap lock is taken with mmap_read_lock(). So a task that exits waits
unkillably (violatin all kinds of grammar here probably) for a writer of
its mm in acct_collect().
I'm decluttering the exec path and moving all kinds of crap that people
do under exec_update_lock out of it and shrinking it's very annoying
existence.
Note that ac_mem can now be off by a mapping if the mm is shared with
another process that maps or unmaps something right then.
Luckily, acct() is deprecated and the mm keeps the sum of the sizes of
its vmas in mm->total_vm. That's what /proc/<pid>/stat reports as the
size of the mm and it doesn't take the mmap lock for it. So use it.
And fsck acct() and nommu, degrade it and use mm->total_vm there as
well. The total_vm is never lowered when a mapping goes away but who the
fsck uses acct() on nommu. And if they do, again, this is a deprecated
api.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
The quest to reduce the amount of unnecessary stalling that happens to
the exec codepath continues. Next victim, acct(). It has no maintainers
and b4 gives me zilch when to Cc so I'll just take the patch.
This makes acct_collect() get the size of the mm without taking the mmap
lock. A task that exits waits for that lock on every kernel with
CONFIG_BSD_PROCESS_ACCT whether anyone accounts or not and it can't be
killed there.
This degrades acct() but it's deprecated so let's do it.
---
kernel/acct.c | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/kernel/acct.c b/kernel/acct.c
index c440d43479ca5..9b99313bcb619 100644
--- a/kernel/acct.c
+++ b/kernel/acct.c
@@ -549,6 +549,12 @@ static void do_acct_process(struct bsd_acct_struct *acct)
current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
}
+/* The size of the mm in kB. */
+static unsigned long acct_vsize(struct mm_struct *mm)
+{
+ return READ_ONCE(mm->total_vm) << (PAGE_SHIFT - 10);
+}
+
/**
* acct_collect - collect accounting information into pacct_struct
* @exitcode: task exit code
@@ -560,20 +566,12 @@ void acct_collect(long exitcode, int group_dead)
u64 utime, stime;
unsigned long vsize = 0;
- if (group_dead && current->mm) {
- struct mm_struct *mm = current->mm;
- VMA_ITERATOR(vmi, mm, 0);
- struct vm_area_struct *vma;
-
- mmap_read_lock(mm);
- for_each_vma(vmi, vma)
- vsize += vma->vm_end - vma->vm_start;
- mmap_read_unlock(mm);
- }
+ if (group_dead && current->mm)
+ vsize = acct_vsize(current->mm);
spin_lock_irq(¤t->sighand->siglock);
if (group_dead)
- pacct->ac_mem = vsize / 1024;
+ pacct->ac_mem = vsize;
if (thread_group_leader(current)) {
pacct->ac_exitcode = exitcode;
if (current->flags & PF_FORKNOEXEC)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261124-work-acct-mmap_lock-1c55bf7181a8
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-24 20:30 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 20:29 [PATCH] acct: don't take the mmap lock to get the size of the mm Christian Brauner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®