Skip to content

Commit e3c1d51

Browse files
amlutogregkh
authored andcommitted
x86/membarrier: Get rid of a dubious optimization
commit a493d1c upstream. sync_core_before_usermode() had an incorrect optimization. If the kernel returns from an interrupt, it can get to usermode without IRET. It just has to schedule to a different task in the same mm and do SYSRET. Fortunately, there were no callers of sync_core_before_usermode() that could have had in_irq() or in_nmi() equal to true, because it's only ever called from the scheduler. While at it, clarify a related comment. Fixes: 70216e1 ("membarrier: Provide core serializing command, *_SYNC_CORE") Signed-off-by: Andy Lutomirski <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Mathieu Desnoyers <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/5afc7632be1422f91eaf7611aaaa1b5b8580a086.1607058304.git.luto@kernel.org Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6346ed6 commit e3c1d51

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

arch/x86/include/asm/sync_core.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ static inline void sync_core_before_usermode(void)
1616
/* With PTI, we unconditionally serialize before running user code. */
1717
if (static_cpu_has(X86_FEATURE_PTI))
1818
return;
19+
1920
/*
20-
* Return from interrupt and NMI is done through iret, which is core
21-
* serializing.
21+
* Even if we're in an interrupt, we might reschedule before returning,
22+
* in which case we could switch to a different thread in the same mm
23+
* and return using SYSRET or SYSEXIT. Instead of trying to keep
24+
* track of our need to sync the core, just sync right away.
2225
*/
23-
if (in_irq() || in_nmi())
24-
return;
2526
sync_core();
2627
}
2728

arch/x86/mm/tlb.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,14 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
327327
/*
328328
* The membarrier system call requires a full memory barrier and
329329
* core serialization before returning to user-space, after
330-
* storing to rq->curr. Writing to CR3 provides that full
331-
* memory barrier and core serializing instruction.
330+
* storing to rq->curr, when changing mm. This is because
331+
* membarrier() sends IPIs to all CPUs that are in the target mm
332+
* to make them issue memory barriers. However, if another CPU
333+
* switches to/from the target mm concurrently with
334+
* membarrier(), it can cause that CPU not to receive an IPI
335+
* when it really should issue a memory barrier. Writing to CR3
336+
* provides that full memory barrier and core serializing
337+
* instruction.
332338
*/
333339
if (real_prev == next) {
334340
VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) !=

0 commit comments

Comments
 (0)