subr_workqueue.c revision 1.40 1 1.40 riastrad /* $NetBSD: subr_workqueue.c,v 1.40 2022/08/15 11:43:56 riastradh 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.40 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_workqueue.c,v 1.40 2022/08/15 11:43:56 riastradh 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.34 ozaki struct workqhead q_queue_pending;
53 1.34 ozaki struct workqhead q_queue_running;
54 1.28 yamt lwp_t *q_worker;
55 1.1 yamt };
56 1.1 yamt
57 1.1 yamt struct workqueue {
58 1.1 yamt void (*wq_func)(struct work *, void *);
59 1.1 yamt void *wq_arg;
60 1.20 yamt int wq_flags;
61 1.20 yamt
62 1.32 jym char wq_name[MAXCOMLEN];
63 1.12 yamt pri_t wq_prio;
64 1.18 rmind void *wq_ptr;
65 1.1 yamt };
66 1.1 yamt
67 1.24 ad #define WQ_SIZE (roundup2(sizeof(struct workqueue), coherency_unit))
68 1.24 ad #define WQ_QUEUE_SIZE (roundup2(sizeof(struct workqueue_queue), coherency_unit))
69 1.18 rmind
70 1.1 yamt #define POISON 0xaabbccdd
71 1.1 yamt
72 1.20 yamt static size_t
73 1.20 yamt workqueue_size(int flags)
74 1.20 yamt {
75 1.20 yamt
76 1.20 yamt return WQ_SIZE
77 1.20 yamt + ((flags & WQ_PERCPU) != 0 ? ncpu : 1) * WQ_QUEUE_SIZE
78 1.24 ad + coherency_unit;
79 1.20 yamt }
80 1.20 yamt
81 1.14 rmind static struct workqueue_queue *
82 1.14 rmind workqueue_queue_lookup(struct workqueue *wq, struct cpu_info *ci)
83 1.14 rmind {
84 1.18 rmind u_int idx = 0;
85 1.14 rmind
86 1.18 rmind if (wq->wq_flags & WQ_PERCPU) {
87 1.18 rmind idx = ci ? cpu_index(ci) : cpu_index(curcpu());
88 1.18 rmind }
89 1.14 rmind
90 1.26 rmind return (void *)((uintptr_t)(wq) + WQ_SIZE + (idx * WQ_QUEUE_SIZE));
91 1.14 rmind }
92 1.14 rmind
93 1.1 yamt static void
94 1.1 yamt workqueue_runlist(struct workqueue *wq, struct workqhead *list)
95 1.1 yamt {
96 1.17 yamt work_impl_t *wk;
97 1.17 yamt work_impl_t *next;
98 1.1 yamt
99 1.1 yamt /*
100 1.1 yamt * note that "list" is not a complete SIMPLEQ.
101 1.1 yamt */
102 1.1 yamt
103 1.1 yamt for (wk = SIMPLEQ_FIRST(list); wk != NULL; wk = next) {
104 1.1 yamt next = SIMPLEQ_NEXT(wk, wk_entry);
105 1.17 yamt (*wq->wq_func)((void *)wk, wq->wq_arg);
106 1.1 yamt }
107 1.1 yamt }
108 1.1 yamt
109 1.1 yamt static void
110 1.21 yamt workqueue_worker(void *cookie)
111 1.1 yamt {
112 1.21 yamt struct workqueue *wq = cookie;
113 1.14 rmind struct workqueue_queue *q;
114 1.38 riastrad int s;
115 1.14 rmind
116 1.14 rmind /* find the workqueue of this kthread */
117 1.14 rmind q = workqueue_queue_lookup(wq, curlwp->l_cpu);
118 1.14 rmind
119 1.38 riastrad if (wq->wq_flags & WQ_FPU)
120 1.38 riastrad s = kthread_fpu_enter();
121 1.3 rpaulo for (;;) {
122 1.1 yamt /*
123 1.1 yamt * we violate abstraction of SIMPLEQ.
124 1.1 yamt */
125 1.1 yamt
126 1.9 ad mutex_enter(&q->q_mutex);
127 1.34 ozaki while (SIMPLEQ_EMPTY(&q->q_queue_pending))
128 1.9 ad cv_wait(&q->q_cv, &q->q_mutex);
129 1.34 ozaki KASSERT(SIMPLEQ_EMPTY(&q->q_queue_running));
130 1.34 ozaki q->q_queue_running.sqh_first =
131 1.34 ozaki q->q_queue_pending.sqh_first; /* XXX */
132 1.34 ozaki SIMPLEQ_INIT(&q->q_queue_pending);
133 1.9 ad mutex_exit(&q->q_mutex);
134 1.1 yamt
135 1.34 ozaki workqueue_runlist(wq, &q->q_queue_running);
136 1.34 ozaki
137 1.34 ozaki mutex_enter(&q->q_mutex);
138 1.34 ozaki KASSERT(!SIMPLEQ_EMPTY(&q->q_queue_running));
139 1.34 ozaki SIMPLEQ_INIT(&q->q_queue_running);
140 1.39 riastrad /* Wake up workqueue_wait */
141 1.39 riastrad cv_broadcast(&q->q_cv);
142 1.34 ozaki mutex_exit(&q->q_mutex);
143 1.1 yamt }
144 1.38 riastrad if (wq->wq_flags & WQ_FPU)
145 1.38 riastrad kthread_fpu_exit(s);
146 1.1 yamt }
147 1.1 yamt
148 1.1 yamt static void
149 1.1 yamt workqueue_init(struct workqueue *wq, const char *name,
150 1.1 yamt void (*callback_func)(struct work *, void *), void *callback_arg,
151 1.12 yamt pri_t prio, int ipl)
152 1.1 yamt {
153 1.1 yamt
154 1.36 ozaki KASSERT(sizeof(wq->wq_name) > strlen(name));
155 1.32 jym strncpy(wq->wq_name, name, sizeof(wq->wq_name));
156 1.32 jym
157 1.1 yamt wq->wq_prio = prio;
158 1.1 yamt wq->wq_func = callback_func;
159 1.1 yamt wq->wq_arg = callback_arg;
160 1.1 yamt }
161 1.1 yamt
162 1.1 yamt static int
163 1.18 rmind workqueue_initqueue(struct workqueue *wq, struct workqueue_queue *q,
164 1.18 rmind int ipl, struct cpu_info *ci)
165 1.1 yamt {
166 1.13 ad int error, ktf;
167 1.14 rmind
168 1.20 yamt KASSERT(q->q_worker == NULL);
169 1.20 yamt
170 1.22 ad mutex_init(&q->q_mutex, MUTEX_DEFAULT, ipl);
171 1.9 ad cv_init(&q->q_cv, wq->wq_name);
172 1.34 ozaki SIMPLEQ_INIT(&q->q_queue_pending);
173 1.34 ozaki SIMPLEQ_INIT(&q->q_queue_running);
174 1.18 rmind ktf = ((wq->wq_flags & WQ_MPSAFE) != 0 ? KTHREAD_MPSAFE : 0);
175 1.33 matt if (wq->wq_prio < PRI_KERNEL)
176 1.33 matt ktf |= KTHREAD_TS;
177 1.18 rmind if (ci) {
178 1.18 rmind error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
179 1.23 martin wq, &q->q_worker, "%s/%u", wq->wq_name, ci->ci_index);
180 1.18 rmind } else {
181 1.18 rmind error = kthread_create(wq->wq_prio, ktf, ci, workqueue_worker,
182 1.18 rmind wq, &q->q_worker, "%s", wq->wq_name);
183 1.18 rmind }
184 1.20 yamt if (error != 0) {
185 1.20 yamt mutex_destroy(&q->q_mutex);
186 1.20 yamt cv_destroy(&q->q_cv);
187 1.20 yamt KASSERT(q->q_worker == NULL);
188 1.20 yamt }
189 1.1 yamt return error;
190 1.1 yamt }
191 1.1 yamt
192 1.5 yamt struct workqueue_exitargs {
193 1.17 yamt work_impl_t wqe_wk;
194 1.5 yamt struct workqueue_queue *wqe_q;
195 1.5 yamt };
196 1.5 yamt
197 1.5 yamt static void
198 1.7 yamt workqueue_exit(struct work *wk, void *arg)
199 1.5 yamt {
200 1.5 yamt struct workqueue_exitargs *wqe = (void *)wk;
201 1.5 yamt struct workqueue_queue *q = wqe->wqe_q;
202 1.5 yamt
203 1.5 yamt /*
204 1.11 yamt * only competition at this point is workqueue_finiqueue.
205 1.5 yamt */
206 1.5 yamt
207 1.13 ad KASSERT(q->q_worker == curlwp);
208 1.34 ozaki KASSERT(SIMPLEQ_EMPTY(&q->q_queue_pending));
209 1.9 ad mutex_enter(&q->q_mutex);
210 1.5 yamt q->q_worker = NULL;
211 1.39 riastrad cv_broadcast(&q->q_cv);
212 1.9 ad mutex_exit(&q->q_mutex);
213 1.5 yamt kthread_exit(0);
214 1.5 yamt }
215 1.5 yamt
216 1.5 yamt static void
217 1.14 rmind workqueue_finiqueue(struct workqueue *wq, struct workqueue_queue *q)
218 1.5 yamt {
219 1.5 yamt struct workqueue_exitargs wqe;
220 1.5 yamt
221 1.20 yamt KASSERT(wq->wq_func == workqueue_exit);
222 1.5 yamt
223 1.5 yamt wqe.wqe_q = q;
224 1.34 ozaki KASSERT(SIMPLEQ_EMPTY(&q->q_queue_pending));
225 1.5 yamt KASSERT(q->q_worker != NULL);
226 1.9 ad mutex_enter(&q->q_mutex);
227 1.34 ozaki SIMPLEQ_INSERT_TAIL(&q->q_queue_pending, &wqe.wqe_wk, wk_entry);
228 1.39 riastrad cv_broadcast(&q->q_cv);
229 1.5 yamt while (q->q_worker != NULL) {
230 1.9 ad cv_wait(&q->q_cv, &q->q_mutex);
231 1.5 yamt }
232 1.9 ad mutex_exit(&q->q_mutex);
233 1.9 ad mutex_destroy(&q->q_mutex);
234 1.9 ad cv_destroy(&q->q_cv);
235 1.5 yamt }
236 1.5 yamt
237 1.1 yamt /* --- */
238 1.1 yamt
239 1.1 yamt int
240 1.1 yamt workqueue_create(struct workqueue **wqp, const char *name,
241 1.1 yamt void (*callback_func)(struct work *, void *), void *callback_arg,
242 1.12 yamt pri_t prio, int ipl, int flags)
243 1.1 yamt {
244 1.1 yamt struct workqueue *wq;
245 1.18 rmind struct workqueue_queue *q;
246 1.18 rmind void *ptr;
247 1.20 yamt int error = 0;
248 1.1 yamt
249 1.25 matt CTASSERT(sizeof(work_impl_t) <= sizeof(struct work));
250 1.17 yamt
251 1.20 yamt ptr = kmem_zalloc(workqueue_size(flags), KM_SLEEP);
252 1.26 rmind wq = (void *)roundup2((uintptr_t)ptr, coherency_unit);
253 1.18 rmind wq->wq_ptr = ptr;
254 1.18 rmind wq->wq_flags = flags;
255 1.1 yamt
256 1.1 yamt workqueue_init(wq, name, callback_func, callback_arg, prio, ipl);
257 1.1 yamt
258 1.14 rmind if (flags & WQ_PERCPU) {
259 1.14 rmind struct cpu_info *ci;
260 1.14 rmind CPU_INFO_ITERATOR cii;
261 1.14 rmind
262 1.14 rmind /* create the work-queue for each CPU */
263 1.14 rmind for (CPU_INFO_FOREACH(cii, ci)) {
264 1.20 yamt q = workqueue_queue_lookup(wq, ci);
265 1.18 rmind error = workqueue_initqueue(wq, q, ipl, ci);
266 1.18 rmind if (error) {
267 1.14 rmind break;
268 1.18 rmind }
269 1.14 rmind }
270 1.14 rmind } else {
271 1.18 rmind /* initialize a work-queue */
272 1.20 yamt q = workqueue_queue_lookup(wq, NULL);
273 1.18 rmind error = workqueue_initqueue(wq, q, ipl, NULL);
274 1.1 yamt }
275 1.18 rmind
276 1.20 yamt if (error != 0) {
277 1.20 yamt workqueue_destroy(wq);
278 1.20 yamt } else {
279 1.20 yamt *wqp = wq;
280 1.15 rmind }
281 1.1 yamt
282 1.20 yamt return error;
283 1.1 yamt }
284 1.1 yamt
285 1.34 ozaki static bool
286 1.34 ozaki workqueue_q_wait(struct workqueue_queue *q, work_impl_t *wk_target)
287 1.34 ozaki {
288 1.34 ozaki work_impl_t *wk;
289 1.34 ozaki bool found = false;
290 1.34 ozaki
291 1.34 ozaki mutex_enter(&q->q_mutex);
292 1.37 ozaki if (q->q_worker == curlwp)
293 1.37 ozaki goto out;
294 1.34 ozaki again:
295 1.34 ozaki SIMPLEQ_FOREACH(wk, &q->q_queue_pending, wk_entry) {
296 1.34 ozaki if (wk == wk_target)
297 1.34 ozaki goto found;
298 1.34 ozaki }
299 1.34 ozaki SIMPLEQ_FOREACH(wk, &q->q_queue_running, wk_entry) {
300 1.34 ozaki if (wk == wk_target)
301 1.34 ozaki goto found;
302 1.34 ozaki }
303 1.34 ozaki found:
304 1.34 ozaki if (wk != NULL) {
305 1.34 ozaki found = true;
306 1.34 ozaki cv_wait(&q->q_cv, &q->q_mutex);
307 1.34 ozaki goto again;
308 1.34 ozaki }
309 1.37 ozaki out:
310 1.34 ozaki mutex_exit(&q->q_mutex);
311 1.34 ozaki
312 1.34 ozaki return found;
313 1.34 ozaki }
314 1.34 ozaki
315 1.34 ozaki /*
316 1.34 ozaki * Wait for a specified work to finish. The caller must ensure that no new
317 1.34 ozaki * work will be enqueued before calling workqueue_wait. Note that if the
318 1.34 ozaki * workqueue is WQ_PERCPU, the caller can enqueue a new work to another queue
319 1.34 ozaki * other than the waiting queue.
320 1.34 ozaki */
321 1.34 ozaki void
322 1.34 ozaki workqueue_wait(struct workqueue *wq, struct work *wk)
323 1.34 ozaki {
324 1.34 ozaki struct workqueue_queue *q;
325 1.34 ozaki bool found;
326 1.34 ozaki
327 1.40 riastrad ASSERT_SLEEPABLE();
328 1.40 riastrad
329 1.34 ozaki if (ISSET(wq->wq_flags, WQ_PERCPU)) {
330 1.34 ozaki struct cpu_info *ci;
331 1.34 ozaki CPU_INFO_ITERATOR cii;
332 1.34 ozaki for (CPU_INFO_FOREACH(cii, ci)) {
333 1.34 ozaki q = workqueue_queue_lookup(wq, ci);
334 1.34 ozaki found = workqueue_q_wait(q, (work_impl_t *)wk);
335 1.34 ozaki if (found)
336 1.34 ozaki break;
337 1.34 ozaki }
338 1.34 ozaki } else {
339 1.34 ozaki q = workqueue_queue_lookup(wq, NULL);
340 1.34 ozaki (void) workqueue_q_wait(q, (work_impl_t *)wk);
341 1.34 ozaki }
342 1.34 ozaki }
343 1.34 ozaki
344 1.1 yamt void
345 1.5 yamt workqueue_destroy(struct workqueue *wq)
346 1.5 yamt {
347 1.14 rmind struct workqueue_queue *q;
348 1.20 yamt struct cpu_info *ci;
349 1.20 yamt CPU_INFO_ITERATOR cii;
350 1.5 yamt
351 1.40 riastrad ASSERT_SLEEPABLE();
352 1.40 riastrad
353 1.20 yamt wq->wq_func = workqueue_exit;
354 1.20 yamt for (CPU_INFO_FOREACH(cii, ci)) {
355 1.20 yamt q = workqueue_queue_lookup(wq, ci);
356 1.20 yamt if (q->q_worker != NULL) {
357 1.18 rmind workqueue_finiqueue(wq, q);
358 1.18 rmind }
359 1.14 rmind }
360 1.20 yamt kmem_free(wq->wq_ptr, workqueue_size(wq->wq_flags));
361 1.5 yamt }
362 1.5 yamt
363 1.35 ozaki #ifdef DEBUG
364 1.35 ozaki static void
365 1.35 ozaki workqueue_check_duplication(struct workqueue_queue *q, work_impl_t *wk)
366 1.35 ozaki {
367 1.35 ozaki work_impl_t *_wk;
368 1.35 ozaki
369 1.35 ozaki SIMPLEQ_FOREACH(_wk, &q->q_queue_pending, wk_entry) {
370 1.35 ozaki if (_wk == wk)
371 1.35 ozaki panic("%s: tried to enqueue a queued work", __func__);
372 1.35 ozaki }
373 1.35 ozaki }
374 1.35 ozaki #endif
375 1.35 ozaki
376 1.5 yamt void
377 1.17 yamt workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
378 1.1 yamt {
379 1.14 rmind struct workqueue_queue *q;
380 1.17 yamt work_impl_t *wk = (void *)wk0;
381 1.14 rmind
382 1.18 rmind KASSERT(wq->wq_flags & WQ_PERCPU || ci == NULL);
383 1.14 rmind q = workqueue_queue_lookup(wq, ci);
384 1.1 yamt
385 1.9 ad mutex_enter(&q->q_mutex);
386 1.35 ozaki #ifdef DEBUG
387 1.35 ozaki workqueue_check_duplication(q, wk);
388 1.35 ozaki #endif
389 1.34 ozaki SIMPLEQ_INSERT_TAIL(&q->q_queue_pending, wk, wk_entry);
390 1.39 riastrad cv_broadcast(&q->q_cv);
391 1.9 ad mutex_exit(&q->q_mutex);
392 1.1 yamt }
393