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