selftests/mm: add memory failure selftests
Subsystems (4)
Miaohe Lin
maintainerv3 cover selftests/mm: add memory failure selftests
Introduce selftests to validate the functionality of memory failure. These tests help ensure that memory failure handling for anonymous pages, pagecaches pages works correctly, including proper SIGBUS delivery to user processes, page isolation, and recovery paths.
Currently madvise syscall is used to inject memory failures. And only anonymous pages and pagecaches are tested. More test scenarios, e.g. hugetlb, shmem, thp, will be added. Also more memory failure injecting methods will be supported, e.g. APEI Error INJection, if required.
---
Changes in v3:
Add HWPOISON_INJECT and MEMORY_FAILURE to config per Mark. Fix kernel test robot warning.
Changes in v2:
Add some codes to guard pagecache testcases against tmpfs. Testcases
for tmpfs will be added soon.
Thanks!
---
Miaohe Lin (3):
selftests/mm: add memory failure anonymous page test selftests/mm: add memory failure clean pagecache test selftests/mm: add memory failure dirty pagecache test
MAINTAINERS | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/config | 2 + tools/testing/selftests/mm/memory-failure.c | 359 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 21 ++ tools/testing/selftests/mm/vm_util.c | 41 +++ tools/testing/selftests/mm/vm_util.h | 3 + 8 files changed, 429 insertions(+) create mode 100644 tools/testing/selftests/mm/memory-failure.c
Signature · 1 line
2.33.0
Miaohe Lin
maintainerPATCH v3 1/3 selftests/mm: add memory failure anonymous page test
This patch adds a new kselftest to validate memory failure handling for anonymous pages. The test performs the following operations:
1. Allocates anonymous pages using mmap().
2. Injects memory failure via madvise syscall.
3. Verifies expected error handling behavior.
4. Unpoison memory.
This test helps ensure that memory failure handling for anonymous pages works correctly, including proper SIGBUS delivery to user processes, page isolation and recovery paths.
--- MAINTAINERS | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/config | 2 + tools/testing/selftests/mm/memory-failure.c | 239 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 21 ++ tools/testing/selftests/mm/vm_util.c | 41 ++++ tools/testing/selftests/mm/vm_util.h | 3 + 8 files changed, 309 insertions(+) create mode 100644 tools/testing/selftests/mm/memory-failure.c
MAINTAINERS +1 -0
@@ -11691,6 +11691,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/testing/selftests/mm/memory-failure.c HYCON HY46XX TOUCHSCREEN SUPPORT M: Giulio Benetti <[email protected]>
tools/testing/selftests/mm/.gitignore +1 -0
@@ -12,6 +12,7 @@ map_hugetlb map_populate thuge-gen compaction_test +memory-failure migration mlock2-tests mrelease_test
tools/testing/selftests/mm/Makefile +1 -0
@@ -75,6 +75,7 @@ TEST_GEN_FILES += map_populate ifneq (,$(filter $(ARCH),arm64 riscv riscv64 x86 x86_64)) TEST_GEN_FILES += memfd_secret endif +TEST_GEN_FILES += memory-failure TEST_GEN_FILES += migration TEST_GEN_FILES += mkdirty TEST_GEN_FILES += mlock-random-test
tools/testing/selftests/mm/config +2 -0
@@ -11,3 +11,5 @@ CONFIG_ANON_VMA_NAME=y CONFIG_FTRACE=y CONFIG_PROFILING=y CONFIG_UPROBES=y +CONFIG_MEMORY_FAILURE=y +CONFIG_HWPOISON_INJECT=m
tools/testing/selftests/mm/memory-failure.c new file +239 -0
239 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +21 -0
@@ -91,6 +91,8 @@ separated by spaces: test VMA merge cases behave as expected - rmap test rmap behaves as expected +- memory-failure + test memory-failure behaves as expected example: ./run_vmtests.sh -t "hmm mmap ksm" EOF
@@ -527,6 +529,25 @@ CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned CATEGORY="rmap" run_test ./rmap +# Try to load hwpoison_inject if not present. +HWPOISON_DIR=/sys/kernel/debug/hwpoison/ +if [ ! -d "$HWPOISON_DIR" ]; then + if ! modprobe -q -R hwpoison_inject; then + echo "Module hwpoison_inject not found, skipping..." + else + modprobe hwpoison_inject > /dev/null 2>&1 + LOADED_MOD=1 + fi +fi + +if [ -d "$HWPOISON_DIR" ]; then + CATEGORY="memory-failure" run_test ./memory-failure +fi + +if [ -n "${LOADED_MOD}" ]; then + modprobe -r hwpoison_inject > /dev/null 2>&1 +fi + if [ "${HAVE_HUGEPAGES}" = 1 ]; then echo "$orig_nr_hugepgs" > /proc/sys/vm/nr_hugepages fi
tools/testing/selftests/mm/vm_util.c +41 -0
44 lines not on this page · raw message
tools/testing/selftests/mm/vm_util.h +3 -0
@@ -20,6 +20,7 @@#define KPF_COMPOUND_HEAD BIT_ULL(15)#define KPF_COMPOUND_TAIL BIT_ULL(16) +#define KPF_HWPOISON BIT_ULL(19)#define KPF_THP BIT_ULL(22)/* * Ignore the checkpatch warning, we must read from x but don't want to do
@@ -154,6 +155,8 @@ long ksm_get_full_scans(void);int ksm_use_zero_pages (void );int ksm_start (void );int ksm_stop (void ); +int get_hardware_corrupted_size (unsigned long *val); +int unpoison_memory (unsigned long pfn);/* * On ppc64 this will only work with radix 2M hugepage size
Signature · 1 line
2.33.0
Re: [PATCH v3 1/3] selftests/mm: add memory failure anonymous page test
tools/testing/selftests/mm/run_vmtests.sh separated by spaces: · line 95
Quoted 11 lines · Miaohe Lin
tools/testing/selftests/mm/memory-failure.c new file +1
-0+TEST_HARNESS_MAIN tools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -91,6 +91,8 @@ separated by spaces: test VMA merge cases behave as expected - rmap test rmap behaves as expected +- memory-failure + test memory-failure behaves as expected
In -next I added a hack with wrapper scripts per category to improve the integration of the mm selftests with the kselftest framework, could you please wire things up into that, see:
6ce964c02f1c (selftests/mm: have the harness run each test category separately)
for details. It's rather dumb and simple but gets the job done.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] selftests/mm: add memory failure anonymous page test
Quoted 18 lines · Mark Brown
tools/testing/selftests/mm/run_vmtests.sh separated by spaces: · line 95
Quoted 11 lines · Miaohe Lin
(quoted diff) +1
-0+TEST_HARNESS_MAINtools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -91,6 +91,8 @@ separated by spaces: test VMA merge cases behave as expected - rmap test rmap behaves as expected +- memory-failure + test memory-failure behaves as expectedIn -next I added a hack with wrapper scripts per category to improve the integration of the mm selftests with the kselftest framework, could you please wire things up into that, see:
Quoted 1 line
6ce964c02f1c(selftests/mm: have the harness run each test category separately)
Sorry, I missed that. Do you mean adding something like below to run memory-failure test separately?
tools/testing/selftests/mm/Makefile +1 -0
@@ -171,6 +171,7 @@ TEST_PROGS += ksft_thp.sh TEST_PROGS += ksft_userfaultfd.sh TEST_PROGS += ksft_vma_merge.sh TEST_PROGS += ksft_vmalloc.sh +TEST_PROGS += ksft_memory_failure.sh TEST_FILES := test_vmalloc.sh TEST_FILES += test_hmm.sh
tools/testing/selftests/mm/ksft_memory_failure.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t memory-failure
Thanks.
.
Re: [PATCH v3 1/3] selftests/mm: add memory failure anonymous page test
Quoted 7 lines · Miaohe Lin
Quoted 3 lines · Mark Brown
In -next I added a hack with wrapper scripts per category to improve the integration of the mm selftests with the kselftest framework, could you please wire things up into that, see:
Sorry, I missed that. Do you mean adding something like below to run memory-failure test separately?
Yes, pretty much - I'd keep the list of TEST_PROGS sorted but otherwise that's it.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] selftests/mm: add memory failure anonymous page test
Quoted 11 lines · Mark Brown
Quoted 7 lines · Miaohe Lin
Quoted 3 lines · Mark Brown
In -next I added a hack with wrapper scripts per category to improve the integration of the mm selftests with the kselftest framework, could you please wire things up into that, see:
Sorry, I missed that. Do you mean adding something like below to run memory-failure test separately?
Yes, pretty much - I'd keep the list of
TEST_PROGSsorted but otherwise that's it.
Sounds good to me. Will do.
Thanks.
.
Miaohe Lin
maintainerPATCH v3 2/3 selftests/mm: add memory failure clean pagecache test
In reply to Miaohe LinThis patch adds a new testcase to validate memory failure handling for clean pagecache. This test performs similar operations as anonymous pages except allocating memory using mmap() with a file fd.
This test helps ensure that memory failure handling for clean pagecache works correctly, including unchanged page content, page isolation, and recovery paths.
--- tools/testing/selftests/mm/memory-failure.c | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/mm/memory-failure.c +66 -0
@@ -10,10 +10,14 @@#include <sys/mman.h> #include <linux/mman.h> #include <linux/string.h> +#include <unistd.h> #include <signal.h> #include <setjmp.h> #include <unistd.h> #include <fcntl.h> +#include <sys/vfs.h> +#include <linux/magic.h> +#include <errno.h> #include "vm_util.h"
@@ -24,7 +28,9 @@ enum inject_type {enum result_type {MADV_HARD_ANON , +MADV_HARD_CLEAN_PAGECACHE ,MADV_SOFT_ANON , +MADV_SOFT_CLEAN_PAGECACHE , };static jmp_buf signal_jmp_buf;
@@ -154,6 +160,8 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failureswitch (type) {case MADV_SOFT_ANON : +case MADV_HARD_CLEAN_PAGECACHE : +case MADV_SOFT_CLEAN_PAGECACHE :/* It is not expected to receive a SIGBUS signal. */ ASSERT_EQ (setjmp,0 );
@@ -236,4 +244,62 @@ TEST_F(memory_failure, anon)ASSERT_EQ (munmap (addr, self->page_size),0 ); } +/* Borrowed from mm/gup_longterm.c. */ +static int get_fs_type (int fd) +{ +struct statfs fs; +int ret; + +do { + ret =fstatfs (fd, &fs); + }while (ret && errno ==EINTR ); + +return ret ?0 : (int )fs.f_type; +} + +TEST_F(memory_failure, clean_pagecache) +{ +const char *fname ="./clean-page-cache-test-file" ; +int fd; +char *addr; +int ret; +int fs_type; + + fd =open (fname,O_RDWR |O_CREAT ,0664 ); +if (fd <0 ) +SKIP (return,"failed to open test file.\n" ); +unlink (fname); +ftruncate (fd, self->page_size); + fs_type =get_fs_type (fd); +if (!fs_type || fs_type ==TMPFS_MAGIC ) +SKIP (return,"unsupported filesystem :%x\n" , fs_type); + + addr =mmap (0 , self->page_size,PROT_READ |PROT_WRITE , +MAP_SHARED , fd,0 ); +if (addr ==MAP_FAILED ) +SKIP (return,"mmap failed, not enough memory.\n" ); +memset (addr,0xce , self->page_size); +fsync (fd); + +prepare (_metadata, self, addr); + + ret =sigsetjmp (signal_jmp_buf,1 ); +if (!self->triggered) { + self->triggered = true; +ASSERT_EQ (variant->inject (self, addr),0 ); +FORCE_READ (*addr); + } + +if (variant->type ==MADV_HARD ) +check (_metadata, self, addr,MADV_HARD_CLEAN_PAGECACHE , ret); +else +check (_metadata, self, addr,MADV_SOFT_CLEAN_PAGECACHE , ret); + +cleanup (_metadata, self, addr); + +ASSERT_EQ (munmap (addr, self->page_size),0 ); + +ASSERT_EQ (close (fd),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.33.0
Miaohe Lin
maintainerPATCH v3 3/3 selftests/mm: add memory failure dirty pagecache test
In reply to Miaohe LinThis patch adds a new testcase to validate memory failure handling for dirty pagecache. This performs similar operations as clean pagecaches except fsync() is not used to keep pages dirty.
This test helps ensure that memory failure handling for dirty pagecache works correctly, including proper SIGBUS delivery, page isolation, and recovery paths.
--- tools/testing/selftests/mm/memory-failure.c | 62 +++++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-)
tools/testing/selftests/mm/memory-failure.c +58 -4
@@ -29,8 +29,10 @@ enum inject_type {enum result_type {MADV_HARD_ANON ,MADV_HARD_CLEAN_PAGECACHE , +MADV_HARD_DIRTY_PAGECACHE ,MADV_SOFT_ANON ,MADV_SOFT_CLEAN_PAGECACHE , +MADV_SOFT_DIRTY_PAGECACHE , };static jmp_buf signal_jmp_buf;
@@ -162,6 +164,7 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failurecase MADV_SOFT_ANON :case MADV_HARD_CLEAN_PAGECACHE :case MADV_SOFT_CLEAN_PAGECACHE : +case MADV_SOFT_DIRTY_PAGECACHE :/* It is not expected to receive a SIGBUS signal. */ ASSERT_EQ (setjmp,0 );
@@ -172,6 +175,7 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failureASSERT_NE (pagemap_get_pfn (self->pagemap_fd, vaddr), self->pfn);break ;case MADV_HARD_ANON : +case MADV_HARD_DIRTY_PAGECACHE :/* The SIGBUS signal should have been received. */ ASSERT_EQ (setjmp,1 );
@@ -244,6 +248,18 @@ TEST_F(memory_failure, anon)ASSERT_EQ (munmap (addr, self->page_size),0 ); } +static int prepare_file (const char *fname,unsigned long size) +{ +int fd; + + fd =open (fname,O_RDWR |O_CREAT ,0664 ); +if (fd >=0 ) { +unlink (fname); +ftruncate (fd, size); + } +return fd; +} +/* Borrowed from mm/gup_longterm.c. */ static int get_fs_type (int fd) {
@@ -259,17 +275,14 @@ static int get_fs_type(int fd) TEST_F(memory_failure, clean_pagecache) { -const char *fname ="./clean-page-cache-test-file" ;int fd;char *addr;int ret;int fs_type; - fd =open (fname,O_RDWR |O_CREAT ,0664 ); + fd =prepare_file ("./clean-page-cache-test-file" , self->page_size);if (fd <0 )SKIP (return,"failed to open test file.\n" ); -unlink (fname); -ftruncate (fd, self->page_size); fs_type =get_fs_type (fd);if (!fs_type || fs_type ==TMPFS_MAGIC )SKIP (return,"unsupported filesystem :%x\n" , fs_type);
@@ -302,4 +315,45 @@ TEST_F(memory_failure, clean_pagecache)ASSERT_EQ (close (fd),0 ); } +TEST_F(memory_failure, dirty_pagecache) +{ +int fd; +char *addr; +int ret; +int fs_type; + + fd =prepare_file ("./dirty-page-cache-test-file" , self->page_size); +if (fd <0 ) +SKIP (return,"failed to open test file.\n" ); + fs_type =get_fs_type (fd); +if (!fs_type || fs_type ==TMPFS_MAGIC ) +SKIP (return,"unsupported filesystem :%x\n" , fs_type); + + addr =mmap (0 , self->page_size,PROT_READ |PROT_WRITE , +MAP_SHARED , fd,0 ); +if (addr ==MAP_FAILED ) +SKIP (return,"mmap failed, not enough memory.\n" ); +memset (addr,0xce , self->page_size); + +prepare (_metadata, self, addr); + + ret =sigsetjmp (signal_jmp_buf,1 ); +if (!self->triggered) { + self->triggered = true; +ASSERT_EQ (variant->inject (self, addr),0 ); +FORCE_READ (*addr); + } + +if (variant->type ==MADV_HARD ) +check (_metadata, self, addr,MADV_HARD_DIRTY_PAGECACHE , ret); +else +check (_metadata, self, addr,MADV_SOFT_DIRTY_PAGECACHE , ret); + +cleanup (_metadata, self, addr); + +ASSERT_EQ (munmap (addr, self->page_size),0 ); + +ASSERT_EQ (close (fd),0 ); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.33.0