* [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill
@ 2026-11-24 20:50 Aaron Tomlin
2026-11-25 15:33 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 3+ messages in thread
From: Aaron Tomlin @ 2026-11-24 20:50 UTC (permalink / raw)
To: david, corbet, akpm
Cc: ljs, liam, vbabka, rppt, surenb, mhocko, skhan, rdunlap, rientjes,
shakeel.butt, sean, neelx, linux-mm, linux-doc
When the OOM killer terminates a task in __oom_kill_process(), the
kernel log records only victim->comm alongside its PID and memory
counters. Because comm[] is truncated to TASK_COMM_LEN and typically
reflects the interpreter binary rather than the executed programme or
script (e.g. "python3" instead of /usr/bin/python3 -sP
/usr/bin/firewalld --nofork --nopid), identifying which service or
worker instance was killed during post-mortem analysis is often
difficult once the process has exited and /proc/[pid]/cmdline is gone.
The standard get_cmdline() helper cannot be called from
__oom_kill_process(). OOM kill paths run whilst holding oom_lock, so
blocking on mmap_read_lock_killable() risks deadlocking against a
thread holding mmap_write_lock() whilst waiting for memory allocation
on oom_lock. Furthermore, get_cmdline() walks user pages via
access_process_vm(), which triggers page faults, memory allocations,
and swap-in I/O during an active OOM condition, and drops an mm_users
reference via mmput() that could run synchronous exit_mmap() teardown
under oom_lock.
Introduce an opt-in vm.oom_dump_cmdline sysctl (documented in
Documentation/admin-guide/sysctl/vm.rst), disabled by default to
preserve existing behaviour and avoid exposing potentially sensitive
command-line arguments unless explicitly requested by the
administrator. When enabled on CONFIG_MMU builds, __oom_kill_process()
attempts a best-effort, non-blocking read of the victim's resident
argument vector (including setproctitle(3) updates inspected via
arg_end - 1) after releasing task_lock(victim). It snapshots the
argument and environment bounds under mm->arg_lock, acquires mmap_lock
via mmap_read_trylock(), bails out if MMF_OOM_SKIP or MMF_UNSTABLE is
set, and walks only present, non-PageHWPoison() anonymous page table
entries via folio_walk_start() without faulting or allocating memory.
After dropping the page table lock, each pinned subpage is flushed via
flush_anon_page() and copied across kmap_local_page() and
copy_from_user_page(), and mmap_lock is released prior to calling
printk.
To preserve backwards compatibility with monitoring tools that parse
the existing "Killed process %d (%s) ..." message, leave that line
untouched and emit a separate rate-limited companion line sanitised
with %*pE when resident argument bytes are recovered. For example:
[ 124.819203] Out of memory: Killed process 1482 (python3) total-vm:184320kB, anon-rss:42100kB, ...
[ 124.819215] Out of memory: Process 1482 (python3) cmdline: /usr/bin/python3 -sP /usr/bin/firewalld --nofork --nopid
Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
---
Documentation/admin-guide/sysctl/vm.rst | 30 ++++
mm/oom_kill.c | 206 ++++++++++++++++++++++++
2 files changed, 236 insertions(+)
diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst
index 3cb0138e850c..55f76ff25955 100644
--- a/Documentation/admin-guide/sysctl/vm.rst
+++ b/Documentation/admin-guide/sysctl/vm.rst
@@ -56,6 +56,7 @@ Currently, these files are in /proc/sys/vm:
- nr_overcommit_hugepages
- nr_trim_pages (only if CONFIG_MMU=n)
- numa_zonelist_order
+- oom_dump_cmdline
- oom_dump_tasks
- oom_kill_allocating_task
- overcommit_kbytes
@@ -784,6 +785,35 @@ Default order is recommended unless this is causing problems for your
system/application.
+oom_dump_cmdline
+================
+
+Enables best-effort logging of an OOM victim's command-line arguments
+as a separate rate-limited companion message alongside the existing
+"Killed process" kernel log line when the OOM killer terminates a task
+(available only when ``CONFIG_MMU`` is enabled). This helps identify
+which script or worker instance was killed when ``comm`` only records
+an interpreter binary name (for example, ``python3``) and
+``/proc/[pid]/cmdline`` disappears once the task exits.
+
+Because the OOM killer runs under ``oom_lock`` and cannot safely sleep
+on ``mmap_lock`` or trigger page faults, only a bounded prefix of
+anonymous pages already resident in memory is inspected, with
+non-printable characters escaped. If ``mmap_lock`` is contended or the
+argument pages have already been reclaimed to swap, the companion line
+is omitted or marked ``(truncated)``.
+
+Command-line arguments may contain sensitive information that would
+otherwise be restricted via ``/proc`` mount options such as
+``hidepid``, whereas the kernel log buffer may be readable by
+unprivileged users when ``kernel.dmesg_restrict`` is disabled.
+
+= ===================================================================
+0 Suppress OOM victim command-line logging (default).
+1 Enable best-effort OOM victim command-line logging.
+= ===================================================================
+
+
oom_dump_tasks
==============
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 5d48bd862c27..46d22c7b3f37 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -45,6 +45,11 @@
#include <linux/mmu_notifier.h>
#include <linux/cred.h>
#include <linux/nmi.h>
+#include <linux/cacheflush.h>
+#include <linux/highmem.h>
+#include <linux/overflow.h>
+#include <linux/pagewalk.h>
+#include <linux/uaccess.h>
#include <asm/tlb.h>
#include "internal.h"
@@ -688,6 +693,191 @@ static void queue_oom_reaper(struct task_struct *tsk)
add_timer(&tsk->oom_reaper_timer);
}
+#define OOM_CMDLINE_LEN 256
+
+static int sysctl_oom_dump_cmdline;
+
+/**
+ * oom_copy_mm_resident() - copy resident anonymous user memory without faulting
+ * @mm: target address space to copy from
+ * @addr: starting user virtual address
+ * @buf: destination kernel buffer
+ * @len: maximum number of bytes to copy
+ *
+ * Walks present, non-PageHWPoison() page table entries in anonymous VMAs of
+ * @mm and copies up to @len contiguous resident bytes into @buf without
+ * triggering page faults, memory allocations, or sleeping locks.
+ *
+ * Context: Process context. Caller must hold @mm->mmap_lock for read.
+ * Return: Number of bytes copied (may be less than @len if an unmapped,
+ * non-anonymous, non-resident, or poisoned page is encountered).
+ */
+static size_t oom_copy_mm_resident(struct mm_struct *mm, unsigned long addr,
+ char *buf, size_t len)
+{
+ size_t copied = 0;
+
+ addr = untagged_addr_remote(mm, addr);
+
+ while (len > 0) {
+ struct vm_area_struct *vma;
+ struct folio_walk fw;
+ struct folio *folio;
+ struct page *page;
+ size_t offset, bytes;
+ void *maddr;
+
+ vma = vma_lookup(mm, addr);
+ if (!vma || !vma_is_anonymous(vma))
+ break;
+
+ folio = folio_walk_start(&fw, vma, addr, 0);
+ if (!folio)
+ break;
+
+ page = fw.page;
+ if (unlikely(PageHWPoison(page))) {
+ folio_walk_end(&fw, vma);
+ break;
+ }
+ folio_get(folio);
+ folio_walk_end(&fw, vma);
+
+ flush_anon_page(vma, page, addr);
+ offset = offset_in_page(addr);
+ bytes = min_t(size_t, len, PAGE_SIZE - offset);
+ maddr = kmap_local_page(page);
+ copy_from_user_page(vma, page, addr, buf + copied,
+ maddr + offset, bytes);
+ kunmap_local(maddr);
+ folio_put(folio);
+
+ copied += bytes;
+ len -= bytes;
+ if (len && check_add_overflow(addr, bytes, &addr))
+ break;
+ }
+
+ return copied;
+}
+
+static noinline_for_stack void __oom_dump_cmdline(struct task_struct *victim,
+ struct mm_struct *mm,
+ const char *message)
+{
+ static DEFINE_RATELIMIT_STATE(oom_cmdline_rs,
+ DEFAULT_RATELIMIT_INTERVAL,
+ DEFAULT_RATELIMIT_BURST);
+ unsigned long arg_start, arg_end, env_start, env_end, arg_len;
+ size_t to_copy, copied;
+ bool truncated = false;
+ bool proctitle;
+ char buf[OOM_CMDLINE_LEN];
+
+ spin_lock(&mm->arg_lock);
+ arg_start = mm->arg_start;
+ arg_end = mm->arg_end;
+ env_start = mm->env_start;
+ env_end = mm->env_end;
+ spin_unlock(&mm->arg_lock);
+
+ if (!env_end || arg_start >= arg_end)
+ return;
+
+ arg_len = arg_end - arg_start;
+ to_copy = min_t(unsigned long, arg_len, sizeof(buf));
+
+ if (!mmap_read_trylock(mm))
+ return;
+
+ if (mm_flags_test(MMF_OOM_SKIP, mm) ||
+ mm_flags_test(MMF_UNSTABLE, mm)) {
+ mmap_read_unlock(mm);
+ return;
+ }
+
+ copied = oom_copy_mm_resident(mm, arg_start, buf, to_copy);
+ if (!copied) {
+ mmap_read_unlock(mm);
+ return;
+ }
+
+ if (copied == arg_len) {
+ proctitle = buf[copied - 1] != '\0';
+ } else {
+ char c;
+
+ proctitle = oom_copy_mm_resident(mm, arg_end - 1, &c, 1) == 1 &&
+ c != '\0';
+ }
+
+ if (proctitle) {
+ /*
+ * The NUL terminator at arg_end - 1 was overwritten, which
+ * indicates setproctitle(3); treat the buffer as a single
+ * NUL-terminated string that may spill into [env_start, env_end).
+ */
+ unsigned long max_len = arg_len;
+ size_t n;
+
+ if (env_start == arg_end && env_end > env_start)
+ max_len = env_end - arg_start;
+
+ n = strnlen(buf, copied);
+ if (n == copied && copied == arg_len &&
+ max_len > arg_len && copied < sizeof(buf)) {
+ size_t rem = min_t(unsigned long, max_len - arg_len,
+ sizeof(buf) - copied);
+
+ copied += oom_copy_mm_resident(mm, env_start,
+ buf + copied, rem);
+ n = strnlen(buf, copied);
+ }
+ truncated = (n == copied && copied < max_len);
+ copied = n;
+ } else {
+ size_t i;
+
+ truncated = copied < arg_len;
+ while (copied > 0 && buf[copied - 1] == '\0')
+ copied--;
+ for (i = 0; i < copied; i++) {
+ if (buf[i] == '\0')
+ buf[i] = ' ';
+ }
+ }
+ mmap_read_unlock(mm);
+
+ if (!copied || !__ratelimit(&oom_cmdline_rs))
+ return;
+
+ pr_err("%s: Process %d (%s) cmdline%s: %*pE\n",
+ message, task_pid_nr(victim), victim->comm,
+ truncated ? " (truncated)" : "", (int)copied, buf);
+}
+
+/**
+ * oom_dump_cmdline() - best-effort log of an OOM victim's command line
+ * @victim: OOM victim task
+ * @mm: victim's address space
+ * @message: OOM context prefix string
+ *
+ * When enabled via vm.oom_dump_cmdline, emits a separate rate-limited
+ * companion kernel log line containing @victim's resident command-line
+ * arguments (with non-printable bytes escaped). Uses a non-blocking
+ * mmap_read_trylock() and skips logging if @mm is contended or already
+ * being torn down.
+ *
+ * Context: Process context. Must be called after releasing task_lock(@victim)
+ * while holding references on @victim and @mm (mmgrab()).
+ */
+static inline void oom_dump_cmdline(struct task_struct *victim,
+ struct mm_struct *mm, const char *message)
+{
+ if (unlikely(READ_ONCE(sysctl_oom_dump_cmdline)))
+ __oom_dump_cmdline(victim, mm, message);
+}
+
#ifdef CONFIG_SYSCTL
static const struct ctl_table vm_oom_kill_table[] = {
{
@@ -713,6 +903,15 @@ static const struct ctl_table vm_oom_kill_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec,
},
+ {
+ .procname = "oom_dump_cmdline",
+ .data = &sysctl_oom_dump_cmdline,
+ .maxlen = sizeof(sysctl_oom_dump_cmdline),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
};
#endif
@@ -729,6 +928,11 @@ subsys_initcall(oom_init)
static inline void queue_oom_reaper(struct task_struct *tsk)
{
}
+
+static inline void oom_dump_cmdline(struct task_struct *victim,
+ struct mm_struct *mm, const char *message)
+{
+}
#endif /* CONFIG_MMU */
/**
@@ -944,6 +1148,8 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
task_unlock(victim);
+ oom_dump_cmdline(victim, mm, message);
+
/*
* Kill all user processes sharing victim->mm in other thread groups, if
* any. They don't get access to memory reserves, though, to avoid
--
2.55.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill
2026-11-24 20:50 [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill Aaron Tomlin
@ 2026-11-25 15:33 ` Lorenzo Stoakes (ARM)
2026-11-25 16:20 ` Aaron Tomlin
0 siblings, 1 reply; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-25 15:33 UTC (permalink / raw)
To: Aaron Tomlin
Cc: david, corbet, akpm, liam, vbabka, rppt, surenb, mhocko, skhan,
rdunlap, rientjes, shakeel.butt, sean, neelx, linux-mm, linux-doc
Nackity nackity NACK NACK NACK!
This is terribly, horribly broken both in concept and implementation.
On Tue, Nov 24, 2026 at 03:50:43PM -0500, Aaron Tomlin wrote:
> When the OOM killer terminates a task in __oom_kill_process(), the
> kernel log records only victim->comm alongside its PID and memory
> counters. Because comm[] is truncated to TASK_COMM_LEN and typically
> reflects the interpreter binary rather than the executed programme or
> script (e.g. "python3" instead of /usr/bin/python3 -sP
> /usr/bin/firewalld --nofork --nopid), identifying which service or
> worker instance was killed during post-mortem analysis is often
> difficult once the process has exited and /proc/[pid]/cmdline is gone.
>
> The standard get_cmdline() helper cannot be called from
> __oom_kill_process(). OOM kill paths run whilst holding oom_lock, so
> blocking on mmap_read_lock_killable() risks deadlocking against a
> thread holding mmap_write_lock() whilst waiting for memory allocation
> on oom_lock. Furthermore, get_cmdline() walks user pages via
> access_process_vm(), which triggers page faults, memory allocations,
> and swap-in I/O during an active OOM condition, and drops an mm_users
> reference via mmput() that could run synchronous exit_mmap() teardown
> under oom_lock.
Also, fiddling with the memory of a soon-to-die process under extreme
memory pressure is insane, additionally _holding that lock_ under memory
pressure is insane.
>
> Introduce an opt-in vm.oom_dump_cmdline sysctl (documented in
> Documentation/admin-guide/sysctl/vm.rst), disabled by default to
'Have mm people support my code literally forever'... and make it an ABI
requirement too!
> preserve existing behaviour and avoid exposing potentially sensitive
> command-line arguments unless explicitly requested by the
> administrator. When enabled on CONFIG_MMU builds, __oom_kill_process()
> attempts a best-effort, non-blocking read of the victim's resident
> argument vector (including setproctitle(3) updates inspected via
> arg_end - 1) after releasing task_lock(victim). It snapshots the
> argument and environment bounds under mm->arg_lock, acquires mmap_lock
> via mmap_read_trylock(), bails out if MMF_OOM_SKIP or MMF_UNSTABLE is
> set, and walks only present, non-PageHWPoison() anonymous page table
> entries via folio_walk_start() without faulting or allocating memory.
> After dropping the page table lock, each pinned subpage is flushed via
> flush_anon_page() and copied across kmap_local_page() and
> copy_from_user_page(), and mmap_lock is released prior to calling
> printk.
Ah the giant slabs of impenetrable text that reviewers are supposed to read in
detail now... LLMs really have a lot to answer for.
You (or far more likely your LLM) is just describing the (horribly broken)
code in English here.
I hate how often I have to read this stuff...
>
> To preserve backwards compatibility with monitoring tools that parse
> the existing "Killed process %d (%s) ..." message, leave that line
> untouched and emit a separate rate-limited companion line sanitised
> with %*pE when resident argument bytes are recovered. For example:
>
> [ 124.819203] Out of memory: Killed process 1482 (python3) total-vm:184320kB, anon-rss:42100kB, ...
> [ 124.819215] Out of memory: Process 1482 (python3) cmdline: /usr/bin/python3 -sP /usr/bin/firewalld --nofork --nopid
>
Doubling the output in a way that is super confusing, great! :)
> Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
This patch is broken in a million different ways.
The process gets sent SIGKILL and _then_ you race it to take the mmap read
lock, which prevents the reaper from running?
You do a lock inversion between oom_lock and the mmap_lock, and hope it'll
be OK?
You take a heavy rwsem lock while the system is under extreme memory
pressure? Which, again, prevents the reaper getting to it?
You do heavy operations during one of the most sensitive, dangerous times
for a kernel for a nice-to-have annotation?
And dump_oom_victim() already outputs memcg information, which systemd
uses, your example mentions firewalld so likely that'd _already_ give you
the kind of information you are interested in (except for mutliple
workers).
You're adding a ton of totally broken complexity then, insanely, making it
a global sysctl flag that we have to support forever as an ABI too.
You intentionally abuse folio_walk_start() to do _what its kdoc tells you
not to_:
* This function must *not* be used as a naive replacement for
* get_user_pages() / pin_user_pages(), especially not to perform DMA or
* to carelessly modify page content. This function may *only* be used to grab
* short-term folio references, never to grab long-term folio references.
You're (or more likely your LLM) is literally copy/pasting code from
__access_remote_vm() combined with this mad abuse of folio_walk_*() as a
proto-GUP to... get more info about a killed process.
You also copy/pasted wrong so this is even more subtly broken.
You are also potentially exposing sensitive information. Your ratelimiting
is broken.
Oh and of course, you add 256 bytes to the VERY limited kernel stack at the
point of an OOMK.
I mean I could go on. Even the concept here is fundamentally broken, so
please don't waste our time with respins.
Thanks.
> ---
> Documentation/admin-guide/sysctl/vm.rst | 30 ++++
> mm/oom_kill.c | 206 ++++++++++++++++++++++++
> 2 files changed, 236 insertions(+)
Of course it's also a massive patch...
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill
2026-11-25 15:33 ` Lorenzo Stoakes (ARM)
@ 2026-11-25 16:20 ` Aaron Tomlin
0 siblings, 0 replies; 3+ messages in thread
From: Aaron Tomlin @ 2026-11-25 16:20 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: david, corbet, akpm, liam, vbabka, rppt, surenb, mhocko, skhan,
rdunlap, rientjes, shakeel.butt, sean, neelx, linux-mm, linux-doc
On Wed, Nov 25, 2026 at 03:33:28PM +0000, Lorenzo Stoakes (ARM) wrote:
> Nackity nackity NACK NACK NACK!
>
> This is terribly, horribly broken both in concept and implementation.
Hi Lorenzo,
Fair enough.
For context, using mmap_read_trylock() before queue_oom_reaper() was
intentional to avoid blocking under oom_lock, which is why I sent it as an
RFC. That said, I take your point: touching mmap_lock and walking page
tables via folio_walk_start() in __oom_kill_process(), along with the stack
footprint and adding a new vm sysctl ABI, is the wrong trade-off at OOM
time when dump_oom_victim() provides memcg context.
Thanks for taking a look.
Kind regards,
--
Aaron Tomlin
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 16:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 20:50 [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill Aaron Tomlin
2026-11-25 15:33 ` Lorenzo Stoakes (ARM)
2026-11-25 16:20 ` Aaron Tomlin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox