From: Aaron Tomlin <atomlin@atomlin.com>
To: david@kernel.org, corbet@lwn.net, akpm@linux-foundation.org
Cc: ljs@kernel.org, liam@infradead.org, vbabka@kernel.org,
rppt@kernel.org, surenb@google.com, mhocko@suse.com,
skhan@linuxfoundation.org, rdunlap@infradead.org,
rientjes@google.com, shakeel.butt@linux.dev, sean@ashe.io,
neelx@suse.com, linux-mm@kvack.org, linux-doc@vger.kernel.org
Subject: [RFC PATCH] mm/oom_kill: optionally log victim command-line arguments on OOM kill
Date: Tue, 24 Nov 2026 15:50:43 -0500 [thread overview]
Message-ID: <20261124205043.1010247-1-atomlin@atomlin.com> (raw)
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
reply other threads:[~2026-11-24 20:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261124205043.1010247-1-atomlin@atomlin.com \
--to=atomlin@atomlin.com \
--cc=akpm@linux-foundation.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=liam@infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=neelx@suse.com \
--cc=rdunlap@infradead.org \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=sean@ashe.io \
--cc=shakeel.butt@linux.dev \
--cc=skhan@linuxfoundation.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.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