Skip to contentSkip to conversation

bcache: Fix, improve efficiency of closure_sync()

PATCH In review Michael Lyle 3 participants 5 messages Dec 30, 2017 – Jan 5, 2018

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 2/2 bcache: mark closure_sync() __sched

In reply to Michael Lyle

From: Kent Overstreet <[email protected]>

[edit by mlyle: include sched/debug.h to get __sched]

---
 drivers/md/bcache/closure.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/bcache/closure.c +2 -1
index 8a4f4e4f5537..23eb3d1223e7 100644
@@ -8,6 +8,7 @@
 #include <linux/debugfs.h>
 #include <linux/module.h>
 #include <linux/seq_file.h>
+#include <linux/sched/debug.h>
 
 #include "closure.h"
 
@@ -107,7 +108,7 @@ static void closure_sync_fn(struct closure *cl)
 	wake_up_process(cl->s->task);
 }
 
-void __closure_sync(struct closure *cl)
+void __sched __closure_sync(struct closure *cl)
 {
 	struct closure_syncer s = { .task = current };
 
Signature · 1 line
2.14.1
#0 lore raw

PATCH 1/2 bcache: Fix, improve efficiency of closure_sync()

From: Kent Overstreet <[email protected]>

Eliminates cases where sync can race and fail to complete / get stuck. Removes many status flags and simplifies entering-and-exiting closure sleeping behaviors.

[mlyle: fixed conflicts due to changed return behavior in mainline. extended commit comment, and squashed down two commits that were mostly contradictory to get to this state]

---
 drivers/md/bcache/closure.c | 47 ++++++++++++++++++-----------------
 drivers/md/bcache/closure.h | 60 +++++++++++++++++----------------------------
 2 files changed, 47 insertions(+), 60 deletions(-)
drivers/md/bcache/closure.c +24 -23
index 1841d0359bac..8a4f4e4f5537 100644
@@ -18,10 +18,6 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)
 	BUG_ON(flags & CLOSURE_GUARD_MASK);
 	BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
 
-	/* Must deliver precisely one wakeup */
-	if (r == 1 && (flags & CLOSURE_SLEEPING))
-		wake_up_process(cl->task);
-
 	if (!r) {
 		if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
 			atomic_set(&cl->remaining,
@@ -100,28 +96,35 @@ bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
 }
 EXPORT_SYMBOL(closure_wait);
 
-/**
- * closure_sync - sleep until a closure has nothing left to wait on
- *
- * Sleeps until the refcount hits 1 - the thread that's running the closure owns
- * the last refcount.
- */
-void closure_sync(struct closure *cl)
+struct closure_syncer {
+	struct task_struct	*task;
+	int			done;
+};
+
+static void closure_sync_fn(struct closure *cl)
 {
-	while (1) {
-		__closure_start_sleep(cl);
-		closure_set_ret_ip(cl);
+	cl->s->done = 1;
+	wake_up_process(cl->s->task);
+}
 
-		if ((atomic_read(&cl->remaining) &
-		     CLOSURE_REMAINING_MASK) == 1)
-			break;
+void __closure_sync(struct closure *cl)
+{
+	struct closure_syncer s = { .task = current };
 
+	cl->s = &s;
+	continue_at(cl, closure_sync_fn, NULL);
+
+	while (1) {
+		__set_current_state(TASK_UNINTERRUPTIBLE);
+		smp_mb(); /* Ensure task state set before load of done flag */
+		if (s.done)
+			break;
 		schedule();
 	}
 
-	__closure_end_sleep(cl);
+	__set_current_state(TASK_RUNNING);
 }
-EXPORT_SYMBOL(closure_sync);
+EXPORT_SYMBOL(__closure_sync);
 
 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
 
@@ -168,12 +171,10 @@ static int debug_seq_show(struct seq_file *f, void *data)
 			   cl, (void *) cl->ip, cl->fn, cl->parent,
 			   r & CLOSURE_REMAINING_MASK);
 
-		seq_printf(f, "%s%s%s%s\n",
+		seq_printf(f, "%s%s\n",
 			   test_bit(WORK_STRUCT_PENDING_BIT,
 				    work_data_bits(&cl->work)) ? "Q" : "",
-			   r & CLOSURE_RUNNING	? "R" : "",
-			   r & CLOSURE_STACK	? "S" : "",
-			   r & CLOSURE_SLEEPING	? "Sl" : "");
+			   r & CLOSURE_RUNNING	? "R" : "");
 
 		if (r & CLOSURE_WAITING)
 			seq_printf(f, " W %pF\n",
drivers/md/bcache/closure.h +23 -37

119 lines not on this page · raw message

Signature · 1 line
2.14.1
maintainer
#2 lore raw

drivers/md/bcache/closure.c bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl) · line 119

Quoted 10 lines · Michael Lyle
drivers/md/bcache/closure.c +9 -0
+void __closure_sync(struct closure *cl)
+{
+	struct closure_syncer s = { .task = current };
 
+	cl->s = &s;
+	continue_at(cl, closure_sync_fn, NULL);
+
+	while (1) {
+		__set_current_state(TASK_UNINTERRUPTIBLE);
+		smp_mb(); /* Ensure task state set before load of done flag */

That's why we have set_current_state().

Signature · 1 line
Jens Axboe
#3 lore raw

Jens & Kent,

Quoted 5 lines · Jens Axboe
Quoted 4 lines · Michael Lyle
drivers/md/bcache/closure.c +3 -0
+void __closure_sync(struct closure *cl)
+{
+	struct closure_syncer s = { .task = current };
Quoted 9 lines

drivers/md/bcache/closure.c bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl) · line 119

Quoted 6 lines
drivers/md/bcache/closure.c +6 -0
+	cl->s = &s;
+	continue_at(cl, closure_sync_fn, NULL);
+
+	while (1) {
+		__set_current_state(TASK_UNINTERRUPTIBLE);
+		smp_mb(); /* Ensure task state set before load of done flag */

That's why we have set_current_state().

I wrote the comment in question-- it seemed like to me set_current_state and a store w/ barrier, but I was nervous since I didn't write the code that there might be another dependency/reason.

Kent-- is there any reason to not just set_current_state(...)?

Thanks,

Mike

#4 lore raw
Quoted 18 lines · Michael Lyle

Jens & Kent,

Quoted 5 lines · Jens Axboe
Quoted 4 lines · Michael Lyle
drivers/md/bcache/closure.c +3 -0
+void __closure_sync(struct closure *cl)
+{
+	struct closure_syncer s = { .task = current };
Quoted 9 lines

drivers/md/bcache/closure.c bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl) · line 119

Quoted 6 lines
drivers/md/bcache/closure.c +6 -0
+	cl->s = &s;
+	continue_at(cl, closure_sync_fn, NULL);
+
+	while (1) {
+		__set_current_state(TASK_UNINTERRUPTIBLE);
+		smp_mb(); /* Ensure task state set before load of done flag */

That's why we have set_current_state().

Quoted 5 lines

I wrote the comment in question-- it seemed like to me set_current_state and a store w/ barrier, but I was nervous since I didn't write the code that there might be another dependency/reason.

Kent-- is there any reason to not just set_current_state(...)?

No, set_current_state() is the right way to do it