Skip to contentSkip to conversation

Memory poison recovery in khugepaged

RFC Superseded Jiaqi Yan 3 participants 14 messages Mar 23, 2022 – Apr 6, 2022

Subsystems (3)

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Jiaqi Yan

author
#0 lore raw

cover Memory poison recovery in khugepaged

Problem
=======
Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged.

Impact
======
The main reason we chose to make khugepaged tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving application 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience.

Solution
========
As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed.

We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages.

Jiaqi Yan (2):
mm: khugepaged: recover from poisoned anonymous memory
mm: khugepaged: recover from poisoned file-backed memory

 include/linux/highmem.h |  37 +++++++
 mm/khugepaged.c         | 211 +++++++++++++++++++++++++++++-----------
 2 files changed, 189 insertions(+), 59 deletions(-)
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
author
#1 lore raw

RFC 1/2 mm: khugepaged: recover from poisoned anonymous memory

Make __collapse_huge_page_copy return whether collapsing/copying anonymous pages succeeded, and make collapse_huge_page handle the return status.

Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. Roll back the page table and page states in the 2nd loop when copy failed:
1) re-establish the PTEs-to-PMD connection.
2) release pages back to their LRU list.

---
 include/linux/highmem.h |  19 ++++++
 mm/khugepaged.c         | 136 ++++++++++++++++++++++++++++++----------
 2 files changed, 122 insertions(+), 33 deletions(-)
include/linux/highmem.h +19 -0
index 39bb9b47fa9c..15d0aa4d349c 100644
@@ -281,6 +281,25 @@ static inline void copy_user_highpage(struct page *to, struct page *from,
 	kunmap_local(vfrom);
 }
 
+/*
+ * Machine check exception handled version of copy_user_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_user_highpage_mc(struct page *to, struct page *from,
+	unsigned long vaddr, struct vm_area_struct *vma)
+{
+	char *vfrom, *vto;
+	unsigned long ret;
+
+	vfrom = kmap_local_page(from);
+	vto = kmap_local_page(to);
+	ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+	kunmap_local(vto);
+	kunmap_local(vfrom);
+
+	return ret > 0;
+}
+
 #endif
 
 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
mm/khugepaged.c +103 -33

183 lines not on this page · raw message

Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#2 lore raw

Re: [RFC v1 1/2] mm: khugepaged: recover from poisoned anonymous memory

Quoted 24 lines · Jiaqi Yan

Make __collapse_huge_page_copy return whether collapsing/copying anonymous pages succeeded, and make collapse_huge_page handle the return status.

Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. Roll back the page table and page states in the 2nd loop when copy failed:
1) re-establish the PTEs-to-PMD connection.
2) release pages back to their LRU list.

---
 include/linux/highmem.h |  19 ++++++
 mm/khugepaged.c         | 136 ++++++++++++++++++++++++++++++----------
 2 files changed, 122 insertions(+), 33 deletions(-)
include/linux/highmem.h +0 -0
index 39bb9b47fa9c..15d0aa4d349c 100644

include/linux/highmem.h static inline void copy_user_highpage(struct page *to, struct page *from, · line 289

Quoted 9 lines
include/linux/highmem.h +6 -0
@@ -281,6 +281,25 @@ static inline void copy_user_highpage(struct page *to, struct page *from,
        kunmap_local(vfrom);
 }
 
+/*
+ * Machine check exception handled version of copy_user_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_user_highpage_mc(struct page *to, struct page *from,
+       unsigned long vaddr, struct vm_area_struct *vma)

Patch #2 defined copy_highpage_mc() which has the same implementation. This function has two unused parameters: vaddr and vma. It seems we just need to keep one.

Quoted 88 lines
include/linux/highmem.h +13 -0
+{
+       char *vfrom, *vto;
+       unsigned long ret;
+
+       vfrom = kmap_local_page(from);
+       vto = kmap_local_page(to);
+       ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+       kunmap_local(vto);
+       kunmap_local(vfrom);
+
+       return ret > 0;
+}
+
 #endif
 
 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
mm/khugepaged.c +42 -17
index 131492fd1148..84ed177f56ff 100644
@@ -739,44 +739,97 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
        return 0;
 }
 
-static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
-                                     struct vm_area_struct *vma,
-                                     unsigned long address,
-                                     spinlock_t *ptl,
-                                     struct list_head *compound_pagelist)
+/*
+ * __collapse_huge_page_copy - attempts to copy memory contents from normal
+ * pages to a hugepage. Cleanup the normal pages if copy succeeds;
+ * otherwise restore the original pmd page table.
+ *
+ * @pte: starting of the PTEs to copy from
+ * @page: the new hugepage to copy contents to
+ * @pmd: pointer to the new hugepage's PMD
+ * @rollback: the original normal PTEs' PMD
+ * @address: starting address to copy
+ * @pte_ptl: lock on normal pages' PTEs
+ * @compound_pagelist: list that stores compound pages
+ */
+static bool __collapse_huge_page_copy(pte_t *pte,
+                               struct page *page,
+                               pmd_t *pmd,
+                               pmd_t rollback,
+                               struct vm_area_struct *vma,
+                               unsigned long address,
+                               spinlock_t *pte_ptl,
+                               struct list_head *compound_pagelist)
 {
        struct page *src_page, *tmp;
        pte_t *_pte;
-       for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-                               _pte++, page++, address += PAGE_SIZE) {
-               pte_t pteval = *_pte;
+       pte_t pteval;
+       unsigned long _address;
+       spinlock_t *pmd_ptl;
+       bool copy_succeeded = true;
 
-               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+       /*
+        * Copying pages' contents is subject to memory poison at any iteration.
+        */
+       for (_pte = pte, _address = address;
+                       _pte < pte + HPAGE_PMD_NR;
+                       _pte++, page++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval)))
                        clear_user_highpage(page, address);
-                       add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
-                       if (is_zero_pfn(pte_pfn(pteval))) {
-                               /*
-                                * ptl mostly unnecessary.
-                                */
-                               spin_lock(ptl);
-                               ptep_clear(vma->vm_mm, address, _pte);
-                               spin_unlock(ptl);
+               else {
+                       src_page = pte_page(pteval);
+                       if (copy_user_highpage_mc(page, src_page, address, vma)) {
+                               copy_succeeded = false;
+                               break;
+                       }
+               }
+       }

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 798

Quoted 10 lines
mm/khugepaged.c +10 -0
+
+       if (!copy_succeeded) {
+               /*
+                * Copying failed, re-establish the regular PMD that
+                * points to regular page table. Since PTEs are still
+                * isolated and locked, acquiring anon_vma_lock is unnecessary.
+                */
+               pmd_ptl = pmd_lock(vma->vm_mm, pmd);
+               pmd_populate(vma->vm_mm, pmd, pmd_pgtable(rollback));
+               spin_unlock(pmd_ptl);

I think we could just jump to list_for_each_entry_safe, right? But it seems that list_for_each_entry_safe is not enough, please see the later comments.

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 805

Quoted 7 lines
mm/khugepaged.c +7 -0
+       }
+
+       for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR;
+                       _pte++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+                       if (copy_succeeded) {

With the above jump, you don't need to check if copy is succeeded or not since this is the succeeded path only.

Quoted 42 lines
mm/khugepaged.c +22 -11
+                               add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
+                               if (is_zero_pfn(pte_pfn(pteval))) {
+                                       /*
+                                        * ptl mostly unnecessary.
+                                        */
+                                       spin_lock(pte_ptl);
+                                       pte_clear(vma->vm_mm, _address, _pte);
+                                       spin_unlock(pte_ptl);
+                               }
                        }
                } else {
                        src_page = pte_page(pteval);
-                       copy_user_highpage(page, src_page, address, vma);
                        if (!PageCompound(src_page))
                                release_pte_page(src_page);
-                       /*
-                        * ptl mostly unnecessary, but preempt has to
-                        * be disabled to update the per-cpu stats
-                        * inside page_remove_rmap().
-                        */
-                       spin_lock(ptl);
-                       ptep_clear(vma->vm_mm, address, _pte);
-                       page_remove_rmap(src_page, false);
-                       spin_unlock(ptl);
-                       free_page_and_swap_cache(src_page);
+
+                       if (copy_succeeded) {
+                               /*
+                                * ptl mostly unnecessary, but preempt has to
+                                * be disabled to update the per-cpu stats
+                                * inside page_remove_rmap().
+                                */
+                               spin_lock(pte_ptl);
+                               pte_clear(vma->vm_mm, _address, _pte);
+                               page_remove_rmap(src_page, false);
+                               spin_unlock(pte_ptl);
+                               free_page_and_swap_cache(src_page);
+                       }
                }
        }
 

mm/khugepaged.c static void __collapse_huge_page_copy(pte_t *pte, struct page *page, · line 839

Quoted 3 lines
mm/khugepaged.c +0 -0
@@ -784,6 +837,8 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
                list_del(&src_page->lru);
                release_pte_page(src_page);
        }

If copy failed you need to unpin the isolated pages and put them back to LRU IIUC.

Quoted 14 lines
mm/khugepaged.c +3 -0
+
+       return copy_succeeded;
 }
 
 static void khugepaged_alloc_sleep(void)
@@ -1066,6 +1121,7 @@ static void collapse_huge_page(struct mm_struct *mm,
        struct vm_area_struct *vma;
        struct mmu_notifier_range range;
        gfp_t gfp;
+       bool copy_succeeded = false;
 
        VM_BUG_ON(address & ~HPAGE_PMD_MASK);
 

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1239

Quoted 9 lines
mm/khugepaged.c +3 -2
@@ -1177,9 +1233,13 @@ static void collapse_huge_page(struct mm_struct *mm,
         */
        anon_vma_unlock_write(vma->anon_vma);
 
-       __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
-                       &compound_pagelist);
+       copy_succeeded = __collapse_huge_page_copy(pte, new_page, pmd, _pmd,
+                       vma, address, pte_ptl, &compound_pagelist);
        pte_unmap(pte);
+       if (!copy_succeeded) {

Shall the fail handling be moved before pte_unmap()?

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1240

Quoted 1 line
mm/khugepaged.c +1 -0
+               result = SCAN_FAIL;

I think a new result is preferred, for example, SCAN_COPY_MC, it would be helpful for debug.

Quoted 8 lines
mm/khugepaged.c +2 -0
+               goto out_up_write;
+       }
        /*
         * spin_lock() below is not the equivalent of smp_wmb(), but
         * the smp_wmb() inside __SetPageUptodate() can be reused to
@@ -1364,9 +1424,14 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
        pte_unmap_unlock(pte, ptl);
        if (ret) {

mm/khugepaged.c static int khugepaged_scan_pmd(struct mm_struct *mm, · line 1432

Quoted 10 lines
mm/khugepaged.c +6 -3
                node = khugepaged_find_target_node();
-               /* collapse_huge_page will return with the mmap_lock released */
-               collapse_huge_page(mm, address, hpage, node,
-                               referenced, unmapped);
+               /*
+                * collapse_huge_page will return with the mmap_r+w_lock released.
+                * It is uncertain if *hpage is NULL or not when collapse_huge_page
+                * returns, so keep ret=1 to jump to breakouterloop_mmap_lock
+                * in khugepaged_scan_mm_slot, then *hpage will be freed
+                * if collapse failed.

It may be not true for !NUMA case, the huge page may be reused, so you need to make sure the huge page is cleared before return, I had a patch that remove the special case for !NUMA
(https://lore.kernel.org/linux-mm/[email protected]/),
it could avoid clearing huge page, I should resurrect it.

Quoted 20 lines
mm/khugepaged.c +7 -1
+                */
+               collapse_huge_page(mm, address, hpage, node, referenced, unmapped);
        }
 out:
        trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
@@ -2168,6 +2233,11 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
                                khugepaged_scan_file(mm, file, pgoff, hpage);
                                fput(file);
                        } else {
+                               /*
+                                * mmap_read_lock is
+                                * 1) released if both scan and collapse succeeded;
+                                * 2) still held if either scan or collapse failed.
+                                */
                                ret = khugepaged_scan_pmd(mm, vma,
                                                khugepaged_scan.address,
                                                hpage);
--

2.35.1.894.gb6a874cedc-goog

author
#3 lore raw

Re: [RFC v1 1/2] mm: khugepaged: recover from poisoned anonymous memory

Quoted 29 lines · Yang Shi
Quoted 27 lines · Jiaqi Yan

Make __collapse_huge_page_copy return whether collapsing/copying anonymous pages succeeded, and make collapse_huge_page handle the return status.

Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. Roll back the page table and page states in the 2nd loop when copy failed:
1) re-establish the PTEs-to-PMD connection.
2) release pages back to their LRU list.

---
 include/linux/highmem.h |  19 ++++++
 mm/khugepaged.c         | 136 ++++++++++++++++++++++++++++++----------
 2 files changed, 122 insertions(+), 33 deletions(-)
include/linux/highmem.h +0 -0
index 39bb9b47fa9c..15d0aa4d349c 100644
@@ -281,6 +281,25 @@ static inline void copy_user_highpage(struct page *to, struct page *from,
        kunmap_local(vfrom);
 }
 
Quoted 11 lines

include/linux/highmem.h static inline void copy_user_highpage(struct page *to, struct page *from, · line 289

Quoted 6 lines
include/linux/highmem.h +6 -0
+/*
+ * Machine check exception handled version of copy_user_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_user_highpage_mc(struct page *to, struct page *from,
+       unsigned long vaddr, struct vm_area_struct *vma)

Patch #2 defined copy_highpage_mc() which has the same implementation. This function has two unused parameters: vaddr and vma. It seems we just need to keep one.

Version 2 will delete copy_user_highpage_mc() from the patch series, and just keep copy_highpage_mc().

Quoted 94 lines
Quoted 94 lines
include/linux/highmem.h +13 -0
+{
+       char *vfrom, *vto;
+       unsigned long ret;
+
+       vfrom = kmap_local_page(from);
+       vto = kmap_local_page(to);
+       ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+       kunmap_local(vto);
+       kunmap_local(vfrom);
+
+       return ret > 0;
+}
+
 #endif
 
 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
mm/khugepaged.c +48 -17
index 131492fd1148..84ed177f56ff 100644
@@ -739,44 +739,97 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
        return 0;
 }
 
-static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
-                                     struct vm_area_struct *vma,
-                                     unsigned long address,
-                                     spinlock_t *ptl,
-                                     struct list_head *compound_pagelist)
+/*
+ * __collapse_huge_page_copy - attempts to copy memory contents from normal
+ * pages to a hugepage. Cleanup the normal pages if copy succeeds;
+ * otherwise restore the original pmd page table.
+ *
+ * @pte: starting of the PTEs to copy from
+ * @page: the new hugepage to copy contents to
+ * @pmd: pointer to the new hugepage's PMD
+ * @rollback: the original normal PTEs' PMD
+ * @address: starting address to copy
+ * @pte_ptl: lock on normal pages' PTEs
+ * @compound_pagelist: list that stores compound pages
+ */
+static bool __collapse_huge_page_copy(pte_t *pte,
+                               struct page *page,
+                               pmd_t *pmd,
+                               pmd_t rollback,
+                               struct vm_area_struct *vma,
+                               unsigned long address,
+                               spinlock_t *pte_ptl,
+                               struct list_head *compound_pagelist)
 {
        struct page *src_page, *tmp;
        pte_t *_pte;
-       for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-                               _pte++, page++, address += PAGE_SIZE) {
-               pte_t pteval = *_pte;
+       pte_t pteval;
+       unsigned long _address;
+       spinlock_t *pmd_ptl;
+       bool copy_succeeded = true;
 
-               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+       /*
+        * Copying pages' contents is subject to memory poison at any iteration.
+        */
+       for (_pte = pte, _address = address;
+                       _pte < pte + HPAGE_PMD_NR;
+                       _pte++, page++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval)))
                        clear_user_highpage(page, address);
-                       add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
-                       if (is_zero_pfn(pte_pfn(pteval))) {
-                               /*
-                                * ptl mostly unnecessary.
-                                */
-                               spin_lock(ptl);
-                               ptep_clear(vma->vm_mm, address, _pte);
-                               spin_unlock(ptl);
+               else {
+                       src_page = pte_page(pteval);
+                       if (copy_user_highpage_mc(page, src_page, address, vma)) {
+                               copy_succeeded = false;
+                               break;
+                       }
+               }
+       }
+
+       if (!copy_succeeded) {
+               /*
+                * Copying failed, re-establish the regular PMD that
+                * points to regular page table. Since PTEs are still
+                * isolated and locked, acquiring anon_vma_lock is unnecessary.
Quoted 8 lines

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 798

Quoted 4 lines
mm/khugepaged.c +4 -0
+                */
+               pmd_ptl = pmd_lock(vma->vm_mm, pmd);
+               pmd_populate(vma->vm_mm, pmd, pmd_pgtable(rollback));
+               spin_unlock(pmd_ptl);

I think we could just jump to list_for_each_entry_safe, right? But it seems that list_for_each_entry_safe is not enough, please see the later comments.

Yes, it is not enough, we need to release_pte_page() for non-compound pages.

If we are going to use jump/goto, we need two for-loops over PTEs, one for the copy
succeeded case, and one for the failure case. What I have right now is combining the two for-loops into one.
I don't like to switch to jump because: 1) goto statements have
bad readability, 2) one for-loop is more concise than two for-loops.

Quoted 53 lines

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 805

Quoted 8 lines
mm/khugepaged.c +7 -0
+       }
+
+       for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR;
+                       _pte++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+                       if (copy_succeeded) {

With the above jump, you don't need to check if copy is succeeded or not since this is the succeeded path only.

Quoted 41 lines
mm/khugepaged.c +22 -11
+                               add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
+                               if (is_zero_pfn(pte_pfn(pteval))) {
+                                       /*
+                                        * ptl mostly unnecessary.
+                                        */
+                                       spin_lock(pte_ptl);
+                                       pte_clear(vma->vm_mm, _address, _pte);
+                                       spin_unlock(pte_ptl);
+                               }
                        }
                } else {
                        src_page = pte_page(pteval);
-                       copy_user_highpage(page, src_page, address, vma);
                        if (!PageCompound(src_page))
                                release_pte_page(src_page);
-                       /*
-                        * ptl mostly unnecessary, but preempt has to
-                        * be disabled to update the per-cpu stats
-                        * inside page_remove_rmap().
-                        */
-                       spin_lock(ptl);
-                       ptep_clear(vma->vm_mm, address, _pte);
-                       page_remove_rmap(src_page, false);
-                       spin_unlock(ptl);
-                       free_page_and_swap_cache(src_page);
+
+                       if (copy_succeeded) {
+                               /*
+                                * ptl mostly unnecessary, but preempt has to
+                                * be disabled to update the per-cpu stats
+                                * inside page_remove_rmap().
+                                */
+                               spin_lock(pte_ptl);
+                               pte_clear(vma->vm_mm, _address, _pte);
+                               page_remove_rmap(src_page, false);
+                               spin_unlock(pte_ptl);
+                               free_page_and_swap_cache(src_page);
+                       }
                }
        }
 
Quoted 7 lines

mm/khugepaged.c static void __collapse_huge_page_copy(pte_t *pte, struct page *page, · line 839

Quoted 4 lines
mm/khugepaged.c +0 -0
@@ -784,6 +837,8 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
                list_del(&src_page->lru);
                release_pte_page(src_page);
        }

If copy failed you need to unpin the isolated pages and put them back to LRU IIUC.

No matter if the copy failed or succeeded, we need to use a for-loop to unpin isolated pages
and put them back to LRU (i.e. release_pte_page()). In current code, this is a common
operation in the above for-loop.

Quoted 18 lines
Quoted 18 lines
mm/khugepaged.c +3 -0
+
+       return copy_succeeded;
 }
 
 static void khugepaged_alloc_sleep(void)
@@ -1066,6 +1121,7 @@ static void collapse_huge_page(struct mm_struct *mm,
        struct vm_area_struct *vma;
        struct mmu_notifier_range range;
        gfp_t gfp;
+       bool copy_succeeded = false;
 
        VM_BUG_ON(address & ~HPAGE_PMD_MASK);
 
@@ -1177,9 +1233,13 @@ static void collapse_huge_page(struct mm_struct *mm,
         */
        anon_vma_unlock_write(vma->anon_vma);
 
Quoted 8 lines

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1239

Quoted 6 lines
mm/khugepaged.c +3 -2
-       __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
-                       &compound_pagelist);
+       copy_succeeded = __collapse_huge_page_copy(pte, new_page, pmd, _pmd,
+                       vma, address, pte_ptl, &compound_pagelist);
        pte_unmap(pte);
+       if (!copy_succeeded) {

Shall the fail handling be moved before pte_unmap()?

I don't think so. I think at this point we need to pte_unmap() no matter if the copying
succeeded or not. It is obviously needed if __collapse_huge_page_copy succeeded (because existing code already assumes __collapse_huge_page_copy always succeeded).
For the failure case, my reasoning is based on __collapse_huge_page_isolate. If __collapse_huge_page_isolate failed, we need to pte_unmap() before goto out_up_write. So I would assume if __collapse_huge_page_copy failed, pte_unmap() is also required.

Quoted 5 lines

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1240

Quoted 2 lines
mm/khugepaged.c +1 -0
+               result = SCAN_FAIL;

I think a new result is preferred, for example, SCAN_COPY_MC, it would be helpful for debug.

Version 2 will add this new enum.

Quoted 17 lines
Quoted 17 lines
mm/khugepaged.c +6 -3
+               goto out_up_write;
+       }
        /*
         * spin_lock() below is not the equivalent of smp_wmb(), but
         * the smp_wmb() inside __SetPageUptodate() can be reused to
@@ -1364,9 +1424,14 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
        pte_unmap_unlock(pte, ptl);
        if (ret) {
                node = khugepaged_find_target_node();
-               /* collapse_huge_page will return with the mmap_lock released */
-               collapse_huge_page(mm, address, hpage, node,
-                               referenced, unmapped);
+               /*
+                * collapse_huge_page will return with the mmap_r+w_lock released.
+                * It is uncertain if *hpage is NULL or not when collapse_huge_page
+                * returns, so keep ret=1 to jump to breakouterloop_mmap_lock
Quoted 8 lines

mm/khugepaged.c static int khugepaged_scan_pmd(struct mm_struct *mm, · line 1432

Quoted 2 lines
mm/khugepaged.c +2 -0
+                * in khugepaged_scan_mm_slot, then *hpage will be freed
+                * if collapse failed.

It may be not true for !NUMA case, the huge page may be reused, so you need to make sure the huge page is cleared before return, I had a patch that remove the special case for !NUMA
(https://lore.kernel.org/linux-mm/[email protected]/),
it could avoid clearing huge page, I should resurrect it.

Thanks for catching this! Clearing the hugepage before returning is for security reasons
or to avoid memory corruption, right?
If my understanding is correct, once your patch is merged, this will be good. Your patch
seems to be a very simple one, do you expect it to be merged soon?

Quoted 13 lines
Quoted 13 lines
mm/khugepaged.c +5 -0
+                */
+               collapse_huge_page(mm, address, hpage, node, referenced, unmapped);
        }
 out:
        trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
@@ -2168,6 +2233,11 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
                                khugepaged_scan_file(mm, file, pgoff, hpage);
                                fput(file);
                        } else {
+                               /*
+                                * mmap_read_lock is
+                                * 1) released if both scan and collapse succeeded;
Quoted 8 lines
Quoted 8 lines
mm/khugepaged.c +2 -1
+                                * 2) still held if either scan or collapse failed.
+                                */
                                ret = khugepaged_scan_pmd(mm, vma,
                                                khugepaged_scan.address,
                                                hpage);
--

2.35.1.894.gb6a874cedc-goog

Thanks for the quick and valuable feedback.
I will send out the v2 RFC, and please leave/reply there. Thanks!

#4 lore raw

Re: [RFC v1 1/2] mm: khugepaged: recover from poisoned anonymous memory

Quoted 149 lines · Jiaqi Yan
Quoted 29 lines · Yang Shi
Quoted 27 lines · Jiaqi Yan

Make __collapse_huge_page_copy return whether collapsing/copying anonymous pages succeeded, and make collapse_huge_page handle the return status.

Break existing PTE scan loop into two for-loops. The first loop copies source pages into target huge page, and can fail gracefully when running into memory errors in source pages. Roll back the page table and page states in the 2nd loop when copy failed:
1) re-establish the PTEs-to-PMD connection.
2) release pages back to their LRU list.

---
 include/linux/highmem.h |  19 ++++++
 mm/khugepaged.c         | 136 ++++++++++++++++++++++++++++++----------
 2 files changed, 122 insertions(+), 33 deletions(-)
include/linux/highmem.h +0 -0
index 39bb9b47fa9c..15d0aa4d349c 100644
@@ -281,6 +281,25 @@ static inline void copy_user_highpage(struct page *to, struct page *from,
        kunmap_local(vfrom);
 }
 
Quoted 11 lines

include/linux/highmem.h static inline void copy_user_highpage(struct page *to, struct page *from, · line 289

Quoted 6 lines
include/linux/highmem.h +6 -0
+/*
+ * Machine check exception handled version of copy_user_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_user_highpage_mc(struct page *to, struct page *from,
+       unsigned long vaddr, struct vm_area_struct *vma)

Patch #2 defined copy_highpage_mc() which has the same implementation. This function has two unused parameters: vaddr and vma. It seems we just need to keep one.

Version 2 will delete copy_user_highpage_mc() from the patch series, and just keep copy_highpage_mc().

Quoted 94 lines
Quoted 94 lines
include/linux/highmem.h +13 -0
+{
+       char *vfrom, *vto;
+       unsigned long ret;
+
+       vfrom = kmap_local_page(from);
+       vto = kmap_local_page(to);
+       ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+       kunmap_local(vto);
+       kunmap_local(vfrom);
+
+       return ret > 0;
+}
+
 #endif
 
 #ifndef __HAVE_ARCH_COPY_HIGHPAGE
mm/khugepaged.c +48 -17
index 131492fd1148..84ed177f56ff 100644
@@ -739,44 +739,97 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
        return 0;
 }
 
-static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
-                                     struct vm_area_struct *vma,
-                                     unsigned long address,
-                                     spinlock_t *ptl,
-                                     struct list_head *compound_pagelist)
+/*
+ * __collapse_huge_page_copy - attempts to copy memory contents from normal
+ * pages to a hugepage. Cleanup the normal pages if copy succeeds;
+ * otherwise restore the original pmd page table.
+ *
+ * @pte: starting of the PTEs to copy from
+ * @page: the new hugepage to copy contents to
+ * @pmd: pointer to the new hugepage's PMD
+ * @rollback: the original normal PTEs' PMD
+ * @address: starting address to copy
+ * @pte_ptl: lock on normal pages' PTEs
+ * @compound_pagelist: list that stores compound pages
+ */
+static bool __collapse_huge_page_copy(pte_t *pte,
+                               struct page *page,
+                               pmd_t *pmd,
+                               pmd_t rollback,
+                               struct vm_area_struct *vma,
+                               unsigned long address,
+                               spinlock_t *pte_ptl,
+                               struct list_head *compound_pagelist)
 {
        struct page *src_page, *tmp;
        pte_t *_pte;
-       for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
-                               _pte++, page++, address += PAGE_SIZE) {
-               pte_t pteval = *_pte;
+       pte_t pteval;
+       unsigned long _address;
+       spinlock_t *pmd_ptl;
+       bool copy_succeeded = true;
 
-               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+       /*
+        * Copying pages' contents is subject to memory poison at any iteration.
+        */
+       for (_pte = pte, _address = address;
+                       _pte < pte + HPAGE_PMD_NR;
+                       _pte++, page++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval)))
                        clear_user_highpage(page, address);
-                       add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
-                       if (is_zero_pfn(pte_pfn(pteval))) {
-                               /*
-                                * ptl mostly unnecessary.
-                                */
-                               spin_lock(ptl);
-                               ptep_clear(vma->vm_mm, address, _pte);
-                               spin_unlock(ptl);
+               else {
+                       src_page = pte_page(pteval);
+                       if (copy_user_highpage_mc(page, src_page, address, vma)) {
+                               copy_succeeded = false;
+                               break;
+                       }
+               }
+       }
+
+       if (!copy_succeeded) {
+               /*
+                * Copying failed, re-establish the regular PMD that
+                * points to regular page table. Since PTEs are still
+                * isolated and locked, acquiring anon_vma_lock is unnecessary.
Quoted 8 lines

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 798

Quoted 4 lines
mm/khugepaged.c +4 -0
+                */
+               pmd_ptl = pmd_lock(vma->vm_mm, pmd);
+               pmd_populate(vma->vm_mm, pmd, pmd_pgtable(rollback));
+               spin_unlock(pmd_ptl);

I think we could just jump to list_for_each_entry_safe, right? But it seems that list_for_each_entry_safe is not enough, please see the later comments.

Quoted 8 lines

Yes, it is not enough, we need to release_pte_page() for non-compound pages.

If we are going to use jump/goto, we need two for-loops over PTEs, one for the copy
succeeded case, and one for the failure case. What I have right now is combining the two for-loops into one.
I don't like to switch to jump because: 1) goto statements have
bad readability, 2) one for-loop is more concise than two for-loops.

Makes sense to me, it is not worth two loops.

Quoted 95 lines
Quoted 54 lines

mm/khugepaged.c static int __collapse_huge_page_isolate(struct vm_area_struct *vma, · line 805

Quoted 9 lines
mm/khugepaged.c +7 -0
+       }
+
+       for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR;
+                       _pte++, _address += PAGE_SIZE) {
+               pteval = *_pte;
+               if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
+                       if (copy_succeeded) {

With the above jump, you don't need to check if copy is succeeded or not since this is the succeeded path only.

Quoted 41 lines
mm/khugepaged.c +22 -11
+                               add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
+                               if (is_zero_pfn(pte_pfn(pteval))) {
+                                       /*
+                                        * ptl mostly unnecessary.
+                                        */
+                                       spin_lock(pte_ptl);
+                                       pte_clear(vma->vm_mm, _address, _pte);
+                                       spin_unlock(pte_ptl);
+                               }
                        }
                } else {
                        src_page = pte_page(pteval);
-                       copy_user_highpage(page, src_page, address, vma);
                        if (!PageCompound(src_page))
                                release_pte_page(src_page);
-                       /*
-                        * ptl mostly unnecessary, but preempt has to
-                        * be disabled to update the per-cpu stats
-                        * inside page_remove_rmap().
-                        */
-                       spin_lock(ptl);
-                       ptep_clear(vma->vm_mm, address, _pte);
-                       page_remove_rmap(src_page, false);
-                       spin_unlock(ptl);
-                       free_page_and_swap_cache(src_page);
+
+                       if (copy_succeeded) {
+                               /*
+                                * ptl mostly unnecessary, but preempt has to
+                                * be disabled to update the per-cpu stats
+                                * inside page_remove_rmap().
+                                */
+                               spin_lock(pte_ptl);
+                               pte_clear(vma->vm_mm, _address, _pte);
+                               page_remove_rmap(src_page, false);
+                               spin_unlock(pte_ptl);
+                               free_page_and_swap_cache(src_page);
+                       }
                }
        }
 
Quoted 7 lines

mm/khugepaged.c static void __collapse_huge_page_copy(pte_t *pte, struct page *page, · line 839

Quoted 4 lines
mm/khugepaged.c +0 -0
@@ -784,6 +837,8 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
                list_del(&src_page->lru);
                release_pte_page(src_page);
        }

If copy failed you need to unpin the isolated pages and put them back to LRU IIUC.

No matter if the copy failed or succeeded, we need to use a for-loop to unpin isolated pages
and put them back to LRU (i.e. release_pte_page()). In current code, this is a common
operation in the above for-loop.

Quoted 18 lines
Quoted 18 lines
mm/khugepaged.c +3 -0
+
+       return copy_succeeded;
 }
 
 static void khugepaged_alloc_sleep(void)
@@ -1066,6 +1121,7 @@ static void collapse_huge_page(struct mm_struct *mm,
        struct vm_area_struct *vma;
        struct mmu_notifier_range range;
        gfp_t gfp;
+       bool copy_succeeded = false;
 
        VM_BUG_ON(address & ~HPAGE_PMD_MASK);
 
@@ -1177,9 +1233,13 @@ static void collapse_huge_page(struct mm_struct *mm,
         */
        anon_vma_unlock_write(vma->anon_vma);
 
Quoted 8 lines

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1239

Quoted 6 lines
mm/khugepaged.c +3 -2
-       __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
-                       &compound_pagelist);
+       copy_succeeded = __collapse_huge_page_copy(pte, new_page, pmd, _pmd,
+                       vma, address, pte_ptl, &compound_pagelist);
        pte_unmap(pte);
+       if (!copy_succeeded) {

Shall the fail handling be moved before pte_unmap()?

Quoted 9 lines

I don't think so. I think at this point we need to pte_unmap() no matter if the copying
succeeded or not. It is obviously needed if __collapse_huge_page_copy succeeded (because existing code already assumes __collapse_huge_page_copy always succeeded).
For the failure case, my reasoning is based on __collapse_huge_page_isolate. If __collapse_huge_page_isolate failed, we need to pte_unmap() before goto out_up_write. So I would assume if __collapse_huge_page_copy failed, pte_unmap() is also required.

Yeah, make sense.

Quoted 36 lines
Quoted 6 lines

mm/khugepaged.c static void collapse_huge_page(struct mm_struct *mm, · line 1240

Quoted 3 lines
mm/khugepaged.c +1 -0
+               result = SCAN_FAIL;

I think a new result is preferred, for example, SCAN_COPY_MC, it would be helpful for debug.

Version 2 will add this new enum.

Quoted 17 lines
Quoted 17 lines
mm/khugepaged.c +6 -3
+               goto out_up_write;
+       }
        /*
         * spin_lock() below is not the equivalent of smp_wmb(), but
         * the smp_wmb() inside __SetPageUptodate() can be reused to
@@ -1364,9 +1424,14 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
        pte_unmap_unlock(pte, ptl);
        if (ret) {
                node = khugepaged_find_target_node();
-               /* collapse_huge_page will return with the mmap_lock released */
-               collapse_huge_page(mm, address, hpage, node,
-                               referenced, unmapped);
+               /*
+                * collapse_huge_page will return with the mmap_r+w_lock released.
+                * It is uncertain if *hpage is NULL or not when collapse_huge_page
+                * returns, so keep ret=1 to jump to breakouterloop_mmap_lock
Quoted 8 lines

mm/khugepaged.c static int khugepaged_scan_pmd(struct mm_struct *mm, · line 1432

Quoted 2 lines
mm/khugepaged.c +2 -0
+                * in khugepaged_scan_mm_slot, then *hpage will be freed
+                * if collapse failed.

It may be not true for !NUMA case, the huge page may be reused, so you need to make sure the huge page is cleared before return, I had a patch that remove the special case for !NUMA
(https://lore.kernel.org/linux-mm/[email protected]/),
it could avoid clearing huge page, I should resurrect it.

Quoted 3 lines

Thanks for catching this! Clearing the hugepage before returning is for security reasons
or to avoid memory corruption, right?

Yes, but it should be fine since the users typically clear the page before using it, for example, do_anonymous_page() does call alloc_zeroed_user_highpage_movable() to allocate a page, which clears the page before returning. So this should not block you, but remind me to relook my patch.

Quoted 3 lines

If my understanding is correct, once your patch is merged, this will be good. Your patch
seems to be a very simple one, do you expect it to be merged soon?

It still has some unsolved review comments, and I didn't get time to work on it yet.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
mm/khugepaged.c +7 -0
+                */
+               collapse_huge_page(mm, address, hpage, node, referenced, unmapped);
        }
 out:
        trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
@@ -2168,6 +2233,11 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
                                khugepaged_scan_file(mm, file, pgoff, hpage);
                                fput(file);
                        } else {
+                               /*
+                                * mmap_read_lock is
+                                * 1) released if both scan and collapse succeeded;
+                                * 2) still held if either scan or collapse failed.
+                                */
                                ret = khugepaged_scan_pmd(mm, vma,
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
mm/khugepaged.c +0 -1
                                                khugepaged_scan.address,
                                                hpage);
--

2.35.1.894.gb6a874cedc-goog

Thanks for the quick and valuable feedback.
I will send out the v2 RFC, and please leave/reply there. Thanks!

I may not be able to take a deep look at v2 soon, I will be off for the following week. I will take a look at it once I'm back.

author
#5 lore raw

RFC 2/2 mm: khugepaged: recover from poisoned file-backed memory

In reply to Jiaqi Yan

Make collapse_file roll back when copying pages failed. More concretely:
* extract copy operations into a separate loop
* postpone the updates for nr_none until both scan and copy succeeded
* postpone joining small xarray entries until both scan and copy succeeded
* as for update operations to NR_XXX_THPS
* for SHMEM file, postpone until both scan and copy succeeded
* for other file, roll back if scan succeeded but copy failed

---
 include/linux/highmem.h | 18 ++++++++++
 mm/khugepaged.c         | 75 +++++++++++++++++++++++++++--------------
 2 files changed, 67 insertions(+), 26 deletions(-)
include/linux/highmem.h +18 -0
index 15d0aa4d349c..fc5aa221bdb5 100644
@@ -315,6 +315,24 @@ static inline void copy_highpage(struct page *to, struct page *from)
 	kunmap_local(vfrom);
 }
 
+/*
+ * Machine check exception handled version of copy_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_highpage_mc(struct page *to, struct page *from)
+{
+	char *vfrom, *vto;
+	unsigned long ret;
+
+	vfrom = kmap_local_page(from);
+	vto = kmap_local_page(to);
+	ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+	kunmap_local(vto);
+	kunmap_local(vfrom);
+
+	return ret > 0;
+}
+
 #endif
 
 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
mm/khugepaged.c +49 -26

131 lines not on this page · raw message

Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#6 lore raw

Re: [RFC v1 2/2] mm: khugepaged: recover from poisoned file-backed memory

Quoted 51 lines · Jiaqi Yan

Make collapse_file roll back when copying pages failed. More concretely:
* extract copy operations into a separate loop
* postpone the updates for nr_none until both scan and copy succeeded
* postpone joining small xarray entries until both scan and copy succeeded
* as for update operations to NR_XXX_THPS
* for SHMEM file, postpone until both scan and copy succeeded
* for other file, roll back if scan succeeded but copy failed

---
 include/linux/highmem.h | 18 ++++++++++
 mm/khugepaged.c         | 75 +++++++++++++++++++++++++++--------------
 2 files changed, 67 insertions(+), 26 deletions(-)
include/linux/highmem.h +18 -0
index 15d0aa4d349c..fc5aa221bdb5 100644
@@ -315,6 +315,24 @@ static inline void copy_highpage(struct page *to, struct page *from)
        kunmap_local(vfrom);
 }
 
+/*
+ * Machine check exception handled version of copy_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_highpage_mc(struct page *to, struct page *from)
+{
+       char *vfrom, *vto;
+       unsigned long ret;
+
+       vfrom = kmap_local_page(from);
+       vto = kmap_local_page(to);
+       ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+       kunmap_local(vto);
+       kunmap_local(vfrom);
+
+       return ret > 0;
+}
+
 #endif
 
 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
mm/khugepaged.c +0 -0
index 84ed177f56ff..ed2b1cd4bbc6 100644

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1711

Quoted 5 lines
mm/khugepaged.c +1 -1
@@ -1708,12 +1708,13 @@ static void collapse_file(struct mm_struct *mm,
 {
        struct address_space *mapping = file->f_mapping;
        gfp_t gfp;
-       struct page *new_page;
+       struct page *new_page, *page, *tmp;

It seems you removed the "struct page *page" from " if (result == SCAN_SUCCEED)", but keep the "struct page *page" under "for (index = start; index < end; index++)". I think the "struct page *page" in the for loop could be removed too.

Quoted 57 lines
mm/khugepaged.c +7 -18
        pgoff_t index, end = start + HPAGE_PMD_NR;
        LIST_HEAD(pagelist);
        XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
        int nr_none = 0, result = SCAN_SUCCEED;
        bool is_shmem = shmem_file(file);
+       bool copy_failed = false;
        int nr;
 
        VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -1936,9 +1937,7 @@ static void collapse_file(struct mm_struct *mm,
        }
        nr = thp_nr_pages(new_page);
 
-       if (is_shmem)
-               __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
-       else {
+       if (!is_shmem) {
                __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr);
                filemap_nr_thps_inc(mapping);
                /*
@@ -1956,34 +1955,39 @@ static void collapse_file(struct mm_struct *mm,
                }
        }
 
-       if (nr_none) {
-               __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
-               if (is_shmem)
-                       __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
-       }
-
-       /* Join all the small entries into a single multi-index entry */
-       xas_set_order(&xas, start, HPAGE_PMD_ORDER);
-       xas_store(&xas, new_page);
 xa_locked:
        xas_unlock_irq(&xas);
 xa_unlocked:
 
        if (result == SCAN_SUCCEED) {
-               struct page *page, *tmp;
-
                /*
                 * Replacing old pages with new one has succeeded, now we
-                * need to copy the content and free the old pages.
+                * attempt to copy the contents.
                 */
                index = start;
-               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
+               list_for_each_entry(page, &pagelist, lru) {
                        while (index < page->index) {
                                clear_highpage(new_page + (index % HPAGE_PMD_NR));
                                index++;
                        }
-                       copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
-                                       page);
+                       if (copy_highpage_mc(new_page + (page->index % HPAGE_PMD_NR), page)) {
+                               copy_failed = true;
+                               break;

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1985

Quoted 10 lines
mm/khugepaged.c +10 -0
+                       }
+                       index++;
+               }
+               while (!copy_failed && index < end) {
+                       clear_highpage(new_page + (page->index % HPAGE_PMD_NR));
+                       index++;
+               }
+       }
+
+       if (result == SCAN_SUCCEED && !copy_failed) {

I think you could set "result" to SCAN_COPY_MC (as same as the anonymous one), then you could drop !copy_failed and use "result" alone afterwards.

Quoted 49 lines
mm/khugepaged.c +22 -7
+               /*
+                * Copying old pages to huge one has succeeded, now we
+                * need to free the old pages.
+                */
+               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
                        list_del(&page->lru);
                        page->mapping = NULL;
                        page_ref_unfreeze(page, 1);
@@ -1991,12 +1995,20 @@ static void collapse_file(struct mm_struct *mm,
                        ClearPageUnevictable(page);
                        unlock_page(page);
                        put_page(page);
-                       index++;
                }
-               while (index < end) {
-                       clear_highpage(new_page + (index % HPAGE_PMD_NR));
-                       index++;
+
+               xas_lock_irq(&xas);
+               if (is_shmem)
+                       __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
+               if (nr_none) {
+                       __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
+                       if (is_shmem)
+                               __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
                }
+               /* Join all the small entries into a single multi-index entry. */
+               xas_set_order(&xas, start, HPAGE_PMD_ORDER);
+               xas_store(&xas, new_page);
+               xas_unlock_irq(&xas);
 
                SetPageUptodate(new_page);
                page_ref_add(new_page, HPAGE_PMD_NR - 1);
@@ -2012,9 +2024,11 @@ static void collapse_file(struct mm_struct *mm,
 
                khugepaged_pages_collapsed++;
        } else {
-               struct page *page;
-
-               /* Something went wrong: roll back page cache changes */
+               /*
+                * Something went wrong:
+                * either result != SCAN_SUCCEED or copy_failed,
+                * roll back page cache changes
+                */
                xas_lock_irq(&xas);
                mapping->nrpages -= nr_none;
 

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 2069

Quoted 9 lines
mm/khugepaged.c +6 -0
@@ -2047,6 +2061,15 @@ static void collapse_file(struct mm_struct *mm,
                        xas_lock_irq(&xas);
                }
                VM_BUG_ON(nr_none);
+               /*
+                * Undo the updates of thp_nr_pages(new_page) for non-SHMEM file,
+                * which is not updated yet for SHMEM file.
+                * These undos are not needed if result is not SCAN_SUCCEED.
+                */
+               if (!is_shmem && result == SCAN_SUCCEED) {

Handling the error case when "result == SCAN_SUCCEED" looks awkward. With the above fixed (set result to SCAN_COPY_MC) we could avoid the awkwardness.

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 2070

Quoted 1 line
mm/khugepaged.c +1 -0
+                       __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr);

I'm wondering whether we may defer NR_FILE_THPS update just like SHMEM because it has not to be updated in advance so that we have the user visible counters update in the single place. Just filemap_nr_thps needs to be updated in advance since it is used to sync with file open path to truncate huge pages.

Quoted 8 lines
mm/khugepaged.c +2 -1
+                       filemap_nr_thps_dec(mapping);
+               }
                xas_unlock_irq(&xas);
 
                new_page->mapping = NULL;
--

2.35.1.894.gb6a874cedc-goog

author
#7 lore raw

Re: [RFC v1 2/2] mm: khugepaged: recover from poisoned file-backed memory

Quoted 47 lines · Yang Shi
Quoted 45 lines · Jiaqi Yan

Make collapse_file roll back when copying pages failed. More concretely:
* extract copy operations into a separate loop
* postpone the updates for nr_none until both scan and copy succeeded
* postpone joining small xarray entries until both scan and copy succeeded
* as for update operations to NR_XXX_THPS
* for SHMEM file, postpone until both scan and copy succeeded
* for other file, roll back if scan succeeded but copy failed

---
 include/linux/highmem.h | 18 ++++++++++
 mm/khugepaged.c         | 75 +++++++++++++++++++++++++++--------------
 2 files changed, 67 insertions(+), 26 deletions(-)
include/linux/highmem.h +18 -0
index 15d0aa4d349c..fc5aa221bdb5 100644
@@ -315,6 +315,24 @@ static inline void copy_highpage(struct page *to, struct page *from)
        kunmap_local(vfrom);
 }
 
+/*
+ * Machine check exception handled version of copy_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_highpage_mc(struct page *to, struct page *from)
+{
+       char *vfrom, *vto;
+       unsigned long ret;
+
+       vfrom = kmap_local_page(from);
+       vto = kmap_local_page(to);
+       ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+       kunmap_local(vto);
+       kunmap_local(vfrom);
+
+       return ret > 0;
+}
+
 #endif
 
Quoted 16 lines

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1711

Quoted 11 lines
include/linux/highmem.h +0 -0
 static inline void memcpy_page(struct page *dst_page, size_t dst_off,
mm/khugepaged.c +1 -1
index 84ed177f56ff..ed2b1cd4bbc6 100644
@@ -1708,12 +1708,13 @@ static void collapse_file(struct mm_struct *mm,
 {
        struct address_space *mapping = file->f_mapping;
        gfp_t gfp;
-       struct page *new_page;
+       struct page *new_page, *page, *tmp;

It seems you removed the "struct page *page" from " if (result == SCAN_SUCCEED)", but keep the "struct page *page" under "for (index = start; index < end; index++)". I think the "struct page *page" in the for loop could be removed too.

I agree. Removed in the next version.

Quoted 64 lines
Quoted 64 lines
mm/khugepaged.c +13 -18
        pgoff_t index, end = start + HPAGE_PMD_NR;
        LIST_HEAD(pagelist);
        XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
        int nr_none = 0, result = SCAN_SUCCEED;
        bool is_shmem = shmem_file(file);
+       bool copy_failed = false;
        int nr;
 
        VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -1936,9 +1937,7 @@ static void collapse_file(struct mm_struct *mm,
        }
        nr = thp_nr_pages(new_page);
 
-       if (is_shmem)
-               __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
-       else {
+       if (!is_shmem) {
                __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr);
                filemap_nr_thps_inc(mapping);
                /*
@@ -1956,34 +1955,39 @@ static void collapse_file(struct mm_struct *mm,
                }
        }
 
-       if (nr_none) {
-               __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
-               if (is_shmem)
-                       __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
-       }
-
-       /* Join all the small entries into a single multi-index entry */
-       xas_set_order(&xas, start, HPAGE_PMD_ORDER);
-       xas_store(&xas, new_page);
 xa_locked:
        xas_unlock_irq(&xas);
 xa_unlocked:
 
        if (result == SCAN_SUCCEED) {
-               struct page *page, *tmp;
-
                /*
                 * Replacing old pages with new one has succeeded, now we
-                * need to copy the content and free the old pages.
+                * attempt to copy the contents.
                 */
                index = start;
-               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
+               list_for_each_entry(page, &pagelist, lru) {
                        while (index < page->index) {
                                clear_highpage(new_page + (index % HPAGE_PMD_NR));
                                index++;
                        }
-                       copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
-                                       page);
+                       if (copy_highpage_mc(new_page + (page->index % HPAGE_PMD_NR), page)) {
+                               copy_failed = true;
+                               break;
+                       }
+                       index++;
+               }
+               while (!copy_failed && index < end) {
+                       clear_highpage(new_page + (page->index % HPAGE_PMD_NR));
+                       index++;
Quoted 8 lines

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1985

Quoted 4 lines
mm/khugepaged.c +4 -0
+               }
+       }
+
+       if (result == SCAN_SUCCEED && !copy_failed) {

I think you could set "result" to SCAN_COPY_MC (as same as the anonymous one), then you could drop !copy_failed and use "result" alone afterwards.

Yes, copy_failed variable will be eliminated in the next version.

Quoted 49 lines
Quoted 49 lines
mm/khugepaged.c +22 -7
+               /*
+                * Copying old pages to huge one has succeeded, now we
+                * need to free the old pages.
+                */
+               list_for_each_entry_safe(page, tmp, &pagelist, lru) {
                        list_del(&page->lru);
                        page->mapping = NULL;
                        page_ref_unfreeze(page, 1);
@@ -1991,12 +1995,20 @@ static void collapse_file(struct mm_struct *mm,
                        ClearPageUnevictable(page);
                        unlock_page(page);
                        put_page(page);
-                       index++;
                }
-               while (index < end) {
-                       clear_highpage(new_page + (index % HPAGE_PMD_NR));
-                       index++;
+
+               xas_lock_irq(&xas);
+               if (is_shmem)
+                       __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
+               if (nr_none) {
+                       __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
+                       if (is_shmem)
+                               __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
                }
+               /* Join all the small entries into a single multi-index entry. */
+               xas_set_order(&xas, start, HPAGE_PMD_ORDER);
+               xas_store(&xas, new_page);
+               xas_unlock_irq(&xas);
 
                SetPageUptodate(new_page);
                page_ref_add(new_page, HPAGE_PMD_NR - 1);
@@ -2012,9 +2024,11 @@ static void collapse_file(struct mm_struct *mm,
 
                khugepaged_pages_collapsed++;
        } else {
-               struct page *page;
-
-               /* Something went wrong: roll back page cache changes */
+               /*
+                * Something went wrong:
+                * either result != SCAN_SUCCEED or copy_failed,
+                * roll back page cache changes
+                */
                xas_lock_irq(&xas);
                mapping->nrpages -= nr_none;
 
Quoted 15 lines

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 2069

Quoted 10 lines
mm/khugepaged.c +6 -0
@@ -2047,6 +2061,15 @@ static void collapse_file(struct mm_struct *mm,
                        xas_lock_irq(&xas);
                }
                VM_BUG_ON(nr_none);
+               /*
+                * Undo the updates of thp_nr_pages(new_page) for non-SHMEM file,
+                * which is not updated yet for SHMEM file.
+                * These undos are not needed if result is not SCAN_SUCCEED.
+                */
+               if (!is_shmem && result == SCAN_SUCCEED) {

Handling the error case when "result == SCAN_SUCCEED" looks awkward. With the above fixed (set result to SCAN_COPY_MC) we could avoid the awkwardness.

Will look less awkward in the next version :)

Quoted 7 lines

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 2070

Quoted 1 line
mm/khugepaged.c +1 -0
+                       __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr);

I'm wondering whether we may defer NR_FILE_THPS update just like SHMEM because it has not to be updated in advance so that we have the user visible counters update in the single place. Just filemap_nr_thps needs to be updated in advance since it is used to sync with file open path to truncate huge pages.

In version 2, I will postpone __mod_lruvec_page_state() until copying succeeded, and undo filemap_nr_thps_inc if (!is_shmem && result == SCAN_COPY_MC).

Quoted 9 lines
Quoted 9 lines
mm/khugepaged.c +2 -1
+                       filemap_nr_thps_dec(mapping);
+               }
                xas_unlock_irq(&xas);
 
                new_page->mapping = NULL;
--

2.35.1.894.gb6a874cedc-goog

Thanks for the valuable feedback. I will send out the v2 RFC separately, and please leave/reply there. Thanks!

#8 lore raw

Re: [RFC v1 2/2] mm: khugepaged: recover from poisoned file-backed memory

In reply to Jiaqi Yan
Quoted 101 lines · Jiaqi Yan

Make collapse_file roll back when copying pages failed. More concretely:
* extract copy operations into a separate loop
* postpone the updates for nr_none until both scan and copy succeeded
* postpone joining small xarray entries until both scan and copy succeeded
* as for update operations to NR_XXX_THPS
* for SHMEM file, postpone until both scan and copy succeeded
* for other file, roll back if scan succeeded but copy failed

---
  include/linux/highmem.h | 18 ++++++++++
  mm/khugepaged.c         | 75 +++++++++++++++++++++++++++--------------
  2 files changed, 67 insertions(+), 26 deletions(-)
include/linux/highmem.h +18 -0
index 15d0aa4d349c..fc5aa221bdb5 100644
@@ -315,6 +315,24 @@ static inline void copy_highpage(struct page *to, struct page *from)
  	kunmap_local(vfrom);
  }
 
+/*
+ * Machine check exception handled version of copy_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_highpage_mc(struct page *to, struct page *from)
+{
+	char *vfrom, *vto;
+	unsigned long ret;
+
+	vfrom = kmap_local_page(from);
+	vto = kmap_local_page(to);
+	ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+	kunmap_local(vto);
+	kunmap_local(vfrom);
+
+	return ret > 0;
+}
+
  #endif
 
  static inline void memcpy_page(struct page *dst_page, size_t dst_off,
mm/khugepaged.c +4 -16
index 84ed177f56ff..ed2b1cd4bbc6 100644
@@ -1708,12 +1708,13 @@ static void collapse_file(struct mm_struct *mm,
  {
  	struct address_space *mapping = file->f_mapping;
  	gfp_t gfp;
-	struct page *new_page;
+	struct page *new_page, *page, *tmp;
  	pgoff_t index, end = start + HPAGE_PMD_NR;
  	LIST_HEAD(pagelist);
  	XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
  	int nr_none = 0, result = SCAN_SUCCEED;
  	bool is_shmem = shmem_file(file);
+	bool copy_failed = false;
  	int nr;
 
  	VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -1936,9 +1937,7 @@ static void collapse_file(struct mm_struct *mm,
  	}
  	nr = thp_nr_pages(new_page);
 
-	if (is_shmem)
-		__mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
-	else {
+	if (!is_shmem) {
  		__mod_lruvec_page_state(new_page, NR_FILE_THPS, nr);
  		filemap_nr_thps_inc(mapping);
  		/*
@@ -1956,34 +1955,39 @@ static void collapse_file(struct mm_struct *mm,
  		}
  	}
 
-	if (nr_none) {
-		__mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
-		if (is_shmem)
-			__mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
-	}
-
-	/* Join all the small entries into a single multi-index entry */
-	xas_set_order(&xas, start, HPAGE_PMD_ORDER);
-	xas_store(&xas, new_page);
  xa_locked:
  	xas_unlock_irq(&xas);
  xa_unlocked:
 
  	if (result == SCAN_SUCCEED) {
-		struct page *page, *tmp;
-
  		/*
  		 * Replacing old pages with new one has succeeded, now we
-		 * need to copy the content and free the old pages.
+		 * attempt to copy the contents.
  		 */
  		index = start;

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1974

Quoted 10 lines
mm/khugepaged.c +3 -3
-		list_for_each_entry_safe(page, tmp, &pagelist, lru) {
+		list_for_each_entry(page, &pagelist, lru) {
  			while (index < page->index) {
  				clear_highpage(new_page + (index % HPAGE_PMD_NR));
  				index++;
  			}
-			copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
-					page);
+			if (copy_highpage_mc(new_page + (page->index % HPAGE_PMD_NR), page)) {
+				copy_failed = true;

The 1st patch here used "copy_succeed = false", It is best that the logic of the two positions can be unified.

Quoted 75 lines
mm/khugepaged.c +42 -7
+				break;
+			}
+			index++;
+		}
+		while (!copy_failed && index < end) {
+			clear_highpage(new_page + (page->index % HPAGE_PMD_NR));
+			index++;
+		}
+	}
+
+	if (result == SCAN_SUCCEED && !copy_failed) {
+		/*
+		 * Copying old pages to huge one has succeeded, now we
+		 * need to free the old pages.
+		 */
+		list_for_each_entry_safe(page, tmp, &pagelist, lru) {
  			list_del(&page->lru);
  			page->mapping = NULL;
  			page_ref_unfreeze(page, 1);
@@ -1991,12 +1995,20 @@ static void collapse_file(struct mm_struct *mm,
  			ClearPageUnevictable(page);
  			unlock_page(page);
  			put_page(page);
-			index++;
  		}
-		while (index < end) {
-			clear_highpage(new_page + (index % HPAGE_PMD_NR));
-			index++;
+
+		xas_lock_irq(&xas);
+		if (is_shmem)
+			__mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
+		if (nr_none) {
+			__mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
+			if (is_shmem)
+				__mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
  		}
+		/* Join all the small entries into a single multi-index entry. */
+		xas_set_order(&xas, start, HPAGE_PMD_ORDER);
+		xas_store(&xas, new_page);
+		xas_unlock_irq(&xas);
 
  		SetPageUptodate(new_page);
  		page_ref_add(new_page, HPAGE_PMD_NR - 1);
@@ -2012,9 +2024,11 @@ static void collapse_file(struct mm_struct *mm,
 
  		khugepaged_pages_collapsed++;
  	} else {
-		struct page *page;
-
-		/* Something went wrong: roll back page cache changes */
+		/*
+		 * Something went wrong:
+		 * either result != SCAN_SUCCEED or copy_failed,
+		 * roll back page cache changes
+		 */
  		xas_lock_irq(&xas);
  		mapping->nrpages -= nr_none;
 
@@ -2047,6 +2061,15 @@ static void collapse_file(struct mm_struct *mm,
  			xas_lock_irq(&xas);
  		}
  		VM_BUG_ON(nr_none);
+		/*
+		 * Undo the updates of thp_nr_pages(new_page) for non-SHMEM file,
+		 * which is not updated yet for SHMEM file.
+		 * These undos are not needed if result is not SCAN_SUCCEED.
+		 */
+		if (!is_shmem && result == SCAN_SUCCEED) {
+			__mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr);
+			filemap_nr_thps_dec(mapping);
+		}
  		xas_unlock_irq(&xas);
 
  		new_page->mapping = NULL;
author
#9 lore raw

Re: [RFC v1 2/2] mm: khugepaged: recover from poisoned file-backed memory

Quoted 110 lines · Tong Tiangen
Quoted 106 lines · Jiaqi Yan

Make collapse_file roll back when copying pages failed. More concretely:
* extract copy operations into a separate loop
* postpone the updates for nr_none until both scan and copy succeeded
* postpone joining small xarray entries until both scan and copy succeeded
* as for update operations to NR_XXX_THPS
* for SHMEM file, postpone until both scan and copy succeeded
* for other file, roll back if scan succeeded but copy failed

---
  include/linux/highmem.h | 18 ++++++++++
  mm/khugepaged.c         | 75 +++++++++++++++++++++++++++--------------
  2 files changed, 67 insertions(+), 26 deletions(-)
include/linux/highmem.h +18 -0
index 15d0aa4d349c..fc5aa221bdb5 100644
@@ -315,6 +315,24 @@ static inline void copy_highpage(struct page *to, struct page *from)
      kunmap_local(vfrom);
  }
 
+/*
+ * Machine check exception handled version of copy_highpage.
+ * Return true if copying page content failed; otherwise false.
+ */
+static inline bool copy_highpage_mc(struct page *to, struct page *from)
+{
+     char *vfrom, *vto;
+     unsigned long ret;
+
+     vfrom = kmap_local_page(from);
+     vto = kmap_local_page(to);
+     ret = copy_mc_to_kernel(vto, vfrom, PAGE_SIZE);
+     kunmap_local(vto);
+     kunmap_local(vfrom);
+
+     return ret > 0;
+}
+
  #endif
 
  static inline void memcpy_page(struct page *dst_page, size_t dst_off,
mm/khugepaged.c +5 -17
index 84ed177f56ff..ed2b1cd4bbc6 100644
@@ -1708,12 +1708,13 @@ static void collapse_file(struct mm_struct *mm,
  {
      struct address_space *mapping = file->f_mapping;
      gfp_t gfp;
-     struct page *new_page;
+     struct page *new_page, *page, *tmp;
      pgoff_t index, end = start + HPAGE_PMD_NR;
      LIST_HEAD(pagelist);
      XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
      int nr_none = 0, result = SCAN_SUCCEED;
      bool is_shmem = shmem_file(file);
+     bool copy_failed = false;
      int nr;
 
      VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
@@ -1936,9 +1937,7 @@ static void collapse_file(struct mm_struct *mm,
      }
      nr = thp_nr_pages(new_page);
 
-     if (is_shmem)
-             __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
-     else {
+     if (!is_shmem) {
              __mod_lruvec_page_state(new_page, NR_FILE_THPS, nr);
              filemap_nr_thps_inc(mapping);
              /*
@@ -1956,34 +1955,39 @@ static void collapse_file(struct mm_struct *mm,
              }
      }
 
-     if (nr_none) {
-             __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
-             if (is_shmem)
-                     __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
-     }
-
-     /* Join all the small entries into a single multi-index entry */
-     xas_set_order(&xas, start, HPAGE_PMD_ORDER);
-     xas_store(&xas, new_page);
  xa_locked:
      xas_unlock_irq(&xas);
  xa_unlocked:
 
      if (result == SCAN_SUCCEED) {
-             struct page *page, *tmp;
-
              /*
               * Replacing old pages with new one has succeeded, now we
-              * need to copy the content and free the old pages.
+              * attempt to copy the contents.
               */
              index = start;
-             list_for_each_entry_safe(page, tmp, &pagelist, lru) {
+             list_for_each_entry(page, &pagelist, lru) {
                      while (index < page->index) {
                              clear_highpage(new_page + (index % HPAGE_PMD_NR));
                              index++;
Quoted 8 lines

mm/khugepaged.c static void collapse_file(struct mm_struct *mm, · line 1974

Quoted 5 lines
mm/khugepaged.c +2 -2
                      }
-                     copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
-                                     page);
+                     if (copy_highpage_mc(new_page + (page->index % HPAGE_PMD_NR), page)) {
+                             copy_failed = true;

The 1st patch here used "copy_succeed = false", It is best that the logic of the two positions can be unified.

copy_failed here will be eliminated once we have SCAN_COPY_MC defined in version 2. Version 2 also renames "copy_succeeded" in collapse_huge_page() to "copied", mimicking the "isolated" variable for __collapse_huge_page_isolate().

Quoted 76 lines
Quoted 76 lines
mm/khugepaged.c +42 -7
+                             break;
+                     }
+                     index++;
+             }
+             while (!copy_failed && index < end) {
+                     clear_highpage(new_page + (page->index % HPAGE_PMD_NR));
+                     index++;
+             }
+     }
+
+     if (result == SCAN_SUCCEED && !copy_failed) {
+             /*
+              * Copying old pages to huge one has succeeded, now we
+              * need to free the old pages.
+              */
+             list_for_each_entry_safe(page, tmp, &pagelist, lru) {
                      list_del(&page->lru);
                      page->mapping = NULL;
                      page_ref_unfreeze(page, 1);
@@ -1991,12 +1995,20 @@ static void collapse_file(struct mm_struct *mm,
                      ClearPageUnevictable(page);
                      unlock_page(page);
                      put_page(page);
-                     index++;
              }
-             while (index < end) {
-                     clear_highpage(new_page + (index % HPAGE_PMD_NR));
-                     index++;
+
+             xas_lock_irq(&xas);
+             if (is_shmem)
+                     __mod_lruvec_page_state(new_page, NR_SHMEM_THPS, nr);
+             if (nr_none) {
+                     __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
+                     if (is_shmem)
+                             __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
              }
+             /* Join all the small entries into a single multi-index entry. */
+             xas_set_order(&xas, start, HPAGE_PMD_ORDER);
+             xas_store(&xas, new_page);
+             xas_unlock_irq(&xas);
 
              SetPageUptodate(new_page);
              page_ref_add(new_page, HPAGE_PMD_NR - 1);
@@ -2012,9 +2024,11 @@ static void collapse_file(struct mm_struct *mm,
 
              khugepaged_pages_collapsed++;
      } else {
-             struct page *page;
-
-             /* Something went wrong: roll back page cache changes */
+             /*
+              * Something went wrong:
+              * either result != SCAN_SUCCEED or copy_failed,
+              * roll back page cache changes
+              */
              xas_lock_irq(&xas);
              mapping->nrpages -= nr_none;
 
@@ -2047,6 +2061,15 @@ static void collapse_file(struct mm_struct *mm,
                      xas_lock_irq(&xas);
              }
              VM_BUG_ON(nr_none);
+             /*
+              * Undo the updates of thp_nr_pages(new_page) for non-SHMEM file,
+              * which is not updated yet for SHMEM file.
+              * These undos are not needed if result is not SCAN_SUCCEED.
+              */
+             if (!is_shmem && result == SCAN_SUCCEED) {
+                     __mod_lruvec_page_state(new_page, NR_FILE_THPS, -nr);
+                     filemap_nr_thps_dec(mapping);
+             }
              xas_unlock_irq(&xas);
 
              new_page->mapping = NULL;
#10 lore raw
In reply to Jiaqi Yan
Quoted 38 lines · Jiaqi Yan

Problem
=======
Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged.

Impact
======
The main reason we chose to make khugepaged tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving application 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience.

Solution
========
As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed.

Quoted 9 lines

We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages.

Actually I should asked the question in the first place before diving into the implementation details, if uncorrectable memory error happens, kernel will pin the poisoned page and set hwpoison flag, the bumped page refcount would prevent the page from being collapsed IIUC. So I'm wondering why we need this?

Quoted 12 lines

Jiaqi Yan (2):
mm: khugepaged: recover from poisoned anonymous memory
mm: khugepaged: recover from poisoned file-backed memory

 include/linux/highmem.h |  37 +++++++
 mm/khugepaged.c         | 211 +++++++++++++++++++++++++++++-----------
 2 files changed, 189 insertions(+), 59 deletions(-)

--
2.35.1.894.gb6a874cedc-goog

author
#11 lore raw
Quoted 50 lines · Yang Shi
Quoted 38 lines · Jiaqi Yan

Problem
=======
Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged.

Impact
======
The main reason we chose to make khugepaged tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving application 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience.

Solution
========
As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed.

Quoted 9 lines

We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages.

Quoted 4 lines

Actually I should asked the question in the first place before diving into the implementation details, if uncorrectable memory error happens, kernel will pin the poisoned page and set hwpoison flag, the bumped page refcount would prevent the page from being collapsed IIUC.

This patch series is for cases where khugepaged is the first guy that detects the memory errors on these poisoned pages. IOW, the pages are not known to have memory errors when khugepaged collapsing gets to them.
In our observation, this happens frequently when the huge page ratio of the system is relatively low, which is fairly common in cloud VMs.

Quoted 14 lines

So I'm wondering why we need this?

Quoted 12 lines

Jiaqi Yan (2):
mm: khugepaged: recover from poisoned anonymous memory
mm: khugepaged: recover from poisoned file-backed memory

 include/linux/highmem.h |  37 +++++++
 mm/khugepaged.c         | 211 +++++++++++++++++++++++++++++-----------
 2 files changed, 189 insertions(+), 59 deletions(-)

--
2.35.1.894.gb6a874cedc-goog

#12 lore raw
Quoted 57 lines · Jiaqi Yan
Quoted 50 lines · Yang Shi
Quoted 38 lines · Jiaqi Yan

Problem
=======
Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged.

Impact
======
The main reason we chose to make khugepaged tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving application 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience.

Solution
========
As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed.

Quoted 9 lines

We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages.

Quoted 4 lines

Actually I should asked the question in the first place before diving into the implementation details, if uncorrectable memory error happens, kernel will pin the poisoned page and set hwpoison flag, the bumped page refcount would prevent the page from being collapsed IIUC.

Quoted 5 lines

This patch series is for cases where khugepaged is the first guy that detects the memory errors on these poisoned pages. IOW, the pages are not known to have memory errors when khugepaged collapsing gets to them.
In our observation, this happens frequently when the huge page ratio of the system is relatively low, which is fairly common in cloud VMs.

Thanks, this is the very important information that needs to be caught in the 1st patch's commit log.

Quoted 17 lines
Quoted 17 lines

So I'm wondering why we need this?

Quoted 12 lines

Jiaqi Yan (2):
mm: khugepaged: recover from poisoned anonymous memory
mm: khugepaged: recover from poisoned file-backed memory

 include/linux/highmem.h |  37 +++++++
 mm/khugepaged.c         | 211 +++++++++++++++++++++++++++++-----------
 2 files changed, 189 insertions(+), 59 deletions(-)

--
2.35.1.894.gb6a874cedc-goog

author
#13 lore raw
Quoted 59 lines · Yang Shi
Quoted 57 lines · Jiaqi Yan
Quoted 54 lines · Yang Shi
Quoted 38 lines · Jiaqi Yan

Problem
=======
Memory DIMMs are subject to multi-bit flips, i.e. memory errors. As memory size and density increase, the chances of and number of memory errors increase. The increasing size and density of server RAM in the data center and cloud have shown increased uncorrectable memory errors. There are already mechanisms in the kernel to recover from uncorrectable memory errors. This series of patches provides the recovery mechanism for the particular kernel agent khugepaged.

Impact
======
The main reason we chose to make khugepaged tolerant of memory failures was its high possibility of accessing poisoned memory while performing functionally optional compaction actions. Standard applications typically don't have strict requirements on the size of its pages. So they are given 4K pages by the kernel. The kernel is able to improve application performance by either 1) giving application 2M pages to begin with, or 2) collapsing 4K pages into 2M pages when possible. This collapsing operation is done by khugepaged, a kernel agent that is constantly scanning memory. When collapsing 4K pages into a 2M page, it must copy the data from the 4K pages into a physically contiguous 2M page. Therefore, as long as there exists one poisoned cache line in collapsible 4K pages, khugepaged will eventually access it. The current impact to users is a machine check exception triggered kernel panic. However, khugepaged’s compaction operations are not functionally required kernel actions. Therefore making khugepaged tolerant to poisoned memory will greatly improve user experience.

Solution
========
As stated before, it is less desirable to crash the system only because khugepaged accesses poisoned pages while it is collapsing 4K pages. The high level idea of this patch series is to skip the group of pages (usually 512 4K-size pages) once khugepaged finds one of them is poisoned, as these pages have become ineligible to be collapsed.

Quoted 9 lines

We are also careful to unwind operations khuagepaged has performed before it detects memory failures. For example, before copying and collapsing a group of anonymous pages into a huge page, the source pages will be isolated and their page table is unlinked from their PMD. These operations need to be undone in order to ensure these pages are not changed/lost from the perspective of other threads (both user and kernel space). As for file backed memory pages, there already exists a rollback case. This patch just extends it so that khugepaged also correctly rolls back when it fails to copy poisoned 4K pages.

Actually I should asked the question in the first place before diving into the implementation details, if uncorrectable memory error happens, kernel will pin the poisoned page and set hwpoison flag, the bumped page refcount would prevent the page from being collapsed IIUC.

Quoted 8 lines
Quoted 5 lines

This patch series is for cases where khugepaged is the first guy that detects the memory errors on these poisoned pages. IOW, the pages are not known to have memory errors when khugepaged collapsing gets to them.
In our observation, this happens frequently when the huge page ratio of the system is relatively low, which is fairly common in cloud VMs.

Thanks, this is the very important information that needs to be caught in the 1st patch's commit log.

Thanks for this valuable feedback. I will add this in the commit msg of v2, but I will wait for your comments on patch 2/2 before sending out v2.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

So I'm wondering why we need this?

Quoted 12 lines

Jiaqi Yan (2):
mm: khugepaged: recover from poisoned anonymous memory
mm: khugepaged: recover from poisoned file-backed memory

 include/linux/highmem.h |  37 +++++++
 mm/khugepaged.c         | 211 +++++++++++++++++++++++++++++-----------
 2 files changed, 189 insertions(+), 59 deletions(-)

--
2.35.1.894.gb6a874cedc-goog