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