Merge remote-tracking branch 'drm/drm-fixes' into drm-misc-fixes
[linux-block.git] / lib / closure.c
CommitLineData
87418ef9 1// SPDX-License-Identifier: GPL-2.0
cafe5635
KO
2/*
3 * Asynchronous refcounty things
4 *
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
7 */
8
8c8d2d96 9#include <linux/closure.h>
cafe5635 10#include <linux/debugfs.h>
8c8d2d96 11#include <linux/export.h>
b414e8ec 12#include <linux/rcupdate.h>
cafe5635 13#include <linux/seq_file.h>
ce439bf7 14#include <linux/sched/debug.h>
cafe5635 15
cafe5635
KO
16static inline void closure_put_after_sub(struct closure *cl, int flags)
17{
18 int r = flags & CLOSURE_REMAINING_MASK;
19
20 BUG_ON(flags & CLOSURE_GUARD_MASK);
faadf0c9 21 BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
cafe5635 22
cafe5635 23 if (!r) {
2bce6368
KO
24 smp_acquire__after_ctrl_dep();
25
ee526b88
KO
26 cl->closure_get_happened = false;
27
cafe5635 28 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
cafe5635
KO
29 atomic_set(&cl->remaining,
30 CLOSURE_REMAINING_INITIALIZER);
31 closure_queue(cl);
32 } else {
33 struct closure *parent = cl->parent;
6aa8f1a6 34 closure_fn *destructor = cl->fn;
cafe5635
KO
35
36 closure_debug_destroy(cl);
37
6aa8f1a6 38 if (destructor)
d4e3b928 39 destructor(&cl->work);
cafe5635
KO
40
41 if (parent)
42 closure_put(parent);
43 }
44 }
45}
46
47/* For clearing flags with the same atomic op as a put */
48void closure_sub(struct closure *cl, int v)
49{
2bce6368 50 closure_put_after_sub(cl, atomic_sub_return_release(v, &cl->remaining));
cafe5635 51}
8c8d2d96 52EXPORT_SYMBOL(closure_sub);
cafe5635 53
47344e33 54/*
1dd13c8d
KO
55 * closure_put - decrement a closure's refcount
56 */
cafe5635
KO
57void closure_put(struct closure *cl)
58{
2bce6368 59 closure_put_after_sub(cl, atomic_dec_return_release(&cl->remaining));
cafe5635 60}
8c8d2d96 61EXPORT_SYMBOL(closure_put);
cafe5635 62
47344e33 63/*
1dd13c8d
KO
64 * closure_wake_up - wake up all closures on a wait list, without memory barrier
65 */
cafe5635
KO
66void __closure_wake_up(struct closure_waitlist *wait_list)
67{
68 struct llist_node *list;
a5f3d8a5 69 struct closure *cl, *t;
cafe5635
KO
70 struct llist_node *reverse = NULL;
71
72 list = llist_del_all(&wait_list->list);
73
74 /* We first reverse the list to preserve FIFO ordering and fairness */
09b3efec 75 reverse = llist_reverse_order(list);
cafe5635
KO
76
77 /* Then do the wakeups */
a5f3d8a5 78 llist_for_each_entry_safe(cl, t, reverse, list) {
1dd13c8d 79 closure_set_waiting(cl, 0);
cafe5635
KO
80 closure_sub(cl, CLOSURE_WAITING + 1);
81 }
82}
8c8d2d96 83EXPORT_SYMBOL(__closure_wake_up);
cafe5635 84
1dd13c8d
KO
85/**
86 * closure_wait - add a closure to a waitlist
47344e33 87 * @waitlist: will own a ref on @cl, which will be released when
1dd13c8d 88 * closure_wake_up() is called on @waitlist.
47344e33 89 * @cl: closure pointer.
1dd13c8d
KO
90 *
91 */
92bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
cafe5635
KO
93{
94 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
95 return false;
96
ee526b88 97 cl->closure_get_happened = true;
1dd13c8d 98 closure_set_waiting(cl, _RET_IP_);
cafe5635 99 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
1dd13c8d 100 llist_add(&cl->list, &waitlist->list);
cafe5635
KO
101
102 return true;
103}
8c8d2d96 104EXPORT_SYMBOL(closure_wait);
cafe5635 105
e4bf7919
KO
106struct closure_syncer {
107 struct task_struct *task;
108 int done;
109};
110
d4e3b928 111static CLOSURE_CALLBACK(closure_sync_fn)
cafe5635 112{
d4e3b928 113 struct closure *cl = container_of(ws, struct closure, work);
a22a9602
KO
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();
e4bf7919 122}
cafe5635 123
ce439bf7 124void __sched __closure_sync(struct closure *cl)
e4bf7919
KO
125{
126 struct closure_syncer s = { .task = current };
cafe5635 127
e4bf7919
KO
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;
cafe5635
KO
135 schedule();
136 }
137
e4bf7919 138 __set_current_state(TASK_RUNNING);
cafe5635 139}
8c8d2d96 140EXPORT_SYMBOL(__closure_sync);
cafe5635 141
4c5b7294
KO
142int __sched __closure_sync_timeout(struct closure *cl, unsigned long timeout)
143{
144 struct closure_syncer s = { .task = current };
145 int ret = 0;
146
147 cl->s = &s;
148 continue_at(cl, closure_sync_fn, NULL);
149
150 while (1) {
151 set_current_state(TASK_UNINTERRUPTIBLE);
152 if (s.done)
153 break;
154 if (!timeout) {
155 /*
156 * Carefully undo the continue_at() - but only if it
157 * hasn't completed, i.e. the final closure_put() hasn't
158 * happened yet:
159 */
160 unsigned old, new, v = atomic_read(&cl->remaining);
161 do {
162 old = v;
163 if (!old || (old & CLOSURE_RUNNING))
164 goto success;
165
166 new = old + CLOSURE_REMAINING_INITIALIZER;
167 } while ((v = atomic_cmpxchg(&cl->remaining, old, new)) != old);
168 ret = -ETIME;
169 }
170
171 timeout = schedule_timeout(timeout);
172 }
173success:
174 __set_current_state(TASK_RUNNING);
175 return ret;
176}
177EXPORT_SYMBOL(__closure_sync_timeout);
178
8c8d2d96 179#ifdef CONFIG_DEBUG_CLOSURES
cafe5635
KO
180
181static LIST_HEAD(closure_list);
182static DEFINE_SPINLOCK(closure_list_lock);
183
184void closure_debug_create(struct closure *cl)
185{
186 unsigned long flags;
187
188 BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
189 cl->magic = CLOSURE_MAGIC_ALIVE;
190
191 spin_lock_irqsave(&closure_list_lock, flags);
192 list_add(&cl->all, &closure_list);
193 spin_unlock_irqrestore(&closure_list_lock, flags);
194}
8c8d2d96 195EXPORT_SYMBOL(closure_debug_create);
cafe5635
KO
196
197void closure_debug_destroy(struct closure *cl)
198{
199 unsigned long flags;
200
201 BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
202 cl->magic = CLOSURE_MAGIC_DEAD;
203
204 spin_lock_irqsave(&closure_list_lock, flags);
205 list_del(&cl->all);
206 spin_unlock_irqrestore(&closure_list_lock, flags);
207}
8c8d2d96 208EXPORT_SYMBOL(closure_debug_destroy);
cafe5635 209
84e5d136 210static int debug_show(struct seq_file *f, void *data)
cafe5635
KO
211{
212 struct closure *cl;
1fae7cf0 213
cafe5635
KO
214 spin_lock_irq(&closure_list_lock);
215
216 list_for_each_entry(cl, &closure_list, all) {
217 int r = atomic_read(&cl->remaining);
218
d9c61d30 219 seq_printf(f, "%p: %pS -> %pS p %p r %i ",
cafe5635
KO
220 cl, (void *) cl->ip, cl->fn, cl->parent,
221 r & CLOSURE_REMAINING_MASK);
222
e4bf7919 223 seq_printf(f, "%s%s\n",
8d090f47 224 test_bit(WORK_STRUCT_PENDING_BIT,
cafe5635 225 work_data_bits(&cl->work)) ? "Q" : "",
e4bf7919 226 r & CLOSURE_RUNNING ? "R" : "");
cafe5635
KO
227
228 if (r & CLOSURE_WAITING)
d9c61d30 229 seq_printf(f, " W %pS\n",
cafe5635
KO
230 (void *) cl->waiting_on);
231
8c8d2d96 232 seq_puts(f, "\n");
cafe5635
KO
233 }
234
235 spin_unlock_irq(&closure_list_lock);
236 return 0;
237}
238
84e5d136 239DEFINE_SHOW_ATTRIBUTE(debug);
cafe5635 240
8c8d2d96 241static int __init closure_debug_init(void)
cafe5635 242{
8c8d2d96
KO
243 debugfs_create_file("closures", 0400, NULL, NULL, &debug_fops);
244 return 0;
cafe5635 245}
8c8d2d96 246late_initcall(closure_debug_init)
cafe5635 247
8c8d2d96 248#endif