tools/mm: add hwpoison-panic tool
Subsystems (2)
PATCH v2 1/1 tools/mm: add hwpoison-panic tool
Add a tool that enables the vm.panic_on_unrecoverable_memory_failure sysctl, picks a kernel-owned PFN and writes its physical address to hard_offline_page. Three page kinds are selectable with -k: rodata (default), slab or pgtable. In all cases the host should panic.
Example:
# ./hwpoison-panic -k slab --yes-panic-my-kernel
injecting hwpoison at phys 0x100032000 (pfn 0x100032, kind=slab)
expecting kernel panic: 'Memory failure: <pfn>: unrecoverable page'In dmesg, you will see:
Memory failure: 0x100032: unhandlable page.
Memory failure: 0x100032: recovery action for reserved kernel page: Ignored
Kernel panic - not syncing: Memory failure: 0x100032: unrecoverable pageThis lives in tools/mm rather than selftests/mm because every successful run crashes the machine, which is not something to run from CI.
The --yes-panic-my-kernel argument is required so an accidental invocation does not take the box down.
---
Changes in v2:
- Reword the file header, the tool triggers a panic rather than
confirming one (SJ Park)
- Rename -f to --yes-panic-my-kernel and say in usage() what it costs
(SJ Park)
- Call check_prereqs() before getpagesize() (SJ Park)
- Use 64-bit types for physical addresses and PFNs. On a 32-bit build
the old long/unsigned long pair truncated addresses above 4G, and the
negative error sentinel was indistinguishable from a valid address at
or above 2G
- Link to v1: https://patch.msgid.link/20260731-memory_failure_rewrite_test-v1-1-6aa8c6435693@debian.org
To: Andrew Morton <[email protected]>
To: Miaohe Lin <[email protected]>
To: Naoya Horiguchi <[email protected]>
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- MAINTAINERS | 1 + tools/mm/.gitignore | 1 + tools/mm/Makefile | 2 + tools/mm/memory-failure/hwpoison-panic.c | 358 +++++++++++++++++++++++++++++++ 4 files changed, 362 insertions(+)
MAINTAINERS +1 -0
@@ -12094,6 +12094,7 @@ F: include/linux/memory-failure.h F: include/trace/events/memory-failure.h F: mm/hwpoison-inject.c F: mm/memory-failure.c +F: tools/mm/memory-failure/ F: tools/testing/selftests/mm/memory-failure.c HYCON HY46XX TOUCHSCREEN SUPPORT
tools/mm/.gitignore +1 -0
@@ -3,3 +3,4 @@ slabinfo page-types page_owner_sort thp_swap_allocator_test +memory-failure/hwpoison-panic
tools/mm/Makefile +2 -0
@@ -4,6 +4,7 @@ include ../scripts/Makefile.include BUILD_TARGETS=page-types slabinfo page_owner_sort page_owner_filter thp_swap_allocator_test +BUILD_TARGETS+=memory-failure/hwpoison-panic INSTALL_TARGETS = $(BUILD_TARGETS) thpmaps LIB_DIR = ../lib/api
@@ -24,6 +25,7 @@ $(LIBS): clean: $(RM) page-types slabinfo page_owner_sort page_owner_filter thp_swap_allocator_test + $(RM) memory-failure/hwpoison-panic make -C $(LIB_DIR) clean sbindir ?= /usr/sbin
tools/mm/memory-failure/hwpoison-panic.c new file +358 -0
358 lines not on this page · raw message
---
base-commit: 95d6a9ccef99117115e41e9adb271243bd5e985b
change-id: 20260728-memory_failure_rewrite_test-7a309a601a6e
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Quoted 17 lines · Breno Leitao
Add a tool that enables the vm.panic_on_unrecoverable_memory_failure sysctl, picks a kernel-owned PFN and writes its physical address to
hard_offline_page. Three page kinds are selectable with -k: rodata (default), slab or pgtable. In all cases the host should panic.Example:
# ./hwpoison-panic -k slab --yes-panic-my-kernel injecting hwpoison at phys 0x100032000 (pfn 0x100032, kind=slab) expecting kernel panic: 'Memory failure: <pfn>: unrecoverable page'In dmesg, you will see:
Memory failure: 0x100032: unhandlable page. Memory failure: 0x100032: recovery action for reserved kernel page: Ignored Kernel panic - not syncing: Memory failure: 0x100032: unrecoverable page
Quoted 7 lines
This lives in
tools/mmrather than selftests/mm because every successful run crashes the machine, which is not something to run from CI.The --yes-panic-my-kernel argument is required so an accidental invocation does not take the box down.
Signed-off-by Breno Leitao <[email protected]>
This patch looks good to me with some nits below.
Quoted 12 lines
---
Changes in v2:
- Reword the file header, the tool triggers a panic rather than
confirming one (SJ Park)
- Rename -f to --yes-panic-my-kernel and say inusage()what it costs
(SJ Park)
- Callcheck_prereqs()beforegetpagesize()(SJ Park)
- Use 64-bit types for physical addresses and PFNs. On a 32-bit build
the old long/unsigned long pair truncated addresses above 4G, and the
negative error sentinel was indistinguishable from a valid address at
or above 2G
- Link to v1: https://patch.msgid.link/20260731-memory_failure_rewrite_test-v1-1-6aa8c6435693@debian.org
...
tools/mm/memory-failure/hwpoison-panic.c line 119
Quoted 4 lines
tools/mm/memory-failure/hwpoison-panic.c new file +4
-0+ +static int pick_kpageflags_phys_addr (uint64_t want,uint64_t *phys_addr) +{ +uint64_t pfn = (16UL <<20 ) / page_size;
Could we use SZ_16M macro here?
Quoted 45 lines
tools/mm/memory-failure/hwpoison-panic.c new file +45
-0+uint64_t flags; +int ret =-1 ; +int fd; + + fd =open (PROC_KPAGEFLAGS ,O_RDONLY ); +if (fd <0 ) +return -1 ; + +for (;kpageflags_read (fd, pfn, &flags) ==0 ; pfn++) { +if ((flags & want) && !(flags &BIT (HWPOISON )) && + !(flags &BIT (NOPAGE )) && !(flags &BIT (COMPOUND_TAIL ))) { + *phys_addr = pfn * page_size; + ret =0 ; +break ; + } + } + +close (fd); +return ret; +} + +static int read_sysctl (unsigned long *val) +{ +FILE *f =fopen (SYSCTL_PATH ,"r" ); +int ret; + +if (!f) +return -1 ; + ret =fscanf (f,"%lu" , val) ==1 ?0 :-1 ; +fclose (f); + +return ret; +} + +static int write_sysctl (unsigned long val) +{ +FILE *f =fopen (SYSCTL_PATH ,"w" ); +int ret; + +if (!f) +return -1 ; + ret =fprintf (f,"%lu" , val) <0 ?-1 :0 ; +fclose (f); + +return ret;
tools/mm/memory-failure/hwpoison-panic.c line 174
Quoted 10 lines
tools/mm/memory-failure/hwpoison-panic.c new file +10
-0+} + +/* hard_offline_page() injects with MF_SW_SIMULATED, so unpoison is allowed. */ +static void unpoison_pfn (uint64_t pfn) +{ +char path[PATH_MAX ], buf[32 ]; +const char *debugfs; +int fd, len; + + debugfs =debugfs__mount ();
I might be miss something but I can't find the implementation of debugfs__mount.
Quoted 14 lines
tools/mm/memory-failure/hwpoison-panic.c new file +14
-0+if (!debugfs) +return ; + +snprintf (path,sizeof (path),"%s/hwpoison/unpoison-pfn" , debugfs); + fd =open (path,O_WRONLY ); +if (fd <0 ) +return ; + + len =snprintf (buf,sizeof (buf),"0x%llx\n" , (unsigned long long )pfn); +if (write (fd, buf, len) <0 ) +perror ("unpoison-pfn" ); +close (fd); +} +
...
tools/mm/memory-failure/hwpoison-panic.c line 283
Quoted 10 lines
tools/mm/memory-failure/hwpoison-panic.c new file +10
-0+ +static void check_prereqs (int armed) +{ +if (geteuid ()) +fatal ("must run as root\n" ); +if (access (SYSCTL_PATH ,W_OK )) +fatal ("%s not present (kernel without the sysctl?)\n" , +SYSCTL_PATH ); +if (access (INJECT_PATH ,W_OK )) +fatal ("%s not present (no MEMORY_HOTPLUG?)\n" ,INJECT_PATH );
MEMORY_HOTPLUG?
Thanks.
.
Hello Miaohe,
First of all, thanks for looking at this one.
Quoted 6 lines · Miaohe Lin
tools/mm/memory-failure/hwpoison-panic.c line 119
Quoted 3 lines · Breno Leitao
tools/mm/memory-failure/hwpoison-panic.c new file +3
-0+static int pick_kpageflags_phys_addr (uint64_t want,uint64_t *phys_addr) +{ +uint64_t pfn = (16UL <<20 ) / page_size;Could we use
SZ_16Mmacro here?
Sure, for that I will include ../../include/linux/sizes.h in the next version.
Quoted 10 lines
tools/mm/memory-failure/hwpoison-panic.c line 174
Quoted 8 lines
tools/mm/memory-failure/hwpoison-panic.c new file +8
-0+/* hard_offline_page() injects with MF_SW_SIMULATED, so unpoison is allowed. */ +static void unpoison_pfn (uint64_t pfn) +{ +char path[PATH_MAX ], buf[32 ]; +const char *debugfs; +int fd, len; + + debugfs =debugfs__mount ();I might be miss something but I can't find the implementation of
debugfs__mount.
It is an ugly macro declared in tools/lib/api/fs/fs.h:
#define FS(name) \
const char *name##__mountpoint(void); \
const char *name##__mount(void); \
bool name##__configured(void);And then:
FS(debugfs)
Quoted 4 lines
tools/mm/memory-failure/hwpoison-panic.c line 283
Quoted 2 lines
tools/mm/memory-failure/hwpoison-panic.c new file +2
-0+if (access (INJECT_PATH ,W_OK )) +fatal ("%s not present (no MEMORY_HOTPLUG?)\n" ,INJECT_PATH );
MEMORY_HOTPLUG?
lol. it should be MEMORY_FAILURE
I will get these points fixed and respin.
--breno
Andrew Morton
maintainerQuoted 11 lines · On Mon, 03 Aug 2026 04:31:20 -0700 Breno Leitao <[email protected]> wrote:
Add a tool that enables the vm.panic_on_unrecoverable_memory_failure sysctl, picks a kernel-owned PFN and writes its physical address to
hard_offline_page. Three page kinds are selectable with -k: rodata (default), slab or pgtable. In all cases the host should panic.Example:
# ./hwpoison-panic -k slab --yes-panic-my-kernel injecting hwpoison at phys 0x100032000 (pfn 0x100032, kind=slab) expecting kernel panic: 'Memory failure: <pfn>: unrecoverable page'
Quoted 8 lines
In dmesg, you will see:
Memory failure: 0x100032: unhandlable page. Memory failure: 0x100032: recovery action for reserved kernel page: Ignored Kernel panic - not syncing: Memory failure: 0x100032: unrecoverable pageThis lives in
tools/mmrather than selftests/mm because every successful run crashes the machine, which is not something to run from CI.
I wonder if we can fix this. Add a knob which tells the kernel not to panic/oops/crash/etc. Instead it sends a message "i tried to crash but you told me not to" and proceeds.
Set that knob around your hwpoison-panic tool and voila, it's now a selftest.
Wondering if we can sneakily use fault-injection to do this. Add a new i_should_crash_now() and use fault-injection to make this function "fail", by returning zero. Or something else.
Quoted 22 lines · Andrew Morton
Quoted 11 lines · On Mon, 03 Aug 2026 04:31:20 -0700 Breno Leitao <[email protected]> wrote:
Add a tool that enables the vm.panic_on_unrecoverable_memory_failure sysctl, picks a kernel-owned PFN and writes its physical address to
hard_offline_page. Three page kinds are selectable with -k: rodata (default), slab or pgtable. In all cases the host should panic.Example:
# ./hwpoison-panic -k slab --yes-panic-my-kernel injecting hwpoison at phys 0x100032000 (pfn 0x100032, kind=slab) expecting kernel panic: 'Memory failure: <pfn>: unrecoverable page'Quoted 8 lines
In dmesg, you will see:
Memory failure: 0x100032: unhandlable page. Memory failure: 0x100032: recovery action for reserved kernel page: Ignored Kernel panic - not syncing: Memory failure: 0x100032: unrecoverable pageThis lives in
tools/mmrather than selftests/mm because every successful run crashes the machine, which is not something to run from CI.
Quoted 6 lines
I wonder if we can fix this. Add a knob which tells the kernel not to panic/oops/crash/etc. Instead it sends a message "i tried to crash but you told me not to" and proceeds.
Set that knob around your hwpoison-panic tool and voila, it's now a selftest.
We just shot down a random "kernel-owned PFN" (marking it hwpoison etc).
That's really not something you want to do from a selftest.
You'd need some kind of hwpoison simulation mode.
Signature · 3 lines
Cheers, David