xref: /freebsd/sys/kern/subr_epoch.c (revision b71a5db306ab1ad1e23c7f9e3c949c6218f43455)
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_critical;
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, count, 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 	count = 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_critical = epoch_alloc(EPOCH_CRITICAL);
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_critical(global_epoch_critical);
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_internal(epoch_t epoch, struct thread *td)
267 {
268 	struct epoch_pcpu_state *eps;
269 
270 	INIT_CHECK(epoch);
271 	critical_enter();
272 	td->td_pre_epoch_prio = td->td_priority;
273 	eps = epoch->e_pcpu[curcpu];
274 #ifdef INVARIANTS
275 	MPASS(td->td_epochnest < UCHAR_MAX - 2);
276 	if (td->td_epochnest > 1) {
277 		struct thread *curtd;
278 		int found = 0;
279 
280 		TAILQ_FOREACH(curtd, &eps->eps_record.er_tdlist, td_epochq)
281 			if (curtd == td)
282 				found = 1;
283 		KASSERT(found, ("recursing on a second epoch"));
284 		critical_exit();
285 		return;
286 	}
287 #endif
288 	TAILQ_INSERT_TAIL(&eps->eps_record.er_tdlist, td, td_epochq);
289 	sched_pin();
290 	ck_epoch_begin(&eps->eps_record.er_record, (ck_epoch_section_t*)&td->td_epoch_section);
291 	critical_exit();
292 }
293 
294 
295 void
296 epoch_enter_critical(epoch_t epoch)
297 {
298 	ck_epoch_record_t *record;
299 	ck_epoch_section_t *section;
300 	struct thread *td;
301 
302 	section = NULL;
303 	td = curthread;
304 	critical_enter();
305 	if (__predict_true(td->td_epochnest++ == 0))
306 		section = (ck_epoch_section_t*)&td->td_epoch_section;
307 
308 	record = &epoch->e_pcpu[curcpu]->eps_record.er_record;
309 	ck_epoch_begin(record, section);
310 }
311 
312 void
313 epoch_exit_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 	ck_epoch_end(&eps->eps_record.er_record, (ck_epoch_section_t*)&td->td_epoch_section);
323 	TAILQ_REMOVE(&eps->eps_record.er_tdlist, td, td_epochq);
324 	eps->eps_record.er_gen++;
325 	sched_unpin();
326 	if (__predict_false(td->td_pre_epoch_prio != td->td_priority)) {
327 		thread_lock(td);
328 		sched_prio(td, td->td_pre_epoch_prio);
329 		thread_unlock(td);
330 	}
331 	critical_exit();
332 }
333 
334 void
335 epoch_exit_critical(epoch_t epoch)
336 {
337 	ck_epoch_record_t *record;
338 	ck_epoch_section_t *section;
339 	struct thread *td;
340 
341 	section = NULL;
342 	td = curthread;
343 	MPASS(td->td_critnest);
344 	if (__predict_true(td->td_epochnest-- == 1))
345 		section = (ck_epoch_section_t*)&td->td_epoch_section;
346 	record = &epoch->e_pcpu[curcpu]->eps_record.er_record;
347 	ck_epoch_end(record, section);
348 	critical_exit();
349 }
350 
351 /*
352  * epoch_block_handler is a callback from the ck code when another thread is
353  * currently in an epoch section.
354  */
355 static void
356 epoch_block_handler(struct ck_epoch *global __unused, ck_epoch_record_t *cr,
357 					void *arg __unused)
358 {
359 	epoch_record_t record;
360 	struct epoch_pcpu_state *eps;
361 	struct thread *td, *tdwait, *owner;
362 	struct turnstile *ts;
363 	struct lock_object *lock;
364 	int spincount, gen;
365 
366 	eps = arg;
367 	record = __containerof(cr, struct epoch_record, er_record);
368 	td = curthread;
369 	spincount = 0;
370 	counter_u64_add(block_count, 1);
371 	if (record->er_cpuid != curcpu) {
372 		/*
373 		 * If the head of the list is running, we can wait for it
374 		 * to remove itself from the list and thus save us the
375 		 * overhead of a migration
376 		 */
377 		if ((tdwait = TAILQ_FIRST(&record->er_tdlist)) != NULL &&
378 			TD_IS_RUNNING(tdwait)) {
379 			gen = record->er_gen;
380 			thread_unlock(td);
381 			do {
382 				cpu_spinwait();
383 			} while (tdwait == TAILQ_FIRST(&record->er_tdlist) &&
384 					 gen == record->er_gen && TD_IS_RUNNING(tdwait) &&
385 					 spincount++ < MAX_ADAPTIVE_SPIN);
386 			thread_lock(td);
387 			return;
388 		}
389 
390 		/*
391 		 * Being on the same CPU as that of the record on which
392 		 * we need to wait allows us access to the thread
393 		 * list associated with that CPU. We can then examine the
394 		 * oldest thread in the queue and wait on its turnstile
395 		 * until it resumes and so on until a grace period
396 		 * elapses.
397 		 *
398 		 */
399 		counter_u64_add(migrate_count, 1);
400 		sched_bind(td, record->er_cpuid);
401 		/*
402 		 * At this point we need to return to the ck code
403 		 * to scan to see if a grace period has elapsed.
404 		 * We can't move on to check the thread list, because
405 		 * in the meantime new threads may have arrived that
406 		 * in fact belong to a different epoch.
407 		 */
408 		return;
409 	}
410 	/*
411 	 * Try to find a thread in an epoch section on this CPU
412 	 * waiting on a turnstile. Otherwise find the lowest
413 	 * priority thread (highest prio value) and drop our priority
414 	 * to match to allow it to run.
415 	 */
416 	TAILQ_FOREACH(tdwait, &record->er_tdlist, td_epochq) {
417 		/*
418 		 * Propagate our priority to any other waiters to prevent us
419 		 * from starving them. They will have their original priority
420 		 * restore on exit from epoch_wait().
421 		 */
422 		if (!TD_IS_INHIBITED(tdwait) && tdwait->td_priority > td->td_priority) {
423 			critical_enter();
424 			thread_unlock(td);
425 			thread_lock(tdwait);
426 			sched_prio(tdwait, td->td_priority);
427 			thread_unlock(tdwait);
428 			thread_lock(td);
429 			critical_exit();
430 		}
431 		if (TD_IS_INHIBITED(tdwait) && TD_ON_LOCK(tdwait) &&
432 			((ts = tdwait->td_blocked) != NULL)) {
433 			/*
434 			 * We unlock td to allow turnstile_wait to reacquire the
435 			 * the thread lock. Before unlocking it we enter a critical
436 			 * section to prevent preemption after we reenable interrupts
437 			 * by dropping the thread lock in order to prevent tdwait
438 			 * from getting to run.
439 			 */
440 			critical_enter();
441 			thread_unlock(td);
442 			owner = turnstile_lock(ts, &lock);
443 			/*
444 			 * The owner pointer indicates that the lock succeeded. Only
445 			 * in case we hold the lock and the turnstile we locked is still
446 			 * the one that tdwait is blocked on can we continue. Otherwise
447 			 * The turnstile pointer has been changed out from underneath
448 			 * us, as in the case where the lock holder has signalled tdwait,
449 			 * and we need to continue.
450 			 */
451 			if (owner != NULL && ts == tdwait->td_blocked) {
452 				MPASS(TD_IS_INHIBITED(tdwait) && TD_ON_LOCK(tdwait));
453 				critical_exit();
454 				turnstile_wait(ts, owner, tdwait->td_tsqueue);
455 				counter_u64_add(turnstile_count, 1);
456 				thread_lock(td);
457 				return;
458 			} else if (owner != NULL)
459 				turnstile_unlock(ts, lock);
460 			thread_lock(td);
461 			critical_exit();
462 			KASSERT(td->td_locks == 0,
463 					("%d locks held", td->td_locks));
464 		}
465 	}
466 	/*
467 	 * We didn't find any threads actually blocked on a lock
468 	 * so we have nothing to do except context switch away.
469 	 */
470 	counter_u64_add(switch_count, 1);
471 	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
472 
473 	/*
474 	 * Release the thread lock while yielding to
475 	 * allow other threads to acquire the lock
476 	 * pointed to by TDQ_LOCKPTR(td). Else a
477 	 * deadlock like situation might happen. (HPS)
478 	 */
479 	thread_unlock(td);
480 	thread_lock(td);
481 }
482 
483 void
484 epoch_wait(epoch_t epoch)
485 {
486 	struct thread *td;
487 	int was_bound;
488 	int old_cpu;
489 	int old_pinned;
490 	u_char old_prio;
491 #ifdef INVARIANTS
492 	int locks;
493 
494 	locks = curthread->td_locks;
495 #endif
496 	INIT_CHECK(epoch);
497 
498 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
499 	    "epoch_wait() can sleep");
500 
501 	td = curthread;
502 	KASSERT(td->td_epochnest == 0, ("epoch_wait() in the middle of an epoch section"));
503 	thread_lock(td);
504 
505 	DROP_GIANT();
506 
507 	old_cpu = PCPU_GET(cpuid);
508 	old_pinned = td->td_pinned;
509 	old_prio = td->td_priority;
510 	was_bound = sched_is_bound(td);
511 	sched_unbind(td);
512 	td->td_pinned = 0;
513 	sched_bind(td, old_cpu);
514 
515 	ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
516 
517 	/* restore CPU binding, if any */
518 	if (was_bound != 0) {
519 		sched_bind(td, old_cpu);
520 	} else {
521 		/* get thread back to initial CPU, if any */
522 		if (old_pinned != 0)
523 			sched_bind(td, old_cpu);
524 		sched_unbind(td);
525 	}
526 	/* restore pinned after bind */
527 	td->td_pinned = old_pinned;
528 
529 	/* restore thread priority */
530 	sched_prio(td, old_prio);
531 	thread_unlock(td);
532 	PICKUP_GIANT();
533 	KASSERT(td->td_locks == locks,
534 			("%d residual locks held", td->td_locks - locks));
535 }
536 
537 static void
538 epoch_block_handler_critical(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
539 					void *arg __unused)
540 {
541 	cpu_spinwait();
542 }
543 
544 void
545 epoch_wait_critical(epoch_t epoch)
546 {
547 
548 	MPASS(epoch->e_flags & EPOCH_CRITICAL);
549 	critical_enter();
550 	ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_critical, 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_critical(global_epoch_critical);
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_critical(global_epoch_critical);
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