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