Skip to contentSkip to conversation

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

RFC PATCH Superseded Nicholas Mc Guire 2 participants 2 messages Jan 23, 2015 – Feb 4, 2015 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sched: completion: lock-free checking of the blocking case

The "thread would block" case can be checked without grabbing the 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.

 kernel/sched/completion.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/sched/completion.c +9 -0
index 7c5cd70..5f7cf31 100644
@@ -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;
Signature · 1 line
1.7.10.4
#1 lore raw

[tip:locking/core] sched/completion: Add lock-free checking of the blocking case

sched/completion: Add lock-free checking of the blocking case
Commit-ID:  7c34e3180a01c800a40bc8535654d5735802fc1b
Gitweb:     http://git.kernel.org/tip/7c34e3180a01c800a40bc8535654d5735802fc1b
Author:     Nicholas Mc Guire <[email protected]>
AuthorDate: Fri, 23 Jan 2015 12:41:47 +0100
Committer:  Ingo Molnar <[email protected]>
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. ]

---
 kernel/sched/completion.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/sched/completion.c +9 -0
index 9d1fe32d..7052d3f 100644
@@ -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;