All of lore.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Nicholas Mc Guire <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: der.herr@hofr.at, peterz@infradead.org, hpa@zytor.com,
	tglx@linutronix.de, mingo@kernel.org,
	torvalds@linux-foundation.org, linux-kernel@vger.kernel.org
Subject: [tip:locking/core] sched/completion: Add lock-free checking of the blocking case
Date: Wed, 4 Feb 2015 06:38:04 -0800	[thread overview]
Message-ID: <tip-7c34e3180a01c800a40bc8535654d5735802fc1b@git.kernel.org> (raw)
In-Reply-To: <1422013307-13200-1-git-send-email-der.herr@hofr.at>

Commit-ID:  7c34e3180a01c800a40bc8535654d5735802fc1b
Gitweb:     http://git.kernel.org/tip/7c34e3180a01c800a40bc8535654d5735802fc1b
Author:     Nicholas Mc Guire <der.herr@hofr.at>
AuthorDate: Fri, 23 Jan 2015 12:41:47 +0100
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Wed, 4 Feb 2015 07:57:37 +0100

sched/completion: Add lock-free checking of the blocking case

The "thread would block" case can be checked without grabbing ->wait.lock.

[ If the check does not return early then grab the lock and recheck.
  A memory barrier is not needed as complete() and complete_all() imply
  a barrier.

  The ACCESS_ONCE() is needed for calls in a loop that, if inlined, could
  optimize out the re-fetching of x->done. ]

Signed-off-by: Nicholas Mc Guire <der.herr@hofr.at>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/1422013307-13200-1-git-send-email-der.herr@hofr.at
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 kernel/sched/completion.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
index 9d1fe32d..7052d3f 100644
--- a/kernel/sched/completion.c
+++ b/kernel/sched/completion.c
@@ -268,6 +268,15 @@ bool try_wait_for_completion(struct completion *x)
 	unsigned long flags;
 	int ret = 1;
 
+	/*
+	 * Since x->done will need to be locked only
+	 * in the non-blocking case, we check x->done
+	 * first without taking the lock so we can
+	 * return early in the blocking case.
+	 */
+	if (!ACCESS_ONCE(x->done))
+		return 0;
+
 	spin_lock_irqsave(&x->wait.lock, flags);
 	if (!x->done)
 		ret = 0;

      reply	other threads:[~2015-02-04 14:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 11:41 [PATCH RFC] sched: completion: lock-free checking of the blocking case Nicholas Mc Guire
2015-02-04 14:38 ` tip-bot for Nicholas Mc Guire [this message]

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=tip-7c34e3180a01c800a40bc8535654d5735802fc1b@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=der.herr@hofr.at \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.