xref: /freebsd/sys/kern/subr_epoch.c (revision e08e9e999091f86081377b7cedc3fd2fe2ab70fc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018, Matthew Macy <mmacy@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/counter.h>
36 #include <sys/epoch.h>
37 #include <sys/gtaskqueue.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/pcpu.h>
44 #include <sys/proc.h>
45 #include <sys/sched.h>
46 #include <sys/smp.h>
47 #include <sys/sysctl.h>
48 #include <sys/turnstile.h>
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_kern.h>
52 
53 #include <ck_epoch.h>
54 
55 static MALLOC_DEFINE(M_EPOCH, "epoch", "epoch based reclamation");
56 
57 /* arbitrary --- needs benchmarking */
58 #define MAX_ADAPTIVE_SPIN 1000
59 #define MAX_EPOCHS 64
60 
61 #ifdef __amd64__
62 #define EPOCH_ALIGN CACHE_LINE_SIZE*2
63 #else
64 #define EPOCH_ALIGN CACHE_LINE_SIZE
65 #endif
66 
67 CTASSERT(sizeof(epoch_section_t) == sizeof(ck_epoch_section_t));
68 CTASSERT(sizeof(ck_epoch_entry_t) == sizeof(struct epoch_context));
69 SYSCTL_NODE(_kern, OID_AUTO, epoch, CTLFLAG_RW, 0, "epoch information");
70 SYSCTL_NODE(_kern_epoch, OID_AUTO, stats, CTLFLAG_RW, 0, "epoch stats");
71 
72 
73 /* Stats. */
74 static counter_u64_t block_count;
75 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, nblocked, CTLFLAG_RW,
76 				   &block_count, "# of times a thread was in an epoch when epoch_wait was called");
77 static counter_u64_t migrate_count;
78 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, migrations, CTLFLAG_RW,
79 				   &migrate_count, "# of times thread was migrated to another CPU in epoch_wait");
80 static counter_u64_t turnstile_count;
81 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, ncontended, CTLFLAG_RW,
82 				   &turnstile_count, "# of times a thread was blocked on a lock in an epoch during an epoch_wait");
83 static counter_u64_t switch_count;
84 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, switches, CTLFLAG_RW,
85 				   &switch_count, "# of times a thread voluntarily context switched in epoch_wait");
86 static counter_u64_t epoch_call_count;
87 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_calls, CTLFLAG_RW,
88 				   &epoch_call_count, "# of times a callback was deferred");
89 static counter_u64_t epoch_call_task_count;
90 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_call_tasks, CTLFLAG_RW,
91 				   &epoch_call_task_count, "# of times a callback task was run");
92 
93 TAILQ_HEAD(threadlist, thread);
94 
95 CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
96     ck_epoch_entry_container)
97 
98 typedef struct epoch_record {
99 	ck_epoch_record_t er_record;
100 	volatile struct threadlist er_tdlist;
101 	volatile uint32_t er_gen;
102 	uint32_t er_cpuid;
103 } *epoch_record_t;
104 
105 struct epoch_pcpu_state {
106 	struct epoch_record eps_record;
107 } __aligned(EPOCH_ALIGN);
108 
109 struct epoch {
110 	struct ck_epoch e_epoch __aligned(EPOCH_ALIGN);
111 	struct epoch_pcpu_state *e_pcpu_dom[MAXMEMDOM] __aligned(EPOCH_ALIGN);
112 	int e_idx;
113 	int e_flags;
114 	struct epoch_pcpu_state *e_pcpu[0];
115 };
116 
117 epoch_t allepochs[MAX_EPOCHS];
118 
119 DPCPU_DEFINE(struct grouptask, epoch_cb_task);
120 DPCPU_DEFINE(int, epoch_cb_count);
121 
122 static __read_mostly int domcount[MAXMEMDOM];
123 static __read_mostly int domoffsets[MAXMEMDOM];
124 static __read_mostly int inited;
125 static __read_mostly int epoch_count;
126 __read_mostly epoch_t global_epoch;
127 __read_mostly epoch_t global_epoch_preempt;
128 
129 static void epoch_call_task(void *context __unused);
130 
131 #if defined(__powerpc64__) || defined(__powerpc__) || !defined(NUMA)
132 static bool usedomains = false;
133 #else
134 static bool usedomains = true;
135 #endif
136 static void
137 epoch_init(void *arg __unused)
138 {
139 	int domain, cpu;
140 
141 	block_count = counter_u64_alloc(M_WAITOK);
142 	migrate_count = counter_u64_alloc(M_WAITOK);
143 	turnstile_count = counter_u64_alloc(M_WAITOK);
144 	switch_count = counter_u64_alloc(M_WAITOK);
145 	epoch_call_count = counter_u64_alloc(M_WAITOK);
146 	epoch_call_task_count = counter_u64_alloc(M_WAITOK);
147 	if (usedomains == false)
148 		goto done;
149 	domain = 0;
150 	domoffsets[0] = 0;
151 	for (domain = 0; domain < vm_ndomains; domain++) {
152 		domcount[domain] = CPU_COUNT(&cpuset_domain[domain]);
153 		if (bootverbose)
154 			printf("domcount[%d] %d\n", domain, domcount[domain]);
155 	}
156 	for (domain = 1; domain < vm_ndomains; domain++)
157 		domoffsets[domain] = domoffsets[domain-1] + domcount[domain-1];
158 
159 	for (domain = 0; domain < vm_ndomains; domain++) {
160 		if (domcount[domain] == 0) {
161 			usedomains = false;
162 			break;
163 		}
164 	}
165  done:
166 	CPU_FOREACH(cpu) {
167 		GROUPTASK_INIT(DPCPU_ID_PTR(cpu, epoch_cb_task), 0, epoch_call_task, NULL);
168 		taskqgroup_attach_cpu(qgroup_softirq, DPCPU_ID_PTR(cpu, epoch_cb_task), NULL, cpu, -1, "epoch call task");
169 	}
170 	inited = 1;
171 	global_epoch = epoch_alloc(0);
172 	global_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
173 }
174 SYSINIT(epoch, SI_SUB_TASKQ + 1, SI_ORDER_FIRST, epoch_init, NULL);
175 
176 static void
177 epoch_init_numa(epoch_t epoch)
178 {
179 	int domain, cpu_offset;
180 	struct epoch_pcpu_state *eps;
181 	epoch_record_t er;
182 
183 	for (domain = 0; domain < vm_ndomains; domain++) {
184 		eps = malloc_domain(sizeof(*eps)*domcount[domain], M_EPOCH,
185 							domain, M_ZERO|M_WAITOK);
186 		epoch->e_pcpu_dom[domain] = eps;
187 		cpu_offset = domoffsets[domain];
188 		for (int i = 0; i < domcount[domain]; i++, eps++) {
189 			epoch->e_pcpu[cpu_offset + i] = eps;
190 			er = &eps->eps_record;
191 			ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
192 			TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
193 			er->er_cpuid = cpu_offset + i;
194 		}
195 	}
196 }
197 
198 static void
199 epoch_init_legacy(epoch_t epoch)
200 {
201 	struct epoch_pcpu_state *eps;
202 	epoch_record_t er;
203 
204 	eps = malloc(sizeof(*eps)*mp_ncpus, M_EPOCH, M_ZERO|M_WAITOK);
205 	epoch->e_pcpu_dom[0] = eps;
206 	for (int i = 0; i < mp_ncpus; i++, eps++) {
207 		epoch->e_pcpu[i] = eps;
208 		er = &eps->eps_record;
209 		ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
210 		TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
211 		er->er_cpuid = i;
212 	}
213 }
214 
215 epoch_t
216 epoch_alloc(int flags)
217 {
218 	epoch_t epoch;
219 
220 	if (__predict_false(!inited))
221 		panic("%s called too early in boot", __func__);
222 	epoch = malloc(sizeof(struct epoch) + mp_ncpus*sizeof(void*),
223 				   M_EPOCH, M_ZERO|M_WAITOK);
224 	ck_epoch_init(&epoch->e_epoch);
225 	if (usedomains)
226 		epoch_init_numa(epoch);
227 	else
228 		epoch_init_legacy(epoch);
229 	MPASS(epoch_count < MAX_EPOCHS-2);
230 	epoch->e_flags = flags;
231 	epoch->e_idx = epoch_count;
232 	allepochs[epoch_count++] = epoch;
233 	return (epoch);
234 }
235 
236 void
237 epoch_free(epoch_t epoch)
238 {
239 	int domain;
240 #ifdef INVARIANTS
241 	struct epoch_pcpu_state *eps;
242 	int cpu;
243 
244 	CPU_FOREACH(cpu) {
245 		eps = epoch->e_pcpu[cpu];
246 		MPASS(TAILQ_EMPTY(&eps->eps_record.er_tdlist));
247 	}
248 #endif
249 	allepochs[epoch->e_idx] = NULL;
250 	epoch_wait(global_epoch);
251 	if (usedomains)
252 		for (domain = 0; domain < vm_ndomains; domain++)
253 			free_domain(epoch->e_pcpu_dom[domain], M_EPOCH);
254 	else
255 		free(epoch->e_pcpu_dom[0], M_EPOCH);
256 	free(epoch, M_EPOCH);
257 }
258 
259 #define INIT_CHECK(epoch)								\
260 	do {											\
261 		if (__predict_false((epoch) == NULL))		\
262 			return;									\
263 	} while (0)
264 
265 void
266 epoch_enter_preempt_internal(epoch_t epoch, struct thread *td)
267 {
268 	struct epoch_pcpu_state *eps;
269 
270 	MPASS(cold || epoch != NULL);
271 	INIT_CHECK(epoch);
272 	MPASS(epoch->e_flags & EPOCH_PREEMPT);
273 	critical_enter();
274 	td->td_pre_epoch_prio = td->td_priority;
275 	eps = epoch->e_pcpu[curcpu];
276 #ifdef INVARIANTS
277 	MPASS(td->td_epochnest < UCHAR_MAX - 2);
278 	if (td->td_epochnest > 1) {
279 		struct thread *curtd;
280 		int found = 0;
281 
282 		TAILQ_FOREACH(curtd, &eps->eps_record.er_tdlist, td_epochq)
283 			if (curtd == td)
284 				found = 1;
285 		KASSERT(found, ("recursing on a second epoch"));
286 		critical_exit();
287 		return;
288 	}
289 #endif
290 	TAILQ_INSERT_TAIL(&eps->eps_record.er_tdlist, td, td_epochq);
291 	sched_pin();
292 	ck_epoch_begin(&eps->eps_record.er_record, (ck_epoch_section_t*)&td->td_epoch_section);
293 	critical_exit();
294 }
295 
296 
297 void
298 epoch_enter(epoch_t epoch)
299 {
300 	ck_epoch_record_t *record;
301 	struct thread *td;
302 
303 	MPASS(cold || epoch != NULL);
304 	td = curthread;
305 
306 	critical_enter();
307 	td->td_epochnest++;
308 	record = &epoch->e_pcpu[curcpu]->eps_record.er_record;
309 	ck_epoch_begin(record, NULL);
310 }
311 
312 void
313 epoch_exit_preempt_internal(epoch_t epoch, struct thread *td)
314 {
315 	struct epoch_pcpu_state *eps;
316 
317 	MPASS(td->td_epochnest == 0);
318 	INIT_CHECK(epoch);
319 	critical_enter();
320 	eps = epoch->e_pcpu[curcpu];
321 
322 	MPASS(epoch->e_flags & EPOCH_PREEMPT);
323 	ck_epoch_end(&eps->eps_record.er_record, (ck_epoch_section_t*)&td->td_epoch_section);
324 	TAILQ_REMOVE(&eps->eps_record.er_tdlist, td, td_epochq);
325 	eps->eps_record.er_gen++;
326 	sched_unpin();
327 	if (__predict_false(td->td_pre_epoch_prio != td->td_priority)) {
328 		thread_lock(td);
329 		sched_prio(td, td->td_pre_epoch_prio);
330 		thread_unlock(td);
331 	}
332 	critical_exit();
333 }
334 
335 void
336 epoch_exit(epoch_t epoch)
337 {
338 	ck_epoch_record_t *record;
339 	struct thread *td;
340 
341 	td = curthread;
342 	td->td_epochnest--;
343 	record = &epoch->e_pcpu[curcpu]->eps_record.er_record;
344 	ck_epoch_end(record, NULL);
345 	critical_exit();
346 }
347 
348 /*
349  * epoch_block_handler_preempt is a callback from the ck code when another thread is
350  * currently in an epoch section.
351  */
352 static void
353 epoch_block_handler_preempt(struct ck_epoch *global __unused, ck_epoch_record_t *cr,
354 					void *arg __unused)
355 {
356 	epoch_record_t record;
357 	struct thread *td, *tdwait, *owner;
358 	struct turnstile *ts;
359 	struct lock_object *lock;
360 	int spincount, gen;
361 
362 	record = __containerof(cr, struct epoch_record, er_record);
363 	td = curthread;
364 	spincount = 0;
365 	counter_u64_add(block_count, 1);
366 	if (record->er_cpuid != curcpu) {
367 		/*
368 		 * If the head of the list is running, we can wait for it
369 		 * to remove itself from the list and thus save us the
370 		 * overhead of a migration
371 		 */
372 		if ((tdwait = TAILQ_FIRST(&record->er_tdlist)) != NULL &&
373 			TD_IS_RUNNING(tdwait)) {
374 			gen = record->er_gen;
375 			thread_unlock(td);
376 			do {
377 				cpu_spinwait();
378 			} while (tdwait == TAILQ_FIRST(&record->er_tdlist) &&
379 					 gen == record->er_gen && TD_IS_RUNNING(tdwait) &&
380 					 spincount++ < MAX_ADAPTIVE_SPIN);
381 			thread_lock(td);
382 			return;
383 		}
384 
385 		/*
386 		 * Being on the same CPU as that of the record on which
387 		 * we need to wait allows us access to the thread
388 		 * list associated with that CPU. We can then examine the
389 		 * oldest thread in the queue and wait on its turnstile
390 		 * until it resumes and so on until a grace period
391 		 * elapses.
392 		 *
393 		 */
394 		counter_u64_add(migrate_count, 1);
395 		sched_bind(td, record->er_cpuid);
396 		/*
397 		 * At this point we need to return to the ck code
398 		 * to scan to see if a grace period has elapsed.
399 		 * We can't move on to check the thread list, because
400 		 * in the meantime new threads may have arrived that
401 		 * in fact belong to a different epoch.
402 		 */
403 		return;
404 	}
405 	/*
406 	 * Try to find a thread in an epoch section on this CPU
407 	 * waiting on a turnstile. Otherwise find the lowest
408 	 * priority thread (highest prio value) and drop our priority
409 	 * to match to allow it to run.
410 	 */
411 	TAILQ_FOREACH(tdwait, &record->er_tdlist, td_epochq) {
412 		/*
413 		 * Propagate our priority to any other waiters to prevent us
414 		 * from starving them. They will have their original priority
415 		 * restore on exit from epoch_wait().
416 		 */
417 		if (!TD_IS_INHIBITED(tdwait) && tdwait->td_priority > td->td_priority) {
418 			critical_enter();
419 			thread_unlock(td);
420 			thread_lock(tdwait);
421 			sched_prio(tdwait, td->td_priority);
422 			thread_unlock(tdwait);
423 			thread_lock(td);
424 			critical_exit();
425 		}
426 		if (TD_IS_INHIBITED(tdwait) && TD_ON_LOCK(tdwait) &&
427 			((ts = tdwait->td_blocked) != NULL)) {
428 			/*
429 			 * We unlock td to allow turnstile_wait to reacquire the
430 			 * the thread lock. Before unlocking it we enter a critical
431 			 * section to prevent preemption after we reenable interrupts
432 			 * by dropping the thread lock in order to prevent tdwait
433 			 * from getting to run.
434 			 */
435 			critical_enter();
436 			thread_unlock(td);
437 			owner = turnstile_lock(ts, &lock);
438 			/*
439 			 * The owner pointer indicates that the lock succeeded. Only
440 			 * in case we hold the lock and the turnstile we locked is still
441 			 * the one that tdwait is blocked on can we continue. Otherwise
442 			 * The turnstile pointer has been changed out from underneath
443 			 * us, as in the case where the lock holder has signalled tdwait,
444 			 * and we need to continue.
445 			 */
446 			if (owner != NULL && ts == tdwait->td_blocked) {
447 				MPASS(TD_IS_INHIBITED(tdwait) && TD_ON_LOCK(tdwait));
448 				critical_exit();
449 				turnstile_wait(ts, owner, tdwait->td_tsqueue);
450 				counter_u64_add(turnstile_count, 1);
451 				thread_lock(td);
452 				return;
453 			} else if (owner != NULL)
454 				turnstile_unlock(ts, lock);
455 			thread_lock(td);
456 			critical_exit();
457 			KASSERT(td->td_locks == 0,
458 					("%d locks held", td->td_locks));
459 		}
460 	}
461 	/*
462 	 * We didn't find any threads actually blocked on a lock
463 	 * so we have nothing to do except context switch away.
464 	 */
465 	counter_u64_add(switch_count, 1);
466 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
467 
468 	/*
469 	 * Release the thread lock while yielding to
470 	 * allow other threads to acquire the lock
471 	 * pointed to by TDQ_LOCKPTR(td). Else a
472 	 * deadlock like situation might happen. (HPS)
473 	 */
474 	thread_unlock(td);
475 	thread_lock(td);
476 }
477 
478 void
479 epoch_wait_preempt(epoch_t epoch)
480 {
481 	struct thread *td;
482 	int was_bound;
483 	int old_cpu;
484 	int old_pinned;
485 	u_char old_prio;
486 #ifdef INVARIANTS
487 	int locks;
488 
489 	locks = curthread->td_locks;
490 #endif
491 
492 	MPASS(cold || epoch != NULL);
493 	INIT_CHECK(epoch);
494 
495 	MPASS(epoch->e_flags & EPOCH_PREEMPT);
496 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
497 	    "epoch_wait() can sleep");
498 
499 	td = curthread;
500 	KASSERT(td->td_epochnest == 0, ("epoch_wait() in the middle of an epoch section"));
501 	thread_lock(td);
502 
503 	DROP_GIANT();
504 
505 	old_cpu = PCPU_GET(cpuid);
506 	old_pinned = td->td_pinned;
507 	old_prio = td->td_priority;
508 	was_bound = sched_is_bound(td);
509 	sched_unbind(td);
510 	td->td_pinned = 0;
511 	sched_bind(td, old_cpu);
512 
513 	ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_preempt, NULL);
514 
515 	/* restore CPU binding, if any */
516 	if (was_bound != 0) {
517 		sched_bind(td, old_cpu);
518 	} else {
519 		/* get thread back to initial CPU, if any */
520 		if (old_pinned != 0)
521 			sched_bind(td, old_cpu);
522 		sched_unbind(td);
523 	}
524 	/* restore pinned after bind */
525 	td->td_pinned = old_pinned;
526 
527 	/* restore thread priority */
528 	sched_prio(td, old_prio);
529 	thread_unlock(td);
530 	PICKUP_GIANT();
531 	KASSERT(td->td_locks == locks,
532 			("%d residual locks held", td->td_locks - locks));
533 }
534 
535 static void
536 epoch_block_handler(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
537 					void *arg __unused)
538 {
539 	cpu_spinwait();
540 }
541 
542 void
543 epoch_wait(epoch_t epoch)
544 {
545 
546 	MPASS(cold || epoch != NULL);
547 	INIT_CHECK(epoch);
548 	MPASS(epoch->e_flags == 0);
549 	critical_enter();
550 	ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
551 	critical_exit();
552 }
553 
554 void
555 epoch_call(epoch_t epoch, epoch_context_t ctx, void (*callback) (epoch_context_t))
556 {
557 	struct epoch_pcpu_state *eps;
558 	ck_epoch_entry_t *cb;
559 
560 	cb = (void *)ctx;
561 
562 	MPASS(callback);
563 	/* too early in boot to have epoch set up */
564 	if (__predict_false(epoch == NULL))
565 		goto boottime;
566 
567 	critical_enter();
568 	*DPCPU_PTR(epoch_cb_count) += 1;
569 	eps = epoch->e_pcpu[curcpu];
570 	ck_epoch_call(&eps->eps_record.er_record, cb, (ck_epoch_cb_t*)callback);
571 	critical_exit();
572 	return;
573  boottime:
574 	callback(ctx);
575 }
576 
577 static void
578 epoch_call_task(void *arg __unused)
579 {
580 	ck_stack_entry_t *cursor, *head, *next;
581 	ck_epoch_record_t *record;
582 	epoch_t epoch;
583 	ck_stack_t cb_stack;
584 	int i, npending, total;
585 
586 	ck_stack_init(&cb_stack);
587 	critical_enter();
588 	epoch_enter(global_epoch);
589 	for (total = i = 0; i < epoch_count; i++) {
590 		if (__predict_false((epoch = allepochs[i]) == NULL))
591 			continue;
592 		record = &epoch->e_pcpu[curcpu]->eps_record.er_record;
593 		if ((npending = record->n_pending) == 0)
594 			continue;
595 		ck_epoch_poll_deferred(record, &cb_stack);
596 		total += npending - record->n_pending;
597 	}
598 	epoch_exit(global_epoch);
599 	*DPCPU_PTR(epoch_cb_count) -= total;
600 	critical_exit();
601 
602 	counter_u64_add(epoch_call_count, total);
603 	counter_u64_add(epoch_call_task_count, 1);
604 
605 	head = ck_stack_batch_pop_npsc(&cb_stack);
606 	for (cursor = head; cursor != NULL; cursor = next) {
607 		struct ck_epoch_entry *entry =
608 		    ck_epoch_entry_container(cursor);
609 		next = CK_STACK_NEXT(cursor);
610 		entry->function(entry);
611 	}
612 }
613 
614 int
615 in_epoch(void)
616 {
617 	return (curthread->td_epochnest != 0);
618 }
619