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 * $FreeBSD$ 30 */ 31 #ifndef _LINUXKPI_LINUX_KTHREAD_H_ 32 #define _LINUXKPI_LINUX_KTHREAD_H_ 33 34 #include <linux/sched.h> 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/malloc.h> 40 #include <sys/queue.h> 41 #include <sys/taskqueue.h> 42 #include <sys/unistd.h> 43 44 struct task_struct; 45 struct kthread_work; 46 47 typedef void (*kthread_work_func_t)(struct kthread_work *work); 48 49 struct kthread_worker { 50 struct task_struct *task; 51 struct taskqueue *tq; 52 }; 53 54 struct kthread_work { 55 struct taskqueue *tq; 56 struct task task; 57 kthread_work_func_t func; 58 }; 59 60 #define kthread_run(fn, data, fmt, ...) ({ \ 61 struct task_struct *__task; \ 62 struct thread *__td; \ 63 \ 64 if (kthread_add(linux_kthread_fn, NULL, NULL, &__td, \ 65 RFSTOPPED, 0, fmt, ## __VA_ARGS__)) \ 66 __task = NULL; \ 67 else \ 68 __task = linux_kthread_setup_and_run(__td, fn, data); \ 69 __task; \ 70 }) 71 72 int linux_kthread_stop(struct task_struct *); 73 bool linux_kthread_should_stop_task(struct task_struct *); 74 bool linux_kthread_should_stop(void); 75 int linux_kthread_park(struct task_struct *); 76 void linux_kthread_parkme(void); 77 bool linux_kthread_should_park(void); 78 void linux_kthread_unpark(struct task_struct *); 79 void linux_kthread_fn(void *); 80 struct task_struct *linux_kthread_setup_and_run(struct thread *, 81 linux_task_fn_t *, void *arg); 82 int linux_in_atomic(void); 83 84 #define kthread_stop(task) linux_kthread_stop(task) 85 #define kthread_should_stop() linux_kthread_should_stop() 86 #define kthread_should_stop_task(task) linux_kthread_should_stop_task(task) 87 #define kthread_park(task) linux_kthread_park(task) 88 #define kthread_parkme() linux_kthread_parkme() 89 #define kthread_should_park() linux_kthread_should_park() 90 #define kthread_unpark(task) linux_kthread_unpark(task) 91 92 #define in_atomic() linux_in_atomic() 93 94 /* Only kthread_(create|destroy)_worker interface is allowed */ 95 #define kthread_init_worker(worker) \ 96 _Static_assert(false, "pre-4.9 worker interface is not supported"); 97 98 task_fn_t lkpi_kthread_work_fn; 99 task_fn_t lkpi_kthread_worker_init_fn; 100 101 #define kthread_create_worker(flags, fmt, ...) ({ \ 102 struct kthread_worker *__w; \ 103 struct task __task; \ 104 \ 105 __w = malloc(sizeof(*__w), M_KMALLOC, M_WAITOK | M_ZERO); \ 106 __w->tq = taskqueue_create("lkpi kthread taskq", M_WAITOK, \ 107 taskqueue_thread_enqueue, &__w->tq); \ 108 taskqueue_start_threads(&__w->tq, 1, PWAIT, fmt, ##__VA_ARGS__);\ 109 TASK_INIT(&__task, 0, lkpi_kthread_worker_init_fn, __w); \ 110 taskqueue_enqueue(__w->tq, &__task); \ 111 taskqueue_drain(__w->tq, &__task); \ 112 __w; \ 113 }) 114 115 static inline void 116 kthread_destroy_worker(struct kthread_worker *worker) 117 { 118 taskqueue_drain_all(worker->tq); 119 taskqueue_free(worker->tq); 120 free(worker, M_KMALLOC); 121 } 122 123 static inline void 124 kthread_init_work(struct kthread_work *work, kthread_work_func_t func) 125 { 126 work->tq = NULL; 127 work->func = func; 128 TASK_INIT(&work->task, 0, lkpi_kthread_work_fn, work); 129 } 130 131 static inline bool 132 kthread_queue_work(struct kthread_worker *worker, struct kthread_work *work) 133 { 134 int error; 135 136 error = taskqueue_enqueue_flags(worker->tq, &work->task, 137 TASKQUEUE_FAIL_IF_CANCELING | TASKQUEUE_FAIL_IF_PENDING); 138 if (error == 0) 139 work->tq = worker->tq; 140 return (error == 0); 141 } 142 143 static inline bool 144 kthread_cancel_work_sync(struct kthread_work *work) 145 { 146 u_int pending = 0; 147 148 if (work->tq != NULL && 149 taskqueue_cancel(work->tq, &work->task, &pending) != 0) 150 taskqueue_drain(work->tq, &work->task); 151 152 return (pending != 0); 153 } 154 155 static inline void 156 kthread_flush_work(struct kthread_work *work) 157 { 158 if (work->tq != NULL) 159 taskqueue_drain(work->tq, &work->task); 160 } 161 162 static inline void 163 kthread_flush_worker(struct kthread_worker *worker) 164 { 165 taskqueue_drain_all(worker->tq); 166 } 167 168 #endif /* _LINUXKPI_LINUX_KTHREAD_H_ */ 169