]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/closure.c
Update bcachefs sources to a8115093df bcachefs: Fix divide by zero in rebalance_work()
[bcachefs-tools-debian] / linux / closure.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Asynchronous refcounty things
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #include <linux/closure.h>
10 #include <linux/debugfs.h>
11 #include <linux/export.h>
12 #include <linux/rcupdate.h>
13 #include <linux/seq_file.h>
14 #include <linux/sched/debug.h>
15
16 static inline void closure_put_after_sub(struct closure *cl, int flags)
17 {
18         int r = flags & CLOSURE_REMAINING_MASK;
19
20         if ((flags & CLOSURE_GUARD_MASK) ||
21             (!r && (flags & ~CLOSURE_DESTRUCTOR)))
22                 panic("closure_put_after_sub: bogus flags %x remaining %i", flags, r);
23
24         if (!r) {
25                 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
26                         atomic_set(&cl->remaining,
27                                    CLOSURE_REMAINING_INITIALIZER);
28                         closure_queue(cl);
29                 } else {
30                         struct closure *parent = cl->parent;
31                         closure_fn *destructor = cl->fn;
32
33                         closure_debug_destroy(cl);
34
35                         if (destructor)
36                                 destructor(cl);
37
38                         if (parent)
39                                 closure_put(parent);
40                 }
41         }
42 }
43
44 /* For clearing flags with the same atomic op as a put */
45 void closure_sub(struct closure *cl, int v)
46 {
47         closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
48 }
49 EXPORT_SYMBOL(closure_sub);
50
51 /*
52  * closure_put - decrement a closure's refcount
53  */
54 void closure_put(struct closure *cl)
55 {
56         closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
57 }
58 EXPORT_SYMBOL(closure_put);
59
60 /*
61  * closure_wake_up - wake up all closures on a wait list, without memory barrier
62  */
63 void __closure_wake_up(struct closure_waitlist *wait_list)
64 {
65         struct llist_node *list;
66         struct closure *cl, *t;
67         struct llist_node *reverse = NULL;
68
69         list = llist_del_all(&wait_list->list);
70
71         /* We first reverse the list to preserve FIFO ordering and fairness */
72         reverse = llist_reverse_order(list);
73
74         /* Then do the wakeups */
75         llist_for_each_entry_safe(cl, t, reverse, list) {
76                 closure_set_waiting(cl, 0);
77                 closure_sub(cl, CLOSURE_WAITING + 1);
78         }
79 }
80 EXPORT_SYMBOL(__closure_wake_up);
81
82 /**
83  * closure_wait - add a closure to a waitlist
84  * @waitlist: will own a ref on @cl, which will be released when
85  * closure_wake_up() is called on @waitlist.
86  * @cl: closure pointer.
87  *
88  */
89 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
90 {
91         if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
92                 return false;
93
94         closure_set_waiting(cl, _RET_IP_);
95         atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
96         llist_add(&cl->list, &waitlist->list);
97
98         return true;
99 }
100 EXPORT_SYMBOL(closure_wait);
101
102 struct closure_syncer {
103         struct task_struct      *task;
104         int                     done;
105 };
106
107 static void closure_sync_fn(struct closure *cl)
108 {
109         struct closure_syncer *s = cl->s;
110         struct task_struct *p;
111
112         rcu_read_lock();
113         p = READ_ONCE(s->task);
114         s->done = 1;
115         wake_up_process(p);
116         rcu_read_unlock();
117 }
118
119 void __sched __closure_sync(struct closure *cl)
120 {
121         struct closure_syncer s = { .task = current };
122
123         cl->s = &s;
124         continue_at(cl, closure_sync_fn, NULL);
125
126         while (1) {
127                 set_current_state(TASK_UNINTERRUPTIBLE);
128                 if (s.done)
129                         break;
130                 schedule();
131         }
132
133         __set_current_state(TASK_RUNNING);
134 }
135 EXPORT_SYMBOL(__closure_sync);
136
137 #ifdef CONFIG_DEBUG_CLOSURES
138
139 static LIST_HEAD(closure_list);
140 static DEFINE_SPINLOCK(closure_list_lock);
141
142 void closure_debug_create(struct closure *cl)
143 {
144         unsigned long flags;
145
146         BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
147         cl->magic = CLOSURE_MAGIC_ALIVE;
148
149         spin_lock_irqsave(&closure_list_lock, flags);
150         list_add(&cl->all, &closure_list);
151         spin_unlock_irqrestore(&closure_list_lock, flags);
152 }
153 EXPORT_SYMBOL(closure_debug_create);
154
155 void closure_debug_destroy(struct closure *cl)
156 {
157         unsigned long flags;
158
159         BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
160         cl->magic = CLOSURE_MAGIC_DEAD;
161
162         spin_lock_irqsave(&closure_list_lock, flags);
163         list_del(&cl->all);
164         spin_unlock_irqrestore(&closure_list_lock, flags);
165 }
166 EXPORT_SYMBOL(closure_debug_destroy);
167
168 static int debug_show(struct seq_file *f, void *data)
169 {
170         struct closure *cl;
171
172         spin_lock_irq(&closure_list_lock);
173
174         list_for_each_entry(cl, &closure_list, all) {
175                 int r = atomic_read(&cl->remaining);
176
177                 seq_printf(f, "%p: %pS -> %pS p %p r %i ",
178                            cl, (void *) cl->ip, cl->fn, cl->parent,
179                            r & CLOSURE_REMAINING_MASK);
180
181                 seq_printf(f, "%s%s\n",
182                            test_bit(WORK_STRUCT_PENDING_BIT,
183                                     work_data_bits(&cl->work)) ? "Q" : "",
184                            r & CLOSURE_RUNNING  ? "R" : "");
185
186                 if (r & CLOSURE_WAITING)
187                         seq_printf(f, " W %pS\n",
188                                    (void *) cl->waiting_on);
189
190                 seq_puts(f, "\n");
191         }
192
193         spin_unlock_irq(&closure_list_lock);
194         return 0;
195 }
196
197 DEFINE_SHOW_ATTRIBUTE(debug);
198
199 static int __init closure_debug_init(void)
200 {
201         debugfs_create_file("closures", 0400, NULL, NULL, &debug_fops);
202         return 0;
203 }
204 late_initcall(closure_debug_init)
205
206 #endif