kern_timeout.c revision 1.36 1 1.36 ad /* $NetBSD: kern_timeout.c,v 1.36 2008/04/22 11:45:28 ad Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.32 ad * Copyright (c) 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.22 ad * by Jason R. Thorpe, and by Andrew Doran.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Copyright (c) 2001 Thomas Nordin <nordin (at) openbsd.org>
41 1.1 thorpej * Copyright (c) 2000-2001 Artur Grabowski <art (at) openbsd.org>
42 1.14 perry * All rights reserved.
43 1.14 perry *
44 1.14 perry * Redistribution and use in source and binary forms, with or without
45 1.14 perry * modification, are permitted provided that the following conditions
46 1.14 perry * are met:
47 1.1 thorpej *
48 1.14 perry * 1. Redistributions of source code must retain the above copyright
49 1.14 perry * notice, this list of conditions and the following disclaimer.
50 1.14 perry * 2. Redistributions in binary form must reproduce the above copyright
51 1.14 perry * notice, this list of conditions and the following disclaimer in the
52 1.14 perry * documentation and/or other materials provided with the distribution.
53 1.1 thorpej * 3. The name of the author may not be used to endorse or promote products
54 1.14 perry * derived from this software without specific prior written permission.
55 1.1 thorpej *
56 1.1 thorpej * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
57 1.1 thorpej * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
58 1.1 thorpej * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
59 1.1 thorpej * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60 1.1 thorpej * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61 1.1 thorpej * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
62 1.1 thorpej * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
63 1.1 thorpej * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
64 1.1 thorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
65 1.14 perry * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 1.1 thorpej */
67 1.7 lukem
68 1.7 lukem #include <sys/cdefs.h>
69 1.36 ad __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.36 2008/04/22 11:45:28 ad Exp $");
70 1.1 thorpej
71 1.1 thorpej /*
72 1.22 ad * Timeouts are kept in a hierarchical timing wheel. The c_time is the
73 1.36 ad * value of c_cpu->cc_ticks when the timeout should be called. There are
74 1.36 ad * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and
75 1.36 ad * Hierarchical Timing Wheels: Efficient Data Structures for Implementing
76 1.36 ad * a Timer Facility" by George Varghese and Tony Lauck.
77 1.22 ad *
78 1.22 ad * Some of the "math" in here is a bit tricky. We have to beware of
79 1.22 ad * wrapping ints.
80 1.22 ad *
81 1.22 ad * We use the fact that any element added to the queue must be added with
82 1.22 ad * a positive time. That means that any element `to' on the queue cannot
83 1.22 ad * be scheduled to timeout further in time than INT_MAX, but c->c_time can
84 1.22 ad * be positive or negative so comparing it with anything is dangerous.
85 1.22 ad * The only way we can use the c->c_time value in any predictable way is
86 1.22 ad * when we calculate how far in the future `to' will timeout - "c->c_time
87 1.36 ad * - c->c_cpu->cc_ticks". The result will always be positive for future
88 1.22 ad * timeouts and 0 or negative for due timeouts.
89 1.1 thorpej */
90 1.1 thorpej
91 1.24 ad #define _CALLOUT_PRIVATE
92 1.24 ad
93 1.1 thorpej #include <sys/param.h>
94 1.1 thorpej #include <sys/systm.h>
95 1.1 thorpej #include <sys/kernel.h>
96 1.1 thorpej #include <sys/callout.h>
97 1.20 ad #include <sys/mutex.h>
98 1.22 ad #include <sys/proc.h>
99 1.22 ad #include <sys/sleepq.h>
100 1.22 ad #include <sys/syncobj.h>
101 1.22 ad #include <sys/evcnt.h>
102 1.27 ad #include <sys/intr.h>
103 1.33 ad #include <sys/cpu.h>
104 1.36 ad #include <sys/kmem.h>
105 1.1 thorpej
106 1.1 thorpej #ifdef DDB
107 1.1 thorpej #include <machine/db_machdep.h>
108 1.1 thorpej #include <ddb/db_interface.h>
109 1.1 thorpej #include <ddb/db_access.h>
110 1.1 thorpej #include <ddb/db_sym.h>
111 1.1 thorpej #include <ddb/db_output.h>
112 1.1 thorpej #endif
113 1.1 thorpej
114 1.22 ad #define BUCKETS 1024
115 1.22 ad #define WHEELSIZE 256
116 1.22 ad #define WHEELMASK 255
117 1.22 ad #define WHEELBITS 8
118 1.22 ad
119 1.1 thorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
120 1.1 thorpej
121 1.36 ad #define BUCKET(cc, rel, abs) \
122 1.1 thorpej (((rel) <= (1 << (2*WHEELBITS))) \
123 1.1 thorpej ? ((rel) <= (1 << WHEELBITS)) \
124 1.36 ad ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))] \
125 1.36 ad : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE] \
126 1.1 thorpej : ((rel) <= (1 << (3*WHEELBITS))) \
127 1.36 ad ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE] \
128 1.36 ad : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
129 1.1 thorpej
130 1.36 ad #define MOVEBUCKET(cc, wheel, time) \
131 1.36 ad CIRCQ_APPEND(&(cc)->cc_todo, \
132 1.36 ad &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
133 1.1 thorpej
134 1.1 thorpej /*
135 1.1 thorpej * Circular queue definitions.
136 1.1 thorpej */
137 1.1 thorpej
138 1.11 scw #define CIRCQ_INIT(list) \
139 1.1 thorpej do { \
140 1.11 scw (list)->cq_next_l = (list); \
141 1.11 scw (list)->cq_prev_l = (list); \
142 1.1 thorpej } while (/*CONSTCOND*/0)
143 1.1 thorpej
144 1.1 thorpej #define CIRCQ_INSERT(elem, list) \
145 1.1 thorpej do { \
146 1.11 scw (elem)->cq_prev_e = (list)->cq_prev_e; \
147 1.11 scw (elem)->cq_next_l = (list); \
148 1.11 scw (list)->cq_prev_l->cq_next_l = (elem); \
149 1.11 scw (list)->cq_prev_l = (elem); \
150 1.1 thorpej } while (/*CONSTCOND*/0)
151 1.1 thorpej
152 1.1 thorpej #define CIRCQ_APPEND(fst, snd) \
153 1.1 thorpej do { \
154 1.1 thorpej if (!CIRCQ_EMPTY(snd)) { \
155 1.11 scw (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l; \
156 1.11 scw (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l; \
157 1.11 scw (snd)->cq_prev_l->cq_next_l = (fst); \
158 1.11 scw (fst)->cq_prev_l = (snd)->cq_prev_l; \
159 1.1 thorpej CIRCQ_INIT(snd); \
160 1.1 thorpej } \
161 1.1 thorpej } while (/*CONSTCOND*/0)
162 1.1 thorpej
163 1.1 thorpej #define CIRCQ_REMOVE(elem) \
164 1.1 thorpej do { \
165 1.11 scw (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e; \
166 1.11 scw (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e; \
167 1.1 thorpej } while (/*CONSTCOND*/0)
168 1.1 thorpej
169 1.11 scw #define CIRCQ_FIRST(list) ((list)->cq_next_e)
170 1.11 scw #define CIRCQ_NEXT(elem) ((elem)->cq_next_e)
171 1.11 scw #define CIRCQ_LAST(elem,list) ((elem)->cq_next_l == (list))
172 1.11 scw #define CIRCQ_EMPTY(list) ((list)->cq_next_l == (list))
173 1.1 thorpej
174 1.22 ad static void callout_softclock(void *);
175 1.22 ad
176 1.36 ad struct callout_cpu {
177 1.36 ad kmutex_t cc_lock;
178 1.36 ad sleepq_t cc_sleepq;
179 1.36 ad u_int cc_nwait;
180 1.36 ad u_int cc_ticks;
181 1.36 ad lwp_t *cc_lwp;
182 1.36 ad callout_impl_t *cc_active;
183 1.36 ad callout_impl_t *cc_cancel;
184 1.36 ad struct evcnt cc_ev_late;
185 1.36 ad struct evcnt cc_ev_block;
186 1.36 ad struct callout_circq cc_todo; /* Worklist */
187 1.36 ad struct callout_circq cc_wheel[BUCKETS]; /* Queues of timeouts */
188 1.36 ad char cc_name1[12];
189 1.36 ad char cc_name2[12];
190 1.36 ad };
191 1.36 ad
192 1.36 ad static struct callout_cpu callout_cpu0;
193 1.36 ad static void *callout_sih;
194 1.36 ad
195 1.36 ad static inline kmutex_t *
196 1.36 ad callout_lock(callout_impl_t *c)
197 1.36 ad {
198 1.36 ad kmutex_t *lock;
199 1.36 ad
200 1.36 ad for (;;) {
201 1.36 ad lock = &c->c_cpu->cc_lock;
202 1.36 ad mutex_spin_enter(lock);
203 1.36 ad if (__predict_true(lock == &c->c_cpu->cc_lock))
204 1.36 ad return lock;
205 1.36 ad mutex_spin_exit(lock);
206 1.36 ad }
207 1.36 ad }
208 1.5 thorpej
209 1.1 thorpej /*
210 1.1 thorpej * callout_startup:
211 1.1 thorpej *
212 1.1 thorpej * Initialize the callout facility, called at system startup time.
213 1.36 ad * Do just enough to allow callouts to be safely registered.
214 1.1 thorpej */
215 1.1 thorpej void
216 1.1 thorpej callout_startup(void)
217 1.1 thorpej {
218 1.36 ad struct callout_cpu *cc;
219 1.1 thorpej int b;
220 1.1 thorpej
221 1.36 ad KASSERT(curcpu()->ci_data.cpu_callout == NULL);
222 1.22 ad
223 1.36 ad cc = &callout_cpu0;
224 1.36 ad mutex_init(&cc->cc_lock, MUTEX_DEFAULT, IPL_SCHED);
225 1.36 ad CIRCQ_INIT(&cc->cc_todo);
226 1.1 thorpej for (b = 0; b < BUCKETS; b++)
227 1.36 ad CIRCQ_INIT(&cc->cc_wheel[b]);
228 1.36 ad curcpu()->ci_data.cpu_callout = cc;
229 1.22 ad }
230 1.22 ad
231 1.22 ad /*
232 1.36 ad * callout_init_cpu:
233 1.22 ad *
234 1.36 ad * Per-CPU initialization.
235 1.22 ad */
236 1.22 ad void
237 1.36 ad callout_init_cpu(struct cpu_info *ci)
238 1.22 ad {
239 1.36 ad struct callout_cpu *cc;
240 1.36 ad int b;
241 1.22 ad
242 1.36 ad KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
243 1.36 ad
244 1.36 ad if ((cc = ci->ci_data.cpu_callout) == NULL) {
245 1.36 ad cc = kmem_zalloc(sizeof(*cc), KM_SLEEP);
246 1.36 ad if (cc == NULL)
247 1.36 ad panic("callout_init_cpu (1)");
248 1.36 ad mutex_init(&cc->cc_lock, MUTEX_DEFAULT, IPL_SCHED);
249 1.36 ad CIRCQ_INIT(&cc->cc_todo);
250 1.36 ad for (b = 0; b < BUCKETS; b++)
251 1.36 ad CIRCQ_INIT(&cc->cc_wheel[b]);
252 1.36 ad } else {
253 1.36 ad /* Boot CPU, one time only. */
254 1.36 ad callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
255 1.36 ad callout_softclock, NULL);
256 1.36 ad if (callout_sih == NULL)
257 1.36 ad panic("callout_init_cpu (2)");
258 1.36 ad }
259 1.36 ad
260 1.36 ad sleepq_init(&cc->cc_sleepq, &cc->cc_lock);
261 1.36 ad
262 1.36 ad snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u",
263 1.36 ad cpu_index(ci));
264 1.36 ad evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC,
265 1.36 ad NULL, "callout", cc->cc_name1);
266 1.36 ad
267 1.36 ad snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u",
268 1.36 ad cpu_index(ci));
269 1.36 ad evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC,
270 1.36 ad NULL, "callout", cc->cc_name2);
271 1.36 ad
272 1.36 ad ci->ci_data.cpu_callout = cc;
273 1.1 thorpej }
274 1.1 thorpej
275 1.1 thorpej /*
276 1.1 thorpej * callout_init:
277 1.1 thorpej *
278 1.36 ad * Initialize a callout structure. This must be quick, so we fill
279 1.36 ad * only the minimum number of fields.
280 1.1 thorpej */
281 1.1 thorpej void
282 1.22 ad callout_init(callout_t *cs, u_int flags)
283 1.1 thorpej {
284 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
285 1.36 ad struct callout_cpu *cc;
286 1.22 ad
287 1.22 ad KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
288 1.1 thorpej
289 1.36 ad cc = curcpu()->ci_data.cpu_callout;
290 1.36 ad c->c_func = NULL;
291 1.22 ad c->c_magic = CALLOUT_MAGIC;
292 1.36 ad if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) {
293 1.36 ad c->c_flags = flags;
294 1.36 ad c->c_cpu = cc;
295 1.36 ad return;
296 1.36 ad }
297 1.36 ad c->c_flags = flags | CALLOUT_BOUND;
298 1.36 ad c->c_cpu = &callout_cpu0;
299 1.22 ad }
300 1.22 ad
301 1.22 ad /*
302 1.22 ad * callout_destroy:
303 1.22 ad *
304 1.22 ad * Destroy a callout structure. The callout must be stopped.
305 1.22 ad */
306 1.22 ad void
307 1.22 ad callout_destroy(callout_t *cs)
308 1.22 ad {
309 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
310 1.22 ad
311 1.22 ad /*
312 1.22 ad * It's not necessary to lock in order to see the correct value
313 1.22 ad * of c->c_flags. If the callout could potentially have been
314 1.22 ad * running, the current thread should have stopped it.
315 1.22 ad */
316 1.22 ad KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
317 1.36 ad KASSERT(c->c_cpu->cc_lwp == curlwp || c->c_cpu->cc_active != c);
318 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
319 1.22 ad c->c_magic = 0;
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej /*
323 1.29 joerg * callout_schedule_locked:
324 1.1 thorpej *
325 1.29 joerg * Schedule a callout to run. The function and argument must
326 1.29 joerg * already be set in the callout structure. Must be called with
327 1.29 joerg * callout_lock.
328 1.1 thorpej */
329 1.29 joerg static void
330 1.36 ad callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks)
331 1.1 thorpej {
332 1.36 ad struct callout_cpu *cc, *occ;
333 1.20 ad int old_time;
334 1.1 thorpej
335 1.1 thorpej KASSERT(to_ticks >= 0);
336 1.29 joerg KASSERT(c->c_func != NULL);
337 1.1 thorpej
338 1.1 thorpej /* Initialize the time here, it won't change. */
339 1.36 ad occ = c->c_cpu;
340 1.22 ad c->c_flags &= ~CALLOUT_FIRED;
341 1.1 thorpej
342 1.1 thorpej /*
343 1.1 thorpej * If this timeout is already scheduled and now is moved
344 1.36 ad * earlier, reschedule it now. Otherwise leave it in place
345 1.1 thorpej * and let it be rescheduled later.
346 1.1 thorpej */
347 1.22 ad if ((c->c_flags & CALLOUT_PENDING) != 0) {
348 1.36 ad /* Leave on existing CPU. */
349 1.36 ad old_time = c->c_time;
350 1.36 ad c->c_time = to_ticks + occ->cc_ticks;
351 1.4 yamt if (c->c_time - old_time < 0) {
352 1.1 thorpej CIRCQ_REMOVE(&c->c_list);
353 1.36 ad CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
354 1.1 thorpej }
355 1.36 ad mutex_spin_exit(lock);
356 1.36 ad return;
357 1.36 ad }
358 1.36 ad
359 1.36 ad cc = curcpu()->ci_data.cpu_callout;
360 1.36 ad if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ ||
361 1.36 ad !mutex_tryenter(&cc->cc_lock)) {
362 1.36 ad /* Leave on existing CPU. */
363 1.36 ad c->c_time = to_ticks + occ->cc_ticks;
364 1.36 ad c->c_flags |= CALLOUT_PENDING;
365 1.36 ad CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
366 1.1 thorpej } else {
367 1.36 ad /* Move to this CPU. */
368 1.36 ad c->c_cpu = cc;
369 1.36 ad c->c_time = to_ticks + cc->cc_ticks;
370 1.1 thorpej c->c_flags |= CALLOUT_PENDING;
371 1.36 ad CIRCQ_INSERT(&c->c_list, &cc->cc_todo);
372 1.36 ad mutex_spin_exit(&cc->cc_lock);
373 1.1 thorpej }
374 1.36 ad mutex_spin_exit(lock);
375 1.29 joerg }
376 1.29 joerg
377 1.29 joerg /*
378 1.29 joerg * callout_reset:
379 1.29 joerg *
380 1.29 joerg * Reset a callout structure with a new function and argument, and
381 1.29 joerg * schedule it to run.
382 1.29 joerg */
383 1.29 joerg void
384 1.29 joerg callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
385 1.29 joerg {
386 1.29 joerg callout_impl_t *c = (callout_impl_t *)cs;
387 1.36 ad kmutex_t *lock;
388 1.29 joerg
389 1.29 joerg KASSERT(c->c_magic == CALLOUT_MAGIC);
390 1.29 joerg
391 1.36 ad lock = callout_lock(c);
392 1.29 joerg c->c_func = func;
393 1.29 joerg c->c_arg = arg;
394 1.36 ad callout_schedule_locked(c, lock, to_ticks);
395 1.1 thorpej }
396 1.1 thorpej
397 1.1 thorpej /*
398 1.1 thorpej * callout_schedule:
399 1.1 thorpej *
400 1.1 thorpej * Schedule a callout to run. The function and argument must
401 1.1 thorpej * already be set in the callout structure.
402 1.1 thorpej */
403 1.1 thorpej void
404 1.22 ad callout_schedule(callout_t *cs, int to_ticks)
405 1.1 thorpej {
406 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
407 1.36 ad kmutex_t *lock;
408 1.1 thorpej
409 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
410 1.1 thorpej
411 1.36 ad lock = callout_lock(c);
412 1.36 ad callout_schedule_locked(c, lock, to_ticks);
413 1.1 thorpej }
414 1.1 thorpej
415 1.1 thorpej /*
416 1.1 thorpej * callout_stop:
417 1.1 thorpej *
418 1.36 ad * Try to cancel a pending callout. It may be too late: the callout
419 1.36 ad * could be running on another CPU. If called from interrupt context,
420 1.36 ad * the callout could already be in progress at a lower priority.
421 1.1 thorpej */
422 1.22 ad bool
423 1.22 ad callout_stop(callout_t *cs)
424 1.1 thorpej {
425 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
426 1.36 ad struct callout_cpu *cc;
427 1.36 ad kmutex_t *lock;
428 1.22 ad bool expired;
429 1.22 ad
430 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
431 1.1 thorpej
432 1.36 ad lock = callout_lock(c);
433 1.20 ad
434 1.22 ad if ((c->c_flags & CALLOUT_PENDING) != 0)
435 1.1 thorpej CIRCQ_REMOVE(&c->c_list);
436 1.32 ad expired = ((c->c_flags & CALLOUT_FIRED) != 0);
437 1.32 ad c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
438 1.32 ad
439 1.36 ad cc = c->c_cpu;
440 1.36 ad if (cc->cc_active == c) {
441 1.32 ad /*
442 1.32 ad * This is for non-MPSAFE callouts only. To synchronize
443 1.32 ad * effectively we must be called with kernel_lock held.
444 1.32 ad * It's also taken in callout_softclock.
445 1.32 ad */
446 1.36 ad cc->cc_cancel = c;
447 1.32 ad }
448 1.32 ad
449 1.36 ad mutex_spin_exit(lock);
450 1.32 ad
451 1.32 ad return expired;
452 1.32 ad }
453 1.32 ad
454 1.32 ad /*
455 1.32 ad * callout_halt:
456 1.32 ad *
457 1.32 ad * Cancel a pending callout. If in-flight, block until it completes.
458 1.36 ad * May not be called from a hard interrupt handler. If the callout
459 1.36 ad * can take locks, the caller of callout_halt() must not hold any of
460 1.36 ad * those locks, otherwise the two could deadlock.
461 1.32 ad */
462 1.32 ad bool
463 1.32 ad callout_halt(callout_t *cs)
464 1.32 ad {
465 1.32 ad callout_impl_t *c = (callout_impl_t *)cs;
466 1.36 ad struct callout_cpu *cc;
467 1.32 ad struct lwp *l;
468 1.36 ad kmutex_t *lock;
469 1.32 ad bool expired;
470 1.32 ad
471 1.32 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
472 1.32 ad KASSERT(!cpu_intr_p());
473 1.32 ad
474 1.36 ad lock = callout_lock(c);
475 1.1 thorpej
476 1.22 ad expired = ((c->c_flags & CALLOUT_FIRED) != 0);
477 1.32 ad if ((c->c_flags & CALLOUT_PENDING) != 0)
478 1.32 ad CIRCQ_REMOVE(&c->c_list);
479 1.9 he c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
480 1.1 thorpej
481 1.32 ad l = curlwp;
482 1.36 ad for (;;) {
483 1.36 ad cc = c->c_cpu;
484 1.36 ad if (__predict_true(cc->cc_active != c || cc->cc_lwp == l))
485 1.36 ad break;
486 1.32 ad KASSERT(l->l_wchan == NULL);
487 1.36 ad cc->cc_nwait++;
488 1.36 ad cc->cc_ev_block.ev_count++;
489 1.32 ad l->l_kpriority = true;
490 1.36 ad sleepq_enter(&cc->cc_sleepq, l);
491 1.36 ad sleepq_enqueue(&cc->cc_sleepq, cc, "callout", &sleep_syncobj);
492 1.32 ad KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
493 1.32 ad sleepq_block(0, false);
494 1.36 ad lock = callout_lock(c);
495 1.32 ad }
496 1.32 ad
497 1.36 ad mutex_spin_exit(lock);
498 1.22 ad
499 1.22 ad return expired;
500 1.22 ad }
501 1.22 ad
502 1.36 ad #ifdef notyet
503 1.36 ad /*
504 1.36 ad * callout_bind:
505 1.36 ad *
506 1.36 ad * Bind a callout so that it will only execute on one CPU.
507 1.36 ad * The callout must be stopped, and must be MPSAFE.
508 1.36 ad *
509 1.36 ad * XXX Disabled for now until it is decided how to handle
510 1.36 ad * offlined CPUs. We may want weak+strong binding.
511 1.36 ad */
512 1.36 ad void
513 1.36 ad callout_bind(callout_t *cs, struct cpu_info *ci)
514 1.36 ad {
515 1.36 ad callout_impl_t *c = (callout_impl_t *)cs;
516 1.36 ad struct callout_cpu *cc;
517 1.36 ad kmutex_t *lock;
518 1.36 ad
519 1.36 ad KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
520 1.36 ad KASSERT(c->c_cpu->cc_active != c);
521 1.36 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
522 1.36 ad KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0);
523 1.36 ad
524 1.36 ad lock = callout_lock(c);
525 1.36 ad cc = ci->ci_data.cpu_callout;
526 1.36 ad c->c_flags |= CALLOUT_BOUND;
527 1.36 ad if (c->c_cpu != cc) {
528 1.36 ad /*
529 1.36 ad * Assigning c_cpu effectively unlocks the callout
530 1.36 ad * structure, as we don't hold the new CPU's lock.
531 1.36 ad * Issue memory barrier to prevent accesses being
532 1.36 ad * reordered.
533 1.36 ad */
534 1.36 ad membar_exit();
535 1.36 ad c->c_cpu = cc;
536 1.36 ad }
537 1.36 ad mutex_spin_exit(lock);
538 1.36 ad }
539 1.36 ad #endif
540 1.36 ad
541 1.22 ad void
542 1.22 ad callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
543 1.22 ad {
544 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
545 1.36 ad kmutex_t *lock;
546 1.22 ad
547 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
548 1.22 ad
549 1.36 ad lock = callout_lock(c);
550 1.22 ad c->c_func = func;
551 1.22 ad c->c_arg = arg;
552 1.36 ad mutex_spin_exit(lock);
553 1.22 ad }
554 1.22 ad
555 1.22 ad bool
556 1.22 ad callout_expired(callout_t *cs)
557 1.22 ad {
558 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
559 1.36 ad kmutex_t *lock;
560 1.22 ad bool rv;
561 1.22 ad
562 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
563 1.22 ad
564 1.36 ad lock = callout_lock(c);
565 1.22 ad rv = ((c->c_flags & CALLOUT_FIRED) != 0);
566 1.36 ad mutex_spin_exit(lock);
567 1.22 ad
568 1.22 ad return rv;
569 1.22 ad }
570 1.22 ad
571 1.22 ad bool
572 1.22 ad callout_active(callout_t *cs)
573 1.22 ad {
574 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
575 1.36 ad kmutex_t *lock;
576 1.22 ad bool rv;
577 1.22 ad
578 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
579 1.22 ad
580 1.36 ad lock = callout_lock(c);
581 1.22 ad rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
582 1.36 ad mutex_spin_exit(lock);
583 1.22 ad
584 1.22 ad return rv;
585 1.22 ad }
586 1.22 ad
587 1.22 ad bool
588 1.22 ad callout_pending(callout_t *cs)
589 1.22 ad {
590 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
591 1.36 ad kmutex_t *lock;
592 1.22 ad bool rv;
593 1.22 ad
594 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
595 1.22 ad
596 1.36 ad lock = callout_lock(c);
597 1.22 ad rv = ((c->c_flags & CALLOUT_PENDING) != 0);
598 1.36 ad mutex_spin_exit(lock);
599 1.22 ad
600 1.22 ad return rv;
601 1.22 ad }
602 1.22 ad
603 1.22 ad bool
604 1.22 ad callout_invoking(callout_t *cs)
605 1.22 ad {
606 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
607 1.36 ad kmutex_t *lock;
608 1.22 ad bool rv;
609 1.22 ad
610 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
611 1.22 ad
612 1.36 ad lock = callout_lock(c);
613 1.22 ad rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
614 1.36 ad mutex_spin_exit(lock);
615 1.22 ad
616 1.22 ad return rv;
617 1.22 ad }
618 1.22 ad
619 1.22 ad void
620 1.22 ad callout_ack(callout_t *cs)
621 1.22 ad {
622 1.22 ad callout_impl_t *c = (callout_impl_t *)cs;
623 1.36 ad kmutex_t *lock;
624 1.22 ad
625 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
626 1.22 ad
627 1.36 ad lock = callout_lock(c);
628 1.22 ad c->c_flags &= ~CALLOUT_INVOKING;
629 1.36 ad mutex_spin_exit(lock);
630 1.1 thorpej }
631 1.1 thorpej
632 1.1 thorpej /*
633 1.36 ad * callout_hardclock:
634 1.36 ad *
635 1.36 ad * Called from hardclock() once every tick. We schedule a soft
636 1.36 ad * interrupt if there is work to be done.
637 1.1 thorpej */
638 1.22 ad void
639 1.1 thorpej callout_hardclock(void)
640 1.1 thorpej {
641 1.36 ad struct callout_cpu *cc;
642 1.36 ad int needsoftclock, ticks;
643 1.1 thorpej
644 1.36 ad cc = curcpu()->ci_data.cpu_callout;
645 1.36 ad mutex_spin_enter(&cc->cc_lock);
646 1.1 thorpej
647 1.36 ad ticks = ++cc->cc_ticks;
648 1.36 ad
649 1.36 ad MOVEBUCKET(cc, 0, ticks);
650 1.36 ad if (MASKWHEEL(0, ticks) == 0) {
651 1.36 ad MOVEBUCKET(cc, 1, ticks);
652 1.36 ad if (MASKWHEEL(1, ticks) == 0) {
653 1.36 ad MOVEBUCKET(cc, 2, ticks);
654 1.36 ad if (MASKWHEEL(2, ticks) == 0)
655 1.36 ad MOVEBUCKET(cc, 3, ticks);
656 1.1 thorpej }
657 1.1 thorpej }
658 1.1 thorpej
659 1.36 ad needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo);
660 1.36 ad mutex_spin_exit(&cc->cc_lock);
661 1.1 thorpej
662 1.22 ad if (needsoftclock)
663 1.36 ad softint_schedule(callout_sih);
664 1.1 thorpej }
665 1.1 thorpej
666 1.36 ad /*
667 1.36 ad * callout_softclock:
668 1.36 ad *
669 1.36 ad * Soft interrupt handler, scheduled above if there is work to
670 1.36 ad * be done. Callouts are made in soft interrupt context.
671 1.36 ad */
672 1.22 ad static void
673 1.22 ad callout_softclock(void *v)
674 1.1 thorpej {
675 1.22 ad callout_impl_t *c;
676 1.36 ad struct callout_cpu *cc;
677 1.1 thorpej void (*func)(void *);
678 1.1 thorpej void *arg;
679 1.36 ad int mpsafe, count, ticks, delta;
680 1.22 ad lwp_t *l;
681 1.1 thorpej
682 1.22 ad l = curlwp;
683 1.36 ad KASSERT(l->l_cpu == curcpu());
684 1.36 ad cc = l->l_cpu->ci_data.cpu_callout;
685 1.1 thorpej
686 1.36 ad mutex_spin_enter(&cc->cc_lock);
687 1.36 ad cc->cc_lwp = l;
688 1.36 ad while (!CIRCQ_EMPTY(&cc->cc_todo)) {
689 1.36 ad c = CIRCQ_FIRST(&cc->cc_todo);
690 1.22 ad KASSERT(c->c_magic == CALLOUT_MAGIC);
691 1.22 ad KASSERT(c->c_func != NULL);
692 1.36 ad KASSERT(c->c_cpu == cc);
693 1.26 ad KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
694 1.26 ad KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
695 1.1 thorpej CIRCQ_REMOVE(&c->c_list);
696 1.1 thorpej
697 1.1 thorpej /* If due run it, otherwise insert it into the right bucket. */
698 1.36 ad ticks = cc->cc_ticks;
699 1.36 ad delta = c->c_time - ticks;
700 1.36 ad if (delta > 0) {
701 1.36 ad CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time));
702 1.36 ad continue;
703 1.36 ad }
704 1.36 ad if (delta < 0)
705 1.36 ad cc->cc_ev_late.ev_count++;
706 1.36 ad
707 1.36 ad c->c_flags ^= (CALLOUT_PENDING | CALLOUT_FIRED);
708 1.36 ad mpsafe = (c->c_flags & CALLOUT_MPSAFE);
709 1.36 ad func = c->c_func;
710 1.36 ad arg = c->c_arg;
711 1.36 ad cc->cc_active = c;
712 1.36 ad
713 1.36 ad mutex_spin_exit(&cc->cc_lock);
714 1.36 ad if (!mpsafe) {
715 1.36 ad KERNEL_LOCK(1, NULL);
716 1.36 ad (*func)(arg);
717 1.36 ad KERNEL_UNLOCK_ONE(NULL);
718 1.36 ad } else
719 1.36 ad (*func)(arg);
720 1.36 ad mutex_spin_enter(&cc->cc_lock);
721 1.36 ad
722 1.36 ad /*
723 1.36 ad * We can't touch 'c' here because it might be
724 1.36 ad * freed already. If LWPs waiting for callout
725 1.36 ad * to complete, awaken them.
726 1.36 ad */
727 1.36 ad cc->cc_active = NULL;
728 1.36 ad if ((count = cc->cc_nwait) != 0) {
729 1.36 ad cc->cc_nwait = 0;
730 1.36 ad /* sleepq_wake() drops the lock. */
731 1.36 ad sleepq_wake(&cc->cc_sleepq, cc, count);
732 1.36 ad mutex_spin_enter(&cc->cc_lock);
733 1.1 thorpej }
734 1.1 thorpej }
735 1.36 ad cc->cc_lwp = NULL;
736 1.36 ad mutex_spin_exit(&cc->cc_lock);
737 1.1 thorpej }
738 1.1 thorpej
739 1.1 thorpej #ifdef DDB
740 1.1 thorpej static void
741 1.36 ad db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *bucket)
742 1.1 thorpej {
743 1.22 ad callout_impl_t *c;
744 1.1 thorpej db_expr_t offset;
745 1.15 christos const char *name;
746 1.15 christos static char question[] = "?";
747 1.36 ad int b;
748 1.1 thorpej
749 1.11 scw if (CIRCQ_EMPTY(bucket))
750 1.11 scw return;
751 1.11 scw
752 1.11 scw for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
753 1.10 scw db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
754 1.10 scw &offset);
755 1.15 christos name = name ? name : question;
756 1.36 ad b = (bucket - cc->cc_wheel);
757 1.36 ad if (b < 0)
758 1.36 ad b = -WHEELSIZE;
759 1.36 ad db_printf("%9d %2d/%-4d %16lx %s\n",
760 1.36 ad c->c_time - cc->cc_ticks, b / WHEELSIZE, b,
761 1.36 ad (u_long)c->c_arg, name);
762 1.11 scw if (CIRCQ_LAST(&c->c_list, bucket))
763 1.11 scw break;
764 1.1 thorpej }
765 1.1 thorpej }
766 1.1 thorpej
767 1.1 thorpej void
768 1.21 matt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
769 1.1 thorpej {
770 1.36 ad CPU_INFO_ITERATOR cii;
771 1.36 ad struct callout_cpu *cc;
772 1.36 ad struct cpu_info *ci;
773 1.1 thorpej int b;
774 1.1 thorpej
775 1.1 thorpej db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
776 1.1 thorpej db_printf(" ticks wheel arg func\n");
777 1.1 thorpej
778 1.1 thorpej /*
779 1.1 thorpej * Don't lock the callwheel; all the other CPUs are paused
780 1.1 thorpej * anyhow, and we might be called in a circumstance where
781 1.1 thorpej * some other CPU was paused while holding the lock.
782 1.1 thorpej */
783 1.36 ad for (CPU_INFO_FOREACH(cii, ci)) {
784 1.36 ad cc = ci->ci_data.cpu_callout;
785 1.36 ad db_show_callout_bucket(cc, &cc->cc_todo);
786 1.36 ad }
787 1.36 ad for (b = 0; b < BUCKETS; b++) {
788 1.36 ad for (CPU_INFO_FOREACH(cii, ci)) {
789 1.36 ad cc = ci->ci_data.cpu_callout;
790 1.36 ad db_show_callout_bucket(cc, &cc->cc_wheel[b]);
791 1.36 ad }
792 1.36 ad }
793 1.1 thorpej }
794 1.1 thorpej #endif /* DDB */
795