]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/mm: add memory failure clean pagecache test
authorMiaohe Lin <linmiaohe@huawei.com>
Fri, 6 Feb 2026 03:16:38 +0000 (11:16 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 12 Feb 2026 23:43:01 +0000 (15:43 -0800)
This 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.

Link: https://lkml.kernel.org/r/20260206031639.2707102-3-linmiaohe@huawei.com
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202601221142.mDWA1ucw-lkp@intel.com/
Cc: David Hildenbrand <david@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Naoya Horiguchi <nao.horiguchi@gmail.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/memory-failure.c

index 37806a58f4b405e302659fcc48d2ab4f5a88e12b..3aa624db95771008dd5e466c3ce2ac34a7c94196 100644 (file)
 #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_failure
 
        switch (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