diff mbox series

[v3,2/4] membarrier: Add an actual barrier before rseq_preempt()

Message ID d3e7197e034fa4852afcf370ca49c30496e58e40.1607058304.git.luto@kernel.org
State Accepted
Commit 2ecedd7569080fd05c1a457e8af2165afecfa29f
Headers show
Series [v3,1/4] x86/membarrier: Get rid of a dubious optimization | expand

Commit Message

Andy Lutomirski Dec. 4, 2020, 5:07 a.m. UTC
It seems to me that most RSEQ membarrier users will expect any
stores done before the membarrier() syscall to be visible to the
target task(s).  While this is extremely likely to be true in
practice, nothing actually guarantees it by a strict reading of the
x86 manuals.  Rather than providing this guarantee by accident and
potentially causing a problem down the road, just add an explicit
barrier.

Cc: stable@vger.kernel.org
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
---
 kernel/sched/membarrier.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Andy Lutomirski Dec. 9, 2020, 4:12 a.m. UTC | #1
On Thu, Dec 3, 2020 at 9:07 PM Andy Lutomirski <luto@kernel.org> wrote:
>

> It seems to me that most RSEQ membarrier users will expect any

> stores done before the membarrier() syscall to be visible to the

> target task(s).  While this is extremely likely to be true in

> practice, nothing actually guarantees it by a strict reading of the

> x86 manuals.  Rather than providing this guarantee by accident and

> potentially causing a problem down the road, just add an explicit

> barrier.

>

> Cc: stable@vger.kernel.org


Fixes: 2a36ab717e8f ("rseq/membarrier: Add
MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ")

which is new in 5.10, so it doesn't need cc:stable if it makes 5.10.
Andy Lutomirski Dec. 14, 2020, 6:05 p.m. UTC | #2
On Wed, Dec 9, 2020 at 12:42 AM tip-bot2 for Andy Lutomirski
<tip-bot2@linutronix.de> wrote:
>

> The following commit has been merged into the x86/urgent branch of tip:

>

> Commit-ID:     2ecedd7569080fd05c1a457e8af2165afecfa29f

> Gitweb:        https://git.kernel.org/tip/2ecedd7569080fd05c1a457e8af2165afecfa29f

> Author:        Andy Lutomirski <luto@kernel.org>

> AuthorDate:    Thu, 03 Dec 2020 21:07:04 -08:00

> Committer:     Thomas Gleixner <tglx@linutronix.de>

> CommitterDate: Wed, 09 Dec 2020 09:37:43 +01:00

>

> membarrier: Add an actual barrier before rseq_preempt()

>

> It seems that most RSEQ membarrier users will expect any stores done before

> the membarrier() syscall to be visible to the target task(s).  While this

> is extremely likely to be true in practice, nothing actually guarantees it

> by a strict reading of the x86 manuals.  Rather than providing this

> guarantee by accident and potentially causing a problem down the road, just

> add an explicit barrier.

>

> Fixes: 70216e18e519 ("membarrier: Provide core serializing command, *_SYNC_CORE")


Whoops, this got mangled on its way to tip.  This should be:

Fixes: 2a36ab717e8f ("rseq/membarrier: Add
MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ")

and this patch does not need to be backported.
diff mbox series

Patch

diff --git a/kernel/sched/membarrier.c b/kernel/sched/membarrier.c
index 5a40b3828ff2..6251d3d12abe 100644
--- a/kernel/sched/membarrier.c
+++ b/kernel/sched/membarrier.c
@@ -168,6 +168,14 @@  static void ipi_mb(void *info)
 
 static void ipi_rseq(void *info)
 {
+	/*
+	 * Ensure that all stores done by the calling thread are visible
+	 * to the current task before the current task resumes.  We could
+	 * probably optimize this away on most architectures, but by the
+	 * time we've already sent an IPI, the cost of the extra smp_mb()
+	 * is negligible.
+	 */
+	smp_mb();
 	rseq_preempt(current);
 }