Skip to content

Commit 758c937

Browse files
amlutoKAGA-KOKO
authored andcommitted
membarrier: Explicitly sync remote cores when SYNC_CORE is requested
membarrier() does not explicitly sync_core() remote CPUs; instead, it relies on the assumption that an IPI will result in a core sync. On x86, this may be true in practice, but it's not architecturally reliable. In particular, the SDM and APM do not appear to guarantee that interrupt delivery is serializing. While IRET does serialize, IPI return can schedule, thereby switching to another task in the same mm that was sleeping in a syscall. The new task could then SYSRET back to usermode without ever executing IRET. Make this more robust by explicitly calling sync_core_before_usermode() on remote cores. (This also helps people who search the kernel tree for instances of sync_core() and sync_core_before_usermode() -- one might be surprised that the core membarrier code doesn't currently show up in a such a search.) 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/776b448d5f7bd6b12690707f5ed67bcda7f1d427.1607058304.git.luto@kernel.org
1 parent 2ecedd7 commit 758c937

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kernel/sched/membarrier.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ static void ipi_mb(void *info)
3838
smp_mb(); /* IPIs should be serializing but paranoid. */
3939
}
4040

41+
static void ipi_sync_core(void *info)
42+
{
43+
/*
44+
* The smp_mb() in membarrier after all the IPIs is supposed to
45+
* ensure that memory on remote CPUs that occur before the IPI
46+
* become visible to membarrier()'s caller -- see scenario B in
47+
* the big comment at the top of this file.
48+
*
49+
* A sync_core() would provide this guarantee, but
50+
* sync_core_before_usermode() might end up being deferred until
51+
* after membarrier()'s smp_mb().
52+
*/
53+
smp_mb(); /* IPIs should be serializing but paranoid. */
54+
55+
sync_core_before_usermode();
56+
}
57+
4158
static void ipi_rseq(void *info)
4259
{
4360
/*
@@ -162,6 +179,7 @@ static int membarrier_private_expedited(int flags, int cpu_id)
162179
if (!(atomic_read(&mm->membarrier_state) &
163180
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
164181
return -EPERM;
182+
ipi_func = ipi_sync_core;
165183
} else if (flags == MEMBARRIER_FLAG_RSEQ) {
166184
if (!IS_ENABLED(CONFIG_RSEQ))
167185
return -EINVAL;

0 commit comments

Comments
 (0)