2 * Copyright (c) 2017-2019 Hans Petter Selasky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <linux/workqueue.h>
31 #include <linux/wait.h>
32 #include <linux/compat.h>
33 #include <linux/spinlock.h>
35 #include <sys/kernel.h>
38 * Define all work struct states
41 WORK_ST_IDLE, /* idle - not started */
42 WORK_ST_TIMER, /* timer is being started */
43 WORK_ST_TASK, /* taskqueue is being queued */
44 WORK_ST_EXEC, /* callback is being called */
45 WORK_ST_CANCEL, /* cancel is being requested */
50 * Define global workqueues
52 static struct workqueue_struct *linux_system_short_wq;
53 static struct workqueue_struct *linux_system_long_wq;
55 struct workqueue_struct *system_wq;
56 struct workqueue_struct *system_long_wq;
57 struct workqueue_struct *system_unbound_wq;
58 struct workqueue_struct *system_highpri_wq;
59 struct workqueue_struct *system_power_efficient_wq;
61 static int linux_default_wq_cpus = 4;
63 static void linux_delayed_work_timer_fn(void *);
66 * This function atomically updates the work state and returns the
67 * previous state at the time of update.
70 linux_update_state(atomic_t *v, const uint8_t *pstate)
76 while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
83 * A LinuxKPI task is allowed to free itself inside the callback function
84 * and cannot safely be referred after the callback function has
85 * completed. This function gives the linux_work_fn() function a hint,
86 * that the task is not going away and can have its state checked
87 * again. Without this extra hint LinuxKPI tasks cannot be serialized
88 * accross multiple worker threads.
91 linux_work_exec_unblock(struct work_struct *work)
93 struct workqueue_struct *wq;
94 struct work_exec *exec;
97 wq = work->work_queue;
98 if (unlikely(wq == NULL))
102 TAILQ_FOREACH(exec, &wq->exec_head, entry) {
103 if (exec->target == work) {
115 linux_delayed_work_enqueue(struct delayed_work *dwork)
117 struct taskqueue *tq;
119 tq = dwork->work.work_queue->taskqueue;
120 taskqueue_enqueue(tq, &dwork->work.work_task);
124 * This function queues the given work structure on the given
125 * workqueue. It returns non-zero if the work was successfully
126 * [re-]queued. Else the work is already pending for completion.
129 linux_queue_work_on(int cpu __unused, struct workqueue_struct *wq,
130 struct work_struct *work)
132 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
133 [WORK_ST_IDLE] = WORK_ST_TASK, /* start queuing task */
134 [WORK_ST_TIMER] = WORK_ST_TIMER, /* NOP */
135 [WORK_ST_TASK] = WORK_ST_TASK, /* NOP */
136 [WORK_ST_EXEC] = WORK_ST_TASK, /* queue task another time */
137 [WORK_ST_CANCEL] = WORK_ST_TASK, /* start queuing task again */
140 if (atomic_read(&wq->draining) != 0)
141 return (!work_pending(work));
143 switch (linux_update_state(&work->state, states)) {
146 if (linux_work_exec_unblock(work) != 0)
150 work->work_queue = wq;
151 taskqueue_enqueue(wq->taskqueue, &work->work_task);
154 return (false); /* already on a queue */
159 * This function queues the given work structure on the given
160 * workqueue after a given delay in ticks. It returns non-zero if the
161 * work was successfully [re-]queued. Else the work is already pending
165 linux_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
166 struct delayed_work *dwork, unsigned delay)
168 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
169 [WORK_ST_IDLE] = WORK_ST_TIMER, /* start timeout */
170 [WORK_ST_TIMER] = WORK_ST_TIMER, /* NOP */
171 [WORK_ST_TASK] = WORK_ST_TASK, /* NOP */
172 [WORK_ST_EXEC] = WORK_ST_TIMER, /* start timeout */
173 [WORK_ST_CANCEL] = WORK_ST_TIMER, /* start timeout */
176 if (atomic_read(&wq->draining) != 0)
177 return (!work_pending(&dwork->work));
179 switch (linux_update_state(&dwork->work.state, states)) {
182 if (delay == 0 && linux_work_exec_unblock(&dwork->work) != 0) {
183 dwork->timer.expires = jiffies;
188 dwork->work.work_queue = wq;
189 dwork->timer.expires = jiffies + delay;
192 linux_delayed_work_enqueue(dwork);
193 } else if (unlikely(cpu != WORK_CPU_UNBOUND)) {
194 mtx_lock(&dwork->timer.mtx);
195 callout_reset_on(&dwork->timer.callout, delay,
196 &linux_delayed_work_timer_fn, dwork, cpu);
197 mtx_unlock(&dwork->timer.mtx);
199 mtx_lock(&dwork->timer.mtx);
200 callout_reset(&dwork->timer.callout, delay,
201 &linux_delayed_work_timer_fn, dwork);
202 mtx_unlock(&dwork->timer.mtx);
206 return (false); /* already on a queue */
211 linux_work_fn(void *context, int pending)
213 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
214 [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */
215 [WORK_ST_TIMER] = WORK_ST_EXEC, /* delayed work w/o timeout */
216 [WORK_ST_TASK] = WORK_ST_EXEC, /* call callback */
217 [WORK_ST_EXEC] = WORK_ST_IDLE, /* complete callback */
218 [WORK_ST_CANCEL] = WORK_ST_EXEC, /* failed to cancel */
220 struct work_struct *work;
221 struct workqueue_struct *wq;
222 struct work_exec exec;
223 struct task_struct *task;
227 /* setup local variables */
229 wq = work->work_queue;
231 /* store target pointer */
234 /* insert executor into list */
236 TAILQ_INSERT_TAIL(&wq->exec_head, &exec, entry);
238 switch (linux_update_state(&work->state, states)) {
244 /* set current work structure */
247 /* call work function */
250 /* set current work structure */
254 /* check if unblocked */
255 if (exec.target != work) {
266 /* remove executor from list */
267 TAILQ_REMOVE(&wq->exec_head, &exec, entry);
272 linux_delayed_work_fn(void *context, int pending)
274 struct delayed_work *dwork = context;
277 * Make sure the timer belonging to the delayed work gets
278 * drained before invoking the work function. Else the timer
279 * mutex may still be in use which can lead to use-after-free
280 * situations, because the work function might free the work
281 * structure before returning.
283 callout_drain(&dwork->timer.callout);
285 linux_work_fn(&dwork->work, pending);
289 linux_delayed_work_timer_fn(void *arg)
291 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
292 [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */
293 [WORK_ST_TIMER] = WORK_ST_TASK, /* start queueing task */
294 [WORK_ST_TASK] = WORK_ST_TASK, /* NOP */
295 [WORK_ST_EXEC] = WORK_ST_EXEC, /* NOP */
296 [WORK_ST_CANCEL] = WORK_ST_TASK, /* failed to cancel */
298 struct delayed_work *dwork = arg;
300 switch (linux_update_state(&dwork->work.state, states)) {
303 linux_delayed_work_enqueue(dwork);
311 * This function cancels the given work structure in a synchronous
312 * fashion. It returns non-zero if the work was successfully
313 * cancelled. Else the work was already cancelled.
316 linux_cancel_work_sync(struct work_struct *work)
318 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
319 [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */
320 [WORK_ST_TIMER] = WORK_ST_TIMER, /* can't happen */
321 [WORK_ST_TASK] = WORK_ST_IDLE, /* cancel and drain */
322 [WORK_ST_EXEC] = WORK_ST_IDLE, /* too late, drain */
323 [WORK_ST_CANCEL] = WORK_ST_IDLE, /* cancel and drain */
325 struct taskqueue *tq;
328 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
329 "linux_cancel_work_sync() might sleep");
331 switch (linux_update_state(&work->state, states)) {
336 tq = work->work_queue->taskqueue;
337 if (taskqueue_cancel(tq, &work->work_task, NULL) != 0)
338 taskqueue_drain(tq, &work->work_task);
339 goto retry; /* work may have restarted itself */
341 tq = work->work_queue->taskqueue;
342 if (taskqueue_cancel(tq, &work->work_task, NULL) != 0)
343 taskqueue_drain(tq, &work->work_task);
350 * This function atomically stops the timer and callback. The timer
351 * callback will not be called after this function returns. This
352 * functions returns true when the timeout was cancelled. Else the
353 * timeout was not started or has already been called.
356 linux_cancel_timer(struct delayed_work *dwork, bool drain)
360 mtx_lock(&dwork->timer.mtx);
361 cancelled = (callout_stop(&dwork->timer.callout) == 1);
362 mtx_unlock(&dwork->timer.mtx);
364 /* check if we should drain */
366 callout_drain(&dwork->timer.callout);
371 * This function cancels the given delayed work structure in a
372 * non-blocking fashion. It returns non-zero if the work was
373 * successfully cancelled. Else the work may still be busy or already
377 linux_cancel_delayed_work(struct delayed_work *dwork)
379 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
380 [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */
381 [WORK_ST_TIMER] = WORK_ST_CANCEL, /* try to cancel */
382 [WORK_ST_TASK] = WORK_ST_CANCEL, /* try to cancel */
383 [WORK_ST_EXEC] = WORK_ST_EXEC, /* NOP */
384 [WORK_ST_CANCEL] = WORK_ST_CANCEL, /* NOP */
386 struct taskqueue *tq;
388 switch (linux_update_state(&dwork->work.state, states)) {
391 if (linux_cancel_timer(dwork, 0)) {
392 atomic_cmpxchg(&dwork->work.state,
393 WORK_ST_CANCEL, WORK_ST_IDLE);
398 tq = dwork->work.work_queue->taskqueue;
399 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) == 0) {
400 atomic_cmpxchg(&dwork->work.state,
401 WORK_ST_CANCEL, WORK_ST_IDLE);
411 * This function cancels the given work structure in a synchronous
412 * fashion. It returns non-zero if the work was successfully
413 * cancelled. Else the work was already cancelled.
416 linux_cancel_delayed_work_sync(struct delayed_work *dwork)
418 static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
419 [WORK_ST_IDLE] = WORK_ST_IDLE, /* NOP */
420 [WORK_ST_TIMER] = WORK_ST_IDLE, /* cancel and drain */
421 [WORK_ST_TASK] = WORK_ST_IDLE, /* cancel and drain */
422 [WORK_ST_EXEC] = WORK_ST_IDLE, /* too late, drain */
423 [WORK_ST_CANCEL] = WORK_ST_IDLE, /* cancel and drain */
425 struct taskqueue *tq;
428 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
429 "linux_cancel_delayed_work_sync() might sleep");
431 switch (linux_update_state(&dwork->work.state, states)) {
435 tq = dwork->work.work_queue->taskqueue;
436 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0)
437 taskqueue_drain(tq, &dwork->work.work_task);
438 goto retry; /* work may have restarted itself */
441 if (linux_cancel_timer(dwork, 1)) {
443 * Make sure taskqueue is also drained before
446 tq = dwork->work.work_queue->taskqueue;
447 taskqueue_drain(tq, &dwork->work.work_task);
453 tq = dwork->work.work_queue->taskqueue;
454 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0)
455 taskqueue_drain(tq, &dwork->work.work_task);
462 * This function waits until the given work structure is completed.
463 * It returns non-zero if the work was successfully
464 * waited for. Else the work was not waited for.
467 linux_flush_work(struct work_struct *work)
469 struct taskqueue *tq;
472 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
473 "linux_flush_work() might sleep");
475 switch (atomic_read(&work->state)) {
479 tq = work->work_queue->taskqueue;
480 retval = taskqueue_poll_is_busy(tq, &work->work_task);
481 taskqueue_drain(tq, &work->work_task);
487 * This function waits until the given delayed work structure is
488 * completed. It returns non-zero if the work was successfully waited
489 * for. Else the work was not waited for.
492 linux_flush_delayed_work(struct delayed_work *dwork)
494 struct taskqueue *tq;
497 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
498 "linux_flush_delayed_work() might sleep");
500 switch (atomic_read(&dwork->work.state)) {
504 if (linux_cancel_timer(dwork, 1))
505 linux_delayed_work_enqueue(dwork);
508 tq = dwork->work.work_queue->taskqueue;
509 retval = taskqueue_poll_is_busy(tq, &dwork->work.work_task);
510 taskqueue_drain(tq, &dwork->work.work_task);
516 * This function returns true if the given work is pending, and not
520 linux_work_pending(struct work_struct *work)
522 switch (atomic_read(&work->state)) {
533 * This function returns true if the given work is busy.
536 linux_work_busy(struct work_struct *work)
538 struct taskqueue *tq;
540 switch (atomic_read(&work->state)) {
544 tq = work->work_queue->taskqueue;
545 return (taskqueue_poll_is_busy(tq, &work->work_task));
551 struct workqueue_struct *
552 linux_create_workqueue_common(const char *name, int cpus)
554 struct workqueue_struct *wq;
557 * If zero CPUs are specified use the default number of CPUs:
560 cpus = linux_default_wq_cpus;
562 wq = kmalloc(sizeof(*wq), M_WAITOK | M_ZERO);
563 wq->taskqueue = taskqueue_create(name, M_WAITOK,
564 taskqueue_thread_enqueue, &wq->taskqueue);
565 atomic_set(&wq->draining, 0);
566 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
567 TAILQ_INIT(&wq->exec_head);
568 mtx_init(&wq->exec_mtx, "linux_wq_exec", NULL, MTX_DEF);
574 linux_destroy_workqueue(struct workqueue_struct *wq)
576 atomic_inc(&wq->draining);
578 taskqueue_free(wq->taskqueue);
579 mtx_destroy(&wq->exec_mtx);
584 linux_init_delayed_work(struct delayed_work *dwork, work_func_t func)
586 memset(dwork, 0, sizeof(*dwork));
587 dwork->work.func = func;
588 TASK_INIT(&dwork->work.work_task, 0, linux_delayed_work_fn, dwork);
589 mtx_init(&dwork->timer.mtx, spin_lock_name("lkpi-dwork"), NULL,
590 MTX_DEF | MTX_NOWITNESS);
591 callout_init_mtx(&dwork->timer.callout, &dwork->timer.mtx, 0);
595 linux_current_work(void)
597 return (current->work);
601 linux_work_init(void *arg)
603 int max_wq_cpus = mp_ncpus + 1;
605 /* avoid deadlock when there are too few threads */
609 /* set default number of CPUs */
610 linux_default_wq_cpus = max_wq_cpus;
612 linux_system_short_wq = alloc_workqueue("linuxkpi_short_wq", 0, max_wq_cpus);
613 linux_system_long_wq = alloc_workqueue("linuxkpi_long_wq", 0, max_wq_cpus);
615 /* populate the workqueue pointers */
616 system_long_wq = linux_system_long_wq;
617 system_wq = linux_system_short_wq;
618 system_power_efficient_wq = linux_system_short_wq;
619 system_unbound_wq = linux_system_short_wq;
620 system_highpri_wq = linux_system_short_wq;
622 SYSINIT(linux_work_init, SI_SUB_TASKQ, SI_ORDER_THIRD, linux_work_init, NULL);
625 linux_work_uninit(void *arg)
627 destroy_workqueue(linux_system_short_wq);
628 destroy_workqueue(linux_system_long_wq);
630 /* clear workqueue pointers */
631 system_long_wq = NULL;
633 system_power_efficient_wq = NULL;
634 system_unbound_wq = NULL;
635 system_highpri_wq = NULL;
637 SYSUNINIT(linux_work_uninit, SI_SUB_TASKQ, SI_ORDER_THIRD, linux_work_uninit, NULL);