Skip to contentSkip to conversation

mm/oom_kill: optionally log victim command-line arguments on OOM kill

RFC PATCH In review Aaron Tomlin 2 participants 3 messages Nov 24 – Nov 25

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

RFC PATCH 1/1 mm/oom_kill: optionally log victim command-line arguments on OOM kill

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
---
 Documentation/admin-guide/sysctl/vm.rst |  30 ++++
 mm/oom_kill.c                           | 206 ++++++++++++++++++++++++
 2 files changed, 236 insertions(+)
Documentation/admin-guide/sysctl/vm.rst +30 -0
index 3cb0138e850c..55f76ff25955 100644
@@ -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
 ==============
 
mm/oom_kill.c +206 -0

236 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw

Nackity nackity NACK NACK NACK!

This is terribly, horribly broken both in concept and implementation.

Quoted 9 lines · Aaron Tomlin

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.

Quoted 9 lines

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.

Quoted 3 lines

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!

Quoted 6 lines

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

Quoted 8 lines

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...

Quoted 9 lines

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! :)

Quoted 1 line

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.

Quoted 4 lines
---
 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

#2 lore raw
Quoted 3 lines · Lorenzo Stoakes (ARM)

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,

Signature · 1 line
Aaron Tomlin