Skip to contentSkip to conversation

Linux VM workaround for Knights Landing A/D leak

PATCH Superseded Lukasz Anaczkowski 8 participants 38 messages Jun 14, 2016 – Jun 16, 2016

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Linux VM workaround for Knights Landing A/D leak

From: Andi Kleen <[email protected]>

Knights Landing has a issue that a thread setting A or D bits may not do so atomically against checking the present bit. A thread which is going to page fault may still set those bits, even though the present bit was already atomically cleared.

This implies that when the kernel clears present atomically, some time later the supposed to be zero entry could be corrupted with stray A or D bits.

Since the PTE could be already used for storing a swap index, or a NUMA migration index, this cannot be tolerated. Most of the time the kernel detects the problem, but in some rare cases it may not.

This patch enforces that the page unmap path in vmscan/direct reclaim always flushes other CPUs after clearing each page, and also clears the PTE again after the flush.

For reclaim this brings the performance back to before Mel's flushing changes, but for unmap it disables batching.

This makes sure any leaked A/D bits are immediately cleared before the entry is used for something else.

Any parallel faults that check for entry is zero may loop, but they should eventually recover after the entry is written.

Also other users may spin in the page table lock until we "fixed" the PTE. This is ensured by always taking the page table lock even for the swap cache case. Previously this was only done on architectures with non atomic PTE accesses (such as 32bit PTE), but now it is also done when this bug workaround is active.

I audited apply_pte_range and other users of arch_enter_lazy... and they seem to all not clear the present bit.

Right now the extra flush is done in the low level architecture code, while the higher level code still does batched TLB flush. This means there is always one extra unnecessary TLB flush now. As a followon optimization this could be avoided by telling the callers that the flush already happenend.

---
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/hugetlb.h     |  9 ++++++++-
 arch/x86/include/asm/pgtable.h     |  5 +++++
 arch/x86/include/asm/pgtable_64.h  |  6 ++++++
 arch/x86/kernel/cpu/intel.c        | 10 ++++++++++
 arch/x86/mm/tlb.c                  | 20 ++++++++++++++++++++
 include/linux/mm.h                 |  4 ++++
 mm/memory.c                        |  3 ++-
 8 files changed, 56 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
index 4a41348..2c48011 100644
@@ -303,6 +303,7 @@
 #define X86_BUG_SYSRET_SS_ATTRS	X86_BUG(8) /* SYSRET doesn't fix up SS attrs */
 #define X86_BUG_NULL_SEG	X86_BUG(9) /* Nulling a selector preserves the base */
 #define X86_BUG_SWAPGS_FENCE	X86_BUG(10) /* SWAPGS without input dep on GS */
+#define X86_BUG_PTE_LEAK        X86_BUG(11) /* PTE may leak A/D bits after clear */
 
 
 #ifdef CONFIG_X86_32
arch/x86/include/asm/hugetlb.h +8 -1
index 3a10616..58e1ca9 100644
@@ -41,10 +41,17 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
 	set_pte_at(mm, addr, ptep, pte);
 }
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 					    unsigned long addr, pte_t *ptep)
 {
-	return ptep_get_and_clear(mm, addr, ptep);
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;
 }
 
 static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/x86/include/asm/pgtable.h +5 -0
index 1a27396..9769355 100644
@@ -794,11 +794,16 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
 				  unsigned long address, pte_t *ptep);
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
 	pte_t pte = native_ptep_get_and_clear(ptep);
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
 	pte_update(mm, addr, ptep);
 	return pte;
 }
arch/x86/include/asm/pgtable_64.h +6 -0
index 2ee7811..6fa4079 100644
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/kernel/cpu/intel.c +10 -0
index 6e2ffbe..f499513 100644
@@ -181,6 +181,16 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 		}
 	}
 
+	if (c->x86_model == 87) {
+		static bool printed;
+
+		if (!printed) {
+			pr_info("Enabling PTE leaking workaround\n");
+			printed = true;
+		}
+		set_cpu_bug(c, X86_BUG_PTE_LEAK);
+	}
+
 	/*
 	 * Intel Quark Core DevMan_001.pdf section 6.4.11
 	 * "The operating system also is required to invalidate (i.e., flush)
arch/x86/mm/tlb.c +20 -0
index 5643fd0..3d54488 100644
@@ -468,4 +468,24 @@ static int __init create_tlb_single_page_flush_ceiling(void)
 }
 late_initcall(create_tlb_single_page_flush_ceiling);
 
+/*
+ * Workaround for KNL issue:
+ *
+ * A thread that is going to page fault due to P=0, may still
+ * non atomically set A or D bits, which could corrupt swap entries.
+ * Always flush the other CPUs and clear the PTE again to avoid
+ * this leakage. We are excluded using the pagetable lock.
+ */
+
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}
+
 #endif /* CONFIG_SMP */
include/linux/mm.h +4 -0
index 5df5feb..5c80fe09 100644
@@ -2404,6 +2404,10 @@ static inline bool debug_guardpage_enabled(void) { return false; }
 static inline bool page_is_guard(struct page *page) { return false; }
 #endif /* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef ARCH_HAS_NEEDS_SWAP_PTL
+static inline bool arch_needs_swap_ptl(void) { return false; }
+#endif
+
 #if MAX_NUMNODES > 1
 void __init setup_nr_node_ids(void);
 #else
mm/memory.c +2 -1
index 15322b7..0d6ef39 100644
@@ -1960,7 +1960,8 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
 {
 	int same = 1;
 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-	if (sizeof(pte_t) > sizeof(unsigned long)) {
+	if (arch_needs_swap_ptl() ||
+	    sizeof(pte_t) > sizeof(unsigned long)) {
 		spinlock_t *ptl = pte_lockptr(mm, pmd);
 		spin_lock(ptl);
 		same = pte_same(*page_table, orig_pte);
Signature · 1 line
1.8.3.1
#1 lore raw
url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610 config: i386-alldefconfig (attached as .config) compiler: gcc-6…

Hi,

[auto build test ERROR on v4.7-rc3]
[also build test ERROR on next-20160614]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610
config: i386-alldefconfig (attached as .config)
compiler: gcc-6 (Debian 6.1.1-1) 6.1.1 20160430 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

mm/built-in.o: In function `unmap_page_range':
(.text+0x1e9e8): undefined reference to fix_pte_leak' mm/built-in.o: In function change_protection_range':

>> mprotect.c:(.text+0x25578): undefined reference to `fix_pte_leak'

mm/built-in.o: In function `move_page_tables':
(.text+0x25d81): undefined reference to `fix_pte_leak'
mm/built-in.o: In function `vunmap_page_range':
vmalloc.c:(.text+0x28419): undefined reference to fix_pte_leak' mm/built-in.o: In function ptep_clear_flush':
(.text+0x2a7b3): undefined reference to fix_pte_leak' mm/built-in.o:madvise.c:(.text+0x2b4b0): more undefined references to fix_pte_leak' follow

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines · Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 489

Quoted 8 lines
arch/x86/mm/tlb.c +8 -0
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

Regards,
Nadav

--
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>

#3 lore raw

From: Nadav Amit [mailto:[email protected]]
Sent: Tuesday, June 14, 2016 6:48 PM

Quoted 7 lines

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines · Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Thanks, Nadav!
I'll add this and re-submit the patch.

Cheers,
Lukasz

--
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>

#4 lore raw

PATCH v2 1/1 Linux VM workaround for Knights Landing A/D leak

In reply to Nadav Amit

From: Andi Kleen <[email protected]>

Knights Landing has a issue that a thread setting A or D bits may not do so atomically against checking the present bit. A thread which is going to page fault may still set those bits, even though the present bit was already atomically cleared.

This implies that when the kernel clears present atomically, some time later the supposed to be zero entry could be corrupted with stray A or D bits.

Since the PTE could be already used for storing a swap index, or a NUMA migration index, this cannot be tolerated. Most of the time the kernel detects the problem, but in some rare cases it may not.

This patch enforces that the page unmap path in vmscan/direct reclaim always flushes other CPUs after clearing each page, and also clears the PTE again after the flush.

For reclaim this brings the performance back to before Mel's flushing changes, but for unmap it disables batching.

This makes sure any leaked A/D bits are immediately cleared before the entry is used for something else.

Any parallel faults that check for entry is zero may loop, but they should eventually recover after the entry is written.

Also other users may spin in the page table lock until we "fixed" the PTE. This is ensured by always taking the page table lock even for the swap cache case. Previously this was only done on architectures with non atomic PTE accesses (such as 32bit PTE), but now it is also done when this bug workaround is active.

I audited apply_pte_range and other users of arch_enter_lazy... and they seem to all not clear the present bit.

Right now the extra flush is done in the low level architecture code, while the higher level code still does batched TLB flush. This means there is always one extra unnecessary TLB flush now. As a followon optimization this could be avoided by telling the callers that the flush already happenend.

v2 (Lukasz Anaczkowski):
() added call to smp_mb__after_atomic() to synchornize with
switch_mm, based on Nadav's comment
() fixed compilation breakage

---
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/hugetlb.h     |  9 ++++++++-
 arch/x86/include/asm/pgtable.h     |  5 +++++
 arch/x86/include/asm/pgtable_64.h  |  6 ++++++
 arch/x86/kernel/cpu/intel.c        | 10 ++++++++++
 arch/x86/mm/tlb.c                  | 22 ++++++++++++++++++++++
 include/linux/mm.h                 |  4 ++++
 mm/memory.c                        |  3 ++-
 8 files changed, 58 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
index 4a41348..2c48011 100644
@@ -303,6 +303,7 @@
 #define X86_BUG_SYSRET_SS_ATTRS	X86_BUG(8) /* SYSRET doesn't fix up SS attrs */
 #define X86_BUG_NULL_SEG	X86_BUG(9) /* Nulling a selector preserves the base */
 #define X86_BUG_SWAPGS_FENCE	X86_BUG(10) /* SWAPGS without input dep on GS */
+#define X86_BUG_PTE_LEAK        X86_BUG(11) /* PTE may leak A/D bits after clear */
 
 
 #ifdef CONFIG_X86_32
arch/x86/include/asm/hugetlb.h +8 -1
index 3a10616..58e1ca9 100644
@@ -41,10 +41,17 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
 	set_pte_at(mm, addr, ptep, pte);
 }
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 					    unsigned long addr, pte_t *ptep)
 {
-	return ptep_get_and_clear(mm, addr, ptep);
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;
 }
 
 static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/x86/include/asm/pgtable.h +5 -0
index 1a27396..9769355 100644
@@ -794,11 +794,16 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
 				  unsigned long address, pte_t *ptep);
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
 	pte_t pte = native_ptep_get_and_clear(ptep);
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
 	pte_update(mm, addr, ptep);
 	return pte;
 }
arch/x86/include/asm/pgtable_64.h +6 -0
index 2ee7811..6fa4079 100644
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/kernel/cpu/intel.c +10 -0
index 6e2ffbe..f499513 100644
@@ -181,6 +181,16 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 		}
 	}
 
+	if (c->x86_model == 87) {
+		static bool printed;
+
+		if (!printed) {
+			pr_info("Enabling PTE leaking workaround\n");
+			printed = true;
+		}
+		set_cpu_bug(c, X86_BUG_PTE_LEAK);
+	}
+
 	/*
 	 * Intel Quark Core DevMan_001.pdf section 6.4.11
 	 * "The operating system also is required to invalidate (i.e., flush)
arch/x86/mm/tlb.c +22 -0
index 5643fd0..9b4c575 100644
@@ -469,3 +469,25 @@ static int __init create_tlb_single_page_flush_ceiling(void)
 late_initcall(create_tlb_single_page_flush_ceiling);
 
 #endif /* CONFIG_SMP */
+
+/*
+ * Workaround for KNL issue:
+ *
+ * A thread that is going to page fault due to P=0, may still
+ * non atomically set A or D bits, which could corrupt swap entries.
+ * Always flush the other CPUs and clear the PTE again to avoid
+ * this leakage. We are excluded using the pagetable lock.
+ */
+
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	smp_mb__after_atomic();
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}
+
include/linux/mm.h +4 -0
index 5df5feb..5c80fe09 100644
@@ -2404,6 +2404,10 @@ static inline bool debug_guardpage_enabled(void) { return false; }
 static inline bool page_is_guard(struct page *page) { return false; }
 #endif /* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef ARCH_HAS_NEEDS_SWAP_PTL
+static inline bool arch_needs_swap_ptl(void) { return false; }
+#endif
+
 #if MAX_NUMNODES > 1
 void __init setup_nr_node_ids(void);
 #else
mm/memory.c +2 -1
index 15322b7..0d6ef39 100644
@@ -1960,7 +1960,8 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
 {
 	int same = 1;
 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-	if (sizeof(pte_t) > sizeof(unsigned long)) {
+	if (arch_needs_swap_ptl() ||
+	    sizeof(pte_t) > sizeof(unsigned long)) {
 		spinlock_t *ptl = pte_lockptr(mm, pmd);
 		spin_lock(ptl);
 		same = pte_same(*page_table, orig_pte);
Signature · 7 lines
1.8.3.1

--
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>
maintainer
#5 lore raw
Quoted 2 lines · Lukasz Anaczkowski

v2 (Lukasz Anaczkowski):
() fixed compilation breakage

...

By unconditionally defining the workaround code, even on kernels where there is no chance of ever hitting this bug. I think that's a pretty poor way to do it.

Can we please stick this in one of the intel.c files, so it's only present on CPU_SUP_INTEL builds?

Which reminds me...

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void); · line 185

Quoted 11 lines
arch/x86/include/asm/pgtable_64.h +5 -0
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);
+}

Does this *REALLY* only affect 64-bit kernels?

--
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>

#6 lore raw
Quoted 11 lines · On Tue, 14 Jun 2016 10:24:16 -0700 Dave Hansen <[email protected]>…
Quoted 2 lines · Lukasz Anaczkowski

v2 (Lukasz Anaczkowski):
() fixed compilation breakage

...

By unconditionally defining the workaround code, even on kernels where there is no chance of ever hitting this bug. I think that's a pretty poor way to do it.

Can we please stick this in one of the intel.c files, so it's only present on CPU_SUP_INTEL builds?

Can we please make it use alternatives or somesuch so that it just goes away at boot if its not a Knights Landing box ?

Alan

--
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>

maintainer
#7 lore raw
Quoted 12 lines · One Thousand Gnomes
Quoted 9 lines · On Tue, 14 Jun 2016 10:24:16 -0700 Dave Hansen <[email protected]>…
Quoted 2 lines · Lukasz Anaczkowski

v2 (Lukasz Anaczkowski):
() fixed compilation breakage

...

By unconditionally defining the workaround code, even on kernels where there is no chance of ever hitting this bug. I think that's a pretty poor way to do it.

Quoted 5 lines
Quoted 2 lines

Can we please stick this in one of the intel.c files, so it's only present on CPU_SUP_INTEL builds?

Can we please make it use alternatives or somesuch so that it just goes away at boot if its not a Knights Landing box ?

Lukasz, Borislav suggested using static_cpu_has_bug(), which will do the alternatives patching. It's definitely the right thing to use here.

maintainer
#8 lore raw
Quoted 2 lines · Dave Hansen

Lukasz, Borislav suggested using static_cpu_has_bug(), which will do the alternatives patching. It's definitely the right thing to use here.

Yeah, either that or do an

alternative_call(null_func, fix_pte_peak, X86_BUG_PTE_LEAK, ...)

or so and you'll need a dummy function to call on !X86_BUG_PTE_LEAK CPUs.

The static_cpu_has_bug() thing should be most likely a penalty of a single JMP (I have to look at the asm) but then since the callers are inlined, you'll have to patch all those places where *ptep_get_and_clear() get inlined.

Shouldn't be a big deal still but...

"debug-alternative" and a kvm guest should help you there to get a quick idea.

HTH.

Signature · 10 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.

--
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>
#9 lore raw
Quoted 16 lines · Borislav Petkov
Quoted 2 lines · Dave Hansen

Lukasz, Borislav suggested using static_cpu_has_bug(), which will do the alternatives patching. It's definitely the right thing to use here.

Yeah, either that or do an

alternative_call(null_func, fix_pte_peak, X86_BUG_PTE_LEAK, ...)

or so and you'll need a dummy function to call on !X86_BUG_PTE_LEAK CPUs.

The static_cpu_has_bug() thing should be most likely a penalty of a single JMP (I have to look at the asm) but then since the callers are inlined, you'll have to patch all those places where *ptep_get_and_clear() get inlined.

Quoted 5 lines

Shouldn't be a big deal still but...

"debug-alternative" and a kvm guest should help you there to get a quick idea.

static_cpu_has_bug() should turn into 5-byte NOP in the common (bugless) case.

-hpa

maintainer
#10 lore raw
Quoted 2 lines · H. Peter Anvin

static_cpu_has_bug() should turn into 5-byte NOP in the common (bugless) case.

Yeah, it does. I looked at the asm.

I wasn't 100% sure because I vaguely remember gcc reordering things in some pathological case but I'm most likely remembering wrong because if it were doing that, then the whole nopping out won't work. F'get about it. :)

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#11 lore raw
Quoted 11 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin

static_cpu_has_bug() should turn into 5-byte NOP in the common (bugless) case.

Yeah, it does. I looked at the asm.

I wasn't 100% sure because I vaguely remember gcc reordering things in some pathological case but I'm most likely remembering wrong because if it were doing that, then the whole nopping out won't work. F'get about it. :)

There was that. It is still possible that we end up with NOP a JMP right before another JMP; we could perhaps make the patching code smarter and see if we have a JMP immediately after.

-hpa

maintainer
#12 lore raw
Quoted 3 lines · H. Peter Anvin

There was that. It is still possible that we end up with NOP a JMP right before another JMP; we could perhaps make the patching code smarter and see if we have a JMP immediately after.

Yeah, I still can't get reproduce that reliably - I remember seeing it at some point but then dismissing it for another, higher-prio thing. And now the whole memory is hazy at best.

But, you're giving me a great idea right now - I have this kernel disassembler tool which dumps alternative sections already and I could teach it to look for pathological cases around the patching sites and scream.

Something for my TODO list when I get a quiet moment.

Thanks!

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#13 lore raw
Quoted 9 lines · Borislav Petkov
Quoted 3 lines · H. Peter Anvin

There was that. It is still possible that we end up with NOP a JMP right before another JMP; we could perhaps make the patching code smarter and see if we have a JMP immediately after.

Yeah, I still can't get reproduce that reliably - I remember seeing it at some point but then dismissing it for another, higher-prio thing. And now the whole memory is hazy at best.

Quoted 7 lines

But, you're giving me a great idea right now - I have this kernel disassembler tool which dumps alternative sections already and I could teach it to look for pathological cases around the patching sites and scream.

Something for my TODO list when I get a quiet moment.

It's not really pathological; the issue is that asm goto() with an unreachable clause after it doesn't tell gcc that a certain code path ought to be linear, so we tell it to fall through. However, if gcc then wants to have a jump there for whatever reason (perhaps it is part of a loop) we end up with a redundant jump, so a patch site followed by a JMP is entirely reasonable.

-hpa

#14 lore raw
In reply to Borislav Petkov
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · H. Peter Anvin

There was that. It is still possible that we end up with NOP a JMP right before another JMP; we could perhaps make the patching code smarter and see if we have a JMP immediately after.

Yeah, I still can't get reproduce that reliably - I remember seeing it at some point but then dismissing it for another, higher-prio thing. And
now the whole memory is hazy at best.

Quoted 8 lines

But, you're giving me a great idea right now - I have this kernel disassembler tool which dumps alternative sections already and I could teach it to look for pathological cases around the patching sites and scream.

Something for my TODO list when I get a quiet moment.

Thanks!

We talked with the GCC people about always bias asm goto toward the first label even if followed by __builtin_unreachable(). I don't know if that happened; if so we should probably insert the unreachable for those versions of gcc only.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse brevity and formatting.
maintainer
#15 lore raw
In reply to Lukasz Anaczkowski
Quoted 83 lines · Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

Knights Landing has a issue that a thread setting A or D bits may not do so atomically against checking the present bit. A thread which is going to page fault may still set those bits, even though the present bit was already atomically cleared.

This implies that when the kernel clears present atomically, some time later the supposed to be zero entry could be corrupted with stray A or D bits.

Since the PTE could be already used for storing a swap index, or a NUMA migration index, this cannot be tolerated. Most of the time the kernel detects the problem, but in some rare cases it may not.

This patch enforces that the page unmap path in vmscan/direct reclaim always flushes other CPUs after clearing each page, and also clears the PTE again after the flush.

For reclaim this brings the performance back to before Mel's flushing changes, but for unmap it disables batching.

This makes sure any leaked A/D bits are immediately cleared before the entry is used for something else.

Any parallel faults that check for entry is zero may loop, but they should eventually recover after the entry is written.

Also other users may spin in the page table lock until we "fixed" the PTE. This is ensured by always taking the page table lock even for the swap cache case. Previously this was only done on architectures with non atomic PTE accesses (such as 32bit PTE), but now it is also done when this bug workaround is active.

I audited apply_pte_range and other users of arch_enter_lazy... and they seem to all not clear the present bit.

Right now the extra flush is done in the low level architecture code, while the higher level code still does batched TLB flush. This means there is always one extra unnecessary TLB flush now. As a followon optimization this could be avoided by telling the callers that the flush already happenend.

v2 (Lukasz Anaczkowski):
() added call to smp_mb__after_atomic() to synchornize with
switch_mm, based on Nadav's comment
() fixed compilation breakage

---
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/hugetlb.h     |  9 ++++++++-
 arch/x86/include/asm/pgtable.h     |  5 +++++
 arch/x86/include/asm/pgtable_64.h  |  6 ++++++
 arch/x86/kernel/cpu/intel.c        | 10 ++++++++++
 arch/x86/mm/tlb.c                  | 22 ++++++++++++++++++++++
 include/linux/mm.h                 |  4 ++++
 mm/memory.c                        |  3 ++-
 8 files changed, 58 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
index 4a41348..2c48011 100644
@@ -303,6 +303,7 @@
 #define X86_BUG_SYSRET_SS_ATTRS	X86_BUG(8) /* SYSRET doesn't fix up SS attrs */
 #define X86_BUG_NULL_SEG	X86_BUG(9) /* Nulling a selector preserves the base */
 #define X86_BUG_SWAPGS_FENCE	X86_BUG(10) /* SWAPGS without input dep on GS */
+#define X86_BUG_PTE_LEAK        X86_BUG(11) /* PTE may leak A/D bits after clear */
 
 
 #ifdef CONFIG_X86_32
arch/x86/include/asm/hugetlb.h +0 -0
index 3a10616..58e1ca9 100644
@@ -41,10 +41,17 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
 	set_pte_at(mm, addr, ptep, pte);
 }
 

arch/x86/include/asm/hugetlb.h static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, · line 52

Quoted 10 lines
arch/x86/include/asm/hugetlb.h +6 -1
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 					    unsigned long addr, pte_t *ptep)
 {
-	return ptep_get_and_clear(mm, addr, ptep);
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))

static_cpu_has_bug()

Quoted 12 lines
arch/x86/include/asm/hugetlb.h +2 -0
+		fix_pte_leak(mm, addr, ptep);
+	return pte;
 }
 
 static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/x86/include/asm/pgtable.h +0 -0
index 1a27396..9769355 100644
@@ -794,11 +794,16 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
 				  unsigned long address, pte_t *ptep);

arch/x86/include/asm/pgtable.h extern int ptep_test_and_clear_young(struct vm_area_struct *vma, · line 805

Quoted 10 lines
arch/x86/include/asm/pgtable.h +4 -0
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
 	pte_t pte = native_ptep_get_and_clear(ptep);
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))

static_cpu_has_bug()

Quoted 9 lines
arch/x86/include/asm/pgtable.h +1 -0
+		fix_pte_leak(mm, addr, ptep);
 	pte_update(mm, addr, ptep);
 	return pte;
 }
arch/x86/include/asm/pgtable_64.h +0 -0
index 2ee7811..6fa4079 100644

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void); · line 184

Quoted 7 lines
arch/x86/include/asm/pgtable_64.h +4 -0
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);

static_cpu_has_bug()

Quoted 10 lines
arch/x86/include/asm/pgtable_64.h +2 -0
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/kernel/cpu/intel.c +0 -0
index 6e2ffbe..f499513 100644

arch/x86/kernel/cpu/intel.c static void early_init_intel(struct cpuinfo_x86 *c) · line 184

Quoted 4 lines
arch/x86/kernel/cpu/intel.c +1 -0
@@ -181,6 +181,16 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 		}
 	}
 
+	if (c->x86_model == 87) {

That should be INTEL_FAM6_XEON_PHI_KNL, AFAICT.

arch/x86/kernel/cpu/intel.c static void early_init_intel(struct cpuinfo_x86 *c) · line 190

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +6 -0
+		static bool printed;
+
+		if (!printed) {
+			pr_info("Enabling PTE leaking workaround\n");
+			printed = true;
+		}

pr_info_once

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +3 -0
+		set_cpu_bug(c, X86_BUG_PTE_LEAK);
+	}
+
 	/*
 	 * Intel Quark Core DevMan_001.pdf section 6.4.11
 	 * "The operating system also is required to invalidate (i.e., flush)
Signature · 10 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.

--
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>
#16 lore raw

From: Borislav Petkov [mailto:[email protected]]
Sent: Tuesday, June 14, 2016 8:10 PM

Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))

static_cpu_has_bug()

arch/x86/kernel/cpu/intel.c static void early_init_intel(struct cpuinfo_x86 *c) · line 184

Quoted 1 line
arch/x86/kernel/cpu/intel.c +1 -0
+	if (c->x86_model == 87) {

That should be INTEL_FAM6_XEON_PHI_KNL, AFAICT.

Quoted 8 lines

arch/x86/kernel/cpu/intel.c static void early_init_intel(struct cpuinfo_x86 *c) · line 190

Quoted 6 lines
arch/x86/kernel/cpu/intel.c +6 -0
+		static bool printed;
+
+		if (!printed) {
+			pr_info("Enabling PTE leaking workaround\n");
+			printed = true;
+		}

pr_info_once

Thanks, Boris! This is very valuable. I'll address those comments in next version of the patch.

Cheers,
Lukasz

#17 lore raw
In reply to Lukasz Anaczkowski
Quoted 11 lines · Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
unsigned long addr, pte_t *ptep)
{

arch/x86/include/asm/hugetlb.h +5 -1
-	return ptep_get_and_clear(mm, addr, ptep);
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;
Quoted 1 line

}

I missed it on the previous iteration: ptep_get_and_clear already calls fix_pte_leak when needed. So do you need to call it again here?

Thanks,
Nadav

--
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>

#18 lore raw

From: Nadav Amit [mailto:[email protected]]
Sent: Tuesday, June 14, 2016 8:38 PM

Quoted 9 lines
Quoted 6 lines
arch/x86/include/asm/hugetlb.h +5 -0
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;

}

I missed it on the previous iteration: ptep_get_and_clear already calls fix_pte_leak when needed. So do you need to call it again here?

You're right, Nadav. Not needing this. Will be removed in next version of the patch.

Cheers,
Lukasz

#19 lore raw
Quoted 13 lines · Lukasz

From: Nadav Amit [mailto:[email protected]]
Sent: Tuesday, June 14, 2016 8:38 PM

Quoted 9 lines
Quoted 6 lines
arch/x86/include/asm/hugetlb.h +5 -0
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;

}

I missed it on the previous iteration: ptep_get_and_clear already calls fix_pte_leak when needed. So do you need to call it again here?

Quoted 1 line

You're right, Nadav. Not needing this. Will be removed in next version of the patch.

Be careful here. According to the SDM when invalidating a huge-page, each 4KB page needs to be invalidated separately. In practice, when Linux invalidates 2MB/1GB pages it performs a full TLB flush. The full flush may not be required on knights landing, and specifically for the workaround, but you should check.

Regards,
Nadav
--
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>

maintainer
#20 lore raw
Quoted 5 lines · Nadav Amit

Be careful here. According to the SDM when invalidating a huge-page, each 4KB page needs to be invalidated separately. In practice, when Linux invalidates 2MB/1GB pages it performs a full TLB flush. The full flush may not be required on knights landing, and specifically for the workaround, but you should check.

Where do you get that? The SDM says: "they (TLB invalidation operations invalidate all TLB entries corresponding to the translation specified by the paging structures."

Here's the full paragraph from the SDM

... some processors may choose to cache multiple smaller-page TLB entries for a translation specified by the paging structures to use a page larger than 4 KBytes. There is no way for software to be aware that multiple translations for smaller pages have been used for a large page. The INVLPG instruction and page faults provide the same assurances that they provide when a single TLB entry is used: they invalidate all TLB entries corresponding to the translation specified by the paging structures.

--
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>

#21 lore raw
Quoted 10 lines · Dave Hansen
Quoted 5 lines · Nadav Amit

Be careful here. According to the SDM when invalidating a huge-page, each 4KB page needs to be invalidated separately. In practice, when Linux invalidates 2MB/1GB pages it performs a full TLB flush. The full flush may not be required on knights landing, and specifically for the workaround, but you should check.

Where do you get that? The SDM says: "they (TLB invalidation operations invalidate all TLB entries corresponding to the translation specified by the paging structures.”

You are absolutely correct. Last time I write something based on my recollection of the SDM without re-reading again. Sorry.

Nadav

--
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>

#22 lore raw

PATCH v3 1/1 Linux VM workaround for Knights Landing A/D leak

In reply to Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

Knights Landing has an issue that a thread setting A or D bits may not do so atomically against checking the present bit. A thread which is going to page fault may still set those bits, even though the present bit was already atomically cleared.

This implies that when the kernel clears present atomically, some time later the supposed to be zero entry could be corrupted with stray A or D bits.

Since the PTE could be already used for storing a swap index, or a NUMA migration index, this cannot be tolerated. Most of the time the kernel detects the problem, but in some rare cases it may not.

This patch enforces that the page unmap path in vmscan/direct reclaim always flushes other CPUs after clearing each page, and also clears the PTE again after the flush. A new memory barrier may be required, but this code is at least consistent with all the existing uses in the kernel. If we decide that we need a new barrier, it can added at the same time as the rest of the tree.

For reclaim this brings the performance back to before Mel's flushing changes, but for unmap it disables batching.

This makes sure any leaked A/D bits are immediately cleared before the entry is used for something else.

Any parallel faults that check for entry is zero may loop, but they should eventually recover after the entry is written.

Also other users may spin in the page table lock until we "fixed" the PTE. This is ensured by always taking the page table lock even for the swap cache case. Previously this was only done on architectures with non atomic PTE accesses (such as 32bit PTE), but now it is also done when this bug workaround is active.

I audited apply_pte_range and other users of arch_enter_lazy... and they seem to all not clear the present bit.

Right now the extra flush is done in the low level architecture code, while the higher level code still does batched TLB flush. This means there is always one extra unnecessary TLB flush now. As a followon optimization this could be avoided by telling the callers that the flush already happenend.

The official erratum will be posted hopefully by the end of July'16.

v3 (Lukasz Anaczkowski):
() Improved documentation
() Removed unnecessary declaration and call to fix_pte_leak from hugetlb.h () Moved fix_pte_leak() definition from tlb.c to intel.c
() Replaced boot_cpu_has_bug() with static_cpu_has_bug()
() pr_info_once instead of pr_info
() Fix applies only to 64-bit kernels as Knights Landing does not
support 32-bit kernels

v2 (Lukasz Anaczkowski):
() added call to smp_mb__after_atomic() to synchornize with
switch_mm, based on Nadav's comment
() fixed compilation breakage

---
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/pgtable.h     | 10 ++++++++++
 arch/x86/include/asm/pgtable_64.h  | 16 +++++++++++++++
 arch/x86/kernel/cpu/intel.c        | 41 ++++++++++++++++++++++++++++++++++++++
 include/linux/mm.h                 |  4 ++++
 mm/memory.c                        |  3 ++-
 6 files changed, 74 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
index 4a41348..2c48011 100644
@@ -303,6 +303,7 @@
 #define X86_BUG_SYSRET_SS_ATTRS	X86_BUG(8) /* SYSRET doesn't fix up SS attrs */
 #define X86_BUG_NULL_SEG	X86_BUG(9) /* Nulling a selector preserves the base */
 #define X86_BUG_SWAPGS_FENCE	X86_BUG(10) /* SWAPGS without input dep on GS */
+#define X86_BUG_PTE_LEAK        X86_BUG(11) /* PTE may leak A/D bits after clear */
 
 
 #ifdef CONFIG_X86_32
arch/x86/include/asm/pgtable.h +10 -0
index 1a27396..14abcb3 100644
@@ -794,11 +794,21 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
 				  unsigned long address, pte_t *ptep);
 
+#if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_INTEL)
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+#else
+static inline void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+				pte_t *ptep) {}
+#endif
+
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
 	pte_t pte = native_ptep_get_and_clear(ptep);
+	if (static_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
 	pte_update(mm, addr, ptep);
 	return pte;
 }
arch/x86/include/asm/pgtable_64.h +16 -0
index 2ee7811..be7d63c 100644
@@ -178,6 +178,22 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+/*
+ * Intel Xeon Phi x200 codenamed Knights Landing has an issue that a thread
+ * setting A or D bits may not do so atomically against checking the present
+ * bit. A thread which is going to page fault may still set those
+ * bits, even though the present bit was already atomically cleared.
+ *
+ * This implies that when the kernel clears present atomically,
+ * some time later the supposed to be zero entry could be corrupted
+ * with stray A or D bits.
+ */
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return static_cpu_has_bug(X86_BUG_PTE_LEAK);
+}
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/kernel/cpu/intel.c +41 -0

65 lines not on this page · raw message

include/linux/mm.h +4 -0
index 5df5feb..5c80fe09 100644
@@ -2404,6 +2404,10 @@ static inline bool debug_guardpage_enabled(void) { return false; }
 static inline bool page_is_guard(struct page *page) { return false; }
 #endif /* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef ARCH_HAS_NEEDS_SWAP_PTL
+static inline bool arch_needs_swap_ptl(void) { return false; }
+#endif
+
 #if MAX_NUMNODES > 1
 void __init setup_nr_node_ids(void);
 #else
mm/memory.c +2 -1
index 15322b7..0d6ef39 100644
@@ -1960,7 +1960,8 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
 {
 	int same = 1;
 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-	if (sizeof(pte_t) > sizeof(unsigned long)) {
+	if (arch_needs_swap_ptl() ||
+	    sizeof(pte_t) > sizeof(unsigned long)) {
 		spinlock_t *ptl = pte_lockptr(mm, pmd);
 		spin_lock(ptl);
 		same = pte_same(*page_table, orig_pte);
Signature · 7 lines
1.8.3.1

--
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>
#23 lore raw

arch/x86/kernel/cpu/intel.c void check_mpx_erratum(struct cpuinfo_x86 *c) · line 84

Quoted 6 lines · Lukasz Anaczkowski

From: Andi Kleen <[email protected]>

arch/x86/kernel/cpu/intel.c +4 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);

This tracing seems incorrect since you don’t perform a local flush. I don’t think you need any tracing - native_flush_tlb_others will do it for you.

arch/x86/kernel/cpu/intel.c void check_mpx_erratum(struct cpuinfo_x86 *c) · line 87

Quoted 3 lines
arch/x86/kernel/cpu/intel.c +3 -0
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();

Why do you need the memory barrier?

Regards,
Nadav
--
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>

maintainer
#24 lore raw
In reply to Lukasz Anaczkowski
Quoted 2 lines · Lukasz Anaczkowski

For reclaim this brings the performance back to before Mel's flushing changes, but for unmap it disables batching.

This turns out to be pretty catastrophic for unmap. In a workload that uses, say 200 hardware threads and alloc/frees() a few MB/sec, this ends up costing hundreds of thousands of extra received IPIs. 10MB=~2500 ptes, and at with 200 threads, that's 250,000 IPIs received just to free 10MB of memory.

The initial testing we did on this was on a *bunch* of threads all doing alloc/free. But this is bottlenecked on other things, like mmap_sem being held for write.

The scenario that we really needed to test here was on lots of threads doing processing and 1 thread doing alloc/free.

maintainer
#25 lore raw
In reply to Nadav Amit
Quoted 8 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 489

Quoted 8 lines
arch/x86/mm/tlb.c +8 -0
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

--
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>

#26 lore raw
Quoted 21 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 489

Quoted 8 lines
arch/x86/mm/tlb.c +8 -0
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

Quoted 2 lines

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

In theory, fix_pte_leak could have used flush_tlb_page. But the problem is that flush_tlb_page requires the vm_area_struct as an argument, which ptep_get_and_clear (and others) do not have.

I don’t know which architecture needs the vm_area_struct, since x86 and some others I looked at (e.g., ARM) only need the mm_struct.

Nadav
--
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>

maintainer
#27 lore raw
Quoted 23 lines · Nadav Amit
Quoted 21 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 489

Quoted 8 lines
arch/x86/mm/tlb.c +8 -0
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

Quoted 5 lines
Quoted 2 lines

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
	/* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
	return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
		nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See

	 * switch_mm().
	 */
	smp_mb__after_atomic();

	return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

Right?

Quoted 3 lines

In theory, fix_pte_leak could have used flush_tlb_page. But the problem is that flush_tlb_page requires the vm_area_struct as an argument, which ptep_get_and_clear (and others) do not have.

That, and we do not want/need to flush the _current_ processor's TLB. flush_tlb_page() would have done that unnecessarily. That's not the end of the world here, but it is a downside.

--
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>

#28 lore raw
Quoted 59 lines · Dave Hansen
Quoted 20 lines · Nadav Amit
Quoted 18 lines · Dave Hansen
Quoted 16 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

+ if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {

arch/x86/mm/tlb.c +7 -0
+         trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+         flush_tlb_others(mm_cpumask(mm), mm, addr,
+                          addr + PAGE_SIZE);
+         mb();
+         set_pte(ptep, __pte(0));
+ }
+}
Quoted 8 lines
Quoted 5 lines

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
        /* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
        return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
                nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 6 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

IMO this is a bit nuts. smp_mb__after_atomic() doesn't do anything on x86. And, even if it did, why should the flush code assume that the previous store was atomic?

What's the issue being fixed / worked around here?

--
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>

#29 lore raw
Quoted 68 lines · Andy Lutomirski
Quoted 59 lines · Dave Hansen
Quoted 20 lines · Nadav Amit
Quoted 18 lines · Dave Hansen
Quoted 16 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

+ if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {

arch/x86/mm/tlb.c +7 -0
+         trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+         flush_tlb_others(mm_cpumask(mm), mm, addr,
+                          addr + PAGE_SIZE);
+         mb();
+         set_pte(ptep, __pte(0));
+ }
+}
Quoted 8 lines
Quoted 5 lines

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
       /* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
       return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
               nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 6 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

Quoted 5 lines

IMO this is a bit nuts. smp_mb__after_atomic() doesn't do anything on x86. And, even if it did, why should the flush code assume that the previous store was atomic?

What's the issue being fixed / worked around here?

It does a compiler barrier, which prevents the decision whether a remote TLB shootdown is required to be made before the PTE is set.

I agree that PTEs may not be written atomically in certain cases (although I am unaware of such cases, except on full-mm flush).

Having said that, I think that all the TLB flush/shootdown logic should not be open-coded at all and be left in the arch-specific implementation. People keep making small mistakes when they reimplement the flushing logic.

This patch, for example, also has a bug in the way it traces the flush - it marks full flush, when it flushes a single page:

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);

Regards,
Nadav
--
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>

#30 lore raw
Quoted 74 lines · Nadav Amit
Quoted 72 lines · Andy Lutomirski
Quoted 59 lines · Dave Hansen
Quoted 20 lines · Nadav Amit
Quoted 18 lines · Dave Hansen
Quoted 16 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

Review

Quoted 3 lines

From: Andi Kleen <[email protected]>

(quoted diff) +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

+ if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {

(quoted diff) +7 -0
+         trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+         flush_tlb_others(mm_cpumask(mm), mm, addr,
+                          addr + PAGE_SIZE);
+         mb();
+         set_pte(ptep, __pte(0));
+ }
+}
Quoted 8 lines
Quoted 5 lines

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
       /* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
       return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
               nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 6 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

IMO this is a bit nuts. smp_mb__after_atomic() doesn't do anything on x86. And, even if it did, why should the flush code assume that the previous store was atomic?

Quoted 7 lines
Quoted 1 line

What's the issue being fixed / worked around here?

It does a compiler barrier, which prevents the decision whether a remote TLB shootdown is required to be made before the PTE is set.

I agree that PTEs may not be written atomically in certain cases (although I am unaware of such cases, except on full-mm flush).

How about plain set_pte? It's atomic (aligned word-sized write), but it's not atomic in the _after_atomic sense.

--Andy

--
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>

#31 lore raw
Quoted 77 lines · Andy Lutomirski
Quoted 76 lines · Nadav Amit
Quoted 73 lines · Andy Lutomirski
Quoted 59 lines · Dave Hansen
Quoted 20 lines · Nadav Amit
Quoted 18 lines · Dave Hansen
Quoted 16 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

Review

Quoted 3 lines

From: Andi Kleen <[email protected]>

(quoted diff) +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

+ if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {

(quoted diff) +7 -0
+         trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+         flush_tlb_others(mm_cpumask(mm), mm, addr,
+                          addr + PAGE_SIZE);
+         mb();
+         set_pte(ptep, __pte(0));
+ }
+}
Quoted 8 lines
Quoted 5 lines

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
      /* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
      return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
              nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 6 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

IMO this is a bit nuts. smp_mb__after_atomic() doesn't do anything on x86. And, even if it did, why should the flush code assume that the previous store was atomic?

What's the issue being fixed / worked around here?

Quoted 8 lines
Quoted 5 lines

It does a compiler barrier, which prevents the decision whether a remote TLB shootdown is required to be made before the PTE is set.

I agree that PTEs may not be written atomically in certain cases (although I am unaware of such cases, except on full-mm flush).

How about plain set_pte? It's atomic (aligned word-sized write), but it's not atomic in the _after_atomic sense.

Can you point me to a place where set_pte is used before a TLB invalidation/shootdown, excluding this patch and the fullmm case?

I am not claiming there is no such case, but I am unaware of such one. PTEs are cleared on SMP using xchg, and similarly the dirty bit is cleared with an atomic operation.

--
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>

#32 lore raw
Quoted 88 lines · Nadav Amit
Quoted 77 lines · Andy Lutomirski
Quoted 76 lines · Nadav Amit
Quoted 73 lines · Andy Lutomirski
Quoted 59 lines · Dave Hansen
Quoted 20 lines · Nadav Amit
Quoted 18 lines · Dave Hansen
Quoted 16 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

Review

Quoted 3 lines

From: Andi Kleen <[email protected]>

(quoted diff) +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

+ if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {

(quoted diff) +7 -0
+         trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+         flush_tlb_others(mm_cpumask(mm), mm, addr,
+                          addr + PAGE_SIZE);
+         mb();
+         set_pte(ptep, __pte(0));
+ }
+}
Quoted 8 lines
Quoted 5 lines

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
      /* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
      return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
              nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 6 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

IMO this is a bit nuts. smp_mb__after_atomic() doesn't do anything on x86. And, even if it did, why should the flush code assume that the previous store was atomic?

What's the issue being fixed / worked around here?

Quoted 8 lines
Quoted 5 lines

It does a compiler barrier, which prevents the decision whether a remote TLB shootdown is required to be made before the PTE is set.

I agree that PTEs may not be written atomically in certain cases (although I am unaware of such cases, except on full-mm flush).

How about plain set_pte? It's atomic (aligned word-sized write), but it's not atomic in the _after_atomic sense.

Quoted 7 lines

Can you point me to a place where set_pte is used before a TLB invalidation/shootdown, excluding this patch and the fullmm case?

I am not claiming there is no such case, but I am unaware of such one. PTEs are cleared on SMP using xchg, and similarly the dirty bit is cleared with an atomic operation.

Hmm, you may be right. I still think this is all disgusting, but I don't have any better ideas.

--Andy

Signature · 8 lines
Andy Lutomirski
AMA Capital Management, LLC

--
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>
#33 lore raw
In reply to Dave Hansen
Quoted 59 lines · Dave Hansen
Quoted 23 lines · Nadav Amit
Quoted 21 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 3 lines · Lukasz Anaczkowski

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 481

Quoted 3 lines

From: Andi Kleen <[email protected]>

arch/x86/mm/tlb.c +2 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{

Here there should be a call to smp_mb__after_atomic() to synchronize with switch_mm. I submitted a similar patch, which is still pending (hint).

Quoted 8 lines
Quoted 8 lines

arch/x86/mm/tlb.c static int __init create_tlb_single_page_flush_ceiling(void) · line 489

Quoted 8 lines
arch/x86/mm/tlb.c +8 -0
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

Shouldn't that barrier be incorporated in the TLB flush code itself and not every single caller (like this code is)?

Quoted 5 lines
Quoted 2 lines

It is insane to require individual TLB flushers to be concerned with the barriers.

IMHO it is best to use existing flushing interfaces instead of creating new ones.

Yeah, or make these things a _little_ harder to get wrong. That little snippet above isn't so crazy that we should be depending on open-coded barriers to get it right.

Should we just add a barrier to mm_cpumask() itself? That should stop the race. Or maybe we need a new primitive like:

/*
* Call this if a full barrier has been executed since the last
* pagetable modification operation.
*/
static int __other_cpus_need_tlb_flush(struct mm_struct *mm)

{
	/* cpumask_any_but() returns >= nr_cpu_ids if no cpus set. */
	return cpumask_any_but(mm_cpumask(mm), smp_processor_id()) <
		nr_cpu_ids;
}

static int other_cpus_need_tlb_flush(struct mm_struct *mm) {
/*
* Synchronizes with switch_mm. Makes sure that we do not
* observe a bit having been cleared in mm_cpumask() before
* the other processor has seen our pagetable update. See
* switch_mm().
*/
smp_mb__after_atomic();

Quoted 8 lines

return __other_cpus_need_tlb_flush(mm)
}

We should be able to deploy other_cpus_need_tlb_flush() in most of the cases where we are doing "cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids".

Right?

This approach may work, but I doubt other_cpus_need_tlb_flush() would be needed by anyone, excluding this "hacky" workaround. There are already five interfaces for invalidation of: a single page, a userspace range, a whole task, a kernel range, and full flush including kernel (global) entries.

Quoted 8 lines
Quoted 4 lines

In theory, fix_pte_leak could have used flush_tlb_page. But the problem is that flush_tlb_page requires the vm_area_struct as an argument, which ptep_get_and_clear (and others) do not have.

That, and we do not want/need to flush the _current_ processor's TLB. flush_tlb_page() would have done that unnecessarily. That's not the end of the world here, but it is a downside.

Oops, I missed the fact a local flush is not needed in this case.

Nadav
--
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>

#34 lore raw
In reply to Lukasz Anaczkowski
url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610 config: i386-randconfig-s0-201624 (attached as .config) compiler…

Hi,

[auto build test ERROR on v4.7-rc3]
[also build test ERROR on next-20160614]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610
config: i386-randconfig-s0-201624 (attached as .config)
compiler: gcc-6 (Debian 6.1.1-1) 6.1.1 20160430 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

arch/x86/built-in.o: In function `__ptep_modify_prot_start':

>> paravirt.c:(.text+0x346f4): undefined reference to `fix_pte_leak'
   mm/built-in.o: In function `unmap_page_range':
>> (.text+0x1dd4c): undefined reference to `fix_pte_leak'
   mm/built-in.o: In function `move_page_tables':
   (.text+0x274f2): undefined reference to `fix_pte_leak'
   mm/built-in.o: In function `vunmap_page_range':
>> vmalloc.c:(.text+0x2aa78): undefined reference to `fix_pte_leak'

mm/built-in.o: In function `ptep_clear_flush':
(.text+0x2cfa7): undefined reference to fix_pte_leak' mm/built-in.o:(.text+0x30dff): more undefined references to fix_pte_leak' follow

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

maintainer
#35 lore raw
In reply to Lukasz Anaczkowski

...

Quoted 26 lines
arch/x86/include/asm/hugetlb.h +8 -1
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+
 static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 					    unsigned long addr, pte_t *ptep)
 {
-	return ptep_get_and_clear(mm, addr, ptep);
+	pte_t pte = ptep_get_and_clear(mm, addr, ptep);
+
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
+	return pte;
 }
 
 static inline void huge_ptep_clear_flush(struct vm_area_struct *vma,
arch/x86/include/asm/pgtable.h +3 -0
index 1a27396..9769355 100644
@@ -794,11 +794,16 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
 				  unsigned long address, pte_t *ptep);
 
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);
+

arch/x86/include/asm/pgtable.h extern int ptep_test_and_clear_young(struct vm_area_struct *vma, · line 809

Quoted 10 lines
arch/x86/include/asm/pgtable.h +2 -0
 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
 	pte_t pte = native_ptep_get_and_clear(ptep);
+	if (boot_cpu_has_bug(X86_BUG_PTE_LEAK))
+		fix_pte_leak(mm, addr, ptep);
 	pte_update(mm, addr, ptep);
 	return pte;
 }

Doesn't hugetlb.h somehow #include pgtable.h? So why double-define fix_pte_leak()?

Quoted 5 lines
arch/x86/include/asm/pgtable_64.h +0 -0
index 2ee7811..6fa4079 100644

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void); · line 187

Quoted 10 lines
arch/x86/include/asm/pgtable_64.h +6 -0
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);
+}
+
 #endif /* !__ASSEMBLY__ */

I think we need a comment on this sucker. I'm not sure we should lean solely on the commit message to record why we need this until the end of time.

Or, refer over to fix_pte_leak() for a full description of what is going on.

Quoted 8 lines
arch/x86/include/asm/pgtable_64.h +0 -0
 #endif /* _ASM_X86_PGTABLE_64_H */
arch/x86/kernel/cpu/intel.c +0 -0
index 6e2ffbe..f499513 100644
@@ -181,6 +181,16 @@ static void early_init_intel(struct cpuinfo_x86 *c)
 		}
 	}

arch/x86/kernel/cpu/intel.c static void early_init_intel(struct cpuinfo_x86 *c) · line 192

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +9 -0
 
+	if (c->x86_model == 87) {
+		static bool printed;
+
+		if (!printed) {
+			pr_info("Enabling PTE leaking workaround\n");
+			printed = true;
+		}
+		set_cpu_bug(c, X86_BUG_PTE_LEAK);
+	}

Please use the macros in here for the model id:

Quoted 1 line

http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/arch/x86/include/asm/intel-family.h

We also probably want to prefix the pr_info() with something like "x86/intel:".

Quoted 2 lines

+/*
+ * Workaround for KNL issue:

Please be specific about what this "KNL issue" *is*. Refer to the public documentation of the erratum, please.

Quoted 6 lines

+ * A thread that is going to page fault due to P=0, may still
+ * non atomically set A or D bits, which could corrupt swap entries.
+ * Always flush the other CPUs and clear the PTE again to avoid
+ * this leakage. We are excluded using the pagetable lock.
+ */

arch/x86/kernel/cpu/intel.c +1 -0
+

arch/x86/kernel/cpu/intel.c

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

I think the comment here is a bit sparse. Can we add some more details, like:

Entering here, the current CPU just cleared the PTE. But, another thread may have raced and set the A or D bits, or be _about_ to set the bits. Shooting their TLB entry down will ensure they see the cleared PTE and will not set A or D.

and by the set_pte():

Clear the PTE one more time, in case the other thread set A/D before we sent the TLB flush.

Quoted 22 lines

#endif /* CONFIG_SMP */

include/linux/mm.h +4 -0
index 5df5feb..5c80fe09 100644
@@ -2404,6 +2404,10 @@ static inline bool debug_guardpage_enabled(void) { return false; }
 static inline bool page_is_guard(struct page *page) { return false; }
 #endif /* CONFIG_DEBUG_PAGEALLOC */
 
+#ifndef ARCH_HAS_NEEDS_SWAP_PTL
+static inline bool arch_needs_swap_ptl(void) { return false; }
+#endif
+
 #if MAX_NUMNODES > 1
 void __init setup_nr_node_ids(void);
 #else
mm/memory.c +0 -0
index 15322b7..0d6ef39 100644

mm/memory.c static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd, · line 1967

Quoted 9 lines
mm/memory.c +2 -1
@@ -1960,7 +1960,8 @@ static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
 {
 	int same = 1;
 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-	if (sizeof(pte_t) > sizeof(unsigned long)) {
+	if (arch_needs_swap_ptl() ||
+	    sizeof(pte_t) > sizeof(unsigned long)) {
 		spinlock_t *ptl = pte_lockptr(mm, pmd);
 		spin_lock(ptl);
 		same = pte_same(*page_table, orig_pte);

--
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>

#36 lore raw

From: Dave Hansen [mailto:[email protected]]
Sent: Tuesday, June 14, 2016 7:20 PM

Quoted 1 line
Quoted 1 line
arch/x86/include/asm/pgtable.h +0 -0

...

Quoted 5 lines

arch/x86/include/asm/pgtable.h

Quoted 2 lines
arch/x86/include/asm/pgtable.h +2 -0
+extern void fix_pte_leak(struct mm_struct *mm, unsigned long addr,
+			 pte_t *ptep);

Doesn't hugetlb.h somehow #include pgtable.h? So why double-define fix_pte_leak()?

It's other way round - pgtable.h somehow includes hugetlb.h. I've removed duplicated fix_pte_leak() declaration.

Quoted 8 lines
Quoted 8 lines
arch/x86/include/asm/pgtable_64.h +0 -0
index 2ee7811..6fa4079 100644
@@ -178,6 +178,12 @@ extern void cleanup_highmap(void);
 extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
 extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
 
Quoted 11 lines

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void); · line 187

Quoted 7 lines
arch/x86/include/asm/pgtable_64.h +6 -0
+#define ARCH_HAS_NEEDS_SWAP_PTL 1
+static inline bool arch_needs_swap_ptl(void)
+{
+       return boot_cpu_has_bug(X86_BUG_PTE_LEAK);
+}
+
 #endif /* !__ASSEMBLY__ */

I think we need a comment on this sucker. I'm not sure we should lean solely on the commit message to record why we need this until the end of time.

OK.

Quoted 3 lines

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void); · line 188

Quoted 1 line
arch/x86/include/asm/pgtable_64.h +1 -0
+	if (c->x86_model == 87) {

Please use the macros in here for the model id:

OK.

Quoted 4 lines

http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/arch/x86/include/asm/intel-family.h

We also probably want to prefix the pr_info() with something like "x86/intel:".

OK

Quoted 4 lines

arch/x86/include/asm/pgtable_64.h extern void cleanup_highmap(void);

Quoted 2 lines
arch/x86/include/asm/pgtable_64.h +2 -0
+/*
+ * Workaround for KNL issue:

Please be specific about what this "KNL issue" *is*.

OK

Quoted 25 lines
Quoted 6 lines

+ * A thread that is going to page fault due to P=0, may still
+ * non atomically set A or D bits, which could corrupt swap entries.
+ * Always flush the other CPUs and clear the PTE again to avoid
+ * this leakage. We are excluded using the pagetable lock.
+ */

arch/x86/include/asm/pgtable_64.h +1 -0
+

arch/x86/include/asm/pgtable_64.h

Quoted 10 lines
arch/x86/include/asm/pgtable_64.h +10 -0
+void fix_pte_leak(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids) {
+		trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
+		flush_tlb_others(mm_cpumask(mm), mm, addr,
+				 addr + PAGE_SIZE);
+		mb();
+		set_pte(ptep, __pte(0));
+	}
+}

I think the comment here is a bit sparse. Can we add some more details, like:

Entering here, the current CPU just cleared the PTE. But, another thread may have raced and set the A or D bits, or be _about_ to set the bits. Shooting their TLB entry down will ensure they see the cleared PTE and will not set A or D.

Quoted 4 lines

and by the set_pte():

Clear the PTE one more time, in case the other thread set A/D before we sent the TLB flush.

Thanks,
Lukasz

#37 lore raw
In reply to Lukasz Anaczkowski
url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610 config: i386-randconfig-r0-201624 (attached as .config) compiler…

Hi,

[auto build test ERROR on v4.7-rc3]
[also build test ERROR on next-20160614]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Lukasz-Anaczkowski/Linux-VM-workaround-for-Knights-Landing-A-D-leak/20160615-000610
config: i386-randconfig-r0-201624 (attached as .config)
compiler: gcc-6 (Debian 6.1.1-1) 6.1.1 20160430 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

arch/x86/built-in.o: In function `__ptep_modify_prot_start':
paravirt.c:(.text+0xc0e3c): undefined reference to fix_pte_leak' mm/built-in.o: In function zap_pte_range':

>> memory.c:(.text+0xdb8b4): undefined reference to `fix_pte_leak'

mm/built-in.o: In function `move_page_tables':
(.text+0x1102ff): undefined reference to `fix_pte_leak'
mm/built-in.o: In function `vunmap_page_range':
vmalloc.c:(.text+0x125741): undefined reference to fix_pte_leak' mm/built-in.o: In function ptep_clear_flush':
(.text+0x12e01f): undefined reference to fix_pte_leak' mm/built-in.o:madvise.c:(.text+0x131138): more undefined references to fix_pte_leak' follow

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation