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 static inline void
114 kthread_destroy_worker(struct kthread_worker *worker)
115 {
116 	taskqueue_drain_all(worker->tq);
117 	taskqueue_free(worker->tq);
118 	free(worker, M_KMALLOC);
119 }
120 
121 static inline void
122 kthread_init_work(struct kthread_work *work, kthread_work_func_t func)
123 {
124 	work->tq = NULL;
125 	work->func = func;
126 	TASK_INIT(&work->task, 0, lkpi_kthread_work_fn, work);
127 }
128 
129 static inline bool
130 kthread_queue_work(struct kthread_worker *worker, struct kthread_work *work)
131 {
132 	int error;
133 
134 	error = taskqueue_enqueue_flags(worker->tq, &work->task,
135 	    TASKQUEUE_FAIL_IF_CANCELING | TASKQUEUE_FAIL_IF_PENDING);
136 	if (error == 0)
137 		work->tq = worker->tq;
138 	return (error == 0);
139 }
140 
141 static inline bool
142 kthread_cancel_work_sync(struct kthread_work *work)
143 {
144 	u_int pending = 0;
145 
146 	if (work->tq != NULL &&
147 	    taskqueue_cancel(work->tq, &work->task, &pending) != 0)
148 		taskqueue_drain(work->tq, &work->task);
149 
150 	return (pending != 0);
151 }
152 
153 static inline void
154 kthread_flush_work(struct kthread_work *work)
155 {
156 	if (work->tq != NULL)
157 		taskqueue_drain(work->tq, &work->task);
158 }
159 
160 static inline void
161 kthread_flush_worker(struct kthread_worker *worker)
162 {
163 	taskqueue_drain_all(worker->tq);
164 }
165 
166 #endif /* _LINUXKPI_LINUX_KTHREAD_H_ */
167