xref: /netbsd-src/sys/kern/subr_workqueue.c (revision c8c024369ccd14b1941474bccdfcee07c8398d5c)
1 /*	$NetBSD: subr_workqueue.c,v 1.18 2007/08/05 01:19:17 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c)2002, 2005 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.18 2007/08/05 01:19:17 rmind Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/cpu.h>
34 #include <sys/systm.h>
35 #include <sys/kthread.h>
36 #include <sys/kmem.h>
37 #include <sys/proc.h>
38 #include <sys/workqueue.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/queue.h>
42 
43 typedef struct work_impl {
44 	SIMPLEQ_ENTRY(work_impl) wk_entry;
45 } work_impl_t;
46 
47 SIMPLEQ_HEAD(workqhead, work_impl);
48 
49 struct workqueue_queue {
50 	kmutex_t q_mutex;
51 	kcondvar_t q_cv;
52 	struct workqhead q_queue;
53 	struct lwp *q_worker;
54 	SLIST_ENTRY(workqueue_queue) q_list;
55 };
56 
57 struct workqueue {
58 	void (*wq_func)(struct work *, void *);
59 	void *wq_arg;
60 	const char *wq_name;
61 	pri_t wq_prio;
62 	int wq_flags;
63 	void *wq_ptr;
64 	ipl_cookie_t wq_ipl;
65 };
66 
67 #ifdef MULTIPROCESSOR
68 #define	CPU_ALIGN_SIZE		CACHE_LINE_SIZE
69 #else
70 #define	CPU_ALIGN_SIZE		(ALIGNBYTES + 1)
71 #endif
72 
73 #define	WQ_SIZE		(roundup2(sizeof(struct workqueue), CPU_ALIGN_SIZE))
74 #define	WQ_QUEUE_SIZE	(roundup2(sizeof(struct workqueue_queue), CPU_ALIGN_SIZE))
75 
76 #define	POISON	0xaabbccdd
77 
78 static struct workqueue_queue *
79 workqueue_queue_lookup(struct workqueue *wq, struct cpu_info *ci)
80 {
81 	u_int idx = 0;
82 
83 	if (wq->wq_flags & WQ_PERCPU) {
84 		idx = ci ? cpu_index(ci) : cpu_index(curcpu());
85 	}
86 
87 	return (void *)((intptr_t)(wq) + WQ_SIZE + (idx * WQ_QUEUE_SIZE));
88 }
89 
90 static void
91 workqueue_runlist(struct workqueue *wq, struct workqhead *list)
92 {
93 	work_impl_t *wk;
94 	work_impl_t *next;
95 
96 	/*
97 	 * note that "list" is not a complete SIMPLEQ.
98 	 */
99 
100 	for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
101 		next = SIMPLEQ_NEXT(wk, wk_entry);
102 		(*wq->wq_func)((void *)wk, wq->wq_arg);
103 	}
104 }
105 
106 static void
107 workqueue_run(struct workqueue *wq)
108 {
109 	struct workqueue_queue *q;
110 
111 	/* find the workqueue of this kthread */
112 	q = workqueue_queue_lookup(wq, curlwp->l_cpu);
113 
114 	for (;;) {
115 		struct workqhead tmp;
116 
117 		/*
118 		 * we violate abstraction of SIMPLEQ.
119 		 */
120 
121 #if defined(DIAGNOSTIC)
122 		tmp.sqh_last = (void *)POISON;
123 #endif /* defined(DIAGNOSTIC) */
124 
125 		mutex_enter(&q->q_mutex);
126 		while (SIMPLEQ_EMPTY(&q->q_queue))
127 			cv_wait(&q->q_cv, &q->q_mutex);
128 		tmp.sqh_first = q->q_queue.sqh_first; /* XXX */
129 		SIMPLEQ_INIT(&q->q_queue);
130 		mutex_exit(&q->q_mutex);
131 
132 		workqueue_runlist(wq, &tmp);
133 	}
134 }
135 
136 static void
137 workqueue_worker(void *arg)
138 {
139 	struct workqueue *wq = arg;
140 	struct lwp *l;
141 
142 	l = curlwp;
143 	lwp_lock(l);
144 	l->l_priority = wq->wq_prio;
145 	l->l_usrpri = wq->wq_prio;
146 	lwp_unlock(l);
147 
148 	workqueue_run(wq);
149 }
150 
151 static void
152 workqueue_init(struct workqueue *wq, const char *name,
153     void (*callback_func)(struct work *, void *), void *callback_arg,
154     pri_t prio, int ipl)
155 {
156 
157 	wq->wq_ipl = makeiplcookie(ipl);
158 	wq->wq_prio = prio;
159 	wq->wq_name = name;
160 	wq->wq_func = callback_func;
161 	wq->wq_arg = callback_arg;
162 }
163 
164 static int
165 workqueue_initqueue(struct workqueue *wq, struct workqueue_queue *q,
166     int ipl, struct cpu_info *ci)
167 {
168 	int error, ktf;
169 
170 	mutex_init(&q->q_mutex, MUTEX_DRIVER, ipl);
171 	cv_init(&q->q_cv, wq->wq_name);
172 	q->q_worker = NULL;
173 	SIMPLEQ_INIT(&q->q_queue);
174 	ktf = ((wq->wq_flags & WQ_MPSAFE) != 0 ? KTHREAD_MPSAFE : 0);
175 	if (ci) {
176 		error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
177 		    wq, &q->q_worker, "%s/%lu", wq->wq_name, cpu_index(ci));
178 	} else {
179 		error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
180 		    wq, &q->q_worker, "%s", wq->wq_name);
181 	}
182 
183 	return error;
184 }
185 
186 struct workqueue_exitargs {
187 	work_impl_t wqe_wk;
188 	struct workqueue_queue *wqe_q;
189 };
190 
191 static void
192 workqueue_exit(struct work *wk, void *arg)
193 {
194 	struct workqueue_exitargs *wqe = (void *)wk;
195 	struct workqueue_queue *q = wqe->wqe_q;
196 
197 	/*
198 	 * only competition at this point is workqueue_finiqueue.
199 	 */
200 
201 	KASSERT(q->q_worker == curlwp);
202 	mutex_enter(&q->q_mutex);
203 	q->q_worker = NULL;
204 	cv_signal(&q->q_cv);
205 	mutex_exit(&q->q_mutex);
206 	kthread_exit(0);
207 }
208 
209 static void
210 workqueue_finiqueue(struct workqueue *wq, struct workqueue_queue *q)
211 {
212 	struct workqueue_exitargs wqe;
213 
214 	wq->wq_func = workqueue_exit;
215 
216 	wqe.wqe_q = q;
217 	KASSERT(SIMPLEQ_EMPTY(&q->q_queue));
218 	KASSERT(q->q_worker != NULL);
219 	mutex_enter(&q->q_mutex);
220 	SIMPLEQ_INSERT_TAIL(&q->q_queue, &wqe.wqe_wk, wk_entry);
221 	cv_signal(&q->q_cv);
222 	while (q->q_worker != NULL) {
223 		cv_wait(&q->q_cv, &q->q_mutex);
224 	}
225 	mutex_exit(&q->q_mutex);
226 	mutex_destroy(&q->q_mutex);
227 	cv_destroy(&q->q_cv);
228 }
229 
230 /* --- */
231 
232 int
233 workqueue_create(struct workqueue **wqp, const char *name,
234     void (*callback_func)(struct work *, void *), void *callback_arg,
235     pri_t prio, int ipl, int flags)
236 {
237 	struct workqueue *wq;
238 	struct workqueue_queue *q;
239 	void *ptr;
240 	int i, error = 0;
241 	size_t size;
242 
243 	KASSERT(sizeof(work_impl_t) <= sizeof(struct work));
244 
245 	i = (flags & WQ_PERCPU) ? ncpu : 1;
246 	if (ncpu == 1) {
247 		flags &= ~WQ_PERCPU;
248 	}
249 
250 	size = WQ_SIZE + (i * WQ_QUEUE_SIZE) + CPU_ALIGN_SIZE;
251 	ptr = kmem_alloc(size, KM_SLEEP);
252 
253 	wq = (void *)roundup2((intptr_t)ptr, CPU_ALIGN_SIZE);
254 	wq->wq_ptr = ptr;
255 	wq->wq_flags = flags;
256 	q = (void *)((intptr_t)(wq) + WQ_SIZE);
257 
258 	workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
259 	i = 0;
260 
261 	if (flags & WQ_PERCPU) {
262 #ifdef MULTIPROCESSOR
263 		struct cpu_info *ci;
264 		CPU_INFO_ITERATOR cii;
265 
266 		/* create the work-queue for each CPU */
267 		for (CPU_INFO_FOREACH(cii, ci)) {
268 			error = workqueue_initqueue(wq, q, ipl, ci);
269 			if (error) {
270 				break;
271 			}
272 			q = (void *)((intptr_t)(q) + WQ_QUEUE_SIZE);
273 			i++;
274 		}
275 #endif
276 	} else {
277 		/* initialize a work-queue */
278 		error = workqueue_initqueue(wq, q, ipl, NULL);
279 	}
280 
281 	if (error) {
282 		/*
283 		 * workqueue_finiqueue() should be
284 		 * called for the failing one too.
285 		 */
286 		do {
287 			workqueue_finiqueue(wq, q);
288 			q = (void *)((intptr_t)(q) - WQ_QUEUE_SIZE);
289 		} while(i--);
290 		kmem_free(ptr, size);
291 		return error;
292 	}
293 
294 	*wqp = wq;
295 	return 0;
296 }
297 
298 void
299 workqueue_destroy(struct workqueue *wq)
300 {
301 	struct workqueue_queue *q;
302 	u_int i = 1;
303 
304 	if (wq->wq_flags & WQ_PERCPU) {
305 #ifdef MULTIPROCESSOR
306 		struct cpu_info *ci;
307 		CPU_INFO_ITERATOR cii;
308 
309 		for (CPU_INFO_FOREACH(cii, ci)) {
310 			q = workqueue_queue_lookup(wq, ci);
311 			workqueue_finiqueue(wq, q);
312 		}
313 		i = ncpu;
314 #endif
315 	} else {
316 		q = workqueue_queue_lookup(wq, NULL);
317 		workqueue_finiqueue(wq, q);
318 	}
319 
320 	kmem_free(wq->wq_ptr, WQ_SIZE + (i * WQ_QUEUE_SIZE) + CPU_ALIGN_SIZE);
321 }
322 
323 void
324 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
325 {
326 	struct workqueue_queue *q;
327 	work_impl_t *wk = (void *)wk0;
328 
329 	KASSERT(wq->wq_flags & WQ_PERCPU || ci == NULL);
330 	q = workqueue_queue_lookup(wq, ci);
331 
332 	mutex_enter(&q->q_mutex);
333 	SIMPLEQ_INSERT_TAIL(&q->q_queue, wk, wk_entry);
334 	cv_signal(&q->q_cv);
335 	mutex_exit(&q->q_mutex);
336 }
337