* [PATCH backport] membarrier: Explicitly sync remote cores when SYNC_CORE is requested
@ 2020-12-14 18:00 Andy Lutomirski
2020-12-19 12:46 ` Greg KH
0 siblings, 1 reply; 2+ messages in thread
From: Andy Lutomirski @ 2020-12-14 18:00 UTC (permalink / raw)
To: x86, Mathieu Desnoyers
Cc: LKML, Nicholas Piggin, Arnd Bergmann, Anton Blanchard,
Andy Lutomirski, Thomas Gleixner, stable
commit 758c9373d84168dc7d039cf85a0e920046b17b41 upstream
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: 70216e18e519 ("membarrier: Provide core serializing command, *_SYNC_CORE")
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/776b448d5f7bd6b12690707f5ed67bcda7f1d427.1607058304.git.luto@kernel.org
---
My stable membarrier series depends on commit 2a36ab717e8f
("rseq/membarrier: Add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ"). I don't
think it makes much sense to backport that feature, so here's a backport of
the patch that doesn't need it.
kernel/sched/membarrier.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c
index 168479a7d61b..be0ca3306be8 100644
--- a/kernel/sched/membarrier.c
+++ b/kernel/sched/membarrier.c
@@ -30,6 +30,23 @@ static void ipi_mb(void *info)
smp_mb(); /* IPIs should be serializing but paranoid. */
}
+static void ipi_sync_core(void *info)
+{
+ /*
+ * The smp_mb() in membarrier after all the IPIs is supposed to
+ * ensure that memory on remote CPUs that occur before the IPI
+ * become visible to membarrier()'s caller -- see scenario B in
+ * the big comment at the top of this file.
+ *
+ * A sync_core() would provide this guarantee, but
+ * sync_core_before_usermode() might end up being deferred until
+ * after membarrier()'s smp_mb().
+ */
+ smp_mb(); /* IPIs should be serializing but paranoid. */
+
+ sync_core_before_usermode();
+}
+
static void ipi_sync_rq_state(void *info)
{
struct mm_struct *mm = (struct mm_struct *) info;
@@ -134,6 +151,7 @@ static int membarrier_private_expedited(int flags)
int cpu;
cpumask_var_t tmpmask;
struct mm_struct *mm = current->mm;
+ smp_call_func_t ipi_func = ipi_mb;
if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
@@ -141,6 +159,7 @@ static int membarrier_private_expedited(int flags)
if (!(atomic_read(&mm->membarrier_state) &
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
return -EPERM;
+ ipi_func = ipi_sync_core;
} else {
if (!(atomic_read(&mm->membarrier_state) &
MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
@@ -181,7 +200,7 @@ static int membarrier_private_expedited(int flags)
rcu_read_unlock();
preempt_disable();
- smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
+ smp_call_function_many(tmpmask, ipi_func, NULL, 1);
preempt_enable();
free_cpumask_var(tmpmask);
--
2.29.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH backport] membarrier: Explicitly sync remote cores when SYNC_CORE is requested
2020-12-14 18:00 [PATCH backport] membarrier: Explicitly sync remote cores when SYNC_CORE is requested Andy Lutomirski
@ 2020-12-19 12:46 ` Greg KH
0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2020-12-19 12:46 UTC (permalink / raw)
To: Andy Lutomirski
Cc: x86, Mathieu Desnoyers, LKML, Nicholas Piggin, Arnd Bergmann,
Anton Blanchard, Thomas Gleixner, stable
On Mon, Dec 14, 2020 at 10:00:43AM -0800, Andy Lutomirski wrote:
> commit 758c9373d84168dc7d039cf85a0e920046b17b41 upstream
>
> 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: 70216e18e519 ("membarrier: Provide core serializing command, *_SYNC_CORE")
> Signed-off-by: Andy Lutomirski <luto@kernel.org>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: stable@vger.kernel.org
> Link: https://lore.kernel.org/r/776b448d5f7bd6b12690707f5ed67bcda7f1d427.1607058304.git.luto@kernel.org
> ---
>
> My stable membarrier series depends on commit 2a36ab717e8f
> ("rseq/membarrier: Add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ"). I don't
> think it makes much sense to backport that feature, so here's a backport of
> the patch that doesn't need it.
Now queued up to 5.4.y and 5.9.y, thanks.
greg k-h
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-12-19 12:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-14 18:00 [PATCH backport] membarrier: Explicitly sync remote cores when SYNC_CORE is requested Andy Lutomirski
2020-12-19 12:46 ` Greg KH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®