]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - linux/closure.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / linux / closure.c
index 26a29356b43e248d9a5a42b62e2038a4c1b7776e..c16540552d61bc14121b034a9d6e302045ff0dc5 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Asynchronous refcounty things
  *
@@ -8,7 +9,9 @@
 #include <linux/closure.h>
 #include <linux/debugfs.h>
 #include <linux/export.h>
+#include <linux/rcupdate.h>
 #include <linux/seq_file.h>
+#include <linux/sched/debug.h>
 
 static inline void closure_put_after_sub(struct closure *cl, int flags)
 {
@@ -18,6 +21,10 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)
        BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
 
        if (!r) {
+               smp_acquire__after_ctrl_dep();
+
+               cl->closure_get_happened = false;
+
                if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
                        atomic_set(&cl->remaining,
                                   CLOSURE_REMAINING_INITIALIZER);
@@ -29,7 +36,7 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)
                        closure_debug_destroy(cl);
 
                        if (destructor)
-                               destructor(cl);
+                               destructor(&cl->work);
 
                        if (parent)
                                closure_put(parent);
@@ -40,37 +47,35 @@ static inline void closure_put_after_sub(struct closure *cl, int flags)
 /* For clearing flags with the same atomic op as a put */
 void closure_sub(struct closure *cl, int v)
 {
-       closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
+       closure_put_after_sub(cl, atomic_sub_return_release(v, &cl->remaining));
 }
 EXPORT_SYMBOL(closure_sub);
 
-/**
+/*
  * closure_put - decrement a closure's refcount
  */
 void closure_put(struct closure *cl)
 {
-       closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
+       closure_put_after_sub(cl, atomic_dec_return_release(&cl->remaining));
 }
 EXPORT_SYMBOL(closure_put);
 
-/**
+/*
  * closure_wake_up - wake up all closures on a wait list, without memory barrier
  */
 void __closure_wake_up(struct closure_waitlist *wait_list)
 {
-       struct llist_node *list, *next;
-       struct closure *cl;
+       struct llist_node *list;
+       struct closure *cl, *t;
+       struct llist_node *reverse = NULL;
 
-       /*
-        * Grab entire list, reverse order to preserve FIFO ordering, and wake
-        * everything up
-        */
-       for (list = llist_reverse_order(llist_del_all(&wait_list->list));
-            list;
-            list = next) {
-               next = llist_next(list);
-               cl = container_of(list, struct closure, list);
+       list = llist_del_all(&wait_list->list);
 
+       /* We first reverse the list to preserve FIFO ordering and fairness */
+       reverse = llist_reverse_order(list);
+
+       /* Then do the wakeups */
+       llist_for_each_entry_safe(cl, t, reverse, list) {
                closure_set_waiting(cl, 0);
                closure_sub(cl, CLOSURE_WAITING + 1);
        }
@@ -79,9 +84,9 @@ EXPORT_SYMBOL(__closure_wake_up);
 
 /**
  * closure_wait - add a closure to a waitlist
- *
- * @waitlist will own a ref on @cl, which will be released when
+ * @waitlist: will own a ref on @cl, which will be released when
  * closure_wake_up() is called on @waitlist.
+ * @cl: closure pointer.
  *
  */
 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
@@ -89,6 +94,7 @@ bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
        if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
                return false;
 
+       cl->closure_get_happened = true;
        closure_set_waiting(cl, _RET_IP_);
        atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
        llist_add(&cl->list, &waitlist->list);
@@ -102,10 +108,17 @@ struct closure_syncer {
        int                     done;
 };
 
-static void closure_sync_fn(struct closure *cl)
+static CLOSURE_CALLBACK(closure_sync_fn)
 {
-       cl->s->done = 1;
-       wake_up_process(cl->s->task);
+       struct closure *cl = container_of(ws, struct closure, work);
+       struct closure_syncer *s = cl->s;
+       struct task_struct *p;
+
+       rcu_read_lock();
+       p = READ_ONCE(s->task);
+       s->done = 1;
+       wake_up_process(p);
+       rcu_read_unlock();
 }
 
 void __sched __closure_sync(struct closure *cl)
@@ -113,11 +126,10 @@ void __sched __closure_sync(struct closure *cl)
        struct closure_syncer s = { .task = current };
 
        cl->s = &s;
-       continue_at_noreturn(cl, closure_sync_fn, NULL);
+       continue_at(cl, closure_sync_fn, NULL);
 
        while (1) {
-               __set_current_state(TASK_UNINTERRUPTIBLE);
-               smp_mb();
+               set_current_state(TASK_UNINTERRUPTIBLE);
                if (s.done)
                        break;
                schedule();
@@ -158,9 +170,7 @@ void closure_debug_destroy(struct closure *cl)
 }
 EXPORT_SYMBOL(closure_debug_destroy);
 
-static struct dentry *debug;
-
-static int debug_seq_show(struct seq_file *f, void *data)
+static int debug_show(struct seq_file *f, void *data)
 {
        struct closure *cl;
 
@@ -169,7 +179,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
        list_for_each_entry(cl, &closure_list, all) {
                int r = atomic_read(&cl->remaining);
 
-               seq_printf(f, "%p: %pF -> %pf p %p r %i ",
+               seq_printf(f, "%p: %pS -> %pS p %p r %i ",
                           cl, (void *) cl->ip, cl->fn, cl->parent,
                           r & CLOSURE_REMAINING_MASK);
 
@@ -179,7 +189,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
                           r & CLOSURE_RUNNING  ? "R" : "");
 
                if (r & CLOSURE_WAITING)
-                       seq_printf(f, " W %pF\n",
+                       seq_printf(f, " W %pS\n",
                                   (void *) cl->waiting_on);
 
                seq_puts(f, "\n");
@@ -189,21 +199,11 @@ static int debug_seq_show(struct seq_file *f, void *data)
        return 0;
 }
 
-static int debug_seq_open(struct inode *inode, struct file *file)
-{
-       return single_open(file, debug_seq_show, NULL);
-}
-
-static const struct file_operations debug_ops = {
-       .owner          = THIS_MODULE,
-       .open           = debug_seq_open,
-       .read           = seq_read,
-       .release        = single_release
-};
+DEFINE_SHOW_ATTRIBUTE(debug);
 
 static int __init closure_debug_init(void)
 {
-       debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
+       debugfs_create_file("closures", 0400, NULL, NULL, &debug_fops);
        return 0;
 }
 late_initcall(closure_debug_init)