Skip to contentSkip to conversation

mm: Fix MF_DELAYED handling on memory failure

PATCH Applied Lisa Wang 3 participants 8 messages Sep 17

Subsystems (6)
lore
Share & tools
lore

Lisa Wang

author
#0 lore raw

v6 cover mm: Fix MF_DELAYED handling on memory failure

Here's a sixth revision to fix MF_DELAYED handling on memory failure.

This patch series addresses an issue in the memory failure handling path where MF_DELAYED is incorrectly treated as an error. This issue was discovered while testing memory failure handling for guest_memfd.

The proposed solution involves -
1. Clarifying the definition of MF_DELAYED to mean that memory failure handling is only partially completed, and that the metadata for the memory that failed (as in struct page/folio) is still referenced.
2. Updating shmem’s handling to align with the clarified definition.
3. Updating how the result of .error_remove_folio() is interpreted.

Changes from v5:
Add comment-style fallthrough in the memory-failure test, as suggested by Ackerley and David

+ RFC v5: https://lore.kernel.org/all/20260823-memory-failure-mf-delayed-fix-v5-0-d97a5703e45a@google.com/

---
Lisa Wang (5):
mm: memory_failure: Clarify the MF_DELAYED definition
mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED
mm: shmem: Update shmem handler to the MF_DELAYED definition
mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases
mm: selftests: Add shmem into memory failure test

 mm/memory-failure.c                         |  29 ++++---
 mm/shmem.c                                  |   2 +-
 tools/testing/selftests/mm/memory-failure.c | 114 +++++++++++++++++++++++++++-
 3 files changed, 126 insertions(+), 19 deletions(-)

---
base-commit: 08745c62350126bc31b09548137be87e2866f628
change-id: 20260527-memory-failure-mf-delayed-fix-7d5a8f4a8a8b

Best regards,

Signature · 1 line
Lisa Wang <[email protected]>
author
#1 lore raw

PATCH v6 1/5 mm: memory_failure: Clarify the MF_DELAYED definition

This patch clarifies the definition of MF_DELAYED to represent cases where a folio's removal is initiated but not immediately completed (e.g., due to remaining metadata references).

---
 mm/memory-failure.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
mm/memory-failure.c +8 -7
index aaf14608b30e..e698d0e9711d 100644
@@ -847,24 +847,25 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn,
 }
 
 /*
- * MF_IGNORED - The m-f() handler marks the page as PG_hwpoisoned'ed.
+ * MF_IGNORED - The m-f() handler marks the page as PG_hwpoison'ed.
  * But it could not do more to isolate the page from being accessed again,
  * nor does it kill the process. This is extremely rare and one of the
  * potential causes is that the page state has been changed due to
  * underlying race condition. This is the most severe outcomes.
  *
- * MF_FAILED - The m-f() handler marks the page as PG_hwpoisoned'ed.
+ * MF_FAILED - The m-f() handler marks the page as PG_hwpoison'ed.
  * It should have killed the process, but it can't isolate the page,
  * due to conditions such as extra pin, unmap failure, etc. Accessing
  * the page again may trigger another MCE and the process will be killed
  * by the m-f() handler immediately.
  *
- * MF_DELAYED - The m-f() handler marks the page as PG_hwpoisoned'ed.
- * The page is unmapped, and is removed from the LRU or file mapping.
- * An attempt to access the page again will trigger page fault and the
- * PF handler will kill the process.
+ * MF_DELAYED - The m-f() handler marks the page as PG_hwpoison'ed.
+ * It means the page was unmapped and partially isolated (e.g. removed from
+ * file mapping or the LRU) but full cleanup is deferred (e.g. the metadata
+ * for the memory, as in struct page/folio, is still referenced). Any
+ * further access to the page will result in the process being killed.
  *
- * MF_RECOVERED - The m-f() handler marks the page as PG_hwpoisoned'ed.
+ * MF_RECOVERED - The m-f() handler marks the page as PG_hwpoison'ed.
  * The page has been completely isolated, that is, unmapped, taken out of
  * the buddy system, or hole-punched out of the file mapping.
  */
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author
#2 lore raw

PATCH v6 2/5 mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED

In reply to Lisa Wang

The .error_remove_folio a_ops is used by different filesystems to handle folio truncation upon discovery of a memory failure in the memory associated with the given folio.

Currently, MF_DELAYED is treated as an error, causing "Failed to punch page" to be written to the console. MF_DELAYED is then relayed to the caller of truncate_error_folio() as MF_FAILED. This further causes memory_failure() to return -EBUSY, which then always causes a SIGBUS.

This is also implies that regardless of whether the thread's memory corruption kill policy is PR_MCE_KILL_EARLY or PR_MCE_KILL_LATE, a memory failure with MF_DELAYED will always cause a SIGBUS.

Update truncate_error_folio() to return MF_DELAYED to the caller if the .error_remove_folio() callback reports MF_DELAYED.

Fixes 6a46079cf57a ("HWPOISON: The high level memory error handler in the VM v7")
Fixes a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
---
 mm/memory-failure.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/memory-failure.c +4 -2
index e698d0e9711d..37ea23489b75 100644
@@ -939,10 +939,12 @@ static int truncate_error_folio(struct folio *folio, unsigned long pfn,
 	if (mapping->a_ops->error_remove_folio) {
 		int err = mapping->a_ops->error_remove_folio(mapping, folio);
 
-		if (err != 0)
+		if (err == MF_DELAYED)
+			ret = err;
+		else if (err != 0)
 			pr_info("%#lx: Failed to punch page: %d\n", pfn, err);
 		else if (!filemap_release_folio(folio, GFP_NOIO))
-			pr_info("%#lx: failed to release buffers\n", pfn);
+			pr_info("%#lx: Failed to release buffers\n", pfn);
 		else
 			ret = MF_RECOVERED;
 	} else {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author
#3 lore raw

PATCH v6 3/5 mm: shmem: Update shmem handler to the MF_DELAYED definition

In reply to Lisa Wang

To align with the definition of MF_DELAYED, update shmem_error_remove_folio() to return MF_DELAYED.

shmem handles memory failures but defers the actual file truncation. The function's return value should therefore be MF_DELAYED to accurately reflect the state.

Currently, this logical error does not cause a bug, because:

- For shmem folios, folio->private is not set.
- As a result, filemap_release_folio() is a no-op and returns true.
- This, in turn, causes truncate_error_folio() to incorrectly return MF_RECOVERED.
- The caller then treats MF_RECOVERED as a success condition, masking the issue.

The previous patch relays MF_DELAYED to the caller of truncate_error_folio() before any logging, so returning MF_DELAYED from shmem_error_remove_folio() will retain the original behavior of not adding any logs.

The return value of truncate_error_folio() is consumed in action_result(), which treats MF_DELAYED the same way as MF_RECOVERED, hence action_result() also returns the same thing after this change.

---
 mm/shmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/shmem.c +1 -1
index 774f4b18ff5c..d6f94786e709 100644
@@ -5136,7 +5136,7 @@ static void __init shmem_destroy_inodecache(void)
 static int shmem_error_remove_folio(struct address_space *mapping,
 				   struct folio *folio)
 {
-	return 0;
+	return MF_DELAYED;
 }
 
 static const struct address_space_operations shmem_aops = {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author
#4 lore raw

PATCH v6 4/5 mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases

In reply to Lisa Wang

Generalize extra_pins handling to all MF_DELAYED cases not only shmem_mapping.

If MF_DELAYED is returned, the filemap continues to hold refcounts on the folio. Hence, take that into account when checking for extra refcounts.

As clarified in an earlier patch, a return value of MF_DELAYED implies that the page still has elevated refcounts. Hence, set extra_pins to true if the return value is MF_DELAYED. This is aligned with the implementation in me_swapcache_dirty(), where, if a folio is still in the swap cache, ret is set to MF_DELAYED and extra_pins is set to true.

---
 mm/memory-failure.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/memory-failure.c +2 -6
index 37ea23489b75..c7a00c7be368 100644
@@ -1039,18 +1039,14 @@ static int me_pagecache_clean(struct page_state *ps, struct page *p)
 		goto out;
 	}
 
-	/*
-	 * The shmem page is kept in page cache instead of truncating
-	 * so is expected to have an extra refcount after error-handling.
-	 */
-	extra_pins = shmem_mapping(mapping);
-
 	/*
 	 * Truncation is a bit tricky. Enable it per file system for now.
 	 *
 	 * Open: to take i_rwsem or not for this? Right now we don't.
 	 */
 	ret = truncate_error_folio(folio, page_to_pfn(p), mapping);
+
+	extra_pins = ret == MF_DELAYED;
 	if (has_extra_refcount(ps, p, extra_pins))
 		ret = MF_FAILED;
 
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author
#5 lore raw

PATCH v6 5/5 mm: selftests: Add shmem into memory failure test

In reply to Lisa Wang

Add a shmem memory failure selftest to test the shmem memory failure is correct after modifying shmem return value.

Specifically, test the expected behavior under various scenarios combining page dirtiness (dirty vs clean) and failure types (hard vs soft):
+ Dirty + Hard: Trigger a SIGBUS on injection, and trigger another SIGBUS when reading the page again.
+ Dirty + Soft: No SIGBUS is triggered, and the original value can be read successfully.
+ Clean + Hard: No SIGBUS is triggered on injection, but trigger a SIGBUS when trying to read the page again.
+ Clean + Soft: No SIGBUS is triggered, and the page can be read successfully.

---
 tools/testing/selftests/mm/memory-failure.c | 114 +++++++++++++++++++++++++++-
 1 file changed, 111 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/memory-failure.c +111 -3
index 1a5a32e22cce..d5d17a262c00 100644
@@ -29,9 +29,14 @@ enum result_type {
 	MADV_HARD_ANON,
 	MADV_HARD_CLEAN_PAGECACHE,
 	MADV_HARD_DIRTY_PAGECACHE,
+	MADV_HARD_CLEAN_SHMEM,
+	MADV_HARD_DIRTY_SHMEM,
 	MADV_SOFT_ANON,
 	MADV_SOFT_CLEAN_PAGECACHE,
 	MADV_SOFT_DIRTY_PAGECACHE,
+	MADV_SOFT_CLEAN_SHMEM,
+	MADV_SOFT_DIRTY_SHMEM,
+	READ_ERROR,
 };
 
 static jmp_buf signal_jmp_buf;
@@ -157,17 +162,22 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure
 	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);
-
+	case MADV_SOFT_DIRTY_SHMEM:
 		/* The page content should remain unchanged. */
 		ASSERT_TRUE(check_memory(vaddr, self->page_size));
+		/* FALLTHORUGH */
+	case MADV_HARD_CLEAN_SHMEM:
+	case MADV_SOFT_CLEAN_SHMEM:
+		/* It is not expected to receive a SIGBUS signal. */
+		ASSERT_EQ(setjmp, 0);
 
 		/* The backing pfn of addr should have changed. */
 		ASSERT_NE(pagemap_get_pfn(self->pagemap_fd, vaddr), self->pfn);
 		break;
 	case MADV_HARD_ANON:
 	case MADV_HARD_DIRTY_PAGECACHE:
+	case MADV_HARD_DIRTY_SHMEM:
+	case READ_ERROR:
 		/* The SIGBUS signal should have been received. */
 		ASSERT_EQ(setjmp, 1);
 
@@ -263,6 +273,20 @@ static int prepare_file(const char *fname, unsigned long size)
 	return fd;
 }
 
+static int prepare_shmem(const char *fname, unsigned long size)
+{
+	int fd;
+
+	fd = memfd_create(fname, 0);
+	if (fd < 0)
+		return -1;
+	if (ftruncate(fd, size) < 0) {
+		close(fd);
+		return -1;
+	}
+	return fd;
+}
+
 /* Borrowed from mm/gup_longterm.c. */
 static int get_fs_type(int fd)
 {
@@ -355,4 +379,88 @@ TEST_F(memory_failure, dirty_pagecache)
 	ASSERT_EQ(close(fd), 0);
 }
 
+TEST_F(memory_failure, dirty_shmem)
+{
+	int fd;
+	char *addr;
+	int ret;
+
+	fd = prepare_shmem("shmem-file", self->page_size);
+	if (fd < 0)
+		SKIP(return, "failed to open test shmem-file.\n");
+
+	addr = mmap(0, self->page_size, PROT_READ | PROT_WRITE,
+		    MAP_SHARED, fd, 0);
+	if (addr == MAP_FAILED) {
+		close(fd);
+		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 (!ret && !self->injection_attempted) {
+		self->injection_attempted = true;
+		ASSERT_EQ(variant->inject(self, addr), 0);
+	}
+
+	if (variant->type == MADV_HARD) {
+		check(_metadata, self, addr, MADV_HARD_DIRTY_SHMEM, ret);
+		ret = sigsetjmp(signal_jmp_buf, 1);
+		if (ret == 0)
+			FORCE_READ(*addr);
+		check(_metadata, self, addr, READ_ERROR, ret);
+	} else {
+		check(_metadata, self, addr, MADV_SOFT_DIRTY_SHMEM, ret);
+	}
+
+	ASSERT_EQ(munmap(addr, self->page_size), 0);
+
+	ASSERT_EQ(close(fd), 0);
+}
+
+TEST_F(memory_failure, clean_shmem)
+{
+	int fd;
+	char *addr;
+	int ret;
+
+	fd = prepare_shmem("shmem-file", self->page_size);
+	if (fd < 0)
+		SKIP(return, "failed to open test shmem-file.\n");
+
+	addr = mmap(0, self->page_size, PROT_READ | PROT_WRITE,
+		    MAP_SHARED, fd, 0);
+	if (addr == MAP_FAILED) {
+		close(fd);
+		SKIP(return, "mmap failed, not enough memory.\n");
+	}
+	FORCE_READ(*addr);
+
+	prepare(_metadata, self, addr);
+
+	ret = sigsetjmp(signal_jmp_buf, 1);
+	if (!ret && !self->injection_attempted) {
+		self->injection_attempted = true;
+		ASSERT_EQ(variant->inject(self, addr), 0);
+	}
+
+	if (variant->type == MADV_HARD) {
+		check(_metadata, self, addr, MADV_HARD_CLEAN_SHMEM, ret);
+		ret = sigsetjmp(signal_jmp_buf, 1);
+		if (ret == 0)
+			FORCE_READ(*addr);
+		check(_metadata, self, addr, READ_ERROR, ret);
+	} else {
+		/* Test the address accessability without check_memory(). */
+		FORCE_READ(*addr);
+		check(_metadata, self, addr, MADV_SOFT_CLEAN_SHMEM, ret);
+	}
+
+	ASSERT_EQ(munmap(addr, self->page_size), 0);
+
+	ASSERT_EQ(close(fd), 0);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#6 lore raw

Re: [PATCH v6 5/5] mm: selftests: Add shmem into memory failure test

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/mm: infinite loop on unexpected SIGBUS during memory-failure test - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/mm: infinite loop on unexpected SIGBUS during memory-failure test
- [Low] selftests/mm: misspelled FALLTHROUGH comment breaks compiler warning suppression --

commit 7c0289ba3b46e8635863454f24b73b9a63de8df1
Author: Lisa Wang <[email protected]>

mm: selftests: Add shmem into memory failure test

This commit adds a shmem memory failure selftest to verify expected behaviors when combining page dirtiness and failure types. It introduces new tests for clean and dirty shmem pages to check if SIGBUS is correctly triggered or suppressed.

Quoted 4 lines
tools/testing/selftests/mm/memory-failure.c +0 -0
index 1a5a32e22cce2..d5d17a262c000 100644

[ ... ]

tools/testing/selftests/mm/memory-failure.c static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure · line 167

Quoted 10 lines
tools/testing/selftests/mm/memory-failure.c +1 -3
@@ -157,17 +162,22 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure
 	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);
-
+	case MADV_SOFT_DIRTY_SHMEM:
 		/* The page content should remain unchanged. */
 		ASSERT_TRUE(check_memory(vaddr, self->page_size));

[Severity: Medium]
Will this cause an infinite loop if an unexpected SIGBUS is caught? In check(), check_memory() is now called before ASSERT_EQ(setjmp, 0). If soft-offlining unexpectedly results in a SIGBUS, the signal handler jumps back, but check_memory() will then re-trigger the SIGBUS before the setjmp assertion can catch the error and fail the test.

tools/testing/selftests/mm/memory-failure.c static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure · line 168

Quoted 1 line
tools/testing/selftests/mm/memory-failure.c +1 -0
+		/* FALLTHORUGH */

[Severity: Low]
Is this a typo? The spelling FALLTHORUGH will fail to match the compiler's regex for intentional fallthroughs and might generate a -Wimplicit-fallthrough build warning.

Quoted 4 lines
tools/testing/selftests/mm/memory-failure.c +4 -0
+	case MADV_HARD_CLEAN_SHMEM:
+	case MADV_SOFT_CLEAN_SHMEM:
+		/* It is not expected to receive a SIGBUS signal. */
+		ASSERT_EQ(setjmp, 0);

[ ... ]

Quoted 1 line
tools/testing/selftests/mm/memory-failure.c +0 -0
@@ -355,4 +379,88 @@ TEST_F(memory_failure, dirty_pagecache)

[ ... ]

Quoted 2 lines
tools/testing/selftests/mm/memory-failure.c +2 -0
+TEST_F(memory_failure, clean_shmem)
+{

[ ... ]

tools/testing/selftests/mm/memory-failure.c TEST_F(memory_failure, dirty_pagecache) · line 457

Quoted 9 lines
tools/testing/selftests/mm/memory-failure.c +9 -0
+	if (variant->type == MADV_HARD) {
+		check(_metadata, self, addr, MADV_HARD_CLEAN_SHMEM, ret);
+		ret = sigsetjmp(signal_jmp_buf, 1);
+		if (ret == 0)
+			FORCE_READ(*addr);
+		check(_metadata, self, addr, READ_ERROR, ret);
+	} else {
+		/* Test the address accessability without check_memory(). */
+		FORCE_READ(*addr);

[Severity: Medium]
Does executing FORCE_READ(*addr) unconditionally before validating the ret value cause an infinite loop?

If the test unexpectedly caught a SIGBUS during injection or a subsequent access, the sigbus_handler jumps back to sigsetjmp() which sets ret = 1. Executing FORCE_READ(*addr) without checking if ret == 0 triggers another SIGBUS endlessly, rather than letting the test report a failure.

Quoted 7 lines
tools/testing/selftests/mm/memory-failure.c +7 -0
+		check(_metadata, self, addr, MADV_SOFT_CLEAN_SHMEM, ret);
+	}
+
+	ASSERT_EQ(munmap(addr, self->page_size), 0);
+
+	ASSERT_EQ(close(fd), 0);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260917-memory-failure-mf-delayed-fix-v6-0-4b00856b5364@google.com?part=5
maintainer
#7 lore raw
In reply to Lisa Wang
Quoted 1 line · On Thu, 17 Sep 2026 20:43:46 +0000 Lisa Wang <[email protected]> wrote:

Here's a sixth revision to fix MF_DELAYED handling on memory failure.

Thanks. Yourself and your reviewers.

I've added this to mm.git's mm-new branch. If it doesn't break horribly then after a few days I'll move it into the mm-unstable branch, from where it will be pulled in linux-next.

All being well, a few weeks from now I'll move it into mm.git's non-rebasing mm-stable branch. mm-stable is where we assemble material for upstreaming in the upcoming mainline merge window.