Skip to content

Commit 54e3d94

Browse files
committed
x86/mm: Remove "INVPCID single" feature tracking
From: Dave Hansen <dave.hansen@linux.intel.com> tl;dr: Replace a synthetic X86_FEATURE with a hardware X86_FEATURE and check of existing per-cpu state. == Background == There are three features in play here: 1. Good old Page Table Isolation (PTI) 2. Process Context IDentifiers (PCIDs) which allow entries from multiple address spaces to be in the TLB at once. 3. Support for the "Invalidate PCID" (INVPCID) instruction, specifically the "individual address" mode (aka. mode 0). When all *three* of these are in place, INVPCID can and should be used to flush out individual addresses in the PTI user address space. But there's a wrinkle or two: First, this INVPCID mode is dependent on CR4.PCIDE. Even if X86_FEATURE_INVPCID==1, the instruction may #GP without setting up CR4. Second, TLB flushing is done very early, even before CR4 is fully set up. That means even if PTI, PCID and INVPCID are supported, there is *still* a window where INVPCID can #GP. == Problem == The current code seems to work, but mostly by chance and there are a bunch of ways it can go wrong. It's also somewhat hard to follow since X86_FEATURE_INVPCID_SINGLE is set far away from its lone user. == Solution == Make "INVPCID single" more robust and easier to follow by placing all the logic in one place. Remove X86_FEATURE_INVPCID_SINGLE. Make two explicit checks before using INVPCID: 1. Check that the system supports INVPCID itself (boot_cpu_has()) 2. Then check the CR4.PCIDE shadow to ensures that the CPU can safely use INVPCID for individual address invalidation. The CR4 check *always* works and is not affected by any X86_FEATURE_* twiddling or inconsistencies between the boot and secondary CPUs. This has been tested on non-Meltdown hardware by using pti=on and then flipping PCID and INVPCID support with qemu. == Aside == How does this code even work today? By chance, I think. First, PTI is initialized around the same time that the boot CPU sets CR4.PCIDE=1. There are currently no TLB invalidations when PTI=1 but CR4.PCIDE=0. That means that the X86_FEATURE_INVPCID_SINGLE check is never even reached. this_cpu_has() is also very nasty to use in this context because the boot CPU reaches here before cpu_data(0) has been initialized. It happens to work for X86_FEATURE_INVPCID_SINGLE since it's a software-defined feature but it would fall over for a hardware- derived X86_FEATURE. Reported-by: Jann Horn <jannh@google.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/all/20230718170630.7922E235%40davehans-spike.ostc.intel.com
1 parent 548cb93 commit 54e3d94

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@
198198
#define X86_FEATURE_CAT_L3 ( 7*32+ 4) /* Cache Allocation Technology L3 */
199199
#define X86_FEATURE_CAT_L2 ( 7*32+ 5) /* Cache Allocation Technology L2 */
200200
#define X86_FEATURE_CDP_L3 ( 7*32+ 6) /* Code and Data Prioritization L3 */
201-
#define X86_FEATURE_INVPCID_SINGLE ( 7*32+ 7) /* Effectively INVPCID && CR4.PCIDE=1 */
202201
#define X86_FEATURE_HW_PSTATE ( 7*32+ 8) /* AMD HW-PState */
203202
#define X86_FEATURE_PROC_FEEDBACK ( 7*32+ 9) /* AMD ProcFeedbackInterface */
204203
#define X86_FEATURE_XCOMPACTED ( 7*32+10) /* "" Use compacted XSTATE (XSAVES or XSAVEC) */

arch/x86/mm/init.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,6 @@ static void setup_pcid(void)
307307
* start_secondary().
308308
*/
309309
cr4_set_bits(X86_CR4_PCIDE);
310-
311-
/*
312-
* INVPCID's single-context modes (2/3) only work if we set
313-
* X86_CR4_PCIDE, *and* we INVPCID support. It's unusable
314-
* on systems that have X86_CR4_PCIDE clear, or that have
315-
* no INVPCID support at all.
316-
*/
317-
if (boot_cpu_has(X86_FEATURE_INVPCID))
318-
setup_force_cpu_cap(X86_FEATURE_INVPCID_SINGLE);
319310
} else {
320311
/*
321312
* flush_tlb_all(), as currently implemented, won't work if

arch/x86/mm/tlb.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,21 +1140,28 @@ void flush_tlb_one_kernel(unsigned long addr)
11401140
*/
11411141
STATIC_NOPV void native_flush_tlb_one_user(unsigned long addr)
11421142
{
1143-
u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
1143+
u32 loaded_mm_asid;
1144+
bool cpu_pcide;
11441145

1146+
/* Flush 'addr' from the kernel PCID: */
11451147
asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
11461148

1149+
/* If PTI is off there is no user PCID and nothing to flush. */
11471150
if (!static_cpu_has(X86_FEATURE_PTI))
11481151
return;
11491152

1153+
loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
1154+
cpu_pcide = this_cpu_read(cpu_tlbstate.cr4) & X86_CR4_PCIDE;
1155+
11501156
/*
1151-
* Some platforms #GP if we call invpcid(type=1/2) before CR4.PCIDE=1.
1152-
* Just use invalidate_user_asid() in case we are called early.
1157+
* invpcid_flush_one(pcid>0) will #GP if CR4.PCIDE==0. Check
1158+
* 'cpu_pcide' to ensure that *this* CPU will not trigger those
1159+
* #GP's even if called before CR4.PCIDE has been initialized.
11531160
*/
1154-
if (!this_cpu_has(X86_FEATURE_INVPCID_SINGLE))
1155-
invalidate_user_asid(loaded_mm_asid);
1156-
else
1161+
if (boot_cpu_has(X86_FEATURE_INVPCID) && cpu_pcide)
11571162
invpcid_flush_one(user_pcid(loaded_mm_asid), addr);
1163+
else
1164+
invalidate_user_asid(loaded_mm_asid);
11581165
}
11591166

11601167
void flush_tlb_one_user(unsigned long addr)

0 commit comments

Comments
 (0)