linux_tasklet.c revision 1.8 1 1.8 riastrad /* $NetBSD: linux_tasklet.c,v 1.8 2021/12/19 11:57:34 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.8 riastrad * Copyright (c) 2018, 2020, 2021 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.8 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_tasklet.c,v 1.8 2021/12/19 11:57:34 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/atomic.h>
37 1.1 riastrad #include <sys/cpu.h>
38 1.1 riastrad #include <sys/errno.h>
39 1.1 riastrad #include <sys/intr.h>
40 1.8 riastrad #include <sys/kmem.h>
41 1.1 riastrad #include <sys/lock.h>
42 1.1 riastrad #include <sys/percpu.h>
43 1.1 riastrad #include <sys/queue.h>
44 1.1 riastrad
45 1.1 riastrad #include <lib/libkern/libkern.h>
46 1.1 riastrad
47 1.1 riastrad #include <machine/limits.h>
48 1.1 riastrad
49 1.1 riastrad #include <linux/tasklet.h>
50 1.1 riastrad
51 1.1 riastrad #define TASKLET_SCHEDULED ((unsigned)__BIT(0))
52 1.1 riastrad #define TASKLET_RUNNING ((unsigned)__BIT(1))
53 1.1 riastrad
54 1.1 riastrad struct tasklet_queue {
55 1.8 riastrad struct percpu *tq_percpu; /* struct tasklet_cpu * */
56 1.1 riastrad void *tq_sih;
57 1.1 riastrad };
58 1.1 riastrad
59 1.1 riastrad SIMPLEQ_HEAD(tasklet_head, tasklet_struct);
60 1.1 riastrad
61 1.1 riastrad struct tasklet_cpu {
62 1.1 riastrad struct tasklet_head tc_head;
63 1.1 riastrad };
64 1.1 riastrad
65 1.1 riastrad static struct tasklet_queue tasklet_queue __read_mostly;
66 1.1 riastrad static struct tasklet_queue tasklet_hi_queue __read_mostly;
67 1.1 riastrad
68 1.1 riastrad static void tasklet_softintr(void *);
69 1.1 riastrad static int tasklet_queue_init(struct tasklet_queue *, unsigned);
70 1.1 riastrad static void tasklet_queue_fini(struct tasklet_queue *);
71 1.1 riastrad static void tasklet_queue_schedule(struct tasklet_queue *,
72 1.1 riastrad struct tasklet_struct *);
73 1.1 riastrad static void tasklet_queue_enqueue(struct tasklet_queue *,
74 1.1 riastrad struct tasklet_struct *);
75 1.1 riastrad
76 1.1 riastrad /*
77 1.1 riastrad * linux_tasklets_init()
78 1.1 riastrad *
79 1.1 riastrad * Initialize the Linux tasklets subsystem. Return 0 on success,
80 1.1 riastrad * error code on failure.
81 1.1 riastrad */
82 1.1 riastrad int
83 1.1 riastrad linux_tasklets_init(void)
84 1.1 riastrad {
85 1.1 riastrad int error;
86 1.1 riastrad
87 1.1 riastrad error = tasklet_queue_init(&tasklet_queue, SOFTINT_CLOCK);
88 1.1 riastrad if (error)
89 1.1 riastrad goto fail0;
90 1.1 riastrad error = tasklet_queue_init(&tasklet_hi_queue, SOFTINT_SERIAL);
91 1.1 riastrad if (error)
92 1.1 riastrad goto fail1;
93 1.1 riastrad
94 1.1 riastrad /* Success! */
95 1.1 riastrad return 0;
96 1.1 riastrad
97 1.1 riastrad fail2: __unused
98 1.1 riastrad tasklet_queue_fini(&tasklet_hi_queue);
99 1.1 riastrad fail1: tasklet_queue_fini(&tasklet_queue);
100 1.1 riastrad fail0: KASSERT(error);
101 1.1 riastrad return error;
102 1.1 riastrad }
103 1.1 riastrad
104 1.1 riastrad /*
105 1.1 riastrad * linux_tasklets_fini()
106 1.1 riastrad *
107 1.1 riastrad * Finalize the Linux tasklets subsystem. All use of tasklets
108 1.1 riastrad * must be done.
109 1.1 riastrad */
110 1.1 riastrad void
111 1.1 riastrad linux_tasklets_fini(void)
112 1.1 riastrad {
113 1.1 riastrad
114 1.1 riastrad tasklet_queue_fini(&tasklet_hi_queue);
115 1.1 riastrad tasklet_queue_fini(&tasklet_queue);
116 1.1 riastrad }
117 1.1 riastrad
118 1.8 riastrad static void
119 1.8 riastrad tasklet_cpu_init(void *ptr, void *cookie, struct cpu_info *ci)
120 1.8 riastrad {
121 1.8 riastrad struct tasklet_cpu **tcp = ptr, *tc;
122 1.8 riastrad
123 1.8 riastrad *tcp = tc = kmem_zalloc(sizeof(*tc), KM_SLEEP);
124 1.8 riastrad SIMPLEQ_INIT(&tc->tc_head);
125 1.8 riastrad }
126 1.8 riastrad
127 1.8 riastrad static void
128 1.8 riastrad tasklet_cpu_fini(void *ptr, void *cookie, struct cpu_info *ci)
129 1.8 riastrad {
130 1.8 riastrad struct tasklet_cpu **tcp = ptr, *tc = *tcp;
131 1.8 riastrad
132 1.8 riastrad KASSERT(SIMPLEQ_EMPTY(&tc->tc_head));
133 1.8 riastrad kmem_free(tc, sizeof(*tc));
134 1.8 riastrad *tcp = NULL; /* paranoia */
135 1.8 riastrad }
136 1.8 riastrad
137 1.1 riastrad /*
138 1.1 riastrad * tasklet_queue_init(tq, prio)
139 1.1 riastrad *
140 1.1 riastrad * Initialize the tasklet queue tq for running tasklets at softint
141 1.1 riastrad * priority prio (SOFTINT_*).
142 1.1 riastrad */
143 1.1 riastrad static int
144 1.1 riastrad tasklet_queue_init(struct tasklet_queue *tq, unsigned prio)
145 1.1 riastrad {
146 1.1 riastrad int error;
147 1.1 riastrad
148 1.1 riastrad /* Allocate per-CPU memory. percpu_alloc cannot fail. */
149 1.8 riastrad tq->tq_percpu = percpu_create(sizeof(struct tasklet_cpu),
150 1.8 riastrad tasklet_cpu_init, tasklet_cpu_fini, NULL);
151 1.1 riastrad KASSERT(tq->tq_percpu != NULL);
152 1.1 riastrad
153 1.1 riastrad /* Try to establish a softint. softint_establish may fail. */
154 1.1 riastrad tq->tq_sih = softint_establish(prio|SOFTINT_MPSAFE, &tasklet_softintr,
155 1.1 riastrad tq);
156 1.1 riastrad if (tq->tq_sih == NULL) {
157 1.1 riastrad error = ENOMEM;
158 1.1 riastrad goto fail1;
159 1.1 riastrad }
160 1.1 riastrad
161 1.1 riastrad /* Success! */
162 1.1 riastrad return 0;
163 1.1 riastrad
164 1.1 riastrad fail2: __unused
165 1.1 riastrad softint_disestablish(tq->tq_sih);
166 1.1 riastrad tq->tq_sih = NULL;
167 1.1 riastrad fail1: percpu_free(tq->tq_percpu, sizeof(struct tasklet_cpu));
168 1.1 riastrad tq->tq_percpu = NULL;
169 1.1 riastrad fail0: __unused
170 1.1 riastrad KASSERT(error);
171 1.1 riastrad return error;
172 1.1 riastrad }
173 1.1 riastrad
174 1.1 riastrad /*
175 1.1 riastrad * tasklet_queue_fini(tq)
176 1.1 riastrad *
177 1.1 riastrad * Finalize the tasklet queue tq: free all resources associated
178 1.1 riastrad * with it.
179 1.1 riastrad */
180 1.1 riastrad static void
181 1.1 riastrad tasklet_queue_fini(struct tasklet_queue *tq)
182 1.1 riastrad {
183 1.1 riastrad
184 1.1 riastrad softint_disestablish(tq->tq_sih);
185 1.1 riastrad tq->tq_sih = NULL;
186 1.1 riastrad percpu_free(tq->tq_percpu, sizeof(struct tasklet_cpu));
187 1.1 riastrad tq->tq_percpu = NULL;
188 1.1 riastrad }
189 1.1 riastrad
190 1.1 riastrad /*
191 1.1 riastrad * tasklet_softintr(cookie)
192 1.1 riastrad *
193 1.1 riastrad * Soft interrupt handler: Process queued tasklets on the tasklet
194 1.1 riastrad * queue passed in as cookie.
195 1.1 riastrad */
196 1.1 riastrad static void
197 1.1 riastrad tasklet_softintr(void *cookie)
198 1.1 riastrad {
199 1.1 riastrad struct tasklet_queue *const tq = cookie;
200 1.1 riastrad struct tasklet_head th = SIMPLEQ_HEAD_INITIALIZER(th);
201 1.8 riastrad struct tasklet_cpu **tcp, *tc;
202 1.1 riastrad int s;
203 1.1 riastrad
204 1.1 riastrad /*
205 1.1 riastrad * With all interrupts deferred, transfer the current CPU's
206 1.1 riastrad * queue of tasklets to a local variable in one swell foop.
207 1.1 riastrad *
208 1.1 riastrad * No memory barriers: CPU-local state only.
209 1.1 riastrad */
210 1.8 riastrad tcp = percpu_getref(tq->tq_percpu);
211 1.8 riastrad tc = *tcp;
212 1.1 riastrad s = splhigh();
213 1.1 riastrad SIMPLEQ_CONCAT(&th, &tc->tc_head);
214 1.1 riastrad splx(s);
215 1.1 riastrad percpu_putref(tq->tq_percpu);
216 1.1 riastrad
217 1.1 riastrad /* Go through the queue of tasklets we grabbed. */
218 1.1 riastrad while (!SIMPLEQ_EMPTY(&th)) {
219 1.1 riastrad struct tasklet_struct *tasklet;
220 1.1 riastrad
221 1.1 riastrad /* Remove the first tasklet from the queue. */
222 1.1 riastrad tasklet = SIMPLEQ_FIRST(&th);
223 1.1 riastrad SIMPLEQ_REMOVE_HEAD(&th, tl_entry);
224 1.1 riastrad
225 1.5 riastrad KASSERT(atomic_load_relaxed(&tasklet->tl_state) &
226 1.5 riastrad TASKLET_SCHEDULED);
227 1.5 riastrad
228 1.1 riastrad /*
229 1.1 riastrad * Test and set RUNNING, in case it is already running
230 1.1 riastrad * on another CPU and got scheduled again on this one
231 1.1 riastrad * before it completed.
232 1.1 riastrad */
233 1.5 riastrad if (!tasklet_trylock(tasklet)) {
234 1.1 riastrad /*
235 1.1 riastrad * Put it back on the queue to run it again in
236 1.1 riastrad * a sort of busy-wait, and move on to the next
237 1.1 riastrad * one.
238 1.1 riastrad */
239 1.1 riastrad tasklet_queue_enqueue(tq, tasklet);
240 1.1 riastrad continue;
241 1.1 riastrad }
242 1.1 riastrad
243 1.5 riastrad /*
244 1.5 riastrad * Check whether it's currently disabled.
245 1.5 riastrad *
246 1.5 riastrad * Pairs with membar_exit in __tasklet_enable.
247 1.5 riastrad */
248 1.5 riastrad if (atomic_load_acquire(&tasklet->tl_disablecount)) {
249 1.1 riastrad /*
250 1.1 riastrad * Disabled: clear the RUNNING bit and, requeue
251 1.1 riastrad * it, but keep it SCHEDULED.
252 1.1 riastrad */
253 1.5 riastrad tasklet_unlock(tasklet);
254 1.1 riastrad tasklet_queue_enqueue(tq, tasklet);
255 1.1 riastrad continue;
256 1.1 riastrad }
257 1.1 riastrad
258 1.1 riastrad /* Not disabled. Clear SCHEDULED and call func. */
259 1.5 riastrad KASSERT(atomic_load_relaxed(&tasklet->tl_state) &
260 1.5 riastrad TASKLET_SCHEDULED);
261 1.1 riastrad atomic_and_uint(&tasklet->tl_state, ~TASKLET_SCHEDULED);
262 1.1 riastrad
263 1.1 riastrad (*tasklet->func)(tasklet->data);
264 1.1 riastrad
265 1.1 riastrad /* Clear RUNNING to notify tasklet_disable. */
266 1.5 riastrad tasklet_unlock(tasklet);
267 1.1 riastrad }
268 1.1 riastrad }
269 1.1 riastrad
270 1.1 riastrad /*
271 1.1 riastrad * tasklet_queue_schedule(tq, tasklet)
272 1.1 riastrad *
273 1.1 riastrad * Schedule tasklet to run on tq. If it was already scheduled and
274 1.1 riastrad * has not yet run, no effect.
275 1.1 riastrad */
276 1.1 riastrad static void
277 1.1 riastrad tasklet_queue_schedule(struct tasklet_queue *tq,
278 1.1 riastrad struct tasklet_struct *tasklet)
279 1.1 riastrad {
280 1.1 riastrad unsigned ostate, nstate;
281 1.1 riastrad
282 1.1 riastrad /* Test and set the SCHEDULED bit. If already set, we're done. */
283 1.1 riastrad do {
284 1.5 riastrad ostate = atomic_load_relaxed(&tasklet->tl_state);
285 1.1 riastrad if (ostate & TASKLET_SCHEDULED)
286 1.1 riastrad return;
287 1.1 riastrad nstate = ostate | TASKLET_SCHEDULED;
288 1.1 riastrad } while (atomic_cas_uint(&tasklet->tl_state, ostate, nstate)
289 1.1 riastrad != ostate);
290 1.1 riastrad
291 1.1 riastrad /*
292 1.1 riastrad * Not already set and we have set it now. Put it on the queue
293 1.1 riastrad * and kick off a softint.
294 1.1 riastrad */
295 1.1 riastrad tasklet_queue_enqueue(tq, tasklet);
296 1.1 riastrad }
297 1.1 riastrad
298 1.1 riastrad /*
299 1.1 riastrad * tasklet_queue_enqueue(tq, tasklet)
300 1.1 riastrad *
301 1.1 riastrad * Put tasklet on the queue tq and ensure it will run. tasklet
302 1.1 riastrad * must be marked SCHEDULED.
303 1.1 riastrad */
304 1.1 riastrad static void
305 1.1 riastrad tasklet_queue_enqueue(struct tasklet_queue *tq, struct tasklet_struct *tasklet)
306 1.1 riastrad {
307 1.8 riastrad struct tasklet_cpu **tcp, *tc;
308 1.1 riastrad int s;
309 1.1 riastrad
310 1.5 riastrad KASSERT(atomic_load_relaxed(&tasklet->tl_state) & TASKLET_SCHEDULED);
311 1.1 riastrad
312 1.1 riastrad /*
313 1.1 riastrad * Insert on the current CPU's queue while all interrupts are
314 1.1 riastrad * blocked, and schedule a soft interrupt to process it. No
315 1.1 riastrad * memory barriers: CPU-local state only.
316 1.1 riastrad */
317 1.8 riastrad tcp = percpu_getref(tq->tq_percpu);
318 1.8 riastrad tc = *tcp;
319 1.1 riastrad s = splhigh();
320 1.1 riastrad SIMPLEQ_INSERT_TAIL(&tc->tc_head, tasklet, tl_entry);
321 1.1 riastrad splx(s);
322 1.1 riastrad softint_schedule(tq->tq_sih);
323 1.1 riastrad percpu_putref(tq->tq_percpu);
324 1.1 riastrad }
325 1.1 riastrad
326 1.1 riastrad /*
327 1.1 riastrad * tasklet_init(tasklet, func, data)
328 1.1 riastrad *
329 1.1 riastrad * Initialize tasklet to call func(data) when scheduled.
330 1.1 riastrad *
331 1.1 riastrad * Caller is responsible for issuing the appropriate memory
332 1.1 riastrad * barriers or store releases to publish the tasklet to other CPUs
333 1.1 riastrad * before use.
334 1.1 riastrad */
335 1.1 riastrad void
336 1.1 riastrad tasklet_init(struct tasklet_struct *tasklet, void (*func)(unsigned long),
337 1.1 riastrad unsigned long data)
338 1.1 riastrad {
339 1.1 riastrad
340 1.5 riastrad atomic_store_relaxed(&tasklet->tl_state, 0);
341 1.5 riastrad atomic_store_relaxed(&tasklet->tl_disablecount, 0);
342 1.1 riastrad tasklet->func = func;
343 1.1 riastrad tasklet->data = data;
344 1.1 riastrad }
345 1.1 riastrad
346 1.1 riastrad /*
347 1.1 riastrad * tasklet_schedule(tasklet)
348 1.1 riastrad *
349 1.1 riastrad * Schedule tasklet to run at regular priority. If it was already
350 1.1 riastrad * scheduled and has not yet run, no effect.
351 1.1 riastrad */
352 1.1 riastrad void
353 1.1 riastrad tasklet_schedule(struct tasklet_struct *tasklet)
354 1.1 riastrad {
355 1.1 riastrad
356 1.1 riastrad tasklet_queue_schedule(&tasklet_queue, tasklet);
357 1.1 riastrad }
358 1.1 riastrad
359 1.1 riastrad /*
360 1.1 riastrad * tasklet_hi_schedule(tasklet)
361 1.1 riastrad *
362 1.1 riastrad * Schedule tasklet to run at high priority. If it was already
363 1.1 riastrad * scheduled and has not yet run, no effect.
364 1.1 riastrad */
365 1.1 riastrad void
366 1.1 riastrad tasklet_hi_schedule(struct tasklet_struct *tasklet)
367 1.1 riastrad {
368 1.1 riastrad
369 1.1 riastrad tasklet_queue_schedule(&tasklet_hi_queue, tasklet);
370 1.1 riastrad }
371 1.1 riastrad
372 1.1 riastrad /*
373 1.7 riastrad * tasklet_disable_nosync(tasklet)
374 1.1 riastrad *
375 1.7 riastrad * Increment the disable count of tasklet, but don't wait for it
376 1.7 riastrad * to complete -- it may remain running after this returns.
377 1.1 riastrad *
378 1.1 riastrad * As long as the disable count is nonzero, the tasklet's function
379 1.1 riastrad * will not run, but if already scheduled, the tasklet will remain
380 1.1 riastrad * so and the softint will repeatedly trigger itself in a sort of
381 1.1 riastrad * busy-wait, so this should be used only for short durations.
382 1.1 riastrad *
383 1.5 riastrad * Load-acquire semantics.
384 1.1 riastrad */
385 1.1 riastrad void
386 1.7 riastrad tasklet_disable_nosync(struct tasklet_struct *tasklet)
387 1.1 riastrad {
388 1.1 riastrad unsigned int disablecount __diagused;
389 1.1 riastrad
390 1.1 riastrad /* Increment the disable count. */
391 1.1 riastrad disablecount = atomic_inc_uint_nv(&tasklet->tl_disablecount);
392 1.1 riastrad KASSERT(disablecount < UINT_MAX);
393 1.2 riastrad KASSERT(disablecount != 0);
394 1.1 riastrad
395 1.6 riastrad /* Pairs with membar_exit in __tasklet_enable. */
396 1.6 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
397 1.6 riastrad membar_enter();
398 1.6 riastrad #endif
399 1.7 riastrad }
400 1.7 riastrad
401 1.7 riastrad /*
402 1.7 riastrad * tasklet_disable(tasklet)
403 1.7 riastrad *
404 1.7 riastrad * Increment the disable count of tasklet, and if it was already
405 1.7 riastrad * running, busy-wait for it to complete.
406 1.7 riastrad *
407 1.7 riastrad * As long as the disable count is nonzero, the tasklet's function
408 1.7 riastrad * will not run, but if already scheduled, the tasklet will remain
409 1.7 riastrad * so and the softint will repeatedly trigger itself in a sort of
410 1.7 riastrad * busy-wait, so this should be used only for short durations.
411 1.7 riastrad *
412 1.7 riastrad * If tasklet is guaranteed not to be scheduled, e.g. if you have
413 1.7 riastrad * just invoked tasklet_kill, then tasklet_disable serves to wait
414 1.7 riastrad * for it to complete in case it might already be running.
415 1.7 riastrad *
416 1.7 riastrad * Load-acquire semantics.
417 1.7 riastrad */
418 1.7 riastrad void
419 1.7 riastrad tasklet_disable(struct tasklet_struct *tasklet)
420 1.7 riastrad {
421 1.7 riastrad
422 1.7 riastrad /* Increment the disable count. */
423 1.7 riastrad tasklet_disable_nosync(tasklet);
424 1.6 riastrad
425 1.1 riastrad /* Wait for it to finish running, if it was running. */
426 1.5 riastrad tasklet_unlock_wait(tasklet);
427 1.1 riastrad }
428 1.1 riastrad
429 1.1 riastrad /*
430 1.1 riastrad * tasklet_enable(tasklet)
431 1.1 riastrad *
432 1.1 riastrad * Decrement tasklet's disable count. If it was previously
433 1.1 riastrad * scheduled to run, it may now run.
434 1.5 riastrad *
435 1.5 riastrad * Store-release semantics.
436 1.1 riastrad */
437 1.1 riastrad void
438 1.1 riastrad tasklet_enable(struct tasklet_struct *tasklet)
439 1.1 riastrad {
440 1.1 riastrad
441 1.5 riastrad (void)__tasklet_enable(tasklet);
442 1.1 riastrad }
443 1.1 riastrad
444 1.1 riastrad /*
445 1.1 riastrad * tasklet_kill(tasklet)
446 1.1 riastrad *
447 1.1 riastrad * Busy-wait for tasklet to run, if it is currently scheduled.
448 1.1 riastrad * Caller must guarantee it does not get scheduled again for this
449 1.1 riastrad * to be useful.
450 1.1 riastrad */
451 1.1 riastrad void
452 1.1 riastrad tasklet_kill(struct tasklet_struct *tasklet)
453 1.1 riastrad {
454 1.1 riastrad
455 1.1 riastrad KASSERTMSG(!cpu_intr_p(),
456 1.1 riastrad "deadlock: soft interrupts are blocked in interrupt context");
457 1.1 riastrad
458 1.1 riastrad /* Wait for it to be removed from the queue. */
459 1.5 riastrad while (atomic_load_relaxed(&tasklet->tl_state) & TASKLET_SCHEDULED)
460 1.1 riastrad SPINLOCK_BACKOFF_HOOK;
461 1.1 riastrad
462 1.1 riastrad /*
463 1.1 riastrad * No need for a memory barrier here because writes to the
464 1.1 riastrad * single state word are globally ordered, and RUNNING is set
465 1.1 riastrad * before SCHEDULED is cleared, so as long as the caller
466 1.1 riastrad * guarantees no scheduling, the only possible transitions we
467 1.1 riastrad * can witness are:
468 1.1 riastrad *
469 1.1 riastrad * 0 -> 0
470 1.1 riastrad * SCHEDULED -> 0
471 1.1 riastrad * SCHEDULED -> RUNNING
472 1.1 riastrad * RUNNING -> 0
473 1.1 riastrad * RUNNING -> RUNNING
474 1.1 riastrad * SCHEDULED|RUNNING -> 0
475 1.1 riastrad * SCHEDULED|RUNNING -> RUNNING
476 1.1 riastrad */
477 1.1 riastrad
478 1.1 riastrad /* Wait for it to finish running. */
479 1.5 riastrad tasklet_unlock_wait(tasklet);
480 1.5 riastrad }
481 1.5 riastrad
482 1.5 riastrad /*
483 1.5 riastrad * tasklet_is_scheduled(tasklet)
484 1.5 riastrad *
485 1.5 riastrad * True if tasklet is currently locked. Caller must use it only
486 1.5 riastrad * for positive assertions.
487 1.5 riastrad */
488 1.5 riastrad bool
489 1.5 riastrad tasklet_is_locked(const struct tasklet_struct *tasklet)
490 1.5 riastrad {
491 1.5 riastrad
492 1.5 riastrad return atomic_load_relaxed(&tasklet->tl_state) & TASKLET_RUNNING;
493 1.5 riastrad }
494 1.5 riastrad
495 1.5 riastrad /*
496 1.5 riastrad * tasklet_trylock(tasklet)
497 1.5 riastrad *
498 1.5 riastrad * Try to lock tasklet, i.e., set TASKLET_RUNNING. Return true if
499 1.5 riastrad * we locked it, false if already locked.
500 1.5 riastrad *
501 1.5 riastrad * Load-acquire semantics.
502 1.5 riastrad */
503 1.5 riastrad bool
504 1.5 riastrad tasklet_trylock(struct tasklet_struct *tasklet)
505 1.5 riastrad {
506 1.5 riastrad unsigned state;
507 1.5 riastrad
508 1.5 riastrad do {
509 1.6 riastrad state = atomic_load_relaxed(&tasklet->tl_state);
510 1.5 riastrad if (state & TASKLET_RUNNING)
511 1.5 riastrad return false;
512 1.5 riastrad } while (atomic_cas_uint(&tasklet->tl_state, state,
513 1.5 riastrad state | TASKLET_RUNNING) != state);
514 1.5 riastrad
515 1.6 riastrad /* Pairs with membar_exit in tasklet_unlock. */
516 1.6 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
517 1.6 riastrad membar_enter();
518 1.6 riastrad #endif
519 1.6 riastrad
520 1.5 riastrad return true;
521 1.5 riastrad }
522 1.5 riastrad
523 1.5 riastrad /*
524 1.5 riastrad * tasklet_unlock(tasklet)
525 1.5 riastrad *
526 1.5 riastrad * Unlock tasklet, i.e., clear TASKLET_RUNNING.
527 1.5 riastrad *
528 1.5 riastrad * Store-release semantics.
529 1.5 riastrad */
530 1.5 riastrad void
531 1.5 riastrad tasklet_unlock(struct tasklet_struct *tasklet)
532 1.5 riastrad {
533 1.5 riastrad
534 1.5 riastrad KASSERT(atomic_load_relaxed(&tasklet->tl_state) & TASKLET_RUNNING);
535 1.1 riastrad
536 1.1 riastrad /*
537 1.6 riastrad * Pairs with membar_enter in tasklet_trylock and with
538 1.6 riastrad * atomic_load_acquire in tasklet_unlock_wait.
539 1.1 riastrad */
540 1.5 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
541 1.5 riastrad membar_exit();
542 1.5 riastrad #endif
543 1.5 riastrad atomic_and_uint(&tasklet->tl_state, ~TASKLET_RUNNING);
544 1.5 riastrad }
545 1.5 riastrad
546 1.5 riastrad /*
547 1.5 riastrad * tasklet_unlock_wait(tasklet)
548 1.5 riastrad *
549 1.5 riastrad * Busy-wait until tasklet is not running.
550 1.5 riastrad *
551 1.5 riastrad * Load-acquire semantics.
552 1.5 riastrad */
553 1.5 riastrad void
554 1.5 riastrad tasklet_unlock_wait(const struct tasklet_struct *tasklet)
555 1.5 riastrad {
556 1.5 riastrad
557 1.5 riastrad /* Pairs with membar_exit in tasklet_unlock. */
558 1.5 riastrad while (atomic_load_acquire(&tasklet->tl_state) & TASKLET_RUNNING)
559 1.5 riastrad SPINLOCK_BACKOFF_HOOK;
560 1.1 riastrad }
561 1.3 riastrad
562 1.3 riastrad /*
563 1.5 riastrad * BEGIN I915 HACKS
564 1.5 riastrad *
565 1.5 riastrad * The i915 driver abuses the tasklet abstraction like a cop abuses his
566 1.5 riastrad * wife.
567 1.5 riastrad */
568 1.5 riastrad
569 1.5 riastrad /*
570 1.5 riastrad * __tasklet_disable_sync_once(tasklet)
571 1.3 riastrad *
572 1.3 riastrad * Increment the disable count of tasklet, and if this is the
573 1.3 riastrad * first time it was disabled and it was already running,
574 1.3 riastrad * busy-wait for it to complete.
575 1.3 riastrad *
576 1.3 riastrad * Caller must not care about whether the tasklet is running, or
577 1.3 riastrad * about waiting for any side effects of the tasklet to complete,
578 1.3 riastrad * if this was not the first time it was disabled.
579 1.3 riastrad */
580 1.3 riastrad void
581 1.5 riastrad __tasklet_disable_sync_once(struct tasklet_struct *tasklet)
582 1.3 riastrad {
583 1.3 riastrad unsigned int disablecount;
584 1.3 riastrad
585 1.3 riastrad /* Increment the disable count. */
586 1.3 riastrad disablecount = atomic_inc_uint_nv(&tasklet->tl_disablecount);
587 1.3 riastrad KASSERT(disablecount < UINT_MAX);
588 1.3 riastrad KASSERT(disablecount != 0);
589 1.3 riastrad
590 1.6 riastrad /* Pairs with membar_exit in __tasklet_enable_sync_once. */
591 1.6 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
592 1.6 riastrad membar_enter();
593 1.6 riastrad #endif
594 1.6 riastrad
595 1.3 riastrad /*
596 1.3 riastrad * If it was zero, wait for it to finish running. If it was
597 1.3 riastrad * not zero, caller must not care whether it was running.
598 1.3 riastrad */
599 1.5 riastrad if (disablecount == 1)
600 1.5 riastrad tasklet_unlock_wait(tasklet);
601 1.3 riastrad }
602 1.3 riastrad
603 1.3 riastrad /*
604 1.5 riastrad * __tasklet_enable_sync_once(tasklet)
605 1.3 riastrad *
606 1.3 riastrad * Decrement the disable count of tasklet, and if it goes to zero,
607 1.3 riastrad * kill tasklet.
608 1.3 riastrad */
609 1.3 riastrad void
610 1.5 riastrad __tasklet_enable_sync_once(struct tasklet_struct *tasklet)
611 1.3 riastrad {
612 1.3 riastrad unsigned int disablecount;
613 1.3 riastrad
614 1.6 riastrad /* Pairs with membar_enter in __tasklet_disable_sync_once. */
615 1.6 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
616 1.6 riastrad membar_exit();
617 1.6 riastrad #endif
618 1.6 riastrad
619 1.3 riastrad /* Decrement the disable count. */
620 1.3 riastrad disablecount = atomic_dec_uint_nv(&tasklet->tl_disablecount);
621 1.3 riastrad KASSERT(disablecount < UINT_MAX);
622 1.3 riastrad
623 1.3 riastrad /*
624 1.3 riastrad * If it became zero, kill the tasklet. If it was not zero,
625 1.3 riastrad * caller must not care whether it was running.
626 1.3 riastrad */
627 1.3 riastrad if (disablecount == 0)
628 1.3 riastrad tasklet_kill(tasklet);
629 1.3 riastrad }
630 1.3 riastrad
631 1.3 riastrad /*
632 1.5 riastrad * __tasklet_is_enabled(tasklet)
633 1.3 riastrad *
634 1.3 riastrad * True if tasklet is not currently disabled. Answer may be stale
635 1.3 riastrad * as soon as it is returned -- caller must use it only as a hint,
636 1.3 riastrad * or must arrange synchronization externally.
637 1.3 riastrad */
638 1.3 riastrad bool
639 1.5 riastrad __tasklet_is_enabled(const struct tasklet_struct *tasklet)
640 1.3 riastrad {
641 1.3 riastrad unsigned int disablecount;
642 1.3 riastrad
643 1.5 riastrad disablecount = atomic_load_relaxed(&tasklet->tl_disablecount);
644 1.5 riastrad
645 1.5 riastrad return (disablecount == 0);
646 1.5 riastrad }
647 1.5 riastrad
648 1.5 riastrad /*
649 1.5 riastrad * __tasklet_is_scheduled(tasklet)
650 1.5 riastrad *
651 1.5 riastrad * True if tasklet is currently scheduled. Answer may be stale as
652 1.5 riastrad * soon as it is returned -- caller must use it only as a hint, or
653 1.5 riastrad * must arrange synchronization externally.
654 1.5 riastrad */
655 1.5 riastrad bool
656 1.5 riastrad __tasklet_is_scheduled(const struct tasklet_struct *tasklet)
657 1.5 riastrad {
658 1.5 riastrad
659 1.5 riastrad return atomic_load_relaxed(&tasklet->tl_state) & TASKLET_SCHEDULED;
660 1.5 riastrad }
661 1.5 riastrad
662 1.5 riastrad /*
663 1.5 riastrad * __tasklet_enable(tasklet)
664 1.5 riastrad *
665 1.5 riastrad * Decrement tasklet's disable count. If it was previously
666 1.5 riastrad * scheduled to run, it may now run. Return true if the disable
667 1.5 riastrad * count went down to zero; otherwise return false.
668 1.5 riastrad *
669 1.5 riastrad * Store-release semantics.
670 1.5 riastrad */
671 1.5 riastrad bool
672 1.5 riastrad __tasklet_enable(struct tasklet_struct *tasklet)
673 1.5 riastrad {
674 1.5 riastrad unsigned int disablecount;
675 1.5 riastrad
676 1.5 riastrad /*
677 1.5 riastrad * Guarantee all caller-relevant reads or writes have completed
678 1.5 riastrad * before potentially allowing tasklet to run again by
679 1.5 riastrad * decrementing the disable count.
680 1.5 riastrad *
681 1.6 riastrad * Pairs with atomic_load_acquire in tasklet_softintr and with
682 1.6 riastrad * membar_enter in tasklet_disable.
683 1.5 riastrad */
684 1.5 riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
685 1.5 riastrad membar_exit();
686 1.5 riastrad #endif
687 1.5 riastrad
688 1.5 riastrad /* Decrement the disable count. */
689 1.5 riastrad disablecount = atomic_dec_uint_nv(&tasklet->tl_disablecount);
690 1.5 riastrad KASSERT(disablecount != UINT_MAX);
691 1.3 riastrad
692 1.3 riastrad return (disablecount == 0);
693 1.3 riastrad }
694