* [PATCH 0/2] A couple of qspinlock fixes
@ 2018-02-13 13:22 Will Deacon
2018-02-13 13:22 ` [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next Will Deacon
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Will Deacon @ 2018-02-13 13:22 UTC (permalink / raw)
To: linux-kernel; +Cc: peterz, mingo, Will Deacon
Hi all,
Here are a couple of fixes for qspinlock issues I've found by code
inspection whilst investigating the possibility of enabling this for
arm64. The first patch fixes a problem with unusual hardware re-ordering
whilst the second fixes a problem with unusual compiler re-ordering.
Cheers,
Will
--->8
Will Deacon (2):
locking/qspinlock: Ensure node is initialised before updating
prev->next
locking/qspinlock: Ensure node->count is updated before initialising
node
kernel/locking/qspinlock.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
--
2.1.4
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next 2018-02-13 13:22 [PATCH 0/2] A couple of qspinlock fixes Will Deacon @ 2018-02-13 13:22 ` Will Deacon 2018-02-13 15:27 ` [tip:locking/urgent] " tip-bot for Will Deacon 2018-02-13 13:22 ` [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node Will Deacon 2018-02-13 13:45 ` [PATCH 0/2] A couple of qspinlock fixes Peter Zijlstra 2 siblings, 1 reply; 6+ messages in thread From: Will Deacon @ 2018-02-13 13:22 UTC (permalink / raw) To: linux-kernel; +Cc: peterz, mingo, Will Deacon When a locker ends up queuing on the qspinlock locking slowpath, we initialise the relevant mcs node and publish it indirectly by updating the tail portion of the lock word using xchg_tail. If we find that there was a pre-existing locker in the queue, we subsequently update their ->next field to point at our node so that we are notified when it's our turn to take the lock. This can be roughly illustrated as follows: /* Initialise the fields in node and encode a pointer to node in tail */ tail = initialise_node(node); /* * Exchange tail into the lockword using an atomic read-modify-write * operation with release semantics */ old = xchg_tail(lock, tail); /* If there was a pre-existing waiter ... */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); smp_read_barrier_depends(); /* ... then update their ->next field to point to node. WRITE_ONCE(prev->next, node); } The conditional update of prev->next therefore relies on the address dependency from the result of xchg_tail ensuring order against the prior initialisation of node. However, since the release semantics of the xchg_tail operation apply only to the write portion of the RmW, then this ordering is not guaranteed and it is possible for the CPU to return old before the writes to node have been published, consequently allowing us to point prev->next to an uninitialised node. This patch fixes the problem by making the update of prev->next a RELEASE operation, which also removes the reliance on dependency ordering. Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Signed-off-by: Will Deacon <will.deacon@arm.com> --- kernel/locking/qspinlock.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 38ece035039e..348c8cec1042 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -408,14 +408,15 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); + /* - * The above xchg_tail() is also a load of @lock which - * generates, through decode_tail(), a pointer. The address - * dependency matches the RELEASE of xchg_tail() such that - * the subsequent access to @prev happens after. + * We must ensure that the stores to @node are observed before + * the write to prev->next. The address dependency from + * xchg_tail is not sufficient to ensure this because the read + * component of xchg_tail is unordered with respect to the + * initialisation of @node. */ - - WRITE_ONCE(prev->next, node); + smp_store_release(&prev->next, node); pv_wait_node(node, prev); arch_mcs_spin_lock_contended(&node->locked); -- 2.1.4 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [tip:locking/urgent] locking/qspinlock: Ensure node is initialised before updating prev->next 2018-02-13 13:22 ` [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next Will Deacon @ 2018-02-13 15:27 ` tip-bot for Will Deacon 0 siblings, 0 replies; 6+ messages in thread From: tip-bot for Will Deacon @ 2018-02-13 15:27 UTC (permalink / raw) To: linux-tip-commits Cc: tglx, torvalds, linux-kernel, hpa, mingo, will.deacon, peterz Commit-ID: 95bcade33a8af38755c9b0636e36a36ad3789fe6 Gitweb: https://git.kernel.org/tip/95bcade33a8af38755c9b0636e36a36ad3789fe6 Author: Will Deacon <will.deacon@arm.com> AuthorDate: Tue, 13 Feb 2018 13:22:56 +0000 Committer: Ingo Molnar <mingo@kernel.org> CommitDate: Tue, 13 Feb 2018 14:50:14 +0100 locking/qspinlock: Ensure node is initialised before updating prev->next When a locker ends up queuing on the qspinlock locking slowpath, we initialise the relevant mcs node and publish it indirectly by updating the tail portion of the lock word using xchg_tail. If we find that there was a pre-existing locker in the queue, we subsequently update their ->next field to point at our node so that we are notified when it's our turn to take the lock. This can be roughly illustrated as follows: /* Initialise the fields in node and encode a pointer to node in tail */ tail = initialise_node(node); /* * Exchange tail into the lockword using an atomic read-modify-write * operation with release semantics */ old = xchg_tail(lock, tail); /* If there was a pre-existing waiter ... */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); smp_read_barrier_depends(); /* ... then update their ->next field to point to node. WRITE_ONCE(prev->next, node); } The conditional update of prev->next therefore relies on the address dependency from the result of xchg_tail ensuring order against the prior initialisation of node. However, since the release semantics of the xchg_tail operation apply only to the write portion of the RmW, then this ordering is not guaranteed and it is possible for the CPU to return old before the writes to node have been published, consequently allowing us to point prev->next to an uninitialised node. This patch fixes the problem by making the update of prev->next a RELEASE operation, which also removes the reliance on dependency ordering. Signed-off-by: Will Deacon <will.deacon@arm.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1518528177-19169-2-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org> --- kernel/locking/qspinlock.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 38ece03..348c8ce 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -408,14 +408,15 @@ queue: */ if (old & _Q_TAIL_MASK) { prev = decode_tail(old); + /* - * The above xchg_tail() is also a load of @lock which - * generates, through decode_tail(), a pointer. The address - * dependency matches the RELEASE of xchg_tail() such that - * the subsequent access to @prev happens after. + * We must ensure that the stores to @node are observed before + * the write to prev->next. The address dependency from + * xchg_tail is not sufficient to ensure this because the read + * component of xchg_tail is unordered with respect to the + * initialisation of @node. */ - - WRITE_ONCE(prev->next, node); + smp_store_release(&prev->next, node); pv_wait_node(node, prev); arch_mcs_spin_lock_contended(&node->locked); ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node 2018-02-13 13:22 [PATCH 0/2] A couple of qspinlock fixes Will Deacon 2018-02-13 13:22 ` [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next Will Deacon @ 2018-02-13 13:22 ` Will Deacon 2018-02-13 15:27 ` [tip:locking/urgent] " tip-bot for Will Deacon 2018-02-13 13:45 ` [PATCH 0/2] A couple of qspinlock fixes Peter Zijlstra 2 siblings, 1 reply; 6+ messages in thread From: Will Deacon @ 2018-02-13 13:22 UTC (permalink / raw) To: linux-kernel; +Cc: peterz, mingo, Will Deacon When queuing on the qspinlock, the count field for the current CPU's head node is incremented. This needn't be atomic because locking in e.g. IRQ context is balanced and so an IRQ will return with node->count as it found it. However, the compiler could in theory reorder the initialisation of node[idx] before the increment of the head node->count, causing an IRQ to overwrite the initialised node and potentially corrupt the lock state. Avoid the potential for this harmful compiler reordering by placing a barrier() between the increment of the head node->count and the subsequent node initialisation. Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Signed-off-by: Will Deacon <will.deacon@arm.com> --- kernel/locking/qspinlock.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 348c8cec1042..d880296245c5 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -379,6 +379,14 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) tail = encode_tail(smp_processor_id(), idx); node += idx; + + /* + * Ensure that we increment the head node->count before initialising + * the actual node. If the compiler is kind enough to reorder these + * stores, then an IRQ could overwrite our assignments. + */ + barrier(); + node->locked = 0; node->next = NULL; pv_init_node(node); -- 2.1.4 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [tip:locking/urgent] locking/qspinlock: Ensure node->count is updated before initialising node 2018-02-13 13:22 ` [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node Will Deacon @ 2018-02-13 15:27 ` tip-bot for Will Deacon 0 siblings, 0 replies; 6+ messages in thread From: tip-bot for Will Deacon @ 2018-02-13 15:27 UTC (permalink / raw) To: linux-tip-commits Cc: will.deacon, peterz, mingo, hpa, linux-kernel, torvalds, tglx Commit-ID: 11dc13224c975efcec96647a4768a6f1bb7a19a8 Gitweb: https://git.kernel.org/tip/11dc13224c975efcec96647a4768a6f1bb7a19a8 Author: Will Deacon <will.deacon@arm.com> AuthorDate: Tue, 13 Feb 2018 13:22:57 +0000 Committer: Ingo Molnar <mingo@kernel.org> CommitDate: Tue, 13 Feb 2018 14:50:14 +0100 locking/qspinlock: Ensure node->count is updated before initialising node When queuing on the qspinlock, the count field for the current CPU's head node is incremented. This needn't be atomic because locking in e.g. IRQ context is balanced and so an IRQ will return with node->count as it found it. However, the compiler could in theory reorder the initialisation of node[idx] before the increment of the head node->count, causing an IRQ to overwrite the initialised node and potentially corrupt the lock state. Avoid the potential for this harmful compiler reordering by placing a barrier() between the increment of the head node->count and the subsequent node initialisation. Signed-off-by: Will Deacon <will.deacon@arm.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1518528177-19169-3-git-send-email-will.deacon@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org> --- kernel/locking/qspinlock.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 348c8ce..d880296 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -379,6 +379,14 @@ queue: tail = encode_tail(smp_processor_id(), idx); node += idx; + + /* + * Ensure that we increment the head node->count before initialising + * the actual node. If the compiler is kind enough to reorder these + * stores, then an IRQ could overwrite our assignments. + */ + barrier(); + node->locked = 0; node->next = NULL; pv_init_node(node); ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] A couple of qspinlock fixes 2018-02-13 13:22 [PATCH 0/2] A couple of qspinlock fixes Will Deacon 2018-02-13 13:22 ` [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next Will Deacon 2018-02-13 13:22 ` [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node Will Deacon @ 2018-02-13 13:45 ` Peter Zijlstra 2 siblings, 0 replies; 6+ messages in thread From: Peter Zijlstra @ 2018-02-13 13:45 UTC (permalink / raw) To: Will Deacon; +Cc: linux-kernel, mingo On Tue, Feb 13, 2018 at 01:22:55PM +0000, Will Deacon wrote: > Hi all, > > Here are a couple of fixes for qspinlock issues I've found by code > inspection whilst investigating the possibility of enabling this for > arm64. The first patch fixes a problem with unusual hardware re-ordering > whilst the second fixes a problem with unusual compiler re-ordering. Thanks Will! Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-02-13 15:29 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-02-13 13:22 [PATCH 0/2] A couple of qspinlock fixes Will Deacon 2018-02-13 13:22 ` [PATCH 1/2] locking/qspinlock: Ensure node is initialised before updating prev->next Will Deacon 2018-02-13 15:27 ` [tip:locking/urgent] " tip-bot for Will Deacon 2018-02-13 13:22 ` [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node Will Deacon 2018-02-13 15:27 ` [tip:locking/urgent] " tip-bot for Will Deacon 2018-02-13 13:45 ` [PATCH 0/2] A couple of qspinlock fixes Peter Zijlstra
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
Powered by JetHome