2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy <mmacy@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/counter.h>
35 #include <sys/epoch.h>
36 #include <sys/gtaskqueue.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
44 #include <sys/sched.h>
47 #include <sys/sysctl.h>
48 #include <sys/turnstile.h>
50 #include <machine/stdarg.h>
51 #include <sys/stack.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_kern.h>
62 #define EPOCH_ALIGN CACHE_LINE_SIZE*2
64 #define EPOCH_ALIGN CACHE_LINE_SIZE
67 TAILQ_HEAD (epoch_tdlist, epoch_tracker);
68 typedef struct epoch_record {
69 ck_epoch_record_t er_record;
70 struct epoch_context er_drain_ctx;
71 struct epoch *er_parent;
72 volatile struct epoch_tdlist er_tdlist;
73 volatile uint32_t er_gen;
75 } __aligned(EPOCH_ALIGN) *epoch_record_t;
78 struct ck_epoch e_epoch __aligned(EPOCH_ALIGN);
79 epoch_record_t e_pcpu_record;
83 struct mtx e_drain_mtx;
84 volatile int e_drain_count;
88 /* arbitrary --- needs benchmarking */
89 #define MAX_ADAPTIVE_SPIN 100
92 CTASSERT(sizeof(ck_epoch_entry_t) == sizeof(struct epoch_context));
93 SYSCTL_NODE(_kern, OID_AUTO, epoch, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 SYSCTL_NODE(_kern_epoch, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
99 static counter_u64_t block_count;
101 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, nblocked, CTLFLAG_RW,
102 &block_count, "# of times a thread was in an epoch when epoch_wait was called");
103 static counter_u64_t migrate_count;
105 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, migrations, CTLFLAG_RW,
106 &migrate_count, "# of times thread was migrated to another CPU in epoch_wait");
107 static counter_u64_t turnstile_count;
109 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, ncontended, CTLFLAG_RW,
110 &turnstile_count, "# of times a thread was blocked on a lock in an epoch during an epoch_wait");
111 static counter_u64_t switch_count;
113 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, switches, CTLFLAG_RW,
114 &switch_count, "# of times a thread voluntarily context switched in epoch_wait");
115 static counter_u64_t epoch_call_count;
117 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_calls, CTLFLAG_RW,
118 &epoch_call_count, "# of times a callback was deferred");
119 static counter_u64_t epoch_call_task_count;
121 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_call_tasks, CTLFLAG_RW,
122 &epoch_call_task_count, "# of times a callback task was run");
124 TAILQ_HEAD (threadlist, thread);
126 CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
127 ck_epoch_entry_container)
129 static struct epoch epoch_array[MAX_EPOCHS];
131 DPCPU_DEFINE(struct grouptask, epoch_cb_task);
132 DPCPU_DEFINE(int, epoch_cb_count);
134 static __read_mostly int inited;
135 __read_mostly epoch_t global_epoch;
136 __read_mostly epoch_t global_epoch_preempt;
138 static void epoch_call_task(void *context __unused);
139 static uma_zone_t pcpu_zone_record;
141 static struct sx epoch_sx;
143 #define EPOCH_LOCK() sx_xlock(&epoch_sx)
144 #define EPOCH_UNLOCK() sx_xunlock(&epoch_sx)
148 RB_ENTRY(stackentry) se_node;
149 struct stack se_stack;
153 stackentry_compare(struct stackentry *a, struct stackentry *b)
156 if (a->se_stack.depth > b->se_stack.depth)
158 if (a->se_stack.depth < b->se_stack.depth)
160 for (int i = 0; i < a->se_stack.depth; i++) {
161 if (a->se_stack.pcs[i] > b->se_stack.pcs[i])
163 if (a->se_stack.pcs[i] < b->se_stack.pcs[i])
170 RB_HEAD(stacktree, stackentry) epoch_stacks = RB_INITIALIZER(&epoch_stacks);
171 RB_GENERATE_STATIC(stacktree, stackentry, se_node, stackentry_compare);
173 static struct mtx epoch_stacks_lock;
174 MTX_SYSINIT(epochstacks, &epoch_stacks_lock, "epoch_stacks", MTX_DEF);
176 static bool epoch_trace_stack_print = true;
177 SYSCTL_BOOL(_kern_epoch, OID_AUTO, trace_stack_print, CTLFLAG_RWTUN,
178 &epoch_trace_stack_print, 0, "Print stack traces on epoch reports");
180 static void epoch_trace_report(const char *fmt, ...) __printflike(1, 2);
182 epoch_trace_report(const char *fmt, ...)
185 struct stackentry se, *new;
187 stack_zero(&se.se_stack); /* XXX: is it really needed? */
188 stack_save(&se.se_stack);
190 /* Tree is never reduced - go lockless. */
191 if (RB_FIND(stacktree, &epoch_stacks, &se) != NULL)
194 new = malloc(sizeof(*new), M_STACK, M_NOWAIT);
196 bcopy(&se.se_stack, &new->se_stack, sizeof(struct stack));
198 mtx_lock(&epoch_stacks_lock);
199 new = RB_INSERT(stacktree, &epoch_stacks, new);
200 mtx_unlock(&epoch_stacks_lock);
206 (void)vprintf(fmt, ap);
208 if (epoch_trace_stack_print)
209 stack_print_ddb(&se.se_stack);
213 epoch_trace_enter(struct thread *td, epoch_t epoch, epoch_tracker_t et,
214 const char *file, int line)
218 SLIST_FOREACH(iet, &td->td_epochs, et_tlink)
219 if (iet->et_epoch == epoch)
220 epoch_trace_report("Recursively entering epoch %s "
221 "at %s:%d, previously entered at %s:%d\n",
222 epoch->e_name, file, line,
223 iet->et_file, iet->et_line);
224 et->et_epoch = epoch;
227 SLIST_INSERT_HEAD(&td->td_epochs, et, et_tlink);
231 epoch_trace_exit(struct thread *td, epoch_t epoch, epoch_tracker_t et,
232 const char *file, int line)
235 if (SLIST_FIRST(&td->td_epochs) != et) {
236 epoch_trace_report("Exiting epoch %s in a not nested order "
237 "at %s:%d. Most recently entered %s at %s:%d\n",
240 SLIST_FIRST(&td->td_epochs)->et_epoch->e_name,
241 SLIST_FIRST(&td->td_epochs)->et_file,
242 SLIST_FIRST(&td->td_epochs)->et_line);
243 /* This will panic if et is not anywhere on td_epochs. */
244 SLIST_REMOVE(&td->td_epochs, et, epoch_tracker, et_tlink);
246 SLIST_REMOVE_HEAD(&td->td_epochs, et_tlink);
249 /* Used by assertions that check thread state before going to sleep. */
251 epoch_trace_list(struct thread *td)
255 SLIST_FOREACH(iet, &td->td_epochs, et_tlink)
256 printf("Epoch %s entered at %s:%d\n", iet->et_epoch->e_name,
257 iet->et_file, iet->et_line);
259 #endif /* EPOCH_TRACE */
262 epoch_init(void *arg __unused)
266 block_count = counter_u64_alloc(M_WAITOK);
267 migrate_count = counter_u64_alloc(M_WAITOK);
268 turnstile_count = counter_u64_alloc(M_WAITOK);
269 switch_count = counter_u64_alloc(M_WAITOK);
270 epoch_call_count = counter_u64_alloc(M_WAITOK);
271 epoch_call_task_count = counter_u64_alloc(M_WAITOK);
273 pcpu_zone_record = uma_zcreate("epoch_record pcpu",
274 sizeof(struct epoch_record), NULL, NULL, NULL, NULL,
275 UMA_ALIGN_PTR, UMA_ZONE_PCPU);
277 GROUPTASK_INIT(DPCPU_ID_PTR(cpu, epoch_cb_task), 0,
278 epoch_call_task, NULL);
279 taskqgroup_attach_cpu(qgroup_softirq,
280 DPCPU_ID_PTR(cpu, epoch_cb_task), NULL, cpu, NULL, NULL,
284 SLIST_INIT(&thread0.td_epochs);
286 sx_init(&epoch_sx, "epoch-sx");
288 global_epoch = epoch_alloc("Global", 0);
289 global_epoch_preempt = epoch_alloc("Global preemptible", EPOCH_PREEMPT);
291 SYSINIT(epoch, SI_SUB_EPOCH, SI_ORDER_FIRST, epoch_init, NULL);
293 #if !defined(EARLY_AP_STARTUP)
295 epoch_init_smp(void *dummy __unused)
299 SYSINIT(epoch_smp, SI_SUB_SMP + 1, SI_ORDER_FIRST, epoch_init_smp, NULL);
303 epoch_ctor(epoch_t epoch)
308 epoch->e_pcpu_record = uma_zalloc_pcpu(pcpu_zone_record, M_WAITOK);
310 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
311 bzero(er, sizeof(*er));
312 ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
313 TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
315 er->er_parent = epoch;
320 epoch_adjust_prio(struct thread *td, u_char prio)
324 sched_prio(td, prio);
329 epoch_alloc(const char *name, int flags)
336 if (__predict_false(!inited))
337 panic("%s called too early in boot", __func__);
342 * Find a free index in the epoch array. If no free index is
343 * found, try to use the index after the last one.
347 * If too many epochs are currently allocated,
350 if (i == MAX_EPOCHS) {
354 if (epoch_array[i].e_in_use == 0)
358 epoch = epoch_array + i;
359 ck_epoch_init(&epoch->e_epoch);
361 epoch->e_flags = flags;
362 epoch->e_name = name;
363 sx_init(&epoch->e_drain_sx, "epoch-drain-sx");
364 mtx_init(&epoch->e_drain_mtx, "epoch-drain-mtx", NULL, MTX_DEF);
367 * Set e_in_use last, because when this field is set the
368 * epoch_call_task() function will start scanning this epoch
371 atomic_store_rel_int(&epoch->e_in_use, 1);
378 epoch_free(epoch_t epoch)
383 MPASS(epoch->e_in_use != 0);
385 epoch_drain_callbacks(epoch);
387 atomic_store_rel_int(&epoch->e_in_use, 0);
389 * Make sure the epoch_call_task() function see e_in_use equal
390 * to zero, by calling epoch_wait() on the global_epoch:
392 epoch_wait(global_epoch);
393 uma_zfree_pcpu(pcpu_zone_record, epoch->e_pcpu_record);
394 mtx_destroy(&epoch->e_drain_mtx);
395 sx_destroy(&epoch->e_drain_sx);
396 memset(epoch, 0, sizeof(*epoch));
401 static epoch_record_t
402 epoch_currecord(epoch_t epoch)
405 return (zpcpu_get(epoch->e_pcpu_record));
408 #define INIT_CHECK(epoch) \
410 if (__predict_false((epoch) == NULL)) \
415 _epoch_enter_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
417 struct epoch_record *er;
420 MPASS(cold || epoch != NULL);
421 MPASS(epoch->e_flags & EPOCH_PREEMPT);
423 MPASS((vm_offset_t)et >= td->td_kstack &&
424 (vm_offset_t)et + sizeof(struct epoch_tracker) <=
425 td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
429 epoch_trace_enter(td, epoch, et, file, line);
432 THREAD_NO_SLEEPING();
435 td->td_pre_epoch_prio = td->td_priority;
436 er = epoch_currecord(epoch);
437 TAILQ_INSERT_TAIL(&er->er_tdlist, et, et_link);
438 ck_epoch_begin(&er->er_record, &et->et_section);
443 epoch_enter(epoch_t epoch)
447 MPASS(cold || epoch != NULL);
450 er = epoch_currecord(epoch);
451 ck_epoch_begin(&er->er_record, NULL);
455 _epoch_exit_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
457 struct epoch_record *er;
464 THREAD_SLEEPING_OK();
465 er = epoch_currecord(epoch);
466 MPASS(epoch->e_flags & EPOCH_PREEMPT);
468 MPASS(et->et_td == td);
470 et->et_td = (void*)0xDEADBEEF;
472 ck_epoch_end(&er->er_record, &et->et_section);
473 TAILQ_REMOVE(&er->er_tdlist, et, et_link);
475 if (__predict_false(td->td_pre_epoch_prio != td->td_priority))
476 epoch_adjust_prio(td, td->td_pre_epoch_prio);
479 epoch_trace_exit(td, epoch, et, file, line);
484 epoch_exit(epoch_t epoch)
489 er = epoch_currecord(epoch);
490 ck_epoch_end(&er->er_record, NULL);
495 * epoch_block_handler_preempt() is a callback from the CK code when another
496 * thread is currently in an epoch section.
499 epoch_block_handler_preempt(struct ck_epoch *global __unused,
500 ck_epoch_record_t *cr, void *arg __unused)
502 epoch_record_t record;
503 struct thread *td, *owner, *curwaittd;
504 struct epoch_tracker *tdwait;
505 struct turnstile *ts;
506 struct lock_object *lock;
508 int locksheld __unused;
510 record = __containerof(cr, struct epoch_record, er_record);
512 locksheld = td->td_locks;
514 counter_u64_add(block_count, 1);
516 * We lost a race and there's no longer any threads
517 * on the CPU in an epoch section.
519 if (TAILQ_EMPTY(&record->er_tdlist))
522 if (record->er_cpuid != curcpu) {
524 * If the head of the list is running, we can wait for it
525 * to remove itself from the list and thus save us the
526 * overhead of a migration
528 gen = record->er_gen;
531 * We can't actually check if the waiting thread is running
532 * so we simply poll for it to exit before giving up and
537 } while (!TAILQ_EMPTY(&record->er_tdlist) &&
538 gen == record->er_gen &&
539 spincount++ < MAX_ADAPTIVE_SPIN);
542 * If the generation has changed we can poll again
543 * otherwise we need to migrate.
545 if (gen != record->er_gen)
548 * Being on the same CPU as that of the record on which
549 * we need to wait allows us access to the thread
550 * list associated with that CPU. We can then examine the
551 * oldest thread in the queue and wait on its turnstile
552 * until it resumes and so on until a grace period
556 counter_u64_add(migrate_count, 1);
557 sched_bind(td, record->er_cpuid);
559 * At this point we need to return to the ck code
560 * to scan to see if a grace period has elapsed.
561 * We can't move on to check the thread list, because
562 * in the meantime new threads may have arrived that
563 * in fact belong to a different epoch.
568 * Try to find a thread in an epoch section on this CPU
569 * waiting on a turnstile. Otherwise find the lowest
570 * priority thread (highest prio value) and drop our priority
571 * to match to allow it to run.
573 TAILQ_FOREACH(tdwait, &record->er_tdlist, et_link) {
575 * Propagate our priority to any other waiters to prevent us
576 * from starving them. They will have their original priority
577 * restore on exit from epoch_wait().
579 curwaittd = tdwait->et_td;
580 if (!TD_IS_INHIBITED(curwaittd) && curwaittd->td_priority > td->td_priority) {
583 thread_lock(curwaittd);
584 sched_prio(curwaittd, td->td_priority);
585 thread_unlock(curwaittd);
589 if (TD_IS_INHIBITED(curwaittd) && TD_ON_LOCK(curwaittd) &&
590 ((ts = curwaittd->td_blocked) != NULL)) {
592 * We unlock td to allow turnstile_wait to reacquire
593 * the thread lock. Before unlocking it we enter a
594 * critical section to prevent preemption after we
595 * reenable interrupts by dropping the thread lock in
596 * order to prevent curwaittd from getting to run.
601 if (turnstile_lock(ts, &lock, &owner)) {
602 if (ts == curwaittd->td_blocked) {
603 MPASS(TD_IS_INHIBITED(curwaittd) &&
604 TD_ON_LOCK(curwaittd));
606 turnstile_wait(ts, owner,
607 curwaittd->td_tsqueue);
608 counter_u64_add(turnstile_count, 1);
612 turnstile_unlock(ts, lock);
616 KASSERT(td->td_locks == locksheld,
617 ("%d extra locks held", td->td_locks - locksheld));
621 * We didn't find any threads actually blocked on a lock
622 * so we have nothing to do except context switch away.
624 counter_u64_add(switch_count, 1);
625 mi_switch(SW_VOL | SWT_RELINQUISH);
627 * It is important the thread lock is dropped while yielding
628 * to allow other threads to acquire the lock pointed to by
629 * TDQ_LOCKPTR(td). Currently mi_switch() will unlock the
630 * thread lock before returning. Else a deadlock like
631 * situation might happen.
637 epoch_wait_preempt(epoch_t epoch)
646 MPASS(cold || epoch != NULL);
650 locks = curthread->td_locks;
651 MPASS(epoch->e_flags & EPOCH_PREEMPT);
652 if ((epoch->e_flags & EPOCH_LOCKED) == 0)
653 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
654 "epoch_wait() can be long running");
655 KASSERT(!in_epoch(epoch), ("epoch_wait_preempt() called in the middle "
656 "of an epoch section of the same epoch"));
661 old_cpu = PCPU_GET(cpuid);
662 old_pinned = td->td_pinned;
663 old_prio = td->td_priority;
664 was_bound = sched_is_bound(td);
667 sched_bind(td, old_cpu);
669 ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_preempt,
672 /* restore CPU binding, if any */
673 if (was_bound != 0) {
674 sched_bind(td, old_cpu);
676 /* get thread back to initial CPU, if any */
678 sched_bind(td, old_cpu);
681 /* restore pinned after bind */
682 td->td_pinned = old_pinned;
684 /* restore thread priority */
685 sched_prio(td, old_prio);
688 KASSERT(td->td_locks == locks,
689 ("%d residual locks held", td->td_locks - locks));
693 epoch_block_handler(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
700 epoch_wait(epoch_t epoch)
703 MPASS(cold || epoch != NULL);
705 MPASS(epoch->e_flags == 0);
707 ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
712 epoch_call(epoch_t epoch, epoch_callback_t callback, epoch_context_t ctx)
715 ck_epoch_entry_t *cb;
720 /* too early in boot to have epoch set up */
721 if (__predict_false(epoch == NULL))
723 #if !defined(EARLY_AP_STARTUP)
724 if (__predict_false(inited < 2))
729 *DPCPU_PTR(epoch_cb_count) += 1;
730 er = epoch_currecord(epoch);
731 ck_epoch_call(&er->er_record, cb, (ck_epoch_cb_t *)callback);
739 epoch_call_task(void *arg __unused)
741 ck_stack_entry_t *cursor, *head, *next;
742 ck_epoch_record_t *record;
746 int i, npending, total;
748 ck_stack_init(&cb_stack);
750 epoch_enter(global_epoch);
751 for (total = i = 0; i != MAX_EPOCHS; i++) {
752 epoch = epoch_array + i;
754 atomic_load_acq_int(&epoch->e_in_use) == 0))
756 er = epoch_currecord(epoch);
757 record = &er->er_record;
758 if ((npending = record->n_pending) == 0)
760 ck_epoch_poll_deferred(record, &cb_stack);
761 total += npending - record->n_pending;
763 epoch_exit(global_epoch);
764 *DPCPU_PTR(epoch_cb_count) -= total;
767 counter_u64_add(epoch_call_count, total);
768 counter_u64_add(epoch_call_task_count, 1);
770 head = ck_stack_batch_pop_npsc(&cb_stack);
771 for (cursor = head; cursor != NULL; cursor = next) {
772 struct ck_epoch_entry *entry =
773 ck_epoch_entry_container(cursor);
775 next = CK_STACK_NEXT(cursor);
776 entry->function(entry);
781 in_epoch_verbose(epoch_t epoch, int dump_onfail)
783 struct epoch_tracker *tdwait;
788 if (THREAD_CAN_SLEEP())
790 if (__predict_false((epoch) == NULL))
793 er = epoch_currecord(epoch);
794 TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
795 if (tdwait->et_td == td) {
801 MPASS(td->td_pinned);
802 printf("cpu: %d id: %d\n", curcpu, td->td_tid);
803 TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
804 printf("td_tid: %d ", tdwait->et_td->td_tid);
813 in_epoch(epoch_t epoch)
815 return (in_epoch_verbose(epoch, 0));
819 epoch_drain_cb(struct epoch_context *ctx)
821 struct epoch *epoch =
822 __containerof(ctx, struct epoch_record, er_drain_ctx)->er_parent;
824 if (atomic_fetchadd_int(&epoch->e_drain_count, -1) == 1) {
825 mtx_lock(&epoch->e_drain_mtx);
827 mtx_unlock(&epoch->e_drain_mtx);
832 epoch_drain_callbacks(epoch_t epoch)
841 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
842 "epoch_drain_callbacks() may sleep!");
844 /* too early in boot to have epoch set up */
845 if (__predict_false(epoch == NULL))
847 #if !defined(EARLY_AP_STARTUP)
848 if (__predict_false(inited < 2))
853 sx_xlock(&epoch->e_drain_sx);
854 mtx_lock(&epoch->e_drain_mtx);
858 old_cpu = PCPU_GET(cpuid);
859 old_pinned = td->td_pinned;
860 was_bound = sched_is_bound(td);
865 epoch->e_drain_count++;
867 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
869 epoch_call(epoch, &epoch_drain_cb, &er->er_drain_ctx);
872 /* restore CPU binding, if any */
873 if (was_bound != 0) {
874 sched_bind(td, old_cpu);
876 /* get thread back to initial CPU, if any */
878 sched_bind(td, old_cpu);
881 /* restore pinned after bind */
882 td->td_pinned = old_pinned;
886 while (epoch->e_drain_count != 0)
887 msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0);
889 mtx_unlock(&epoch->e_drain_mtx);
890 sx_xunlock(&epoch->e_drain_sx);