mm: Fix MF_DELAYED handling on memory failure
Subsystems (6)
v4 cover mm: Fix MF_DELAYED handling on memory failure
Here's a fourth 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 v3:
+ Split an independent guest_memfd_memory_failure_test, as suggested by Ackerley and Sean
+ Align error logging style in truncate_error_folio, as suggested by Miaohe and David
+ Verify a clean shmem page can be read successfully after soft-offline memory failure, as suggested by Miaohe
Thanks!
---
Lisa Wang (7):
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
KVM: selftests: Add the guest_memfd memory failure test
KVM: selftests: Test guest_memfd behavior with respect to stage 2 page tables
mm/memory-failure.c | 29 +- mm/shmem.c | 2 +- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../kvm/guest_memfd_memory_failure_test.c | 402 +++++++++++++++++++++ tools/testing/selftests/mm/memory-failure.c | 111 +++++- 5 files changed, 527 insertions(+), 19 deletions(-)
---
base-commit: 38741a8e3bc1b809d64f8c8885ab15c3e40700ff
change-id: 20260527-memory-failure-mf-delayed-fix-7d5a8f4a8a8b
Best regards,
Signature · 1 line
Lisa Wang <[email protected]>
PATCH v4 1/7 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
@@ -849,24 +849,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.54.0.1013.g208068f2d8-goog
Re: [PATCH v4 1/7] mm: memory_failure: Clarify the MF_DELAYED definition
Quoted 5 lines · Lisa Wang
This patch clarifies the definition of
MF_DELAYEDto represent cases where a folio's removal is initiated but not immediately completed (e.g., due to remaining metadata references).Signed-off-by Lisa Wang <[email protected]>
Signature · 3 lines
Cheers, David
Re: [PATCH v4 1/7] mm: memory_failure: Clarify the MF_DELAYED definition
In reply to Lisa WangQuoted 5 lines · Lisa Wang
This patch clarifies the definition of
MF_DELAYEDto represent cases where a folio's removal is initiated but not immediately completed (e.g., due to remaining metadata references).Signed-off-by Lisa Wang <[email protected]>
Thanks.
.
Re: [PATCH v4 1/7] mm: memory_failure: Clarify the MF_DELAYED definition
In reply to Lisa WangQuoted 13 lines · Lisa Wang
[...snip...]
- *
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.
Quoted 2 lines
[...snip...]
PATCH v4 2/7 mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED
In reply to Lisa WangThe .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.
--- mm/memory-failure.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/memory-failure.c +4 -2
@@ -941,10 +941,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.54.0.1013.g208068f2d8-goog
Re: [PATCH v4 2/7] mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED
Quoted 13 lines · Lisa Wang
The .error_remove_folio
a_opsis used by different filesystems to handle folio truncation upon discovery of a memory failure in the memory associated with the given folio.Currently,
MF_DELAYEDis treated as an error, causing "Failed to punch page" to be written to the console.MF_DELAYEDis then relayed to the caller oftruncate_error_folio()asMF_FAILED. This further causesmemory_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_EARLYorPR_MCE_KILL_LATE, a memory failure withMF_DELAYEDwill always cause a SIGBUS.
Quoted 5 lines
Update
truncate_error_folio()to returnMF_DELAYEDto the caller if the .error_remove_folio() callback reportsMF_DELAYED.Signed-off-by Lisa Wang <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v4 2/7] mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED
In reply to Lisa WangQuoted 9 lines · Lisa Wang
The .error_remove_folio
a_opsis used by different filesystems to handle folio truncation upon discovery of a memory failure in the memory associated with the given folio.Currently,
MF_DELAYEDis treated as an error, causing "Failed to punch page" to be written to the console.MF_DELAYEDis then relayed to the caller oftruncate_error_folio()asMF_FAILED. This further causesmemory_failure()to return-EBUSY, which then always causes a SIGBUS.
Quoted 8 lines
This is also implies that regardless of whether the thread's memory corruption kill policy is
PR_MCE_KILL_EARLYorPR_MCE_KILL_LATE, a memory failure withMF_DELAYEDwill always cause a SIGBUS.Update
truncate_error_folio()to returnMF_DELAYEDto the caller if the .error_remove_folio() callback reportsMF_DELAYED.Signed-off-by Lisa Wang <[email protected]>
Should we add a Fixes tag here?
Thanks.
.
Re: [PATCH v4 2/7] mm: memory_failure: Allow truncate_error_folio to return MF_DELAYED
In reply to Lisa WangQuoted 26 lines · Lisa Wang
The .error_remove_folio
a_opsis used by different filesystems to handle folio truncation upon discovery of a memory failure in the memory associated with the given folio.Currently,
MF_DELAYEDis treated as an error, causing "Failed to punch page" to be written to the console.MF_DELAYEDis then relayed to the caller oftruncate_error_folio()asMF_FAILED. This further causesmemory_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_EARLYorPR_MCE_KILL_LATE, a memory failure withMF_DELAYEDwill always cause a SIGBUS.Update
truncate_error_folio()to returnMF_DELAYEDto the caller if the .error_remove_folio() callback reportsMF_DELAYED.Signed-off-by Lisa Wang <[email protected]>--- mm/memory-failure.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/memory-failure.c +0
-0
mm/memory-failure.c static int truncate_error_folio(struct folio *folio, unsigned long pfn, · line 945
Quoted 6 lines
mm/memory-failure.c +2
-1@@ -941,10 +941,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;
Perhaps better to set ret = MF_DELAYED directly so that it saves the reader one step of connecting err == MF_DELAYED with ret = err, but not a big issue.
Quoted 11 lines
mm/memory-failure.c +2
-2+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 { --2.54.0.1013.g208068f2d8-goog
PATCH v4 3/7 mm: shmem: Update shmem handler to the MF_DELAYED definition
In reply to Lisa WangTo 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
@@ -5207,7 +5207,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.54.0.1013.g208068f2d8-goog
Re: [PATCH v4 3/7] mm: shmem: Update shmem handler to the MF_DELAYED definition
Quoted 21 lines · Lisa Wang
To align with the definition of
MF_DELAYED, updateshmem_error_remove_folio()to returnMF_DELAYED.shmem handles memory failures but defers the actual file truncation. The function's return value should therefore be
MF_DELAYEDto accurately reflect the state.Currently, this logical error does not cause a bug, because:
- For shmem folios,
folio->privateis not set.
- As a result,filemap_release_folio()is a no-op and returns true.
- This, in turn, causestruncate_error_folio()to incorrectly returnMF_RECOVERED.
- The caller then treatsMF_RECOVEREDas a success condition, masking the issue.The previous patch relays
MF_DELAYEDto the caller oftruncate_error_folio()before any logging, so returningMF_DELAYEDfromshmem_error_remove_folio()will retain the original behavior of not adding any logs.
Quoted 6 lines
The return value of
truncate_error_folio()is consumed inaction_result(), which treatsMF_DELAYEDthe same way asMF_RECOVERED, henceaction_result()also returns the same thing after this change.Signed-off-by Lisa Wang <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v4 3/7] mm: shmem: Update shmem handler to the MF_DELAYED definition
In reply to Lisa WangQuoted 21 lines · Lisa Wang
To align with the definition of
MF_DELAYED, updateshmem_error_remove_folio()to returnMF_DELAYED.shmem handles memory failures but defers the actual file truncation. The function's return value should therefore be
MF_DELAYEDto accurately reflect the state.Currently, this logical error does not cause a bug, because:
- For shmem folios,
folio->privateis not set.
- As a result,filemap_release_folio()is a no-op and returns true.
- This, in turn, causestruncate_error_folio()to incorrectly returnMF_RECOVERED.
- The caller then treatsMF_RECOVEREDas a success condition, masking the issue.The previous patch relays
MF_DELAYEDto the caller oftruncate_error_folio()before any logging, so returningMF_DELAYEDfromshmem_error_remove_folio()will retain the original behavior of not adding any logs.
Quoted 5 lines
The return value of
truncate_error_folio()is consumed inaction_result(), which treatsMF_DELAYEDthe same way asMF_RECOVERED, henceaction_result()also returns the same thing after this change.Signed-off-by Lisa Wang <[email protected]>
Thanks.
.
Re: [PATCH v4 3/7] mm: shmem: Update shmem handler to the MF_DELAYED definition
In reply to Lisa WangQuoted 16 lines · Lisa Wang
To align with the definition of
MF_DELAYED, updateshmem_error_remove_folio()to returnMF_DELAYED.shmem handles memory failures but defers the actual file truncation. The function's return value should therefore be
MF_DELAYEDto accurately reflect the state.Currently, this logical error does not cause a bug, because:
- For shmem folios,
folio->privateis not set.
- As a result,filemap_release_folio()is a no-op and returns true.
- This, in turn, causestruncate_error_folio()to incorrectly returnMF_RECOVERED.
- The caller then treatsMF_RECOVEREDas a success condition, masking the issue.
Quoted 9 lines
The previous patch relays
MF_DELAYEDto the caller oftruncate_error_folio()before any logging, so returningMF_DELAYEDfromshmem_error_remove_folio()will retain the original behavior of not adding any logs.The return value of
truncate_error_folio()is consumed inaction_result(), which treatsMF_DELAYEDthe same way asMF_RECOVERED, henceaction_result()also returns the same thing after this change.
Thanks for explaining here!
Quoted 8 lines
Signed-off-by Lisa Wang <[email protected]>--- mm/shmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)[...snip...]
PATCH v4 4/7 mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases
In reply to Lisa WangGeneralize 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
@@ -1052,18 +1052,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.54.0.1013.g208068f2d8-goog
Re: [PATCH v4 4/7] mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases
Quoted 6 lines · Lisa Wang
Generalize
extra_pinshandling to allMF_DELAYEDcases not onlyshmem_mapping.If
MF_DELAYEDis returned, the filemap continues to hold refcounts on the folio. Hence, take that into account when checking for extra refcounts.
Quoted 8 lines
As clarified in an earlier patch, a return value of
MF_DELAYEDimplies that the page still has elevated refcounts. Hence, setextra_pinsto true if the return value isMF_DELAYED. This is aligned with the implementation inme_swapcache_dirty(), where, if a folio is still in the swap cache, ret is set toMF_DELAYEDandextra_pinsis set to true.Signed-off-by Lisa Wang <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v4 4/7] mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases
In reply to Lisa WangQuoted 6 lines · Lisa Wang
Generalize
extra_pinshandling to allMF_DELAYEDcases not onlyshmem_mapping.If
MF_DELAYEDis returned, the filemap continues to hold refcounts on the folio. Hence, take that into account when checking for extra refcounts.
Quoted 7 lines
As clarified in an earlier patch, a return value of
MF_DELAYEDimplies that the page still has elevated refcounts. Hence, setextra_pinsto true if the return value isMF_DELAYED. This is aligned with the implementation inme_swapcache_dirty(), where, if a folio is still in the swap cache, ret is set toMF_DELAYEDandextra_pinsis set to true.Signed-off-by Lisa Wang <[email protected]>
Thanks.
.
Re: [PATCH v4 4/7] mm: memory_failure: Generalize extra_pins handling to all MF_DELAYED cases
In reply to Lisa WangQuoted 18 lines · Lisa Wang
[...snip...]
mm/memory-failure.c +0
-6@@ -1052,18 +1052,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); -/*
mm/memory-failure.c static int me_pagecache_clean(struct page_state *ps, struct page *p) · line 1065
Quoted 10 lines
mm/memory-failure.c +2
-0* 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 ;
PATCH v4 5/7 mm: selftests: Add shmem into memory failure test
In reply to Lisa WangAdd 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 | 111 +++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 3 deletions(-)
tools/testing/selftests/mm/memory-failure.c +108 -3
@@ -30,9 +30,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;
@@ -165,17 +170,21 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failurecase 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)); +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 );
@@ -260,6 +269,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) {
@@ -356,4 +379,86 @@ 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 ==0 ) +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 ); +cleanup (_metadata, self, addr); +} + +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 ==0 ) +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 ); +cleanup (_metadata, self, addr); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
David Hildenbrand (Arm)
maintainerRe: [PATCH v4 5/7] mm: selftests: Add shmem into memory failure test
Quoted 40 lines · 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.Signed-off-by Lisa Wang <[email protected]>--- tools/testing/selftests/mm/memory-failure.c | 111 +++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 3 deletions(-)tools/testing/selftests/mm/memory-failure.c +5
-0@@ -30,9 +30,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;
tools/testing/selftests/mm/memory-failure.c static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure · line 175
Quoted 9 lines
tools/testing/selftests/mm/memory-failure.c +1
-3@@ -165,17 +170,21 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failurecase 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));
You should likely use "fallthrough;" ... unless you are missing a break; here.
Signature · 3 lines
Cheers, David
Re: [PATCH v4 5/7] mm: selftests: Add shmem into memory failure test
On Fri, Jun 05, 2026 at 01:38:28PM +0200, David Hildenbrand (Arm) wrote:
[...snip...]
Quoted 5 lines
Quoted 5 lines
static
jmp_bufsignal_jmp_buf;tools/testing/selftests/mm/memory-failure.c +0
-0@@ -165,17 +170,21 @@ static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failurecase MADV_HARD_CLEAN_PAGECACHE :case MADV_SOFT_CLEAN_PAGECACHE :case MADV_SOFT_DIRTY_PAGECACHE :
Quoted 8 lines
tools/testing/selftests/mm/memory-failure.c static void check(struct __test_metadata *_metadata, FIXTURE_DATA(memory_failure · line 175
Quoted 6 lines
tools/testing/selftests/mm/memory-failure.c +1
-3-/* 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));You should likely use "fallthrough;" ... unless you are missing a break; here.
Thanks, will fix this in the next version.
Lisa
Quoted 6 lines
Signature · 4 lines
-- Cheers, David
Re: [PATCH v4 5/7] mm: selftests: Add shmem into memory failure test
In reply to Lisa WangQuoted 15 lines · 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.
Quoted 1 line
Signed-off-by Lisa Wang <[email protected]>
With David's comment addressed:
Thanks.
.
PATCH v4 6/7 KVM: selftests: Add the guest_memfd memory failure test
In reply to Lisa WangAfter modifying truncate_error_folio(), we expect memory_failure() will return 0 instead of MF_FAILED. Also, we want to make sure memory_failure() signaling function is same.
Test that memory_failure() returns 0 for guest_memfd, where .error_remove_folio() is handled by not actually truncating, and returning MF_DELAYED.
In addition, test that SIGBUS signaling behavior is not changed before and after this modification.
There are two kinds of guest memory failure injections - madvise or debugfs. When memory failure is injected using madvise, the MF_ACTION_REQUIRED flag is set, and the page is mapped and dirty, the process should get a SIGBUS. When memory is failure is injected using debugfs, the KILL_EARLY machine check memory corruption kill policy is set, and the page is mapped and dirty, the process should get a SIGBUS.
--- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../kvm/guest_memfd_memory_failure_test.c | 336 +++++++++++++++++++++ 2 files changed, 338 insertions(+)
tools/testing/selftests/kvm/Makefile.kvm +2 -0
@@ -146,6 +146,7 @@ TEST_GEN_PROGS_x86 += access_tracking_perf_test TEST_GEN_PROGS_x86 += coalesced_io_test TEST_GEN_PROGS_x86 += dirty_log_perf_test TEST_GEN_PROGS_x86 += guest_memfd_test +TEST_GEN_PROGS_x86 += guest_memfd_memory_failure_test TEST_GEN_PROGS_x86 += hardware_disable_test TEST_GEN_PROGS_x86 += memslot_modification_stress_test TEST_GEN_PROGS_x86 += memslot_perf_test
@@ -186,6 +187,7 @@ TEST_GEN_PROGS_arm64 += coalesced_io_test TEST_GEN_PROGS_arm64 += dirty_log_perf_test TEST_GEN_PROGS_arm64 += get-reg-list TEST_GEN_PROGS_arm64 += guest_memfd_test +TEST_GEN_PROGS_arm64 += guest_memfd_memory_failure_test TEST_GEN_PROGS_arm64 += memslot_modification_stress_test TEST_GEN_PROGS_arm64 += memslot_perf_test TEST_GEN_PROGS_arm64 += mmu_stress_test
tools/testing/selftests/kvm/guest_memfd_memory_failure_test.c new file +336 -0
336 lines not on this page · raw message
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
PATCH v4 7/7 KVM: selftests: Test guest_memfd behavior with respect to stage 2 page tables
In reply to Lisa WangTest that
+ memory failure handling results in unmapping of bad memory from stage 2 page tables, hence requiring faulting on next guest access
+ when the guest tries to fault a poisoned page from guest_memfd, the userspace VMM informed with EHWPOISON
--- .../kvm/guest_memfd_memory_failure_test.c | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/kvm/guest_memfd_memory_failure_test.c +66 -0
@@ -24,6 +24,7 @@#include "kvm_util.h" #include "test_util.h" #include "kselftest_harness.h" +#include "ucall_common.h" static size_t page_size, total_size;
@@ -313,6 +314,71 @@ TEST_F(guest_memfd_failure, test_memory_failure) } } +static void __guest_code_read (uint64_t gpa) +{ +uint8_t *mem = (uint8_t *)gpa; + +READ_ONCE (*mem); +GUEST_SYNC (0 ); +READ_ONCE (*mem); +GUEST_DONE (); +} + +static void guest_read (struct kvm_vcpu *vcpu,int expected_errno) +{ +if (expected_errno) { +TEST_ASSERT_EQ (_vcpu_run (vcpu),-1 ); +TEST_ASSERT_EQ (errno, expected_errno); + }else { +vcpu_run (vcpu); +TEST_ASSERT_EQ (get_ucall (vcpu,NULL ),UCALL_SYNC ); + } +} + +TEST_F(guest_memfd_failure, test_memory_failure_guest) +{ +const uint64_t gpa =SZ_4G ; +const int slot =1 ; + +unsigned long memory_failure_pfn; +struct kvm_vcpu *vcpu; +uint8_t *mem; + +/* Limit guest test execution to a single variant to avoid redundant runs. */ +if (variant->method !=MF_INJECT_DEBUGFS || + variant->kill_config !=PR_MCE_KILL_EARLY || + !variant->map_page || !variant->dirty_page) +return ; + + self->vm =__vm_create_shape_with_one_vcpu (VM_SHAPE_DEFAULT , &vcpu,1 , __guest_code_read); +vcpu_args_set (vcpu,1 , gpa); + + self->fd =vm_create_guest_memfd (self->vm, self->vm->page_size, +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED ); +vm_set_user_memory_region2 (self->vm, slot,KVM_MEM_GUEST_MEMFD , gpa, + self->vm->page_size,NULL , self->fd,0 ); + + mem =mmap (NULL , self->vm->page_size,PROT_READ |PROT_WRITE , +MAP_SHARED , self->fd,0 ); +TEST_ASSERT (mem !=MAP_FAILED ,"mmap() for guest_memfd should succeed." ); +virt_pg_map (self->vm, gpa, gpa); + +/* Fault in page to read pfn, then unmap page for testing. */ +READ_ONCE (*mem); + + memory_failure_pfn =addr_to_pfn (mem); +munmap (mem, self->vm->page_size); + +/* Fault page into stage2 page tables. */ +guest_read (vcpu,0 ); + + self->poisoned_pfn = memory_failure_pfn; +mark_memory_failure (memory_failure_pfn,0 ); + +guest_read (vcpu,EHWPOISON ); +} +static bool can_inject_memory_failure (void ) {int fd;
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
Andrew Morton
maintainerQuoted 5 lines · On Tue, 02 Jun 2026 21:55:40 +0000 Lisa Wang <[email protected]> wrote:
Here's a fourth revision to fix
MF_DELAYEDhandling on memory failure.This patch series addresses an issue in the memory failure handling path where
MF_DELAYEDis incorrectly treated as an error. This issue was discovered while testing memory failure handling forguest_memfd.
Please include a description of the userspace-visible effects of the issue?
Quoted 6 lines
The proposed solution involves -
1. Clarifying the definition ofMF_DELAYEDto 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.
Thanks. I'll take no action at this time - it's late in the cycle and we lack review.
For some reason Sashiko wasn't able to apply this series to the various branches which it attempts. Click on the "baseline" thingy at
https://sashiko.dev/#/patchset/20260602-memory-failure-mf-delayed-fix-v4-0-a5bc7db5a9b2@google.com
to see what it tried to apply this to.