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/seq_file.h>
13 #include <linux/sched/debug.h>
15 static inline void closure_put_after_sub(struct closure *cl, int flags)
17 int r = flags & CLOSURE_REMAINING_MASK;
19 BUG_ON(flags & CLOSURE_GUARD_MASK);
20 BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
23 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
24 atomic_set(&cl->remaining,
25 CLOSURE_REMAINING_INITIALIZER);
28 struct closure *parent = cl->parent;
29 closure_fn *destructor = cl->fn;
31 closure_debug_destroy(cl);
42 /* For clearing flags with the same atomic op as a put */
43 void closure_sub(struct closure *cl, int v)
45 closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
47 EXPORT_SYMBOL(closure_sub);
50 * closure_put - decrement a closure's refcount
52 void closure_put(struct closure *cl)
54 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
56 EXPORT_SYMBOL(closure_put);
59 * closure_wake_up - wake up all closures on a wait list, without memory barrier
61 void __closure_wake_up(struct closure_waitlist *wait_list)
63 struct llist_node *list;
64 struct closure *cl, *t;
65 struct llist_node *reverse = NULL;
67 list = llist_del_all(&wait_list->list);
69 /* We first reverse the list to preserve FIFO ordering and fairness */
70 reverse = llist_reverse_order(list);
72 /* Then do the wakeups */
73 llist_for_each_entry_safe(cl, t, reverse, list) {
74 closure_set_waiting(cl, 0);
75 closure_sub(cl, CLOSURE_WAITING + 1);
78 EXPORT_SYMBOL(__closure_wake_up);
81 * closure_wait - add a closure to a waitlist
82 * @waitlist: will own a ref on @cl, which will be released when
83 * closure_wake_up() is called on @waitlist.
84 * @cl: closure pointer.
87 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
89 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
92 closure_set_waiting(cl, _RET_IP_);
93 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
94 llist_add(&cl->list, &waitlist->list);
98 EXPORT_SYMBOL(closure_wait);
100 struct closure_syncer {
101 struct task_struct *task;
105 static void closure_sync_fn(struct closure *cl)
107 struct closure_syncer *s = cl->s;
108 struct task_struct *p;
111 p = READ_ONCE(s->task);
117 void __sched __closure_sync(struct closure *cl)
119 struct closure_syncer s = { .task = current };
122 continue_at(cl, closure_sync_fn, NULL);
125 set_current_state(TASK_UNINTERRUPTIBLE);
131 __set_current_state(TASK_RUNNING);
133 EXPORT_SYMBOL(__closure_sync);
135 #ifdef CONFIG_DEBUG_CLOSURES
137 static LIST_HEAD(closure_list);
138 static DEFINE_SPINLOCK(closure_list_lock);
140 void closure_debug_create(struct closure *cl)
144 BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
145 cl->magic = CLOSURE_MAGIC_ALIVE;
147 spin_lock_irqsave(&closure_list_lock, flags);
148 list_add(&cl->all, &closure_list);
149 spin_unlock_irqrestore(&closure_list_lock, flags);
151 EXPORT_SYMBOL(closure_debug_create);
153 void closure_debug_destroy(struct closure *cl)
157 BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
158 cl->magic = CLOSURE_MAGIC_DEAD;
160 spin_lock_irqsave(&closure_list_lock, flags);
162 spin_unlock_irqrestore(&closure_list_lock, flags);
164 EXPORT_SYMBOL(closure_debug_destroy);
166 static int debug_show(struct seq_file *f, void *data)
170 spin_lock_irq(&closure_list_lock);
172 list_for_each_entry(cl, &closure_list, all) {
173 int r = atomic_read(&cl->remaining);
175 seq_printf(f, "%p: %pS -> %pS p %p r %i ",
176 cl, (void *) cl->ip, cl->fn, cl->parent,
177 r & CLOSURE_REMAINING_MASK);
179 seq_printf(f, "%s%s\n",
180 test_bit(WORK_STRUCT_PENDING_BIT,
181 work_data_bits(&cl->work)) ? "Q" : "",
182 r & CLOSURE_RUNNING ? "R" : "");
184 if (r & CLOSURE_WAITING)
185 seq_printf(f, " W %pS\n",
186 (void *) cl->waiting_on);
191 spin_unlock_irq(&closure_list_lock);
195 DEFINE_SHOW_ATTRIBUTE(debug);
197 static int __init closure_debug_init(void)
199 debugfs_create_file("closures", 0400, NULL, NULL, &debug_fops);
202 late_initcall(closure_debug_init)