linux_work.c revision 1.26 1 1.2 riastrad /* $NetBSD: linux_work.c,v 1.26 2018/08/27 15:02:52 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.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_work.c,v 1.26 2018/08/27 15:02:52 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.1 skrll #include <sys/queue.h>
45 1.1 skrll
46 1.1 skrll #include <linux/workqueue.h>
47 1.1 skrll
48 1.1 skrll struct workqueue_struct {
49 1.1 skrll kmutex_t wq_lock;
50 1.1 skrll kcondvar_t wq_cv;
51 1.1 skrll TAILQ_HEAD(, delayed_work) wq_delayed;
52 1.12 riastrad TAILQ_HEAD(, work_struct) wq_queue;
53 1.1 skrll struct work_struct *wq_current_work;
54 1.12 riastrad int wq_flags;
55 1.12 riastrad struct lwp *wq_lwp;
56 1.12 riastrad uint64_t wq_gen;
57 1.12 riastrad bool wq_requeued:1;
58 1.12 riastrad bool wq_dying:1;
59 1.1 skrll };
60 1.1 skrll
61 1.12 riastrad static void __dead linux_workqueue_thread(void *);
62 1.12 riastrad static void linux_workqueue_timeout(void *);
63 1.17 riastrad static struct workqueue_struct *
64 1.17 riastrad acquire_work(struct work_struct *,
65 1.17 riastrad struct workqueue_struct *);
66 1.17 riastrad static void release_work(struct work_struct *,
67 1.17 riastrad struct workqueue_struct *);
68 1.12 riastrad static void queue_delayed_work_anew(struct workqueue_struct *,
69 1.12 riastrad struct delayed_work *, unsigned long);
70 1.23 riastrad static void cancel_delayed_work_done(struct workqueue_struct *,
71 1.23 riastrad struct delayed_work *);
72 1.12 riastrad
73 1.12 riastrad static specificdata_key_t workqueue_key __read_mostly;
74 1.12 riastrad
75 1.12 riastrad struct workqueue_struct *system_wq __read_mostly;
76 1.12 riastrad struct workqueue_struct *system_long_wq __read_mostly;
77 1.12 riastrad struct workqueue_struct *system_power_efficient_wq __read_mostly;
78 1.3 riastrad
79 1.1 skrll int
80 1.1 skrll linux_workqueue_init(void)
81 1.1 skrll {
82 1.12 riastrad int error;
83 1.3 riastrad
84 1.12 riastrad error = lwp_specific_key_create(&workqueue_key, NULL);
85 1.12 riastrad if (error)
86 1.12 riastrad goto fail0;
87 1.1 skrll
88 1.1 skrll system_wq = alloc_ordered_workqueue("lnxsyswq", 0);
89 1.12 riastrad if (system_wq == NULL) {
90 1.12 riastrad error = ENOMEM;
91 1.12 riastrad goto fail1;
92 1.12 riastrad }
93 1.2 riastrad
94 1.2 riastrad system_long_wq = alloc_ordered_workqueue("lnxlngwq", 0);
95 1.12 riastrad if (system_long_wq == NULL) {
96 1.12 riastrad error = ENOMEM;
97 1.12 riastrad goto fail2;
98 1.12 riastrad }
99 1.1 skrll
100 1.6 riastrad system_power_efficient_wq = alloc_ordered_workqueue("lnxpwrwq", 0);
101 1.12 riastrad if (system_long_wq == NULL) {
102 1.12 riastrad error = ENOMEM;
103 1.12 riastrad goto fail3;
104 1.12 riastrad }
105 1.6 riastrad
106 1.1 skrll return 0;
107 1.2 riastrad
108 1.12 riastrad fail4: __unused
109 1.6 riastrad destroy_workqueue(system_power_efficient_wq);
110 1.12 riastrad fail3: destroy_workqueue(system_long_wq);
111 1.12 riastrad fail2: destroy_workqueue(system_wq);
112 1.12 riastrad fail1: lwp_specific_key_delete(workqueue_key);
113 1.12 riastrad fail0: KASSERT(error);
114 1.12 riastrad return error;
115 1.1 skrll }
116 1.1 skrll
117 1.1 skrll void
118 1.1 skrll linux_workqueue_fini(void)
119 1.1 skrll {
120 1.2 riastrad
121 1.12 riastrad destroy_workqueue(system_power_efficient_wq);
122 1.2 riastrad destroy_workqueue(system_long_wq);
123 1.1 skrll destroy_workqueue(system_wq);
124 1.12 riastrad lwp_specific_key_delete(workqueue_key);
125 1.1 skrll }
126 1.1 skrll
127 1.1 skrll /*
129 1.1 skrll * Workqueues
130 1.1 skrll */
131 1.1 skrll
132 1.12 riastrad struct workqueue_struct *
133 1.1 skrll alloc_ordered_workqueue(const char *name, int flags)
134 1.1 skrll {
135 1.1 skrll struct workqueue_struct *wq;
136 1.1 skrll int error;
137 1.12 riastrad
138 1.1 skrll KASSERT(flags == 0);
139 1.25 riastrad
140 1.1 skrll wq = kmem_zalloc(sizeof(*wq), KM_SLEEP);
141 1.12 riastrad
142 1.1 skrll mutex_init(&wq->wq_lock, MUTEX_DEFAULT, IPL_NONE);
143 1.1 skrll cv_init(&wq->wq_cv, name);
144 1.12 riastrad TAILQ_INIT(&wq->wq_delayed);
145 1.1 skrll TAILQ_INIT(&wq->wq_queue);
146 1.25 riastrad wq->wq_current_work = NULL;
147 1.25 riastrad wq->wq_flags = 0;
148 1.25 riastrad wq->wq_lwp = NULL;
149 1.25 riastrad wq->wq_gen = 0;
150 1.25 riastrad wq->wq_requeued = false;
151 1.1 skrll wq->wq_dying = false;
152 1.12 riastrad
153 1.12 riastrad error = kthread_create(PRI_NONE,
154 1.12 riastrad KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL,
155 1.12 riastrad &linux_workqueue_thread, wq, &wq->wq_lwp, "%s", name);
156 1.12 riastrad if (error)
157 1.3 riastrad goto fail0;
158 1.1 skrll
159 1.12 riastrad return wq;
160 1.12 riastrad
161 1.12 riastrad fail0: KASSERT(TAILQ_EMPTY(&wq->wq_queue));
162 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
163 1.12 riastrad cv_destroy(&wq->wq_cv);
164 1.12 riastrad mutex_destroy(&wq->wq_lock);
165 1.12 riastrad kmem_free(wq, sizeof(*wq));
166 1.1 skrll return NULL;
167 1.1 skrll }
168 1.1 skrll
169 1.1 skrll void
170 1.1 skrll destroy_workqueue(struct workqueue_struct *wq)
171 1.1 skrll {
172 1.1 skrll
173 1.12 riastrad /*
174 1.12 riastrad * Cancel all delayed work. We do this first because any
175 1.12 riastrad * delayed work that that has already timed out, which we can't
176 1.1 skrll * cancel, may have queued new work.
177 1.26 riastrad */
178 1.26 riastrad mutex_enter(&wq->wq_lock);
179 1.26 riastrad while (!TAILQ_EMPTY(&wq->wq_delayed)) {
180 1.1 skrll struct delayed_work *const dw = TAILQ_FIRST(&wq->wq_delayed);
181 1.26 riastrad
182 1.26 riastrad KASSERT(dw->work.work_queue == wq);
183 1.26 riastrad KASSERTMSG((dw->dw_state == DELAYED_WORK_SCHEDULED ||
184 1.26 riastrad dw->dw_state == DELAYED_WORK_RESCHEDULED ||
185 1.26 riastrad dw->dw_state == DELAYED_WORK_CANCELLED),
186 1.26 riastrad "delayed work %p in bad state: %d",
187 1.26 riastrad dw, dw->dw_state);
188 1.26 riastrad
189 1.26 riastrad /*
190 1.26 riastrad * Mark it cancelled and try to stop the callout before
191 1.26 riastrad * it starts.
192 1.26 riastrad *
193 1.26 riastrad * If it's too late and the callout has already begun
194 1.26 riastrad * to execute, then it will notice that we asked to
195 1.26 riastrad * cancel it and remove itself from the queue before
196 1.26 riastrad * returning.
197 1.26 riastrad *
198 1.26 riastrad * If we stopped the callout before it started,
199 1.26 riastrad * however, then we can safely destroy the callout and
200 1.26 riastrad * dissociate it from the workqueue ourselves.
201 1.26 riastrad */
202 1.26 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
203 1.26 riastrad if (!callout_halt(&dw->dw_callout, &wq->wq_lock))
204 1.26 riastrad cancel_delayed_work_done(wq, dw);
205 1.26 riastrad }
206 1.1 skrll mutex_exit(&wq->wq_lock);
207 1.26 riastrad
208 1.26 riastrad /*
209 1.26 riastrad * At this point, no new work can be put on the queue.
210 1.1 skrll */
211 1.12 riastrad
212 1.12 riastrad /* Tell the thread to exit. */
213 1.12 riastrad mutex_enter(&wq->wq_lock);
214 1.12 riastrad wq->wq_dying = true;
215 1.12 riastrad cv_broadcast(&wq->wq_cv);
216 1.12 riastrad mutex_exit(&wq->wq_lock);
217 1.12 riastrad
218 1.12 riastrad /* Wait for it to exit. */
219 1.12 riastrad (void)kthread_join(wq->wq_lwp);
220 1.25 riastrad
221 1.25 riastrad KASSERT(wq->wq_dying);
222 1.25 riastrad KASSERT(!wq->wq_requeued);
223 1.1 skrll KASSERT(wq->wq_flags == 0);
224 1.12 riastrad KASSERT(wq->wq_current_work == NULL);
225 1.12 riastrad KASSERT(TAILQ_EMPTY(&wq->wq_queue));
226 1.1 skrll KASSERT(TAILQ_EMPTY(&wq->wq_delayed));
227 1.1 skrll cv_destroy(&wq->wq_cv);
228 1.1 skrll mutex_destroy(&wq->wq_lock);
229 1.1 skrll
230 1.1 skrll kmem_free(wq, sizeof(*wq));
231 1.1 skrll }
232 1.1 skrll
233 1.12 riastrad /*
235 1.1 skrll * Work thread and callout
236 1.12 riastrad */
237 1.12 riastrad
238 1.1 skrll static void __dead
239 1.12 riastrad linux_workqueue_thread(void *cookie)
240 1.12 riastrad {
241 1.1 skrll struct workqueue_struct *const wq = cookie;
242 1.12 riastrad TAILQ_HEAD(, work_struct) tmp;
243 1.1 skrll
244 1.12 riastrad lwp_setspecific(workqueue_key, wq);
245 1.12 riastrad
246 1.26 riastrad mutex_enter(&wq->wq_lock);
247 1.26 riastrad for (;;) {
248 1.26 riastrad /*
249 1.26 riastrad * Wait until there's activity. If there's no work and
250 1.12 riastrad * we're dying, stop here.
251 1.12 riastrad */
252 1.26 riastrad while (TAILQ_EMPTY(&wq->wq_queue) && !wq->wq_dying)
253 1.26 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
254 1.12 riastrad if (TAILQ_EMPTY(&wq->wq_queue)) {
255 1.26 riastrad KASSERT(wq->wq_dying);
256 1.1 skrll break;
257 1.12 riastrad }
258 1.12 riastrad
259 1.12 riastrad /* Grab a batch of work off the queue. */
260 1.12 riastrad KASSERT(!TAILQ_EMPTY(&wq->wq_queue));
261 1.12 riastrad TAILQ_INIT(&tmp);
262 1.12 riastrad TAILQ_CONCAT(&tmp, &wq->wq_queue, work_entry);
263 1.12 riastrad
264 1.12 riastrad /* Process each work item in the batch. */
265 1.12 riastrad while (!TAILQ_EMPTY(&tmp)) {
266 1.18 riastrad struct work_struct *const work = TAILQ_FIRST(&tmp);
267 1.12 riastrad
268 1.12 riastrad KASSERT(work->work_queue == wq);
269 1.12 riastrad TAILQ_REMOVE(&tmp, work, work_entry);
270 1.1 skrll KASSERT(wq->wq_current_work == NULL);
271 1.12 riastrad wq->wq_current_work = work;
272 1.12 riastrad
273 1.12 riastrad mutex_exit(&wq->wq_lock);
274 1.1 skrll (*work->func)(work);
275 1.12 riastrad mutex_enter(&wq->wq_lock);
276 1.12 riastrad
277 1.12 riastrad KASSERT(wq->wq_current_work == work);
278 1.12 riastrad KASSERT(work->work_queue == wq);
279 1.12 riastrad if (wq->wq_requeued)
280 1.17 riastrad wq->wq_requeued = false;
281 1.12 riastrad else
282 1.12 riastrad release_work(work, wq);
283 1.12 riastrad wq->wq_current_work = NULL;
284 1.1 skrll cv_broadcast(&wq->wq_cv);
285 1.12 riastrad }
286 1.12 riastrad
287 1.12 riastrad /* Notify flush that we've completed a batch of work. */
288 1.1 skrll wq->wq_gen++;
289 1.12 riastrad cv_broadcast(&wq->wq_cv);
290 1.1 skrll }
291 1.12 riastrad mutex_exit(&wq->wq_lock);
292 1.1 skrll
293 1.1 skrll kthread_exit(0);
294 1.1 skrll }
295 1.12 riastrad
296 1.1 skrll static void
297 1.12 riastrad linux_workqueue_timeout(void *cookie)
298 1.12 riastrad {
299 1.1 skrll struct delayed_work *const dw = cookie;
300 1.12 riastrad struct workqueue_struct *const wq = dw->work.work_queue;
301 1.14 riastrad
302 1.12 riastrad KASSERT(wq != NULL);
303 1.18 riastrad
304 1.12 riastrad mutex_enter(&wq->wq_lock);
305 1.12 riastrad KASSERT(dw->work.work_queue == wq);
306 1.12 riastrad switch (dw->dw_state) {
307 1.12 riastrad case DELAYED_WORK_IDLE:
308 1.12 riastrad panic("delayed work callout uninitialized: %p", dw);
309 1.12 riastrad case DELAYED_WORK_SCHEDULED:
310 1.12 riastrad dw->dw_state = DELAYED_WORK_IDLE;
311 1.12 riastrad callout_destroy(&dw->dw_callout);
312 1.12 riastrad TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
313 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
314 1.12 riastrad cv_broadcast(&wq->wq_cv);
315 1.12 riastrad break;
316 1.12 riastrad case DELAYED_WORK_RESCHEDULED:
317 1.12 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
318 1.23 riastrad break;
319 1.22 riastrad case DELAYED_WORK_CANCELLED:
320 1.22 riastrad cancel_delayed_work_done(wq, dw);
321 1.12 riastrad /* Can't touch dw any more. */
322 1.12 riastrad goto out;
323 1.12 riastrad default:
324 1.15 riastrad panic("delayed work callout in bad state: %p", dw);
325 1.15 riastrad }
326 1.22 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE ||
327 1.1 skrll dw->dw_state == DELAYED_WORK_SCHEDULED);
328 1.1 skrll out: mutex_exit(&wq->wq_lock);
329 1.12 riastrad }
330 1.12 riastrad
331 1.1 skrll struct work_struct *
332 1.12 riastrad current_work(void)
333 1.1 skrll {
334 1.12 riastrad struct workqueue_struct *wq = lwp_getspecific(workqueue_key);
335 1.12 riastrad
336 1.12 riastrad /* If we're not a workqueue thread, then there's no work. */
337 1.1 skrll if (wq == NULL)
338 1.12 riastrad return NULL;
339 1.12 riastrad
340 1.12 riastrad /*
341 1.12 riastrad * Otherwise, this should be possible only while work is in
342 1.12 riastrad * progress. Return the current work item.
343 1.12 riastrad */
344 1.1 skrll KASSERT(wq->wq_current_work != NULL);
345 1.1 skrll return wq->wq_current_work;
346 1.1 skrll }
347 1.1 skrll
348 1.1 skrll /*
350 1.1 skrll * Work
351 1.1 skrll */
352 1.1 skrll
353 1.1 skrll void
354 1.12 riastrad INIT_WORK(struct work_struct *work, void (*fn)(struct work_struct *))
355 1.4 riastrad {
356 1.1 skrll
357 1.1 skrll work->work_queue = NULL;
358 1.17 riastrad work->func = fn;
359 1.17 riastrad }
360 1.17 riastrad
361 1.17 riastrad static struct workqueue_struct *
362 1.17 riastrad acquire_work(struct work_struct *work, struct workqueue_struct *wq)
363 1.17 riastrad {
364 1.17 riastrad struct workqueue_struct *wq0;
365 1.17 riastrad
366 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
367 1.17 riastrad
368 1.17 riastrad wq0 = atomic_cas_ptr(&work->work_queue, NULL, wq);
369 1.17 riastrad if (wq0 == NULL) {
370 1.17 riastrad membar_enter();
371 1.17 riastrad KASSERT(work->work_queue == wq);
372 1.17 riastrad }
373 1.17 riastrad
374 1.17 riastrad return wq0;
375 1.17 riastrad }
376 1.17 riastrad
377 1.17 riastrad static void
378 1.17 riastrad release_work(struct work_struct *work, struct workqueue_struct *wq)
379 1.17 riastrad {
380 1.17 riastrad
381 1.17 riastrad KASSERT(work->work_queue == wq);
382 1.17 riastrad KASSERT(mutex_owned(&wq->wq_lock));
383 1.17 riastrad
384 1.17 riastrad membar_exit();
385 1.1 skrll work->work_queue = NULL;
386 1.1 skrll }
387 1.1 skrll
388 1.12 riastrad bool
389 1.1 skrll schedule_work(struct work_struct *work)
390 1.1 skrll {
391 1.1 skrll
392 1.1 skrll return queue_work(system_wq, work);
393 1.1 skrll }
394 1.1 skrll
395 1.12 riastrad bool
396 1.1 skrll queue_work(struct workqueue_struct *wq, struct work_struct *work)
397 1.1 skrll {
398 1.1 skrll struct workqueue_struct *wq0;
399 1.1 skrll bool newly_queued;
400 1.12 riastrad
401 1.17 riastrad KASSERT(wq != NULL);
402 1.12 riastrad
403 1.1 skrll mutex_enter(&wq->wq_lock);
404 1.12 riastrad if (__predict_true((wq0 = acquire_work(work, wq)) == NULL)) {
405 1.12 riastrad TAILQ_INSERT_TAIL(&wq->wq_queue, work, work_entry);
406 1.1 skrll newly_queued = true;
407 1.1 skrll } else {
408 1.12 riastrad KASSERT(wq0 == wq);
409 1.1 skrll newly_queued = false;
410 1.1 skrll }
411 1.1 skrll mutex_exit(&wq->wq_lock);
412 1.1 skrll
413 1.1 skrll return newly_queued;
414 1.12 riastrad }
415 1.1 skrll
416 1.12 riastrad bool
417 1.1 skrll cancel_work(struct work_struct *work)
418 1.1 skrll {
419 1.13 riastrad struct workqueue_struct *wq;
420 1.13 riastrad bool cancelled_p = false;
421 1.13 riastrad
422 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
423 1.12 riastrad if ((wq = work->work_queue) == NULL)
424 1.12 riastrad goto out;
425 1.12 riastrad
426 1.12 riastrad mutex_enter(&wq->wq_lock);
427 1.12 riastrad if (__predict_false(work->work_queue != wq)) {
428 1.12 riastrad cancelled_p = false;
429 1.12 riastrad } else if (wq->wq_current_work == work) {
430 1.1 skrll cancelled_p = false;
431 1.1 skrll } else {
432 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
433 1.1 skrll cancelled_p = true;
434 1.13 riastrad }
435 1.1 skrll mutex_exit(&wq->wq_lock);
436 1.1 skrll
437 1.12 riastrad out: return cancelled_p;
438 1.12 riastrad }
439 1.1 skrll
440 1.1 skrll bool
441 1.12 riastrad cancel_work_sync(struct work_struct *work)
442 1.1 skrll {
443 1.13 riastrad struct workqueue_struct *wq;
444 1.13 riastrad bool cancelled_p = false;
445 1.13 riastrad
446 1.13 riastrad /* If there's no workqueue, nothing to cancel. */
447 1.1 skrll if ((wq = work->work_queue) == NULL)
448 1.12 riastrad goto out;
449 1.12 riastrad
450 1.12 riastrad mutex_enter(&wq->wq_lock);
451 1.12 riastrad if (__predict_false(work->work_queue != wq)) {
452 1.12 riastrad cancelled_p = false;
453 1.12 riastrad } else if (wq->wq_current_work == work) {
454 1.12 riastrad do {
455 1.12 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
456 1.12 riastrad } while (wq->wq_current_work == work);
457 1.12 riastrad cancelled_p = false;
458 1.12 riastrad } else {
459 1.1 skrll TAILQ_REMOVE(&wq->wq_queue, work, work_entry);
460 1.1 skrll cancelled_p = true;
461 1.13 riastrad }
462 1.1 skrll mutex_exit(&wq->wq_lock);
463 1.1 skrll
464 1.1 skrll out: return cancelled_p;
465 1.1 skrll }
466 1.1 skrll
467 1.1 skrll /*
469 1.1 skrll * Delayed work
470 1.1 skrll */
471 1.12 riastrad
472 1.1 skrll void
473 1.12 riastrad INIT_DELAYED_WORK(struct delayed_work *dw, void (*fn)(struct work_struct *))
474 1.12 riastrad {
475 1.12 riastrad
476 1.12 riastrad INIT_WORK(&dw->work, fn);
477 1.12 riastrad dw->dw_state = DELAYED_WORK_IDLE;
478 1.12 riastrad
479 1.12 riastrad /*
480 1.12 riastrad * Defer callout_init until we are going to schedule the
481 1.1 skrll * callout, which can then callout_destroy it, because
482 1.1 skrll * otherwise since there's no DESTROY_DELAYED_WORK or anything
483 1.1 skrll * we have no opportunity to call callout_destroy.
484 1.1 skrll */
485 1.1 skrll }
486 1.12 riastrad
487 1.1 skrll bool
488 1.1 skrll schedule_delayed_work(struct delayed_work *dw, unsigned long ticks)
489 1.1 skrll {
490 1.12 riastrad
491 1.12 riastrad return queue_delayed_work(system_wq, dw, ticks);
492 1.1 skrll }
493 1.1 skrll
494 1.1 skrll static void
495 1.12 riastrad queue_delayed_work_anew(struct workqueue_struct *wq, struct delayed_work *dw,
496 1.12 riastrad unsigned long ticks)
497 1.12 riastrad {
498 1.12 riastrad
499 1.12 riastrad KASSERT(mutex_owned(&wq->wq_lock));
500 1.12 riastrad KASSERT(dw->work.work_queue == wq);
501 1.12 riastrad KASSERT((dw->dw_state == DELAYED_WORK_IDLE) ||
502 1.12 riastrad (dw->dw_state == DELAYED_WORK_SCHEDULED));
503 1.12 riastrad
504 1.1 skrll if (ticks == 0) {
505 1.12 riastrad if (dw->dw_state == DELAYED_WORK_SCHEDULED) {
506 1.12 riastrad callout_destroy(&dw->dw_callout);
507 1.12 riastrad TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
508 1.12 riastrad } else {
509 1.12 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
510 1.12 riastrad }
511 1.1 skrll TAILQ_INSERT_TAIL(&wq->wq_queue, &dw->work, work_entry);
512 1.12 riastrad dw->dw_state = DELAYED_WORK_IDLE;
513 1.12 riastrad } else {
514 1.1 skrll if (dw->dw_state == DELAYED_WORK_IDLE) {
515 1.12 riastrad callout_init(&dw->dw_callout, CALLOUT_MPSAFE);
516 1.12 riastrad callout_reset(&dw->dw_callout, MIN(INT_MAX, ticks),
517 1.1 skrll &linux_workqueue_timeout, dw);
518 1.12 riastrad TAILQ_INSERT_HEAD(&wq->wq_delayed, dw, dw_entry);
519 1.12 riastrad } else {
520 1.12 riastrad KASSERT(dw->dw_state == DELAYED_WORK_SCHEDULED);
521 1.1 skrll }
522 1.23 riastrad dw->dw_state = DELAYED_WORK_SCHEDULED;
523 1.23 riastrad }
524 1.23 riastrad }
525 1.23 riastrad
526 1.23 riastrad static void
527 1.23 riastrad cancel_delayed_work_done(struct workqueue_struct *wq, struct delayed_work *dw)
528 1.23 riastrad {
529 1.23 riastrad
530 1.23 riastrad KASSERT(mutex_owned(&wq->wq_lock));
531 1.23 riastrad KASSERT(dw->work.work_queue == wq);
532 1.23 riastrad KASSERT(dw->dw_state == DELAYED_WORK_CANCELLED);
533 1.23 riastrad dw->dw_state = DELAYED_WORK_IDLE;
534 1.23 riastrad callout_destroy(&dw->dw_callout);
535 1.23 riastrad TAILQ_REMOVE(&wq->wq_delayed, dw, dw_entry);
536 1.12 riastrad release_work(&dw->work, wq);
537 1.12 riastrad /* Can't touch dw after this point. */
538 1.12 riastrad }
539 1.12 riastrad
540 1.12 riastrad bool
541 1.12 riastrad queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
542 1.1 skrll unsigned long ticks)
543 1.12 riastrad {
544 1.17 riastrad struct workqueue_struct *wq0;
545 1.12 riastrad bool newly_queued;
546 1.12 riastrad
547 1.12 riastrad mutex_enter(&wq->wq_lock);
548 1.12 riastrad if (__predict_true((wq0 = acquire_work(&dw->work, wq)) == NULL)) {
549 1.12 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
550 1.1 skrll queue_delayed_work_anew(wq, dw, ticks);
551 1.1 skrll newly_queued = true;
552 1.12 riastrad } else {
553 1.1 skrll KASSERT(wq0 == wq);
554 1.1 skrll newly_queued = false;
555 1.1 skrll }
556 1.1 skrll mutex_exit(&wq->wq_lock);
557 1.1 skrll
558 1.1 skrll return newly_queued;
559 1.1 skrll }
560 1.1 skrll
561 1.12 riastrad bool
562 1.1 skrll mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw,
563 1.1 skrll unsigned long ticks)
564 1.12 riastrad {
565 1.17 riastrad struct workqueue_struct *wq0;
566 1.12 riastrad bool timer_modified;
567 1.12 riastrad
568 1.12 riastrad mutex_enter(&wq->wq_lock);
569 1.12 riastrad if ((wq0 = acquire_work(&dw->work, wq)) == NULL) {
570 1.12 riastrad KASSERT(dw->dw_state == DELAYED_WORK_IDLE);
571 1.12 riastrad queue_delayed_work_anew(wq, dw, ticks);
572 1.12 riastrad timer_modified = false;
573 1.12 riastrad } else {
574 1.12 riastrad KASSERT(wq0 == wq);
575 1.12 riastrad switch (dw->dw_state) {
576 1.12 riastrad case DELAYED_WORK_IDLE:
577 1.12 riastrad if (wq->wq_current_work != &dw->work) {
578 1.12 riastrad /* Work is queued, but hasn't started yet. */
579 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
580 1.12 riastrad work_entry);
581 1.12 riastrad queue_delayed_work_anew(wq, dw, ticks);
582 1.12 riastrad timer_modified = true;
583 1.12 riastrad } else {
584 1.12 riastrad /*
585 1.12 riastrad * Too late. Queue it anew. If that
586 1.12 riastrad * would skip the callout because it's
587 1.12 riastrad * immediate, notify the workqueue.
588 1.12 riastrad */
589 1.12 riastrad wq->wq_requeued = ticks == 0;
590 1.12 riastrad queue_delayed_work_anew(wq, dw, ticks);
591 1.12 riastrad timer_modified = false;
592 1.12 riastrad }
593 1.12 riastrad break;
594 1.12 riastrad case DELAYED_WORK_SCHEDULED:
595 1.12 riastrad if (callout_stop(&dw->dw_callout)) {
596 1.12 riastrad /*
597 1.12 riastrad * Too late to stop, but we got in
598 1.12 riastrad * before the callout acquired the
599 1.12 riastrad * lock. Reschedule it and tell it
600 1.12 riastrad * we've done so.
601 1.12 riastrad */
602 1.12 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
603 1.12 riastrad callout_schedule(&dw->dw_callout,
604 1.12 riastrad MIN(INT_MAX, ticks));
605 1.12 riastrad } else {
606 1.12 riastrad /* Stopped it. Queue it anew. */
607 1.12 riastrad queue_delayed_work_anew(wq, dw, ticks);
608 1.12 riastrad }
609 1.12 riastrad timer_modified = true;
610 1.12 riastrad break;
611 1.12 riastrad case DELAYED_WORK_RESCHEDULED:
612 1.12 riastrad case DELAYED_WORK_CANCELLED:
613 1.12 riastrad /*
614 1.12 riastrad * Someone modified the timer _again_, or
615 1.12 riastrad * cancelled it, after the callout started but
616 1.12 riastrad * before the poor thing even had a chance to
617 1.12 riastrad * acquire the lock. Just reschedule it once
618 1.12 riastrad * more.
619 1.12 riastrad */
620 1.12 riastrad callout_schedule(&dw->dw_callout, MIN(INT_MAX, ticks));
621 1.12 riastrad dw->dw_state = DELAYED_WORK_RESCHEDULED;
622 1.12 riastrad timer_modified = true;
623 1.1 skrll break;
624 1.1 skrll default:
625 1.12 riastrad panic("invalid delayed work state: %d",
626 1.1 skrll dw->dw_state);
627 1.1 skrll }
628 1.1 skrll }
629 1.1 skrll mutex_exit(&wq->wq_lock);
630 1.1 skrll
631 1.1 skrll return timer_modified;
632 1.1 skrll }
633 1.12 riastrad
634 1.12 riastrad bool
635 1.1 skrll cancel_delayed_work(struct delayed_work *dw)
636 1.14 riastrad {
637 1.14 riastrad struct workqueue_struct *wq;
638 1.14 riastrad bool cancelled_p;
639 1.14 riastrad
640 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
641 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
642 1.12 riastrad return false;
643 1.12 riastrad
644 1.12 riastrad mutex_enter(&wq->wq_lock);
645 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
646 1.12 riastrad cancelled_p = false;
647 1.12 riastrad } else {
648 1.12 riastrad switch (dw->dw_state) {
649 1.12 riastrad case DELAYED_WORK_IDLE:
650 1.12 riastrad if (wq->wq_current_work == &dw->work) {
651 1.12 riastrad /* Too late, it's already running. */
652 1.12 riastrad cancelled_p = false;
653 1.12 riastrad } else {
654 1.12 riastrad /* Got in before it started. Remove it. */
655 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
656 1.12 riastrad work_entry);
657 1.12 riastrad cancelled_p = true;
658 1.12 riastrad }
659 1.21 riastrad break;
660 1.21 riastrad case DELAYED_WORK_SCHEDULED:
661 1.21 riastrad case DELAYED_WORK_RESCHEDULED:
662 1.21 riastrad case DELAYED_WORK_CANCELLED:
663 1.21 riastrad /*
664 1.21 riastrad * If it is scheduled, mark it cancelled and
665 1.21 riastrad * try to stop the callout before it starts.
666 1.21 riastrad *
667 1.21 riastrad * If it's too late and the callout has already
668 1.21 riastrad * begun to execute, tough.
669 1.21 riastrad *
670 1.21 riastrad * If we stopped the callout before it started,
671 1.12 riastrad * however, then destroy the callout and
672 1.21 riastrad * dissociate it from the workqueue ourselves.
673 1.21 riastrad */
674 1.23 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
675 1.16 riastrad cancelled_p = true;
676 1.12 riastrad if (callout_stop(&dw->dw_callout))
677 1.12 riastrad break;
678 1.12 riastrad cancel_delayed_work_done(wq, dw);
679 1.12 riastrad break;
680 1.1 skrll default:
681 1.12 riastrad panic("invalid delayed work state: %d",
682 1.1 skrll dw->dw_state);
683 1.1 skrll }
684 1.1 skrll }
685 1.1 skrll mutex_exit(&wq->wq_lock);
686 1.1 skrll
687 1.1 skrll return cancelled_p;
688 1.1 skrll }
689 1.12 riastrad
690 1.24 riastrad bool
691 1.1 skrll cancel_delayed_work_sync(struct delayed_work *dw)
692 1.24 riastrad {
693 1.14 riastrad struct workqueue_struct *wq;
694 1.24 riastrad bool cancelled_p;
695 1.14 riastrad
696 1.12 riastrad /* If there's no workqueue, nothing to cancel. */
697 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
698 1.12 riastrad return false;
699 1.12 riastrad
700 1.20 riastrad mutex_enter(&wq->wq_lock);
701 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
702 1.12 riastrad cancelled_p = false;
703 1.12 riastrad } else {
704 1.12 riastrad switch (dw->dw_state) {
705 1.12 riastrad case DELAYED_WORK_IDLE:
706 1.12 riastrad if (wq->wq_current_work == &dw->work) {
707 1.12 riastrad /* Too late, it's already running. Wait. */
708 1.12 riastrad do {
709 1.12 riastrad cv_wait(&wq->wq_cv, &wq->wq_lock);
710 1.12 riastrad } while (wq->wq_current_work == &dw->work);
711 1.12 riastrad cancelled_p = false;
712 1.12 riastrad } else {
713 1.12 riastrad /* Got in before it started. Remove it. */
714 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
715 1.12 riastrad work_entry);
716 1.12 riastrad cancelled_p = true;
717 1.12 riastrad }
718 1.12 riastrad break;
719 1.20 riastrad case DELAYED_WORK_SCHEDULED:
720 1.20 riastrad case DELAYED_WORK_RESCHEDULED:
721 1.20 riastrad case DELAYED_WORK_CANCELLED:
722 1.20 riastrad /*
723 1.20 riastrad * If it is scheduled, mark it cancelled and
724 1.24 riastrad * try to stop the callout before it starts.
725 1.24 riastrad *
726 1.24 riastrad * If it's too late and the callout has already
727 1.20 riastrad * begun to execute, we must wait for it to
728 1.20 riastrad * complete. But we got in soon enough to ask
729 1.20 riastrad * the callout not to run, so we successfully
730 1.20 riastrad * cancelled it in that case.
731 1.12 riastrad *
732 1.12 riastrad * If we stopped the callout before it started,
733 1.20 riastrad * however, then destroy the callout and
734 1.20 riastrad * dissociate it from the workqueue ourselves.
735 1.24 riastrad */
736 1.23 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
737 1.20 riastrad cancelled_p = true;
738 1.12 riastrad if (callout_halt(&dw->dw_callout, &wq->wq_lock))
739 1.12 riastrad break;
740 1.12 riastrad cancel_delayed_work_done(wq, dw);
741 1.12 riastrad break;
742 1.1 skrll default:
743 1.12 riastrad panic("invalid delayed work state: %d",
744 1.1 skrll dw->dw_state);
745 1.1 skrll }
746 1.1 skrll }
747 1.12 riastrad mutex_exit(&wq->wq_lock);
748 1.12 riastrad
749 1.12 riastrad return cancelled_p;
750 1.12 riastrad }
751 1.1 skrll
752 1.5 riastrad /*
754 1.5 riastrad * Flush
755 1.5 riastrad */
756 1.12 riastrad
757 1.5 riastrad void
758 1.5 riastrad flush_scheduled_work(void)
759 1.12 riastrad {
760 1.12 riastrad
761 1.1 skrll flush_workqueue(system_wq);
762 1.12 riastrad }
763 1.1 skrll
764 1.12 riastrad void
765 1.19 riastrad flush_workqueue(struct workqueue_struct *wq)
766 1.19 riastrad {
767 1.19 riastrad uint64_t gen;
768 1.19 riastrad
769 1.19 riastrad mutex_enter(&wq->wq_lock);
770 1.19 riastrad if (wq->wq_current_work || !TAILQ_EMPTY(&wq->wq_queue)) {
771 1.12 riastrad gen = wq->wq_gen;
772 1.1 skrll do {
773 1.1 skrll cv_wait(&wq->wq_cv, &wq->wq_lock);
774 1.12 riastrad } while (gen == wq->wq_gen);
775 1.12 riastrad }
776 1.1 skrll mutex_exit(&wq->wq_lock);
777 1.14 riastrad }
778 1.1 skrll
779 1.14 riastrad bool
780 1.14 riastrad flush_work(struct work_struct *work)
781 1.12 riastrad {
782 1.1 skrll struct workqueue_struct *wq;
783 1.12 riastrad
784 1.12 riastrad /* If there's no workqueue, nothing to flush. */
785 1.1 skrll if ((wq = work->work_queue) == NULL)
786 1.1 skrll return false;
787 1.12 riastrad
788 1.12 riastrad flush_workqueue(wq);
789 1.1 skrll return true;
790 1.14 riastrad }
791 1.12 riastrad
792 1.1 skrll bool
793 1.14 riastrad flush_delayed_work(struct delayed_work *dw)
794 1.14 riastrad {
795 1.12 riastrad struct workqueue_struct *wq;
796 1.1 skrll bool do_flush = false;
797 1.1 skrll
798 1.12 riastrad /* If there's no workqueue, nothing to flush. */
799 1.12 riastrad if ((wq = dw->work.work_queue) == NULL)
800 1.1 skrll return false;
801 1.12 riastrad
802 1.12 riastrad mutex_enter(&wq->wq_lock);
803 1.12 riastrad if (__predict_false(dw->work.work_queue != wq)) {
804 1.12 riastrad do_flush = true;
805 1.12 riastrad } else {
806 1.12 riastrad retry: switch (dw->dw_state) {
807 1.12 riastrad case DELAYED_WORK_IDLE:
808 1.12 riastrad if (wq->wq_current_work != &dw->work) {
809 1.12 riastrad TAILQ_REMOVE(&wq->wq_queue, &dw->work,
810 1.12 riastrad work_entry);
811 1.12 riastrad } else {
812 1.12 riastrad do_flush = true;
813 1.12 riastrad }
814 1.12 riastrad break;
815 1.12 riastrad case DELAYED_WORK_SCHEDULED:
816 1.12 riastrad case DELAYED_WORK_RESCHEDULED:
817 1.12 riastrad case DELAYED_WORK_CANCELLED:
818 1.12 riastrad dw->dw_state = DELAYED_WORK_CANCELLED;
819 1.12 riastrad callout_halt(&dw->dw_callout, &wq->wq_lock);
820 1.1 skrll goto retry;
821 1.12 riastrad default:
822 1.1 skrll panic("invalid delayed work state: %d",
823 1.12 riastrad dw->dw_state);
824 1.12 riastrad }
825 1.1 skrll }
826 1.12 riastrad mutex_exit(&wq->wq_lock);
827 1.1 skrll
828 if (do_flush)
829 flush_workqueue(wq);
830
831 return true;
832 }
833