x86/mm: TLB flush fixes
v2 cover x86/mm: TLB flush fixes
1) Fix a potential overflow in user_pcid_flush_mask. I do not think anybody is hitting this in practice, but they could if they wanted to.
2) Change the early boot initialized value of invlpgb_count_max to 1, to avoid an infinite loop when...
3) Having cpa_flush() call flush_kernel_range(), which results in the INVPLGB code being called very early at boot time.
v2:
- changelog improvements (Dave Hansen)
- get rid of #ifdefs in .c files (Dave Hansen)
PATCH v2 1/3 x86/mm: Fix potential overflow in user_pcid_flush_mask
From: Rik van Riel <[email protected]>
Currently no system with AMD INVLPGB support requires the page table isolation mitigation. However, people could still enable PTI manually, or a vulnerability could be found in the future that makes PTI useful on certain AMD CPUs.
The combination of PTI and broadcast TLB flush has a problem:
- invalidate_user_asid() sets a bit corresponding to the process PCID in user_pcid_flush_mask
- SWITCH_TO_USER_CR3 tests and clears a bit corresponding to the process PCID in user_pcid_flush_mask
When using only ASIDs 0-5 this does not cause any issues, because only PCID numbers 1-6 ever get used.
However, with broadcast TLB flushing PCID numbers up to 2048 can be used, leading to an overflow of the user_pcid_flush_mask, if a system using INVLPGB is booted with the pti=on option.
Enlarge user_pcid_flush_mask to fit the PCID numbers that can be present when using broadcast TLB flushing. This takes up 256 or 512 bytes per CPU, depending on whether or not page table isolation is built into the kernel.
--- arch/x86/include/asm/tlbflush.h | 49 +++++++++++++++++++++++++++------ arch/x86/mm/tlb.c | 22 +-------------- 2 files changed, 42 insertions(+), 29 deletions(-)
arch/x86/include/asm/tlbflush.h +41 -8
@@ -23,6 +23,40 @@ void __flush_tlb_all(void);#define TLB_FLUSH_ALL -1UL#define TLB_GENERATION_INVALID 0 +/* +* When enabled, MITIGATION_PAGE_TABLE_ISOLATION consumes a single bit for +* user/kernel switches +*/ +#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION +# define PTI_CONSUMED_PCID_BITS 1 +#else +# define PTI_CONSUMED_PCID_BITS 0 +#endif + +#define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS) + +/* +* ASIDs are zero-based: 0->MAX_AVAIL_ASID are valid. -1 below to account +* for them being zero-based. Another -1 is because PCID 0 is reserved for +* use by non-PCID-aware users. +*/ +#define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2) + +/* +* With page table isolation, the user_pcid_flush_mask is used to indicate +* that the TLB for a process needs to be flushed when switching to user +* space. Broadcast TLB flushing uses more PCIDs, and a larger bitmap. +*/ +#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION +# ifdef CONFIG_BROADCAST_TLB_FLUSH +# define CR3_AVAIL_PCID_LONGS ((1 << CR3_AVAIL_PCID_BITS) / BITS_PER_LONG) +# else +# define CR3_AVAIL_PCID_LONGS 1 +# endif +#else +# define CR3_AVAIL_PCID_LONGS 0 +#endif +void cr4_update_irqsoff (unsigned long set,unsigned long clear);unsigned long cr4_read_shadow (void );
@@ -115,14 +149,6 @@ struct tlb_state {*/ u8 lam;#endif - -/* -* Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate -* the corresponding user PCID needs a flush next time we -* switch to it; see SWITCH_TO_USER_CR3. -*/ -unsigned short user_pcid_flush_mask; -/* * Access to this CR4 shadow and to H/W CR4 is protected by * disabling interrupts when modifying either one.
@@ -149,6 +175,13 @@ struct tlb_state {* context 0. */ struct tlb_context ctxs[TLB_NR_DYN_ASIDS ]; + +/* +* Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate +* the corresponding user PCID needs a flush next time we +* switch to it; see SWITCH_TO_USER_CR3. +*/ +unsigned long user_pcid_flush_mask[CR3_AVAIL_PCID_LONGS ]; }; DECLARE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate);
arch/x86/mm/tlb.c +1 -21
@@ -90,25 +90,6 @@* */ -/* -* When enabled, MITIGATION_PAGE_TABLE_ISOLATION consumes a single bit for -* user/kernel switches -*/ -#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION -# define PTI_CONSUMED_PCID_BITS 1 -#else -# define PTI_CONSUMED_PCID_BITS 0 -#endif - -#define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS) - -/* -* ASIDs are zero-based: 0->MAX_AVAIL_ASID are valid. -1 below to account -* for them being zero-based. Another -1 is because PCID 0 is reserved for -* use by non-PCID-aware users. -*/ -#define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2) -/* * Given @asid, compute kPCID */
@@ -557,8 +538,7 @@ static inline void invalidate_user_asid(u16 asid)if (!static_cpu_has (X86_FEATURE_PTI ))return ; -__set_bit (kern_pcid (asid), - (unsigned long *)this_cpu_ptr (&cpu_tlbstate.user_pcid_flush_mask)); +__set_bit (kern_pcid (asid),this_cpu_ptr (&cpu_tlbstate.user_pcid_flush_mask[0 ])); } static void load_new_mm_cr3(pgd_t *pgdir, u16 new_asid, unsigned long lam,
Signature · 1 line
2.49.0
Re: [PATCH v2 1/3] x86/mm: Fix potential overflow in user_pcid_flush_mask
Quoted 4 lines · Rik van Riel
arch/x86/include/asm/tlbflush.h +4
-0+/* +* With page table isolation, the user_pcid_flush_mask is used to indicate +* that the TLB for a process needs to be flushed when switching to user +* space. Broadcast TLB flushing uses more PCIDs, and a larger bitmap.
arch/x86/include/asm/tlbflush.h void __flush_tlb_all(void); · line 58
Quoted 10 lines
arch/x86/include/asm/tlbflush.h +10
-0+*/ +#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION +# ifdef CONFIG_BROADCAST_TLB_FLUSH +# define CR3_AVAIL_PCID_LONGS ((1 << CR3_AVAIL_PCID_BITS) / BITS_PER_LONG) +# else +# define CR3_AVAIL_PCID_LONGS 1 +# endif +#else +# define CR3_AVAIL_PCID_LONGS 0 +#endif
Just so nobody goes and applies this...
I don't like how this looks. I'd much rather have the code be concentrating on *bits* of ASID space rather than longs. I'm going to rework this a bit.
PATCH v2 2/3 x86/mm: Fix early boot use of INVPLGB
In reply to Rik van RielThe INVLPGB instruction has limits on how many pages it can invalidate at once. That limit is enumerated in CPUID, read by the kernel, and stored in 'invpgb_count_max'. Ranged invalidation, like invlpgb_kernel_range_flush() break up their invalidations so that they do not exceed the limit.
However, early boot code currently attempts to do ranged invalidation before populating 'invlpgb_count_max'. There is a for loop which is basically:
for (...; addr < end; addr += invlpgb_count_max*PAGE_SIZE)
If invlpgb_kernel_range_flush is called before the kernel has read the value of invlpgb_count_max from the hardware, the normally bounded loop can become an infinite loop if invlpgb_count_max is initialized to zero.
Fix that issue by initializing invlpgb_count_max to 1.
This way INVPLGB at early boot time will be a little bit slower than normal (with initialized invplgb_count_max), and not an instant hang at bootup time.
--- arch/x86/kernel/cpu/amd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/amd.c +1 -1
@@ -31,7 +31,7 @@#include "cpu.h" -u16 invlpgb_count_max__ro_after_init ; +u16 invlpgb_count_max__ro_after_init =1 ;static inline int rdmsrq_amd_safe (unsigned msr,u64 *p) {
Signature · 1 line
2.49.0
PATCH v2 3/3 x86/mm: Change cpa_flush() to call flush_kernel_range() directly
In reply to Rik van RielFrom: Yu-cheng Yu <[email protected]>
The function cpa_flush() calls __flush_tlb_one_kernel() and flush_tlb_all().
Replacing that with a call to flush_tlb_kernel_range() allows cpa_flush() to make use of INVLPGB or RAR without any additional changes.
Initialize invlpgb_count_max to 1, since flush_tlb_kernel_range() can now be called before invlpgb_count_max has been initialized to the value read from CPUID.
[riel: remove now unused __cpa_flush_tlb]
--- arch/x86/mm/pat/set_memory.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
arch/x86/mm/pat/set_memory.c +7 -13
@@ -399,15 +399,6 @@ static void cpa_flush_all(unsigned long cache)on_each_cpu (__cpa_flush_all, (void *) cache,1 ); } -static void __cpa_flush_tlb (void *data) -{ -struct cpa_data *cpa = data; -unsigned int i; - -for (i =0 ; i < cpa->numpages; i++) -flush_tlb_one_kernel (fix_addr (__cpa_addr (cpa, i))); -} -static int collapse_large_pages (unsigned long addr,struct list_head *pgtables);static void cpa_collapse_large_pages (struct cpa_data *cpa)
@@ -444,6 +435,7 @@ static void cpa_collapse_large_pages(struct cpa_data *cpa)static void cpa_flush (struct cpa_data *cpa,int cache) { +unsigned long start, end;unsigned int i;BUG_ON (irqs_disabled () && !early_boot_irqs_disabled);
@@ -453,10 +445,12 @@ static void cpa_flush(struct cpa_data *cpa, int cache) goto collapse_large_pages; } -if (cpa->force_flush_all || cpa->numpages > tlb_single_page_flush_ceiling) -flush_tlb_all (); -else -on_each_cpu (__cpa_flush_tlb, cpa,1 ); + start =fix_addr (__cpa_addr (cpa,0 )); + end =fix_addr (__cpa_addr (cpa, cpa->numpages)); +if (cpa->force_flush_all) + end =TLB_FLUSH_ALL ; + +flush_tlb_kernel_range (start, end);if (!cache) goto collapse_large_pages;
Signature · 1 line
2.49.0