Skip to content

Commit ec225f8

Browse files
yosrym93hansendc
authored andcommitted
x86/mm: Fix LAM inconsistency during context switch
LAM can only be enabled when a process is single-threaded. But _kernel_ threads can temporarily use a single-threaded process's mm. That means that a context-switching kernel thread can race and observe the mm's LAM metadata (mm->context.lam_cr3_mask) change. The context switch code does two logical things with that metadata: populate CR3 and populate 'cpu_tlbstate.lam'. If it hits this race, 'cpu_tlbstate.lam' and CR3 can end up out of sync. This de-synchronization is currently harmless. But it is confusing and might lead to warnings or real bugs. Update set_tlbstate_lam_mode() to take in the LAM mask and untag mask instead of an mm_struct pointer, and while we are at it, rename it to cpu_tlbstate_update_lam(). This should also make it clearer that we are updating cpu_tlbstate. In switch_mm_irqs_off(), read the LAM mask once and use it for both the cpu_tlbstate update and the CR3 update. Signed-off-by: Yosry Ahmed <yosryahmed@google.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Link: https://lore.kernel.org/all/20240702132139.3332013-3-yosryahmed%40google.com
1 parent 3b299b9 commit ec225f8

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

arch/x86/include/asm/mmu_context.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
8888
#ifdef CONFIG_ADDRESS_MASKING
8989
static inline unsigned long mm_lam_cr3_mask(struct mm_struct *mm)
9090
{
91-
return mm->context.lam_cr3_mask;
91+
/*
92+
* When switch_mm_irqs_off() is called for a kthread, it may race with
93+
* LAM enablement. switch_mm_irqs_off() uses the LAM mask to do two
94+
* things: populate CR3 and populate 'cpu_tlbstate.lam'. Make sure it
95+
* reads a single value for both.
96+
*/
97+
return READ_ONCE(mm->context.lam_cr3_mask);
9298
}
9399

94100
static inline void dup_lam(struct mm_struct *oldmm, struct mm_struct *mm)

arch/x86/include/asm/tlbflush.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,10 @@ static inline u64 tlbstate_lam_cr3_mask(void)
399399
return lam << X86_CR3_LAM_U57_BIT;
400400
}
401401

402-
static inline void set_tlbstate_lam_mode(struct mm_struct *mm)
402+
static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask)
403403
{
404-
this_cpu_write(cpu_tlbstate.lam,
405-
mm->context.lam_cr3_mask >> X86_CR3_LAM_U57_BIT);
406-
this_cpu_write(tlbstate_untag_mask, mm->context.untag_mask);
404+
this_cpu_write(cpu_tlbstate.lam, lam >> X86_CR3_LAM_U57_BIT);
405+
this_cpu_write(tlbstate_untag_mask, untag_mask);
407406
}
408407

409408
#else
@@ -413,7 +412,7 @@ static inline u64 tlbstate_lam_cr3_mask(void)
413412
return 0;
414413
}
415414

416-
static inline void set_tlbstate_lam_mode(struct mm_struct *mm)
415+
static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask)
417416
{
418417
}
419418
#endif

arch/x86/kernel/process_64.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,10 +801,12 @@ static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr)
801801
static void enable_lam_func(void *__mm)
802802
{
803803
struct mm_struct *mm = __mm;
804+
unsigned long lam;
804805

805806
if (this_cpu_read(cpu_tlbstate.loaded_mm) == mm) {
806-
write_cr3(__read_cr3() | mm->context.lam_cr3_mask);
807-
set_tlbstate_lam_mode(mm);
807+
lam = mm_lam_cr3_mask(mm);
808+
write_cr3(__read_cr3() | lam);
809+
cpu_tlbstate_update_lam(lam, mm_untag_mask(mm));
808810
}
809811
}
810812

arch/x86/mm/tlb.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/sched/smt.h>
1212
#include <linux/task_work.h>
1313
#include <linux/mmu_notifier.h>
14+
#include <linux/mmu_context.h>
1415

1516
#include <asm/tlbflush.h>
1617
#include <asm/mmu_context.h>
@@ -632,7 +633,6 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next,
632633
}
633634

634635
new_lam = mm_lam_cr3_mask(next);
635-
set_tlbstate_lam_mode(next);
636636
if (need_flush) {
637637
this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
638638
this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
@@ -651,6 +651,7 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next,
651651

652652
this_cpu_write(cpu_tlbstate.loaded_mm, next);
653653
this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid);
654+
cpu_tlbstate_update_lam(new_lam, mm_untag_mask(next));
654655

655656
if (next != prev) {
656657
cr4_update_pce_mm(next);
@@ -697,14 +698,15 @@ void initialize_tlbstate_and_flush(void)
697698
int i;
698699
struct mm_struct *mm = this_cpu_read(cpu_tlbstate.loaded_mm);
699700
u64 tlb_gen = atomic64_read(&init_mm.context.tlb_gen);
701+
unsigned long lam = mm_lam_cr3_mask(mm);
700702
unsigned long cr3 = __read_cr3();
701703

702704
/* Assert that CR3 already references the right mm. */
703705
WARN_ON((cr3 & CR3_ADDR_MASK) != __pa(mm->pgd));
704706

705707
/* LAM expected to be disabled */
706708
WARN_ON(cr3 & (X86_CR3_LAM_U48 | X86_CR3_LAM_U57));
707-
WARN_ON(mm_lam_cr3_mask(mm));
709+
WARN_ON(lam);
708710

709711
/*
710712
* Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization
@@ -723,7 +725,7 @@ void initialize_tlbstate_and_flush(void)
723725
this_cpu_write(cpu_tlbstate.next_asid, 1);
724726
this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id);
725727
this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, tlb_gen);
726-
set_tlbstate_lam_mode(mm);
728+
cpu_tlbstate_update_lam(lam, mm_untag_mask(mm));
727729

728730
for (i = 1; i < TLB_NR_DYN_ASIDS; i++)
729731
this_cpu_write(cpu_tlbstate.ctxs[i].ctx_id, 0);

0 commit comments

Comments
 (0)