mm: change huge_ptep_clear_flush() to return the original pte
authorBaolin Wang <baolin.wang@linux.alibaba.com>
Fri, 13 May 2022 23:48:55 +0000 (16:48 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 13 May 2022 23:48:55 +0000 (16:48 -0700)
Patch series "Fix CONT-PTE/PMD size hugetlb issue when unmapping or migrating", v4.

presently, migrating a hugetlb page or unmapping a poisoned hugetlb page,
we'll use ptep_clear_flush() and set_pte_at() to nuke the page table entry
and remap it, and this is incorrect for CONT-PTE or CONT-PMD size hugetlb
page, which will cause potential data consistent issue.  This patch set
will change to use hugetlb related APIs to fix this issue.

Note: Mike pointed out the huge_ptep_get() will only return the one
specific value, and it would not take into account the dirty or young bits
of CONT-PTE/PMDs like the huge_ptep_get_and_clear() [1].  This
inconsistent issue is not introduced by this patch set, and this issue
will be addressed in another thread [2].  Meanwhile the uffd for hugetlb
case [3] pointed out by Gerald also needs another patch to address.

[1] https://lore.kernel.org/linux-mm/85bd80b4-b4fd-0d3f-a2e5-149559f2f387@oracle.com/
[2] https://lore.kernel.org/all/cover.1651998586.git.baolin.wang@linux.alibaba.com/
[3] https://lore.kernel.org/linux-mm/20220503120343.6264e126@thinkpad/

This patch (of 3):

It is incorrect to use ptep_clear_flush() to nuke a hugetlb page table
when unmapping or migrating a hugetlb page, and will change to use
huge_ptep_clear_flush() instead in the following patches.

So this is a preparation patch, which changes the huge_ptep_clear_flush()
to return the original pte to help to nuke a hugetlb page table.

[baolin.wang@linux.alibaba.com: fix build in several more architectures]
Link: https://lkml.kernel.org/r/0009a4cd-2826-e8be-e671-f050d4f18d5d@linux.alibaba.com
[sfr@canb.auug.org.au: fixup]
Link: https://lkml.kernel.org/r/20220511181531.7f27a5c1@canb.auug.org.au
Link: https://lkml.kernel.org/r/cover.1652270205.git.baolin.wang@linux.alibaba.com
Link: https://lkml.kernel.org/r/20f77ddab90baa249bd24504c413189b82acde69.1652270205.git.baolin.wang@linux.alibaba.com
Link: https://lkml.kernel.org/r/cover.1652147571.git.baolin.wang@linux.alibaba.com
Link: https://lkml.kernel.org/r/dcf065868cce35bceaf138613ad27f17bb7c0c19.1652147571.git.baolin.wang@linux.alibaba.com
Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Acked-by: Mike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: Muchun Song <songmuchun@bytedance.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Yoshinori Sato <ysato@users.osdn.me>
Cc: Rich Felker <dalias@libc.org>
Cc: David S. Miller <davem@davemloft.net>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
arch/arm64/include/asm/hugetlb.h
arch/arm64/mm/hugetlbpage.c
arch/ia64/include/asm/hugetlb.h
arch/mips/include/asm/hugetlb.h
arch/parisc/include/asm/hugetlb.h
arch/powerpc/include/asm/hugetlb.h
arch/s390/include/asm/hugetlb.h
arch/sh/include/asm/hugetlb.h
arch/sparc/include/asm/hugetlb.h
include/asm-generic/hugetlb.h

index 1242f71937f80ead96fe32e0cd30c6c30f923c42..616b2ca8b23eb02b688e8f3c7df2c277ed36f4e3 100644 (file)
@@ -39,8 +39,8 @@ extern pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 extern void huge_ptep_set_wrprotect(struct mm_struct *mm,
                                    unsigned long addr, pte_t *ptep);
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-extern void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                 unsigned long addr, pte_t *ptep);
+extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                  unsigned long addr, pte_t *ptep);
 #define __HAVE_ARCH_HUGE_PTE_CLEAR
 extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
                           pte_t *ptep, unsigned long sz);
index cbace1c9e1372450cc708744f874ac705aed7c31..ca8e65c943597600d71da78d23877d933e28db3a 100644 (file)
@@ -486,19 +486,17 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
                set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
 }
 
-void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                          unsigned long addr, pte_t *ptep)
+pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                           unsigned long addr, pte_t *ptep)
 {
        size_t pgsize;
        int ncontig;
 
-       if (!pte_cont(READ_ONCE(*ptep))) {
-               ptep_clear_flush(vma, addr, ptep);
-               return;
-       }
+       if (!pte_cont(READ_ONCE(*ptep)))
+               return ptep_clear_flush(vma, addr, ptep);
 
        ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
-       clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
+       return get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
 }
 
 static int __init hugetlbpage_init(void)
index 7e46ebde8c0c9cc1ea7c71411632c03b6e8b4832..026ead47cd53bd8a764b24eb14b00f0275d7f7b4 100644 (file)
@@ -23,9 +23,10 @@ static inline int is_hugepage_only_range(struct mm_struct *mm,
 #define is_hugepage_only_range is_hugepage_only_range
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
+       return *ptep;
 }
 
 #include <asm-generic/hugetlb.h>
index c2144409c0c40db371a25c1805a85d36df0dedbe..fd69c88085542e370b2a09df004a782fec48729d 100644 (file)
@@ -43,16 +43,19 @@ static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
+       pte_t pte;
+
        /*
         * clear the huge pte entry firstly, so that the other smp threads will
         * not get old pte entry after finishing flush_tlb_page and before
         * setting new huge pte entry
         */
-       huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
+       pte = huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
        flush_tlb_page(vma, addr);
+       return pte;
 }
 
 #define __HAVE_ARCH_HUGE_PTE_NONE
index a69cf9efb0c1c451a21a196610b1d04eec1df7b0..f7f078c2872c439307783fd81961e1b2faf577da 100644 (file)
@@ -28,9 +28,10 @@ static inline int prepare_hugepage_range(struct file *file,
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
+       return *ptep;
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT
index 6a1a1ac5743b8a2ff7a5ea6f8cf63f883b268f21..8a5674fd120d9e8b700cfdaea6fa7b2dc6624bc9 100644 (file)
@@ -43,11 +43,14 @@ static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
-       huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
+       pte_t pte;
+
+       pte = huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
        flush_hugetlb_page(vma, addr);
+       return pte;
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_SET_ACCESS_FLAGS
index 32c3fd6e878eecb8d0d0f9df0db1a4fd6db821aa..f22beda9e6d5cd40b3cebe77077e15e3150622ad 100644 (file)
@@ -50,10 +50,10 @@ static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
                set_pte(ptep, __pte(_SEGMENT_ENTRY_EMPTY));
 }
 
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long address, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long address, pte_t *ptep)
 {
-       huge_ptep_get_and_clear(vma->vm_mm, address, ptep);
+       return huge_ptep_get_and_clear(vma->vm_mm, address, ptep);
 }
 
 static inline int huge_ptep_set_access_flags(struct vm_area_struct *vma,
index ae4de7b8921034baa3dc06a59c5835d856ca38b8..4d3ba39e681c4ff7b93dd9e8f87c47d43fe2b0af 100644 (file)
@@ -21,9 +21,10 @@ static inline int prepare_hugepage_range(struct file *file,
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
+       return *ptep;
 }
 
 static inline void arch_clear_hugepage_flags(struct page *page)
index 53838a173f62652e28dbb8b0b4551448b872da38..0a26cca24232c0811a0bda5c347c9db8dd206fa3 100644 (file)
@@ -21,9 +21,10 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
                              pte_t *ptep);
 
 #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
-                                        unsigned long addr, pte_t *ptep)
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
+                                         unsigned long addr, pte_t *ptep)
 {
+       return *ptep;
 }
 
 #define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT
index 896f341f614d435b06e160dec92eed9f42e9e367..a57d667addd2e2289b1c950caf37b53f0118767f 100644 (file)
@@ -84,10 +84,10 @@ static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 #endif
 
 #ifndef __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH
-static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
+static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
                unsigned long addr, pte_t *ptep)
 {
-       ptep_clear_flush(vma, addr, ptep);
+       return ptep_clear_flush(vma, addr, ptep);
 }
 #endif