Skip to contentSkip to conversation

AMD broadcast TLB invalidation

PATCH Superseded Rik van Riel 8 participants 38 messages Jan 16, 2025 – Jan 21, 2025

Subsystems (7)

v5 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v5 cover AMD broadcast TLB invalidation

Add support for broadcast TLB invalidation using AMD's INVLPGB instruction.

This allows the kernel to invalidate TLB entries on remote CPUs without needing to send IPIs, without having to wait for remote CPUs to handle those interrupts, and with less interruption to what was running on those CPUs.

Because x86 PCID space is limited, and there are some very large systems out there, broadcast TLB invalidation is only used for processes that are active on 3 or more CPUs, with the threshold being gradually increased the more the PCID space gets exhausted.

Combined with the removal of unnecessary lru_add_drain calls (see https://lkml.org/lkml/2024/12/19/1388) this results in a nice performance boost for the will-it-scale tlb_flush2_threads test on an AMD Milan system with 36 cores:

- vanilla kernel:           527k loops/second
- lru_add_drain removal:    731k loops/second
- only INVLPGB:             527k loops/second

- lru_add_drain + INVLPGB: 1157k loops/second

Profiling with only the INVLPGB changes showed while TLB invalidation went down from 40% of the total CPU time to only around 4% of CPU time, the contention simply moved to the LRU lock.

Fixing both at the same time about doubles the
number of iterations per second from this case.

Some numbers closer to real world performance
can be found at Phoronix, thanks to Michael:

https://www.phoronix.com/news/AMD-INVLPGB-Linux-Benefits

The code is now in a state where I am not sure what else needs to be done before it can be merged. If you can think of something, please let me know ;)

v5:
- use byte assembly for compatibility with older toolchains (Borislav, Michael)
- ensure a panic on an invalid number of extra pages (Dave, Tom)
- add cant_migrate() assertion to tlbsync (Jann)
- a bunch more cleanups (Nadav)
- key TCE enabling off X86_FEATURE_TCE (Andrew)
- fix a race between reclaim and ASID transition (Jann)
v4:
- Use only bitmaps to track free global ASIDs (Nadav)
- Improved AMD initialization (Borislav & Tom)
- Various naming and documentation improvements (Peter, Nadav, Tom, Dave)
- Fixes for subtle race conditions (Jann)
v3:
- Remove paravirt tlb_remove_table call (thank you Qi Zheng)
- More suggested cleanups and changelog fixes by Peter and Nadav
v2:
- Apply suggestions by Peter and Borislav (thank you!)
- Fix bug in arch_tlbbatch_flush, where we need to do both
the TLBSYNC, and flush the CPUs that are in the cpumask.
- Some updates to comments and changelogs based on questions.

#1 lore raw

PATCH v5 1/12 x86/mm: make MMU_GATHER_RCU_TABLE_FREE unconditional

Currently x86 uses CONFIG_MMU_GATHER_TABLE_FREE when using paravirt, and not when running on bare metal.

There is no real good reason to do things differently for each setup. Make them all the same.

Currently get_user_pages_fast synchronizes against page table freeing in two different ways:
- on bare metal, by blocking IRQs, which block TLB flush IPIs
- on paravirt, with MMU_GATHER_RCU_TABLE_FREE

This is done because some paravirt TLB flush implementations handle the TLB flush in the hypervisor, and will do the flush even when the target CPU has interrupts disabled.

Always handle page table freeing with MMU_GATHER_RCU_TABLE_FREE. Using RCU synchronization between page table freeing and get_user_pages_fast() allows bare metal to also do TLB flushing while interrupts are disabled.

That makes it safe to use INVLPGB on AMD CPUs.

---
 arch/x86/Kconfig           | 2 +-
 arch/x86/kernel/paravirt.c | 7 +------
 2 files changed, 2 insertions(+), 7 deletions(-)
arch/x86/Kconfig +1 -1
index 9d7bd0ae48c4..e8743f8c9fd0 100644
@@ -274,7 +274,7 @@ config X86
 	select HAVE_PCI
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
-	select MMU_GATHER_RCU_TABLE_FREE	if PARAVIRT
+	select MMU_GATHER_RCU_TABLE_FREE
 	select MMU_GATHER_MERGE_VMAS
 	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
 	select HAVE_REGS_AND_STACK_ACCESS_API
arch/x86/kernel/paravirt.c +1 -6
index fec381533555..2b78a6b466ed 100644
@@ -59,11 +59,6 @@ void __init native_pv_lock_init(void)
 		static_branch_enable(&virt_spin_lock_key);
 }
 
-static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)
-{
-	tlb_remove_page(tlb, table);
-}
-
 struct static_key paravirt_steal_enabled;
 struct static_key paravirt_steal_rq_enabled;
 
@@ -191,7 +186,7 @@ struct paravirt_patch_template pv_ops = {
 	.mmu.flush_tlb_kernel	= native_flush_tlb_global,
 	.mmu.flush_tlb_one_user	= native_flush_tlb_one_user,
 	.mmu.flush_tlb_multi	= native_flush_tlb_multi,
-	.mmu.tlb_remove_table	= native_tlb_remove_table,
+	.mmu.tlb_remove_table	= tlb_remove_table,
 
 	.mmu.exit_mmap		= paravirt_nop,
 	.mmu.notify_page_enc_status_changed	= paravirt_nop,
Signature · 1 line
2.47.1
#2 lore raw

PATCH v5 2/12 x86/mm: remove pv_ops.mmu.tlb_remove_table call

In reply to Rik van Riel

Every pv_ops.mmu.tlb_remove_table call ends up calling tlb_remove_table.

Get rid of the indirection by simply calling tlb_remove_table directly, and not going through the paravirt function pointers.

---
 arch/x86/hyperv/mmu.c                 |  1 -
 arch/x86/include/asm/paravirt.h       |  5 -----
 arch/x86/include/asm/paravirt_types.h |  2 --
 arch/x86/kernel/kvm.c                 |  1 -
 arch/x86/kernel/paravirt.c            |  1 -
 arch/x86/mm/pgtable.c                 | 16 ++++------------
 arch/x86/xen/mmu_pv.c                 |  1 -
 7 files changed, 4 insertions(+), 23 deletions(-)
arch/x86/hyperv/mmu.c +0 -1
index 1cc113200ff5..cbe6c71e17c1 100644
@@ -240,5 +240,4 @@ void hyperv_setup_mmu_ops(void)
 
 	pr_info("Using hypercall for remote TLB flush\n");
 	pv_ops.mmu.flush_tlb_multi = hyperv_flush_tlb_multi;
-	pv_ops.mmu.tlb_remove_table = tlb_remove_table;
 }
arch/x86/include/asm/paravirt.h +0 -5
index d4eb9e1d61b8..794ba3647c6c 100644
@@ -91,11 +91,6 @@ static inline void __flush_tlb_multi(const struct cpumask *cpumask,
 	PVOP_VCALL2(mmu.flush_tlb_multi, cpumask, info);
 }
 
-static inline void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table)
-{
-	PVOP_VCALL2(mmu.tlb_remove_table, tlb, table);
-}
-
 static inline void paravirt_arch_exit_mmap(struct mm_struct *mm)
 {
 	PVOP_VCALL1(mmu.exit_mmap, mm);
arch/x86/include/asm/paravirt_types.h +0 -2
index 8d4fbe1be489..13405959e4db 100644
@@ -136,8 +136,6 @@ struct pv_mmu_ops {
 	void (*flush_tlb_multi)(const struct cpumask *cpus,
 				const struct flush_tlb_info *info);
 
-	void (*tlb_remove_table)(struct mmu_gather *tlb, void *table);
-
 	/* Hook for intercepting the destruction of an mm_struct. */
 	void (*exit_mmap)(struct mm_struct *mm);
 	void (*notify_page_enc_status_changed)(unsigned long pfn, int npages, bool enc);
arch/x86/kernel/kvm.c +0 -1
index 7a422a6c5983..3be9b3342c67 100644
@@ -838,7 +838,6 @@ static void __init kvm_guest_init(void)
 #ifdef CONFIG_SMP
 	if (pv_tlb_flush_supported()) {
 		pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi;
-		pv_ops.mmu.tlb_remove_table = tlb_remove_table;
 		pr_info("KVM setup pv remote TLB flush\n");
 	}
 
arch/x86/kernel/paravirt.c +0 -1
index 2b78a6b466ed..c019771e0123 100644
@@ -186,7 +186,6 @@ struct paravirt_patch_template pv_ops = {
 	.mmu.flush_tlb_kernel	= native_flush_tlb_global,
 	.mmu.flush_tlb_one_user	= native_flush_tlb_one_user,
 	.mmu.flush_tlb_multi	= native_flush_tlb_multi,
-	.mmu.tlb_remove_table	= tlb_remove_table,
 
 	.mmu.exit_mmap		= paravirt_nop,
 	.mmu.notify_page_enc_status_changed	= paravirt_nop,
arch/x86/mm/pgtable.c +4 -12
index 5745a354a241..3dc4af1f7868 100644
@@ -18,14 +18,6 @@ EXPORT_SYMBOL(physical_mask);
 #define PGTABLE_HIGHMEM 0
 #endif
 
-#ifndef CONFIG_PARAVIRT
-static inline
-void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table)
-{
-	tlb_remove_page(tlb, table);
-}
-#endif
-
 gfp_t __userpte_alloc_gfp = GFP_PGTABLE_USER | PGTABLE_HIGHMEM;
 
 pgtable_t pte_alloc_one(struct mm_struct *mm)
@@ -54,7 +46,7 @@ void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
 {
 	pagetable_pte_dtor(page_ptdesc(pte));
 	paravirt_release_pte(page_to_pfn(pte));
-	paravirt_tlb_remove_table(tlb, pte);
+	tlb_remove_table(tlb, pte);
 }
 
 #if CONFIG_PGTABLE_LEVELS > 2
@@ -70,7 +62,7 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
 	tlb->need_flush_all = 1;
 #endif
 	pagetable_pmd_dtor(ptdesc);
-	paravirt_tlb_remove_table(tlb, ptdesc_page(ptdesc));
+	tlb_remove_table(tlb, ptdesc_page(ptdesc));
 }
 
 #if CONFIG_PGTABLE_LEVELS > 3
@@ -80,14 +72,14 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
 
 	pagetable_pud_dtor(ptdesc);
 	paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
-	paravirt_tlb_remove_table(tlb, virt_to_page(pud));
+	tlb_remove_table(tlb, virt_to_page(pud));
 }
 
 #if CONFIG_PGTABLE_LEVELS > 4
 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d)
 {
 	paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT);
-	paravirt_tlb_remove_table(tlb, virt_to_page(p4d));
+	tlb_remove_table(tlb, virt_to_page(p4d));
 }
 #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
 #endif	/* CONFIG_PGTABLE_LEVELS > 3 */
arch/x86/xen/mmu_pv.c +0 -1
index 55a4996d0c04..041e17282af0 100644
@@ -2137,7 +2137,6 @@ static const typeof(pv_ops) xen_mmu_ops __initconst = {
 		.flush_tlb_kernel = xen_flush_tlb,
 		.flush_tlb_one_user = xen_flush_tlb_one_user,
 		.flush_tlb_multi = xen_flush_tlb_multi,
-		.tlb_remove_table = tlb_remove_table,
 
 		.pgd_alloc = xen_pgd_alloc,
 		.pgd_free = xen_pgd_free,
Signature · 1 line
2.47.1
#3 lore raw

PATCH v5 3/12 x86/mm: consolidate full flush threshold decision

In reply to Rik van Riel

Reduce code duplication by consolidating the decision point for whether to do individual invalidations or a full flush inside get_flush_tlb_info.

---
 arch/x86/mm/tlb.c | 43 ++++++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 23 deletions(-)
arch/x86/mm/tlb.c +20 -23
index 6cf881a942bb..2f38cf95dee3 100644
@@ -1009,6 +1009,15 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm,
 	info->initiating_cpu	= smp_processor_id();
 	info->trim_cpumask	= 0;
 
+	/*
+	 * If the number of flushes is so large that a full flush
+	 * would be faster, do a full flush.
+	 */
+	if ((end - start) >> stride_shift > tlb_single_page_flush_ceiling) {
+		info->start = 0;
+		info->end = TLB_FLUSH_ALL;
+	}
+
 	return info;
 }
 
@@ -1026,17 +1035,8 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
 				bool freed_tables)
 {
 	struct flush_tlb_info *info;
+	int cpu = get_cpu();
 	u64 new_tlb_gen;
-	int cpu;
-
-	cpu = get_cpu();
-
-	/* Should we flush just the requested range? */
-	if ((end == TLB_FLUSH_ALL) ||
-	    ((end - start) >> stride_shift) > tlb_single_page_flush_ceiling) {
-		start = 0;
-		end = TLB_FLUSH_ALL;
-	}
 
 	/* This is also a barrier that synchronizes with switch_mm(). */
 	new_tlb_gen = inc_mm_tlb_gen(mm);
@@ -1089,22 +1089,19 @@ static void do_kernel_range_flush(void *info)
 
 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
-	/* Balance as user space task's flush, a bit conservative */
-	if (end == TLB_FLUSH_ALL ||
-	    (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
-		on_each_cpu(do_flush_tlb_all, NULL, 1);
-	} else {
-		struct flush_tlb_info *info;
+	struct flush_tlb_info *info;
 
-		preempt_disable();
-		info = get_flush_tlb_info(NULL, start, end, 0, false,
-					  TLB_GENERATION_INVALID);
+	guard(preempt)();
+
+	info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false,
+				  TLB_GENERATION_INVALID);
 
+	if (end == TLB_FLUSH_ALL)
+		on_each_cpu(do_flush_tlb_all, NULL, 1);
+	else
 		on_each_cpu(do_kernel_range_flush, info, 1);
 
-		put_flush_tlb_info();
-		preempt_enable();
-	}
+	put_flush_tlb_info();
 }
 
 /*
@@ -1276,7 +1273,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 
 	int cpu = get_cpu();
 
-	info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false,
+	info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, PAGE_SHIFT, false,
 				  TLB_GENERATION_INVALID);
 	/*
 	 * flush_tlb_multi() is not optimized for the common case in which only
Signature · 1 line
2.47.1
#4 lore raw

RE: [PATCH v5 03/12] x86/mm: consolidate full flush threshold decision

From: [email protected] <[email protected]> Sent: Wednesday, January 15, 2025 6:30 PM

Quoted 15 lines

Reduce code duplication by consolidating the decision point for whether to do individual invalidations or a full flush inside get_flush_tlb_info.

---
 arch/x86/mm/tlb.c | 43 ++++++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 23 deletions(-)
arch/x86/mm/tlb.c +0 -0
index 6cf881a942bb..2f38cf95dee3 100644

[snip]

Quoted 11 lines
arch/x86/mm/tlb.c +1 -6
@@ -1089,22 +1089,19 @@ static void do_kernel_range_flush(void *info)
 
 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
-	/* Balance as user space task's flush, a bit conservative */
-	if (end == TLB_FLUSH_ALL ||
-	    (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
-		on_each_cpu(do_flush_tlb_all, NULL, 1);
-	} else {
-		struct flush_tlb_info *info;
+	struct flush_tlb_info *info;

arch/x86/mm/tlb.c static void do_kernel_range_flush(void *info) · line 1099

Quoted 10 lines
arch/x86/mm/tlb.c +5 -3
 
-		preempt_disable();
-		info = get_flush_tlb_info(NULL, start, end, 0, false,
-					  TLB_GENERATION_INVALID);
+	guard(preempt)();
+
+	info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false,
+				  TLB_GENERATION_INVALID);
 
+	if (end == TLB_FLUSH_ALL)

This needs to test "info->end", not "end".

In my VM on Hyper-V *without* INVLPGB support, the bug causes boot to hang as it tries to individually flush each page in the [0, TLB_FLUSH_ALL] range. :-)

Michael

Quoted 11 lines
arch/x86/mm/tlb.c +3 -3
+		on_each_cpu(do_flush_tlb_all, NULL, 1);
+	else
 		on_each_cpu(do_kernel_range_flush, info, 1);
 
-		put_flush_tlb_info();
-		preempt_enable();
-	}
+	put_flush_tlb_info();
 }
 
 /*
#5 lore raw

Re: [PATCH v5 03/12] x86/mm: consolidate full flush threshold decision

Quoted 12 lines · Michael Kelley

From: [email protected] <[email protected]> Sent: Wednesday, January 15, 2025 6:30 PM

Quoted 10 lines
(quoted diff) +0 -1
-		info = get_flush_tlb_info(NULL, start, end, 0,

false,

arch/x86/mm/tlb.c +3 -1
-					  TLB_GENERATION_INVALID);
+	guard(preempt)();
+
+	info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT,

false,

arch/x86/mm/tlb.c +1 -0
+				  TLB_GENERATION_INVALID);
Quoted 7 lines

arch/x86/mm/tlb.c static void do_kernel_range_flush(void *info) · line 1099

Quoted 1 line
arch/x86/mm/tlb.c +1 -0
+	if (end == TLB_FLUSH_ALL)

This needs to test "info->end", not "end".

In my VM on Hyper-V *without* INVLPGB support, the bug causes boot to hang as it tries to individually flush each page in the [0, TLB_FLUSH_ALL] range. :-)

Ohhhh, good catch.

I guess on bare metal the automatic tests that run on every kernel build didn't notice the slower boot, because it's not as severe of an impact as in a virtual machine.

Signature · 1 line
All Rights Reversed.
#6 lore raw

PATCH v5 4/12 x86/mm: get INVLPGB count max from CPUID

In reply to Rik van Riel

The CPU advertises the maximum number of pages that can be shot down with one INVLPGB instruction in the CPUID data.

Save that information for later use.

---
 arch/x86/Kconfig.cpu               | 5 +++++
 arch/x86/include/asm/cpufeatures.h | 1 +
 arch/x86/include/asm/tlbflush.h    | 7 +++++++
 arch/x86/kernel/cpu/amd.c          | 8 ++++++++
 4 files changed, 21 insertions(+)
arch/x86/Kconfig.cpu +5 -0
index 2a7279d80460..bacdc502903f 100644
@@ -395,6 +395,10 @@ config X86_VMX_FEATURE_NAMES
 	def_bool y
 	depends on IA32_FEAT_CTL
 
+config X86_BROADCAST_TLB_FLUSH
+	def_bool y
+	depends on CPU_SUP_AMD
+
 menuconfig PROCESSOR_SELECT
 	bool "Supported processor vendors" if EXPERT
 	help
@@ -431,6 +435,7 @@ config CPU_SUP_CYRIX_32
 config CPU_SUP_AMD
 	default y
 	bool "Support AMD processors" if PROCESSOR_SELECT
+	select X86_BROADCAST_TLB_FLUSH
 	help
 	  This enables detection, tunings and quirks for AMD processors
 
arch/x86/include/asm/cpufeatures.h +1 -0
index 17b6590748c0..f9b832e971c5 100644
@@ -338,6 +338,7 @@
 #define X86_FEATURE_CLZERO		(13*32+ 0) /* "clzero" CLZERO instruction */
 #define X86_FEATURE_IRPERF		(13*32+ 1) /* "irperf" Instructions Retired Count */
 #define X86_FEATURE_XSAVEERPTR		(13*32+ 2) /* "xsaveerptr" Always save/restore FP error pointers */
+#define X86_FEATURE_INVLPGB		(13*32+ 3) /* INVLPGB and TLBSYNC instruction supported. */
 #define X86_FEATURE_RDPRU		(13*32+ 4) /* "rdpru" Read processor register at user level */
 #define X86_FEATURE_WBNOINVD		(13*32+ 9) /* "wbnoinvd" WBNOINVD instruction */
 #define X86_FEATURE_AMD_IBPB		(13*32+12) /* Indirect Branch Prediction Barrier */
arch/x86/include/asm/tlbflush.h +7 -0
index 02fc2aa06e9e..8fe3b2dda507 100644
@@ -183,6 +183,13 @@ static inline void cr4_init_shadow(void)
 extern unsigned long mmu_cr4_features;
 extern u32 *trampoline_cr4_features;
 
+/* How many pages can we invalidate with one INVLPGB. */
+#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH
+extern u16 invlpgb_count_max;
+#else
+#define invlpgb_count_max 1
+#endif
+
 extern void initialize_tlbstate_and_flush(void);
 
 /*
arch/x86/kernel/cpu/amd.c +8 -0
index 79d2e17f6582..bcf73775b4f8 100644
@@ -29,6 +29,8 @@
 
 #include "cpu.h"
 
+u16 invlpgb_count_max __ro_after_init;
+
 static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p)
 {
 	u32 gprs[8] = { 0 };
@@ -1135,6 +1137,12 @@ static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
 		tlb_lli_2m[ENTRIES] = eax & mask;
 
 	tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
+
+	/* Max number of pages INVLPGB can invalidate in one shot */
+	if (boot_cpu_has(X86_FEATURE_INVLPGB)) {
+		cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
+		invlpgb_count_max = (edx & 0xffff) + 1;
+	}
 }
 
 static const struct cpu_dev amd_cpu_dev = {
Signature · 1 line
2.47.1
#7 lore raw

PATCH v5 5/12 x86/mm: add INVLPGB support code

In reply to Rik van Riel

Add invlpgb.h with the helper functions and definitions needed to use broadcast TLB invalidation on AMD EPYC 3 and newer CPUs.

---
 arch/x86/include/asm/invlpgb.h  | 97 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/tlbflush.h |  1 +
 2 files changed, 98 insertions(+)
 create mode 100644 arch/x86/include/asm/invlpgb.h
arch/x86/include/asm/invlpgb.h new file +97 -0
new file mode 100644
index 000000000000..4dfd09e65fa6
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_INVLPGB
+#define _ASM_X86_INVLPGB
+
+#include <linux/kernel.h>
+#include <vdso/bits.h>
+
+/*
+ * INVLPGB does broadcast TLB invalidation across all the CPUs in the system.
+ *
+ * The INVLPGB instruction is weakly ordered, and a batch of invalidations can
+ * be done in a parallel fashion.
+ *
+ * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from
+ * this CPU have completed.
+ */
+static inline void __invlpgb(unsigned long asid, unsigned long pcid,
+			     unsigned long addr, u16 extra_count,
+			     bool pmd_stride, unsigned long flags)
+{
+	u32 edx = (pcid << 16) | asid;
+	u32 ecx = (pmd_stride << 31) | extra_count;
+	u64 rax = addr | flags;
+
+	/* INVLPGB; supported in binutils >= 2.36. */
+	asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
+}
+
+/* Wait for INVLPGB originated by this CPU to complete. */
+static inline void tlbsync(void)
+{
+	cant_migrate();
+	/* TLBSYNC: supported in binutils >= 0.36. */
+	asm volatile(".byte 0x0f, 0x01, 0xff" ::: "memory");
+}
+
+/*
+ * INVLPGB can be targeted by virtual address, PCID, ASID, or any combination
+ * of the three. For example:
+ * - INVLPGB_VA | INVLPGB_INCLUDE_GLOBAL: invalidate all TLB entries at the address
+ * - INVLPGB_PCID:			  invalidate all TLB entries matching the PCID
+ *
+ * The first can be used to invalidate (kernel) mappings at a particular
+ * address across all processes.
+ *
+ * The latter invalidates all TLB entries matching a PCID.
+ */
+#define INVLPGB_VA			BIT(0)
+#define INVLPGB_PCID			BIT(1)
+#define INVLPGB_ASID			BIT(2)
+#define INVLPGB_INCLUDE_GLOBAL		BIT(3)
+#define INVLPGB_FINAL_ONLY		BIT(4)
+#define INVLPGB_INCLUDE_NESTED		BIT(5)
+
+/* Flush all mappings for a given pcid and addr, not including globals. */
+static inline void invlpgb_flush_user(unsigned long pcid,
+				      unsigned long addr)
+{
+	__invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA);
+	tlbsync();
+}
+
+static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid,
+						unsigned long addr,
+						u16 nr,
+						bool pmd_stride)
+{
+	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA);
+}
+
+/* Flush all mappings for a given PCID, not including globals. */
+static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid)
+{
+	__invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID);
+}
+
+/* Flush all mappings, including globals, for all PCIDs. */
+static inline void invlpgb_flush_all(void)
+{
+	__invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL);
+	tlbsync();
+}
+
+/* Flush addr, including globals, for all PCIDs. */
+static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr)
+{
+	__invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL);
+}
+
+/* Flush all mappings for all PCIDs except globals. */
+static inline void invlpgb_flush_all_nonglobals(void)
+{
+	__invlpgb(0, 0, 0, 0, 0, 0);
+	tlbsync();
+}
+
+#endif /* _ASM_X86_INVLPGB */
arch/x86/include/asm/tlbflush.h +1 -0
index 8fe3b2dda507..dba5caa4a9f4 100644
@@ -10,6 +10,7 @@
 #include <asm/cpufeature.h>
 #include <asm/special_insns.h>
 #include <asm/smp.h>
+#include <asm/invlpgb.h>
 #include <asm/invpcid.h>
 #include <asm/pti.h>
 #include <asm/processor-flags.h>
Signature · 1 line
2.47.1
#8 lore raw

Re: [PATCH v5 05/12] x86/mm: add INVLPGB support code

kernel test robot noticed the following build errors:

Hi Rik,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/x86/mm]
[also build test ERROR on tip/master linus/master tip/x86/core v6.13-rc7] [cannot apply to tip/auto-latest next-20250116]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rik-van-Riel/x86-mm-make-MMU_GATHER_RCU_TABLE_FREE-unconditional/20250116-103657
base:   tip/x86/mm
patch link:    https://lore.kernel.org/r/20250116023127.1531583-6-riel%40surriel.com
patch subject: [PATCH v5 05/12] x86/mm: add INVLPGB support code
config: i386-buildonly-randconfig-001-20250117 (https://download.01.org/0day-ci/archive/20250117/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/x86/kernel/asm-offsets.c:14:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:17:
In file included from include/linux/fs.h:33:
In file included from include/linux/percpu-rwsem.h:7:
In file included from include/linux/rcuwait.h:6:
In file included from include/linux/sched/signal.h:9:
In file included from include/linux/sched/task.h:13:
In file included from include/linux/uaccess.h:12:
In file included from arch/x86/include/asm/uaccess.h:17:
In file included from arch/x86/include/asm/tlbflush.h:13:

>> arch/x86/include/asm/invlpgb.h:26:49: error: invalid input size for constraint 'a'
      26 |         asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
         |                                                        ^
   In file included from arch/x86/kernel/asm-offsets.c:14:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:21:
   In file included from include/linux/mm.h:2223:
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~

1 warning and 1 error generated.
make[3]: *** [scripts/Makefile.build:102: arch/x86/kernel/asm-offsets.s] Error 1 shuffle=3995685176 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1263: prepare0] Error 2 shuffle=3995685176
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:251: __sub-make] Error 2 shuffle=3995685176
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:251: __sub-make] Error 2 shuffle=3995685176
make: Target 'prepare' not remade because of errors.

vim +/a +26 arch/x86/include/asm/invlpgb.h

     7	
     8	/*
     9	 * INVLPGB does broadcast TLB invalidation across all the CPUs in the system.
    10	 *
    11	 * The INVLPGB instruction is weakly ordered, and a batch of invalidations can
    12	 * be done in a parallel fashion.
    13	 *
    14	 * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from
    15	 * this CPU have completed.
    16	 */
    17	static inline void __invlpgb(unsigned long asid, unsigned long pcid,
    18				     unsigned long addr, u16 extra_count,
    19				     bool pmd_stride, unsigned long flags)
    20	{
    21		u32 edx = (pcid << 16) | asid;
    22		u32 ecx = (pmd_stride << 31) | extra_count;
    23		u64 rax = addr | flags;
    24	
    25		/* INVLPGB; supported in binutils >= 2.36. */
  > 26		asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
    27	}
    28	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

Re: [PATCH v5 05/12] x86/mm: add INVLPGB support code

Quoted 9 lines · kernel test robot

compiler: clang version 19.1.3
(https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b25
4b6afab99)
reproduce (this is a W=1 build):
(https://download.01.org/0day-ci/archive/20250117/202501171121.nQcwVI
[email protected]/reproduce)

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

arch/x86/include/asm/invlpgb.h:26:49: error: invalid input size for constraint 'a'

      26 |         asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
         |                                                        ^

What do I do about this?

It didn't used to complain when we had a named
assembler operator there:

    asm volatile("invlpgb" : : "a" (rax), "c" (ecx), "d" (edx));

However, now that we're using .byte for compatibility with older binutils, things break with newer llvm?

Signature · 1 line
All Rights Reversed.
#10 lore raw

Re: [PATCH v5 05/12] x86/mm: add INVLPGB support code

Quoted 12 lines · Rik van Riel
Quoted 10 lines · kernel test robot

compiler: clang version 19.1.3
(
https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b25
4b6afab99)
reproduce (this is a W=1 build):
(
https://download.01.org/0day-ci/archive/20250117/202501171121.nQcwVI
[email protected]/reproduce)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

arch/x86/include/asm/invlpgb.h:26:49: error: invalid input size for constraint 'a'

      26 |         asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
         |                                                        ^

What do I do about this?

Ohhh, never mind.

The failing build is a 32 bit build!

I just need to use unsigned long instead of u64 in the next version.

Signature · 1 line
All Rights Reversed.
#11 lore raw

PATCH v5 6/12 x86/mm: use INVLPGB for kernel TLB flushes

In reply to Rik van Riel

Use broadcast TLB invalidation for kernel addresses when available.

Remove the need to send IPIs for kernel TLB flushes.

---
 arch/x86/mm/tlb.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +27 -1
index 2f38cf95dee3..0761dd224e84 100644
@@ -1077,6 +1077,30 @@ void flush_tlb_all(void)
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
 
+static bool broadcast_kernel_range_flush(struct flush_tlb_info *info)
+{
+	unsigned long addr;
+	unsigned long nr;
+
+	if (!IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH))
+		return false;
+
+	if (!cpu_feature_enabled(X86_FEATURE_INVLPGB))
+		return false;
+
+	if (info->end == TLB_FLUSH_ALL) {
+		invlpgb_flush_all();
+		return true;
+	}
+
+	for (addr = info->start; addr < info->end; addr += nr << PAGE_SHIFT) {
+		nr = min((info->end - addr) >> PAGE_SHIFT, invlpgb_count_max);
+		invlpgb_flush_addr_nosync(addr, nr);
+	}
+	tlbsync();
+	return true;
+}
+
 static void do_kernel_range_flush(void *info)
 {
 	struct flush_tlb_info *f = info;
@@ -1096,7 +1120,9 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 	info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false,
 				  TLB_GENERATION_INVALID);
 
-	if (end == TLB_FLUSH_ALL)
+	if (broadcast_kernel_range_flush(info))
+		; /* Fall through. */
+	else if (end == TLB_FLUSH_ALL)
 		on_each_cpu(do_flush_tlb_all, NULL, 1);
 	else
 		on_each_cpu(do_kernel_range_flush, info, 1);
Signature · 1 line
2.47.1
#12 lore raw

Re: [PATCH v5 06/12] x86/mm: use INVLPGB for kernel TLB flushes

kernel test robot noticed the following build errors:

Hi Rik,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/x86/mm]
[also build test ERROR on tip/master linus/master tip/x86/core v6.13-rc7] [cannot apply to tip/auto-latest next-20250116]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rik-van-Riel/x86-mm-make-MMU_GATHER_RCU_TABLE_FREE-unconditional/20250116-103657
base:   tip/x86/mm
patch link:    https://lore.kernel.org/r/20250116023127.1531583-7-riel%40surriel.com
patch subject: [PATCH v5 06/12] x86/mm: use INVLPGB for kernel TLB flushes
config: i386-buildonly-randconfig-004-20250117 (https://download.01.org/0day-ci/archive/20250117/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250117/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from arch/x86/include/asm/tlbflush.h:13,

                    from arch/x86/include/asm/uaccess.h:17,
                    from include/linux/uaccess.h:12,
                    from include/linux/sched/task.h:13,
                    from include/linux/sched/signal.h:9,
                    from include/linux/rcuwait.h:6,
                    from include/linux/percpu-rwsem.h:7,
                    from include/linux/fs.h:33,
                    from include/linux/huge_mm.h:7,
                    from include/linux/mm.h:1132,
                    from arch/x86/mm/tlb.c:4:
   In function '__invlpgb',
       inlined from 'invlpgb_flush_all' at arch/x86/include/asm/invlpgb.h:80:2,
       inlined from 'broadcast_kernel_range_flush' at arch/x86/mm/tlb.c:1092:3,
       inlined from 'flush_tlb_kernel_range' at arch/x86/mm/tlb.c:1123:6:
>> arch/x86/include/asm/invlpgb.h:26:9: error: inconsistent operand constraints in an 'asm'
      26 |         asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
         |         ^~~
   In function '__invlpgb',
       inlined from 'invlpgb_flush_addr_nosync' at arch/x86/include/asm/invlpgb.h:87:2,
       inlined from 'broadcast_kernel_range_flush' at arch/x86/mm/tlb.c:1098:3,
       inlined from 'flush_tlb_kernel_range' at arch/x86/mm/tlb.c:1123:6:
>> arch/x86/include/asm/invlpgb.h:26:9: error: inconsistent operand constraints in an 'asm'
      26 |         asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
         |         ^~~

vim +/asm +26 arch/x86/include/asm/invlpgb.h

cd277b8b690f1e Rik van Riel 2025-01-15   7  
cd277b8b690f1e Rik van Riel 2025-01-15   8  /*
cd277b8b690f1e Rik van Riel 2025-01-15   9   * INVLPGB does broadcast TLB invalidation across all the CPUs in the system.
cd277b8b690f1e Rik van Riel 2025-01-15  10   *
cd277b8b690f1e Rik van Riel 2025-01-15  11   * The INVLPGB instruction is weakly ordered, and a batch of invalidations can
cd277b8b690f1e Rik van Riel 2025-01-15  12   * be done in a parallel fashion.
cd277b8b690f1e Rik van Riel 2025-01-15  13   *
cd277b8b690f1e Rik van Riel 2025-01-15  14   * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from
cd277b8b690f1e Rik van Riel 2025-01-15  15   * this CPU have completed.
cd277b8b690f1e Rik van Riel 2025-01-15  16   */
cd277b8b690f1e Rik van Riel 2025-01-15  17  static inline void __invlpgb(unsigned long asid, unsigned long pcid,
cd277b8b690f1e Rik van Riel 2025-01-15  18  			     unsigned long addr, u16 extra_count,
cd277b8b690f1e Rik van Riel 2025-01-15  19  			     bool pmd_stride, unsigned long flags)
cd277b8b690f1e Rik van Riel 2025-01-15  20  {
cd277b8b690f1e Rik van Riel 2025-01-15  21  	u32 edx = (pcid << 16) | asid;
cd277b8b690f1e Rik van Riel 2025-01-15  22  	u32 ecx = (pmd_stride << 31) | extra_count;
cd277b8b690f1e Rik van Riel 2025-01-15  23  	u64 rax = addr | flags;
cd277b8b690f1e Rik van Riel 2025-01-15  24  
cd277b8b690f1e Rik van Riel 2025-01-15  25  	/* INVLPGB; supported in binutils >= 2.36. */

cd277b8b690f1e Rik van Riel 2025-01-15 @26 asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx));
cd277b8b690f1e Rik van Riel 2025-01-15 27 }
cd277b8b690f1e Rik van Riel 2025-01-15 28

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#13 lore raw

PATCH v5 7/12 x86/tlb: use INVLPGB in flush_tlb_all

In reply to Rik van Riel

The flush_tlb_all() function is not used a whole lot, but we might as well use broadcast TLB flushing there, too.

---
 arch/x86/mm/tlb.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/mm/tlb.c +15 -0
index 0761dd224e84..49b3e90503a3 100644
@@ -1065,6 +1065,19 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
 }
 
 
+static bool broadcast_flush_tlb_all(void)
+{
+	if (!IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH))
+		return false;
+
+	if (!cpu_feature_enabled(X86_FEATURE_INVLPGB))
+		return false;
+
+	guard(preempt)();
+	invlpgb_flush_all();
+	return true;
+}
+
 static void do_flush_tlb_all(void *info)
 {
 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
@@ -1073,6 +1086,8 @@ static void do_flush_tlb_all(void *info)
 
 void flush_tlb_all(void)
 {
+	if (broadcast_flush_tlb_all())
+		return;
 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
Signature · 1 line
2.47.1
#14 lore raw

PATCH v5 8/12 x86/mm: use broadcast TLB flushing for page reclaim TLB flushing

In reply to Rik van Riel

In the page reclaim code, we only track the CPU(s) where the TLB needs to be flushed, rather than all the individual mappings that may be getting invalidated.

Use broadcast TLB flushing when that is available.

---
 arch/x86/mm/tlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +3 -1
index 49b3e90503a3..746a89924f02 100644
@@ -1321,7 +1321,9 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	 * a local TLB flush is needed. Optimize this use-case by calling
 	 * flush_tlb_func_local() directly in this case.
 	 */
-	if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
+		invlpgb_flush_all_nonglobals();
+	} else if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
 		flush_tlb_multi(&batch->cpumask, info);
 	} else if (cpumask_test_cpu(cpu, &batch->cpumask)) {
 		lockdep_assert_irqs_enabled();
Signature · 1 line
2.47.1
#15 lore raw

PATCH v5 9/12 x86/mm: enable broadcast TLB invalidation for multi-threaded processes

In reply to Rik van Riel

Use broadcast TLB invalidation, using the INVPLGB instruction, on AMD EPYC 3 and newer CPUs.

In order to not exhaust PCID space, and keep TLB flushes local for single threaded processes, we only hand out broadcast ASIDs to processes active on 3 or more CPUs, and gradually increase the threshold as broadcast ASID space is depleted.

---
 arch/x86/include/asm/mmu.h         |   6 +
 arch/x86/include/asm/mmu_context.h |  14 ++
 arch/x86/include/asm/tlbflush.h    |  72 ++++++
 arch/x86/mm/tlb.c                  | 362 ++++++++++++++++++++++++++++-
 4 files changed, 442 insertions(+), 12 deletions(-)
arch/x86/include/asm/mmu.h +6 -0
index 3b496cdcb74b..d71cd599fec4 100644
@@ -69,6 +69,12 @@ typedef struct {
 	u16 pkey_allocation_map;
 	s16 execute_only_pkey;
 #endif
+
+#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH
+	u16 global_asid;
+	bool asid_transition;
+#endif
+
 } mm_context_t;
 
 #define INIT_MM_CONTEXT(mm)						\
arch/x86/include/asm/mmu_context.h +14 -0
index 795fdd53bd0a..d670699d32c2 100644
@@ -139,6 +139,8 @@ static inline void mm_reset_untag_mask(struct mm_struct *mm)
 #define enter_lazy_tlb enter_lazy_tlb
 extern void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk);
 
+extern void destroy_context_free_global_asid(struct mm_struct *mm);
+
 /*
  * Init a new mm.  Used on mm copies, like at fork()
  * and on mm's that are brand-new, like at execve().
@@ -161,6 +163,14 @@ static inline int init_new_context(struct task_struct *tsk,
 		mm->context.execute_only_pkey = -1;
 	}
 #endif
+
+#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
+		mm->context.global_asid = 0;
+		mm->context.asid_transition = false;
+	}
+#endif
+
 	mm_reset_untag_mask(mm);
 	init_new_context_ldt(mm);
 	return 0;
@@ -170,6 +180,10 @@ static inline int init_new_context(struct task_struct *tsk,
 static inline void destroy_context(struct mm_struct *mm)
 {
 	destroy_context_ldt(mm);
+#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB))
+		destroy_context_free_global_asid(mm);
+#endif
 }
 
 extern void switch_mm(struct mm_struct *prev, struct mm_struct *next,
arch/x86/include/asm/tlbflush.h +72 -0

78 lines not on this page · raw message

arch/x86/mm/tlb.c +350 -12

444 lines not on this page · raw message

Signature · 1 line
2.47.1
#16 lore raw

PATCH v5 10/12 x86,tlb: do targeted broadcast flushing from tlbbatch code

In reply to Rik van Riel

Instead of doing a system-wide TLB flush from arch_tlbbatch_flush, queue up asynchronous, targeted flushes from arch_tlbbatch_add_pending.

This also allows us to avoid adding the CPUs of processes using broadcast flushing to the batch->cpumask, and will hopefully further reduce TLB flushing from the reclaim and compaction paths.

---
 arch/x86/include/asm/tlbbatch.h |  1 +
 arch/x86/include/asm/tlbflush.h | 12 ++------
 arch/x86/mm/tlb.c               | 54 +++++++++++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 12 deletions(-)
arch/x86/include/asm/tlbbatch.h +1 -0
index 1ad56eb3e8a8..f9a17edf63ad 100644
@@ -10,6 +10,7 @@ struct arch_tlbflush_unmap_batch {
 	 * the PFNs being flushed..
 	 */
 	struct cpumask cpumask;
+	bool used_invlpgb;
 };
 
 #endif /* _ARCH_X86_TLBBATCH_H */
arch/x86/include/asm/tlbflush.h +3 -9
index 5eae5c1aafa5..e5516afdef7d 100644
@@ -358,21 +358,15 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
 	return atomic64_inc_return(&mm->context.tlb_gen);
 }
 
-static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
-					     struct mm_struct *mm,
-					     unsigned long uaddr)
-{
-	inc_mm_tlb_gen(mm);
-	cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
-	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
-}
-
 static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm)
 {
 	flush_tlb_mm(mm);
 }
 
 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
+extern void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
+					     struct mm_struct *mm,
+					     unsigned long uaddr);
 
 static inline bool pte_flags_need_flush(unsigned long oldflags,
 					unsigned long newflags,
arch/x86/mm/tlb.c +51 -3

72 lines not on this page · raw message

Signature · 1 line
2.47.1
#17 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 3 lines · Rik van Riel

Instead of doing a system-wide TLB flush from arch_tlbbatch_flush, queue up asynchronous, targeted flushes from arch_tlbbatch_add_pending.

[snip]

Quoted 15 lines
arch/x86/mm/tlb.c +1 -3
@@ -1659,9 +1659,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  	 * a local TLB flush is needed. Optimize this use-case by calling
  	 * flush_tlb_func_local() directly in this case.
  	 */
-	if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
-		invlpgb_flush_all_nonglobals();
-	} else if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
+	if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
  		flush_tlb_multi(&batch->cpumask, info);
  	} else if (cpumask_test_cpu(cpu, &batch->cpumask)) {
  		lockdep_assert_irqs_enabled();
@@ -1670,12 +1668,62 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  		local_irq_enable();

arch/x86/mm/tlb.c void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) · line 1678

Quoted 10 lines
arch/x86/mm/tlb.c +8 -0
  	}
 
+	/*
+	 * If we issued (asynchronous) INVLPGB flushes, wait for them here.
+	 * The cpumask above contains only CPUs that were running tasks
+	 * not using broadcast TLB flushing.
+	 */
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->used_invlpgb) {
+		tlbsync();
+		migrate_enable();

Maybe someone mentioned it before, but I would emphasize that I do not think that preventing migration for potentially long time is that great.

One alternative solution would be to set a bit on cpu_tlbstate, that when set, you'd issue a tlbsync on context switch.

(I can think about other solutions, but I think the one I just mentioned is the cleanest one).

Quoted 13 lines
arch/x86/mm/tlb.c +7 -0
+		batch->used_invlpgb = false;
+	}
+
  	cpumask_clear(&batch->cpumask);
 
  	put_flush_tlb_info();
  	put_cpu();
  }
 
+void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
+					     struct mm_struct *mm,
+					     unsigned long uaddr)
+{

arch/x86/mm/tlb.c void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) · line 1701

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+	if (static_cpu_has(X86_FEATURE_INVLPGB) && mm_global_asid(mm)) {
+		u16 asid = mm_global_asid(mm);
+		/*
+		 * Queue up an asynchronous invalidation. The corresponding
+		 * TLBSYNC is done in arch_tlbbatch_flush(), and must be done
+		 * on the same CPU.
+		 */
+		if (!batch->used_invlpgb) {
+			batch->used_invlpgb = true;
+			migrate_disable();

See my comment above...

Quoted 9 lines
arch/x86/mm/tlb.c +9 -0
+		}
+		invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false);
+		/* Do any CPUs supporting INVLPGB need PTI? */
+		if (static_cpu_has(X86_FEATURE_PTI))
+			invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false);
+
+		/*
+		 * Some CPUs might still be using a local ASID for this
+		 * process, and require IPIs, while others are using the

arch/x86/mm/tlb.c void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) · line 1720

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+		 * global ASID.
+		 *
+		 * In this corner case we need to do both the broadcast
+		 * TLB invalidation, and send IPIs. The IPIs will help
+		 * stragglers transition to the broadcast ASID.
+		 */
+		if (READ_ONCE(mm->context.asid_transition))
+			goto also_send_ipi;
+	} else {
+also_send_ipi:

I really think you should avoid such goto's. A simple bool variable of "need_ipi" would suffice.

Quoted 9 lines
arch/x86/mm/tlb.c +6 -0
+		inc_mm_tlb_gen(mm);
+		cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
+	}
+	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
+}
+
  /*
   * Blindly accessing user memory from NMI context can be dangerous
   * if we're in the middle of switching the current user task or
#18 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 23 lines · Nadav Amit
Quoted 15 lines
arch/x86/mm/tlb.c +0 -0
@@ -1670,12 +1668,62 @@ void arch_tlbbatch_flush(struct
 arch_tlbflush_unmap_batch *batch)
   		local_irq_enable();
   	}

(quoted diff) +2 -0
+	/*
+	 * If we issued (asynchronous) INVLPGB flushes, wait for

them here.

(quoted diff) +1 -0
+	 * The cpumask above contains only CPUs that were running

tasks

arch/x86/mm/tlb.c +3 -0
+	 * not using broadcast TLB flushing.
+	 */
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch-
Quoted 1 line

used_invlpgb) {

arch/x86/mm/tlb.c void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) · line 1678

Quoted 2 lines
arch/x86/mm/tlb.c +2 -0
+		tlbsync();
+		migrate_enable();

Maybe someone mentioned it before, but I would emphasize that I do not
think that preventing migration for potentially long time is that great.

Quoted 6 lines

One alternative solution would be to set a bit on cpu_tlbstate, that when set, you'd issue a tlbsync on context switch.

(I can think about other solutions, but I think the one I just mentioned
is the cleanest one).

It is clean, but I'm not convinced it is good enough.

We need to guarantee that the INVLPGBs have finished before we free the pages.

Running a TLBSYNC at the next context switch could mean that TLBSYNC won't run until after the pages have been freed.

In practice it is probably good enough, since it would be simpler for TLBSYNC to return once all pending (older) INVLPGBs have finished, but it's not architecturally guaranteed.

We could send an IPI to remote CPUs in order for them to call TLBSYNC, but is that really better?

Signature · 1 line
All Rights Reversed.
#19 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 40 lines · Rik van Riel
Quoted 23 lines · Nadav Amit
Quoted 15 lines
arch/x86/mm/tlb.c +2 -0
@@ -1670,12 +1668,62 @@ void arch_tlbbatch_flush(struct
 arch_tlbflush_unmap_batch *batch)
   		local_irq_enable();
   	}
 
+	/*
+	 * If we issued (asynchronous) INVLPGB flushes, wait for

them here.

(quoted diff) +1 -0
+	 * The cpumask above contains only CPUs that were running

tasks

arch/x86/mm/tlb.c +3 -0
+	 * not using broadcast TLB flushing.
+	 */
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch-
Quoted 1 line

used_invlpgb) {

arch/x86/mm/tlb.c void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) · line 1678

Quoted 2 lines
arch/x86/mm/tlb.c +2 -0
+		tlbsync();
+		migrate_enable();

Maybe someone mentioned it before, but I would emphasize that I do not
think that preventing migration for potentially long time is that great.

Quoted 6 lines

One alternative solution would be to set a bit on cpu_tlbstate, that when set, you'd issue a tlbsync on context switch.

(I can think about other solutions, but I think the one I just mentioned
is the cleanest one).

It is clean, but I'm not convinced it is good enough.

We need to guarantee that the INVLPGBs have finished before we free the pages.

Running a TLBSYNC at the next context switch could mean that TLBSYNC won't run until after the pages have been freed.

Quoted 8 lines

In practice it is probably good enough, since it would be simpler for TLBSYNC to return once all pending (older) INVLPGBs have finished, but it's not architecturally guaranteed.

We could send an IPI to remote CPUs in order for them to call TLBSYNC, but is that really better?

I am not sure we are on the same page. What I suggested is:

1. arch_tlbbatch_flush() would still do tlbsync()
2. No migrate_enable() in arch_tlbbatch_flush()
3. No migrate_disable() in arch_tlbbatch_add_pending()
4. arch_tlbbatch_add_pending() sets cpu_tlbstate.pending_tlb_broadcast
5. switch_mm_irqs_off() checks cpu_tlbstate.pending_tlb_broadcast and if it is set performs tlbsync and clears it.

#20 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 12 lines · Nadav Amit

I am not sure we are on the same page. What I suggested is:

1. arch_tlbbatch_flush() would still do tlbsync()
2. No migrate_enable() in arch_tlbbatch_flush()
3. No migrate_disable() in arch_tlbbatch_add_pending()
4. arch_tlbbatch_add_pending() sets cpu_tlbstate.pending_tlb_broadcast
5. switch_mm_irqs_off() checks cpu_tlbstate.pending_tlb_broadcast and if
it is set performs tlbsync and clears it.

How does that synchronize the page freeing (from page reclaim) with the TLBSYNCs?

What guarantees that the page reclaim path won't free the pages until after TLBSYNC has completed on the CPUs that kicked off asynchronous flushes with INVPLGB?

Signature · 1 line
All Rights Reversed.
#21 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 13 lines · Rik van Riel
Quoted 12 lines · Nadav Amit

I am not sure we are on the same page. What I suggested is:

1. arch_tlbbatch_flush() would still do tlbsync()
2. No migrate_enable() in arch_tlbbatch_flush()
3. No migrate_disable() in arch_tlbbatch_add_pending()
4. arch_tlbbatch_add_pending() sets cpu_tlbstate.pending_tlb_broadcast
5. switch_mm_irqs_off() checks cpu_tlbstate.pending_tlb_broadcast and if
it is set performs tlbsync and clears it.

Quoted 6 lines

How does that synchronize the page freeing (from page reclaim) with the TLBSYNCs?

What guarantees that the page reclaim path won't free the pages until after TLBSYNC has completed on the CPUs that kicked off asynchronous flushes with INVPLGB?

[ you make me lose my confidence, although I see nothing wrong ]

Freeing the pages must be done after the TLBSYNC. I did not imply it needs to be changed.

The page freeing (and reclaim) path is only initiated after arch_tlbbatch_flush() is completed. If no migration is initiated, since we did not remove any tlbsync, it should be fine.

If migration was initiated, and some invlpgb's were already initiated, then we need for correctness to initiate tlbsync before the task might be scheduled on another core. That's exactly why adding tlbsync to switch_mm_irqs_off() is needed in such a case.

#22 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 17 lines · Nadav Amit
Quoted 4 lines · Rik van Riel

What guarantees that the page reclaim path won't free the pages until after TLBSYNC has completed on the CPUs that kicked off asynchronous flushes with INVPLGB?

[ you make me lose my confidence, although I see nothing wrong ]

Freeing the pages must be done after the TLBSYNC. I did not imply it needs to be changed.

The page freeing (and reclaim) path is only initiated after arch_tlbbatch_flush() is completed. If no migration is initiated, since
we did not remove any tlbsync, it should be fine.

Quoted 6 lines

If migration was initiated, and some invlpgb's were already initiated,
then we need for correctness to initiate tlbsync before the task might
be scheduled on another core. That's exactly why adding tlbsync to switch_mm_irqs_off() is needed in such a case.

This is the page reclaim code.

The process that has those other pages mapped might be running on other CPUs simultaneously with the page reclaim code.

Even if we were invalidating one of our own pages this way, there could be other threads in the same process, running while we are in the page reclaim code.

Signature · 1 line
All Rights Reversed.
#23 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

[ Thanks for your patience. ]

Quoted 6 lines · Rik van Riel

On Mon, 2025-01-20 at 19:09 +0200, Nadav Amit wrote:

This is the page reclaim code.

Quoted 7 lines

The process that has those other pages mapped might be running on other CPUs simultaneously with the page reclaim code.

Even if we were invalidating one of our own pages this way, there could be other threads in the same process, running while we are in the page reclaim code.

Of course, but there is nothing new here. Let me see where we lose each other by first stating the goal, what you propose, and what I suggest.

We are issuing invlpgb and we need to ensure tlbsync on the same core that initiated invlpgb before arch_tlbbatch_flush() finishes. That’s all that matters for our discussion (correct me if I miss something).

You solved it by disabling migration and running tlbsync at the end.

I suggest *not* to disable migration, to keep running tlbsync at the arch_tlbbatch_flush() as you do, and if context-switch happens after arch_tlbbatch_add_pending() and before arch_tlbbatch_flush(), to run tlbsync during the context switch.

#24 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 26 lines · Nadav Amit

[ Thanks for your patience. ]

Quoted 6 lines · Rik van Riel

On Mon, 2025-01-20 at 19:09 +0200, Nadav Amit wrote:

This is the page reclaim code.

Quoted 7 lines

The process that has those other pages mapped might be running on other CPUs simultaneously with the page reclaim code.

Even if we were invalidating one of our own pages this way, there could be other threads in the same process, running while we are in the page reclaim code.

Of course, but there is nothing new here. Let me see where we lose each other by first stating the goal, what you propose, and what I suggest.

We are issuing invlpgb and we need to ensure tlbsync on the same core that initiated invlpgb before arch_tlbbatch_flush() finishes. That’s all that matters for our discussion (correct me if I miss something).

Quoted 7 lines

You solved it by disabling migration and running tlbsync at the end.

I suggest *not* to disable migration, to keep running tlbsync at the arch_tlbbatch_flush() as you do, and if context-switch happens after arch_tlbbatch_add_pending() and before arch_tlbbatch_flush(), to run tlbsync during the context switch.

How would you keep track of CPUs where the tlbsync has NOT happened before arch_tlbbatch_flush()?

That part seems to be missing still.

Signature · 1 line
All Rights Reversed.
#25 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 8 lines · Rik van Riel

How would you keep track of CPUs where the tlbsync has NOT happened before arch_tlbbatch_flush()?

That part seems to be missing still.

You only keep track if there is a pending tlbsync on *your* CPU. No need to track if other CPUs did not issue tlbsync during arch_tlbbatch_add_pending(). If the process that does the reclamation was migrated, a TLBSYNC is issued during the context switch, before that thread that does the reclamation has any chance of being scheduled.

I hope this code changes on top of your would make it clearer:

Review

Quoted 11 lines
(quoted diff) +11 -0
+void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
+					     struct mm_struct *mm,
+					     unsigned long uaddr)
+{
+	if (static_cpu_has(X86_FEATURE_INVLPGB) && mm_global_asid(mm)) {
+		u16 asid = mm_global_asid(mm);
+		/*
+		 * Queue up an asynchronous invalidation. The corresponding
+		 * TLBSYNC is done in arch_tlbbatch_flush(), and must be done
+		 * on the same CPU.
+		 */

#if 0 // remove

Review

Quoted 4 lines
(quoted diff) +4 -0
+		if (!batch->used_invlpgb) {
+			batch->used_invlpgb = true;
+			migrate_disable();
+		}

#endif

		 batch->used_invlpg = true;
		 preempt_disable();

Review

Quoted 4 lines
(quoted diff) +4 -0
+		invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false);
+		/* Do any CPUs supporting INVLPGB need PTI? */
+		if (static_cpu_has(X86_FEATURE_PTI))
+			invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false);
		 this_cpu_write(cpu_tlbstate.pending_tlbsync, true);
		 preempt_enable();
Quoted 10 lines
(quoted diff) +10 -0
+
+		/*
+		 * Some CPUs might still be using a local ASID for this
+		 * process, and require IPIs, while others are using the
+		 * global ASID.
+		 *
+		 * In this corner case we need to do both the broadcast
+		 * TLB invalidation, and send IPIs. The IPIs will help
+		 * stragglers transition to the broadcast ASID.
+		 */

Review

Quoted 10 lines
(quoted diff) +10 -0
+		if (READ_ONCE(mm->context.asid_transition))
+			goto also_send_ipi;
+	} else {
+also_send_ipi:
+		inc_mm_tlb_gen(mm);
+		cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
+	}
+	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
+}
+

Then in switch_mm_irqs_off(), b

	if (this_cpu_read(cpu_tlbstate.pending_tlbsync))
		tlbsync();

Note that when switch_mm_irqs_off() is called due to context switch from context_switch(), finish_task_switch() has still not took place, so the task cannot be scheduled on other cores.

#26 lore raw

Re: [PATCH v5 10/12] x86,tlb: do targeted broadcast flushing from tlbbatch code

Quoted 13 lines · Nadav Amit

Then in switch_mm_irqs_off(), b

	if (this_cpu_read(cpu_tlbstate.pending_tlbsync))
		tlbsync();

Note that when switch_mm_irqs_off() is called due to context switch from
context_switch(), finish_task_switch() has still not took place, so the
task cannot be scheduled on other cores.

That would certainly work.

I'll add that as a separate patch, which the x86 maintainers can either accept or leave out.

I'm not sure what's worse, making reclaim non-migratable, or adding an extra branch (we can probably avoid a cache line miss) to switch_mm_irqs_off.

Signature · 1 line
All Rights Reversed.
#27 lore raw

PATCH v5 11/12 x86/mm: enable AMD translation cache extensions

In reply to Rik van Riel

With AMD TCE (translation cache extensions) only the intermediate mappings that cover the address range zapped by INVLPG / INVLPGB get invalidated, rather than all intermediate mappings getting zapped at every TLB invalidation.

This can help reduce the TLB miss rate, by keeping more intermediate mappings in the cache.

From the AMD manual:

Translation Cache Extension (TCE) Bit. Bit 15, read/write. Setting this bit to 1 changes how the INVLPG, INVLPGB, and INVPCID instructions operate on TLB entries. When this bit is 0, these instructions remove the target PTE from the TLB as well as all upper-level table entries that are cached in the TLB, whether or not they are associated with the target PTE. When this bit is set, these instructions will remove the target PTE and only those upper-level entries that lead to the target PTE in the page table hierarchy, leaving unrelated upper-level entries intact.

---
 arch/x86/include/asm/msr-index.h       | 2 ++
 arch/x86/kernel/cpu/amd.c              | 4 ++++
 tools/arch/x86/include/asm/msr-index.h | 2 ++
 3 files changed, 8 insertions(+)
arch/x86/include/asm/msr-index.h +2 -0
index 3ae84c3b8e6d..dc1c1057f26e 100644
@@ -25,6 +25,7 @@
 #define _EFER_SVME		12 /* Enable virtualization */
 #define _EFER_LMSLE		13 /* Long Mode Segment Limit Enable */
 #define _EFER_FFXSR		14 /* Enable Fast FXSAVE/FXRSTOR */
+#define _EFER_TCE		15 /* Enable Translation Cache Extensions */
 #define _EFER_AUTOIBRS		21 /* Enable Automatic IBRS */
 
 #define EFER_SCE		(1<<_EFER_SCE)
@@ -34,6 +35,7 @@
 #define EFER_SVME		(1<<_EFER_SVME)
 #define EFER_LMSLE		(1<<_EFER_LMSLE)
 #define EFER_FFXSR		(1<<_EFER_FFXSR)
+#define EFER_TCE		(1<<_EFER_TCE)
 #define EFER_AUTOIBRS		(1<<_EFER_AUTOIBRS)
 
 /*
arch/x86/kernel/cpu/amd.c +4 -0
index bcf73775b4f8..21076252a491 100644
@@ -1071,6 +1071,10 @@ static void init_amd(struct cpuinfo_x86 *c)
 
 	/* AMD CPUs don't need fencing after x2APIC/TSC_DEADLINE MSR writes. */
 	clear_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
+
+	/* Enable Translation Cache Extension */
+	if (cpu_feature_enabled(X86_FEATURE_TCE))
+		msr_set_bit(MSR_EFER, _EFER_TCE);
 }
 
 #ifdef CONFIG_X86_32
tools/arch/x86/include/asm/msr-index.h +2 -0
index 3ae84c3b8e6d..dc1c1057f26e 100644
@@ -25,6 +25,7 @@
 #define _EFER_SVME		12 /* Enable virtualization */
 #define _EFER_LMSLE		13 /* Long Mode Segment Limit Enable */
 #define _EFER_FFXSR		14 /* Enable Fast FXSAVE/FXRSTOR */
+#define _EFER_TCE		15 /* Enable Translation Cache Extensions */
 #define _EFER_AUTOIBRS		21 /* Enable Automatic IBRS */
 
 #define EFER_SCE		(1<<_EFER_SCE)
@@ -34,6 +35,7 @@
 #define EFER_SVME		(1<<_EFER_SVME)
 #define EFER_LMSLE		(1<<_EFER_LMSLE)
 #define EFER_FFXSR		(1<<_EFER_FFXSR)
+#define EFER_TCE		(1<<_EFER_TCE)
 #define EFER_AUTOIBRS		(1<<_EFER_AUTOIBRS)
 
 /*
Signature · 1 line
2.47.1
#28 lore raw

PATCH v5 12/12 x86/mm: only invalidate final translations with INVLPGB

In reply to Rik van Riel

Use the INVLPGB_FINAL_ONLY flag when invalidating mappings with INVPLGB. This way only leaf mappings get removed from the TLB, leaving intermediate translations cached.

On the (rare) occasions where we free page tables we do a full flush, ensuring intermediate translations get flushed from the TLB.

---
 arch/x86/include/asm/invlpgb.h | 10 ++++++++--
 arch/x86/mm/tlb.c              |  8 ++++----
 2 files changed, 12 insertions(+), 6 deletions(-)
arch/x86/include/asm/invlpgb.h +8 -2
index 4dfd09e65fa6..418402535319 100644
@@ -63,9 +63,15 @@ static inline void invlpgb_flush_user(unsigned long pcid,
 static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid,
 						unsigned long addr,
 						u16 nr,
-						bool pmd_stride)
+						bool pmd_stride,
+						bool freed_tables)
 {
-	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA);
+	unsigned long flags = INVLPGB_PCID | INVLPGB_VA;
+
+	if (!freed_tables)
+		flags |= INVLPGB_FINAL_ONLY;
+
+	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, flags);
 }
 
 /* Flush all mappings for a given PCID, not including globals. */
arch/x86/mm/tlb.c +4 -4
index 81f847c94321..82fed9fd5ac2 100644
@@ -538,10 +538,10 @@ static void broadcast_tlb_flush(struct flush_tlb_info *info)
 		nr = min(maxnr, (info->end - addr) >> info->stride_shift);
 		nr = max(nr, 1);
 
-		invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd);
+		invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd, info->freed_tables);
 		/* Do any CPUs supporting INVLPGB need PTI? */
 		if (static_cpu_has(X86_FEATURE_PTI))
-			invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd);
+			invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd, info->freed_tables);
 	}
 
 	finish_asid_transition(info);
@@ -1700,10 +1700,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
 			batch->used_invlpgb = true;
 			migrate_disable();
 		}
-		invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false);
+		invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false, false);
 		/* Do any CPUs supporting INVLPGB need PTI? */
 		if (static_cpu_has(X86_FEATURE_PTI))
-			invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false);
+			invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false, false);
 
 		/*
 		 * Some CPUs might still be using a local ASID for this
Signature · 1 line
2.47.1
#29 lore raw
In reply to Rik van Riel

From: [email protected] <[email protected]> Sent: Wednesday, January 15, 2025 6:30 PM

Quoted 31 lines

Add support for broadcast TLB invalidation using AMD's INVLPGB instruction.

This allows the kernel to invalidate TLB entries on remote CPUs without needing to send IPIs, without having to wait for remote CPUs to handle those interrupts, and with less interruption to what was running on those CPUs.

Because x86 PCID space is limited, and there are some very large systems out there, broadcast TLB invalidation is only used for processes that are active on 3 or more CPUs, with the threshold being gradually increased the more the PCID space gets exhausted.

Combined with the removal of unnecessary lru_add_drain calls (see https://lkml.org/lkml/2024/12/19/1388) this results in a nice performance boost for the will-it-scale tlb_flush2_threads test on an AMD Milan system with 36 cores:

- vanilla kernel:           527k loops/second
- lru_add_drain removal:    731k loops/second
- only INVLPGB:             527k loops/second

- lru_add_drain + INVLPGB: 1157k loops/second

Profiling with only the INVLPGB changes showed while TLB invalidation went down from 40% of the total CPU time to only around 4% of CPU time, the contention simply moved to the LRU lock.

Fixing both at the same time about doubles the
number of iterations per second from this case.

Quoted 8 lines

Some numbers closer to real world performance
can be found at Phoronix, thanks to Michael:

https://www.phoronix.com/news/AMD-INVLPGB-Linux-Benefits

The code is now in a state where I am not sure what else needs to be done before it can be merged. If you can think of something, please let me know ;)

Rik --

We had an earlier thread about INVLPGB/TLBSYNC in a VM [1]. It turns out that Hyper-V in the Azure public cloud enables INVLPGB/TLBSYNC in Confidential VMs (CVMs, which conform to the Linux concept of a CoCo VM) running on AMD processors using SEV-SNP. The CPUID instruction in a such a VM reports the enablement as expected. The instructions are *not* enabled in general purpose VMs running on the same AMD processors. The enablement is a natural outgrowth of CoCo VM's wanting to be able to avoid a dependency on the untrusted hypervisor to perform TLB flushes. Of course, Linux hasn't been updated to make use of the instructions in this scenario, and your patch set doesn't use the instructions in all situations. So CoCo VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

For a couple of days, I've been running your v4 patch set in an Azure CVM, just to make sure nothing bad happens. From a basic testing standpoint, no issues. As expected, INVLPGB is used in some cases, and the existing paravirt hypercalls are used in other cases. But I have not fully reviewed your code looking for potential VM-only issues.

All of this is to say "Don't exclude the VM scenario from your thinking." The scenario exists in real life today. I don't have any specific code changes needed for the scenario.

Michael

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 22 lines

v5:
- use byte assembly for compatibility with older toolchains (Borislav, Michael)
- ensure a panic on an invalid number of extra pages (Dave, Tom)
- add cant_migrate() assertion to tlbsync (Jann)
- a bunch more cleanups (Nadav)
- key TCE enabling off X86_FEATURE_TCE (Andrew)
- fix a race between reclaim and ASID transition (Jann)
v4:
- Use only bitmaps to track free global ASIDs (Nadav)
- Improved AMD initialization (Borislav & Tom)
- Various naming and documentation improvements (Peter, Nadav, Tom, Dave)
- Fixes for subtle race conditions (Jann)
v3:
- Remove paravirt tlb_remove_table call (thank you Qi Zheng)
- More suggested cleanups and changelog fixes by Peter and Nadav
v2:
- Apply suggestions by Peter and Borislav (thank you!)
- Fix bug in arch_tlbbatch_flush, where we need to do both
the TLBSYNC, and flush the CPUs that are in the cpumask.
- Some updates to comments and changelogs based on questions.

maintainer
#30 lore raw
Quoted 4 lines · Michael Kelley

So CoCo
VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

That would place a limit on the number of CPUs, to be no larger than the number of available ASIDs.

#31 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Michael Kelley

So CoCo
VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

That would place a limit on the number of CPUs, to be no larger than the number of available ASIDs.

Can you please be specific between PCID (the x86 architectural thing commonly called ASID) or ASID (the thing named by the AMD architecture).

INVLPGB instruction under virt can use PCIDs to its hearts content, but ASIDs are rewritten behind the scenes because VM does not usually know the ASID the VMM assigned to it.

~Andrew

maintainer
#32 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Michael Kelley

So CoCo
VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

That would place a limit on the number of CPUs, to be no larger than the number of available ASIDs.

Quoted 6 lines

Can you please be specific between PCID (the x86 architectural thing commonly called ASID) or ASID (the thing named by the AMD architecture).

INVLPGB instruction under virt can use PCIDs to its hearts content, but ASIDs are rewritten behind the scenes because VM does not usually know the ASID the VMM assigned to it.

Sorry, I had to re-read the documents again to find out what you were talking about since I habitually skip all virt bits when reading.

Urgh @ AMD for adding ASID, although I think I understand what they're doing.

Anyway, PCID, the thing commonly called ASID by pretty much all other architectures using broadcast TLBI.

Since ASIDs be global and independent execution of CPUs means you then need at least one ASID per CPU (when they're all running a different process), you must not have more CPUs than available ASIDs, or you're into trouble.

Since PCID space is 12 bits, we cannot have more than 4k CPUs (minus a few for that other PCID crap we do), or half that when PTI.

And while that sounds like a lot -- its getting easier to hit every passing year, we're having how many CPUs on a single Epyc now? 192*2 or somesuch nonsense IIRC.

So if someone wants to mandate Broadcast TLBI, someone needs to figure out what to do when we hit this limit.

#33 lore raw
In reply to Michael Kelley
Quoted 3 lines · Michael Kelley

So CoCo VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

How would this actually work, though?

One of the reasons that we have the whole TLB_NR_DYN_ASIDS mechanism is that picking a PCID to shove in CR3 is an entirely CPU local operation. The PCID is in a CPU-local namespace and nobody else cares what it is.

Rik obviously now has a system-wide set of IDs which are globally visible and globally valid. But his mechanism is also a bit choosy so that the global resource management doesn't get to be a choke point.

But if we move to INVLPGB-only, we're all of a sudden *forced* to do some kind of wide global resource management, even for single-threaded processes. Rik doesn't have a scheme for that in the current set.

So I think this would be a whole separate conversation from this series.

#34 lore raw

From: Dave Hansen <[email protected]> Sent: Tuesday, January 21, 2025 9:15 AM

Quoted 16 lines · Michael Kelley
Quoted 3 lines

So CoCo VMs may still use the paravirtualization that makes hypercalls to do TLB flushes. It's future work to *always* use INVLPGB (if available) in a CoCo VM.

How would this actually work, though?

One of the reasons that we have the whole TLB_NR_DYN_ASIDS mechanism is that picking a PCID to shove in CR3 is an entirely CPU local operation. The PCID is in a CPU-local namespace and nobody else cares what it is.

Rik obviously now has a system-wide set of IDs which are globally visible and globally valid. But his mechanism is also a bit choosy so that the global resource management doesn't get to be a choke point.

Quoted 5 lines

But if we move to INVLPGB-only, we're all of a sudden *forced* to do some kind of wide global resource management, even for single-threaded processes. Rik doesn't have a scheme for that in the current set.

So I think this would be a whole separate conversation from this series.

Yes, I agree completely.

The topic initially came up when I was wondering whether INVLPGB is ever enabled in a VM, in which case Rik's patch set would be active in the VM. I was somewhat surprised to find that INVLPGB *is* enabled in Azure CoCo VMs. But that turned out to be for the entirely different use case of avoiding a dependency on the untrusted hypervisor to do TLB flushes.

The thread perhaps went a bit too far afield from Rik's patch set, but there's value in having some discussion about the CoCo use case.

Michael

#35 lore raw
In reply to Michael Kelley
Quoted 9 lines · Michael Kelley

We had an earlier thread about INVLPGB/TLBSYNC in a VM [1]. It turns out that Hyper-V in the Azure public cloud enables INVLPGB/TLBSYNC in Confidential VMs (CVMs, which conform to the Linux concept of a CoCo VM) running on AMD processors using SEV-SNP. The CPUID instruction in a such a VM reports the enablement as expected. The instructions are *not* enabled in general purpose VMs running on the same AMD processors. The enablement is a natural outgrowth of CoCo VM's wanting to be able to avoid a dependency on the untrusted hypervisor to perform TLB flushes.

What is this current dependency on the untrusted hypervisor? Is it just the PV TLB flushing optimization for preempted vCPUs? The normal x86 TLB flushing machinery waits for confirmation from the other vCPUs in smp_call_function_many_cond(), and the hypervisor shouldn't be able to fake that confirmation, right?

Can you avoid this issue by disabling the PV TLB flushing optimization?

#36 lore raw

From: Jann Horn <[email protected]> Sent: Tuesday, January 21, 2025 9:22 AM

Quoted 12 lines · Michael Kelley
Quoted 9 lines

We had an earlier thread about INVLPGB/TLBSYNC in a VM [1]. It turns out that Hyper-V in the Azure public cloud enables INVLPGB/TLBSYNC in Confidential VMs (CVMs, which conform to the Linux concept of a CoCo VM) running on AMD processors using SEV-SNP. The CPUID instruction in a such a VM reports the enablement as expected. The instructions are *not* enabled in general purpose VMs running on the same AMD processors. The enablement is a natural outgrowth of CoCo VM's wanting to be able to avoid a dependency on the untrusted hypervisor to perform TLB flushes.

Quoted 2 lines

What is this current dependency on the untrusted hypervisor? Is it just the PV TLB flushing optimization for preempted vCPUs?

On Hyper-V, the PV TLB flushing is a performance optimization to avoid the overhead of the IPIs, and the overhead of trapping the TLB flush instructions to the hypervisor. Both are expensive in a guest, and making a single hypercall is much more efficient. The hypercall can specify flushes on multiple vCPUs, including those that are currently running. In that case the hypervisor may do IPIs to implement the hypercall, but that should be cheaper than the guest doing the IPIs.

Quoted 4 lines

The normal
x86 TLB flushing machinery waits for confirmation from the other vCPUs in smp_call_function_many_cond(), and the hypervisor shouldn't be able to fake that confirmation, right?

Agreed, as long as that confirmation is via in-memory values.

Quoted 2 lines

Can you avoid this issue by disabling the PV TLB flushing optimization?

At least on Hyper-V, I don't think this helps. Disabling the PV hypercalls just reverts to the native instructions, which I think will trap to the hypervisor for emulation (though maybe I'm wrong about this).

Michael

#37 lore raw
Quoted 13 lines · Michael Kelley

From: Jann Horn <[email protected]> Sent: Tuesday, January 21, 2025 9:22 AM

Quoted 12 lines · Michael Kelley
Quoted 9 lines

We had an earlier thread about INVLPGB/TLBSYNC in a VM [1]. It turns out that Hyper-V in the Azure public cloud enables INVLPGB/TLBSYNC in Confidential VMs (CVMs, which conform to the Linux concept of a CoCo VM) running on AMD processors using SEV-SNP. The CPUID instruction in a such a VM reports the enablement as expected. The instructions are *not* enabled in general purpose VMs running on the same AMD processors. The enablement is a natural outgrowth of CoCo VM's wanting to be able to avoid a dependency on the untrusted hypervisor to perform TLB flushes.

Quoted 6 lines
Quoted 2 lines

What is this current dependency on the untrusted hypervisor? Is it just the PV TLB flushing optimization for preempted vCPUs?

On Hyper-V, the PV TLB flushing is a performance optimization to avoid the overhead of the IPIs, and the overhead of trapping the TLB flush instructions to the hypervisor. Both are expensive in a guest, and making

Is the hypervisor even able to trap TLB flushing instructions for a CoCo VM? Looking at vc_handle_exitcode(), I don't see any support for handling #VC caused by TLB invalidation; and my understanding is that the measured guest code would need to include such handling to allow the hypervisor to emulate an instruction? But I don't know how this stuff works particularly well, I might well be totally wrong about this.