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