mm: Fix race between __split_huge_pmd_locked() and GUP-fast
Subsystems (12)
Ryan Roberts
reviewerPATCH v3 1/1 mm: Fix race between __split_huge_pmd_locked() and GUP-fast
__split_huge_pmd_locked() can be called for a present THP, devmap or (non-present) migration entry. It calls pmdp_invalidate() unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case because pmd_mkinvalid(), called by pmdp_invalidate() must only be called for a present pmd.
On arm64 at least, pmd_mkinvalid() will mark the pmd such that any future call to pmd_present() will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.
x86 does not suffer the above problem, but instead pmd_mkinvalid() will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.
Fix all of this by only calling pmdp_invalidate() for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.
This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle pmd_mkinvalid() being called for non-present pmds either.
I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================
- Improve pmdp_mkinvalid() docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location of pmpd_invalidate() (per Zi Yan)
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Thanks,
Ryan
Documentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)
Documentation/mm/arch_pgtable_helpers.rst +4 -2
@@ -140,7 +140,8 @@ PMD Page Table Helpers +---------------------------+--------------------------------------------------+ | pmd_swp_clear_soft_dirty | Clears a soft dirty swapped PMD | +---------------------------+--------------------------------------------------+ -| pmd_mkinvalid | Invalidates a mapped PMD [1] | +| pmd_mkinvalid | Invalidates a present PMD; do not call for | +| | non-present PMD [1] | +---------------------------+--------------------------------------------------+ | pmd_set_huge | Creates a PMD huge mapping | +---------------------------+--------------------------------------------------+
@@ -196,7 +197,8 @@ PUD Page Table Helpers +---------------------------+--------------------------------------------------+ | pud_mkdevmap | Creates a ZONE_DEVICE mapped PUD | +---------------------------+--------------------------------------------------+ -| pud_mkinvalid | Invalidates a mapped PUD [1] | +| pud_mkinvalid | Invalidates a present PUD; do not call for | +| | non-present PUD [1] | +---------------------------+--------------------------------------------------+ | pud_set_huge | Creates a PUD huge mapping | +---------------------------+--------------------------------------------------+
arch/powerpc/mm/book3s64/pgtable.c +1 -0
@@ -170,6 +170,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {unsigned long old_pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); old_pmd =pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return __pmd (old_pmd);
arch/s390/include/asm/pgtable.h +3 -1
@@ -1768,8 +1768,10 @@ static inline pmd_t pmdp_huge_clear_flush(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); +pmd_t pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); + pmd =__pmd (pmd_val (*pmdp) | _SEGMENT_ENTRY_INVALID);return pmdp_xchg_direct (vma->vm_mm, addr, pmdp, pmd); }
arch/sparc/mm/tlb.c +1 -0
@@ -249,6 +249,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {pmd_t old, entry; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); entry =__pmd (pmd_val (*pmdp) & ~_PAGE_VALID); old =pmdp_establish (vma, address, pmdp, entry);flush_tlb_range (vma, address, address +HPAGE_PMD_SIZE );
arch/x86/mm/pgtable.c +2 -0
@@ -631,6 +631,8 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); +/* * No flush is necessary. Once an invalid PTE is established, the PTE's * access and dirty bits cannot be updated.
mm/huge_memory.c +26 -23
64 lines not on this page · raw message
mm/pgtable-generic.c +2 -0
@@ -198,6 +198,7 @@ pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));pmd_t old =pmdp_establish (vma, address, pmdp,pmd_mkinvalid (*pmdp));flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return old;
@@ -208,6 +209,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));return pmdp_invalidate (vma, address, pmdp); }#endif
--
2.25.1
Quoted 59 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Fix all of this by only calling
pmdp_invalidate()for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
Fixes 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path")Signed-off-by Ryan Roberts <[email protected]>---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle
pmd_mkinvalid()being called for non-present pmds either.I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================- Improve
pmdp_mkinvalid()docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location ofpmpd_invalidate()(per Zi Yan)[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Thanks,
Ryan
Quoted 8 lines
Documentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)
The changes in Documentation/mm and mm/* look good to me. Thanks.
I wonder if making Documentation/mm and mm/* changes in a separate patch would be better, since you will not need acks from arch maintainers and get the patch in quicker.
--
Best Regards,
Yan, Zi
Quoted 30 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Fix all of this by only calling
pmdp_invalidate()for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
Fixes 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path")Signed-off-by Ryan Roberts <[email protected]>
Quoted 5 lines
---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle
pmd_mkinvalid()being called for non-present pmds either.
This is a better approach indeed.
Quoted 48 lines
I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================- Improve
pmdp_mkinvalid()docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location ofpmpd_invalidate()(per Zi Yan)[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Thanks,
RyanDocumentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)Documentation/mm/arch_pgtable_helpers.rst +2
-1@@ -140,7 +140,8 @@ PMD Page Table Helpers +---------------------------+--------------------------------------------------+ | pmd_swp_clear_soft_dirty | Clears a soft dirty swapped PMD | +---------------------------+--------------------------------------------------+ -| pmd_mkinvalid | Invalidates a mapped PMD [1] | +| pmd_mkinvalid | Invalidates a present PMD; do not call for | +| | non-present PMD [1] | +---------------------------+--------------------------------------------------+ | pmd_set_huge | Creates a PMD huge mapping | +---------------------------+--------------------------------------------------+
Documentation/mm/arch_pgtable_helpers.rst PUD Page Table Helpers · line 204
Quoted 9 lines
Documentation/mm/arch_pgtable_helpers.rst +2
-1@@ -196,7 +197,8 @@ PUD Page Table Helpers +---------------------------+--------------------------------------------------+ | pud_mkdevmap | Creates a ZONE_DEVICE mapped PUD | +---------------------------+--------------------------------------------------+ -| pud_mkinvalid | Invalidates a mapped PUD [1] | +| pud_mkinvalid | Invalidates a present PUD; do not call for | +| | non-present PUD [1] | +---------------------------+--------------------------------------------------+ | pud_set_huge | Creates a PUD huge mapping | +---------------------------+--------------------------------------------------+
LGTM but guess this will conflict with your other patch for mm/debug_vm_pgtable.c if you choose to update pud_mkinvalid() description for pmd_leaf().
Quoted 136 lines
arch/powerpc/mm/book3s64/pgtable.c +1
-0@@ -170,6 +170,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {unsigned long old_pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); old_pmd =pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return __pmd (old_pmd);arch/s390/include/asm/pgtable.h +3
-1@@ -1768,8 +1768,10 @@ static inline pmd_t pmdp_huge_clear_flush(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); +pmd_t pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); + pmd =__pmd (pmd_val (*pmdp) | _SEGMENT_ENTRY_INVALID);return pmdp_xchg_direct (vma->vm_mm, addr, pmdp, pmd); }arch/sparc/mm/tlb.c +1
-0@@ -249,6 +249,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {pmd_t old, entry; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); entry =__pmd (pmd_val (*pmdp) & ~_PAGE_VALID); old =pmdp_establish (vma, address, pmdp, entry);flush_tlb_range (vma, address, address +HPAGE_PMD_SIZE );arch/x86/mm/pgtable.c +2
-0@@ -631,6 +631,8 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); +/* * No flush is necessary. Once an invalid PTE is established, the PTE's * access and dirty bits cannot be updated. mm/huge_memory.c +26
-23@@ -2493,32 +2493,11 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } -/* -* Up to this point the pmd is present and huge and userland has the -* whole access to the hugepage during the split (which happens in -* place). If we overwrite the pmd with the not-huge version pointing -* to the pte here (which of course we could if all CPUs were bug -* free), userland could trigger a small page size TLB miss on the -* small sized TLB while the hugepage TLB entry is still established in -* the huge TLB. Some CPU doesn't like that. -* See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum -* 383 on page 105. Intel should be safe but is also warns that it's -* only safe if the permission and cache attributes of the two entries -* loaded in the two TLB is identical (which should be the case here). -* But it is generally safer to never allow small and huge TLB entries -* for the same virtual address to be loaded simultaneously. So instead -* of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the -* current pmd notpresent (atomically because here the pmd_trans_huge -* must remain set at all times on the pmd until the split is complete -* for this pmd), then we flush the SMP TLB and finally we write the -* non-huge version of the pmd entry with pmd_populate. -*/ - old_pmd =pmdp_invalidate (vma, haddr, pmd); - - pmd_migration =is_pmd_migration_entry (old_pmd); + pmd_migration =is_pmd_migration_entry (*pmd);if (unlikely (pmd_migration)) {swp_entry_t entry; + old_pmd = *pmd; entry =pmd_to_swp_entry (old_pmd); page =pfn_swap_entry_to_page (entry); write =is_writable_migration_entry (entry);@@ -2529,6 +2508,30 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); }else { +/* +* Up to this point the pmd is present and huge and userland has +* the whole access to the hugepage during the split (which +* happens in place). If we overwrite the pmd with the not-huge +* version pointing to the pte here (which of course we could if +* all CPUs were bug free), userland could trigger a small page +* size TLB miss on the small sized TLB while the hugepage TLB +* entry is still established in the huge TLB. Some CPU doesn't +* like that. See +* http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum +* 383 on page 105. Intel should be safe but is also warns that +* it's only safe if the permission and cache attributes of the +* two entries loaded in the two TLB is identical (which should +* be the case here). But it is generally safer to never allow +* small and huge TLB entries for the same virtual address to be +* loaded simultaneously. So instead of doing "pmd_populate(); +* flush_pmd_tlb_range();" we first mark the current pmd +* notpresent (atomically because here the pmd_trans_huge must +* remain set at all times on the pmd until the split is +* complete for this pmd), then we flush the SMP TLB and finally +* we write the non-huge version of the pmd entry with +* pmd_populate. +*/ + old_pmd =pmdp_invalidate (vma, haddr, pmd); page =pmd_page (old_pmd); folio =page_folio (page);if (pmd_dirty (old_pmd)) {mm/pgtable-generic.c +1
-0@@ -198,6 +198,7 @@ pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));pmd_t old =pmdp_establish (vma, address, pmdp,pmd_mkinvalid (*pmdp));flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return old;
mm/pgtable-generic.c pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, · line 215
Quoted 7 lines
mm/pgtable-generic.c +1
-0@@ -208,6 +209,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));return pmdp_invalidate (vma, address, pmdp); }#endif
Rest LGTM but let's wait for this to run on multiple platforms.
Ryan Roberts
reviewerQuoted 70 lines · Zi Yan
Quoted 59 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Fix all of this by only calling
pmdp_invalidate()for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
Fixes 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path")Signed-off-by Ryan Roberts <[email protected]>---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle
pmd_mkinvalid()being called for non-present pmds either.I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================- Improve
pmdp_mkinvalid()docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location ofpmpd_invalidate()(per Zi Yan)[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Thanks,
RyanQuoted 8 lines
Documentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)
Quoted 2 lines
The changes in
Documentation/mmandmm/*look good to me. Thanks.Reviewed-by Zi Yan <[email protected]>
Thanks!
Quoted 4 lines
I wonder if making
Documentation/mmandmm/*changes in a separate patch would be better, since you will not need acks from arch maintainers and get the patch in quicker.
Yeah maybe - I considered that, but then decided I'm literally just adding a debug warning to the arch code so it shouldn't be too controversial. Perhaps wait a few days for acks and if nothing turns up then I'll re-post with it split?
Quoted 5 lines
--
Best Regards,
Yan, Zi
Ryan Roberts
reviewerQuoted 102 lines · Anshuman Khandual
Quoted 30 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Fix all of this by only calling
pmdp_invalidate()for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
Fixes 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path")Signed-off-by Ryan Roberts <[email protected]>Quoted 5 lines
---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle
pmd_mkinvalid()being called for non-present pmds either.This is a better approach indeed.
Quoted 48 lines
I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================- Improve
pmdp_mkinvalid()docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location ofpmpd_invalidate()(per Zi Yan)[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Thanks,
RyanDocumentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)Documentation/mm/arch_pgtable_helpers.rst +2
-1@@ -140,7 +140,8 @@ PMD Page Table Helpers +---------------------------+--------------------------------------------------+ | pmd_swp_clear_soft_dirty | Clears a soft dirty swapped PMD | +---------------------------+--------------------------------------------------+ -| pmd_mkinvalid | Invalidates a mapped PMD [1] | +| pmd_mkinvalid | Invalidates a present PMD; do not call for | +| | non-present PMD [1] | +---------------------------+--------------------------------------------------+ | pmd_set_huge | Creates a PMD huge mapping | +---------------------------+--------------------------------------------------+Documentation/mm/arch_pgtable_helpers.rst PUD Page Table Helpers · line 204
Quoted 9 lines
Documentation/mm/arch_pgtable_helpers.rst +2
-1@@ -196,7 +197,8 @@ PUD Page Table Helpers +---------------------------+--------------------------------------------------+ | pud_mkdevmap | Creates a ZONE_DEVICE mapped PUD | +---------------------------+--------------------------------------------------+ -| pud_mkinvalid | Invalidates a mapped PUD [1] | +| pud_mkinvalid | Invalidates a present PUD; do not call for | +| | non-present PUD [1] | +---------------------------+--------------------------------------------------+ | pud_set_huge | Creates a PUD huge mapping | +---------------------------+--------------------------------------------------+LGTM but guess this will conflict with your other patch for
mm/debug_vm_pgtable.cif you choose to updatepud_mkinvalid()description forpmd_leaf().
Quoted 1 line
Indeed, a reason to avoid adding docs to that patch :)
Quoted 147 lines
Quoted 137 lines
arch/powerpc/mm/book3s64/pgtable.c +1
-0@@ -170,6 +170,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {unsigned long old_pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); old_pmd =pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID);flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return __pmd (old_pmd);arch/s390/include/asm/pgtable.h +3
-1@@ -1768,8 +1768,10 @@ static inline pmd_t pmdp_huge_clear_flush(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); +pmd_t pmd; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); + pmd =__pmd (pmd_val (*pmdp) | _SEGMENT_ENTRY_INVALID);return pmdp_xchg_direct (vma->vm_mm, addr, pmdp, pmd); }arch/sparc/mm/tlb.c +1
-0@@ -249,6 +249,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, {pmd_t old, entry; +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); entry =__pmd (pmd_val (*pmdp) & ~_PAGE_VALID); old =pmdp_establish (vma, address, pmdp, entry);flush_tlb_range (vma, address, address +HPAGE_PMD_SIZE );arch/x86/mm/pgtable.c +2
-0@@ -631,6 +631,8 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp)); +/* * No flush is necessary. Once an invalid PTE is established, the PTE's * access and dirty bits cannot be updated. mm/huge_memory.c +26
-23@@ -2493,32 +2493,11 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } -/* -* Up to this point the pmd is present and huge and userland has the -* whole access to the hugepage during the split (which happens in -* place). If we overwrite the pmd with the not-huge version pointing -* to the pte here (which of course we could if all CPUs were bug -* free), userland could trigger a small page size TLB miss on the -* small sized TLB while the hugepage TLB entry is still established in -* the huge TLB. Some CPU doesn't like that. -* See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum -* 383 on page 105. Intel should be safe but is also warns that it's -* only safe if the permission and cache attributes of the two entries -* loaded in the two TLB is identical (which should be the case here). -* But it is generally safer to never allow small and huge TLB entries -* for the same virtual address to be loaded simultaneously. So instead -* of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the -* current pmd notpresent (atomically because here the pmd_trans_huge -* must remain set at all times on the pmd until the split is complete -* for this pmd), then we flush the SMP TLB and finally we write the -* non-huge version of the pmd entry with pmd_populate. -*/ - old_pmd =pmdp_invalidate (vma, haddr, pmd); - - pmd_migration =is_pmd_migration_entry (old_pmd); + pmd_migration =is_pmd_migration_entry (*pmd);if (unlikely (pmd_migration)) {swp_entry_t entry; + old_pmd = *pmd; entry =pmd_to_swp_entry (old_pmd); page =pfn_swap_entry_to_page (entry); write =is_writable_migration_entry (entry);@@ -2529,6 +2508,30 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); }else { +/* +* Up to this point the pmd is present and huge and userland has +* the whole access to the hugepage during the split (which +* happens in place). If we overwrite the pmd with the not-huge +* version pointing to the pte here (which of course we could if +* all CPUs were bug free), userland could trigger a small page +* size TLB miss on the small sized TLB while the hugepage TLB +* entry is still established in the huge TLB. Some CPU doesn't +* like that. See +* http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum +* 383 on page 105. Intel should be safe but is also warns that +* it's only safe if the permission and cache attributes of the +* two entries loaded in the two TLB is identical (which should +* be the case here). But it is generally safer to never allow +* small and huge TLB entries for the same virtual address to be +* loaded simultaneously. So instead of doing "pmd_populate(); +* flush_pmd_tlb_range();" we first mark the current pmd +* notpresent (atomically because here the pmd_trans_huge must +* remain set at all times on the pmd until the split is +* complete for this pmd), then we flush the SMP TLB and finally +* we write the non-huge version of the pmd entry with +* pmd_populate. +*/ + old_pmd =pmdp_invalidate (vma, haddr, pmd); page =pmd_page (old_pmd); folio =page_folio (page);if (pmd_dirty (old_pmd)) {mm/pgtable-generic.c +1
-0@@ -198,6 +198,7 @@ pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));pmd_t old =pmdp_establish (vma, address, pmdp,pmd_mkinvalid (*pmdp));flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE );return old;mm/pgtable-generic.c pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, · line 215
Quoted 7 lines
mm/pgtable-generic.c +1
-0@@ -208,6 +209,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,pmd_t pmdp_invalidate_ad (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { +VM_WARN_ON_ONCE (!pmd_present (*pmdp));return pmdp_invalidate (vma, address, pmdp); }#endif Rest LGTM but let's wait for this to run on multiple platforms.
Quoted 1 line
Reviewed-by Anshuman Khandual <[email protected]>
Thanks!
Quoted 13 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.
Quoted 3 lines
x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.
Could that explain:
https://lore.kernel.org/all/[email protected]/
Where the PFN of a migration entry might have been corrupted?
Ccing Felix
Patch itself looks good to me
Signature · 3 lines
Cheers, David / dhildenb
Zi Yan
reviewerQuoted 77 lines · Ryan Roberts
Quoted 70 lines · Zi Yan
Quoted 59 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Fix all of this by only calling
pmdp_invalidate()for a present pmd. And for good measure let's add a warning to all implementations of pmdp_invalidate[_ad](). I've manually reviewed all other pmdp_invalidate[_ad]() call sites and believe all others to be conformant.This is a theoretical bug found during code review. I don't have any test case to trigger it in practice.
Fixes 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path")Signed-off-by Ryan Roberts <[email protected]>---
Right v3; this goes back to the original approach in v1 to fix core-mm rather than push the fix into arm64, since we discovered that x86 can't handle
pmd_mkinvalid()being called for non-present pmds either.I'm pulling in more arch maintainers because this version adds some warnings in arch code to help spot incorrect usage.
Although Catalin had already accepted v2 (fixing arm64) [2] into for-next/fixes, he's agreed to either remove or revert it.
Changes since v1 [1]
====================- Improve
pmdp_mkinvalid()docs to make it clear it can only be called for present pmd (per JohnH, Zi Yan)
- Added warnings to arch overrides of pmdp_invalidate[_ad]() (per Zi Yan)
- Moved comment next to new location ofpmpd_invalidate()(per Zi Yan)[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Thanks,
RyanQuoted 8 lines
Documentation/mm/arch_pgtable_helpers.rst | 6 ++- arch/powerpc/mm/book3s64/pgtable.c | 1 + arch/s390/include/asm/pgtable.h | 4 +- arch/sparc/mm/tlb.c | 1 + arch/x86/mm/pgtable.c | 2 + mm/huge_memory.c | 49 ++++++++++++----------- mm/pgtable-generic.c | 2 + 7 files changed, 39 insertions(+), 26 deletions(-)Quoted 2 lines
The changes in
Documentation/mmandmm/*look good to me. Thanks.Reviewed-by Zi Yan <[email protected]>Thanks!
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
I wonder if making
Documentation/mmandmm/*changes in a separate patch would be better, since you will not need acks from arch maintainers and get the patch in quicker.Yeah maybe - I considered that, but then decided I'm literally just adding a debug warning to the arch code so it shouldn't be too controversial. Perhaps wait a few days for acks and if nothing turns up then I'll re-post with it split?
Sure, sounds good to me. Thanks.
--
Best Regards,
Yan, Zi
Ryan Roberts
reviewerQuoted 18 lines · David Hildenbrand
Quoted 13 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.Quoted 3 lines
x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.
Quoted 5 lines
Could that explain:
https://lore.kernel.org/all/[email protected]/
Where the PFN of a migration entry might have been corrupted?
Ahh interesting! Yes, it seems to fit...
Quoted 2 lines
Ccing Felix
Are you able to reliably reproduce the bug, Felix? If so, would you mind trying with this patch to see if it goes away?
Quoted 5 lines
Patch itself looks good to me
Thanks!
Quoted 20 lines · Ryan Roberts
Quoted 19 lines · David Hildenbrand
Quoted 13 lines · Ryan Roberts
__split_huge_pmd_locked()can be called for a present THP, devmap or (non-present) migration entry. It callspmdp_invalidate()unconditionally on the pmdp and only determines if it is present or not based on the returned old pmd. This is a problem for the migration entry case becausepmd_mkinvalid(), called bypmdp_invalidate()must only be called for a present pmd.On arm64 at least,
pmd_mkinvalid()will mark the pmd such that any future call topmd_present()will return true. And therefore any lockless pgtable walker could see the migration entry pmd in this state and start interpretting the fields as if it were present, leading to BadThings (TM). GUP-fast appears to be one such lockless pgtable walker.Quoted 3 lines
x86 does not suffer the above problem, but instead
pmd_mkinvalid()will corrupt the offset field of the swap entry within the swap pte. See link below for discussion of that problem.Could that explain:
Quoted 8 lines
Quoted 3 lines
https://lore.kernel.org/all/[email protected]/
Where the PFN of a migration entry might have been corrupted?
Ahh interesting! Yes, it seems to fit...
Quoted 1 line
Ccing Felix
Are you able to reliably reproduce the bug, Felix? If so, would you mind trying with this patch to see if it goes away?
Sorry, this question got lost and I found it while cleaning my inbox. The team that ran into this problems reported that it was a one-off failure. They didn't see this problem again. So I can't help with verifying the fix.
Regards,
Felix
Quoted 6 lines
Quoted 4 lines
Patch itself looks good to me
Thanks!