x86/mm: Define the zapped-entry checks only with CONFIG_DEBUG_VM
Subsystems (5)
Kiryl Shutsemau (Meta)
reviewerPATCH 1/1 x86/mm: Define the zapped-entry checks only with CONFIG_DEBUG_VM
Zapping a range calls arch_check_zapped_pte() for every batch of PTEs, which is every page for small folios, and arch_check_zapped_pmd() or arch_check_zapped_pud() for every huge leaf. On x86 these are out-of-line functions whose only content is a VM_WARN_ON_ONCE(), so without CONFIG_DEBUG_VM they are empty, but the call remains. With the SRSO "Safe RET" mitigation the return of every such call mispredicts.
Declare and define the checks only with CONFIG_DEBUG_VM. Otherwise the empty inlines from <linux/pgtable.h> apply, as on every other architecture. They cannot be inlines in <asm/pgtable.h>: VM_SHADOW_STACK is defined in <linux/mm.h> after it includes <linux/pgtable.h>.
In a KVM guest on Zen 4 with Safe RET, zapping 4K pages takes one branch miss per page fewer. MADV_DONTNEED of 1 GiB gets 6% faster on anonymous memory and 8% on shmem, munmap() 5%. THP is unchanged.
--- arch/x86/include/asm/pgtable.h | 2 ++ arch/x86/mm/pgtable.c | 2 ++ 2 files changed, 4 insertions(+)
arch/x86/include/asm/pgtable.h +2 -0
@@ -1666,6 +1666,7 @@ static inline bool arch_has_pfn_modify_check(void)return boot_cpu_has_bug (X86_BUG_L1TF ); } +#ifdef CONFIG_DEBUG_VM #define arch_check_zapped_pte arch_check_zapped_ptevoid arch_check_zapped_pte (struct vm_area_struct *vma,pte_t pte);
@@ -1674,6 +1675,7 @@ void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd);#define arch_check_zapped_pud arch_check_zapped_pudvoid arch_check_zapped_pud (struct vm_area_struct *vma,pud_t pud); +#endif #ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young
arch/x86/mm/pgtable.c +2 -0
@@ -798,6 +798,7 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)return pmd_clear_saveddirty (pmd); } +#ifdef CONFIG_DEBUG_VM void arch_check_zapped_pte (struct vm_area_struct *vma,pte_t pte) {/*
@@ -823,3 +824,4 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud)/* See note in arch_check_zapped_pte() */ VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud)); } +#endif
Signature · 1 line
2.54.0
Quoted 12 lines · Kiryl Shutsemau (Meta)
Zapping a range calls
arch_check_zapped_pte()for every batch of PTEs, which is every page for small folios, andarch_check_zapped_pmd()orarch_check_zapped_pud()for every huge leaf. On x86 these are out-of-line functions whose only content is aVM_WARN_ON_ONCE(), so withoutCONFIG_DEBUG_VMthey are empty, but the call remains. With the SRSO "Safe RET" mitigation the return of every such call mispredicts.Declare and define the checks only with
CONFIG_DEBUG_VM. Otherwise the empty inlines from <linux/pgtable.h> apply, as on every other architecture. They cannot be inlines in <asm/pgtable.h>:VM_SHADOW_STACKis defined in <linux/mm.h> after it includes <linux/pgtable.h>.
Quoted 6 lines
In a KVM guest on Zen 4 with Safe RET, zapping 4K pages takes one branch miss per page fewer.
MADV_DONTNEEDof 1 GiB gets 6% faster on anonymous memory and 8% on shmem,munmap()5%. THP is unchanged.Fixes e5136e876581 ("mm: Warn on shadow stack memory in wrong vma")Signed-off-by Kiryl Shutsemau (Meta) <[email protected]>
LGTM:
Signature · 2 lines
cheers, jose a. p-a
Quoted 7 lines · Kiryl Shutsemau (Meta)
Zapping a range calls
arch_check_zapped_pte()for every batch of PTEs, which is every page for small folios, andarch_check_zapped_pmd()orarch_check_zapped_pud()for every huge leaf. On x86 these are out-of-line functions whose only content is aVM_WARN_ON_ONCE(), so withoutCONFIG_DEBUG_VMthey are empty, but the call remains. With the SRSO "Safe RET" mitigation the return of every such call mispredicts.
Quoted 8 lines
Declare and define the checks only with
CONFIG_DEBUG_VM. Otherwise the empty inlines from <linux/pgtable.h> apply, as on every other architecture. They cannot be inlines in <asm/pgtable.h>:VM_SHADOW_STACKis defined in <linux/mm.h> after it includes <linux/pgtable.h>.In a KVM guest on Zen 4 with Safe RET, zapping 4K pages takes one branch miss per page fewer.
MADV_DONTNEEDof 1 GiB gets 6% faster on anonymous memory and 8% on shmem,munmap()5%. THP is unchanged.
I guess this is the impact of this fix on Safe RET machines, but it's a little ambiguous. With the assumption that a mitigation didn't result in the performance improvement I can parse it.
Do you happen to know if this performance impact is relevant at all in the non Safe RET case?
Quoted 44 lines
Fixes e5136e876581 ("mm: Warn on shadow stack memory in wrong vma")Signed-off-by Kiryl Shutsemau (Meta) <[email protected]>---
arch/x86/include/asm/pgtable.h | 2 ++
arch/x86/mm/pgtable.c | 2 ++2 files changed, 4 insertions(+)arch/x86/include/asm/pgtable.h +0
-0@@ -1666,6 +1666,7 @@ static inline bool arch_has_pfn_modify_check(void) return boot_cpu_has_bug(X86_BUG_L1TF); }
+#ifdefCONFIG_DEBUG_VM
#definearch_check_zapped_ptearch_check_zapped_ptevoidarch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte);arch/x86/include/asm/pgtable.h +0
-0@@ -1674,6 +1675,7 @@ void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd);
#definearch_check_zapped_pudarch_check_zapped_pud
voidarch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud); +#endif
#ifdefCONFIG_XEN_PV
#definearch_has_hw_nonleaf_pmd_youngarch_has_hw_nonleaf_pmd_youngarch/x86/mm/pgtable.c +0
-0@@ -798,6 +798,7 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) return pmd_clear_saveddirty(pmd); }
+#ifdefCONFIG_DEBUG_VM
voidarch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte){
/*arch/x86/mm/pgtable.c +0
-0@@ -823,3 +824,4 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) /* See note in arch_check_zapped_pte() */
Quoted 2 lines
VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud));
I guess all of these don't need to be the "VM_WARN_" varieties if they are compiled out in the other case.
Quoted 2 lines
}
+#endif
Kiryl Shutsemau
reviewerQuoted 2 lines · Rick P
Do you happen to know if this performance impact is relevant at all in the non Safe RET case?
Yes, both sides of the comparison are the same kernel with Safe RET on, with and without the patch.
Without Safe RET (spec_rstack_overflow=off, same two kernels) the call and return predict fine: branch misses are the same with and without the patch, and the gain is under 2%, within noise.
Quoted 12 lines
Quoted 12 lines
arch/x86/mm/pgtable.c +0
-0@@ -798,6 +798,7 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) return pmd_clear_saveddirty(pmd); }
+#ifdefCONFIG_DEBUG_VM
voidarch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte){
/*
Quoted 8 lines
Quoted 5 lines
arch/x86/mm/pgtable.c +0
-0@@ -823,3 +824,4 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) /* See note in arch_check_zapped_pte() */
VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud));I guess all of these don't need to be the "
VM_WARN_" varieties if they are compiled out in the other case.
I can do either way. It is functionally equivalent.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 10 lines · Kiryl Shutsemau
Quoted 2 lines · Rick P
Do you happen to know if this performance impact is relevant at all in the non Safe RET case?
Yes, both sides of the comparison are the same kernel with Safe RET on, with and without the patch.
Without Safe RET (spec_rstack_overflow=off, same two kernels) the call and return predict fine: branch misses are the same with and without the patch, and the gain is under 2%, within noise.
Hmm, I'd wonder if the fixes tag is appropriate then. Should it be the safe ret commits?
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
arch/x86/mm/pgtable.c +0
-0@@ -798,6 +798,7 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) return pmd_clear_saveddirty(pmd); }
+#ifdefCONFIG_DEBUG_VM
voidarch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte){
/*Quoted 8 lines
Quoted 5 lines
arch/x86/mm/pgtable.c +0
-0@@ -823,3 +824,4 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) /* See note in arch_check_zapped_pte() */
VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud));I guess all of these don't need to be the "
VM_WARN_" varieties if they are compiled out in the other case.
Quoted 1 line
I can do either way. It is functionally equivalent.
Otherwise it's effectively a duplicate check.
On 11/2/26 06:39, Kiryl Shutsemau (Meta) wrote:
...
arch/x86/mm/pgtable.c void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) · line 827
Quoted 9 lines
arch/x86/mm/pgtable.c +2
-0+#ifdef CONFIG_DEBUG_VM void arch_check_zapped_pte (struct vm_area_struct *vma,pte_t pte) {/* @@ -823,3 +824,4 @@ void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud)/* See note in arch_check_zapped_pte() */ VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud)); } +#endif
So all these #ifdefs are because VM_SHADOW_STACK, a *constant* is not available? That's unfortunate.
I dislike doing this gunk with macros, but I'd take that over new #ifdefs. Or, settle the "does the arch provide these debug functions?" at Kconfig time with an ARCH_HAS_FOO option. That could even be driven from the x86 side with:
select ARCH_HAS_CHECK_ZAPPED_PTE if DEBUG_VM
Or, put them in a new header. They're only used in memory.c, so what's keeping them in pgtable.h? Another header could reference VM_SHADOW_STACK. No?
Or, heck, I'm not that attached to these checks. Let's just kill them.
But I really don't like the new #ifdefs. It would be about my fifth favorite way to fix this.
Kiryl Shutsemau
reviewerQuoted 1 line · Dave Hansen
Or, heck, I'm not that attached to these checks. Let's just kill them.
Works for me. x86 is the only architecture with them, although arm64 and riscv have shadow stacks too, and the x86 helpers already keep Write=0,Dirty=1 out of other VMAs. Patch below, without the Fixes: tag.
Rick, any objection?
-- >8 --
From: "Kiryl Shutsemau (Meta)" <[email protected]>
Subject: [PATCH] x86/mm: Remove the zapped-entry checks
Zapping a range calls arch_check_zapped_pte() for every batch of PTEs, which is every page for small folios, and arch_check_zapped_pmd() or arch_check_zapped_pud() for every huge leaf. Only x86 implements them, as out-of-line functions that warn about a shadow stack entry (Write=0, Dirty=1) in a VMA without VM_SHADOW_STACK.
The warning is a VM_WARN_ON_ONCE(), so without CONFIG_DEBUG_VM the functions are empty, but the calls remain. With the SRSO "Safe RET" mitigation the return of every such call mispredicts.
The x86 helpers already keep such entries out of other VMAs. pte_wrprotect(), pte_mkdirty(), pte_modify() and their PMD versions go through SavedDirty, and only pte_mkwrite() and pmd_mkwrite() on a VM_SHADOW_STACK VMA create the combination.
Remove the checks, their callers and pud_shstk(), which has no other user.
In a KVM guest on Zen 4 with Safe RET, zapping 4K pages takes one branch miss per page fewer. MADV_DONTNEED of 1 GiB gets 6% faster on anonymous memory and 8% on shmem, munmap() 5%. THP is unchanged. Without Safe RET the difference is within noise.
--- arch/x86/include/asm/pgtable.h | 16 ---------------- arch/x86/mm/pgtable.c | 26 -------------------------- include/linux/pgtable.h | 20 -------------------- mm/huge_memory.c | 2 -- mm/memory.c | 3 --- 5 files changed, 67 deletions(-)
arch/x86/include/asm/pgtable.h +0 -16
@@ -199,13 +199,6 @@ static inline int pud_young(pud_t pud)return pud_flags (pud) & _PAGE_ACCESSED; } -static inline bool pud_shstk (pud_t pud) -{ -return cpu_feature_enabled (X86_FEATURE_SHSTK ) && - (pud_flags (pud) & (_PAGE_RW | _PAGE_DIRTY | _PAGE_PSE)) == - (_PAGE_DIRTY | _PAGE_PSE); -} -static inline int pte_write (pte_t pte) {/*
@@ -1666,15 +1659,6 @@ static inline bool arch_has_pfn_modify_check(void)return boot_cpu_has_bug (X86_BUG_L1TF ); } -#define arch_check_zapped_pte arch_check_zapped_pte -void arch_check_zapped_pte (struct vm_area_struct *vma,pte_t pte); - -#define arch_check_zapped_pmd arch_check_zapped_pmd -void arch_check_zapped_pmd (struct vm_area_struct *vma,pmd_t pmd); - -#define arch_check_zapped_pud arch_check_zapped_pud -void arch_check_zapped_pud (struct vm_area_struct *vma,pud_t pud); -#ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_youngstatic inline bool arch_has_hw_nonleaf_pmd_young (void )
arch/x86/mm/pgtable.c +0 -26
@@ -797,29 +797,3 @@ pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)return pmd_clear_saveddirty (pmd); } - -void arch_check_zapped_pte (struct vm_area_struct *vma,pte_t pte) -{ -/* -* Hardware before shadow stack can (rarely) set Dirty=1 -* on a Write=0 PTE. So the below condition -* only indicates a software bug when shadow stack is -* supported by the HW. This checking is covered in -* pte_shstk(). -*/ -VM_WARN_ON_ONCE (!(vma->vm_flags &VM_SHADOW_STACK ) && -pte_shstk (pte)); -} - -void arch_check_zapped_pmd (struct vm_area_struct *vma,pmd_t pmd) -{ -/* See note in arch_check_zapped_pte() */ -VM_WARN_ON_ONCE (!(vma->vm_flags &VM_SHADOW_STACK ) && -pmd_shstk (pmd)); -} - -void arch_check_zapped_pud (struct vm_area_struct *vma,pud_t pud) -{ -/* See note in arch_check_zapped_pte() */ -VM_WARN_ON_ONCE (!(vma->vm_flags &VM_SHADOW_STACK ) &&pud_shstk (pud)); -}
include/linux/pgtable.h +0 -20
@@ -621,26 +621,6 @@ static inline unsigned int exec_folio_order(void) }#endif -#ifndef arch_check_zapped_pte -static inline void arch_check_zapped_pte (struct vm_area_struct *vma, -pte_t pte) -{ -} -#endif - -#ifndef arch_check_zapped_pmd -static inline void arch_check_zapped_pmd (struct vm_area_struct *vma, -pmd_t pmd) -{ -} -#endif - -#ifndef arch_check_zapped_pud -static inline void arch_check_zapped_pud (struct vm_area_struct *vma,pud_t pud) -{ -} -#endif -#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address,
mm/huge_memory.c +0 -2
@@ -2558,7 +2558,6 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,*/ orig_pmd =pmdp_huge_get_and_clear_full (vma, addr, pmd, tlb->fullmm); -arch_check_zapped_pmd (vma, orig_pmd);tlb_remove_pmd_tlb_entry (tlb, pmd, addr); is_present =pmd_present (orig_pmd);
@@ -3045,7 +3044,6 @@ int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,return 0 ; orig_pud =pudp_huge_get_and_clear_full (vma, addr, pud, tlb->fullmm); -arch_check_zapped_pud (vma, orig_pud);tlb_remove_pud_tlb_entry (tlb, pud, addr);if (!vma_is_mm_managed (vma)) {spin_unlock (ptl);
mm/memory.c +0 -3
@@ -1755,8 +1755,6 @@ static __always_inline void zap_present_folio_ptes(struct mmu_gather *tlb,clear_full_ptes (mm, addr, pte, nr, tlb->fullmm); rss[MM_ANONPAGES ] -= nr; } -/* Checking a single PTE in a batch is sufficient. */ -arch_check_zapped_pte (vma, ptent);tlb_remove_tlb_entries (tlb, pte, nr, addr);if (unlikely (userfaultfd_pte_wp (vma, ptent))) *any_skipped = zap_install_uffd_wp_if_needed(vma, addr, pte,
@@ -1795,7 +1793,6 @@ static inline int zap_present_ptes(struct mmu_gather *tlb,if (!page) {/* We don't need up-to-date accessed/dirty bits. */ ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); -arch_check_zapped_pte (vma, ptent);tlb_remove_tlb_entry (tlb, pte, addr);if (userfaultfd_pte_wp (vma, ptent)) *any_skipped = zap_install_uffd_wp_if_needed(vma, addr,
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 8 lines · Kiryl Shutsemau
Quoted 1 line · Dave Hansen
Or, heck, I'm not that attached to these checks. Let's just kill them.
Works for me. x86 is the only architecture with them, although arm64 and riscv have shadow stacks too, and the x86 helpers already keep Write=0,Dirty=1 out of other VMAs. Patch below, without the Fixes: tag.
Rick, any objection?
Eh, I guess I'm lukewarm on it. These checks only catch the case at zap time, so it's not a huge protection that is being pulled out. I thought they were useful. At the same time, they have never triggered.
Kiryl Shutsemau
reviewerQuoted 8 lines · Rick P
Quoted 7 lines · Kiryl Shutsemau
Quoted 1 line · Dave Hansen
Or, heck, I'm not that attached to these checks. Let's just kill them.
Works for me. x86 is the only architecture with them, although arm64 and riscv have shadow stacks too, and the x86 helpers already keep Write=0,Dirty=1 out of other VMAs. Patch below, without the Fixes: tag.
Quoted 5 lines
Quoted 1 line
Rick, any objection?
Eh, I guess I'm lukewarm on it. These checks only catch the case at zap time, so it's not a huge protection that is being pulled out. I thought they were useful. At the same time, they have never triggered.
Yeah, I have not find any reports.
So, we can claim that the check served its purpose to stabilize shadow stack and can be retired now :P
I will post the patch separately.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] x86/mm: commit title contradicts code and changelog has unstripped scissors --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] x86/mm: commit title contradicts code and changelog has unstripped scissors --
commit 1651219a7e4a74c995baa4203be8528bd7cfe1d0
Author: Kiryl Shutsemau <[email protected]>
x86/mm: Define the zapped-entry checks only with CONFIG_DEBUG_VM
This commit removes the arch_check_zapped_pte(), arch_check_zapped_pmd(), and arch_check_zapped_pud() helpers along with their callers and pud_shstk() to avoid return mispredictions during page table zapping.
Quoted 33 lines
x86/mm: Define the zapped-entry checks only with
CONFIG_DEBUG_VMOn Mon, Nov 02, 2026 at 01:12:47PM -0800, Dave Hansen wrote:
> Or, heck, I'm not that attached to these checks. Let's just kill them.Works for me. x86 is the only architecture with them, although arm64 and riscv have shadow stacks too, and the x86 helpers already keep Write=0,Dirty=1 out of other VMAs. Patch below, without the Fixes: tag.
Rick, any objection?
-- >8 --
From: "Kiryl Shutsemau (Meta)" <[email protected]>
Subject: [PATCH] x86/mm: Remove the zapped-entry checksZapping a range calls
arch_check_zapped_pte()for every batch of PTEs, which is every page for small folios, andarch_check_zapped_pmd()orarch_check_zapped_pud()for every huge leaf. Only x86 implements them, as out-of-line functions that warn about a shadow stack entry (Write=0, Dirty=1) in a VMA withoutVM_SHADOW_STACK.The warning is a
VM_WARN_ON_ONCE(), so withoutCONFIG_DEBUG_VMthe functions are empty, but the calls remain. With the SRSO "Safe RET" mitigation the return of every such call mispredicts.The x86 helpers already keep such entries out of other VMAs.
pte_wrprotect(),pte_mkdirty(),pte_modify()and their PMD versions go through SavedDirty, and onlypte_mkwrite()andpmd_mkwrite()on aVM_SHADOW_STACKVMA create the combination.Remove the checks, their callers and
pud_shstk(), which has no other user.
Quoted 7 lines
In a KVM guest on Zen 4 with Safe RET, zapping 4K pages takes one branch miss per page fewer.
MADV_DONTNEEDof 1 GiB gets 6% faster on anonymous memory and 8% on shmem,munmap()5%. THP is unchanged. Without Safe RET the difference is within noise.Suggested-by Dave Hansen <[email protected]>Signed-off-by Kiryl Shutsemau (Meta) <[email protected]>
[Severity: Medium]
Was this patch applied without passing --scissors to git am?
The commit title still reads "x86/mm: Define the zapped-entry checks only with CONFIG_DEBUG_VM", whereas the patch unconditionally deletes arch_check_zapped_pte(), arch_check_zapped_pmd(), and arch_check_zapped_pud() in arch/x86/mm/pgtable.c, arch/x86/include/asm/pgtable.h, and include/linux/pgtable.h, along with pud_shstk() in arch/x86/include/asm/pgtable.h and the callers in zap_present_folio_ptes() and zap_present_ptes() in mm/memory.c and zap_huge_pmd() and zap_huge_pud() in mm/huge_memory.c.
In addition, the changelog retains the mailing list reply, the "-- >8 --" scissors line, and the embedded From and Subject headers ("x86/mm: Remove the zapped-entry checks"). Should the preceding reply and scissors headers be stripped and the commit title updated to "x86/mm: Remove the zapped-entry checks"?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/aumzjHyJsshPJBUI@thinkstation?part=1