memfd-based Userspace MFR Policy for HugeTLB
Subsystems (9)
v3 cover memfd-based Userspace MFR Policy for HugeTLB
Problem
=======
This patchset is a follow-up for the userspace memory failure recovery (MFR) policy proposed in [1] and [2], but focused on a smaller scope: HugeTLB.
To recap the problem for HugeTLB discussed in [1] and [2]:
Cloud providers like Google and Oracle usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, the kernel's current MFR behavior for HugeTLB is not ideal. Once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion, from the HugeTLB system. Customer workload running in the VM can hardly recover from such a great loss of memory.
[1] and [2] proposed the idea that the decision to keep or discard a large chunk of contiguous memory exclusively owned by a userspace process due to a recoverable uncorrected memory error (UE) should be controlled by userspace. What this means in the Cloud case is that, since a virtual machine monitor (VMM) has taken host memory to exclusively back the guest memory for a VM, the VMM can keep holding the memory even after memory errors occur.
MFD_MF_KEEP_UE_MAPPED for HugeTLB
=================================
[2] proposed a solution centered around the memfd associated with the memory exclusively owned by userspace.
A userspace process must opt into the MFD_MF_KEEP_UE_MAPPED policy when it creates a new HugeTLB-backed memfd:
#define MFD_MF_KEEP_UE_MAPPED 0x0020U
int memfd_create(const char *name, unsigned int flags);
For any hugepage associated with the MFD_MF_KEEP_UE_MAPPED enabled memfd, whenever it runs into a UE, MFR doesn't hard offline the HWPoison huge folio. In other words, the HWPoison memory remains accessible via the returned memfd or the memory mapping created with that memfd. MFR still sends SIGBUS to the userspace process as required. MFR also still maintains HWPoison metadata on the hugepage having the UE.
A HWPoison hugepage will be immediately isolated and prevented from future allocation once userspace truncates it via the memfd, or the owning memfd is closed.
By default MFD_MF_KEEP_UE_MAPPED is not set, and MFR hard offlines hugepages having UEs.
Implementation
==============
Implementation is relatively straightforward with two major parts.
Part 1: When hugepages owned by an MFD_MF_KEEP_UE_MAPPED enabled memfd run into a UE:
* MFR defers hard offline operations, i.e., unmapping and dissolving. MFR still sets HWPoison flags and holds a refcount for every raw HWPoison page. MFR still sends SIGBUS to the consuming thread, but si_addr_lsb will be reduced to PAGE_SHIFT.
* If the memory was not faulted in yet, the fault handler also needs to unblock the fault to the HWPoison folio.
Part 2: When an MFD_MF_KEEP_UE_MAPPED enabled memfd is being released, or when a userspace process truncates a range of hugepages belonging to an MFD_MF_KEEP_UE_MAPPED enabled memfd:
* When the HugeTLB in-memory file system removes a filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by filemap_offline_hwpoison_folio().
* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes a free HugeTLB folio, MFR dissolves it into a set of raw pages.
Changelog
=========
v3 -> v2 [3]
- Rebase onto [4] to simplify filemap_offline_hwpoison_folio_hugetlb(). With free_has_hwpoisoned() rejecting HWPoison subpages in a HugeTLB folio, there is no need to take_page_off_buddy() after dissolve_free_hugetlb_folio().
- Address comments from William Roche <[email protected]> and Jane Chu <[email protected]>.
- Update size_shift in kill_accessing_process() if MFD_MF_KEEP_UE_MAPPED is enabled. Thanks William Roche <[email protected]> for providing his patch on this.
- Add a new tunable to hugetlb-mfr to control the number of pages within the 1st hugepage to MADV_HWPOISON.
v2 -> v1 [2]
- Rebase onto commit 6da43bbeb6918 ("Merge tag 'vfio-v6.18-rc6' of
https://github.com/awilliam/linux-vfio").
- Remove populate_memfd_hwp_folios() and offline_memfd_hwp_folios() so that no memory allocation is needed during releasing HWPoison memfd.
- Insert filemap_offline_hwpoison_folio() into remove_inode_single_folio(). Now dissolving and offlining HWPoison huge folios is done on the fly.
- Fix the bug pointed out by William Roche <[email protected]>:
call take_page_off_buddy() no matter HWPoison page is buddy page or not.
- Remove update_per_node_mf_stats() when dissolve failed.
- Make hugetlb-mfr allocate 4 1G hugepages to cover new code introduced in remove_inode_hugepages().
- Make hugetlb-mfr support testing both 1GB and 2MB HugeTLB hugepages.
- Fix some typos in documentation.
[1] https://lwn.net/Articles/991513
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/linux-mm/[email protected]
[4] https://lore.kernel.org/linux-mm/[email protected]
Jiaqi Yan (3):
mm: memfd/hugetlb: introduce memfd-based userspace MFR policy selftests/mm: test userspace MFR for HugeTLB hugepage
Documentation: add documentation for MFD_MF_KEEP_UE_MAPPED
Documentation/userspace-api/index.rst | 1 + .../userspace-api/mfd_mfr_policy.rst | 60 +++ fs/hugetlbfs/inode.c | 25 +- include/linux/hugetlb.h | 7 + include/linux/pagemap.h | 23 ++ include/uapi/linux/memfd.h | 6 + mm/hugetlb.c | 8 +- mm/memfd.c | 15 +- mm/memory-failure.c | 124 +++++- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/hugetlb-mfr.c | 369 ++++++++++++++++++ 12 files changed, 627 insertions(+), 15 deletions(-) create mode 100644 Documentation/userspace-api/mfd_mfr_policy.rst create mode 100644 tools/testing/selftests/mm/hugetlb-mfr.c
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v3 1/3 mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Introduce a memfd-based userspace memory failure (MFR) policy, MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.
For a hugepage associated with MFD_MF_KEEP_UE_MAPPED enabled memfd, whenever it runs into a new UE,
* MFR defers hard offline operations, i.e., unmapping and dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but si_addr_lsb is reduced to PAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.
* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a MFD_MF_KEEP_UE_MAPPED enabled memfd, when it is closed, or when userspace process truncates its hugepages:
* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by filemap_offline_hwpoison_folio().
* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
By default MFD_MF_KEEP_UE_MAPPED is not set, so MFR immediately hard offlines hugepages having UEs.
Tested with new selftest hugetlb-mfr in the follow-up commit.
--- fs/hugetlbfs/inode.c | 25 +++++++- include/linux/hugetlb.h | 7 +++ include/linux/pagemap.h | 23 +++++++ include/uapi/linux/memfd.h | 6 ++ mm/hugetlb.c | 8 ++- mm/memfd.c | 15 ++++- mm/memory-failure.c | 124 ++++++++++++++++++++++++++++++++++--- 7 files changed, 193 insertions(+), 15 deletions(-)
fs/hugetlbfs/inode.c +23 -2
@@ -551,6 +551,18 @@ static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, }folio_unlock (folio); + +/* +* There may be pending HWPoison-ed folios when a memfd is being +* removed or part of it is being truncated. +* +* HugeTLBFS' error_remove_folio keeps the HWPoison-ed folios in +* page cache until mm wants to drop the folio at the end of the +* of the filemap. At this point, if memory failure was delayed +* by MFD_MF_KEEP_UE_MAPPED in the past, we can now deal with it. +*/ +filemap_offline_hwpoison_folio (mapping, folio); +return ret; }
@@ -582,13 +594,13 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,const pgoff_t end = lend >>PAGE_SHIFT ;struct folio_batch fbatch;pgoff_t next, index; -int i, freed =0 ; +int i, j, freed =0 ;bool truncate_op = (lend ==LLONG_MAX );folio_batch_init (&fbatch); next = lstart >>PAGE_SHIFT ;while (filemap_get_folios (mapping, &next, end -1 , &fbatch)) { -for (i =0 ; i <folio_batch_count (&fbatch); ++i) { +for (i =0 , j =0 ; i <folio_batch_count (&fbatch); ++i) {struct folio *folio = fbatch.folios[i];u32 hash =0 ;
@@ -603,8 +615,17 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart, index, truncate_op)) freed++; +/* +* Skip HWPoison-ed hugepages, which should no +* longer be hugetlb if successfully dissolved. +*/ +if (folio_test_hugetlb (folio)) + fbatch.folios[j++] = folio; +mutex_unlock (&hugetlb_fault_mutex_table[hash]); } + fbatch.nr = j; +folio_batch_release (&fbatch);cond_resched (); }
include/linux/hugetlb.h +7 -0
@@ -879,10 +879,17 @@ int dissolve_free_hugetlb_folios(unsigned long start_pfn,#ifdef CONFIG_MEMORY_FAILURE extern void folio_clear_hugetlb_hwpoison (struct folio *folio); +extern bool hugetlb_should_keep_hwpoison_mapped (struct folio *folio, +struct address_space *mapping);#else static inline void folio_clear_hugetlb_hwpoison (struct folio *folio) { } +static inline bool hugetlb_should_keep_hwpoison_mapped(struct folio *folio + struct address_space *mapping) +{ +return false; +}#endif #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
include/linux/pagemap.h +23 -0
@@ -211,6 +211,7 @@ enum mapping_flags {AS_KERNEL_FILE =10 ,/* mapping for a fake kernel file that shouldn't account usage to user cgroups */ AS_NO_DATA_INTEGRITY =11 ,/* no data integrity guarantees */ +AS_MF_KEEP_UE_MAPPED =12 ,/* For MFD_MF_KEEP_UE_MAPPED. */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =5 , AS_FOLIO_ORDER_MIN = 16,
@@ -356,6 +357,16 @@ static inline bool mapping_no_data_integrity(const struct address_space *mappingreturn test_bit (AS_NO_DATA_INTEGRITY , &mapping->flags); } +static inline bool mapping_mf_keep_ue_mapped (const struct address_space *mapping) +{ +return test_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} + +static inline void mapping_set_mf_keep_ue_mapped (struct address_space *mapping) +{ +set_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (const struct address_space *mapping) {return mapping->gfp_mask;
@@ -1303,6 +1314,18 @@ void replace_page_cache_folio(struct folio *old, struct folio *new);void delete_from_page_cache_batch (struct address_space *mapping,struct folio_batch *fbatch);bool filemap_release_folio (struct folio *folio,gfp_t gfp); +#ifdef CONFIG_MEMORY_FAILURE +/* +* Provided by memory failure to offline HWPoison-ed folio managed by memfd. +*/ +void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio); +#else +static inline void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio) +{ +} +#endif loff_t mapping_seek_hole_data (struct address_space *,loff_t start,loff_t end,int whence);
include/uapi/linux/memfd.h +6 -0
@@ -12,6 +12,12 @@#define MFD_NOEXEC_SEAL 0x0008U/* executable */ #define MFD_EXEC 0x0010U +/* +* Keep owned folios mapped when uncorrectable memory errors (UE) causes +* memory failure (MF) within the folio. Only at the end of the mapping +* will its HWPoison-ed folios be dealt with. +*/ +#define MFD_MF_KEEP_UE_MAPPED 0x0020U/* * Huge page size encoding when MFD_HUGETLB is specified, and a huge page
mm/hugetlb.c +5 -3
@@ -5836,9 +5836,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,* So we need to block hugepage fault by PG_hwpoison bit check. */ if (unlikely (folio_test_hwpoison (folio))) { - ret =VM_FAULT_HWPOISON_LARGE | -VM_FAULT_SET_HINDEX (hstate_index (h)); - goto backout_unlocked; +if (!mapping_mf_keep_ue_mapped (mapping)) { + ret =VM_FAULT_HWPOISON_LARGE | +VM_FAULT_SET_HINDEX (hstate_index (h)); + goto backout_unlocked; + } }/* Check for page in userfault range. */
mm/memfd.c +14 -1
@@ -340,7 +340,8 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) -#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC) +#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | \ + MFD_NOEXEC_SEAL | MFD_EXEC | MFD_MF_KEEP_UE_MAPPED)static int check_sysctl_memfd_noexec (unsigned int *flags) {
@@ -414,6 +415,8 @@ static int sanitize_flags(unsigned int *flags_ptr)if (!(flags &MFD_HUGETLB )) {if (flags & ~MFD_ALL_FLAGS )return -EINVAL ; +if (flags &MFD_MF_KEEP_UE_MAPPED ) +return -EINVAL ; }else {/* Allow huge page size encoding in flags. */ if (flags & ~(MFD_ALL_FLAGS |
@@ -486,6 +489,16 @@ static struct file *alloc_file(const char *name, unsigned int flags) file->f_mode |=FMODE_LSEEK |FMODE_PREAD |FMODE_PWRITE ; file->f_flags |=O_LARGEFILE ; +/* +* MFD_MF_KEEP_UE_MAPPED can only be specified in memfd_create; +* no API to update it once memfd is created. MFD_MF_KEEP_UE_MAPPED +* is not seal-able. +* +* For now MFD_MF_KEEP_UE_MAPPED is only supported by HugeTLBFS. +*/ +if (flags &MFD_MF_KEEP_UE_MAPPED ) +mapping_set_mf_keep_ue_mapped (file->f_mapping); +if (flags &MFD_NOEXEC_SEAL ) { inode->i_mode &= ~0111 ; file_seals =memfd_file_seals_ptr (file);
mm/memory-failure.c +115 -9
210 lines not on this page · raw message
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v3 2/3 selftests/mm: test userspace MFR for HugeTLB hugepage
In reply to Jiaqi YanTest the userspace memory failure recovery (MFR) policy for HugeTLB:
1. Create a memfd backed by HugeTLB and had MFD_MF_KEEP_UE_MAPPED set.
2. Allocate and map 4 hugepages to the process.
3. Create sub-threads to MADV_HWPOISON inner addresses of the 1st hugepage.
4. Check if the process gets correct SIGBUS for each poisoned raw page.
5. Check if all memory are still accessible and content valid.
6. Check if the poisoned hugepage is dealt with after memfd released.
Two configurables in the test:
- hugepage_size: size of the hugepage, 1G or 2M.
- nr_hwp_pages: number of pages within the 1st hugepage to MADV_HWPOISON.
--- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/hugetlb-mfr.c | 369 +++++++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb-mfr.c
tools/testing/selftests/mm/.gitignore +1 -0
@@ -5,6 +5,7 @@ hugepage-mremap hugepage-shm hugepage-vmemmap hugetlb-madvise +hugetlb-mfr hugetlb-read-hwpoison hugetlb-soft-offline khugepaged
tools/testing/selftests/mm/Makefile +3 -0
@@ -63,6 +63,7 @@ TEST_GEN_FILES += hmm-tests TEST_GEN_FILES += hugetlb-madvise TEST_GEN_FILES += hugetlb-read-hwpoison TEST_GEN_FILES += hugetlb-soft-offline +TEST_GEN_FILES += hugetlb-mfr TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-mremap TEST_GEN_FILES += hugepage-shm
@@ -233,6 +234,8 @@ $(OUTPUT)/migration: LDLIBS += -lnuma $(OUTPUT)/rmap: LDLIBS += -lnuma +$(OUTPUT)/hugetlb-mfr: LDLIBS += -lnuma + local_config.mk local_config.h: check_config.sh /bin/sh ./check_config.sh $(CC)
tools/testing/selftests/mm/hugetlb-mfr.c new file +369 -0
369 lines not on this page · raw message
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v3 3/3 Documentation: add documentation for MFD_MF_KEEP_UE_MAPPED
In reply to Jiaqi YanDocument its motivation, userspace API, behaviors, and limitations.
--- Documentation/userspace-api/index.rst | 1 + .../userspace-api/mfd_mfr_policy.rst | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Documentation/userspace-api/mfd_mfr_policy.rst
Documentation/userspace-api/index.rst +1 -0
@@ -68,6 +68,7 @@ Everything else futex2 perf_ring_buffer ntsync + mfd_mfr_policy .. only:: subproject and html
Documentation/userspace-api/mfd_mfr_policy.rst new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
In reply to Jiaqi YanQuoted 7 lines · Jiaqi Yan
[...]
fs/hugetlbfs/inode.c +0
-0@@ -551,6 +551,18 @@ static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, }
fs/hugetlbfs/inode.c static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, · line 561
Quoted 10 lines
fs/hugetlbfs/inode.c +8
-0folio_unlock (folio); + +/* +* There may be pending HWPoison-ed folios when a memfd is being +* removed or part of it is being truncated. +* +* HugeTLBFS' error_remove_folio keeps the HWPoison-ed folios in +* page cache until mm wants to drop the folio at the end of the +* of the filemap. At this point, if memory failure was delayed
"of the" is repeated
Quoted 46 lines
fs/hugetlbfs/inode.c +15
-2+* by MFD_MF_KEEP_UE_MAPPED in the past, we can now deal with it. +*/ +filemap_offline_hwpoison_folio (mapping, folio); +return ret; }@@ -582,13 +594,13 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,const pgoff_t end = lend >>PAGE_SHIFT ;struct folio_batch fbatch;pgoff_t next, index; -int i, freed =0 ; +int i, j, freed =0 ;bool truncate_op = (lend ==LLONG_MAX );folio_batch_init (&fbatch); next = lstart >>PAGE_SHIFT ;while (filemap_get_folios (mapping, &next, end -1 , &fbatch)) { -for (i =0 ; i <folio_batch_count (&fbatch); ++i) { +for (i =0 , j =0 ; i <folio_batch_count (&fbatch); ++i) {struct folio *folio = fbatch.folios[i];u32 hash =0 ;@@ -603,8 +615,17 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart, index, truncate_op)) freed++; +/* +* Skip HWPoison-ed hugepages, which should no +* longer be hugetlb if successfully dissolved. +*/ +if (folio_test_hugetlb (folio)) + fbatch.folios[j++] = folio; +mutex_unlock (&hugetlb_fault_mutex_table[hash]); } + fbatch.nr = j; +folio_batch_release (&fbatch);cond_resched (); }include/linux/hugetlb.h +0
-0
include/linux/hugetlb.h int dissolve_free_hugetlb_folios(unsigned long start_pfn, · line 888
Quoted 10 lines
include/linux/hugetlb.h +3
-0@@ -879,10 +879,17 @@ int dissolve_free_hugetlb_folios(unsigned long start_pfn,#ifdef CONFIG_MEMORY_FAILURE extern void folio_clear_hugetlb_hwpoison (struct folio *folio); +extern bool hugetlb_should_keep_hwpoison_mapped (struct folio *folio, +struct address_space *mapping);#else static inline void folio_clear_hugetlb_hwpoison (struct folio *folio) { } +static inline bool hugetlb_should_keep_hwpoison_mapped(struct folio *folio
comma is missing
Quoted 192 lines
include/linux/hugetlb.h +4
-0+ struct address_space *mapping) +{ +return false; +}#endif #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION include/linux/pagemap.h +23
-0@@ -211,6 +211,7 @@ enum mapping_flags {AS_KERNEL_FILE =10 ,/* mapping for a fake kernel file that shouldn't account usage to user cgroups */ AS_NO_DATA_INTEGRITY =11 ,/* no data integrity guarantees */ +AS_MF_KEEP_UE_MAPPED =12 ,/* For MFD_MF_KEEP_UE_MAPPED. */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =5 , AS_FOLIO_ORDER_MIN = 16,@@ -356,6 +357,16 @@ static inline bool mapping_no_data_integrity(const struct address_space *mappingreturn test_bit (AS_NO_DATA_INTEGRITY , &mapping->flags); } +static inline bool mapping_mf_keep_ue_mapped (const struct address_space *mapping) +{ +return test_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} + +static inline void mapping_set_mf_keep_ue_mapped (struct address_space *mapping) +{ +set_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (const struct address_space *mapping) {return mapping->gfp_mask;@@ -1303,6 +1314,18 @@ void replace_page_cache_folio(struct folio *old, struct folio *new);void delete_from_page_cache_batch (struct address_space *mapping,struct folio_batch *fbatch);bool filemap_release_folio (struct folio *folio,gfp_t gfp); +#ifdef CONFIG_MEMORY_FAILURE +/* +* Provided by memory failure to offline HWPoison-ed folio managed by memfd. +*/ +void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio); +#else +static inline void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio) +{ +} +#endif loff_t mapping_seek_hole_data (struct address_space *,loff_t start,loff_t end,int whence);include/uapi/linux/memfd.h +6
-0@@ -12,6 +12,12 @@#define MFD_NOEXEC_SEAL 0x0008U/* executable */ #define MFD_EXEC 0x0010U +/* +* Keep owned folios mapped when uncorrectable memory errors (UE) causes +* memory failure (MF) within the folio. Only at the end of the mapping +* will its HWPoison-ed folios be dealt with. +*/ +#define MFD_MF_KEEP_UE_MAPPED 0x0020U/* * Huge page size encoding when MFD_HUGETLB is specified, and a huge page mm/hugetlb.c +5
-3@@ -5836,9 +5836,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,* So we need to block hugepage fault by PG_hwpoison bit check. */ if (unlikely (folio_test_hwpoison (folio))) { - ret =VM_FAULT_HWPOISON_LARGE | -VM_FAULT_SET_HINDEX (hstate_index (h)); - goto backout_unlocked; +if (!mapping_mf_keep_ue_mapped (mapping)) { + ret =VM_FAULT_HWPOISON_LARGE | +VM_FAULT_SET_HINDEX (hstate_index (h)); + goto backout_unlocked; + } }/* Check for page in userfault range. */ mm/memfd.c +14
-1@@ -340,7 +340,8 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) -#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC) +#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | \ + MFD_NOEXEC_SEAL | MFD_EXEC | MFD_MF_KEEP_UE_MAPPED)static int check_sysctl_memfd_noexec (unsigned int *flags) {@@ -414,6 +415,8 @@ static int sanitize_flags(unsigned int *flags_ptr)if (!(flags &MFD_HUGETLB )) {if (flags & ~MFD_ALL_FLAGS )return -EINVAL ; +if (flags &MFD_MF_KEEP_UE_MAPPED ) +return -EINVAL ; }else {/* Allow huge page size encoding in flags. */ if (flags & ~(MFD_ALL_FLAGS |@@ -486,6 +489,16 @@ static struct file *alloc_file(const char *name, unsigned int flags) file->f_mode |=FMODE_LSEEK |FMODE_PREAD |FMODE_PWRITE ; file->f_flags |=O_LARGEFILE ; +/* +* MFD_MF_KEEP_UE_MAPPED can only be specified in memfd_create; +* no API to update it once memfd is created. MFD_MF_KEEP_UE_MAPPED +* is not seal-able. +* +* For now MFD_MF_KEEP_UE_MAPPED is only supported by HugeTLBFS. +*/ +if (flags &MFD_MF_KEEP_UE_MAPPED ) +mapping_set_mf_keep_ue_mapped (file->f_mapping); +if (flags &MFD_NOEXEC_SEAL ) { inode->i_mode &= ~0111 ; file_seals =memfd_file_seals_ptr (file);mm/memory-failure.c +24
-3@@ -410,6 +410,8 @@ static void __add_to_kill(struct task_struct *tsk, const struct page *p, unsigned long addr) {struct to_kill *tk; +const struct folio *folio; +struct address_space *mapping; tk =kmalloc (sizeof (struct to_kill ),GFP_ATOMIC );if (!tk) {@@ -420,8 +422,19 @@ static void __add_to_kill(struct task_struct *tsk, const struct page *p, tk->addr = addr;if (is_zone_device_page (p)) tk->size_shift =dev_pagemap_mapping_shift (vma, tk->addr); -else - tk->size_shift =folio_shift (page_folio (p)); +else { + folio =page_folio (p); + mapping =folio_mapping (folio); +if (mapping &&mapping_mf_keep_ue_mapped (mapping)) +/* +* Let userspace know the radius of HWPoison is +* the size of raw page; accessing other pages +* inside the folio is still ok. +*/ + tk->size_shift =PAGE_SHIFT ; +else + tk->size_shift =folio_shift (folio); + }/* * Send SIGKILL if "tk->addr == -EFAULT". Also, as @@ -844,6 +857,8 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn, int flags) {int ret; +struct folio *folio; +struct address_space *mapping;struct hwpoison_walk priv = { .pfn = pfn, };@@ -861,8 +876,14 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn,* ret = 0 when poison page is a clean page and it's dropped, no * SIGBUS is needed. */ -if (ret ==1 && priv.tk.addr) +if (ret ==1 && priv.tk.addr) { + folio =pfn_folio (pfn); + mapping =folio_mapping (folio); +if (mapping &&mapping_mf_keep_ue_mapped (mapping)) + priv.tk.size_shift =PAGE_SHIFT ; +kill_proc (&priv.tk, pfn, flags); + }mmap_read_unlock (p->mm);return ret >0 ? -EHWPOISON :0 ;
mm/memory-failure.c static int me_huge_page(struct page_state *ps, struct page *p) · line 1231
Quoted 5 lines
mm/memory-failure.c +2
-0@@ -1206,6 +1227,13 @@ static int me_huge_page(struct page_state *ps, struct page *p) } } +/* +* MF still needs to holds a refcount for the deferred actions in
to hold (without the s)
Quoted 17 lines
mm/memory-failure.c +6
-0+* filemap_offline_hwpoison_folio. +*/ +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +return res; +if (has_extra_refcount (ps, p, extra_pins)) res =MF_FAILED ;@@ -1602,6 +1630,7 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p, {LIST_HEAD (tokill);bool unmap_success; +bool keep_mapped;int forcekill;bool mlocked =folio_test_mlocked (folio);
mm/memory-failure.c static bool hwpoison_user_mappings(struct folio *folio, struct page *p, · line 1661
Quoted 6 lines
mm/memory-failure.c +1
-2@@ -1629,8 +1658,12 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p,*/ collect_procs (folio, p, &tokill, flags &MF_ACTION_REQUIRED ); - unmap_success = !unmap_poisoned_folio (folio, pfn, flags &MF_MUST_KILL ); -if (!unmap_success) + keep_mapped =hugetlb_should_keep_hwpoison_mapped (folio, folio->mapping);
We shoud use folio_mapping(folio) instead of folio->mapping.
But more importantly this function can be called on non hugepages folios, and hugetlb_should_keep_hwpoison_mapped() is warning (ONCE) in this case. So shouldn't the caller make sure that we are dealing with hugepages first ?
Quoted 32 lines
mm/memory-failure.c +13
-1+if (!keep_mapped) +unmap_poisoned_folio (folio, pfn, flags &MF_MUST_KILL ); + + unmap_success = !folio_mapped (folio); +if (!keep_mapped && !unmap_success)pr_err ("%#lx: failed to unmap page (folio mapcount=%d)\n" , pfn,folio_mapcount (folio));@@ -1655,7 +1688,7 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p, !unmap_success;kill_procs (&tokill, forcekill, pfn, flags); -return unmap_success; +return unmap_success || keep_mapped; } static int identify_page_state(unsigned long pfn, struct page *p,@@ -1896,6 +1929,13 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)unsigned long count =0 ; head =llist_del_all (raw_hwp_list_head (folio)); +/* +* If filemap_offline_hwpoison_folio_hugetlb is handling this folio, +* it has already taken off the head of the llist. +*/ +if (head ==NULL ) +return 0 ; +llist_for_each_entry_safe (p, next, head, node) {if (move_flag)SetPageHWPoison (p->page);
mm/memory-failure.c static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page) · line 1955
Quoted 5 lines
mm/memory-failure.c +1
-1@@ -1912,7 +1952,8 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)struct llist_head *head;struct raw_hwp_page *raw_hwp;struct raw_hwp_page *p; -int ret =folio_test_set_hwpoison (folio) ? -EHWPOISON :0 ; +struct address_space *mapping = folio->mapping;
Same here - We shoud use folio_mapping(folio) instead of folio->mapping.
Quoted 36 lines
mm/memory-failure.c +12
-2+bool has_hwpoison =folio_test_set_hwpoison (folio);/* * Once the hwpoison hugepage has lost reliable raw error info, @@ -1931,8 +1972,15 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)if (raw_hwp) { raw_hwp->page = page;llist_add (&raw_hwp->node, head); +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +/* +* A new raw HWPoison page. Don't return HWPOISON. +* Error event will be counted in action_result(). +*/ +return 0 ; +/* the first error event will be counted in action_result(). */ -if (ret) +if (has_hwpoison)num_poisoned_pages_inc (page_to_pfn (page)); }else {/* @@ -1947,7 +1995,8 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)*/ __folio_free_raw_hwp (folio, false); } -return ret; + +return has_hwpoison ? -EHWPOISON :0 ; }static unsigned long folio_free_raw_hwp (struct folio *folio,bool move_flag)@@ -1980,6 +2029,18 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)folio_free_raw_hwp (folio, true); } +bool hugetlb_should_keep_hwpoison_mapped(struct folio *folio,
mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 2042
Quoted 10 lines
mm/memory-failure.c +10
-0+ struct address_space *mapping) +{ +if (WARN_ON_ONCE (!folio_test_hugetlb (folio))) +return false; + +if (!mapping) +return false; + +return mapping_mf_keep_ue_mapped (mapping); +}
The definition of this above function should be encapsulated with #ifdef CONFIG_MEMORY_FAILURE
#endif
Quoted 41 lines
mm/memory-failure.c +34
-0+/* * Called from hugetlb code with hugetlb_lock held. * @@ -2037,6 +2098,51 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,return ret; } +static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio)); + +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio); +SetPageHWPoison (curr->page); +kfree (curr); + } + +/* Refcount now should be zero and ready to dissolve folio. */ + ret =dissolve_free_hugetlb_folio (folio); +if (ret) +pr_err ("failed to dissolve hugetlb folio: %d\n" , ret); +} + +void filemap_offline_hwpoison_folio(struct address_space *mapping,
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2143
Quoted 10 lines
mm/memory-failure.c +10
-0+ struct folio *folio) +{ +WARN_ON_ONCE (!mapping); + +if (!folio_test_hwpoison (folio)) +return ; + +/* Pending MFR currently only exist for hugetlb. */ +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +filemap_offline_hwpoison_folio_hugetlb (folio);
Shouldn't we also test here that we are dealing with hugepages first before testing hugetlb_should_keep_hwpoison_mapped(folio, mapping) ?
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2148
Quoted 5 lines
mm/memory-failure.c +2
-0+} +/* * Taking refcount of hugetlb pages needs extra care about race conditions * with basic operations like hugepage allocation/free/demotion.
Don't we also need to take into account the repeated errors in try_memory_failure_hugetlb() ?
Something like that:
@@ -2036,9 +2099,10 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb
{
int res, rv;
struct page *p = pfn_to_page(pfn);
- struct folio *folio;
+ struct folio *folio = page_folio(p);
unsigned long page_flags;
bool migratable_cleared = false;
+ struct address_space *mapping = folio_mapping(folio);
*hugetlb = 1;
retry:
@@ -2060,15 +2124,17 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb
rv = kill_accessing_process(current, pfn, flags);
if (res == MF_HUGETLB_PAGE_PRE_POISONED)
action_result(pfn, MF_MSG_ALREADY_POISONED, MF_FAILED);
- else
+ else {
+ if (hugetlb_should_keep_hwpoison_mapped(folio, mapping))
+ return action_result(pfn, MF_MSG_UNMAP_FAILED, MF_DELAYED);
action_result(pfn, MF_MSG_HUGE, MF_FAILED);
+ }
return rv;
default:
WARN_ON((res != MF_HUGETLB_FREED) && (res != MF_HUGETLB_IN_USED)); break;
}
- folio = page_folio(p);
folio_lock(folio);
if (hwpoison_filter(p)) {
So that we don't call action_result(pfn, MF_MSG_HUGE, MF_FAILED); for a repeated error ?
Signature · 1 line
2.47.3
Re: [PATCH v3 2/3] selftests/mm: test userspace MFR for HugeTLB hugepage
In reply to Jiaqi YanQuoted 12 lines · Jiaqi Yan
Test the userspace memory failure recovery (MFR) policy for HugeTLB:
1. Create a memfd backed by HugeTLB and had
MFD_MF_KEEP_UE_MAPPEDset.2. Allocate and map 4 hugepages to the process.
3. Create sub-threads to
MADV_HWPOISONinner addresses of the 1st hugepage.4. Check if the process gets correct SIGBUS for each poisoned raw page.
5. Check if all memory are still accessible and content valid.
Quoted 7 lines
6. Check if the poisoned hugepage is dealt with after memfd released.
Two configurables in the test:
-
hugepage_size: size of the hugepage, 1G or 2M.-
nr_hwp_pages: number of pages within the 1st hugepage toMADV_HWPOISON.
In this version, you are introducing this new test argument "nr_hwp_pages" to indicate how many of the pre-defined offsets we want to poison inside the hugepage (between 1 and 8).
But is there any advantage to give the choice to the user instead of testing them all ?
As a suggestion, should we have this test program setting or verifying the minimal number of hugepages of the right type, instead of relying on the user to set them manually ?
And at the end, should we try to unpoison the impacted pages ? So that the lab machine where the tests run can continue to use all its memory ?
Thanks for your feedback,
William.
Re: [PATCH v3 3/3] Documentation: add documentation for MFD_MF_KEEP_UE_MAPPED
In reply to Jiaqi YanQuoted 78 lines · Jiaqi Yan
Document its motivation, userspace API, behaviors, and limitations.
Reviewed-by Jane Chu <[email protected]>Signed-off-by Jiaqi Yan <[email protected]>--- Documentation/userspace-api/index.rst | 1 + .../userspace-api/mfd_mfr_policy.rst | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+)create mode 100644
Documentation/userspace-api/mfd_mfr_policy.rstDocumentation/userspace-api/index.rst +1
-0@@ -68,6 +68,7 @@ Everything else futex2 perf_ring_buffer ntsync + mfd_mfr_policy .. only:: subproject and htmlDocumentation/userspace-api/mfd_mfr_policy.rst new file +50
-0@@ -0,0 +1,60 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================================== +Userspace Memory Failure Recovery Policy via memfd +================================================== + +:Author: + Jiaqi Yan <[email protected]> + + +Motivation +========== + +When a userspace process is able to recover from memory failures (MF) +caused by uncorrected memory error (UE) in the DIMM, especially when it is +able to avoid consuming known UEs, keeping the memory page mapped and +accessible is benifical to the owning process for a couple of reasons: + +- The memory pages affected by UE have a large smallest granularity, for + example 1G hugepage, but the actual corrupted amount of the page is only + several cachlines. Losing the entire hugepage of data is unacceptable to + the application. + +- In addition to keeping the data accessible, the application still wants + to access with a large page size for the fastest virtual-to-physical + translations. + +Memory failure recovery for 1G or larger HugeTLB is a good example. With +memfd userspace process can control whether the kernel hard offlines its +hugepages that backs the in-RAM file created by memfd. + + +User API +======== + +``int memfd_create(const char *name, unsigned int flags)`` + +``MFD_MF_KEEP_UE_MAPPED`` + + When ``MFD_MF_KEEP_UE_MAPPED`` bit is set in ``flags``, MF recovery + in the kernel does not hard offline memory due to UE until the + returned ``memfd`` is released. IOW, the HWPoison-ed memory remains + accessible via the returned ``memfd`` or the memory mapping created + with the returned ``memfd``. Note the affected memory will be + immediately isolated and prevented from future use once the memfd + is closed. By default ``MFD_MF_KEEP_UE_MAPPED`` is not set, and + kernel hard offlines memory having UEs. + +Notes about the behavior and limitations +
Documentation/userspace-api/mfd_mfr_policy.rst line 60
Quoted 10 lines
Documentation/userspace-api/mfd_mfr_policy.rst new file +10
-0+- Even if the page affected by UE is kept, a portion of the (huge)page is + already lost due to hardware corruption, and the size of the portion + is the smallest page size that kernel uses to manages memory on the + architecture, i.e. PAGESIZE. Accessing a virtual address within any of + these parts results in a SIGBUS; accessing virtual address outside these + parts are good until it is corrupted by new memory error. + +- ``MFD_MF_KEEP_UE_MAPPED`` currently only works for HugeTLB, so + ``MFD_HUGETLB`` must also be set when setting ``MFD_MF_KEEP_UE_MAPPED``. + Otherwise ``memfd_create`` returns EINVAL.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
In reply to Jiaqi YanQuoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 22 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 13 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));
We might race with get_huge_page_for_hwpoison()? llist_add() might be called by folio_set_hugetlb_hwpoison() just after llist_del_all()?
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 10 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See __get_huge_page_for_hwpoison() for details. So folio_put() might be called more times than folio_try_get() in __get_huge_page_for_hwpoison().
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2123
Quoted 1 line
mm/memory-failure.c +1
-0+SetPageHWPoison (curr->page);
If hugetlb folio vmemmap is optimized, I think SetPageHWPoison might trigger BUG.
Review
Quoted 2 lines
(quoted diff) +2
-0+ kfree(curr); + }
Above logic is almost same as folio_clear_hugetlb_hwpoison. Maybe we can reuse that?
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2132
Quoted 7 lines
mm/memory-failure.c +7
-0+ +/* Refcount now should be zero and ready to dissolve folio. */ + ret =dissolve_free_hugetlb_folio (folio); +if (ret) +pr_err ("failed to dissolve hugetlb folio: %d\n" , ret); +} +
Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 2/3] selftests/mm: test userspace MFR for HugeTLB hugepage
In reply to Jiaqi YanQuoted 14 lines · Jiaqi Yan
Test the userspace memory failure recovery (MFR) policy for HugeTLB:
1. Create a memfd backed by HugeTLB and had
MFD_MF_KEEP_UE_MAPPEDset.2. Allocate and map 4 hugepages to the process.
3. Create sub-threads to
MADV_HWPOISONinner addresses of the 1st hugepage.4. Check if the process gets correct SIGBUS for each poisoned raw page.
5. Check if all memory are still accessible and content valid.
6. Check if the poisoned hugepage is dealt with after memfd released.
Quoted 8 lines
Two configurables in the test:
-
hugepage_size: size of the hugepage, 1G or 2M.-
nr_hwp_pages: number of pages within the 1st hugepage toMADV_HWPOISON.Reviewed-by Jane Chu <[email protected]>Signed-off-by Jiaqi Yan <[email protected]>
It's not required but could this testcase be written into the tools/testing/selftests/mm/memory-failure.c [1]?
Thanks.
.
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
In reply to William RocheQuoted 307 lines · William Roche
Quoted 7 lines · Jiaqi Yan
[...]
fs/hugetlbfs/inode.c +0
-0@@ -551,6 +551,18 @@ static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, }fs/hugetlbfs/inode.c static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, · line 561
Quoted 10 lines
fs/hugetlbfs/inode.c +8
-0folio_unlock (folio); + +/* +* There may be pending HWPoison-ed folios when a memfd is being +* removed or part of it is being truncated. +* +* HugeTLBFS' error_remove_folio keeps the HWPoison-ed folios in +* page cache until mm wants to drop the folio at the end of the +* of the filemap. At this point, if memory failure was delayed "of the" is repeated
Quoted 46 lines
fs/hugetlbfs/inode.c +15
-2+* by MFD_MF_KEEP_UE_MAPPED in the past, we can now deal with it. +*/ +filemap_offline_hwpoison_folio (mapping, folio); +return ret; }@@ -582,13 +594,13 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,const pgoff_t end = lend >>PAGE_SHIFT ;struct folio_batch fbatch;pgoff_t next, index; -int i, freed =0 ; +int i, j, freed =0 ;bool truncate_op = (lend ==LLONG_MAX );folio_batch_init (&fbatch); next = lstart >>PAGE_SHIFT ;while (filemap_get_folios (mapping, &next, end -1 , &fbatch)) { -for (i =0 ; i <folio_batch_count (&fbatch); ++i) { +for (i =0 , j =0 ; i <folio_batch_count (&fbatch); ++i) {struct folio *folio = fbatch.folios[i];u32 hash =0 ;@@ -603,8 +615,17 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart, index, truncate_op)) freed++; +/* +* Skip HWPoison-ed hugepages, which should no +* longer be hugetlb if successfully dissolved. +*/ +if (folio_test_hugetlb (folio)) + fbatch.folios[j++] = folio; +mutex_unlock (&hugetlb_fault_mutex_table[hash]); } + fbatch.nr = j; +folio_batch_release (&fbatch);cond_resched (); }include/linux/hugetlb.h +0
-0include/linux/hugetlb.h int dissolve_free_hugetlb_folios(unsigned long start_pfn, · line 888
Quoted 10 lines
include/linux/hugetlb.h +3
-0@@ -879,10 +879,17 @@ int dissolve_free_hugetlb_folios(unsigned long start_pfn,#ifdef CONFIG_MEMORY_FAILURE extern void folio_clear_hugetlb_hwpoison (struct folio *folio); +extern bool hugetlb_should_keep_hwpoison_mapped (struct folio *folio, +struct address_space *mapping);#else static inline void folio_clear_hugetlb_hwpoison (struct folio *folio) { } +static inline bool hugetlb_should_keep_hwpoison_mapped(struct folio *foliocomma is missing
Quoted 192 lines
include/linux/hugetlb.h +4
-0+ struct address_space *mapping) +{ +return false; +}#endif #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION include/linux/pagemap.h +23
-0@@ -211,6 +211,7 @@ enum mapping_flags {AS_KERNEL_FILE =10 ,/* mapping for a fake kernel file that shouldn't account usage to user cgroups */ AS_NO_DATA_INTEGRITY =11 ,/* no data integrity guarantees */ +AS_MF_KEEP_UE_MAPPED =12 ,/* For MFD_MF_KEEP_UE_MAPPED. */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =5 , AS_FOLIO_ORDER_MIN = 16,@@ -356,6 +357,16 @@ static inline bool mapping_no_data_integrity(const struct address_space *mappingreturn test_bit (AS_NO_DATA_INTEGRITY , &mapping->flags); } +static inline bool mapping_mf_keep_ue_mapped (const struct address_space *mapping) +{ +return test_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} + +static inline void mapping_set_mf_keep_ue_mapped (struct address_space *mapping) +{ +set_bit (AS_MF_KEEP_UE_MAPPED , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (const struct address_space *mapping) {return mapping->gfp_mask;@@ -1303,6 +1314,18 @@ void replace_page_cache_folio(struct folio *old, struct folio *new);void delete_from_page_cache_batch (struct address_space *mapping,struct folio_batch *fbatch);bool filemap_release_folio (struct folio *folio,gfp_t gfp); +#ifdef CONFIG_MEMORY_FAILURE +/* +* Provided by memory failure to offline HWPoison-ed folio managed by memfd. +*/ +void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio); +#else +static inline void filemap_offline_hwpoison_folio (struct address_space *mapping, +struct folio *folio) +{ +} +#endif loff_t mapping_seek_hole_data (struct address_space *,loff_t start,loff_t end,int whence);include/uapi/linux/memfd.h +6
-0@@ -12,6 +12,12 @@#define MFD_NOEXEC_SEAL 0x0008U/* executable */ #define MFD_EXEC 0x0010U +/* +* Keep owned folios mapped when uncorrectable memory errors (UE) causes +* memory failure (MF) within the folio. Only at the end of the mapping +* will its HWPoison-ed folios be dealt with. +*/ +#define MFD_MF_KEEP_UE_MAPPED 0x0020U/* * Huge page size encoding when MFD_HUGETLB is specified, and a huge page mm/hugetlb.c +5
-3@@ -5836,9 +5836,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,* So we need to block hugepage fault by PG_hwpoison bit check. */ if (unlikely (folio_test_hwpoison (folio))) { - ret =VM_FAULT_HWPOISON_LARGE | -VM_FAULT_SET_HINDEX (hstate_index (h)); - goto backout_unlocked; +if (!mapping_mf_keep_ue_mapped (mapping)) { + ret =VM_FAULT_HWPOISON_LARGE | +VM_FAULT_SET_HINDEX (hstate_index (h)); + goto backout_unlocked; + } }/* Check for page in userfault range. */ mm/memfd.c +14
-1@@ -340,7 +340,8 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) -#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | MFD_NOEXEC_SEAL | MFD_EXEC) +#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | \ + MFD_NOEXEC_SEAL | MFD_EXEC | MFD_MF_KEEP_UE_MAPPED)static int check_sysctl_memfd_noexec (unsigned int *flags) {@@ -414,6 +415,8 @@ static int sanitize_flags(unsigned int *flags_ptr)if (!(flags &MFD_HUGETLB )) {if (flags & ~MFD_ALL_FLAGS )return -EINVAL ; +if (flags &MFD_MF_KEEP_UE_MAPPED ) +return -EINVAL ; }else {/* Allow huge page size encoding in flags. */ if (flags & ~(MFD_ALL_FLAGS |@@ -486,6 +489,16 @@ static struct file *alloc_file(const char *name, unsigned int flags) file->f_mode |=FMODE_LSEEK |FMODE_PREAD |FMODE_PWRITE ; file->f_flags |=O_LARGEFILE ; +/* +* MFD_MF_KEEP_UE_MAPPED can only be specified in memfd_create; +* no API to update it once memfd is created. MFD_MF_KEEP_UE_MAPPED +* is not seal-able. +* +* For now MFD_MF_KEEP_UE_MAPPED is only supported by HugeTLBFS. +*/ +if (flags &MFD_MF_KEEP_UE_MAPPED ) +mapping_set_mf_keep_ue_mapped (file->f_mapping); +if (flags &MFD_NOEXEC_SEAL ) { inode->i_mode &= ~0111 ; file_seals =memfd_file_seals_ptr (file);mm/memory-failure.c +24
-3@@ -410,6 +410,8 @@ static void __add_to_kill(struct task_struct *tsk, const struct page *p, unsigned long addr) {struct to_kill *tk; +const struct folio *folio; +struct address_space *mapping; tk =kmalloc (sizeof (struct to_kill ),GFP_ATOMIC );if (!tk) {@@ -420,8 +422,19 @@ static void __add_to_kill(struct task_struct *tsk, const struct page *p, tk->addr = addr;if (is_zone_device_page (p)) tk->size_shift =dev_pagemap_mapping_shift (vma, tk->addr); -else - tk->size_shift =folio_shift (page_folio (p)); +else { + folio =page_folio (p); + mapping =folio_mapping (folio); +if (mapping &&mapping_mf_keep_ue_mapped (mapping)) +/* +* Let userspace know the radius of HWPoison is +* the size of raw page; accessing other pages +* inside the folio is still ok. +*/ + tk->size_shift =PAGE_SHIFT ; +else + tk->size_shift =folio_shift (folio); + }/* * Send SIGKILL if "tk->addr == -EFAULT". Also, as @@ -844,6 +857,8 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn, int flags) {int ret; +struct folio *folio; +struct address_space *mapping;struct hwpoison_walk priv = { .pfn = pfn, };@@ -861,8 +876,14 @@ static int kill_accessing_process(struct task_struct *p, unsigned long pfn,* ret = 0 when poison page is a clean page and it's dropped, no * SIGBUS is needed. */ -if (ret ==1 && priv.tk.addr) +if (ret ==1 && priv.tk.addr) { + folio =pfn_folio (pfn); + mapping =folio_mapping (folio); +if (mapping &&mapping_mf_keep_ue_mapped (mapping)) + priv.tk.size_shift =PAGE_SHIFT ; +kill_proc (&priv.tk, pfn, flags); + }mmap_read_unlock (p->mm);return ret >0 ? -EHWPOISON :0 ;mm/memory-failure.c static int me_huge_page(struct page_state *ps, struct page *p) · line 1231
Quoted 5 lines
mm/memory-failure.c +2
-0@@ -1206,6 +1227,13 @@ static int me_huge_page(struct page_state *ps, struct page *p) } } +/* +* MF still needs to holds a refcount for the deferred actions in to hold (without the s)
Quoted 17 lines
mm/memory-failure.c +6
-0+* filemap_offline_hwpoison_folio. +*/ +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +return res; +if (has_extra_refcount (ps, p, extra_pins)) res =MF_FAILED ;@@ -1602,6 +1630,7 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p, {LIST_HEAD (tokill);bool unmap_success; +bool keep_mapped;int forcekill;bool mlocked =folio_test_mlocked (folio);mm/memory-failure.c static bool hwpoison_user_mappings(struct folio *folio, struct page *p, · line 1661
Quoted 6 lines
mm/memory-failure.c +1
-2@@ -1629,8 +1658,12 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p,*/ collect_procs (folio, p, &tokill, flags &MF_ACTION_REQUIRED ); - unmap_success = !unmap_poisoned_folio (folio, pfn, flags &MF_MUST_KILL ); -if (!unmap_success) + keep_mapped =hugetlb_should_keep_hwpoison_mapped (folio, folio->mapping);We shoud use
folio_mapping(folio)instead offolio->mapping.
Quoted 4 lines
But more importantly this function can be called on non hugepages folios, and
hugetlb_should_keep_hwpoison_mapped()is warning (ONCE) in this case. So shouldn't the caller make sure that we are dealing with hugepages first ?
I guess the WARN_ON_ONCE() in hugetlb_should_keep_hwpoison_mapped() is confusing. I want hugetlb_should_keep_hwpoison_mapped() to test and return false for non hugepage. Let me remove WARN_ON_ONCE().
Quoted 148 lines
Quoted 34 lines
mm/memory-failure.c +13
-1+if (!keep_mapped) +unmap_poisoned_folio (folio, pfn, flags &MF_MUST_KILL ); + + unmap_success = !folio_mapped (folio); +if (!keep_mapped && !unmap_success)pr_err ("%#lx: failed to unmap page (folio mapcount=%d)\n" , pfn,folio_mapcount (folio));@@ -1655,7 +1688,7 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p, !unmap_success;kill_procs (&tokill, forcekill, pfn, flags); -return unmap_success; +return unmap_success || keep_mapped; } static int identify_page_state(unsigned long pfn, struct page *p,@@ -1896,6 +1929,13 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)unsigned long count =0 ; head =llist_del_all (raw_hwp_list_head (folio)); +/* +* If filemap_offline_hwpoison_folio_hugetlb is handling this folio, +* it has already taken off the head of the llist. +*/ +if (head ==NULL ) +return 0 ; +llist_for_each_entry_safe (p, next, head, node) {if (move_flag)SetPageHWPoison (p->page);mm/memory-failure.c static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page) · line 1955
Quoted 5 lines
mm/memory-failure.c +1
-1@@ -1912,7 +1952,8 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)struct llist_head *head;struct raw_hwp_page *raw_hwp;struct raw_hwp_page *p; -int ret =folio_test_set_hwpoison (folio) ? -EHWPOISON :0 ; +struct address_space *mapping = folio->mapping;Same here - We shoud use
folio_mapping(folio)instead offolio->mapping.Quoted 36 lines
mm/memory-failure.c +12
-2+bool has_hwpoison =folio_test_set_hwpoison (folio);/* * Once the hwpoison hugepage has lost reliable raw error info, @@ -1931,8 +1972,15 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)if (raw_hwp) { raw_hwp->page = page;llist_add (&raw_hwp->node, head); +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +/* +* A new raw HWPoison page. Don't return HWPOISON. +* Error event will be counted in action_result(). +*/ +return 0 ; +/* the first error event will be counted in action_result(). */ -if (ret) +if (has_hwpoison)num_poisoned_pages_inc (page_to_pfn (page)); }else {/* @@ -1947,7 +1995,8 @@ static int folio_set_hugetlb_hwpoison(struct folio *folio, struct page *page)*/ __folio_free_raw_hwp (folio, false); } -return ret; + +return has_hwpoison ? -EHWPOISON :0 ; }static unsigned long folio_free_raw_hwp (struct folio *folio,bool move_flag)@@ -1980,6 +2029,18 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)folio_free_raw_hwp (folio, true); } +bool hugetlb_should_keep_hwpoison_mapped(struct folio *folio,mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 2042
Quoted 10 lines
mm/memory-failure.c +10
-0+ struct address_space *mapping) +{ +if (WARN_ON_ONCE (!folio_test_hugetlb (folio))) +return false; + +if (!mapping) +return false; + +return mapping_mf_keep_ue_mapped (mapping); +}The definition of this above function should be encapsulated with #ifdef
CONFIG_MEMORY_FAILURE
#endifQuoted 41 lines
mm/memory-failure.c +34
-0+/* * Called from hugetlb code with hugetlb_lock held. * @@ -2037,6 +2098,51 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,return ret; } +static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio)); + +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio); +SetPageHWPoison (curr->page); +kfree (curr); + } + +/* Refcount now should be zero and ready to dissolve folio. */ + ret =dissolve_free_hugetlb_folio (folio); +if (ret) +pr_err ("failed to dissolve hugetlb folio: %d\n" , ret); +} + +void filemap_offline_hwpoison_folio(struct address_space *mapping,mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2143
Quoted 10 lines
mm/memory-failure.c +10
-0+ struct folio *folio) +{ +WARN_ON_ONCE (!mapping); + +if (!folio_test_hwpoison (folio)) +return ; + +/* Pending MFR currently only exist for hugetlb. */ +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +filemap_offline_hwpoison_folio_hugetlb (folio);Shouldn't we also test here that we are dealing with hugepages first before testing
hugetlb_should_keep_hwpoison_mapped(folio, mapping)?
Quoted 9 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2148
Quoted 5 lines
mm/memory-failure.c +2
-0+} +/* * Taking refcount of hugetlb pages needs extra care about race conditions * with basic operations like hugepage allocation/free/demotion. Don't we also need to take into account the repeated errors in
try_memory_failure_hugetlb()?
Ah, looks like I haven't pull the recently commit a148a2040191 ("mm/memory-failure: fix missing ->mf_stats count in hugetlb poison").
When dealing with a new error in already HWPoison folio, MFD_MF_KEEP_UE_MAPPED makes folio_set_hugetlb_hwpoison() return 0 (now MF_HUGETLB_IN_USED for hugetlb_update_hwpoison()) so __get_huge_page_for_hwpoison() can return 1/MF_HUGETLB_IN_USED. The idea is to make try_memory_failure_hugetlb() just handle new error as a first-time poisoned in-use hugetlb page.
Of course for an old error __get_huge_page_for_hwpoison should return MF_HUGETLB_PAGE_PRE_POISONED.
Quoted 21 lines
Something like that:
(quoted diff) +0
-0@@ -2036,9 +2099,10 @@ static int try_memory_failure_hugetlb(unsignedlong pfn, int flags, int *hugetlb
{ int res, rv; struct page *p = pfn_to_page(pfn);(quoted diff) +2
-1- struct folio *folio; + struct folio *folio = page_folio(p); unsigned long page_flags; bool migratable_cleared = false; + struct address_space *mapping = folio_mapping(folio); *hugetlb = 1; retry:@@ -2060,15 +2124,17 @@ static int try_memory_failure_hugetlb(unsignedlong pfn, int flags, int *hugetlb
rv = kill_accessing_process(current, pfn, flags); if (res == MF_HUGETLB_PAGE_PRE_POISONED) action_result(pfn, MF_MSG_ALREADY_POISONED, MF_FAILED);
mm/memory-failure.c static void __add_to_kill(struct task_struct *tsk, const struct page *p,
Quoted 4 lines
mm/memory-failure.c +3
-1-else +else { +if (hugetlb_should_keep_hwpoison_mapped (folio, mapping)) +return action_result (pfn,MF_MSG_UNMAP_FAILED ,MF_DELAYED );
If hugetlb_update_hwpoison() returns MF_HUGETLB_IN_USED for MFD_MF_KEEP_UE_MAPPED, then try_memory_failure_hugetlb() should normally run to the end and report MF_MSG_HUGE + MF_RECOVERED.
Quoted 21 lines
action_result(pfn, MF_MSG_HUGE, MF_FAILED);(quoted diff) +1
-1+ } return rv; default: WARN_ON((res != MF_HUGETLB_FREED) && (res != MF_HUGETLB_IN_USED)); break; } - folio = page_folio(p); folio_lock(folio); if (hwpoison_filter(p)) {So that we don't call
action_result(pfn, MF_MSG_HUGE, MF_FAILED); for a repeated error ?--
2.47.3
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
In reply to Miaohe LinQuoted 24 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 10 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.
Quoted 6 lines
Quoted 4 lines
For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Are you asking from testing perspective, are we still able to clean up injected test errors via unpoison_memory() with MFD_MF_KEEP_UE_MAPPED?
If so, unpoison_memory() can't turn the HWPoison hugetlb page to normal hugetlb page as MFD_MF_KEEP_UE_MAPPED automatically dissolves it. unpoison_memory(pfn) can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.
Quoted 26 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Quoted 1 line
Quoted 15 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 12 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?
Oh, when there is a new UE while we releasing the folio here, right? In that case, would mutex_lock(&mf_mutex) eliminate potential race?
Quoted 12 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 11 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);
Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().
The changes in folio_set_hugetlb_hwpoison() should make __get_huge_page_for_hwpoison() not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.
Quoted 4 lines
Quoted 2 lines
+
SetPageHWPoison(curr->page);If hugetlb folio vmemmap is optimized, I think SetPageHWPoison might trigger BUG.
Ah, I see, vmemmap optimization doesn't allow us to move flags from raw_hwp_list to tail pages. I guess the best I can do is to bail out if vmemmap is enabled like folio_clear_hugetlb_hwpoison().
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ kfree(curr); + }Above logic is almost same as
folio_clear_hugetlb_hwpoison. Maybe we can reuse that?
Will give it a try.
Quoted 12 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2132
Quoted 8 lines
mm/memory-failure.c +7
-0+ +/* Refcount now should be zero and ready to dissolve folio. */ + ret =dissolve_free_hugetlb_folio (folio); +if (ret) +pr_err ("failed to dissolve hugetlb folio: %d\n" , ret); +} +Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 30 lines · Jiaqi Yan
Quoted 29 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 14 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
Quoted 7 lines
Quoted 1 line
So the folio can't be unpoisoned until hugetlb folio becomes free?
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolves
We might loss some testability but that should be an acceptable compromise.
Quoted 45 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 26 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Quoted 1 line
Quoted 15 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 12 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?
IMO spin_lock_irq(&hugetlb_lock) might be better.
Quoted 17 lines
Quoted 13 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 12 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().
Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.
See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool *migratable_cleared)
{
struct page *page = pfn_to_page(pfn);
struct folio *folio = page_folio(page);
int ret = 2; /* fallback to normal page handling */
bool count_increased = false;
if (!folio_test_hugetlb(folio))
goto out;
if (flags & MF_COUNT_INCREASED) {
ret = 1;
count_increased = true;
} else if (folio_test_hugetlb_freed(folio)) {
ret = 0;
} else if (folio_test_hugetlb_migratable(folio)) {
^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1]
ret = folio_try_get(folio);
if (ret)
count_increased = true;
} else {
ret = -EBUSY;
if (!(flags & MF_NO_RETRY))
goto out;
}
if (folio_set_hugetlb_hwpoison(folio, page)) {
ret = -EHWPOISON;
goto out;
}
/*
* Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them
* from being migrated by memory hotremove.
*/
if (count_increased && folio_test_hugetlb_migratable(folio)) {
folio_clear_hugetlb_migratable(folio);
^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2]
*migratable_cleared = true;
}Or am I miss something?
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
+
SetPageHWPoison(curr->page);If hugetlb folio vmemmap is optimized, I think SetPageHWPoison might trigger BUG.
Ah, I see, vmemmap optimization doesn't allow us to move flags from
raw_hwp_listto tail pages. I guess the best I can do is to bail out if vmemmap is enabled likefolio_clear_hugetlb_hwpoison().
I think you can do this after hugetlb_vmemmap_restore_folio() is called.
Thanks.
.
Re: [PATCH v3 2/3] selftests/mm: test userspace MFR for HugeTLB hugepage
In reply to William RocheQuoted 19 lines · William Roche
Quoted 16 lines · Jiaqi Yan
Test the userspace memory failure recovery (MFR) policy for HugeTLB:
1. Create a memfd backed by HugeTLB and had
MFD_MF_KEEP_UE_MAPPEDset.2. Allocate and map 4 hugepages to the process.
3. Create sub-threads to
MADV_HWPOISONinner addresses of the 1st hugepage.4. Check if the process gets correct SIGBUS for each poisoned raw page.
5. Check if all memory are still accessible and content valid.
6. Check if the poisoned hugepage is dealt with after memfd released.
Two configurables in the test:
Quoted 8 lines
Quoted 3 lines
-
hugepage_size: size of the hugepage, 1G or 2M.-
nr_hwp_pages: number of pages within the 1st hugepage toMADV_HWPOISON.In this version, you are introducing this new test argument "
nr_hwp_pages" to indicate how many of the pre-defined offsets we want to poison inside the hugepage (between 1 and 8).
But is there any advantage to give the choice to the user instead of testing them all ?
Yeah, nr_hwp_pages doesn't seem very useful. It was useful when I want the test to run in two modes: single page vs multple pages. Let me just make the test to testing 8 pages.
Quoted 4 lines
As a suggestion, should we have this test program setting or verifying the minimal number of hugepages of the right type, instead of relying on the user to set them manually ?
Yeah, I agree, we can just run for 2 different hugepagesize.
Quoted 2 lines
And at the end, should we try to unpoison the impacted pages ? So that the lab machine where the tests run can continue to use all its memory ?
Good point, we can include this as a test cleanup step.
Quoted 4 lines
Thanks for your feedback,
William.
Re: [PATCH v3 2/3] selftests/mm: test userspace MFR for HugeTLB hugepage
In reply to Miaohe LinQuoted 18 lines · Miaohe Lin
Quoted 16 lines · Jiaqi Yan
Test the userspace memory failure recovery (MFR) policy for HugeTLB:
1. Create a memfd backed by HugeTLB and had
MFD_MF_KEEP_UE_MAPPEDset.2. Allocate and map 4 hugepages to the process.
3. Create sub-threads to
MADV_HWPOISONinner addresses of the 1st hugepage.4. Check if the process gets correct SIGBUS for each poisoned raw page.
5. Check if all memory are still accessible and content valid.
6. Check if the poisoned hugepage is dealt with after memfd released.
Two configurables in the test:
Quoted 8 lines
Quoted 6 lines
-
hugepage_size: size of the hugepage, 1G or 2M.-
nr_hwp_pages: number of pages within the 1st hugepage toMADV_HWPOISON.Reviewed-by Jane Chu <[email protected]>Signed-off-by Jiaqi Yan <[email protected]>It's not required but could this testcase be written into the
tools/testing/selftests/mm/memory-failure.c[1]?
Good point, let me catch up with your new test fixtures and see what I can do with this new test.
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
In reply to Miaohe LinQuoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.
What I wanted to say is, for my test hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.
Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 13 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?
Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.
Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 13 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }
Quoted 1 line
Or am I miss something?
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running hugetlb-mfr.c) on the folio at the moment of filemap_offline_hwpoison_folio_hugetlb() is actually because of the MF_COUNT_INCREASED during MADV_HWPOISON. In the past I used to think that is the effect of folio_try_get() in __get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
- MADV_HWPOISON: instead of __get_huge_page_for_hwpoison(), madvise_inject_error() is the one that increments hugepage refcount for every error injected. Different from other cases, MFD_MF_KEEP_UE_MAPPED makes the hugepage still a in-use page after memory_failure(MF_COUNT_INCREASED), so I think madvise_inject_error() should decrement in MFD_MF_KEEP_UE_MAPPED case.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in __get_huge_page_for_hwpoison(), even if it runs into multiple errors. When filemap_offline_hwpoison_folio_hugetlb() drops the refcount elevated by filemap_get_folios(), it only needs to decrement again if folio_ref_dec_and_test() returns false. I tested something like below:
/* drop the refcount elevated by filemap_get_folios. */
folio_put(folio);
if (folio_ref_count(folio))
folio_put(folio);
/* now refcount should be zero. */
ret = dissolve_free_hugetlb_folio(folio);Besides, the good news is that filemap_offline_hwpoison_folio_hugetlb() no longer needs to touch raw_hwp_list.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
+
SetPageHWPoison(curr->page);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
If hugetlb folio vmemmap is optimized, I think SetPageHWPoison might trigger BUG.
Ah, I see, vmemmap optimization doesn't allow us to move flags from
raw_hwp_listto tail pages. I guess the best I can do is to bail out if vmemmap is enabled likefolio_clear_hugetlb_hwpoison().I think you can do this after
hugetlb_vmemmap_restore_folio()is called.
Since I can get rid of the wrong folio_put() per raw HWPoison page, I can just rely on dissolve_free_hugetlb_folio() to do the hugetlb_vmemmap_restore_folio() and reuse the folio_clear_hugetlb_hwpoison() code to move HWPoison flags to raw pages.
I will do some more testing while preparing v4. Will also try if I can avoid adding a speical cased folio_put() in madvise_inject_error().
Quoted 3 lines
Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 13 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 13 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. When
This might not always hold true. When MF occurs while hugetlb folio is under isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case), __get_huge_page_for_hwpoison won't get extra folio refcnt.
Quoted 10 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);
So I think above code might drop the folio refcnt held by isolating code.
Thanks.
.
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2112
Quoted 13 lines
mm/memory-failure.c +12
-0+static void filemap_offline_hwpoison_folio_hugetlb (struct folio *folio) +{ +int ret; +struct llist_node *head; +struct raw_hwp_page *curr, *next; + +/* +* Since folio is still in the folio_batch, drop the refcount +* elevated by filemap_get_folios. +*/ +folio_put_refs (folio,1 ); + head =llist_del_all (raw_hwp_list_head (folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
mm/memory-failure.c int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, · line 2122
Quoted 13 lines
mm/memory-failure.c +10
-0+ +/* +* Release refcounts held by try_memory_failure_hugetlb, one per +* HWPoison-ed page in the raw hwp list. +* +* Set HWPoison flag on each page so that free_has_hwpoisoned() +* can exclude them during dissolve_free_hugetlb_folio(). +*/ +llist_for_each_entry_safe (curr, next, head, node) { +folio_put (folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:
Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Hi Miaohe, thanks for raising the concern. Given two things below
- both folio_isolate_hugetlb() and get_huge_page_for_hwpoison() are guarded by hugetlb_lock.
- hugetlb_update_hwpoison() only folio_test_set_hwpoison() for non-isolated folio after folio_try_get() succeeds.
as long as folio_test_set_hwpoison() is true here, this refcount should never come from folio_isolate_hugetlb(). What do you think?
For folio under isolation, MF ignores it without folio_test_set_hwpoison(), and filemap_offline_hwpoison_folio_hugetlb() won't happen at all. For HWPoison folio, MF has made the folio no longer being able to isolate/migrate.
Quoted 3 lines
Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 217 lines · Jiaqi Yan
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Review
Quoted 13 lines
(quoted diff) +12
-0+static void filemap_offline_hwpoison_folio_hugetlb(struct folio *folio) +{ + int ret; + struct llist_node *head; + struct raw_hwp_page *curr, *next; + + /* + * Since folio is still in the folio_batch, drop the refcount + * elevated by filemap_get_folios. + */ + folio_put_refs(folio, 1); + head = llist_del_all(raw_hwp_list_head(folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +10
-0+ + /* + * Release refcounts held by try_memory_failure_hugetlb, one per + * HWPoison-ed page in the raw hwp list. + * + * Set HWPoison flag on each page so that free_has_hwpoisoned() + * can exclude them during dissolve_free_hugetlb_folio(). + */ + llist_for_each_entry_safe(curr, next, head, node) { + folio_put(folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Quoted 9 lines
Hi Miaohe, thanks for raising the concern. Given two things below
- bothfolio_isolate_hugetlb()andget_huge_page_for_hwpoison()are guarded byhugetlb_lock.
-hugetlb_update_hwpoison()onlyfolio_test_set_hwpoison()for non-isolated folio afterfolio_try_get()succeeds.as long as
folio_test_set_hwpoison()is true here, this refcount should never come fromfolio_isolate_hugetlb(). What do you think?
Let's think about below scenario. When __get_huge_page_for_hwpoison() encounters an isolated hugetlb folio:
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool *migratable_cleared)
{
struct page *page = pfn_to_page(pfn);
struct folio *folio = page_folio(page);
bool count_increased = false;
int ret, rc;
if (!folio_test_hugetlb(folio)) {
ret = MF_HUGETLB_NON_HUGEPAGE;
goto out;
} else if (flags & MF_COUNT_INCREASED) {
ret = MF_HUGETLB_IN_USED;
count_increased = true;
} else if (folio_test_hugetlb_freed(folio)) {
ret = MF_HUGETLB_FREED;
} else if (folio_test_hugetlb_migratable(folio)) {
^^^^*Since hugetlb_migratable is cleared for the isolated hugetlb folio*
if (folio_try_get(folio)) {
ret = MF_HUGETLB_IN_USED;
count_increased = true;
} else {
ret = MF_HUGETLB_FREED;
}
} else {
^^^^*Code will reach here without extra refcnt increased*
ret = MF_HUGETLB_RETRY;
if (!(flags & MF_NO_RETRY))
goto out;
}
*Code will reach here after retry*
rc = hugetlb_update_hwpoison(folio, page);
if (rc >= MF_HUGETLB_FOLIO_PRE_POISONED) {
ret = rc;
goto out;
}So hugetlb_update_hwpoison() will be called even for folio under isolation without folio_try_get(). Or am I miss something?
Thanks.
.
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 260 lines · Miaohe Lin
Quoted 217 lines · Jiaqi Yan
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Review
Quoted 13 lines
(quoted diff) +12
-0+static void filemap_offline_hwpoison_folio_hugetlb(struct folio *folio) +{ + int ret; + struct llist_node *head; + struct raw_hwp_page *curr, *next; + + /* + * Since folio is still in the folio_batch, drop the refcount + * elevated by filemap_get_folios. + */ + folio_put_refs(folio, 1); + head = llist_del_all(raw_hwp_list_head(folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +10
-0+ + /* + * Release refcounts held by try_memory_failure_hugetlb, one per + * HWPoison-ed page in the raw hwp list. + * + * Set HWPoison flag on each page so that free_has_hwpoisoned() + * can exclude them during dissolve_free_hugetlb_folio(). + */ + llist_for_each_entry_safe(curr, next, head, node) { + folio_put(folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Quoted 9 lines
Hi Miaohe, thanks for raising the concern. Given two things below
- bothfolio_isolate_hugetlb()andget_huge_page_for_hwpoison()are guarded byhugetlb_lock.
-hugetlb_update_hwpoison()onlyfolio_test_set_hwpoison()for non-isolated folio afterfolio_try_get()succeeds.as long as
folio_test_set_hwpoison()is true here, this refcount should never come fromfolio_isolate_hugetlb(). What do you think?Let's think about below scenario. When
__get_huge_page_for_hwpoison()encounters an isolated hugetlb folio:int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); bool count_increased = false; int ret, rc; if (!folio_test_hugetlb(folio)) { ret = MF_HUGETLB_NON_HUGEPAGE; goto out; } else if (flags & MF_COUNT_INCREASED) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = MF_HUGETLB_FREED; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*Since hugetlb_migratable is cleared for the isolated hugetlb folio* if (folio_try_get(folio)) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else { ret = MF_HUGETLB_FREED; } } else {
Quoted 8 lines
^^^^*Code will reach here without extra refcnt increased*
ret = MF_HUGETLB_RETRY;
if (!(flags & MF_NO_RETRY))
goto out;
}
*Code will reach here after retry*You are right, thanks for pointing that out. Let me think about more how to handle this.
Quoted 8 lines
rc = hugetlb_update_hwpoison(folio, page); if (rc >= MF_HUGETLB_FOLIO_PRE_POISONED) { ret = rc; goto out; }So
hugetlb_update_hwpoison()will be called even for folio under isolation withoutfolio_try_get(). Or am I miss something?
Just a random question: if MF never increments a hugepage's refcount, what does the folio_put() in me_huge_page() (when mapping = null) do? Is it dropping for something other than MF?
Quoted 3 lines
Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 279 lines · Jiaqi Yan
Quoted 260 lines · Miaohe Lin
Quoted 217 lines · Jiaqi Yan
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Review
Quoted 13 lines
(quoted diff) +12
-0+static void filemap_offline_hwpoison_folio_hugetlb(struct folio *folio) +{ + int ret; + struct llist_node *head; + struct raw_hwp_page *curr, *next; + + /* + * Since folio is still in the folio_batch, drop the refcount + * elevated by filemap_get_folios. + */ + folio_put_refs(folio, 1); + head = llist_del_all(raw_hwp_list_head(folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +10
-0+ + /* + * Release refcounts held by try_memory_failure_hugetlb, one per + * HWPoison-ed page in the raw hwp list. + * + * Set HWPoison flag on each page so that free_has_hwpoisoned() + * can exclude them during dissolve_free_hugetlb_folio(). + */ + llist_for_each_entry_safe(curr, next, head, node) { + folio_put(folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Quoted 9 lines
Hi Miaohe, thanks for raising the concern. Given two things below
- bothfolio_isolate_hugetlb()andget_huge_page_for_hwpoison()are guarded byhugetlb_lock.
-hugetlb_update_hwpoison()onlyfolio_test_set_hwpoison()for non-isolated folio afterfolio_try_get()succeeds.as long as
folio_test_set_hwpoison()is true here, this refcount should never come fromfolio_isolate_hugetlb(). What do you think?Let's think about below scenario. When
__get_huge_page_for_hwpoison()encounters an isolated hugetlb folio:int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); bool count_increased = false; int ret, rc; if (!folio_test_hugetlb(folio)) { ret = MF_HUGETLB_NON_HUGEPAGE; goto out; } else if (flags & MF_COUNT_INCREASED) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = MF_HUGETLB_FREED; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*Since hugetlb_migratable is cleared for the isolated hugetlb folio* if (folio_try_get(folio)) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else { ret = MF_HUGETLB_FREED; } } else {Quoted 8 lines
^^^^*Code will reach here without extra refcnt increased* ret = MF_HUGETLB_RETRY; if (!(flags & MF_NO_RETRY)) goto out; } *Code will reach here after retry*You are right, thanks for pointing that out. Let me think about more how to handle this.
Quoted 6 lines
rc = hugetlb_update_hwpoison(folio, page); if (rc >= MF_HUGETLB_FOLIO_PRE_POISONED) { ret = rc; goto out; }
Quoted 4 lines
Quoted 2 lines
So
hugetlb_update_hwpoison()will be called even for folio under isolation withoutfolio_try_get(). Or am I miss something?Just a random question: if MF never increments a hugepage's refcount,
MF will hold hugetlb folio's refcount unless it's freed or isolated.
Quoted 2 lines
what does the
folio_put()inme_huge_page()(when mapping = null) do? Is it dropping for something other than MF?
For isolated hugetlb folio, MF_HUGETLB_RETRY will be returned and code won't reach here. Thanks.
.
Re: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 270 lines · Miaohe Lin
Quoted 268 lines · Jiaqi Yan
Quoted 267 lines · Miaohe Lin
Quoted 217 lines · Jiaqi Yan
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Review
Quoted 13 lines
(quoted diff) +12
-0+static void filemap_offline_hwpoison_folio_hugetlb(struct folio *folio) +{ + int ret; + struct llist_node *head; + struct raw_hwp_page *curr, *next; + + /* + * Since folio is still in the folio_batch, drop the refcount + * elevated by filemap_get_folios. + */ + folio_put_refs(folio, 1); + head = llist_del_all(raw_hwp_list_head(folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +10
-0+ + /* + * Release refcounts held by try_memory_failure_hugetlb, one per + * HWPoison-ed page in the raw hwp list. + * + * Set HWPoison flag on each page so that free_has_hwpoisoned() + * can exclude them during dissolve_free_hugetlb_folio(). + */ + llist_for_each_entry_safe(curr, next, head, node) { + folio_put(folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Quoted 9 lines
Hi Miaohe, thanks for raising the concern. Given two things below
- bothfolio_isolate_hugetlb()andget_huge_page_for_hwpoison()are guarded byhugetlb_lock.
-hugetlb_update_hwpoison()onlyfolio_test_set_hwpoison()for non-isolated folio afterfolio_try_get()succeeds.as long as
folio_test_set_hwpoison()is true here, this refcount should never come fromfolio_isolate_hugetlb(). What do you think?Let's think about below scenario. When
__get_huge_page_for_hwpoison()encounters an isolated hugetlb folio:int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); bool count_increased = false; int ret, rc; if (!folio_test_hugetlb(folio)) { ret = MF_HUGETLB_NON_HUGEPAGE; goto out; } else if (flags & MF_COUNT_INCREASED) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = MF_HUGETLB_FREED; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*Since hugetlb_migratable is cleared for the isolated hugetlb folio* if (folio_try_get(folio)) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else { ret = MF_HUGETLB_FREED; } } else { ^^^^*Code will reach here without extra refcnt increased* ret = MF_HUGETLB_RETRY; if (!(flags & MF_NO_RETRY)) goto out; }
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
*Code will reach here after retry*
You are right, thanks for pointing that out. Let me think about more how to handle this.
I was struggling to find a good fix, as I really don't want to memoize into the folio that if memory_failure has elevated a refcount.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
rc = hugetlb_update_hwpoison(folio, page); if (rc >= MF_HUGETLB_FOLIO_PRE_POISONED) { ret = rc; goto out; }
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So
hugetlb_update_hwpoison()will be called even for folio under isolation withoutfolio_try_get(). Or am I miss something?Just a random question: if MF never increments a hugepage's refcount,
MF will hold hugetlb folio's refcount unless it's freed or isolated.
A random thought. For an isolated hugetlb folio, if it becomes hwpoison (after __get_huge_page_for_hwpoison() failed with retries), and then folio_putback_hugetlb() is called, should we block setting migratable and putting it back to hugepage_activelist? IWO, make it forever isolated and just decrement refcount:
void folio_putback_hugetlb(struct folio *folio)
{
spin_lock_irq(&hugetlb_lock);- folio_set_hugetlb_migratable(folio);
- list_move_tail(&folio->lru,
&(folio_hstate(folio))->hugepage_activelist);
+ if (!folio_test_hwpoison(folio)) {
+ folio_set_hugetlb_migratable(folio);
+ list_move_tail(&folio->lru,
&(folio_hstate(folio))->hugepage_activelist);
+ }
spin_unlock_irq(&hugetlb_lock);
folio_put(folio);(Maybe the event "become hwpoison => folio_putback_hugetlb()" can never happen?)
If so, as a side effect, I can use folio_putback_hugetlb() to decrement the refcount even if we are uncertain that the residue refcount is whether from memory_failure or folio_isolate_hugetlb().
Quoted 7 lines
Quoted 3 lines
what does the
folio_put()inme_huge_page()(when mapping = null) do? Is it dropping for something other than MF?For isolated hugetlb folio,
MF_HUGETLB_RETRYwill be returned and code won't reach here. Thanks.
.
Miaohe Lin
maintainerRe: [PATCH v3 1/3] mm: memfd/hugetlb: introduce memfd-based userspace MFR policy
Quoted 308 lines · Jiaqi Yan
Quoted 270 lines · Miaohe Lin
Quoted 268 lines · Jiaqi Yan
Quoted 267 lines · Miaohe Lin
Quoted 217 lines · Jiaqi Yan
Quoted 207 lines · Miaohe Lin
Quoted 181 lines · Jiaqi Yan
Quoted 34 lines · Miaohe Lin
Quoted 32 lines · Jiaqi Yan
Quoted 30 lines · Miaohe Lin
Quoted 9 lines · Jiaqi Yan
Sometimes immediately hard offlining a large chunk of contigous memory having uncorrected memory errors (UE) may not be the best option. Cloud providers usually serve capacity- and performance-critical guest memory with 1G HugeTLB hugepages, as this significantly reduces the overhead associated with managing page tables and TLB misses. However, for today's HugeTLB system, once a byte of memory in a hugepage is hardware corrupted, the kernel discards the whole hugepage, including the healthy portion. Customer workload running in the VM can hardly recover from such a great loss of memory.
Thanks for your patch. Some questions below.
Quoted 6 lines
Therefore keeping or discarding a large chunk of contiguous memory owned by userspace (particularly to serve guest memory) due to recoverable UE may better be controlled by userspace process that owns the memory, e.g. VMM in the Cloud environment.
Quoted 8 lines
Introduce a memfd-based userspace memory failure (MFR) policy,
MFD_MF_KEEP_UE_MAPPED. It is possible to support for other memfd, but the current implementation only covers HugeTLB.For a hugepage associated with
MFD_MF_KEEP_UE_MAPPEDenabled memfd, whenever it runs into a new UE,* MFR defers hard offline operations, i.e., unmapping and
So the folio can't be unpoisoned until hugetlb folio becomes free?
Quoted 7 lines
Quoted 5 lines
Are you asking from testing perspective, are we still able to clean up injected test errors via
unpoison_memory()withMFD_MF_KEEP_UE_MAPPED?If so,
unpoison_memory()can't turn the HWPoison hugetlb page to normal hugetlb page asMFD_MF_KEEP_UE_MAPPEDautomatically dissolvesWe might loss some testability but that should be an acceptable compromise.
To clarify, looking at
unpoison_memory(), it seems unpoison should still work if called before truncated or memfd closed.What I wanted to say is, for my test
hugetlb-mfr.c, since I really want to test the cleanup code (dissolving free hugepage having multiple errors) after truncation or memfd closed, so we can only unpoison the raw pages rejected by buddy allocator.Quoted 46 lines
Quoted 46 lines
it.
unpoison_memory(pfn)can probably still turn the HWPoison raw page back to a normal one, but you already lost the hugetlb page.Quoted 41 lines
Quoted 23 lines
dissolving. MFR still sets HWPoison flag, holds a refcount for every raw HWPoison page, record them in a list, sends SIGBUS to the consuming thread, but
si_addr_lsbis reduced toPAGE_SHIFT. If userspace is able to handle the SIGBUS, the HWPoison hugepage remains accessible via the mapping created with that memfd.* If the memory was not faulted in yet, the fault handler also allows fault in the HWPoison folio.
For a
MFD_MF_KEEP_UE_MAPPEDenabled memfd, when it is closed, or when userspace process truncates its hugepages:* When the HugeTLB in-memory file system removes the filemap's folios one by one, it asks MFR to deal with HWPoison folios on the fly, implemented by
filemap_offline_hwpoison_folio().* MFR drops the refcounts being held for the raw HWPoison pages within the folio. Now that the HWPoison folio becomes free, MFR dissolves it into a set of raw pages. The healthy pages are recycled into buddy allocator, while the HWPoison ones are prevented from re-allocation.
...
Review
Quoted 13 lines
(quoted diff) +12
-0+static void filemap_offline_hwpoison_folio_hugetlb(struct folio *folio) +{ + int ret; + struct llist_node *head; + struct raw_hwp_page *curr, *next; + + /* + * Since folio is still in the folio_batch, drop the refcount + * elevated by filemap_get_folios. + */ + folio_put_refs(folio, 1); + head = llist_del_all(raw_hwp_list_head(folio));We might race with
get_huge_page_for_hwpoison()?llist_add()might be called byfolio_set_hugetlb_hwpoison()just afterllist_del_all()?Quoted 7 lines
Quoted 1 line
Oh, when there is a new UE while we releasing the folio here, right?
Right.
Quoted 1 line
In that case, would
mutex_lock(&mf_mutex)eliminate potential race?IMO
spin_lock_irq(&hugetlb_lock)might be better.Looks like I don't need any lock given the correction below.
Quoted 72 lines
Quoted 18 lines
Quoted 14 lines
Review
Quoted 13 lines
(quoted diff) +10
-0+ + /* + * Release refcounts held by try_memory_failure_hugetlb, one per + * HWPoison-ed page in the raw hwp list. + * + * Set HWPoison flag on each page so that free_has_hwpoisoned() + * can exclude them during dissolve_free_hugetlb_folio(). + */ + llist_for_each_entry_safe(curr, next, head, node) { + folio_put(folio);Quoted 3 lines
The hugetlb folio refcnt will only be increased once even if it contains multiple UE sub-pages. See
__get_huge_page_for_hwpoison()for details. Sofolio_put()might be called more times thanfolio_try_get()in__get_huge_page_for_hwpoison().Quoted 5 lines
The changes in
folio_set_hugetlb_hwpoison()should make__get_huge_page_for_hwpoison()not to take the "out" path which decrease the increased refcount for folio. IOW, every time a new UE happens, we handle the hugetlb page as if it is an in-use hugetlb page.See below code snippet (comment [1] and [2]):
int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); int ret = 2; /* fallback to normal page handling */ bool count_increased = false; if (!folio_test_hugetlb(folio)) goto out; if (flags & MF_COUNT_INCREASED) { ret = 1; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = 0; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*hugetlb_migratable is checked before trying to get folio refcnt* [1] ret = folio_try_get(folio); if (ret) count_increased = true; } else { ret = -EBUSY; if (!(flags & MF_NO_RETRY)) goto out; } if (folio_set_hugetlb_hwpoison(folio, page)) { ret = -EHWPOISON; goto out; } /* * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them * from being migrated by memory hotremove. */ if (count_increased && folio_test_hugetlb_migratable(folio)) { folio_clear_hugetlb_migratable(folio); ^^^^^*hugetlb_migratable is cleared when first time seeing folio* [2] *migratable_cleared = true; }Quoted 1 line
Or am I miss something?
Quoted 15 lines
Thanks for your explaination! You are absolutely right. It turns out the extra refcount I saw (during running
hugetlb-mfr.c) on the folio at the moment offilemap_offline_hwpoison_folio_hugetlb()is actually because of theMF_COUNT_INCREASEDduringMADV_HWPOISON. In the past I used to think that is the effect offolio_try_get()in__get_huge_page_for_hwpoison(), and it is wrong. Now I see two cases:
-MADV_HWPOISON: instead of__get_huge_page_for_hwpoison(),madvise_inject_error()is the one that increments hugepage refcount for every error injected. Different from other cases,MFD_MF_KEEP_UE_MAPPEDmakes the hugepage still a in-use page aftermemory_failure(MF_COUNT_INCREASED), so I thinkmadvise_inject_error()should decrement inMFD_MF_KEEP_UE_MAPPEDcase.
- In the real world: as you pointed out, MF always just increments hugepage refcount once in__get_huge_page_for_hwpoison(), even if it runs into multiple errors. WhenThis might not always hold true. When MF occurs while hugetlb folio is under
isolation(hugetlb_migratable is cleared and extra folio refcnt is held by isolating code in that case),__get_huge_page_for_hwpoisonwon't get extra folio refcnt.Quoted 4 lines
filemap_offline_hwpoison_folio_hugetlb()drops the refcount elevated byfilemap_get_folios(), it only needs to decrement again iffolio_ref_dec_and_test()returns false. I tested something like below:Quoted 8 lines
Quoted 6 lines
/* drop the refcount elevated by filemap_get_folios. */ folio_put(folio); if (folio_ref_count(folio)) folio_put(folio); /* now refcount should be zero. */ ret = dissolve_free_hugetlb_folio(folio);So I think above code might drop the folio refcnt held by isolating code.
Quoted 9 lines
Hi Miaohe, thanks for raising the concern. Given two things below
- bothfolio_isolate_hugetlb()andget_huge_page_for_hwpoison()are guarded byhugetlb_lock.
-hugetlb_update_hwpoison()onlyfolio_test_set_hwpoison()for non-isolated folio afterfolio_try_get()succeeds.as long as
folio_test_set_hwpoison()is true here, this refcount should never come fromfolio_isolate_hugetlb(). What do you think?Let's think about below scenario. When
__get_huge_page_for_hwpoison()encounters an isolated hugetlb folio:int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool*migratable_cleared){ struct page *page = pfn_to_page(pfn); struct folio *folio = page_folio(page); bool count_increased = false; int ret, rc; if (!folio_test_hugetlb(folio)) { ret = MF_HUGETLB_NON_HUGEPAGE; goto out; } else if (flags & MF_COUNT_INCREASED) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else if (folio_test_hugetlb_freed(folio)) { ret = MF_HUGETLB_FREED; } else if (folio_test_hugetlb_migratable(folio)) { ^^^^*Since hugetlb_migratable is cleared for the isolated hugetlb folio* if (folio_try_get(folio)) { ret = MF_HUGETLB_IN_USED; count_increased = true; } else { ret = MF_HUGETLB_FREED; } } else { ^^^^*Code will reach here without extra refcnt increased* ret = MF_HUGETLB_RETRY; if (!(flags & MF_NO_RETRY)) goto out; }Quoted 4 lines
Quoted 4 lines
Quoted 1 line
*Code will reach here after retry*
You are right, thanks for pointing that out. Let me think about more how to handle this.
I was struggling to find a good fix, as I really don't want to memoize into the folio that if
memory_failurehas elevated a refcount.Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
rc = hugetlb_update_hwpoison(folio, page); if (rc >= MF_HUGETLB_FOLIO_PRE_POISONED) { ret = rc; goto out; }Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So
hugetlb_update_hwpoison()will be called even for folio under isolation withoutfolio_try_get(). Or am I miss something?Just a random question: if MF never increments a hugepage's refcount,
MF will hold hugetlb folio's refcount unless it's freed or isolated.
A random thought. For an isolated hugetlb folio, if it becomes hwpoison (after
__get_huge_page_for_hwpoison()failed with retries), and thenfolio_putback_hugetlb()is called, should we block setting migratable and putting it back tohugepage_activelist? IWO, make it forever isolated and just decrement refcount:void
folio_putback_hugetlb(struct folio *folio){ spin_lock_irq(&hugetlb_lock);(quoted diff) +0
-2- folio_set_hugetlb_migratable(folio); - list_move_tail(&folio->lru,&(folio_hstate(folio))->hugepage_activelist);
(quoted diff) +3
-0+ if (!folio_test_hwpoison(folio)) { + folio_set_hugetlb_migratable(folio); + list_move_tail(&folio->lru,
Quoted 2 lines
&(folio_hstate(folio))->hugepage_activelist);
+ }
Will it also block the hugetlb folio from being freed and dissolved later when last folio refcnt is gone?
Quoted 8 lines
spin_unlock_irq(&hugetlb_lock); folio_put(folio);(Maybe the event "become hwpoison =>
folio_putback_hugetlb()" can never happen?)If so, as a side effect, I can use
folio_putback_hugetlb()to decrement the refcount even if we are uncertain that the residue refcount is whether frommemory_failureorfolio_isolate_hugetlb().
What if the caller of folio_isolate_hugetlb() has called folio_putback_hugetlb() before us? Can we tell that apart?
Thanks.
.
Lorenzo Stoakes
reviewerHi Jiaqi,
It seems this series got stalled on review, wondered if you planned to respin it at all?
Thanks, Lorenzo
Quoted 5 lines · Lorenzo Stoakes
Hi Jiaqi,
It seems this series got stalled on review, wondered if you planned to respin it at all?
Hi Lorenzo,
Sorry for the late reply.
Yeah, I was stuck on the issue Miaohe pointed out for quite a long time...
In offline chat with William <[email protected]> he expressed interests in taking over this patch series. However, he seemed busy with something else and couldn't get to it.
Fortunately we have more powerful AI now than we did six months ago. While I worked on [1] with an AI assistant since 2 weeks ago, I worked out a solution to the issue Miaohe raised [2] with the help from AI, and now a working draft patch series. I think by the end of this weekend, I can send out a v4 built on [1], the patch series I just posted for freeing high-order folio that has HWPoison subpages.
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]/#t
Quoted 2 lines
Thanks, Lorenzo
Lorenzo Stoakes (ARM)
reviewerQuoted 24 lines · Jiaqi Yan
Quoted 5 lines · Lorenzo Stoakes
Hi Jiaqi,
It seems this series got stalled on review, wondered if you planned to respin it at all?
Hi Lorenzo,
Sorry for the late reply.
Yeah, I was stuck on the issue Miaohe pointed out for quite a long time...
In offline chat with William <[email protected]> he expressed interests in taking over this patch series. However, he seemed busy with something else and couldn't get to it.
Fortunately we have more powerful AI now than we did six months ago. While I worked on [1] with an AI assistant since 2 weeks ago, I worked out a solution to the issue Miaohe raised [2] with the help from AI, and now a working draft patch series. I think by the end of this weekend, I can send out a v4 built on [1], the patch series I just posted for freeing high-order folio that has HWPoison subpages.
Quoted 2 lines
[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]/#t
Thanks for the update! Though do note [0], given the current wave of AI-generated fixes this cycle is focused on that rather than new features (sorry I know I chased this, but nobody anticipated how busy we would be with these!)
And since you mention AI, obligatory disclaimer: as with everything AI - please rewrite all commit messages and comments in kernel mm style, and make sure you understand every bit of the code when you submit! :)
--
Cheers, Lorenzo