From: Will Deacon <will.deacon@arm.com>
To: linux-kernel@vger.kernel.org
Cc: peterz@infradead.org, mingo@redhat.com,
Will Deacon <will.deacon@arm.com>
Subject: [PATCH 2/2] locking/qspinlock: Ensure node->count is updated before initialising node
Date: Tue, 13 Feb 2018 13:22:57 +0000 [thread overview]
Message-ID: <1518528177-19169-3-git-send-email-will.deacon@arm.com> (raw)
In-Reply-To: <1518528177-19169-1-git-send-email-will.deacon@arm.com>
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
next prev parent reply other threads:[~2018-02-13 13:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Will Deacon [this message]
2018-02-13 15:27 ` [tip:locking/urgent] locking/qspinlock: Ensure node->count is updated before initialising node tip-bot for Will Deacon
2018-02-13 13:45 ` [PATCH 0/2] A couple of qspinlock fixes Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1518528177-19169-3-git-send-email-will.deacon@arm.com \
--to=will.deacon@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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