mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jianyu Zhan <nasa4836@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: tglx@linutronix.de, dave@stgolabs.net, peterz@infradead.org,
	akpm@linux-foundation.org, mingo@kernel.org,
	dvhart@linux.intel.com, bigeasy@linutronix.de,
	nasa4836@gmail.com
Subject: [PATCH] futex:  replace bare barrier() with a READ_ONCE()
Date: Mon, 29 Feb 2016 19:41:33 +0800	[thread overview]
Message-ID: <1456746093-10578-1-git-send-email-nasa4836@gmail.com> (raw)

Commit e91467ecd1ef ("bug in futex unqueue_me") introduces a barrier()
in unqueue_me(), to address a pointer aliasing problem in s390x, between
q->lock_ptr and local variable lock_ptr.

Since there is a retry logic to reload q->lock_ptr into local variable lock_ptr,
s390x generates code that aliases q->lock_ptr with lock_ptr, while q->lock_ptr
might change between retries, which beats the alias and causes problem.

This patch replaces this bare barrier() with a READ_ONCE(), a weaker form of
barrier(), which could be more informative.

And furthermore, this retry logic is effectively the same with:

	while (lock_ptr = q->lock_ptr)
		do_something_with(lock_ptr);

and compiler is at its will to merge successive load of q->lock_ptr, which is also problematic
at this scenario. READ_ONCE() can avoid this problem.

Signed-off-by: Jianyu Zhan <nasa4836@gmail.com>
---
 kernel/futex.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 5d6ce64..20e8466 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -1927,8 +1927,12 @@ static int unqueue_me(struct futex_q *q)
 
 	/* In the common case we don't take the spinlock, which is nice. */
 retry:
-	lock_ptr = q->lock_ptr;
-	barrier();
+	/*
+	 *  q->lock_ptr can change and a pointer aliasing(of lock_ptr) will cause problem, 
+	 *  and also to avoid potential compiler merging of successive load of q->lock_ptr under
+	 *  this retry logic, so we use READ_ONCE() here.
+	 */
+	lock_ptr = READ_ONCE(q->lock_ptr);
 	if (lock_ptr != NULL) {
 		spin_lock(lock_ptr);
 		/*
-- 
2.4.3

             reply	other threads:[~2016-02-29 11:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-29 11:41 Jianyu Zhan [this message]
2016-02-29 22:22 ` Darren Hart
2016-03-01  0:44   ` Jianyu Zhan
2016-03-02  1:37     ` Darren Hart
2016-03-02  2:31       ` Jianyu Zhan
2016-03-02  6:39         ` Darren Hart
2016-03-02  7:12           ` Jianyu Zhan

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=1456746093-10578-1-git-send-email-nasa4836@gmail.com \
    --to=nasa4836@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=dave@stgolabs.net \
    --cc=dvhart@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®