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