Skip to contentSkip to conversation

AMD broadcast TLB invalidation

RFC PATCH Superseded Rik van Riel 3 participants 28 messages Dec 22, 2024 – Dec 24, 2024

Subsystems (3)

v1 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

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.

#1 lore raw

PATCH 1/10 Add X86_FEATURE_INVLPGB definition.

Add the INVPLGB CPUID definition, allowing the kernel to recognize whether the CPU supports the INVLPGB instruction.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 17b6590748c0..b7209d6c3a5f 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" INVLPGB instruction */
 #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 */
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/10 x86,tlb: 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/include/asm/processor.h | 2 ++
 arch/x86/kernel/cpu/amd.c        | 8 ++++++++
 2 files changed, 10 insertions(+)
arch/x86/include/asm/processor.h +2 -0
index 20e6009381ed..dd32a75d5da8 100644
@@ -185,6 +185,8 @@ struct cpuinfo_x86 {
 	u16			booted_cores;
 	/* Index into per_cpu list: */
 	u16			cpu_index;
+	/* Max number of pages invalidated with one INVLPGB */
+	u16			invlpgb_count_max;
 	/*  Is SMT active on this core? */
 	bool			smt_active;
 	u32			microcode;
arch/x86/kernel/cpu/amd.c +8 -0
index 79d2e17f6582..6a6adbe9ae54 100644
@@ -1135,6 +1135,14 @@ 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;
+
+	if (c->extended_cpuid_level < 0x80000008)
+		return;
+
+	cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
+
+	/* Max number of pages INVLPGB can invalidate in one shot */
+	c->invlpgb_count_max = (edx & 0xffff) + 1;
 }
 
 static const struct cpu_dev amd_cpu_dev = {
Signature · 1 line
2.47.1
maintainer
#3 lore raw

Re: [PATCH 02/10] x86,tlb: get INVLPGB count max from CPUID

Quoted 4 lines · 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.

Definitely not replicate a single datum into every CPU's descriptor.

Signature · 1 line
Sent from a small device: formatting sucks and brevity is inevitable.
#4 lore raw

Re: [PATCH 02/10] x86,tlb: get INVLPGB count max from CPUID

Quoted 10 lines · Borislav Petkov
Quoted 5 lines · 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.

Definitely not replicate a single datum into every CPU's descriptor.

Oops. I was under the impression this was only done for the boot CPU.

I'll turn it into a global variable.

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

PATCH 3/10 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  | 98 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/tlbflush.h |  1 +
 2 files changed, 99 insertions(+)
 create mode 100644 arch/x86/include/asm/invlpgb.h
arch/x86/include/asm/invlpgb.h new file +98 -0
new file mode 100644
index 000000000000..ef12ea4a8d65
@@ -0,0 +1,98 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_INVLPGB
+#define _ASM_X86_INVLPGB
+
+#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,
+			    int extra_count, bool pmd_stride, unsigned long flags)
+{
+	u64 rax = addr | flags;
+	u32 ecx = (pmd_stride << 31) | extra_count;
+	u32 edx = (pcid << 16) | asid;
+
+	/*
+	 * The memory clobber is because the whole point is to invalidate
+	 * stale TLB entries and, especially if we're flushing global
+	 * mappings, we don't want the compiler to reorder any subsequent
+	 * memory accesses before the TLB flush.
+	 */
+	asm volatile("invlpgb" : : "a" (rax), "c" (ecx), "d" (edx));
+}
+
+/*
+ * 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
+ * - INVLPGB_VA | INVLPGB_ASID: 	  invalidate TLB entries matching the address & ASID
+ *
+ * The first can be used to invalidate kernel mappings across all processes.
+ * The last can be used to invalidate mappings across both PCIDs of a process when using PTI.
+ */
+#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);
+}
+
+static inline void invlpgb_flush_user_nr(unsigned long pcid, unsigned long addr, int nr,
+					 bool pmd_stride)
+{
+	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA);
+}
+
+/* Flush all mappings for a given ASID, not including globals. */
+static inline void invlpgb_flush_single_asid(unsigned long asid)
+{
+	__invlpgb(asid, 0, 0, 0, 0, INVLPGB_ASID);
+}
+
+/* Flush all mappings for a given PCID, not including globals. */
+static inline void invlpgb_flush_single_pcid(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);
+}
+
+/* Flush addr, including globals, for all PCIDs. */
+static inline void invlpgb_flush_addr(unsigned long addr, int 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);
+}
+
+/* Wait for INVLPGB originated by this CPU to complete. */
+static inline void tlbsync(void)
+{
+	asm volatile("tlbsync");
+}
+
+#endif /* _ASM_X86_INVLPGB */
arch/x86/include/asm/tlbflush.h +1 -0
index 02fc2aa06e9e..1f518fb9abba 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
maintainer
#6 lore raw

Re: [PATCH 03/10] x86,mm: add INVLPGB support code

Quoted 4 lines · Rik van Riel
arch/x86/include/asm/invlpgb.h new file +4 -0
+static inline void __invlpgb(unsigned long asid, unsigned long pcid, unsigned long addr,
+			    int extra_count, bool pmd_stride, unsigned long flags)
+{
+	u64 rax = addr | flags;

arch/x86/include/asm/invlpgb.h line 29

Quoted 10 lines
arch/x86/include/asm/invlpgb.h new file +10 -0
+	u32 ecx = (pmd_stride << 31) | extra_count;
+	u32 edx = (pcid << 16) | asid;
+
+	/*
+	 * The memory clobber is because the whole point is to invalidate
+	 * stale TLB entries and, especially if we're flushing global
+	 * mappings, we don't want the compiler to reorder any subsequent
+	 * memory accesses before the TLB flush.
+	 */
+	asm volatile("invlpgb" : : "a" (rax), "c" (ecx), "d" (edx));

What memory clobber? Is "memory" gone missing?

Quoted 1 line

+}

#7 lore raw

Re: [PATCH 03/10] x86,mm: add INVLPGB support code

Quoted 16 lines · Peter Zijlstra
Quoted 14 lines · Rik van Riel

+static inline void __invlpgb(unsigned long asid, unsigned long pcid, unsigned long addr,

(quoted diff) +1 -0
+			    int extra_count, bool pmd_stride,

unsigned long flags)

arch/x86/include/asm/invlpgb.h new file +7 -0
+{
+	u64 rax = addr | flags;
+	u32 ecx = (pmd_stride << 31) | extra_count;
+	u32 edx = (pcid << 16) | asid;
+
+	/*
+	 * The memory clobber is because the whole point is to

invalidate

(quoted diff) +1 -0
+	 * stale TLB entries and, especially if we're flushing

global

Quoted 8 lines
Quoted 6 lines
(quoted diff) +1 -0
+	 * mappings, we don't want the compiler to reorder any

subsequent

arch/x86/include/asm/invlpgb.h new file +3 -0
+	 * memory accesses before the TLB flush.
+	 */
+	asm volatile("invlpgb" : : "a" (rax), "c" (ecx), "d"

(edx));

What memory clobber? Is "memory" gone missing?

I'm not even sure where that comment came from any more.

We do not need any barriers here, because one of the big points of INVLPGB is that the flush is kicked off asynchronously, and flushes may end up being done out of order, many can be pending simultaneously, etc.

The sync point is the TLBSYNC instruction.

I removed that comment.

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

PATCH 4/10 x86,mm: use INVLPGB for kernel TLB flushes

In reply to Rik van Riel

Use broadcast TLB invalidation for kernel addresses when available.

This stops us from having to send IPIs for kernel TLB flushes.

---
 arch/x86/mm/tlb.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/x86/mm/tlb.c +32 -0
index 6cf881a942bb..09980fb17907 100644
@@ -1077,6 +1077,33 @@ void flush_tlb_all(void)
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
 
+static void broadcast_kernel_range_flush(unsigned long start, unsigned long end)
+{
+	unsigned long addr;
+	unsigned long maxnr = boot_cpu_data.invlpgb_count_max;
+	unsigned long threshold = tlb_single_page_flush_ceiling * maxnr;
+
+	/*
+	 * TLBSYNC only waits for flushes originating on the same CPU.
+	 * Disabling migration allows us to wait on all flushes.
+	 */
+	migrate_disable();
+
+	if (end == TLB_FLUSH_ALL ||
+	    (end - start) > threshold << PAGE_SHIFT) {
+		invlpgb_flush_all();
+	} else {
+		unsigned long nr;
+		for (addr = start; addr < end; addr += nr << PAGE_SHIFT) {
+			nr = min((end - addr) >> PAGE_SHIFT, maxnr);
+			invlpgb_flush_addr(addr, nr);
+		}
+	}
+
+	tlbsync();
+	migrate_enable();
+}
+
 static void do_kernel_range_flush(void *info)
 {
 	struct flush_tlb_info *f = info;
@@ -1089,6 +1116,11 @@ static void do_kernel_range_flush(void *info)
 
 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
+		broadcast_kernel_range_flush(start, end);
+		return;
+	}
+
 	/* Balance as user space task's flush, a bit conservative */
 	if (end == TLB_FLUSH_ALL ||
 	    (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
Signature · 1 line
2.47.1
maintainer
#9 lore raw

Re: [PATCH 04/10] x86,mm: use INVLPGB for kernel TLB flushes

Quoted 18 lines · Rik van Riel

Use broadcast TLB invalidation for kernel addresses when available.

This stops us from having to send IPIs for kernel TLB flushes.

---
 arch/x86/mm/tlb.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/x86/mm/tlb.c +1 -0
index 6cf881a942bb..09980fb17907 100644
@@ -1077,6 +1077,33 @@ void flush_tlb_all(void)
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
 
+static void broadcast_kernel_range_flush(unsigned long start, unsigned long end)

arch/x86/mm/tlb.c void flush_tlb_all(void) · line 1090

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+{
+	unsigned long addr;
+	unsigned long maxnr = boot_cpu_data.invlpgb_count_max;
+	unsigned long threshold = tlb_single_page_flush_ceiling * maxnr;
+
+	/*
+	 * TLBSYNC only waits for flushes originating on the same CPU.
+	 * Disabling migration allows us to wait on all flushes.
+	 */
+	migrate_disable();

So how expensive is all this? That is, I think I would feel better is this were preempt_disable().

Quoted 34 lines
arch/x86/mm/tlb.c +21 -0
+
+	if (end == TLB_FLUSH_ALL ||
+	    (end - start) > threshold << PAGE_SHIFT) {
+		invlpgb_flush_all();
+	} else {
+		unsigned long nr;
+		for (addr = start; addr < end; addr += nr << PAGE_SHIFT) {
+			nr = min((end - addr) >> PAGE_SHIFT, maxnr);
+			invlpgb_flush_addr(addr, nr);
+		}
+	}
+
+	tlbsync();
+	migrate_enable();
+}
+
 static void do_kernel_range_flush(void *info)
 {
 	struct flush_tlb_info *f = info;
@@ -1089,6 +1116,11 @@ static void do_kernel_range_flush(void *info)
 
 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
+		broadcast_kernel_range_flush(start, end);
+		return;
+	}
+
 	/* Balance as user space task's flush, a bit conservative */
 	if (end == TLB_FLUSH_ALL ||
 	    (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
Signature · 1 line
2.47.1
#10 lore raw

Re: [PATCH 04/10] x86,mm: use INVLPGB for kernel TLB flushes

Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Rik van Riel

Use broadcast TLB invalidation for kernel addresses when available.

+static void broadcast_kernel_range_flush(unsigned long start, unsigned long end)

arch/x86/mm/tlb.c +4 -0
+{
+	unsigned long addr;
+	unsigned long maxnr = boot_cpu_data.invlpgb_count_max;
+	unsigned long threshold = tlb_single_page_flush_ceiling *

maxnr;

arch/x86/mm/tlb.c +2 -0
+
+	/*
Quoted 8 lines

arch/x86/mm/tlb.c void flush_tlb_all(void) · line 1090

Quoted 5 lines
(quoted diff) +1 -0
+	 * TLBSYNC only waits for flushes originating on the same

CPU.

arch/x86/mm/tlb.c +3 -0
+	 * Disabling migration allows us to wait on all flushes.
+	 */
+	migrate_disable();

So how expensive is all this? That is, I think I would feel better is this were preempt_disable().

Either should work. If preempt_disable() is cheaper, I'm happy to use that.

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

Re: [PATCH 04/10] x86,mm: use INVLPGB for kernel TLB flushes

Quoted 22 lines · Rik van Riel
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Rik van Riel

Use broadcast TLB invalidation for kernel addresses when available.

+static void broadcast_kernel_range_flush(unsigned long start, unsigned long end)

arch/x86/mm/tlb.c +4 -0
+{
+	unsigned long addr;
+	unsigned long maxnr = boot_cpu_data.invlpgb_count_max;
+	unsigned long threshold = tlb_single_page_flush_ceiling *

maxnr;

arch/x86/mm/tlb.c +2 -0
+
+	/*
Quoted 8 lines

arch/x86/mm/tlb.c void flush_tlb_all(void) · line 1090

Quoted 5 lines
(quoted diff) +1 -0
+	 * TLBSYNC only waits for flushes originating on the same

CPU.

arch/x86/mm/tlb.c +3 -0
+	 * Disabling migration allows us to wait on all flushes.
+	 */
+	migrate_disable();

So how expensive is all this? That is, I think I would feel better is this were preempt_disable().

Quoted 2 lines

Either should work. If preempt_disable() is cheaper, I'm happy to use that.

I'm not sure about cheaper -- but getting arbitrary scheduling delays in the middle of TLBi sounds like waaay to much 'fun'.

maintainer
#12 lore raw

Re: [PATCH 04/10] x86,mm: use INVLPGB for kernel TLB flushes

In reply to Rik van Riel

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

Quoted 5 lines · Rik van Riel
arch/x86/mm/tlb.c +1 -0
@@ -1089,6 +1116,11 @@ static void do_kernel_range_flush(void *info)
 
 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {

cpu_feature_enabled

Signature · 1 line
Sent from a small device: formatting sucks and brevity is inevitable.
#13 lore raw

PATCH 5/10 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 | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/mm/tlb.c +5 -0
index 09980fb17907..bf85cd0590d5 100644
@@ -1074,6 +1074,11 @@ static void do_flush_tlb_all(void *info)
 void flush_tlb_all(void)
 {
 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
+		invlpgb_flush_all();
+		tlbsync();
+		return;
+	}
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
 
Signature · 1 line
2.47.1
maintainer
#14 lore raw

Re: [PATCH 05/10] x86,tlb: use INVLPGB in flush_tlb_all

Quoted 13 lines · 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 | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/mm/tlb.c +0 -0
index 09980fb17907..bf85cd0590d5 100644

arch/x86/mm/tlb.c static void do_flush_tlb_all(void *info) · line 1077

Quoted 4 lines
arch/x86/mm/tlb.c +1 -0
@@ -1074,6 +1074,11 @@ static void do_flush_tlb_all(void *info)
 void flush_tlb_all(void)
 {
 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
		guard(preempt)();

?

Quoted 10 lines
arch/x86/mm/tlb.c +4 -0
+		invlpgb_flush_all();
+		tlbsync();
+		return;
+	}
 	on_each_cpu(do_flush_tlb_all, NULL, 1);
 }
 
Signature · 1 line
2.47.1
#15 lore raw

PATCH 6/10 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 | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/mm/tlb.c +7 -0
index bf85cd0590d5..9422b10edec1 100644
@@ -1313,6 +1313,12 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 
 	int cpu = get_cpu();
 
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
+		invlpgb_flush_all_nonglobals();
+		tlbsync();
+		goto out_put_cpu;
+	}
+
 	info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false,
 				  TLB_GENERATION_INVALID);
 	/*
@@ -1332,6 +1338,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	cpumask_clear(&batch->cpumask);
 
 	put_flush_tlb_info();
+out_put_cpu:
 	put_cpu();
 }
 
Signature · 1 line
2.47.1
maintainer
#16 lore raw

Re: [PATCH 06/10] x86,mm: use broadcast TLB flushing for page reclaim TLB flushing

Quoted 16 lines · 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 | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/mm/tlb.c +0 -0
index bf85cd0590d5..9422b10edec1 100644

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

Quoted 8 lines
arch/x86/mm/tlb.c +5 -0
@@ -1313,6 +1313,12 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 
 	int cpu = get_cpu();
 
+	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
+		invlpgb_flush_all_nonglobals();
+		tlbsync();
+		goto out_put_cpu;
+	}

Urgh, move this before the get_cpu(), and write it like:

	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
		guard(preempt)();
		invlpgb_flush_all_nonglobals();
		tlbsync();
		return;	
	}

?

Quoted 15 lines
arch/x86/mm/tlb.c +2 -0
+
 	info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false,
 				  TLB_GENERATION_INVALID);
 	/*
@@ -1332,6 +1338,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	cpumask_clear(&batch->cpumask);
 
 	put_flush_tlb_info();
+out_put_cpu:
 	put_cpu();
 }
 
Signature · 1 line
2.47.1
#17 lore raw

PATCH 7/10 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 |  12 ++
 arch/x86/include/asm/tlbflush.h    |  15 ++
 arch/x86/mm/tlb.c                  | 322 ++++++++++++++++++++++++++++-
 4 files changed, 346 insertions(+), 9 deletions(-)
arch/x86/include/asm/mmu.h +6 -0
index 3b496cdcb74b..a8e8dfa5a520 100644
@@ -48,6 +48,12 @@ typedef struct {
 	unsigned long flags;
 #endif
 
+#ifdef CONFIG_CPU_SUP_AMD
+	struct list_head broadcast_asid_list;
+	u16 broadcast_asid;
+	bool asid_transition;
+#endif
+
 #ifdef CONFIG_ADDRESS_MASKING
 	/* Active LAM mode:  X86_CR3_LAM_U48 or X86_CR3_LAM_U57 or 0 (disabled) */
 	unsigned long lam_cr3_mask;
arch/x86/include/asm/mmu_context.h +12 -0
index 795fdd53bd0a..0dc446c427d2 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_broadcast_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,13 @@ static inline int init_new_context(struct task_struct *tsk,
 		mm->context.execute_only_pkey = -1;
 	}
 #endif
+
+#ifdef CONFIG_CPU_SUP_AMD
+	INIT_LIST_HEAD(&mm->context.broadcast_asid_list);
+	mm->context.broadcast_asid = 0;
+	mm->context.asid_transition = false;
+#endif
+
 	mm_reset_untag_mask(mm);
 	init_new_context_ldt(mm);
 	return 0;
@@ -170,6 +179,9 @@ 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_CPU_SUP_AMD
+	destroy_context_free_broadcast_asid(mm);
+#endif
 }
 
 extern void switch_mm(struct mm_struct *prev, struct mm_struct *next,
arch/x86/include/asm/tlbflush.h +15 -0
index 1f518fb9abba..a59f56c9b355 100644
@@ -65,6 +65,21 @@ static inline void cr4_clear_bits(unsigned long mask)
  */
 #define TLB_NR_DYN_ASIDS	6
 
+#ifdef CONFIG_CPU_SUP_AMD
+#define is_dyn_asid(asid) (asid) < TLB_NR_DYN_ASIDS
+#define is_broadcast_asid(asid) (asid) >= TLB_NR_DYN_ASIDS
+#define in_asid_transition(info) (info->mm && info->mm->context.asid_transition)
+#else
+#define is_dyn_asid(asid) true
+#define is_broadcast_asid(asid) false
+#define in_asid_transition(info) false
+
+inline bool needs_broadcast_asid_reload(struct mm_struct *next, u16 prev_asid)
+{
+	return false;
+}
+#endif
+
 struct tlb_context {
 	u64 ctx_id;
 	u64 tlb_gen;
arch/x86/mm/tlb.c +313 -9

409 lines not on this page · raw message

Signature · 1 line
2.47.1
maintainer
#18 lore raw

Re: [PATCH 07/10] x86,mm: enable broadcast TLB invalidation for multi-threaded processes

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 272

Quoted 5 lines · Rik van Riel
arch/x86/mm/tlb.c +5 -0
+#ifdef CONFIG_CPU_SUP_AMD
+/*
+ * Logic for AMD INVLPGB support.
+ */
+static DEFINE_SPINLOCK(broadcast_asid_lock);

RAW_SPINLOCK ?

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 282

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+static u16 last_broadcast_asid = TLB_NR_DYN_ASIDS;
+static DECLARE_BITMAP(broadcast_asid_used, MAX_ASID_AVAILABLE) = { 0 };
+static LIST_HEAD(broadcast_asid_list);
+static int broadcast_asid_available = MAX_ASID_AVAILABLE - TLB_NR_DYN_ASIDS - 1;
+
+static void reset_broadcast_asid_space(void)
+{
+	mm_context_t *context;
+
+	assert_spin_locked(&broadcast_asid_lock);
	lockdep_assert_locked(&broadcast_asid_lock);
Quoted 12 lines
arch/x86/mm/tlb.c +12 -0
+
+	/*
+	 * Flush once when we wrap around the ASID space, so we won't need
+	 * to flush every time we allocate an ASID for boradcast flushing.
+	 */
+	invlpgb_flush_all_nonglobals();
+	tlbsync();
+
+	/*
+	 * Leave the currently used broadcast ASIDs set in the bitmap, since
+	 * those cannot be reused before the next wraparound and flush..
+	 */

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 304

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+	bitmap_clear(broadcast_asid_used, 0, MAX_ASID_AVAILABLE);
+	list_for_each_entry(context, &broadcast_asid_list, broadcast_asid_list)
+		__set_bit(context->broadcast_asid, broadcast_asid_used);
+
+	last_broadcast_asid = TLB_NR_DYN_ASIDS;
+}
+
+static u16 get_broadcast_asid(void)
+{
+	assert_spin_locked(&broadcast_asid_lock);

lockdep_assert_locked

Quoted 26 lines
arch/x86/mm/tlb.c +26 -0
+
+	do {
+		u16 start = last_broadcast_asid;
+		u16 asid = find_next_zero_bit(broadcast_asid_used, MAX_ASID_AVAILABLE, start);
+
+		if (asid >= MAX_ASID_AVAILABLE) {
+			reset_broadcast_asid_space();
+			continue;
+		}
+
+		/* Try claiming this broadcast ASID. */
+		if (!test_and_set_bit(asid, broadcast_asid_used)) {
+			last_broadcast_asid = asid;
+			return asid;
+		}
+	} while (1);
+}
+
+/*
+ * Returns true if the mm is transitioning from a CPU-local ASID to a broadcast
+ * (INVLPGB) ASID, or the other way around.
+ */
+static bool needs_broadcast_asid_reload(struct mm_struct *next, u16 prev_asid)
+{
+	u16 broadcast_asid = next->context.broadcast_asid;
+

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 340

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+	if (broadcast_asid && prev_asid != broadcast_asid) {
+		return true;
+	}
+
+	if (!broadcast_asid && is_broadcast_asid(prev_asid)) {
+		return true;
+	}
+
+	return false;
+}

Those return statements don't really need {} on.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+void destroy_context_free_broadcast_asid(struct mm_struct *mm) {

{ goes on a new line.

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 348

Quoted 6 lines
arch/x86/mm/tlb.c +6 -0
+	unsigned long flags;
+
+	if (!mm->context.broadcast_asid)
+		return;
+
+	spin_lock_irqsave(&broadcast_asid_lock, flags);
	guard(raw_spin_lock_irqsave)(&broadcast_asid_lock);
Quoted 23 lines
arch/x86/mm/tlb.c +23 -0
+	mm->context.broadcast_asid = 0;
+	list_del(&mm->context.broadcast_asid_list);
+	broadcast_asid_available++;
+	spin_unlock_irqrestore(&broadcast_asid_lock, flags);
+}
+
+static int mm_active_cpus(struct mm_struct *mm)
+{
+	int count = 0;
+	int cpu;
+
+	for_each_cpu(cpu, mm_cpumask(mm)) {
+		/* Skip the CPUs that aren't really running this process. */
+		if (per_cpu(cpu_tlbstate.loaded_mm, cpu) != mm)
+			continue;
+
+		if (per_cpu(cpu_tlbstate_shared.is_lazy, cpu))
+			continue;
+
+		count++;
+	}
+	return count;
+}

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 381

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+
+/*
+ * Assign a broadcast ASID to the current process, protecting against
+ * races between multiple threads in the process.
+ */
+static void use_broadcast_asid(struct mm_struct *mm)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&broadcast_asid_lock, flags);
	guard(raw_spin_lock_irqsave)(&broadcast_asid_lock);

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 385

Quoted 4 lines
arch/x86/mm/tlb.c +4 -0
+
+	/* This process is already using broadcast TLB invalidation. */
+	if (mm->context.broadcast_asid)
+		goto out_unlock;
		return;

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 392

Quoted 6 lines
arch/x86/mm/tlb.c +6 -0
+	mm->context.broadcast_asid = get_broadcast_asid();
+	mm->context.asid_transition = true;
+	list_add(&mm->context.broadcast_asid_list, &broadcast_asid_list);
+	broadcast_asid_available--;
+
+out_unlock:

Notably we're really wanting to get away from the whole goto unlock pattern.

Quoted 5 lines
arch/x86/mm/tlb.c +5 -0
+	spin_unlock_irqrestore(&broadcast_asid_lock, flags);
+}
+
+/*
+ * Figure out whether to assign a broadcast (global) ASID to a process.

arch/x86/mm/tlb.c static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, · line 407

Quoted 10 lines
arch/x86/mm/tlb.c +10 -0
+ * We vary the threshold by how empty or full broadcast ASID space is.
+ * 1/4 full: >= 4 active threads
+ * 1/2 full: >= 8 active threads
+ * 3/4 full: >= 16 active threads
+ * 7/8 full: >= 32 active threads
+ * etc
+ *
+ * This way we should never exhaust the broadcast ASID space, even on very
+ * large systems, and the processes with the largest number of active
+ * threads should be able to use broadcast TLB invalidation.

I'm a little confused, at most we need one ASID per CPU, IIRC we have something like 4k ASIDs (page-offset bits in the physical address bits) so for anything with less than 4K CPUs we're good, but with anything having more CPUs we're up a creek irrespective of the above scheme, no?

Quoted 30 lines

+ */

arch/x86/mm/tlb.c +29 -0
+#define HALFFULL_THRESHOLD 8
+static bool meets_broadcast_asid_threshold(struct mm_struct *mm)
+{
+	int avail = broadcast_asid_available;
+	int threshold = HALFFULL_THRESHOLD;
+	int mm_active_threads;
+
+	if (!avail)
+		return false;
+
+	mm_active_threads = mm_active_cpus(mm);
+
+	/* Small processes can just use IPI TLB flushing. */
+	if (mm_active_threads < 3)
+		return false;
+
+	if (avail > MAX_ASID_AVAILABLE * 3 / 4) {
+		threshold = HALFFULL_THRESHOLD / 4;
+	} else if (avail > MAX_ASID_AVAILABLE / 2) {
+		threshold = HALFFULL_THRESHOLD / 2;
+	} else if (avail < MAX_ASID_AVAILABLE / 3) {
+		do {
+			avail *= 2;
+			threshold *= 2;
+		} while ((avail + threshold ) < MAX_ASID_AVAILABLE / 2);
+	}
+
+	return mm_active_threads > threshold;
+}
#19 lore raw

Re: [PATCH 07/10] x86,mm: enable broadcast TLB invalidation for multi-threaded processes

Quoted 19 lines · Peter Zijlstra
Quoted 8 lines · Rik van Riel

+/*
+ * Figure out whether to assign a broadcast (global) ASID to a process.
+ * We vary the threshold by how empty or full broadcast ASID space is.
+ * 1/4 full: >= 4 active threads
+ * 1/2 full: >= 8 active threads
+ * 3/4 full: >= 16 active threads

Quoted 8 lines

+ * 7/8 full: >= 32 active threads
+ * etc
+ *
+ * This way we should never exhaust the broadcast ASID space, even on very
+ * large systems, and the processes with the largest number of active
+ * threads should be able to use broadcast TLB invalidation.

Quoted 6 lines

I'm a little confused, at most we need one ASID per CPU, IIRC we have something like 4k ASIDs (page-offset bits in the physical address bits)
so for anything with less than 4K CPUs we're good, but with anything having more CPUs we're up a creek irrespective of the above scheme, no?

We don't need to use broadcast TLB flushing for every process in the system.

If a system with 8196 CPUs runs a few large processes (hundreds, or thousands of active CPUs), we want to ensure those use broadcast TLB flushing with INVLPGB.

We don't really care if some random systemd helper task ends up being relegated to IPI TLB flushing.

The code is meant to ensure that the tasks where INVLPGB will help the most are the most likely to be able to use it.

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

PATCH 8/10 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               | 33 ++++++++++++++++++++++++++++++---
 3 files changed, 34 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 a59f56c9b355..87f9a3725d95 100644
@@ -294,21 +294,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 +30 -3
index 11ecffa26567..0482042e011c 100644
@@ -1614,12 +1614,13 @@ EXPORT_SYMBOL_GPL(__flush_tlb_all);
 void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 {
 	struct flush_tlb_info *info;
-
 	int cpu = get_cpu();
 
-	if (static_cpu_has(X86_FEATURE_INVLPGB)) {
-		invlpgb_flush_all_nonglobals();
+	/* If we issued (asynchronous) INVLPGB flushes, wait for them here. */
+	if (static_cpu_has(X86_FEATURE_INVLPGB) && batch->used_invlpgb) {
 		tlbsync();
+		migrate_enable();
+		batch->used_invlpgb = false;
 		goto out_put_cpu;
 	}
 
@@ -1646,6 +1647,32 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	put_cpu();
 }
 
+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->context.broadcast_asid) {
+		u16 asid = mm->context.broadcast_asid;
+		/*
+		 * 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();
+		}
+		invlpgb_flush_user_nr(kern_pcid(asid), uaddr, 1, 0);
+		/* Do any CPUs supporting INVLPGB need PTI? */
+		if (static_cpu_has(X86_FEATURE_PTI))
+			invlpgb_flush_user_nr(user_pcid(asid), uaddr, 1, 0);
+	} else {
+		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
Signature · 1 line
2.47.1
#21 lore raw

PATCH 9/10 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.

---
 arch/x86/kernel/cpu/amd.c |  8 ++++++++
 arch/x86/mm/tlb.c         | 10 +++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/amd.c +8 -0
index 6a6adbe9ae54..34f85aa18fca 100644
@@ -1143,6 +1143,14 @@ static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
 
 	/* Max number of pages INVLPGB can invalidate in one shot */
 	c->invlpgb_count_max = (edx & 0xffff) + 1;
+
+	/* If supported, enable translation cache extensions (TCE) */
+	cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
+	if (ecx & BIT(17)) {
+		u64 msr = native_read_msr(MSR_EFER);;
+		msr |= BIT(15);
+		wrmsrl(MSR_EFER, msr);
+	}
 }
 
 static const struct cpu_dev amd_cpu_dev = {
arch/x86/mm/tlb.c +7 -3
index 0482042e011c..9b13d97d0fb5 100644
@@ -489,7 +489,7 @@ static void broadcast_tlb_flush(struct flush_tlb_info *info)
 	if (info->stride_shift > PMD_SHIFT)
 		maxnr = 1;
 
-	if (info->end == TLB_FLUSH_ALL) {
+	if (info->end == TLB_FLUSH_ALL || info->freed_tables) {
 		invlpgb_flush_single_pcid(kern_pcid(asid));
 		/* Do any CPUs supporting INVLPGB need PTI? */
 		if (static_cpu_has(X86_FEATURE_PTI))
@@ -1122,7 +1122,7 @@ static void flush_tlb_func(void *info)
 	 *
 	 * The only question is whether to do a full or partial flush.
 	 *
-	 * We do a partial flush if requested and two extra conditions
+	 * We do a partial flush if requested and three extra conditions
 	 * are met:
 	 *
 	 * 1. f->new_tlb_gen == local_tlb_gen + 1.  We have an invariant that
@@ -1149,10 +1149,14 @@ static void flush_tlb_func(void *info)
 	 *    date.  By doing a full flush instead, we can increase
 	 *    local_tlb_gen all the way to mm_tlb_gen and we can probably
 	 *    avoid another flush in the very near future.
+	 *
+	 * 3. No page tables were freed. If page tables were freed, a full
+	 *    flush ensures intermediate translations in the TLB get flushed.
 	 */
 	if (f->end != TLB_FLUSH_ALL &&
 	    f->new_tlb_gen == local_tlb_gen + 1 &&
-	    f->new_tlb_gen == mm_tlb_gen) {
+	    f->new_tlb_gen == mm_tlb_gen &&
+	    !f->freed_tables) {
 		/* Partial flush */
 		unsigned long addr = f->start;
 
Signature · 1 line
2.47.1
maintainer
#22 lore raw

Re: [PATCH 09/10] x86/mm: enable AMD translation cache extensions

Quoted 1 line · Rik van Riel

With AMD TCE (translation cache extensions) only the intermediate mappings

Only the leave mapings, as written this all don't make sense,

Quoted 5 lines

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.

#23 lore raw

Re: [PATCH 09/10] x86/mm: enable AMD translation cache extensions

Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Rik van Riel

With AMD TCE (translation cache extensions) only the intermediate mappings

Only the leave mapings, as written this all don't make sense,

Check out page 513 of the AMD manual:

https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/40332.pdf

"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. This may provide a performance benefit.

Page table management software must be written in a way that takes this behavior into account. Software that was written for a processor that does not cache upper-level table entries may result in stale entries being incorrectly used for translations when TCE is enabled. Software that is compatible with TCE mode will operate in either mode.

For software using INVLPGB to broadcast TLB invalidations, the invalidations are controlled by the EFER.TCE value on the processor executing the INVLPGB instruction.

Before setting TCE, system software should verify that this feature is supported by examining the feature flag CPUID Fn8000_0001_ECX[TCE]. See Section 3.3 “Processor Feature Identification,” on
page 71 for information on using the CPUID instruction"

This suggests that:
1) TCE does control the "don't make sense" behavior :)

2) Wait, does EFER.TCE need to be set on every CPU in the system? Could a system run with TCE set on some CPUs, and cleared on another?!

It would be nice if somebody from AMD could chime in on the latter, and ensure that this patch actually does the right thing :)

I'll incorporate all the other feedback from you and Borislav into the next version of the patch series! Thank you for the feedback.

Signature · 1 line
All Rights Reversed.
maintainer
#24 lore raw

Re: [PATCH 09/10] x86/mm: enable AMD translation cache extensions

Quoted 30 lines · Rik van Riel
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Rik van Riel

With AMD TCE (translation cache extensions) only the intermediate mappings

Only the leave mapings, as written this all don't make sense,

Check out page 513 of the AMD manual:

https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/40332.pdf

"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. This may provide a performance benefit.

Page table management software must be written in a way that takes this behavior into account. Software that was written for a processor that does not cache upper-level table entries may result in stale entries being incorrectly used for translations when TCE is enabled. Software that is compatible with TCE mode will operate in either mode.

Quoted 8 lines

For software using INVLPGB to broadcast TLB invalidations, the invalidations are controlled by the EFER.TCE value on the processor executing the INVLPGB instruction.

Before setting TCE, system software should verify that this feature is supported by examining the feature flag CPUID Fn8000_0001_ECX[TCE]. See Section 3.3 “Processor Feature Identification,” on
page 71 for information on using the CPUID instruction"

So that makes a ton more sense.

Quoted 3 lines

This suggests that:
1) TCE does control the "don't make sense" behavior :)

Quoted 3 lines · you
Quoted 3 lines

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.

And I read that like it would zap only the intermediate mappings rather than the intermediate mappings.

Reading it a wee bit more carefully, I see it's not quite as bad, but still not very clear.

Quoted 3 lines

2) Wait, does EFER.TCE need to be set on every CPU in the system? Could a system run with TCE set on some CPUs, and cleared on another?!

I would imagine it can; I don't think they would recommend anybody do this though.

#25 lore raw

PATCH 10/10 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 leaves intermediate translations cached in the TLB.

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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/invlpgb.h +1 -1
index ef12ea4a8d65..c1478277b9ea 100644
@@ -56,7 +56,7 @@ static inline void invlpgb_flush_user(unsigned long pcid,
 static inline void invlpgb_flush_user_nr(unsigned long pcid, unsigned long addr, int nr,
 					 bool pmd_stride)
 {
-	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA);
+	__invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA | INVLPGB_FINAL_ONLY);
 }
 
 /* Flush all mappings for a given ASID, not including globals. */
Signature · 1 line
2.47.1
maintainer
#26 lore raw
In reply to Rik van Riel
Quoted 6 lines · Rik van Riel

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.

You're going to be needing this somewhere at the start of this series.

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 -1
index fec381533555..c037592c67ef 100644
@@ -61,7 +61,7 @@ void __init native_pv_lock_init(void)
 
 static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)
 {
-	tlb_remove_page(tlb, table);
+	tlb_remove_table(tlb, table);
 }
 
 struct static_key paravirt_steal_enabled;
#27 lore raw
Quoted 9 lines · On Sun, 22 Dec 2024 12:11:13 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · Rik van Riel

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.

You're going to be needing this somewhere at the start of this series.

I like this change, and would be happy to add it.

What exactly is it needed for?

When I add this change, what other things do
I need to be looking for?

Can we just set the function pointer to
tlb_remove_table, and get rid of the function,
like this?

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,