1 // SPDX-License-Identifier: GPL-2.0
3 * Asynchronous refcounty things
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
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>
16 static inline void closure_put_after_sub_checks(int flags
)
18 int r
= flags
& CLOSURE_REMAINING_MASK
;
20 if (WARN(flags
& CLOSURE_GUARD_MASK
,
21 "closure has guard bits set: %x (%u)",
22 flags
& CLOSURE_GUARD_MASK
, (unsigned) __fls(r
)))
23 r
&= ~CLOSURE_GUARD_MASK
;
25 WARN(!r
&& (flags
& ~CLOSURE_DESTRUCTOR
),
26 "closure ref hit 0 with incorrect flags set: %x (%u)",
27 flags
& ~CLOSURE_DESTRUCTOR
, (unsigned) __fls(flags
));
30 static inline void closure_put_after_sub(struct closure
*cl
, int flags
)
32 closure_put_after_sub_checks(flags
);
34 if (!(flags
& CLOSURE_REMAINING_MASK
)) {
35 smp_acquire__after_ctrl_dep();
37 cl
->closure_get_happened
= false;
39 if (cl
->fn
&& !(flags
& CLOSURE_DESTRUCTOR
)) {
40 atomic_set(&cl
->remaining
,
41 CLOSURE_REMAINING_INITIALIZER
);
44 struct closure
*parent
= cl
->parent
;
45 closure_fn
*destructor
= cl
->fn
;
47 closure_debug_destroy(cl
);
50 destructor(&cl
->work
);
58 /* For clearing flags with the same atomic op as a put */
59 void closure_sub(struct closure
*cl
, int v
)
61 closure_put_after_sub(cl
, atomic_sub_return_release(v
, &cl
->remaining
));
63 EXPORT_SYMBOL(closure_sub
);
66 * closure_put - decrement a closure's refcount
68 void closure_put(struct closure
*cl
)
70 closure_put_after_sub(cl
, atomic_dec_return_release(&cl
->remaining
));
72 EXPORT_SYMBOL(closure_put
);
75 * closure_wake_up - wake up all closures on a wait list, without memory barrier
77 void __closure_wake_up(struct closure_waitlist
*wait_list
)
79 struct llist_node
*list
;
80 struct closure
*cl
, *t
;
81 struct llist_node
*reverse
= NULL
;
83 list
= llist_del_all(&wait_list
->list
);
85 /* We first reverse the list to preserve FIFO ordering and fairness */
86 reverse
= llist_reverse_order(list
);
88 /* Then do the wakeups */
89 llist_for_each_entry_safe(cl
, t
, reverse
, list
) {
90 closure_set_waiting(cl
, 0);
91 closure_sub(cl
, CLOSURE_WAITING
+ 1);
94 EXPORT_SYMBOL(__closure_wake_up
);
97 * closure_wait - add a closure to a waitlist
98 * @waitlist: will own a ref on @cl, which will be released when
99 * closure_wake_up() is called on @waitlist.
100 * @cl: closure pointer.
103 bool closure_wait(struct closure_waitlist
*waitlist
, struct closure
*cl
)
105 if (atomic_read(&cl
->remaining
) & CLOSURE_WAITING
)
108 cl
->closure_get_happened
= true;
109 closure_set_waiting(cl
, _RET_IP_
);
110 atomic_add(CLOSURE_WAITING
+ 1, &cl
->remaining
);
111 llist_add(&cl
->list
, &waitlist
->list
);
115 EXPORT_SYMBOL(closure_wait
);
117 struct closure_syncer
{
118 struct task_struct
*task
;
122 static CLOSURE_CALLBACK(closure_sync_fn
)
124 struct closure
*cl
= container_of(ws
, struct closure
, work
);
125 struct closure_syncer
*s
= cl
->s
;
126 struct task_struct
*p
;
129 p
= READ_ONCE(s
->task
);
135 void __sched
__closure_sync(struct closure
*cl
)
137 struct closure_syncer s
= { .task
= current
};
140 continue_at(cl
, closure_sync_fn
, NULL
);
143 set_current_state(TASK_UNINTERRUPTIBLE
);
149 __set_current_state(TASK_RUNNING
);
151 EXPORT_SYMBOL(__closure_sync
);
154 * closure_return_sync - finish running a closure, synchronously (i.e. waiting
155 * for outstanding get()s to finish) and returning once closure refcount is 0.
157 * Unlike closure_sync() this doesn't reinit the ref to 1; subsequent
158 * closure_get_not_zero() calls waill fail.
160 void __sched
closure_return_sync(struct closure
*cl
)
162 struct closure_syncer s
= { .task
= current
};
165 set_closure_fn(cl
, closure_sync_fn
, NULL
);
167 unsigned flags
= atomic_sub_return_release(1 + CLOSURE_RUNNING
- CLOSURE_DESTRUCTOR
,
170 closure_put_after_sub_checks(flags
);
172 if (unlikely(flags
& CLOSURE_REMAINING_MASK
)) {
174 set_current_state(TASK_UNINTERRUPTIBLE
);
180 __set_current_state(TASK_RUNNING
);
184 closure_put(cl
->parent
);
186 EXPORT_SYMBOL(closure_return_sync
);
188 int __sched
__closure_sync_timeout(struct closure
*cl
, unsigned long timeout
)
190 struct closure_syncer s
= { .task
= current
};
194 continue_at(cl
, closure_sync_fn
, NULL
);
197 set_current_state(TASK_UNINTERRUPTIBLE
);
202 * Carefully undo the continue_at() - but only if it
203 * hasn't completed, i.e. the final closure_put() hasn't
206 unsigned old
, new, v
= atomic_read(&cl
->remaining
);
209 if (!old
|| (old
& CLOSURE_RUNNING
))
212 new = old
+ CLOSURE_REMAINING_INITIALIZER
;
213 } while ((v
= atomic_cmpxchg(&cl
->remaining
, old
, new)) != old
);
217 timeout
= schedule_timeout(timeout
);
220 __set_current_state(TASK_RUNNING
);
223 EXPORT_SYMBOL(__closure_sync_timeout
);
225 #ifdef CONFIG_DEBUG_CLOSURES
227 static LIST_HEAD(closure_list
);
228 static DEFINE_SPINLOCK(closure_list_lock
);
230 void closure_debug_create(struct closure
*cl
)
234 BUG_ON(cl
->magic
== CLOSURE_MAGIC_ALIVE
);
235 cl
->magic
= CLOSURE_MAGIC_ALIVE
;
237 spin_lock_irqsave(&closure_list_lock
, flags
);
238 list_add(&cl
->all
, &closure_list
);
239 spin_unlock_irqrestore(&closure_list_lock
, flags
);
241 EXPORT_SYMBOL(closure_debug_create
);
243 void closure_debug_destroy(struct closure
*cl
)
247 if (cl
->magic
== CLOSURE_MAGIC_STACK
)
250 BUG_ON(cl
->magic
!= CLOSURE_MAGIC_ALIVE
);
251 cl
->magic
= CLOSURE_MAGIC_DEAD
;
253 spin_lock_irqsave(&closure_list_lock
, flags
);
255 spin_unlock_irqrestore(&closure_list_lock
, flags
);
257 EXPORT_SYMBOL(closure_debug_destroy
);
259 static int debug_show(struct seq_file
*f
, void *data
)
263 spin_lock_irq(&closure_list_lock
);
265 list_for_each_entry(cl
, &closure_list
, all
) {
266 int r
= atomic_read(&cl
->remaining
);
268 seq_printf(f
, "%p: %pS -> %pS p %p r %i ",
269 cl
, (void *) cl
->ip
, cl
->fn
, cl
->parent
,
270 r
& CLOSURE_REMAINING_MASK
);
272 seq_printf(f
, "%s%s\n",
273 test_bit(WORK_STRUCT_PENDING_BIT
,
274 work_data_bits(&cl
->work
)) ? "Q" : "",
275 r
& CLOSURE_RUNNING
? "R" : "");
277 if (r
& CLOSURE_WAITING
)
278 seq_printf(f
, " W %pS\n",
279 (void *) cl
->waiting_on
);
284 spin_unlock_irq(&closure_list_lock
);
288 DEFINE_SHOW_ATTRIBUTE(debug
);
290 static int __init
closure_debug_init(void)
292 debugfs_create_file("closures", 0400, NULL
, NULL
, &debug_fops
);
295 late_initcall(closure_debug_init
)