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