Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1323183 > unrolled thread

[RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock()

Started byPeter Zijlstra <peterz@infradead.org>
First post2016-02-01 15:40 +0100
Last post2016-02-01 18:30 +0100
Articles 3 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock() Peter Zijlstra <peterz@infradead.org> - 2016-02-01 15:40 +0100
    Re: [RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock() Will Deacon <will.deacon@arm.com> - 2016-02-01 18:00 +0100
      Re: [RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock() Peter Zijlstra <peterz@infradead.org> - 2016-02-01 18:30 +0100

#1323183 — [RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock()

FromPeter Zijlstra <peterz@infradead.org>
Date2016-02-01 15:40 +0100
Subject[RFC][PATCH] locking/mcs: Fix ordering for mcs_spin_lock()
Message-ID<qXnOa-4aB-211@gated-at.bofh.it>
Given the below patch; we've now got an unconditional full global
barrier in, does this make the MCS spinlock RCsc ?

The 'problem' is that this barrier can happen before we actually acquire
the lock. That is, if we hit arch_mcs_spin_lock_contended() _that_ will
be the acquire barrier and we end up with a SYNC in between unlock and
lock -- ie. not an smp_mb__after_unlock_lock() equivalent.



---
Subject: locking/mcs: Fix ordering for mcs_spin_lock()
From: Peter Zijlstra <peterz@infradead.org>
Date: Mon Feb  1 15:11:28 CET 2016

Similar to commit b4b29f94856a ("locking/osq: Fix ordering of node
initialisation in osq_lock") the use of xchg_acquire() is
fundamentally broken with MCS like constructs.

Furthermore, it turns out we rely on the global transitivity of this
operation because the unlock path observes the pointer with a
READ_ONCE(), not an smp_load_acquire().

This is non-critical because the MCS code isn't actually used and
mostly serves as documentation, a stepping stone to the more complex
things we've build on top of the idea.

Cc: Will Deacon <will.deacon@arm.com>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Reported-by: Andrea Parri <parri.andrea@gmail.com>
Fixes: 3552a07a9c4a ("locking/mcs: Use acquire/release semantics")
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/locking/mcs_spinlock.h |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

--- a/kernel/locking/mcs_spinlock.h
+++ b/kernel/locking/mcs_spinlock.h
@@ -67,7 +67,13 @@ void mcs_spin_lock(struct mcs_spinlock *
 	node->locked = 0;
 	node->next   = NULL;
 
-	prev = xchg_acquire(lock, node);
+	/*
+	 * We rely on the full barrier with global transitivity implied by the
+	 * below xchg() to order the initialization stores above against any
+	 * observation of @node. And to provide the ACQUIRE ordering associated
+	 * with a LOCK primitive.
+	 */
+	prev = xchg(lock, node);
 	if (likely(prev == NULL)) {
 		/*
 		 * Lock acquired, don't need to set node->locked to 1. Threads

[toc] | [next] | [standalone]


#1323318

FromWill Deacon <will.deacon@arm.com>
Date2016-02-01 18:00 +0100
Message-ID<qXpZw-5Jg-17@gated-at.bofh.it>
In reply to#1323183
Hi Peter,

On Mon, Feb 01, 2016 at 03:37:24PM +0100, Peter Zijlstra wrote:
> Given the below patch; we've now got an unconditional full global
> barrier in, does this make the MCS spinlock RCsc ?
> 
> The 'problem' is that this barrier can happen before we actually acquire
> the lock. That is, if we hit arch_mcs_spin_lock_contended() _that_ will
> be the acquire barrier and we end up with a SYNC in between unlock and
> lock -- ie. not an smp_mb__after_unlock_lock() equivalent.

In which case, I don't think the lock will be RCsc with this change;
you'd need an smp_mb__after_unlock_lock() after
arch_mcs_spin_lock_contended(...) if you wanted the thing to be RCsc.

> Subject: locking/mcs: Fix ordering for mcs_spin_lock()
> From: Peter Zijlstra <peterz@infradead.org>
> Date: Mon Feb  1 15:11:28 CET 2016
> 
> Similar to commit b4b29f94856a ("locking/osq: Fix ordering of node
> initialisation in osq_lock") the use of xchg_acquire() is
> fundamentally broken with MCS like constructs.
> 
> Furthermore, it turns out we rely on the global transitivity of this
> operation because the unlock path observes the pointer with a
> READ_ONCE(), not an smp_load_acquire().
> 
> This is non-critical because the MCS code isn't actually used and
> mostly serves as documentation, a stepping stone to the more complex
> things we've build on top of the idea.
> 
> Cc: Will Deacon <will.deacon@arm.com>
> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
> Reported-by: Andrea Parri <parri.andrea@gmail.com>
> Fixes: 3552a07a9c4a ("locking/mcs: Use acquire/release semantics")
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  kernel/locking/mcs_spinlock.h |    8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)

Acked-by: Will Deacon <will.deacon@arm.com>

Although I wonder how useful this is as a documentation aid now that we
have the osq_lock.

Will

[toc] | [prev] | [next] | [standalone]


#1323338

FromPeter Zijlstra <peterz@infradead.org>
Date2016-02-01 18:30 +0100
Message-ID<qXqsy-6bL-11@gated-at.bofh.it>
In reply to#1323318
On Mon, Feb 01, 2016 at 04:58:13PM +0000, Will Deacon wrote:
> Hi Peter,
> 
> On Mon, Feb 01, 2016 at 03:37:24PM +0100, Peter Zijlstra wrote:
> > Given the below patch; we've now got an unconditional full global
> > barrier in, does this make the MCS spinlock RCsc ?
> > 
> > The 'problem' is that this barrier can happen before we actually acquire
> > the lock. That is, if we hit arch_mcs_spin_lock_contended() _that_ will
> > be the acquire barrier and we end up with a SYNC in between unlock and
> > lock -- ie. not an smp_mb__after_unlock_lock() equivalent.
> 
> In which case, I don't think the lock will be RCsc with this change;
> you'd need an smp_mb__after_unlock_lock() after
> arch_mcs_spin_lock_contended(...) if you wanted the thing to be RCsc.

Right, I think it works for TSO, but in general it makes my head hurt.

> > This is non-critical because the MCS code isn't actually used and
> > mostly serves as documentation, a stepping stone to the more complex
> > things we've build on top of the idea.
> 
> Although I wonder how useful this is as a documentation aid now that we
> have the osq_lock.

So the OSQ thing is horribly complex, pure MCS is a nice step-stone.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web