1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 #ifndef _LINUXKPI_LINUX_KTHREAD_H_ 30 #define _LINUXKPI_LINUX_KTHREAD_H_ 31 32 #include <linux/sched.h> 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/kthread.h> 37 #include <sys/malloc.h> 38 #include <sys/queue.h> 39 #include <sys/taskqueue.h> 40 #include <sys/unistd.h> 41 42 struct task_struct; 43 struct kthread_work; 44 45 typedef void (*kthread_work_func_t)(struct kthread_work *work); 46 47 struct kthread_worker { 48 struct task_struct *task; 49 struct taskqueue *tq; 50 }; 51 52 struct kthread_work { 53 struct taskqueue *tq; 54 struct task task; 55 kthread_work_func_t func; 56 }; 57 58 #define kthread_run(fn, data, fmt, ...) ({ \ 59 struct task_struct *__task; \ 60 struct thread *__td; \ 61 \ 62 if (kthread_add(linux_kthread_fn, NULL, NULL, &__td, \ 63 RFSTOPPED, 0, fmt, ## __VA_ARGS__)) \ 64 __task = NULL; \ 65 else \ 66 __task = linux_kthread_setup_and_run(__td, fn, data); \ 67 __task; \ 68 }) 69 70 int linux_kthread_stop(struct task_struct *); 71 bool linux_kthread_should_stop_task(struct task_struct *); 72 bool linux_kthread_should_stop(void); 73 int linux_kthread_park(struct task_struct *); 74 void linux_kthread_parkme(void); 75 bool linux_kthread_should_park(void); 76 void linux_kthread_unpark(struct task_struct *); 77 void linux_kthread_fn(void *); 78 struct task_struct *linux_kthread_setup_and_run(struct thread *, 79 linux_task_fn_t *, void *arg); 80 int linux_in_atomic(void); 81 82 #define kthread_stop(task) linux_kthread_stop(task) 83 #define kthread_should_stop() linux_kthread_should_stop() 84 #define kthread_should_stop_task(task) linux_kthread_should_stop_task(task) 85 #define kthread_park(task) linux_kthread_park(task) 86 #define kthread_parkme() linux_kthread_parkme() 87 #define kthread_should_park() linux_kthread_should_park() 88 #define kthread_unpark(task) linux_kthread_unpark(task) 89 90 #define in_atomic() linux_in_atomic() 91 92 /* Only kthread_(create|destroy)_worker interface is allowed */ 93 #define kthread_init_worker(worker) \ 94 _Static_assert(false, "pre-4.9 worker interface is not supported"); 95 96 task_fn_t lkpi_kthread_work_fn; 97 task_fn_t lkpi_kthread_worker_init_fn; 98 99 #define kthread_create_worker(flags, fmt, ...) ({ \ 100 struct kthread_worker *__w; \ 101 struct task __task; \ 102 \ 103 __w = malloc(sizeof(*__w), M_KMALLOC, M_WAITOK | M_ZERO); \ 104 __w->tq = taskqueue_create("lkpi kthread taskq", M_WAITOK, \ 105 taskqueue_thread_enqueue, &__w->tq); \ 106 taskqueue_start_threads(&__w->tq, 1, PWAIT, fmt, ##__VA_ARGS__);\ 107 TASK_INIT(&__task, 0, lkpi_kthread_worker_init_fn, __w); \ 108 taskqueue_enqueue(__w->tq, &__task); \ 109 taskqueue_drain(__w->tq, &__task); \ 110 __w; \ 111 }) 112 113 /* 114 * On Linux, `kthread_run_worker()` differs from `kthread_create_worker()` by 115 * waking up the task after creating it with `kthread_create_worker()`. 116 * 117 * On FreeBSD, we already execute it and wait for it, so probably no need to do 118 * anything further. Therefore, `kthread_run_worker()` is an alias to 119 * `kthread_create_worker()`. 120 */ 121 #define kthread_run_worker(flags, fmt, ...) \ 122 kthread_create_worker(flags, fmt, ##__VA_ARGS__) 123 124 static inline void 125 kthread_destroy_worker(struct kthread_worker *worker) 126 { 127 taskqueue_drain_all(worker->tq); 128 taskqueue_free(worker->tq); 129 free(worker, M_KMALLOC); 130 } 131 132 static inline void 133 kthread_init_work(struct kthread_work *work, kthread_work_func_t func) 134 { 135 work->tq = NULL; 136 work->func = func; 137 TASK_INIT(&work->task, 0, lkpi_kthread_work_fn, work); 138 } 139 140 static inline bool 141 kthread_queue_work(struct kthread_worker *worker, struct kthread_work *work) 142 { 143 int error; 144 145 error = taskqueue_enqueue_flags(worker->tq, &work->task, 146 TASKQUEUE_FAIL_IF_CANCELING | TASKQUEUE_FAIL_IF_PENDING); 147 if (error == 0) 148 work->tq = worker->tq; 149 return (error == 0); 150 } 151 152 static inline bool 153 kthread_cancel_work_sync(struct kthread_work *work) 154 { 155 u_int pending = 0; 156 157 if (work->tq != NULL && 158 taskqueue_cancel(work->tq, &work->task, &pending) != 0) 159 taskqueue_drain(work->tq, &work->task); 160 161 return (pending != 0); 162 } 163 164 static inline void 165 kthread_flush_work(struct kthread_work *work) 166 { 167 if (work->tq != NULL) 168 taskqueue_drain(work->tq, &work->task); 169 } 170 171 static inline void 172 kthread_flush_worker(struct kthread_worker *worker) 173 { 174 taskqueue_drain_all(worker->tq); 175 } 176 177 #endif /* _LINUXKPI_LINUX_KTHREAD_H_ */ 178