Do not lose dirty bit on THP pages
Subsystems (15)
v4 cover Do not lose dirty bit on THP pages
Vlastimil noted that pmdp_invalidate() is not atomic and we can lose dirty and access bits if CPU sets them after pmdp dereference, but before set_pmd_at().
The bug can lead to data loss, but the race window is tiny and I haven't seen any reports that suggested that it happens in reality. So I don't think it worth sending it to stable.
Unfortunately, there's no way to address the issue in a generic way. We need to fix all architectures that support THP one-by-one.
All architectures that have THP supported have to provide atomic pmdp_invalidate() that returns previous value.
If generic implementation of pmdp_invalidate() is used, architecture needs to provide atomic pmdp_estabish().
pmdp_estabish() is not used out-side generic implementation of pmdp_invalidate() so far, but I think this can change in the future.
Aneesh Kumar K.V (2):
powerpc/mm: update pmdp_invalidate to return old pmd value mm/thp: Remove pmd_huge_split_prepare
Catalin Marinas (1):
arm64: Provide pmdp_establish() helper
Kirill A. Shutemov (7):
asm-generic: Provide generic_pmdp_establish()
arc: Use generic_pmdp_establish as pmdp_establish arm/mm: Provide pmdp_establish() helper
mips: Use generic_pmdp_establish as pmdp_establish x86/mm: Provide pmdp_establish() helper
mm: Do not lose dirty and access bits in pmdp_invalidate()
mm: Use updated pmdp_invalidate() interface to track dirty/accessed
bits
Martin Schwidefsky (1):
s390/mm: Modify pmdp_invalidate to return old value.
Nitin Gupta (1):
sparc64: Update pmdp_invalidate() to return old pmd value
arch/arc/include/asm/hugepage.h | 3 + arch/arm/include/asm/pgtable-3level.h | 3 + arch/arm64/include/asm/pgtable.h | 7 +++ arch/mips/include/asm/pgtable.h | 3 + arch/powerpc/include/asm/book3s/64/hash-4k.h | 2 - arch/powerpc/include/asm/book3s/64/hash-64k.h | 2 - arch/powerpc/include/asm/book3s/64/pgtable.h | 13 +---- arch/powerpc/include/asm/book3s/64/radix.h | 6 -- arch/powerpc/mm/pgtable-book3s64.c | 7 ++- arch/powerpc/mm/pgtable-hash64.c | 22 ------- arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/include/asm/pgtable_64.h | 2 +- arch/sparc/mm/tlb.c | 23 ++++++-- arch/x86/include/asm/pgtable-3level.h | 37 +++++++++++- arch/x86/include/asm/pgtable.h | 15 +++++ fs/proc/task_mmu.c | 8 +-- include/asm-generic/pgtable.h | 25 +++++--- mm/huge_memory.c | 83 ++++++++++++--------------- mm/pgtable-generic.c | 6 +- 19 files changed, 156 insertions(+), 115 deletions(-)
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 1/12 asm-generic: Provide generic_pmdp_establish()
This is an implementation of pmdp_establish() that is only suitable for an architecture that doesn't have hardware dirty/accessed bits. In this case we can't race with CPU which sets these bits and non-atomic approach is fine.
--- include/asm-generic/pgtable.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
include/asm-generic/pgtable.h +15 -0
@@ -309,6 +309,21 @@ extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,extern pgtable_t pgtable_trans_huge_withdraw (struct mm_struct *mm,pmd_t *pmdp);#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +/* +* This is an implementation of pmdp_establish() that is only suitable for an +* architecture that doesn't have hardware dirty/accessed bits. In this case we +* can't race with CPU which sets these bits and non-atomic aproach is fine. +*/ +static inline pmd_t generic_pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old_pmd = *pmdp; +set_pmd_at (vma->vm_mm, address, pmdp, pmd); +return old_pmd; +} +#endif +#ifndef __HAVE_ARCH_PMDP_INVALIDATEextern void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp);
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 2/12 arc: Use generic_pmdp_establish as pmdp_establish
In reply to Kirill A. ShutemovARC doesn't support hardware dirty/accessed bits. generic_pmdp_establish() is suitable in this case.
--- arch/arc/include/asm/hugepage.h | 3 +++ 1 file changed, 3 insertions(+)
arch/arc/include/asm/hugepage.h +3 -0
@@ -74,4 +74,7 @@ extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);extern void flush_pmd_tlb_range (struct vm_area_struct *vma,unsigned long start,unsigned long end); +/* We don't have hardware dirty/accessed bits, generic_pmdp_establish is fine.*/ +#define pmdp_establish generic_pmdp_establish +#endif
Signature · 1 line
2.15.0
PATCH v4 3/12 arm/mm: Provide pmdp_establish() helper
In reply to Kirill A. ShutemovARM LPAE doesn't have hardware dirty/accessed bits.
generic_pmdp_establish() is the right implementation of pmdp_establish for this case.
--- arch/arm/include/asm/pgtable-3level.h | 3 +++ 1 file changed, 3 insertions(+)
arch/arm/include/asm/pgtable-3level.h +3 -0
@@ -249,6 +249,9 @@ PMD_BIT_FUNC(mkyoung, |= PMD_SECT_AF);#define pfn_pmd (pfn,prot) (__pmd(((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)))#define mk_pmd (page,prot) pfn_pmd(page_to_pfn(page),prot) +/* No hardware dirty/accessed bits -- generic_pmdp_establish() fits */ +#define pmdp_establish generic_pmdp_establish +/* represent a notpresent pmd by faulting entry, this is used by pmdp_invalidate */ static inline pmd_t pmd_mknotpresent (pmd_t pmd) {
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 4/12 arm64: Provide pmdp_establish() helper
In reply to Kirill A. ShutemovFrom: Catalin Marinas <[email protected]>
We need an atomic way to setup pmd page table entry, avoiding races with CPU setting dirty/accessed bits. This is required to implement pmdp_invalidate() that doesn't lose these bits.
--- arch/arm64/include/asm/pgtable.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/arm64/include/asm/pgtable.h +7 -0
@@ -676,6 +676,13 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm, {ptep_set_wrprotect (mm, address, (pte_t *)pmdp); } + +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +return __pmd (xchg_relaxed (&pmd_val (*pmdp),pmd_val (pmd))); +}#endif extern pgd_t swapper_pg_dir[PTRS_PER_PGD ];
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 5/12 mips: Use generic_pmdp_establish as pmdp_establish
In reply to Kirill A. ShutemovMIPS doesn't support hardware dirty/accessed bits. generic_pmdp_establish() is suitable in this case.
--- arch/mips/include/asm/pgtable.h | 3 +++ 1 file changed, 3 insertions(+)
arch/mips/include/asm/pgtable.h +3 -0
@@ -534,6 +534,9 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma,#ifdef CONFIG_TRANSPARENT_HUGEPAGE +/* We don't have hardware dirty/accessed bits, generic_pmdp_establish is fine.*/ +#define pmdp_establish generic_pmdp_establish +#define has_transparent_hugepage has_transparent_hugepageextern int has_transparent_hugepage (void );
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 6/12 powerpc/mm: update pmdp_invalidate to return old pmd value
In reply to Kirill A. ShutemovFrom: "Aneesh Kumar K.V" <[email protected]>
It's required to avoid losing dirty and accessed bits.
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 ++-- arch/powerpc/mm/pgtable-book3s64.c | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +2 -2
@@ -1137,8 +1137,8 @@ static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, }#define __HAVE_ARCH_PMDP_INVALIDATE -extern void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, -pmd_t *pmdp); +extern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +pmd_t *pmdp);#define __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE static inline void pmdp_huge_split_prepare(struct vm_area_struct *vma,
arch/powerpc/mm/pgtable-book3s64.c +5 -2
@@ -90,16 +90,19 @@ void serialize_against_pte_lookup(struct mm_struct *mm)* We use this to invalidate a pmdp entry before switching from a * hugepte to regular pmd entry. */ -void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { -pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT,0 ); +unsigned long old_pmd; + + old_pmd =pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT,0 );flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );/* * This ensures that generic code that rely on IRQ disabling * to prevent a parallel THP split work as expected. */ serialize_against_pte_lookup (vma->vm_mm); +return __pmd (old_pmd); }static pmd_t pmd_set_protbits (pmd_t pmd,pgprot_t pgprot)
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 7/12 s390/mm: Modify pmdp_invalidate to return old value.
In reply to Kirill A. ShutemovFrom: Martin Schwidefsky <[email protected]>
It's required to avoid losing dirty and accessed bits.
--- arch/s390/include/asm/pgtable.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/include/asm/pgtable.h +2 -2
@@ -1511,12 +1511,12 @@ static inline pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, }#define __HAVE_ARCH_PMDP_INVALIDATE -static inline void pmdp_invalidate (struct vm_area_struct *vma, +static inline pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmdp) {pmd_t pmd =__pmd (pmd_val (*pmdp) | _SEGMENT_ENTRY_INVALID); -pmdp_xchg_direct (vma->vm_mm, addr, pmdp, pmd); +return pmdp_xchg_direct (vma->vm_mm, addr, pmdp, pmd); }#define __HAVE_ARCH_PMDP_SET_WRPROTECT
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 8/12 sparc64: Update pmdp_invalidate() to return old pmd value
In reply to Kirill A. ShutemovFrom: Nitin Gupta <[email protected]>
It's required to avoid losing dirty and accessed bits.
--- arch/sparc/include/asm/pgtable_64.h | 2 +- arch/sparc/mm/tlb.c | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-)
arch/sparc/include/asm/pgtable_64.h +1 -1
@@ -1010,7 +1010,7 @@ void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd);#define __HAVE_ARCH_PMDP_INVALIDATE -extern void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +extern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp);#define __HAVE_ARCH_PGTABLE_DEPOSIT
arch/sparc/mm/tlb.c +18 -5
@@ -219,17 +219,28 @@ void set_pmd_at(struct mm_struct *mm, unsigned long addr, } } +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old; + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd); + +return old; +} +/* * This routine is only called when splitting a THP */ -void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { -pmd_t entry = *pmdp; - -pmd_val (entry) &= ~_PAGE_VALID; +pmd_t old, entry; -set_pmd_at (vma->vm_mm, address, pmdp, entry); + entry =__pmd (pmd_val (*pmdp) & ~_PAGE_VALID); + old =pmdp_establish (vma, address, pmdp, entry);flush_tlb_range (vma, address, address +HPAGE_PMD_SIZE );/*
@@ -240,6 +251,8 @@ void pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,if ((pmd_val (entry) & _PAGE_PMD_HUGE) && !is_huge_zero_page (pmd_page (entry))) (vma->vm_mm)->context.thp_pte_count--; + +return old; } void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCHv4 08/12] sparc64: Update pmdp_invalidate() to return old pmd value
Quoted 26 lines · On Wed, 13 Dec 2017 13:57:52 +0300 "Kirill A. Shutemov"…
From: Nitin Gupta <[email protected]>
It's required to avoid losing dirty and accessed bits.
...
arch/sparc/include/asm/pgtable_64.h +1
-1@@ -1010,7 +1010,7 @@ void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd);#define __HAVE_ARCH_PMDP_INVALIDATE -extern void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +extern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp);#define __HAVE_ARCH_PGTABLE_DEPOSITarch/sparc/mm/tlb.c +1
-0@@ -219,17 +219,28 @@ void set_pmd_at(struct mm_struct *mm, unsigned long addr, } } +static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
arch/sparc/mm/tlb.c void set_pmd_at(struct mm_struct *mm, unsigned long addr, · line 232
Quoted 10 lines
arch/sparc/mm/tlb.c +10
-0+ unsigned long address, pmd_t *pmdp, pmd_t pmd) +{ + pmd_t old; + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd); + +return old; +}
um, I think I'll put a "do" in there...
And I'll wait until we see a "tested-by" or a nice ack, please.
PATCH v4 9/12 x86/mm: Provide pmdp_establish() helper
In reply to Kirill A. ShutemovWe need an atomic way to setup pmd page table entry, avoiding races with CPU setting dirty/accessed bits. This is required to implement pmdp_invalidate() that doesn't lose these bits.
On PAE we can avoid expensive cmpxchg8b for cases when new page table entry is not present. If it's present, fallback to cpmxchg loop.
--- arch/x86/include/asm/pgtable-3level.h | 37 ++++++++++++++++++++++++++++++++++- arch/x86/include/asm/pgtable.h | 15 ++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable-3level.h +36 -1
@@ -158,7 +158,6 @@ static inline pte_t native_ptep_get_and_clear(pte_t *ptep)#define native_ptep_get_and_clear (xp) native_local_ptep_get_and_clear(xp)#endif -#ifdef CONFIG_SMP union split_pmd { struct { u32 pmd_low;
@@ -166,6 +165,8 @@ union split_pmd { };pmd_t pmd; }; + +#ifdef CONFIG_SMP static inline pmd_t native_pmdp_get_and_clear (pmd_t *pmdp) {union split_pmd res, *orig = (union split_pmd *)pmdp;
@@ -181,6 +182,40 @@ static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)#define native_pmdp_get_and_clear (xp) native_local_pmdp_get_and_clear(xp)#endif +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old; + +/* +* If pmd has present bit cleared we can get away without expensive +* cmpxchg64: we can update pmdp half-by-half without racing with +* anybody. +*/ +if (!(pmd_val (pmd) & _PAGE_PRESENT)) { +union split_pmd old, new, *ptr; + + ptr = (union split_pmd *)pmdp; + + new.pmd = pmd; + +/* xchg acts as a barrier before setting of the high bits */ + old.pmd_low =xchg (&ptr->pmd_low, new.pmd_low); + old.pmd_high = ptr->pmd_high; + ptr->pmd_high = new.pmd_high; +return old.pmd; + } + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd); + +return old; +} +#endif + #ifdef CONFIG_SMP union split_pud { struct {
arch/x86/include/asm/pgtable.h +15 -0
@@ -1094,6 +1094,21 @@ static inline int pud_write(pud_t pud)return pud_flags (pud) & _PAGE_RW; } +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +if (IS_ENABLED (CONFIG_SMP )) { +return xchg (pmdp, pmd); + }else { +pmd_t old = *pmdp; + *pmdp = pmd; +return old; + } +} +#endif +/* * clone_pgd_range(pgd_t *dst, pgd_t *src, int count); *
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerRe: [PATCHv4 09/12] x86/mm: Provide pmdp_establish() helper
Quoted 51 lines · On Wed, 13 Dec 2017 13:57:53 +0300 "Kirill A. Shutemov"…
We need an atomic way to setup pmd page table entry, avoiding races with CPU setting dirty/accessed bits. This is required to implement
pmdp_invalidate()that doesn't lose these bits.On PAE we can avoid expensive cmpxchg8b for cases when new page table entry is not present. If it's present, fallback to cpmxchg loop.
...
arch/x86/include/asm/pgtable-3level.h +21
-1@@ -158,7 +158,6 @@ static inline pte_t native_ptep_get_and_clear(pte_t *ptep)#define native_ptep_get_and_clear (xp) native_local_ptep_get_and_clear(xp)#endif -#ifdef CONFIG_SMP union split_pmd { struct { u32 pmd_low;@@ -166,6 +165,8 @@ union split_pmd { };pmd_t pmd; }; + +#ifdef CONFIG_SMP static inline pmd_t native_pmdp_get_and_clear (pmd_t *pmdp) {union split_pmd res, *orig = (union split_pmd *)pmdp;@@ -181,6 +182,40 @@ static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)#define native_pmdp_get_and_clear (xp) native_local_pmdp_get_and_clear(xp)#endif +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old; + +/* +* If pmd has present bit cleared we can get away without expensive +* cmpxchg64: we can update pmdp half-by-half without racing with +* anybody. +*/ +if (!(pmd_val (pmd) & _PAGE_PRESENT)) { +union split_pmd old, new, *ptr; + + ptr = (union split_pmd *)pmdp; + + new.pmd = pmd; +
arch/x86/include/asm/pgtable-3level.h static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp) · line 213
Quoted 10 lines
arch/x86/include/asm/pgtable-3level.h +10
-0+/* xchg acts as a barrier before setting of the high bits */ + old.pmd_low =xchg (&ptr->pmd_low, new.pmd_low); + old.pmd_high = ptr->pmd_high; + ptr->pmd_high = new.pmd_high; +return old.pmd; + } + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);
um, what happened here?
Quoted 3 lines
arch/x86/include/asm/pgtable-3level.h +3
-0+ return old; +} +#endif
Re: [PATCHv4 09/12] x86/mm: Provide pmdp_establish() helper
Quoted 27 lines · Andrew Morton
Quoted 27 lines
arch/x86/include/asm/pgtable-3level.h +23
-0@@ -181,6 +182,40 @@ static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)#define native_pmdp_get_and_clear (xp) native_local_pmdp_get_and_clear(xp)#endif +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old; + +/* +* If pmd has present bit cleared we can get away without expensive +* cmpxchg64: we can update pmdp half-by-half without racing with +* anybody. +*/ +if (!(pmd_val (pmd) & _PAGE_PRESENT)) { +union split_pmd old, new, *ptr; + + ptr = (union split_pmd *)pmdp; + + new.pmd = pmd; + +/* xchg acts as a barrier before setting of the high bits */ + old.pmd_low =xchg (&ptr->pmd_low, new.pmd_low); + old.pmd_high = ptr->pmd_high; + ptr->pmd_high = new.pmd_high;
Quoted 8 lines
arch/x86/include/asm/pgtable-3level.h static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp) · line 213
Quoted 6 lines
arch/x86/include/asm/pgtable-3level.h +6
-0+return old.pmd; + } + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);um, what happened here?
Ouch.. Yeah, we need 'do' here. :-/
Apparently, it's a valid C code that would run the body once and it worked for me because I didn't hit the race condition.
Signature · 1 line
Kirill A. Shutemov
Andrew Morton
maintainerRe: [PATCHv4 09/12] x86/mm: Provide pmdp_establish() helper
Quoted 37 lines · On Thu, 14 Dec 2017 03:33:18 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 27 lines · Andrew Morton
Quoted 27 lines
arch/x86/include/asm/pgtable-3level.h +23
-0@@ -181,6 +182,40 @@ static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp)#define native_pmdp_get_and_clear (xp) native_local_pmdp_get_and_clear(xp)#endif +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish (struct vm_area_struct *vma, +unsigned long address,pmd_t *pmdp,pmd_t pmd) +{ +pmd_t old; + +/* +* If pmd has present bit cleared we can get away without expensive +* cmpxchg64: we can update pmdp half-by-half without racing with +* anybody. +*/ +if (!(pmd_val (pmd) & _PAGE_PRESENT)) { +union split_pmd old, new, *ptr; + + ptr = (union split_pmd *)pmdp; + + new.pmd = pmd; + +/* xchg acts as a barrier before setting of the high bits */ + old.pmd_low =xchg (&ptr->pmd_low, new.pmd_low); + old.pmd_high = ptr->pmd_high; + ptr->pmd_high = new.pmd_high;Quoted 8 lines
arch/x86/include/asm/pgtable-3level.h static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp) · line 213
Quoted 6 lines
arch/x86/include/asm/pgtable-3level.h +6
-0+return old.pmd; + } + + { + old = *pmdp; + }while (cmpxchg64 (&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);um, what happened here?
Quoted 4 lines
Ouch.. Yeah, we need 'do' here. :-/
Apparently, it's a valid C code that would run the body once and it worked for me because I didn't hit the race condition.
So how the heck do we test this? Add an artificial delay on the other side to open the race window?
Re: [PATCHv4 09/12] x86/mm: Provide pmdp_establish() helper
Quoted 2 lines · Andrew Morton
So how the heck do we test this? Add an artificial delay on the other side to open the race window?
I'll look tomorrow how we can provide proper testing for the corner case.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCHv4 09/12] x86/mm: Provide pmdp_establish() helper
In reply to Andrew MortonQuoted 39 lines · Andrew Morton
Quoted 37 lines · On Thu, 14 Dec 2017 03:33:18 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 35 lines · Andrew Morton
Quoted 23 lines
(quoted diff) +19
-0@@ -181,6 +182,40 @@ static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp) #define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp) #endif +#ifndef pmdp_establish +#define pmdp_establish pmdp_establish +static inline pmd_t pmdp_establish(struct vm_area_struct *vma, + unsigned long address, pmd_t *pmdp, pmd_t pmd) +{ + pmd_t old; + + /* + * If pmd has present bit cleared we can get away without expensive + * cmpxchg64: we can update pmdp half-by-half without racing with + * anybody. + */ + if (!(pmd_val(pmd) & _PAGE_PRESENT)) { + union split_pmd old, new, *ptr; + + ptr = (union split_pmd *)pmdp; + + new.pmd = pmd; +Review static inline pmd_t native_pmdp_get_and_clear(pmd_t *pmdp) · line 213
Quoted 10 lines
(quoted diff) +10
-0+ /* xchg acts as a barrier before setting of the high bits */ + old.pmd_low = xchg(&ptr->pmd_low, new.pmd_low); + old.pmd_high = ptr->pmd_high; + ptr->pmd_high = new.pmd_high; + return old.pmd; + } + + { + old = *pmdp; + } while (cmpxchg64(&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);um, what happened here?
Quoted 7 lines
Quoted 4 lines
Ouch.. Yeah, we need 'do' here. :-/
Apparently, it's a valid C code that would run the body once and it worked for me because I didn't hit the race condition.
So how the heck do we test this? Add an artificial delay on the other side to open the race window?
Okay, here's a testcase I've come up with:
#include <pthread.h>
#include <sys/mman.h>
#define MAP_BASE ((void *)0x200000)
#define MAP_SIZE (0x200000)
void *thread(void *p)
{
*(char *)p = 1;
return NULL;
}
int main(void)
{
pthread_t t;
char *p;
p = mmap(MAP_BASE, MAP_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
/* Allocate THP */
*p = 1;
/* Make page and PMD clean */
madvise(p, MAP_SIZE, MADV_FREE);
/* New thread would make the PMD dirty again */
pthread_create(&t, NULL, thread, p);
/* Trigger split_huge_pmd(). It may lose dirty bit. */
mprotect(p, 4096, PROT_READ | PROT_WRITE | PROT_EXEC);
/*
* Wait thread to complete.
*
* Page or PTE by MAP_BASE address *must* be dirty here. If it's not we
* can lose data if the page is reclaimed.
*/
pthread_join(t, NULL);
return 0;
}To make the bug triggered, I had to make race window larger. See patch below.
I also check if the page is dirty on exit on kernel side. It's just easier than make the page reclaimed from userspace and check if data is still there.
With this patch, the testcase can trigger the race condition within 10 or so runs in my KVM box.
mm/huge_memory.c +2 -0
@@ -33,6 +33,7 @@#include <linux/page_idle.h> #include <linux/shmem_fs.h> #include <linux/oom.h> +#include <linux/delay.h> #include <asm/tlb.h> #include <asm/pgalloc.h>
@@ -2131,6 +2132,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, write =pmd_write (*pmd); young =pmd_young (*pmd); dirty =pmd_dirty (*pmd); +mdelay (100 ); soft_dirty =pmd_soft_dirty (*pmd);pmdp_huge_split_prepare (vma, haddr, pmd);
mm/memory.c +4 -0
@@ -1305,6 +1305,10 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,struct page *page; page =_vm_normal_page (vma, addr, ptent, true); + +if (addr ==0x200000 ) +BUG_ON (!(PageDirty (page) ||pte_dirty (*pte))); +if (unlikely (details) && page) {/* * unmap_shared_mapping_pages() wants to
Signature · 1 line
Kirill A. Shutemov
PATCH v4 10/12 mm: Do not lose dirty and access bits in pmdp_invalidate()
In reply to Kirill A. ShutemovVlastimil noted that pmdp_invalidate() is not atomic and we can lose dirty and access bits if CPU sets them after pmdp dereference, but before set_pmd_at().
The patch change pmdp_invalidate() to make the entry non-present atomically and return previous value of the entry. This value can be used to check if CPU set dirty/accessed bits under us.
The race window is very small and I haven't seen any reports that can be attributed to the bug. For this reason, I don't think backporting to stable trees needed.
--- include/asm-generic/pgtable.h | 2 +- mm/pgtable-generic.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/asm-generic/pgtable.h +1 -1
@@ -325,7 +325,7 @@ static inline pmd_t generic_pmdp_establish(struct vm_area_struct *vma,#endif #ifndef __HAVE_ARCH_PMDP_INVALIDATE -extern void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +extern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp);#endif
mm/pgtable-generic.c +3 -3
@@ -181,12 +181,12 @@ pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)#endif #ifndef __HAVE_ARCH_PMDP_INVALIDATE -void pmdp_invalidate (struct vm_area_struct *vma,unsigned long address, +pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { -pmd_t entry = *pmdp; -set_pmd_at (vma->vm_mm, address, pmdp,pmd_mknotpresent (entry)); +pmd_t old =pmdp_establish (vma, address, pmdp,pmd_mknotpresent (*pmdp));flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE ); +return old; }#endif
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 11/12 mm: Use updated pmdp_invalidate() interface to track dirty/accessed bits
In reply to Kirill A. ShutemovThis patch uses modifed pmdp_invalidate(), that return previous value of pmd, to transfer dirty and accessed bits.
--- fs/proc/task_mmu.c | 8 ++++---- mm/huge_memory.c | 29 ++++++++++++----------------- 2 files changed, 16 insertions(+), 21 deletions(-)
fs/proc/task_mmu.c +4 -4
@@ -977,14 +977,14 @@ static inline void clear_soft_dirty(struct vm_area_struct *vma,static inline void clear_soft_dirty_pmd (struct vm_area_struct *vma,unsigned long addr,pmd_t *pmdp) { -pmd_t pmd = *pmdp; +pmd_t old, pmd = *pmdp;if (pmd_present (pmd)) {/* See comment in change_huge_pmd() */ -pmdp_invalidate (vma, addr, pmdp); -if (pmd_dirty (*pmdp)) + old =pmdp_invalidate (vma, addr, pmdp); +if (pmd_dirty (old)) pmd =pmd_mkdirty (pmd); -if (pmd_young (*pmdp)) +if (pmd_young (old)) pmd =pmd_mkyoung (pmd); pmd =pmd_wrprotect (pmd);
mm/huge_memory.c +12 -17
@@ -1910,17 +1910,7 @@ int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,* pmdp_invalidate() is required to make sure we don't miss * dirty/young flags set by hardware. */ - entry = *pmd; -pmdp_invalidate (vma, addr, pmd); - -/* -* Recover dirty/young flags. It relies on pmdp_invalidate to not -* corrupt them. -*/ -if (pmd_dirty (*pmd)) - entry =pmd_mkdirty (entry); -if (pmd_young (*pmd)) - entry =pmd_mkyoung (entry); + entry =pmdp_invalidate (vma, addr, pmd); entry =pmd_modify (entry, newprot);if (preserve_write)
@@ -2073,8 +2063,8 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct mm_struct *mm = vma->vm_mm;struct page *page;pgtable_t pgtable; -pmd_t _pmd; -bool young, write, dirty, soft_dirty, pmd_migration = false; +pmd_t old, _pmd; +bool young, write, soft_dirty, pmd_migration = false;unsigned long addr;int i;
@@ -2130,7 +2120,6 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,page_ref_add (page,HPAGE_PMD_NR -1 ); write =pmd_write (*pmd); young =pmd_young (*pmd); - dirty =pmd_dirty (*pmd); soft_dirty =pmd_soft_dirty (*pmd);pmdp_huge_split_prepare (vma, haddr, pmd);
@@ -2160,8 +2149,6 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,if (soft_dirty) entry =pte_mksoft_dirty (entry); } -if (dirty) -SetPageDirty (page + i); pte =pte_offset_map (&_pmd, addr);BUG_ON (!pte_none (*pte));set_pte_at (mm, addr, pte, entry);
@@ -2210,7 +2197,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* and finally we write the non-huge version of the pmd entry with * pmd_populate. */ -pmdp_invalidate (vma, haddr, pmd); + old =pmdp_invalidate (vma, haddr, pmd); + +/* +* Transfer dirty bit using value returned by pmd_invalidate() to be +* sure we don't race with CPU that can set the bit under us. +*/ +if (pmd_dirty (old)) +SetPageDirty (page); +pmd_populate (mm, pmd, pgtable);if (freeze) {
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v4 12/12 mm/thp: Remove pmd_huge_split_prepare
In reply to Kirill A. ShutemovFrom: "Aneesh Kumar K.V" <[email protected]>
Instead of marking the pmd ready for split, invalidate the pmd. This should take care of powerpc requirement. Only side effect is that we mark the pmd invalid early. This can result in us blocking access to the page a bit longer if we race against a thp split.
--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 2 - arch/powerpc/include/asm/book3s/64/hash-64k.h | 2 - arch/powerpc/include/asm/book3s/64/pgtable.h | 9 ---- arch/powerpc/include/asm/book3s/64/radix.h | 6 --- arch/powerpc/mm/pgtable-hash64.c | 22 -------- include/asm-generic/pgtable.h | 8 --- mm/huge_memory.c | 74 +++++++++++++-------------- 7 files changed, 36 insertions(+), 87 deletions(-)
arch/powerpc/include/asm/book3s/64/hash-4k.h +0 -2
@@ -101,8 +101,6 @@ extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma,extern void hash__pgtable_trans_huge_deposit (struct mm_struct *mm,pmd_t *pmdp,pgtable_t pgtable);extern pgtable_t hash__pgtable_trans_huge_withdraw (struct mm_struct *mm,pmd_t *pmdp); -extern void hash__pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp);extern pmd_t hash__pmdp_huge_get_and_clear (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp);extern int hash__has_transparent_hugepage (void );
arch/powerpc/include/asm/book3s/64/hash-64k.h +0 -2
@@ -203,8 +203,6 @@ extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma,extern void hash__pgtable_trans_huge_deposit (struct mm_struct *mm,pmd_t *pmdp,pgtable_t pgtable);extern pgtable_t hash__pgtable_trans_huge_withdraw (struct mm_struct *mm,pmd_t *pmdp); -extern void hash__pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp);extern pmd_t hash__pmdp_huge_get_and_clear (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp);extern int hash__has_transparent_hugepage (void );
arch/powerpc/include/asm/book3s/64/pgtable.h +0 -9
@@ -1140,15 +1140,6 @@ static inline pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm,extern pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp); -#define __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE -static inline void pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp) -{ -if (radix_enabled ()) -return radix__pmdp_huge_split_prepare (vma, address, pmdp); -return hash__pmdp_huge_split_prepare (vma, address, pmdp); -} -#define pmd_move_must_withdraw pmd_move_must_withdrawstruct spinlock ; static inline int pmd_move_must_withdraw(struct spinlock *new_pmd_ptl,
arch/powerpc/include/asm/book3s/64/radix.h +0 -6
@@ -269,12 +269,6 @@ static inline pmd_t radix__pmd_mkhuge(pmd_t pmd)return __pmd (pmd_val (pmd) | _PAGE_PTE |R_PAGE_LARGE );return __pmd (pmd_val (pmd) | _PAGE_PTE); } -static inline void radix__pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp) -{ -/* Nothing to do for radix. */ -return ; -} extern unsigned long radix__pmd_hugepage_update(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, unsigned long clr,
arch/powerpc/mm/pgtable-hash64.c +0 -22
@@ -296,28 +296,6 @@ pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)return pgtable; } -void hash__pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp) -{ -VM_BUG_ON (address & ~HPAGE_PMD_MASK ); -VM_BUG_ON (REGION_ID (address) !=USER_REGION_ID ); -VM_BUG_ON (pmd_devmap (*pmdp)); - -/* -* We can't mark the pmd none here, because that will cause a race -* against exit_mmap. We need to continue mark pmd TRANS HUGE, while -* we spilt, but at the same time we wan't rest of the ppc64 code -* not to insert hash pte on this, because we will be modifying -* the deposited pgtable in the caller of this function. Hence -* clear the _PAGE_USER so that we move the fault handling to -* higher level function and that will serialize against ptl. -* We need to flush existing hash pte entries here even though, -* the translation is still valid, because we will withdraw -* pgtable_t after this. -*/ -pmd_hugepage_update (vma->vm_mm, address, pmdp,0 , _PAGE_PRIVILEGED); -} -/* * A linux hugepage PMD was changed and the corresponding hash table entries * neesd to be flushed.
include/asm-generic/pgtable.h +0 -8
@@ -329,14 +329,6 @@ extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp);#endif -#ifndef __HAVE_ARCH_PMDP_HUGE_SPLIT_PREPARE -static inline void pmdp_huge_split_prepare (struct vm_area_struct *vma, -unsigned long address,pmd_t *pmdp) -{ - -} -#endif -#ifndef __HAVE_ARCH_PTE_SAMEstatic inline int pte_same (pte_t pte_a,pte_t pte_b) {
mm/huge_memory.c +36 -38
102 lines not on this page · raw message
Signature · 7 lines
2.15.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>