linux_work.c revision 1.1.26.1 1 1.1.26.1 christos /* $NetBSD: linux_work.c,v 1.1.26.1 2019/06/10 22:07:45 christos Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1.26.1 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Taylor R. Campbell.
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #include <sys/cdefs.h>
33 1.1.26.1 christos __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.1.26.1 2019/06/10 22:07:45 christos Exp $");
34 1.1 skrll
35 1.1 skrll #include <sys/types.h>
36 1.1 skrll #include <sys/atomic.h>
37 1.1 skrll #include <sys/callout.h>
38 1.1 skrll #include <sys/condvar.h>
39 1.1 skrll #include <sys/errno.h>
40 1.1 skrll #include <sys/kmem.h>
41 1.1.26.1 christos #include <sys/kthread.h>
42 1.1.26.1 christos #include <sys/lwp.h>
43 1.1 skrll #include <sys/mutex.h>
44 1.1.26.1 christos #ifndef _MODULE
45 1.1.26.1 christos #include <sys/once.h>
46 1.1.26.1 christos #endif
47 1.1 skrll #include <sys/queue.h>
48 1.1.26.1 christos #include <sys/sdt.h>
49 1.1 skrll
50 1.1 skrll #include <linux/workqueue.h>
51 1.1 skrll
52 1.1.26.1 christos TAILQ_HEAD(work_head, work_struct);
53 1.1.26.1 christos TAILQ_HEAD(dwork_head, delayed_work);
54 1.1 skrll
55 1.1 skrll struct workqueue_struct {
56 1.1.26.1 christos kmutex_t wq_lock;
57 1.1.26.1 christos kcondvar_t wq_cv;
58 1.1.26.1 christos struct dwork_head wq_delayed; /* delayed work scheduled */
59 1.1.26.1 christos struct work_head wq_queue; /* work to run */
60 1.1.26.1 christos struct work_head wq_dqueue; /* delayed work to run now */
61 1.1.26.1 christos struct work_struct *wq_current_work;
62 1.1.26.1 christos int wq_flags;
63 1.1.26.1 christos bool wq_dying;
64 1.1.26.1 christos uint64_t wq_gen;
65 1.1.26.1 christos struct lwp *wq_lwp;
66 1.1 skrll };
67 1.1 skrll
68 1.1.26.1 christos static void __dead linux_workqueue_thread(void *);
69 1.1.26.1 christos static void linux_workqueue_timeout(void *);
70 1.1.26.1 christos static bool work_claimed(struct work_struct *,
71 1.1.26.1 christos struct workqueue_struct *);
72 1.1.26.1 christos static struct workqueue_struct *
73 1.1.26.1 christos work_queue(struct work_struct *);
74 1.1.26.1 christos static bool acquire_work(struct work_struct *,
75 1.1.26.1 christos struct workqueue_struct *);
76 1.1.26.1 christos static void release_work(struct work_struct *,
77 1.1.26.1 christos struct workqueue_struct *);
78 1.1.26.1 christos static void wait_for_current_work(struct work_struct *,
79 1.1.26.1 christos struct workqueue_struct *);
80 1.1.26.1 christos static void dw_callout_init(struct workqueue_struct *,
81 1.1.26.1 christos struct delayed_work *);
82 1.1.26.1 christos static void dw_callout_destroy(struct workqueue_struct *,
83 1.1.26.1 christos struct delayed_work *);
84 1.1.26.1 christos static void cancel_delayed_work_done(struct workqueue_struct *,
85 1.1.26.1 christos struct delayed_work *);
86 1.1.26.1 christos
87 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, acquire,
88 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
89 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, release,
90 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
91 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, queue,
92 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
93 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, cancel,
94 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
95 1.1.26.1 christos SDT_PROBE_DEFINE3(sdt, linux, work, schedule,
96 1.1.26.1 christos "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/,
97 1.1.26.1 christos "unsigned long"/*ticks*/);
98 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, timer,
99 1.1.26.1 christos "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
100 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, wait__start,
101 1.1.26.1 christos "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
102 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, wait__done,
103 1.1.26.1 christos "struct delayed_work *"/*dw*/, "struct workqueue_struct *"/*wq*/);
104 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, run,
105 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
106 1.1.26.1 christos SDT_PROBE_DEFINE2(sdt, linux, work, done,
107 1.1.26.1 christos "struct work_struct *"/*work*/, "struct workqueue_struct *"/*wq*/);
108 1.1.26.1 christos SDT_PROBE_DEFINE1(sdt, linux, work, batch__start,
109 1.1.26.1 christos "struct workqueue_struct *"/*wq*/);
110 1.1.26.1 christos SDT_PROBE_DEFINE1(sdt, linux, work, batch__done,
111 1.1.26.1 christos "struct workqueue_struct *"/*wq*/);
112 1.1.26.1 christos SDT_PROBE_DEFINE1(sdt, linux, work, flush__start,
113 1.1.26.1 christos "struct workqueue_struct *"/*wq*/);
114 1.1.26.1 christos SDT_PROBE_DEFINE1(sdt, linux, work, flush__done,
115 1.1.26.1 christos "struct workqueue_struct *"/*wq*/);
116 1.1.26.1 christos
117 1.1.26.1 christos static specificdata_key_t workqueue_key __read_mostly;
118 1.1.26.1 christos
119 1.1.26.1 christos struct workqueue_struct *system_wq __read_mostly;
120 1.1.26.1 christos struct workqueue_struct *system_long_wq __read_mostly;
121 1.1.26.1 christos struct workqueue_struct *system_power_efficient_wq __read_mostly;
122 1.1 skrll
123 1.1.26.1 christos static inline uintptr_t
124 1.1.26.1 christos atomic_cas_uintptr(volatile uintptr_t *p, uintptr_t old, uintptr_t new)
125 1.1.26.1 christos {
126 1.1 skrll
127 1.1.26.1 christos return (uintptr_t)atomic_cas_ptr(p, (void *)old, (void *)new);
128 1.1.26.1 christos }
129 1.1.26.1 christos
130 1.1.26.1 christos /*
131 1.1.26.1 christos * linux_workqueue_init()
132 1.1.26.1 christos *
133 1.1.26.1 christos * Initialize the Linux workqueue subsystem. Return 0 on success,
134 1.1.26.1 christos * NetBSD error on failure.
135 1.1.26.1 christos */
136 1.1.26.1 christos static int
137 1.1.26.1 christos linux_workqueue_init0(void)
138 1.1 skrll {
139 1.1.26.1 christos int error;
140 1.1.26.1 christos
141 1.1.26.1 christos error = lwp_specific_key_create(&workqueue_key, NULL);
142 1.1.26.1 christos if (error)
143 1.1.26.1 christos goto fail0;
144 1.1 skrll
145 1.1 skrll system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
146 1.1.26.1 christos if (system_wq == NULL) {
147 1.1.26.1 christos error = ENOMEM;
148 1.1.26.1 christos goto fail1;
149 1.1.26.1 christos }
150 1.1.26.1 christos
151 1.1.26.1 christos system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
152 1.1.26.1 christos if (system_long_wq == NULL) {
153 1.1.26.1 christos error = ENOMEM;
154 1.1.26.1 christos goto fail2;
155 1.1.26.1 christos }
156 1.1.26.1 christos
157 1.1.26.1 christos system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
158 1.1.26.1 christos if (system_long_wq == NULL) {
159 1.1.26.1 christos error = ENOMEM;
160 1.1.26.1 christos goto fail3;
161 1.1.26.1 christos }
162 1.1 skrll
163 1.1 skrll return 0;
164 1.1.26.1 christos
165 1.1.26.1 christos fail4: __unused
166 1.1.26.1 christos destroy_workqueue(system_power_efficient_wq);
167 1.1.26.1 christos fail3: destroy_workqueue(system_long_wq);
168 1.1.26.1 christos fail2: destroy_workqueue(system_wq);
169 1.1.26.1 christos fail1: lwp_specific_key_delete(workqueue_key);
170 1.1.26.1 christos fail0: KASSERT(error);
171 1.1.26.1 christos return error;
172 1.1.26.1 christos }
173 1.1.26.1 christos
174 1.1.26.1 christos /*
175 1.1.26.1 christos * linux_workqueue_fini()
176 1.1.26.1 christos *
177 1.1.26.1 christos * Destroy the Linux workqueue subsystem. Never fails.
178 1.1.26.1 christos */
179 1.1.26.1 christos static void
180 1.1.26.1 christos linux_workqueue_fini0(void)
181 1.1.26.1 christos {
182 1.1.26.1 christos
183 1.1.26.1 christos destroy_workqueue(system_power_efficient_wq);
184 1.1.26.1 christos destroy_workqueue(system_long_wq);
185 1.1.26.1 christos destroy_workqueue(system_wq);
186 1.1.26.1 christos lwp_specific_key_delete(workqueue_key);
187 1.1.26.1 christos }
188 1.1.26.1 christos
189 1.1.26.1 christos #ifndef _MODULE
190 1.1.26.1 christos static ONCE_DECL(linux_workqueue_init_once);
191 1.1.26.1 christos #endif
192 1.1.26.1 christos
193 1.1.26.1 christos int
194 1.1.26.1 christos linux_workqueue_init(void)
195 1.1.26.1 christos {
196 1.1.26.1 christos #ifdef _MODULE
197 1.1.26.1 christos return linux_workqueue_init0();
198 1.1.26.1 christos #else
199 1.1.26.1 christos return INIT_ONCE(&linux_workqueue_init_once, &linux_workqueue_init0);
200 1.1.26.1 christos #endif
201 1.1 skrll }
202 1.1 skrll
203 1.1 skrll void
204 1.1 skrll linux_workqueue_fini(void)
205 1.1 skrll {
206 1.1.26.1 christos #ifdef _MODULE
207 1.1.26.1 christos return linux_workqueue_fini0();
208 1.1.26.1 christos #else
209 1.1.26.1 christos return FINI_ONCE(&linux_workqueue_init_once, &linux_workqueue_fini0);
210 1.1.26.1 christos #endif
211 1.1 skrll }
212 1.1 skrll
213 1.1 skrll /*
215 1.1 skrll * Workqueues
216 1.1 skrll */
217 1.1.26.1 christos
218 1.1.26.1 christos /*
219 1.1.26.1 christos * alloc_ordered_workqueue(name, flags)
220 1.1.26.1 christos *
221 1.1.26.1 christos * Create a workqueue of the given name. No flags are currently
222 1.1.26.1 christos * defined. Return NULL on failure, pointer to struct
223 1.1.26.1 christos * workqueue_struct object on success.
224 1.1 skrll */
225 1.1.26.1 christos struct workqueue_struct *
226 1.1 skrll alloc_ordered_workqueue(const char *name, int flags)
227 1.1 skrll {
228 1.1 skrll struct workqueue_struct *wq;
229 1.1 skrll int error;
230 1.1.26.1 christos
231 1.1 skrll KASSERT(flags == 0);
232 1.1.26.1 christos
233 1.1 skrll wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
234 1.1 skrll
235 1.1 skrll mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_VM);
236 1.1 skrll cv_init(&wq->wq_cv, name);
237 1.1.26.1 christos TAILQ_INIT(&wq->wq_delayed);
238 1.1.26.1 christos TAILQ_INIT(&wq->wq_queue);
239 1.1 skrll TAILQ_INIT(&wq->wq_dqueue);
240 1.1.26.1 christos wq->wq_current_work = NULL;
241 1.1.26.1 christos wq->wq_flags = 0;
242 1.1.26.1 christos wq->wq_dying = false;
243 1.1.26.1 christos wq->wq_gen = 0;
244 1.1.26.1 christos wq->wq_lwp = NULL;
245 1.1.26.1 christos
246 1.1.26.1 christos error = kthread_create(PRI_NONE,
247 1.1.26.1 christos KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
248 1.1.26.1 christos &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
249 1.1.26.1 christos if (error)
250 1.1 skrll goto fail0;
251 1.1 skrll
252 1.1.26.1 christos return wq;
253 1.1.26.1 christos
254 1.1.26.1 christos fail0: KASSERT(TAILQ_EMPTY(&wq->wq_dqueue));
255 1.1.26.1 christos KASSERT(TAILQ_EMPTY(&wq->wq_queue));
256 1.1.26.1 christos KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
257 1.1.26.1 christos cv_destroy(&wq->wq_cv);
258 1.1.26.1 christos mutex_destroy(&wq->wq_lock);
259 1.1.26.1 christos kmem_free(wq, sizeof(*wq));
260 1.1 skrll return NULL;
261 1.1 skrll }
262 1.1.26.1 christos
263 1.1.26.1 christos /*
264 1.1.26.1 christos * destroy_workqueue(wq)
265 1.1.26.1 christos *
266 1.1.26.1 christos * Destroy a workqueue created with wq. Cancel any pending
267 1.1.26.1 christos * delayed work. Wait for all queued work to complete.
268 1.1.26.1 christos *
269 1.1.26.1 christos * May sleep.
270 1.1 skrll */
271 1.1 skrll void
272 1.1 skrll destroy_workqueue(struct workqueue_struct *wq)
273 1.1 skrll {
274 1.1 skrll
275 1.1.26.1 christos /*
276 1.1.26.1 christos * Cancel all delayed work. We do this first because any
277 1.1.26.1 christos * delayed work that that has already timed out, which we can't
278 1.1 skrll * cancel, may have queued new work.
279 1.1.26.1 christos */
280 1.1.26.1 christos mutex_enter(&wq->wq_lock);
281 1.1.26.1 christos while (!TAILQ_EMPTY(&wq->wq_delayed)) {
282 1.1 skrll struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
283 1.1.26.1 christos
284 1.1.26.1 christos KASSERT(work_queue(&dw->work) == wq);
285 1.1.26.1 christos KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
286 1.1.26.1 christos dw->dw_state == DELAYED_WORK_RESCHEDULED ||
287 1.1.26.1 christos dw->dw_state == DELAYED_WORK_CANCELLED),
288 1.1.26.1 christos "delayed work %p in bad state: %d",
289 1.1 skrll dw, dw->dw_state);
290 1.1.26.1 christos
291 1.1.26.1 christos /*
292 1.1.26.1 christos * Mark it cancelled and try to stop the callout before
293 1.1.26.1 christos * it starts.
294 1.1.26.1 christos *
295 1.1.26.1 christos * If it's too late and the callout has already begun
296 1.1.26.1 christos * to execute, then it will notice that we asked to
297 1.1.26.1 christos * cancel it and remove itself from the queue before
298 1.1.26.1 christos * returning.
299 1.1.26.1 christos *
300 1.1.26.1 christos * If we stopped the callout before it started,
301 1.1.26.1 christos * however, then we can safely destroy the callout and
302 1.1.26.1 christos * dissociate it from the workqueue ourselves.
303 1.1.26.1 christos */
304 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, &dw->work, wq);
305 1.1.26.1 christos dw->dw_state = DELAYED_WORK_CANCELLED;
306 1.1.26.1 christos if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
307 1.1 skrll cancel_delayed_work_done(wq, dw);
308 1.1.26.1 christos }
309 1.1 skrll mutex_exit(&wq->wq_lock);
310 1.1 skrll
311 1.1.26.1 christos /*
312 1.1 skrll * At this point, no new work can be put on the queue.
313 1.1 skrll */
314 1.1.26.1 christos
315 1.1.26.1 christos /* Tell the thread to exit. */
316 1.1.26.1 christos mutex_enter(&wq->wq_lock);
317 1.1.26.1 christos wq->wq_dying = true;
318 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
319 1.1.26.1 christos mutex_exit(&wq->wq_lock);
320 1.1.26.1 christos
321 1.1.26.1 christos /* Wait for it to exit. */
322 1.1.26.1 christos (void)kthread_join(wq->wq_lwp);
323 1.1.26.1 christos
324 1.1.26.1 christos KASSERT(wq->wq_dying);
325 1.1.26.1 christos KASSERT(wq->wq_flags == 0);
326 1.1.26.1 christos KASSERT(wq->wq_current_work == NULL);
327 1.1.26.1 christos KASSERT(TAILQ_EMPTY(&wq->wq_dqueue));
328 1.1.26.1 christos KASSERT(TAILQ_EMPTY(&wq->wq_queue));
329 1.1 skrll KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
330 1.1 skrll cv_destroy(&wq->wq_cv);
331 1.1 skrll mutex_destroy(&wq->wq_lock);
332 1.1 skrll
333 1.1 skrll kmem_free(wq, sizeof(*wq));
334 1.1 skrll }
335 1.1 skrll
336 1.1.26.1 christos /*
338 1.1 skrll * Work thread and callout
339 1.1.26.1 christos */
340 1.1.26.1 christos
341 1.1.26.1 christos /*
342 1.1.26.1 christos * linux_workqueue_thread(cookie)
343 1.1.26.1 christos *
344 1.1.26.1 christos * Main function for a workqueue's worker thread. Waits until
345 1.1.26.1 christos * there is work queued, grabs a batch of work off the queue,
346 1.1.26.1 christos * executes it all, bumps the generation number, and repeats,
347 1.1.26.1 christos * until dying.
348 1.1.26.1 christos */
349 1.1 skrll static void __dead
350 1.1.26.1 christos linux_workqueue_thread(void *cookie)
351 1.1.26.1 christos {
352 1.1.26.1 christos struct workqueue_struct *const wq = cookie;
353 1.1.26.1 christos struct work_head queue, dqueue;
354 1.1 skrll struct work_head *const q[2] = { &queue, &dqueue };
355 1.1.26.1 christos unsigned i;
356 1.1 skrll
357 1.1.26.1 christos lwp_setspecific(workqueue_key, wq);
358 1.1.26.1 christos
359 1.1.26.1 christos mutex_enter(&wq->wq_lock);
360 1.1.26.1 christos for (;;) {
361 1.1.26.1 christos /*
362 1.1.26.1 christos * Wait until there's activity. If there's no work and
363 1.1.26.1 christos * we're dying, stop here.
364 1.1.26.1 christos */
365 1.1.26.1 christos if (TAILQ_EMPTY(&wq->wq_queue) &&
366 1.1.26.1 christos TAILQ_EMPTY(&wq->wq_dqueue)) {
367 1.1.26.1 christos if (wq->wq_dying)
368 1.1.26.1 christos break;
369 1.1.26.1 christos cv_wait(&wq->wq_cv, &wq->wq_lock);
370 1.1 skrll continue;
371 1.1.26.1 christos }
372 1.1.26.1 christos
373 1.1.26.1 christos /* Grab a batch of work off the queue. */
374 1.1.26.1 christos SDT_PROBE1(sdt, linux, work, batch__start, wq);
375 1.1.26.1 christos TAILQ_INIT(&queue);
376 1.1.26.1 christos TAILQ_INIT(&dqueue);
377 1.1.26.1 christos TAILQ_CONCAT(&queue, &wq->wq_queue, work_entry);
378 1.1.26.1 christos TAILQ_CONCAT(&dqueue, &wq->wq_dqueue, work_entry);
379 1.1.26.1 christos
380 1.1.26.1 christos /* Process each work item in the batch. */
381 1.1.26.1 christos for (i = 0; i < 2; i++) {
382 1.1.26.1 christos while (!TAILQ_EMPTY(q[i])) {
383 1.1.26.1 christos struct work_struct *work = TAILQ_FIRST(q[i]);
384 1.1.26.1 christos void (*func)(struct work_struct *);
385 1.1.26.1 christos
386 1.1.26.1 christos KASSERT(work_queue(work) == wq);
387 1.1.26.1 christos KASSERT(work_claimed(work, wq));
388 1.1.26.1 christos KASSERTMSG((q[i] != &dqueue ||
389 1.1.26.1 christos container_of(work, struct delayed_work,
390 1.1.26.1 christos work)->dw_state ==
391 1.1.26.1 christos DELAYED_WORK_IDLE),
392 1.1.26.1 christos "delayed work %p queued and scheduled",
393 1.1.26.1 christos work);
394 1.1.26.1 christos
395 1.1.26.1 christos TAILQ_REMOVE(q[i], work, work_entry);
396 1.1.26.1 christos KASSERT(wq->wq_current_work == NULL);
397 1.1.26.1 christos wq->wq_current_work = work;
398 1.1.26.1 christos func = work->func;
399 1.1.26.1 christos release_work(work, wq);
400 1.1.26.1 christos /* Can't dereference work after this point. */
401 1.1.26.1 christos
402 1.1.26.1 christos mutex_exit(&wq->wq_lock);
403 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, run, work, wq);
404 1.1.26.1 christos (*func)(work);
405 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, done, work, wq);
406 1.1.26.1 christos mutex_enter(&wq->wq_lock);
407 1.1.26.1 christos
408 1.1.26.1 christos KASSERT(wq->wq_current_work == work);
409 1.1.26.1 christos wq->wq_current_work = NULL;
410 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
411 1.1 skrll }
412 1.1.26.1 christos }
413 1.1.26.1 christos
414 1.1.26.1 christos /* Notify flush that we've completed a batch of work. */
415 1.1.26.1 christos wq->wq_gen++;
416 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
417 1.1.26.1 christos SDT_PROBE1(sdt, linux, work, batch__done, wq);
418 1.1.26.1 christos }
419 1.1.26.1 christos mutex_exit(&wq->wq_lock);
420 1.1 skrll
421 1.1 skrll kthread_exit(0);
422 1.1.26.1 christos }
423 1.1.26.1 christos
424 1.1.26.1 christos /*
425 1.1.26.1 christos * linux_workqueue_timeout(cookie)
426 1.1.26.1 christos *
427 1.1.26.1 christos * Delayed work timeout callback.
428 1.1.26.1 christos *
429 1.1.26.1 christos * - If scheduled, queue it.
430 1.1.26.1 christos * - If rescheduled, callout_schedule ourselves again.
431 1.1.26.1 christos * - If cancelled, destroy the callout and release the work from
432 1.1.26.1 christos * the workqueue.
433 1.1.26.1 christos */
434 1.1 skrll static void
435 1.1.26.1 christos linux_workqueue_timeout(void *cookie)
436 1.1.26.1 christos {
437 1.1 skrll struct delayed_work *const dw = cookie;
438 1.1.26.1 christos struct workqueue_struct *const wq = work_queue(&dw->work);
439 1.1.26.1 christos
440 1.1.26.1 christos KASSERTMSG(wq != NULL,
441 1.1 skrll "delayed work %p state %d resched %d",
442 1.1.26.1 christos dw, dw->dw_state, dw->dw_resched);
443 1.1 skrll
444 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, timer, dw, wq);
445 1.1.26.1 christos
446 1.1.26.1 christos mutex_enter(&wq->wq_lock);
447 1.1.26.1 christos KASSERT(work_queue(&dw->work) == wq);
448 1.1.26.1 christos switch (dw->dw_state) {
449 1.1.26.1 christos case DELAYED_WORK_IDLE:
450 1.1.26.1 christos panic("delayed work callout uninitialized: %p", dw);
451 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
452 1.1.26.1 christos dw_callout_destroy(wq, dw);
453 1.1.26.1 christos TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work, work_entry);
454 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
455 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue, &dw->work, wq);
456 1.1.26.1 christos break;
457 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
458 1.1.26.1 christos KASSERT(dw->dw_resched >= 0);
459 1.1.26.1 christos callout_schedule(&dw->dw_callout, dw->dw_resched);
460 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
461 1.1.26.1 christos dw->dw_resched = -1;
462 1.1.26.1 christos break;
463 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
464 1.1.26.1 christos cancel_delayed_work_done(wq, dw);
465 1.1.26.1 christos /* Can't dereference dw after this point. */
466 1.1.26.1 christos goto out;
467 1.1.26.1 christos default:
468 1.1.26.1 christos panic("delayed work callout in bad state: %p", dw);
469 1.1.26.1 christos }
470 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
471 1.1 skrll dw->dw_state == DELAYED_WORK_SCHEDULED);
472 1.1 skrll out: mutex_exit(&wq->wq_lock);
473 1.1.26.1 christos }
474 1.1.26.1 christos
475 1.1.26.1 christos /*
476 1.1.26.1 christos * current_work()
477 1.1.26.1 christos *
478 1.1.26.1 christos * If in a workqueue worker thread, return the work it is
479 1.1.26.1 christos * currently executing. Otherwise return NULL.
480 1.1.26.1 christos */
481 1.1 skrll struct work_struct *
482 1.1.26.1 christos current_work(void)
483 1.1 skrll {
484 1.1.26.1 christos struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
485 1.1.26.1 christos
486 1.1.26.1 christos /* If we're not a workqueue thread, then there's no work. */
487 1.1.26.1 christos if (wq == NULL)
488 1.1.26.1 christos return NULL;
489 1.1.26.1 christos
490 1.1.26.1 christos /*
491 1.1.26.1 christos * Otherwise, this should be possible only while work is in
492 1.1.26.1 christos * progress. Return the current work item.
493 1.1.26.1 christos */
494 1.1 skrll KASSERT(wq->wq_current_work != NULL);
495 1.1 skrll return wq->wq_current_work;
496 1.1 skrll }
497 1.1.26.1 christos
498 1.1 skrll /*
500 1.1.26.1 christos * Work
501 1.1.26.1 christos */
502 1.1.26.1 christos
503 1.1.26.1 christos /*
504 1.1.26.1 christos * INIT_WORK(work, fn)
505 1.1.26.1 christos *
506 1.1.26.1 christos * Initialize work for use with a workqueue to call fn in a worker
507 1.1.26.1 christos * thread. There is no corresponding destruction operation.
508 1.1 skrll */
509 1.1 skrll void
510 1.1.26.1 christos INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
511 1.1.26.1 christos {
512 1.1 skrll
513 1.1 skrll work->work_owner = 0;
514 1.1.26.1 christos work->func = fn;
515 1.1.26.1 christos }
516 1.1.26.1 christos
517 1.1.26.1 christos /*
518 1.1.26.1 christos * work_claimed(work, wq)
519 1.1.26.1 christos *
520 1.1.26.1 christos * True if work is currently claimed by a workqueue, meaning it is
521 1.1.26.1 christos * either on the queue or scheduled in a callout. The workqueue
522 1.1.26.1 christos * must be wq, and caller must hold wq's lock.
523 1.1 skrll */
524 1.1 skrll static bool
525 1.1.26.1 christos work_claimed(struct work_struct *work, struct workqueue_struct *wq)
526 1.1.26.1 christos {
527 1.1 skrll
528 1.1.26.1 christos KASSERT(work_queue(work) == wq);
529 1.1 skrll KASSERT(mutex_owned(&wq->wq_lock));
530 1.1 skrll
531 1.1.26.1 christos return work->work_owner & 1;
532 1.1.26.1 christos }
533 1.1.26.1 christos
534 1.1.26.1 christos /*
535 1.1.26.1 christos * work_queue(work)
536 1.1.26.1 christos *
537 1.1.26.1 christos * Return the last queue that work was queued on, or NULL if it
538 1.1.26.1 christos * was never queued.
539 1.1 skrll */
540 1.1 skrll static struct workqueue_struct *
541 1.1.26.1 christos work_queue(struct work_struct *work)
542 1.1 skrll {
543 1.1 skrll
544 1.1.26.1 christos return (struct workqueue_struct *)(work->work_owner & ~(uintptr_t)1);
545 1.1.26.1 christos }
546 1.1.26.1 christos
547 1.1.26.1 christos /*
548 1.1.26.1 christos * acquire_work(work, wq)
549 1.1.26.1 christos *
550 1.1.26.1 christos * Try to claim work for wq. If work is already claimed, it must
551 1.1.26.1 christos * be claimed by wq; return false. If work is not already
552 1.1.26.1 christos * claimed, claim it, issue a memory barrier to match any prior
553 1.1.26.1 christos * release_work, and return true.
554 1.1.26.1 christos *
555 1.1.26.1 christos * Caller must hold wq's lock.
556 1.1 skrll */
557 1.1.26.1 christos static bool
558 1.1.26.1 christos acquire_work(struct work_struct *work, struct workqueue_struct *wq)
559 1.1.26.1 christos {
560 1.1.26.1 christos uintptr_t owner0, owner;
561 1.1.26.1 christos
562 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
563 1.1.26.1 christos KASSERT(((uintptr_t)wq & 1) == 0);
564 1.1.26.1 christos
565 1.1.26.1 christos owner = (uintptr_t)wq | 1;
566 1.1.26.1 christos do {
567 1.1.26.1 christos owner0 = work->work_owner;
568 1.1.26.1 christos if (owner0 & 1) {
569 1.1.26.1 christos KASSERT((owner0 & ~(uintptr_t)1) == (uintptr_t)wq);
570 1.1.26.1 christos return false;
571 1.1.26.1 christos }
572 1.1.26.1 christos KASSERT(owner0 == (uintptr_t)NULL || owner0 == (uintptr_t)wq);
573 1.1.26.1 christos } while (atomic_cas_uintptr(&work->work_owner, owner0, owner) !=
574 1.1.26.1 christos owner0);
575 1.1.26.1 christos
576 1.1.26.1 christos KASSERT(work_queue(work) == wq);
577 1.1 skrll membar_enter();
578 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, acquire, work, wq);
579 1.1 skrll return true;
580 1.1.26.1 christos }
581 1.1.26.1 christos
582 1.1.26.1 christos /*
583 1.1.26.1 christos * release_work(work, wq)
584 1.1.26.1 christos *
585 1.1.26.1 christos * Issue a memory barrier to match any subsequent acquire_work and
586 1.1 skrll * dissociate work from wq.
587 1.1.26.1 christos *
588 1.1.26.1 christos * Caller must hold wq's lock and work must be associated with wq.
589 1.1 skrll */
590 1.1 skrll static void
591 1.1.26.1 christos release_work(struct work_struct *work, struct workqueue_struct *wq)
592 1.1.26.1 christos {
593 1.1.26.1 christos
594 1.1.26.1 christos KASSERT(work_queue(work) == wq);
595 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
596 1.1.26.1 christos
597 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, release, work, wq);
598 1.1.26.1 christos membar_exit();
599 1.1.26.1 christos
600 1.1.26.1 christos /*
601 1.1.26.1 christos * Non-interlocked r/m/w is safe here because nobody else can
602 1.1.26.1 christos * write to this while the claimed bit is setand the workqueue
603 1.1 skrll * lock is held.
604 1.1 skrll */
605 1.1.26.1 christos work->work_owner &= ~(uintptr_t)1;
606 1.1.26.1 christos }
607 1.1.26.1 christos
608 1.1.26.1 christos /*
609 1.1.26.1 christos * schedule_work(work)
610 1.1.26.1 christos *
611 1.1.26.1 christos * If work is not already queued on system_wq, queue it to be run
612 1.1.26.1 christos * by system_wq's worker thread when it next can. True if it was
613 1.1.26.1 christos * newly queued, false if it was already queued. If the work was
614 1.1.26.1 christos * already running, queue it to run again.
615 1.1.26.1 christos *
616 1.1 skrll * Caller must ensure work is not queued to run on a different
617 1.1 skrll * workqueue.
618 1.1 skrll */
619 1.1.26.1 christos bool
620 1.1 skrll schedule_work(struct work_struct *work)
621 1.1 skrll {
622 1.1 skrll
623 1.1.26.1 christos return queue_work(system_wq, work);
624 1.1.26.1 christos }
625 1.1.26.1 christos
626 1.1.26.1 christos /*
627 1.1.26.1 christos * queue_work(wq, work)
628 1.1.26.1 christos *
629 1.1.26.1 christos * If work is not already queued on wq, queue it to be run by wq's
630 1.1.26.1 christos * worker thread when it next can. True if it was newly queued,
631 1.1.26.1 christos * false if it was already queued. If the work was already
632 1.1.26.1 christos * running, queue it to run again.
633 1.1.26.1 christos *
634 1.1 skrll * Caller must ensure work is not queued to run on a different
635 1.1 skrll * workqueue.
636 1.1 skrll */
637 1.1 skrll bool
638 1.1 skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
639 1.1 skrll {
640 1.1 skrll bool newly_queued;
641 1.1.26.1 christos
642 1.1.26.1 christos KASSERT(wq != NULL);
643 1.1.26.1 christos
644 1.1.26.1 christos mutex_enter(&wq->wq_lock);
645 1.1.26.1 christos if (__predict_true(acquire_work(work, wq))) {
646 1.1.26.1 christos /*
647 1.1.26.1 christos * It wasn't on any workqueue at all. Put it on this
648 1.1.26.1 christos * one, and signal the worker thread that there is work
649 1.1.26.1 christos * to do.
650 1.1.26.1 christos */
651 1.1 skrll TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
652 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
653 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue, work, wq);
654 1.1.26.1 christos newly_queued = true;
655 1.1.26.1 christos } else {
656 1.1.26.1 christos /*
657 1.1 skrll * It was already on this workqueue. Nothing to do
658 1.1 skrll * since it is already queued.
659 1.1.26.1 christos */
660 1.1 skrll newly_queued = false;
661 1.1 skrll }
662 1.1 skrll mutex_exit(&wq->wq_lock);
663 1.1 skrll
664 1.1.26.1 christos return newly_queued;
665 1.1.26.1 christos }
666 1.1.26.1 christos
667 1.1.26.1 christos /*
668 1.1.26.1 christos * cancel_work(work)
669 1.1.26.1 christos *
670 1.1.26.1 christos * If work was queued, remove it from the queue and return true.
671 1.1 skrll * If work was not queued, return false. Work may still be
672 1.1.26.1 christos * running when this returns.
673 1.1 skrll */
674 1.1.26.1 christos bool
675 1.1 skrll cancel_work(struct work_struct *work)
676 1.1 skrll {
677 1.1.26.1 christos struct workqueue_struct *wq;
678 1.1.26.1 christos bool cancelled_p = false;
679 1.1.26.1 christos
680 1.1 skrll /* If there's no workqueue, nothing to cancel. */
681 1.1.26.1 christos if ((wq = work_queue(work)) == NULL)
682 1.1.26.1 christos goto out;
683 1.1.26.1 christos
684 1.1.26.1 christos mutex_enter(&wq->wq_lock);
685 1.1.26.1 christos if (__predict_false(work_queue(work) != wq)) {
686 1.1.26.1 christos /*
687 1.1.26.1 christos * It has finished execution or been cancelled by
688 1.1.26.1 christos * another thread, and has been moved off the
689 1.1.26.1 christos * workqueue, so it's too to cancel.
690 1.1.26.1 christos */
691 1.1.26.1 christos cancelled_p = false;
692 1.1.26.1 christos } else {
693 1.1.26.1 christos /* Check whether it's on the queue. */
694 1.1.26.1 christos if (work_claimed(work, wq)) {
695 1.1.26.1 christos /*
696 1.1.26.1 christos * It is still on the queue. Take it off the
697 1.1.26.1 christos * queue and report successful cancellation.
698 1.1.26.1 christos */
699 1.1.26.1 christos TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
700 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, work, wq);
701 1.1.26.1 christos release_work(work, wq);
702 1.1.26.1 christos /* Can't dereference work after this point. */
703 1.1.26.1 christos cancelled_p = true;
704 1.1.26.1 christos } else {
705 1.1.26.1 christos /* Not on the queue. Couldn't cancel it. */
706 1.1.26.1 christos cancelled_p = false;
707 1.1 skrll }
708 1.1.26.1 christos }
709 1.1.26.1 christos mutex_exit(&wq->wq_lock);
710 1.1 skrll
711 1.1.26.1 christos out: return cancelled_p;
712 1.1.26.1 christos }
713 1.1.26.1 christos
714 1.1.26.1 christos /*
715 1.1.26.1 christos * cancel_work_sync(work)
716 1.1.26.1 christos *
717 1.1.26.1 christos * If work was queued, remove it from the queue and return true.
718 1.1.26.1 christos * If work was not queued, return false. Either way, if work is
719 1.1.26.1 christos * currently running, wait for it to complete.
720 1.1.26.1 christos *
721 1.1.26.1 christos * May sleep.
722 1.1.26.1 christos */
723 1.1.26.1 christos bool
724 1.1.26.1 christos cancel_work_sync(struct work_struct *work)
725 1.1 skrll {
726 1.1.26.1 christos struct workqueue_struct *wq;
727 1.1.26.1 christos bool cancelled_p = false;
728 1.1.26.1 christos
729 1.1 skrll /* If there's no workqueue, nothing to cancel. */
730 1.1.26.1 christos if ((wq = work_queue(work)) == NULL)
731 1.1.26.1 christos goto out;
732 1.1.26.1 christos
733 1.1.26.1 christos mutex_enter(&wq->wq_lock);
734 1.1.26.1 christos if (__predict_false(work_queue(work) != wq)) {
735 1.1.26.1 christos /*
736 1.1.26.1 christos * It has finished execution or been cancelled by
737 1.1.26.1 christos * another thread, and has been moved off the
738 1.1.26.1 christos * workqueue, so it's too late to cancel.
739 1.1.26.1 christos */
740 1.1.26.1 christos cancelled_p = false;
741 1.1.26.1 christos } else {
742 1.1.26.1 christos /* Check whether it's on the queue. */
743 1.1.26.1 christos if (work_claimed(work, wq)) {
744 1.1.26.1 christos /*
745 1.1.26.1 christos * It is still on the queue. Take it off the
746 1.1.26.1 christos * queue and report successful cancellation.
747 1.1.26.1 christos */
748 1.1.26.1 christos TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
749 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, work, wq);
750 1.1.26.1 christos release_work(work, wq);
751 1.1.26.1 christos /* Can't dereference work after this point. */
752 1.1.26.1 christos cancelled_p = true;
753 1.1.26.1 christos } else {
754 1.1.26.1 christos /* Not on the queue. Couldn't cancel it. */
755 1.1.26.1 christos cancelled_p = false;
756 1.1.26.1 christos }
757 1.1 skrll /* If it's still running, wait for it to complete. */
758 1.1.26.1 christos if (wq->wq_current_work == work)
759 1.1 skrll wait_for_current_work(work, wq);
760 1.1.26.1 christos }
761 1.1 skrll mutex_exit(&wq->wq_lock);
762 1.1 skrll
763 1.1.26.1 christos out: return cancelled_p;
764 1.1.26.1 christos }
765 1.1.26.1 christos
766 1.1.26.1 christos /*
767 1.1.26.1 christos * wait_for_current_work(work, wq)
768 1.1.26.1 christos *
769 1.1.26.1 christos * wq must be currently executing work. Wait for it to finish.
770 1.1 skrll *
771 1.1.26.1 christos * Does not dereference work.
772 1.1 skrll */
773 1.1.26.1 christos static void
774 1.1 skrll wait_for_current_work(struct work_struct *work, struct workqueue_struct *wq)
775 1.1.26.1 christos {
776 1.1.26.1 christos uint64_t gen;
777 1.1 skrll
778 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
779 1.1.26.1 christos KASSERT(wq->wq_current_work == work);
780 1.1.26.1 christos
781 1.1 skrll /* Wait only one generation in case it gets requeued quickly. */
782 1.1 skrll SDT_PROBE2(sdt, linux, work, wait__start, work, wq);
783 1.1.26.1 christos gen = wq->wq_gen;
784 1.1.26.1 christos do {
785 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
786 1.1.26.1 christos } while (wq->wq_current_work == work && wq->wq_gen == gen);
787 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, wait__done, work, wq);
788 1.1.26.1 christos }
789 1.1.26.1 christos
790 1.1 skrll /*
792 1.1.26.1 christos * Delayed work
793 1.1.26.1 christos */
794 1.1.26.1 christos
795 1.1.26.1 christos /*
796 1.1.26.1 christos * INIT_DELAYED_WORK(dw, fn)
797 1.1.26.1 christos *
798 1.1.26.1 christos * Initialize dw for use with a workqueue to call fn in a worker
799 1.1.26.1 christos * thread after a delay. There is no corresponding destruction
800 1.1 skrll * operation.
801 1.1 skrll */
802 1.1.26.1 christos void
803 1.1.26.1 christos INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
804 1.1.26.1 christos {
805 1.1 skrll
806 1.1.26.1 christos INIT_WORK(&dw->work, fn);
807 1.1.26.1 christos dw->dw_state = DELAYED_WORK_IDLE;
808 1.1.26.1 christos dw->dw_resched = -1;
809 1.1.26.1 christos
810 1.1.26.1 christos /*
811 1.1.26.1 christos * Defer callout_init until we are going to schedule the
812 1.1 skrll * callout, which can then callout_destroy it, because
813 1.1 skrll * otherwise since there's no DESTROY_DELAYED_WORK or anything
814 1.1.26.1 christos * we have no opportunity to call callout_destroy.
815 1.1.26.1 christos */
816 1.1.26.1 christos }
817 1.1.26.1 christos
818 1.1.26.1 christos /*
819 1.1.26.1 christos * schedule_delayed_work(dw, ticks)
820 1.1.26.1 christos *
821 1.1.26.1 christos * If it is not currently scheduled, schedule dw to run after
822 1.1.26.1 christos * ticks on system_wq. If currently executing and not already
823 1.1.26.1 christos * rescheduled, reschedule it. True if it was newly scheduled,
824 1.1.26.1 christos * false if it was already scheduled.
825 1.1.26.1 christos *
826 1.1.26.1 christos * If ticks == 0, queue it to run as soon as the worker can,
827 1.1 skrll * without waiting for the next callout tick to run.
828 1.1 skrll */
829 1.1.26.1 christos bool
830 1.1.26.1 christos schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
831 1.1 skrll {
832 1.1.26.1 christos
833 1.1.26.1 christos return queue_delayed_work(system_wq, dw, ticks);
834 1.1.26.1 christos }
835 1.1.26.1 christos
836 1.1.26.1 christos /*
837 1.1.26.1 christos * dw_callout_init(wq, dw)
838 1.1.26.1 christos *
839 1.1.26.1 christos * Initialize the callout of dw and transition to
840 1.1.26.1 christos * DELAYED_WORK_SCHEDULED. Caller must use callout_schedule.
841 1.1 skrll */
842 1.1.26.1 christos static void
843 1.1.26.1 christos dw_callout_init(struct workqueue_struct *wq, struct delayed_work *dw)
844 1.1.26.1 christos {
845 1.1 skrll
846 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
847 1.1.26.1 christos KASSERT(work_queue(&dw->work) == wq);
848 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
849 1.1.26.1 christos
850 1.1.26.1 christos callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
851 1.1 skrll callout_setfunc(&dw->dw_callout, &linux_workqueue_timeout, dw);
852 1.1.26.1 christos TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
853 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
854 1.1.26.1 christos }
855 1.1.26.1 christos
856 1.1.26.1 christos /*
857 1.1.26.1 christos * dw_callout_destroy(wq, dw)
858 1.1.26.1 christos *
859 1.1.26.1 christos * Destroy the callout of dw and transition to DELAYED_WORK_IDLE.
860 1.1 skrll */
861 1.1.26.1 christos static void
862 1.1.26.1 christos dw_callout_destroy(struct workqueue_struct *wq, struct delayed_work *dw)
863 1.1.26.1 christos {
864 1.1.26.1 christos
865 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
866 1.1 skrll KASSERT(work_queue(&dw->work) == wq);
867 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED ||
868 1.1.26.1 christos dw->dw_state == DELAYED_WORK_RESCHEDULED ||
869 1.1.26.1 christos dw->dw_state == DELAYED_WORK_CANCELLED);
870 1.1.26.1 christos
871 1.1 skrll TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
872 1.1.26.1 christos callout_destroy(&dw->dw_callout);
873 1.1 skrll dw->dw_resched = -1;
874 1.1.26.1 christos dw->dw_state = DELAYED_WORK_IDLE;
875 1.1.26.1 christos }
876 1.1.26.1 christos
877 1.1.26.1 christos /*
878 1.1.26.1 christos * cancel_delayed_work_done(wq, dw)
879 1.1 skrll *
880 1.1.26.1 christos * Complete cancellation of a delayed work: transition from
881 1.1.26.1 christos * DELAYED_WORK_CANCELLED to DELAYED_WORK_IDLE and off the
882 1.1 skrll * workqueue. Caller must not dereference dw after this returns.
883 1.1 skrll */
884 1.1.26.1 christos static void
885 1.1.26.1 christos cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
886 1.1.26.1 christos {
887 1.1.26.1 christos
888 1.1.26.1 christos KASSERT(mutex_owned(&wq->wq_lock));
889 1.1.26.1 christos KASSERT(work_queue(&dw->work) == wq);
890 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
891 1.1 skrll
892 1.1 skrll dw_callout_destroy(wq, dw);
893 1.1.26.1 christos release_work(&dw->work, wq);
894 1.1.26.1 christos /* Can't dereference dw after this point. */
895 1.1.26.1 christos }
896 1.1.26.1 christos
897 1.1.26.1 christos /*
898 1.1.26.1 christos * queue_delayed_work(wq, dw, ticks)
899 1.1.26.1 christos *
900 1.1.26.1 christos * If it is not currently scheduled, schedule dw to run after
901 1.1.26.1 christos * ticks on wq. If currently queued, remove it from the queue
902 1.1.26.1 christos * first.
903 1.1 skrll *
904 1.1 skrll * If ticks == 0, queue it to run as soon as the worker can,
905 1.1 skrll * without waiting for the next callout tick to run.
906 1.1 skrll */
907 1.1 skrll bool
908 1.1 skrll queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
909 1.1.26.1 christos unsigned long ticks)
910 1.1.26.1 christos {
911 1.1.26.1 christos bool newly_queued;
912 1.1.26.1 christos
913 1.1.26.1 christos mutex_enter(&wq->wq_lock);
914 1.1.26.1 christos if (__predict_true(acquire_work(&dw->work, wq))) {
915 1.1.26.1 christos /*
916 1.1 skrll * It wasn't on any workqueue at all. Schedule it to
917 1.1.26.1 christos * run on this one.
918 1.1.26.1 christos */
919 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
920 1.1.26.1 christos if (ticks == 0) {
921 1.1 skrll TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
922 1.1.26.1 christos work_entry);
923 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
924 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue, &dw->work, wq);
925 1.1.26.1 christos } else {
926 1.1.26.1 christos /*
927 1.1.26.1 christos * Initialize a callout and schedule to run
928 1.1.26.1 christos * after a delay.
929 1.1 skrll */
930 1.1 skrll dw_callout_init(wq, dw);
931 1.1.26.1 christos callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
932 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule, dw, wq, ticks);
933 1.1.26.1 christos }
934 1.1.26.1 christos newly_queued = true;
935 1.1.26.1 christos } else {
936 1.1.26.1 christos /* It was already on this workqueue. */
937 1.1.26.1 christos switch (dw->dw_state) {
938 1.1.26.1 christos case DELAYED_WORK_IDLE:
939 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
940 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
941 1.1.26.1 christos /* On the queue or already scheduled. Leave it. */
942 1.1.26.1 christos newly_queued = false;
943 1.1.26.1 christos break;
944 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
945 1.1.26.1 christos /*
946 1.1.26.1 christos * Scheduled and the callout began, but it was
947 1.1.26.1 christos * cancelled. Reschedule it.
948 1.1.26.1 christos */
949 1.1.26.1 christos if (ticks == 0) {
950 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
951 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
952 1.1.26.1 christos &dw->work, wq);
953 1.1.26.1 christos } else {
954 1.1.26.1 christos dw->dw_state = DELAYED_WORK_RESCHEDULED;
955 1.1.26.1 christos dw->dw_resched = MIN(INT_MAX, ticks);
956 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
957 1.1.26.1 christos dw, wq, ticks);
958 1.1.26.1 christos }
959 1.1.26.1 christos newly_queued = true;
960 1.1.26.1 christos break;
961 1.1 skrll default:
962 1.1.26.1 christos panic("invalid delayed work state: %d",
963 1.1 skrll dw->dw_state);
964 1.1 skrll }
965 1.1 skrll }
966 1.1 skrll mutex_exit(&wq->wq_lock);
967 1.1.26.1 christos
968 1.1.26.1 christos return newly_queued;
969 1.1.26.1 christos }
970 1.1.26.1 christos
971 1.1.26.1 christos /*
972 1.1.26.1 christos * mod_delayed_work(wq, dw, ticks)
973 1.1.26.1 christos *
974 1.1.26.1 christos * Schedule dw to run after ticks. If scheduled or queued,
975 1.1.26.1 christos * reschedule. If ticks == 0, run without delay.
976 1.1 skrll *
977 1.1 skrll * True if it modified the timer of an already scheduled work,
978 1.1 skrll * false if it newly scheduled the work.
979 1.1 skrll */
980 1.1 skrll bool
981 1.1 skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
982 1.1.26.1 christos unsigned long ticks)
983 1.1.26.1 christos {
984 1.1.26.1 christos bool timer_modified;
985 1.1.26.1 christos
986 1.1.26.1 christos mutex_enter(&wq->wq_lock);
987 1.1.26.1 christos if (acquire_work(&dw->work, wq)) {
988 1.1.26.1 christos /*
989 1.1 skrll * It wasn't on any workqueue at all. Schedule it to
990 1.1.26.1 christos * run on this one.
991 1.1.26.1 christos */
992 1.1.26.1 christos KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
993 1.1.26.1 christos if (ticks == 0) {
994 1.1.26.1 christos /*
995 1.1.26.1 christos * Run immediately: put it on the queue and
996 1.1.26.1 christos * signal the worker thread.
997 1.1.26.1 christos */
998 1.1 skrll TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
999 1.1.26.1 christos work_entry);
1000 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
1001 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue, &dw->work, wq);
1002 1.1.26.1 christos } else {
1003 1.1.26.1 christos /*
1004 1.1.26.1 christos * Initialize a callout and schedule to run
1005 1.1.26.1 christos * after a delay.
1006 1.1 skrll */
1007 1.1 skrll dw_callout_init(wq, dw);
1008 1.1.26.1 christos callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
1009 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule, dw, wq, ticks);
1010 1.1.26.1 christos }
1011 1.1.26.1 christos timer_modified = false;
1012 1.1.26.1 christos } else {
1013 1.1.26.1 christos /* It was already on this workqueue. */
1014 1.1.26.1 christos switch (dw->dw_state) {
1015 1.1.26.1 christos case DELAYED_WORK_IDLE:
1016 1.1.26.1 christos /* On the queue. */
1017 1.1.26.1 christos if (ticks == 0) {
1018 1.1.26.1 christos /* Leave it be. */
1019 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1020 1.1.26.1 christos &dw->work, wq);
1021 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1022 1.1.26.1 christos &dw->work, wq);
1023 1.1.26.1 christos } else {
1024 1.1.26.1 christos /* Remove from the queue and schedule. */
1025 1.1.26.1 christos TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
1026 1.1.26.1 christos work_entry);
1027 1.1.26.1 christos dw_callout_init(wq, dw);
1028 1.1.26.1 christos callout_schedule(&dw->dw_callout,
1029 1.1.26.1 christos MIN(INT_MAX, ticks));
1030 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1031 1.1.26.1 christos &dw->work, wq);
1032 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
1033 1.1.26.1 christos dw, wq, ticks);
1034 1.1.26.1 christos }
1035 1.1.26.1 christos timer_modified = true;
1036 1.1.26.1 christos break;
1037 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
1038 1.1.26.1 christos /*
1039 1.1.26.1 christos * It is scheduled to run after a delay. Try
1040 1.1.26.1 christos * to stop it and reschedule it; if we can't,
1041 1.1.26.1 christos * either reschedule it or cancel it to put it
1042 1.1.26.1 christos * on the queue, and inform the callout.
1043 1.1.26.1 christos */
1044 1.1.26.1 christos if (callout_stop(&dw->dw_callout)) {
1045 1.1.26.1 christos /* Can't stop, callout has begun. */
1046 1.1.26.1 christos if (ticks == 0) {
1047 1.1.26.1 christos /*
1048 1.1.26.1 christos * We don't actually need to do
1049 1.1.26.1 christos * anything. The callout will
1050 1.1.26.1 christos * queue it as soon as it gets
1051 1.1.26.1 christos * the lock.
1052 1.1.26.1 christos */
1053 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1054 1.1.26.1 christos &dw->work, wq);
1055 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1056 1.1.26.1 christos &dw->work, wq);
1057 1.1.26.1 christos } else {
1058 1.1.26.1 christos /* Ask the callout to reschedule. */
1059 1.1.26.1 christos dw->dw_state = DELAYED_WORK_RESCHEDULED;
1060 1.1.26.1 christos dw->dw_resched = MIN(INT_MAX, ticks);
1061 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1062 1.1.26.1 christos &dw->work, wq);
1063 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
1064 1.1.26.1 christos dw, wq, ticks);
1065 1.1.26.1 christos }
1066 1.1.26.1 christos } else {
1067 1.1.26.1 christos /* We stopped the callout before it began. */
1068 1.1.26.1 christos if (ticks == 0) {
1069 1.1.26.1 christos /*
1070 1.1.26.1 christos * Run immediately: destroy the
1071 1.1.26.1 christos * callout, put it on the
1072 1.1.26.1 christos * queue, and signal the worker
1073 1.1.26.1 christos * thread.
1074 1.1.26.1 christos */
1075 1.1.26.1 christos dw_callout_destroy(wq, dw);
1076 1.1.26.1 christos TAILQ_INSERT_TAIL(&wq->wq_dqueue,
1077 1.1.26.1 christos &dw->work, work_entry);
1078 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
1079 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1080 1.1.26.1 christos &dw->work, wq);
1081 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1082 1.1.26.1 christos &dw->work, wq);
1083 1.1.26.1 christos } else {
1084 1.1.26.1 christos /*
1085 1.1.26.1 christos * Reschedule the callout. No
1086 1.1.26.1 christos * state change.
1087 1.1.26.1 christos */
1088 1.1.26.1 christos callout_schedule(&dw->dw_callout,
1089 1.1.26.1 christos MIN(INT_MAX, ticks));
1090 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1091 1.1.26.1 christos &dw->work, wq);
1092 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
1093 1.1.26.1 christos dw, wq, ticks);
1094 1.1.26.1 christos }
1095 1.1.26.1 christos }
1096 1.1.26.1 christos timer_modified = true;
1097 1.1.26.1 christos break;
1098 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
1099 1.1.26.1 christos /*
1100 1.1.26.1 christos * Someone rescheduled it after the callout
1101 1.1.26.1 christos * started but before the poor thing even had a
1102 1.1.26.1 christos * chance to acquire the lock.
1103 1.1.26.1 christos */
1104 1.1.26.1 christos if (ticks == 0) {
1105 1.1.26.1 christos /*
1106 1.1.26.1 christos * We can just switch back to
1107 1.1.26.1 christos * DELAYED_WORK_SCHEDULED so that the
1108 1.1.26.1 christos * callout will queue the work as soon
1109 1.1.26.1 christos * as it gets the lock.
1110 1.1.26.1 christos */
1111 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
1112 1.1.26.1 christos dw->dw_resched = -1;
1113 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1114 1.1.26.1 christos &dw->work, wq);
1115 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1116 1.1.26.1 christos &dw->work, wq);
1117 1.1.26.1 christos } else {
1118 1.1.26.1 christos /* Change the rescheduled time. */
1119 1.1.26.1 christos dw->dw_resched = ticks;
1120 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1121 1.1.26.1 christos &dw->work, wq);
1122 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
1123 1.1.26.1 christos dw, wq, ticks);
1124 1.1.26.1 christos }
1125 1.1.26.1 christos timer_modified = true;
1126 1.1.26.1 christos break;
1127 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
1128 1.1.26.1 christos /*
1129 1.1.26.1 christos * Someone cancelled it after the callout
1130 1.1.26.1 christos * started but before the poor thing even had a
1131 1.1.26.1 christos * chance to acquire the lock.
1132 1.1.26.1 christos */
1133 1.1.26.1 christos if (ticks == 0) {
1134 1.1.26.1 christos /*
1135 1.1.26.1 christos * We can just switch back to
1136 1.1.26.1 christos * DELAYED_WORK_SCHEDULED so that the
1137 1.1.26.1 christos * callout will queue the work as soon
1138 1.1.26.1 christos * as it gets the lock.
1139 1.1.26.1 christos */
1140 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
1141 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1142 1.1.26.1 christos &dw->work, wq);
1143 1.1.26.1 christos } else {
1144 1.1.26.1 christos /* Ask it to reschedule. */
1145 1.1.26.1 christos dw->dw_state = DELAYED_WORK_RESCHEDULED;
1146 1.1.26.1 christos dw->dw_resched = MIN(INT_MAX, ticks);
1147 1.1.26.1 christos SDT_PROBE3(sdt, linux, work, schedule,
1148 1.1.26.1 christos dw, wq, ticks);
1149 1.1.26.1 christos }
1150 1.1.26.1 christos timer_modified = false;
1151 1.1 skrll break;
1152 1.1.26.1 christos default:
1153 1.1 skrll panic("invalid delayed work state: %d", dw->dw_state);
1154 1.1 skrll }
1155 1.1 skrll }
1156 1.1 skrll mutex_exit(&wq->wq_lock);
1157 1.1.26.1 christos
1158 1.1.26.1 christos return timer_modified;
1159 1.1.26.1 christos }
1160 1.1.26.1 christos
1161 1.1.26.1 christos /*
1162 1.1.26.1 christos * cancel_delayed_work(dw)
1163 1.1.26.1 christos *
1164 1.1.26.1 christos * If work was scheduled or queued, remove it from the schedule or
1165 1.1.26.1 christos * queue and return true. If work was not scheduled or queued,
1166 1.1.26.1 christos * return false. Note that work may already be running; if it
1167 1.1 skrll * hasn't been rescheduled or requeued, then cancel_delayed_work
1168 1.1 skrll * will return false, and either way, cancel_delayed_work will NOT
1169 1.1 skrll * wait for the work to complete.
1170 1.1.26.1 christos */
1171 1.1.26.1 christos bool
1172 1.1 skrll cancel_delayed_work(struct delayed_work *dw)
1173 1.1.26.1 christos {
1174 1.1.26.1 christos struct workqueue_struct *wq;
1175 1.1.26.1 christos bool cancelled_p;
1176 1.1 skrll
1177 1.1.26.1 christos /* If there's no workqueue, nothing to cancel. */
1178 1.1.26.1 christos if ((wq = work_queue(&dw->work)) == NULL)
1179 1.1.26.1 christos return false;
1180 1.1.26.1 christos
1181 1.1.26.1 christos mutex_enter(&wq->wq_lock);
1182 1.1.26.1 christos if (__predict_false(work_queue(&dw->work) != wq)) {
1183 1.1.26.1 christos cancelled_p = false;
1184 1.1.26.1 christos } else {
1185 1.1.26.1 christos switch (dw->dw_state) {
1186 1.1.26.1 christos case DELAYED_WORK_IDLE:
1187 1.1.26.1 christos /*
1188 1.1.26.1 christos * It is either on the queue or already running
1189 1.1.26.1 christos * or both.
1190 1.1.26.1 christos */
1191 1.1.26.1 christos if (work_claimed(&dw->work, wq)) {
1192 1.1.26.1 christos /* On the queue. Remove and release. */
1193 1.1.26.1 christos TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
1194 1.1.26.1 christos work_entry);
1195 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1196 1.1.26.1 christos &dw->work, wq);
1197 1.1.26.1 christos release_work(&dw->work, wq);
1198 1.1.26.1 christos /* Can't dereference dw after this point. */
1199 1.1.26.1 christos cancelled_p = true;
1200 1.1.26.1 christos } else {
1201 1.1.26.1 christos /* Not on the queue, so didn't cancel. */
1202 1.1.26.1 christos cancelled_p = false;
1203 1.1.26.1 christos }
1204 1.1.26.1 christos break;
1205 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
1206 1.1.26.1 christos /*
1207 1.1.26.1 christos * If it is scheduled, mark it cancelled and
1208 1.1.26.1 christos * try to stop the callout before it starts.
1209 1.1.26.1 christos *
1210 1.1.26.1 christos * If it's too late and the callout has already
1211 1.1.26.1 christos * begun to execute, tough.
1212 1.1.26.1 christos *
1213 1.1.26.1 christos * If we stopped the callout before it started,
1214 1.1.26.1 christos * however, then destroy the callout and
1215 1.1.26.1 christos * dissociate it from the workqueue ourselves.
1216 1.1.26.1 christos */
1217 1.1.26.1 christos dw->dw_state = DELAYED_WORK_CANCELLED;
1218 1.1.26.1 christos cancelled_p = true;
1219 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, &dw->work, wq);
1220 1.1.26.1 christos if (!callout_stop(&dw->dw_callout))
1221 1.1.26.1 christos cancel_delayed_work_done(wq, dw);
1222 1.1.26.1 christos break;
1223 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
1224 1.1.26.1 christos /*
1225 1.1.26.1 christos * If it is being rescheduled, the callout has
1226 1.1.26.1 christos * already fired. We must ask it to cancel.
1227 1.1.26.1 christos */
1228 1.1.26.1 christos dw->dw_state = DELAYED_WORK_CANCELLED;
1229 1.1.26.1 christos dw->dw_resched = -1;
1230 1.1.26.1 christos cancelled_p = true;
1231 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, &dw->work, wq);
1232 1.1.26.1 christos break;
1233 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
1234 1.1.26.1 christos /*
1235 1.1.26.1 christos * If it is being cancelled, the callout has
1236 1.1.26.1 christos * already fired. There is nothing more for us
1237 1.1.26.1 christos * to do. Someone else claims credit for
1238 1.1.26.1 christos * cancelling it.
1239 1.1.26.1 christos */
1240 1.1.26.1 christos cancelled_p = false;
1241 1.1.26.1 christos break;
1242 1.1 skrll default:
1243 1.1.26.1 christos panic("invalid delayed work state: %d",
1244 1.1 skrll dw->dw_state);
1245 1.1 skrll }
1246 1.1 skrll }
1247 1.1 skrll mutex_exit(&wq->wq_lock);
1248 1.1.26.1 christos
1249 1.1.26.1 christos return cancelled_p;
1250 1.1.26.1 christos }
1251 1.1.26.1 christos
1252 1.1.26.1 christos /*
1253 1.1.26.1 christos * cancel_delayed_work_sync(dw)
1254 1.1.26.1 christos *
1255 1.1.26.1 christos * If work was scheduled or queued, remove it from the schedule or
1256 1.1.26.1 christos * queue and return true. If work was not scheduled or queued,
1257 1.1 skrll * return false. Note that work may already be running; if it
1258 1.1 skrll * hasn't been rescheduled or requeued, then cancel_delayed_work
1259 1.1 skrll * will return false; either way, wait for it to complete.
1260 1.1.26.1 christos */
1261 1.1.26.1 christos bool
1262 1.1 skrll cancel_delayed_work_sync(struct delayed_work *dw)
1263 1.1.26.1 christos {
1264 1.1.26.1 christos struct workqueue_struct *wq;
1265 1.1.26.1 christos bool cancelled_p;
1266 1.1 skrll
1267 1.1.26.1 christos /* If there's no workqueue, nothing to cancel. */
1268 1.1.26.1 christos if ((wq = work_queue(&dw->work)) == NULL)
1269 1.1.26.1 christos return false;
1270 1.1.26.1 christos
1271 1.1.26.1 christos mutex_enter(&wq->wq_lock);
1272 1.1.26.1 christos if (__predict_false(work_queue(&dw->work) != wq)) {
1273 1.1.26.1 christos cancelled_p = false;
1274 1.1.26.1 christos } else {
1275 1.1.26.1 christos switch (dw->dw_state) {
1276 1.1.26.1 christos case DELAYED_WORK_IDLE:
1277 1.1.26.1 christos /*
1278 1.1.26.1 christos * It is either on the queue or already running
1279 1.1.26.1 christos * or both.
1280 1.1.26.1 christos */
1281 1.1.26.1 christos if (work_claimed(&dw->work, wq)) {
1282 1.1.26.1 christos /* On the queue. Remove and release. */
1283 1.1.26.1 christos TAILQ_REMOVE(&wq->wq_dqueue, &dw->work,
1284 1.1.26.1 christos work_entry);
1285 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel,
1286 1.1.26.1 christos &dw->work, wq);
1287 1.1.26.1 christos release_work(&dw->work, wq);
1288 1.1.26.1 christos /* Can't dereference dw after this point. */
1289 1.1.26.1 christos cancelled_p = true;
1290 1.1.26.1 christos } else {
1291 1.1.26.1 christos /* Not on the queue, so didn't cancel. */
1292 1.1.26.1 christos cancelled_p = false;
1293 1.1.26.1 christos }
1294 1.1.26.1 christos /* If it's still running, wait for it to complete. */
1295 1.1.26.1 christos if (wq->wq_current_work == &dw->work)
1296 1.1.26.1 christos wait_for_current_work(&dw->work, wq);
1297 1.1.26.1 christos break;
1298 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
1299 1.1.26.1 christos /*
1300 1.1.26.1 christos * If it is scheduled, mark it cancelled and
1301 1.1.26.1 christos * try to stop the callout before it starts.
1302 1.1.26.1 christos *
1303 1.1.26.1 christos * If it's too late and the callout has already
1304 1.1.26.1 christos * begun to execute, we must wait for it to
1305 1.1.26.1 christos * complete. But we got in soon enough to ask
1306 1.1.26.1 christos * the callout not to run, so we successfully
1307 1.1.26.1 christos * cancelled it in that case.
1308 1.1.26.1 christos *
1309 1.1.26.1 christos * If we stopped the callout before it started,
1310 1.1.26.1 christos * then we must destroy the callout and
1311 1.1.26.1 christos * dissociate it from the workqueue ourselves.
1312 1.1.26.1 christos */
1313 1.1.26.1 christos dw->dw_state = DELAYED_WORK_CANCELLED;
1314 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, &dw->work, wq);
1315 1.1.26.1 christos if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
1316 1.1.26.1 christos cancel_delayed_work_done(wq, dw);
1317 1.1.26.1 christos cancelled_p = true;
1318 1.1.26.1 christos break;
1319 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
1320 1.1.26.1 christos /*
1321 1.1.26.1 christos * If it is being rescheduled, the callout has
1322 1.1.26.1 christos * already fired. We must ask it to cancel and
1323 1.1.26.1 christos * wait for it to complete.
1324 1.1.26.1 christos */
1325 1.1.26.1 christos dw->dw_state = DELAYED_WORK_CANCELLED;
1326 1.1.26.1 christos dw->dw_resched = -1;
1327 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, cancel, &dw->work, wq);
1328 1.1.26.1 christos (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1329 1.1.26.1 christos cancelled_p = true;
1330 1.1.26.1 christos break;
1331 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
1332 1.1.26.1 christos /*
1333 1.1.26.1 christos * If it is being cancelled, the callout has
1334 1.1.26.1 christos * already fired. We need only wait for it to
1335 1.1.26.1 christos * complete. Someone else, however, claims
1336 1.1.26.1 christos * credit for cancelling it.
1337 1.1.26.1 christos */
1338 1.1.26.1 christos (void)callout_halt(&dw->dw_callout, &wq->wq_lock);
1339 1.1.26.1 christos cancelled_p = false;
1340 1.1.26.1 christos break;
1341 1.1 skrll default:
1342 1.1.26.1 christos panic("invalid delayed work state: %d",
1343 1.1 skrll dw->dw_state);
1344 1.1 skrll }
1345 1.1 skrll }
1346 1.1.26.1 christos mutex_exit(&wq->wq_lock);
1347 1.1.26.1 christos
1348 1.1.26.1 christos return cancelled_p;
1349 1.1.26.1 christos }
1350 1.1.26.1 christos
1351 1.1.26.1 christos /*
1353 1.1.26.1 christos * Flush
1354 1.1.26.1 christos */
1355 1.1.26.1 christos
1356 1.1.26.1 christos /*
1357 1.1.26.1 christos * flush_scheduled_work()
1358 1.1.26.1 christos *
1359 1.1.26.1 christos * Wait for all work queued on system_wq to complete. This does
1360 1.1.26.1 christos * not include delayed work.
1361 1.1.26.1 christos */
1362 1.1.26.1 christos void
1363 1.1 skrll flush_scheduled_work(void)
1364 1.1.26.1 christos {
1365 1.1.26.1 christos
1366 1.1.26.1 christos flush_workqueue(system_wq);
1367 1.1.26.1 christos }
1368 1.1.26.1 christos
1369 1.1.26.1 christos /*
1370 1.1.26.1 christos * flush_workqueue_locked(wq)
1371 1.1.26.1 christos *
1372 1.1 skrll * Wait for all work queued on wq to complete. This does not
1373 1.1.26.1 christos * include delayed work.
1374 1.1 skrll *
1375 1.1.26.1 christos * Caller must hold wq's lock.
1376 1.1 skrll */
1377 1.1.26.1 christos static void
1378 1.1 skrll flush_workqueue_locked(struct workqueue_struct *wq)
1379 1.1.26.1 christos {
1380 1.1.26.1 christos uint64_t gen;
1381 1.1 skrll
1382 1.1 skrll KASSERT(mutex_owned(&wq->wq_lock));
1383 1.1.26.1 christos
1384 1.1.26.1 christos /* Get the current generation number. */
1385 1.1 skrll gen = wq->wq_gen;
1386 1.1.26.1 christos
1387 1.1.26.1 christos /*
1388 1.1 skrll * If there's a batch of work in progress, we must wait for the
1389 1.1.26.1 christos * worker thread to finish that batch.
1390 1.1.26.1 christos */
1391 1.1.26.1 christos if (wq->wq_current_work != NULL)
1392 1.1.26.1 christos gen++;
1393 1.1.26.1 christos
1394 1.1.26.1 christos /*
1395 1.1.26.1 christos * If there's any work yet to be claimed from the queue by the
1396 1.1 skrll * worker thread, we must wait for it to finish one more batch
1397 1.1.26.1 christos * too.
1398 1.1.26.1 christos */
1399 1.1.26.1 christos if (!TAILQ_EMPTY(&wq->wq_queue) || !TAILQ_EMPTY(&wq->wq_dqueue))
1400 1.1.26.1 christos gen++;
1401 1.1.26.1 christos
1402 1.1 skrll /* Wait until the generation number has caught up. */
1403 1.1 skrll SDT_PROBE1(sdt, linux, work, flush__start, wq);
1404 1.1.26.1 christos while (wq->wq_gen < gen)
1405 1.1.26.1 christos cv_wait(&wq->wq_cv, &wq->wq_lock);
1406 1.1.26.1 christos SDT_PROBE1(sdt, linux, work, flush__done, wq);
1407 1.1.26.1 christos }
1408 1.1.26.1 christos
1409 1.1.26.1 christos /*
1410 1.1.26.1 christos * flush_workqueue(wq)
1411 1.1.26.1 christos *
1412 1.1.26.1 christos * Wait for all work queued on wq to complete. This does not
1413 1.1.26.1 christos * include delayed work.
1414 1.1.26.1 christos */
1415 1.1.26.1 christos void
1416 1.1.26.1 christos flush_workqueue(struct workqueue_struct *wq)
1417 1.1.26.1 christos {
1418 1.1.26.1 christos
1419 1.1.26.1 christos mutex_enter(&wq->wq_lock);
1420 1.1.26.1 christos flush_workqueue_locked(wq);
1421 1.1.26.1 christos mutex_exit(&wq->wq_lock);
1422 1.1.26.1 christos }
1423 1.1.26.1 christos
1424 1.1.26.1 christos /*
1425 1.1.26.1 christos * flush_work(work)
1426 1.1.26.1 christos *
1427 1.1 skrll * If work is queued or currently executing, wait for it to
1428 1.1 skrll * complete.
1429 1.1 skrll */
1430 1.1.26.1 christos void
1431 1.1.26.1 christos flush_work(struct work_struct *work)
1432 1.1.26.1 christos {
1433 1.1 skrll struct workqueue_struct *wq;
1434 1.1.26.1 christos
1435 1.1 skrll /* If there's no workqueue, nothing to flush. */
1436 1.1 skrll if ((wq = work_queue(work)) == NULL)
1437 1.1.26.1 christos return;
1438 1.1.26.1 christos
1439 1.1.26.1 christos flush_workqueue(wq);
1440 1.1.26.1 christos }
1441 1.1.26.1 christos
1442 1.1.26.1 christos /*
1443 1.1.26.1 christos * flush_delayed_work(dw)
1444 1.1.26.1 christos *
1445 1.1.26.1 christos * If dw is scheduled to run after a delay, queue it immediately
1446 1.1 skrll * instead. Then, if dw is queued or currently executing, wait
1447 1.1 skrll * for it to complete.
1448 1.1 skrll */
1449 1.1.26.1 christos void
1450 1.1.26.1 christos flush_delayed_work(struct delayed_work *dw)
1451 1.1.26.1 christos {
1452 1.1 skrll struct workqueue_struct *wq;
1453 1.1 skrll
1454 1.1.26.1 christos /* If there's no workqueue, nothing to flush. */
1455 1.1.26.1 christos if ((wq = work_queue(&dw->work)) == NULL)
1456 1.1.26.1 christos return;
1457 1.1.26.1 christos
1458 1.1.26.1 christos mutex_enter(&wq->wq_lock);
1459 1.1.26.1 christos if (__predict_false(work_queue(&dw->work) != wq)) {
1460 1.1 skrll /*
1461 1.1.26.1 christos * Moved off the queue already (and possibly to another
1462 1.1.26.1 christos * queue, though that would be ill-advised), so it must
1463 1.1.26.1 christos * have completed, and we have nothing more to do.
1464 1.1.26.1 christos */
1465 1.1.26.1 christos } else {
1466 1.1.26.1 christos switch (dw->dw_state) {
1467 1.1.26.1 christos case DELAYED_WORK_IDLE:
1468 1.1.26.1 christos /*
1469 1.1.26.1 christos * It has a workqueue assigned and the callout
1470 1.1.26.1 christos * is idle, so it must be in progress or on the
1471 1.1.26.1 christos * queue. In that case, we'll wait for it to
1472 1.1.26.1 christos * complete.
1473 1.1.26.1 christos */
1474 1.1.26.1 christos break;
1475 1.1.26.1 christos case DELAYED_WORK_SCHEDULED:
1476 1.1.26.1 christos case DELAYED_WORK_RESCHEDULED:
1477 1.1.26.1 christos case DELAYED_WORK_CANCELLED:
1478 1.1.26.1 christos /*
1479 1.1.26.1 christos * The callout is scheduled, and may have even
1480 1.1.26.1 christos * started. Mark it as scheduled so that if
1481 1.1.26.1 christos * the callout has fired it will queue the work
1482 1.1.26.1 christos * itself. Try to stop the callout -- if we
1483 1.1.26.1 christos * can, queue the work now; if we can't, wait
1484 1.1.26.1 christos * for the callout to complete, which entails
1485 1.1.26.1 christos * queueing it.
1486 1.1.26.1 christos */
1487 1.1.26.1 christos dw->dw_state = DELAYED_WORK_SCHEDULED;
1488 1.1.26.1 christos if (!callout_halt(&dw->dw_callout, &wq->wq_lock)) {
1489 1.1.26.1 christos /*
1490 1.1.26.1 christos * We stopped it before it ran. No
1491 1.1.26.1 christos * state change in the interim is
1492 1.1.26.1 christos * possible. Destroy the callout and
1493 1.1.26.1 christos * queue it ourselves.
1494 1.1.26.1 christos */
1495 1.1.26.1 christos KASSERT(dw->dw_state ==
1496 1.1.26.1 christos DELAYED_WORK_SCHEDULED);
1497 1.1.26.1 christos dw_callout_destroy(wq, dw);
1498 1.1.26.1 christos TAILQ_INSERT_TAIL(&wq->wq_dqueue, &dw->work,
1499 1.1.26.1 christos work_entry);
1500 1.1.26.1 christos cv_broadcast(&wq->wq_cv);
1501 1.1.26.1 christos SDT_PROBE2(sdt, linux, work, queue,
1502 1.1.26.1 christos &dw->work, wq);
1503 1.1.26.1 christos }
1504 1.1.26.1 christos break;
1505 1.1.26.1 christos default:
1506 1.1.26.1 christos panic("invalid delayed work state: %d", dw->dw_state);
1507 1.1.26.1 christos }
1508 1.1 skrll /*
1509 1.1 skrll * Waiting for the whole queue to flush is overkill,
1510 1.1 skrll * but doesn't hurt.
1511 */
1512 flush_workqueue_locked(wq);
1513 }
1514 mutex_exit(&wq->wq_lock);
1515 }
1516