]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_work.c
Fix few issues of LinuxKPI workqueue.
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / src / linux_work.c
1 /*-
2  * Copyright (c) 2017 Hans Petter Selasky
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <linux/workqueue.h>
31 #include <linux/wait.h>
32 #include <linux/compat.h>
33 #include <linux/spinlock.h>
34
35 #include <sys/kernel.h>
36
37 /*
38  * Define all work struct states
39  */
40 enum {
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 */
46         WORK_ST_MAX,
47 };
48
49 /*
50  * Define global workqueues
51  */
52 static struct workqueue_struct *linux_system_short_wq;
53 static struct workqueue_struct *linux_system_long_wq;
54
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_power_efficient_wq;
59
60 static int linux_default_wq_cpus = 4;
61
62 static void linux_delayed_work_timer_fn(void *);
63
64 /*
65  * This function atomically updates the work state and returns the
66  * previous state at the time of update.
67  */
68 static uint8_t
69 linux_update_state(atomic_t *v, const uint8_t *pstate)
70 {
71         int c, old;
72
73         c = v->counter;
74
75         while ((old = atomic_cmpxchg(v, c, pstate[c])) != c)
76                 c = old;
77
78         return (c);
79 }
80
81 /*
82  * A LinuxKPI task is allowed to free itself inside the callback function
83  * and cannot safely be referred after the callback function has
84  * completed. This function gives the linux_work_fn() function a hint,
85  * that the task is not going away and can have its state checked
86  * again. Without this extra hint LinuxKPI tasks cannot be serialized
87  * accross multiple worker threads.
88  */
89 static bool
90 linux_work_exec_unblock(struct work_struct *work)
91 {
92         struct workqueue_struct *wq;
93         struct work_exec *exec;
94         bool retval = 0;
95
96         wq = work->work_queue;
97         if (unlikely(wq == NULL))
98                 goto done;
99
100         WQ_EXEC_LOCK(wq);
101         TAILQ_FOREACH(exec, &wq->exec_head, entry) {
102                 if (exec->target == work) {
103                         exec->target = NULL;
104                         retval = 1;
105                         break;
106                 }
107         }
108         WQ_EXEC_UNLOCK(wq);
109 done:
110         return (retval);
111 }
112
113 static void
114 linux_delayed_work_enqueue(struct delayed_work *dwork)
115 {
116         struct taskqueue *tq;
117
118         tq = dwork->work.work_queue->taskqueue;
119         taskqueue_enqueue(tq, &dwork->work.work_task);
120 }
121
122 /*
123  * This function queues the given work structure on the given
124  * workqueue. It returns non-zero if the work was successfully
125  * [re-]queued. Else the work is already pending for completion.
126  */
127 bool
128 linux_queue_work_on(int cpu __unused, struct workqueue_struct *wq,
129     struct work_struct *work)
130 {
131         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
132                 [WORK_ST_IDLE] = WORK_ST_TASK,          /* start queuing task */
133                 [WORK_ST_TIMER] = WORK_ST_TIMER,        /* NOP */
134                 [WORK_ST_TASK] = WORK_ST_TASK,          /* NOP */
135                 [WORK_ST_EXEC] = WORK_ST_TASK,          /* queue task another time */
136                 [WORK_ST_CANCEL] = WORK_ST_TASK,        /* start queuing task again */
137         };
138
139         if (atomic_read(&wq->draining) != 0)
140                 return (!work_pending(work));
141
142         switch (linux_update_state(&work->state, states)) {
143         case WORK_ST_EXEC:
144         case WORK_ST_CANCEL:
145                 if (linux_work_exec_unblock(work) != 0)
146                         return (1);
147                 /* FALLTHROUGH */
148         case WORK_ST_IDLE:
149                 work->work_queue = wq;
150                 taskqueue_enqueue(wq->taskqueue, &work->work_task);
151                 return (1);
152         default:
153                 return (0);             /* already on a queue */
154         }
155 }
156
157 /*
158  * This function queues the given work structure on the given
159  * workqueue after a given delay in ticks. It returns non-zero if the
160  * work was successfully [re-]queued. Else the work is already pending
161  * for completion.
162  */
163 bool
164 linux_queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
165     struct delayed_work *dwork, unsigned delay)
166 {
167         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
168                 [WORK_ST_IDLE] = WORK_ST_TIMER,         /* start timeout */
169                 [WORK_ST_TIMER] = WORK_ST_TIMER,        /* NOP */
170                 [WORK_ST_TASK] = WORK_ST_TASK,          /* NOP */
171                 [WORK_ST_EXEC] = WORK_ST_TIMER,         /* start timeout */
172                 [WORK_ST_CANCEL] = WORK_ST_TIMER,       /* start timeout */
173         };
174
175         if (atomic_read(&wq->draining) != 0)
176                 return (!work_pending(&dwork->work));
177
178         switch (linux_update_state(&dwork->work.state, states)) {
179         case WORK_ST_EXEC:
180         case WORK_ST_CANCEL:
181                 if (delay == 0 && linux_work_exec_unblock(&dwork->work) != 0) {
182                         dwork->timer.expires = jiffies;
183                         return (1);
184                 }
185                 /* FALLTHROUGH */
186         case WORK_ST_IDLE:
187                 dwork->work.work_queue = wq;
188                 dwork->timer.expires = jiffies + delay;
189
190                 if (delay == 0) {
191                         linux_delayed_work_enqueue(dwork);
192                 } else if (unlikely(cpu != WORK_CPU_UNBOUND)) {
193                         mtx_lock(&dwork->timer.mtx);
194                         callout_reset_on(&dwork->timer.callout, delay,
195                             &linux_delayed_work_timer_fn, dwork, cpu);
196                         mtx_unlock(&dwork->timer.mtx);
197                 } else {
198                         mtx_lock(&dwork->timer.mtx);
199                         callout_reset(&dwork->timer.callout, delay,
200                             &linux_delayed_work_timer_fn, dwork);
201                         mtx_unlock(&dwork->timer.mtx);
202                 }
203                 return (1);
204         default:
205                 return (0);             /* already on a queue */
206         }
207 }
208
209 void
210 linux_work_fn(void *context, int pending)
211 {
212         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
213                 [WORK_ST_IDLE] = WORK_ST_IDLE,          /* NOP */
214                 [WORK_ST_TIMER] = WORK_ST_EXEC,         /* delayed work w/o timeout */
215                 [WORK_ST_TASK] = WORK_ST_EXEC,          /* call callback */
216                 [WORK_ST_EXEC] = WORK_ST_IDLE,          /* complete callback */
217                 [WORK_ST_CANCEL] = WORK_ST_EXEC,        /* failed to cancel */
218         };
219         struct work_struct *work;
220         struct workqueue_struct *wq;
221         struct work_exec exec;
222
223         linux_set_current(curthread);
224
225         /* setup local variables */
226         work = context;
227         wq = work->work_queue;
228
229         /* store target pointer */
230         exec.target = work;
231
232         /* insert executor into list */
233         WQ_EXEC_LOCK(wq);
234         TAILQ_INSERT_TAIL(&wq->exec_head, &exec, entry);
235         while (1) {
236                 switch (linux_update_state(&work->state, states)) {
237                 case WORK_ST_TIMER:
238                 case WORK_ST_TASK:
239                 case WORK_ST_CANCEL:
240                         WQ_EXEC_UNLOCK(wq);
241
242                         /* call work function */
243                         work->func(work);
244
245                         WQ_EXEC_LOCK(wq);
246                         /* check if unblocked */
247                         if (exec.target != work) {
248                                 /* reapply block */
249                                 exec.target = work;
250                                 break;
251                         }
252                         /* FALLTHROUGH */
253                 default:
254                         goto done;
255                 }
256         }
257 done:
258         /* remove executor from list */
259         TAILQ_REMOVE(&wq->exec_head, &exec, entry);
260         WQ_EXEC_UNLOCK(wq);
261 }
262
263 static void
264 linux_delayed_work_timer_fn(void *arg)
265 {
266         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
267                 [WORK_ST_IDLE] = WORK_ST_IDLE,          /* NOP */
268                 [WORK_ST_TIMER] = WORK_ST_TASK,         /* start queueing task */
269                 [WORK_ST_TASK] = WORK_ST_TASK,          /* NOP */
270                 [WORK_ST_EXEC] = WORK_ST_EXEC,          /* NOP */
271                 [WORK_ST_CANCEL] = WORK_ST_TASK,        /* failed to cancel */
272         };
273         struct delayed_work *dwork = arg;
274
275         switch (linux_update_state(&dwork->work.state, states)) {
276         case WORK_ST_TIMER:
277         case WORK_ST_CANCEL:
278                 linux_delayed_work_enqueue(dwork);
279                 break;
280         default:
281                 break;
282         }
283 }
284
285 /*
286  * This function cancels the given work structure in a synchronous
287  * fashion. It returns non-zero if the work was successfully
288  * cancelled. Else the work was already cancelled.
289  */
290 bool
291 linux_cancel_work_sync(struct work_struct *work)
292 {
293         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
294                 [WORK_ST_IDLE] = WORK_ST_IDLE,          /* NOP */
295                 [WORK_ST_TIMER] = WORK_ST_TIMER,        /* can't happen */
296                 [WORK_ST_TASK] = WORK_ST_IDLE,          /* cancel and drain */
297                 [WORK_ST_EXEC] = WORK_ST_IDLE,          /* too late, drain */
298                 [WORK_ST_CANCEL] = WORK_ST_IDLE,        /* cancel and drain */
299         };
300         struct taskqueue *tq;
301
302         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
303             "linux_cancel_work_sync() might sleep");
304
305         switch (linux_update_state(&work->state, states)) {
306         case WORK_ST_IDLE:
307         case WORK_ST_TIMER:
308                 return (0);
309         case WORK_ST_EXEC:
310                 tq = work->work_queue->taskqueue;
311                 if (taskqueue_cancel(tq, &work->work_task, NULL) != 0)
312                         taskqueue_drain(tq, &work->work_task);
313                 return (0);
314         default:
315                 tq = work->work_queue->taskqueue;
316                 if (taskqueue_cancel(tq, &work->work_task, NULL) != 0)
317                         taskqueue_drain(tq, &work->work_task);
318                 return (1);
319         }
320 }
321
322 /*
323  * This function atomically stops the timer and callback. The timer
324  * callback will not be called after this function returns. This
325  * functions returns true when the timeout was cancelled. Else the
326  * timeout was not started or has already been called.
327  */
328 static inline bool
329 linux_cancel_timer(struct delayed_work *dwork, bool drain)
330 {
331         bool cancelled;
332
333         mtx_lock(&dwork->timer.mtx);
334         cancelled = (callout_stop(&dwork->timer.callout) == 1);
335         mtx_unlock(&dwork->timer.mtx);
336
337         /* check if we should drain */
338         if (drain)
339                 callout_drain(&dwork->timer.callout);
340         return (cancelled);
341 }
342
343 /*
344  * This function cancels the given delayed work structure in a
345  * non-blocking fashion. It returns non-zero if the work was
346  * successfully cancelled. Else the work may still be busy or already
347  * cancelled.
348  */
349 bool
350 linux_cancel_delayed_work(struct delayed_work *dwork)
351 {
352         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
353                 [WORK_ST_IDLE] = WORK_ST_IDLE,          /* NOP */
354                 [WORK_ST_TIMER] = WORK_ST_CANCEL,       /* try to cancel */
355                 [WORK_ST_TASK] = WORK_ST_CANCEL,        /* try to cancel */
356                 [WORK_ST_EXEC] = WORK_ST_EXEC,          /* NOP */
357                 [WORK_ST_CANCEL] = WORK_ST_CANCEL,      /* NOP */
358         };
359         struct taskqueue *tq;
360
361         switch (linux_update_state(&dwork->work.state, states)) {
362         case WORK_ST_TIMER:
363         case WORK_ST_CANCEL:
364                 if (linux_cancel_timer(dwork, 0)) {
365                         atomic_cmpxchg(&dwork->work.state,
366                             WORK_ST_CANCEL, WORK_ST_IDLE);
367                         return (1);
368                 }
369                 /* FALLTHROUGH */
370         case WORK_ST_TASK:
371                 tq = dwork->work.work_queue->taskqueue;
372                 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) == 0) {
373                         atomic_cmpxchg(&dwork->work.state,
374                             WORK_ST_CANCEL, WORK_ST_IDLE);
375                         return (1);
376                 }
377                 /* FALLTHROUGH */
378         default:
379                 return (0);
380         }
381 }
382
383 /*
384  * This function cancels the given work structure in a synchronous
385  * fashion. It returns non-zero if the work was successfully
386  * cancelled. Else the work was already cancelled.
387  */
388 bool
389 linux_cancel_delayed_work_sync(struct delayed_work *dwork)
390 {
391         static const uint8_t states[WORK_ST_MAX] __aligned(8) = {
392                 [WORK_ST_IDLE] = WORK_ST_IDLE,          /* NOP */
393                 [WORK_ST_TIMER] = WORK_ST_IDLE,         /* cancel and drain */
394                 [WORK_ST_TASK] = WORK_ST_IDLE,          /* cancel and drain */
395                 [WORK_ST_EXEC] = WORK_ST_IDLE,          /* too late, drain */
396                 [WORK_ST_CANCEL] = WORK_ST_IDLE,        /* cancel and drain */
397         };
398         struct taskqueue *tq;
399
400         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
401             "linux_cancel_delayed_work_sync() might sleep");
402
403         switch (linux_update_state(&dwork->work.state, states)) {
404         case WORK_ST_IDLE:
405                 return (0);
406         case WORK_ST_EXEC:
407                 tq = dwork->work.work_queue->taskqueue;
408                 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0)
409                         taskqueue_drain(tq, &dwork->work.work_task);
410                 return (0);
411         case WORK_ST_TIMER:
412         case WORK_ST_CANCEL:
413                 if (linux_cancel_timer(dwork, 1)) {
414                         /*
415                          * Make sure taskqueue is also drained before
416                          * returning:
417                          */
418                         tq = dwork->work.work_queue->taskqueue;
419                         taskqueue_drain(tq, &dwork->work.work_task);
420                         return (1);
421                 }
422                 /* FALLTHROUGH */
423         default:
424                 tq = dwork->work.work_queue->taskqueue;
425                 if (taskqueue_cancel(tq, &dwork->work.work_task, NULL) != 0)
426                         taskqueue_drain(tq, &dwork->work.work_task);
427                 return (1);
428         }
429 }
430
431 /*
432  * This function waits until the given work structure is completed.
433  * It returns non-zero if the work was successfully
434  * waited for. Else the work was not waited for.
435  */
436 bool
437 linux_flush_work(struct work_struct *work)
438 {
439         struct taskqueue *tq;
440
441         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
442             "linux_flush_work() might sleep");
443
444         switch (atomic_read(&work->state)) {
445         case WORK_ST_IDLE:
446                 return (0);
447         default:
448                 tq = work->work_queue->taskqueue;
449                 taskqueue_drain(tq, &work->work_task);
450                 return (1);
451         }
452 }
453
454 /*
455  * This function waits until the given delayed work structure is
456  * completed. It returns non-zero if the work was successfully waited
457  * for. Else the work was not waited for.
458  */
459 bool
460 linux_flush_delayed_work(struct delayed_work *dwork)
461 {
462         struct taskqueue *tq;
463
464         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
465             "linux_flush_delayed_work() might sleep");
466
467         switch (atomic_read(&dwork->work.state)) {
468         case WORK_ST_IDLE:
469                 return (0);
470         case WORK_ST_TIMER:
471                 if (linux_cancel_timer(dwork, 1))
472                         linux_delayed_work_enqueue(dwork);
473                 /* FALLTHROUGH */
474         default:
475                 tq = dwork->work.work_queue->taskqueue;
476                 taskqueue_drain(tq, &dwork->work.work_task);
477                 return (1);
478         }
479 }
480
481 /*
482  * This function returns true if the given work is pending, and not
483  * yet executing:
484  */
485 bool
486 linux_work_pending(struct work_struct *work)
487 {
488         switch (atomic_read(&work->state)) {
489         case WORK_ST_TIMER:
490         case WORK_ST_TASK:
491         case WORK_ST_CANCEL:
492                 return (1);
493         default:
494                 return (0);
495         }
496 }
497
498 /*
499  * This function returns true if the given work is busy.
500  */
501 bool
502 linux_work_busy(struct work_struct *work)
503 {
504         struct taskqueue *tq;
505
506         switch (atomic_read(&work->state)) {
507         case WORK_ST_IDLE:
508                 return (0);
509         case WORK_ST_EXEC:
510                 tq = work->work_queue->taskqueue;
511                 return (taskqueue_poll_is_busy(tq, &work->work_task));
512         default:
513                 return (1);
514         }
515 }
516
517 struct workqueue_struct *
518 linux_create_workqueue_common(const char *name, int cpus)
519 {
520         struct workqueue_struct *wq;
521
522         /*
523          * If zero CPUs are specified use the default number of CPUs:
524          */
525         if (cpus == 0)
526                 cpus = linux_default_wq_cpus;
527
528         wq = kmalloc(sizeof(*wq), M_WAITOK | M_ZERO);
529         wq->taskqueue = taskqueue_create(name, M_WAITOK,
530             taskqueue_thread_enqueue, &wq->taskqueue);
531         atomic_set(&wq->draining, 0);
532         taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
533         TAILQ_INIT(&wq->exec_head);
534         mtx_init(&wq->exec_mtx, "linux_wq_exec", NULL, MTX_DEF);
535
536         return (wq);
537 }
538
539 void
540 linux_destroy_workqueue(struct workqueue_struct *wq)
541 {
542         atomic_inc(&wq->draining);
543         drain_workqueue(wq);
544         taskqueue_free(wq->taskqueue);
545         mtx_destroy(&wq->exec_mtx);
546         kfree(wq);
547 }
548
549 void
550 linux_init_delayed_work(struct delayed_work *dwork, work_func_t func)
551 {
552         memset(dwork, 0, sizeof(*dwork));
553         INIT_WORK(&dwork->work, func);
554         mtx_init(&dwork->timer.mtx, spin_lock_name("lkpi-dwork"), NULL,
555             MTX_DEF | MTX_NOWITNESS);
556         callout_init_mtx(&dwork->timer.callout, &dwork->timer.mtx, 0);
557 }
558
559 static void
560 linux_work_init(void *arg)
561 {
562         int max_wq_cpus = mp_ncpus + 1;
563
564         /* avoid deadlock when there are too few threads */
565         if (max_wq_cpus < 4)
566                 max_wq_cpus = 4;
567
568         /* set default number of CPUs */
569         linux_default_wq_cpus = max_wq_cpus;
570
571         linux_system_short_wq = alloc_workqueue("linuxkpi_short_wq", 0, max_wq_cpus);
572         linux_system_long_wq = alloc_workqueue("linuxkpi_long_wq", 0, max_wq_cpus);
573
574         /* populate the workqueue pointers */
575         system_long_wq = linux_system_long_wq;
576         system_wq = linux_system_short_wq;
577         system_power_efficient_wq = linux_system_short_wq;
578         system_unbound_wq = linux_system_short_wq;
579 }
580 SYSINIT(linux_work_init, SI_SUB_INIT_IF, SI_ORDER_THIRD, linux_work_init, NULL);
581
582 static void
583 linux_work_uninit(void *arg)
584 {
585         destroy_workqueue(linux_system_short_wq);
586         destroy_workqueue(linux_system_long_wq);
587
588         /* clear workqueue pointers */
589         system_long_wq = NULL;
590         system_wq = NULL;
591         system_power_efficient_wq = NULL;
592         system_unbound_wq = NULL;
593 }
594 SYSUNINIT(linux_work_uninit, SI_SUB_INIT_IF, SI_ORDER_THIRD, linux_work_uninit, NULL);