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;
76 /* Used to verify record ownership for non-preemptible epochs. */
79 } __aligned(EPOCH_ALIGN) *epoch_record_t;
82 struct ck_epoch e_epoch __aligned(EPOCH_ALIGN);
83 epoch_record_t e_pcpu_record;
87 struct mtx e_drain_mtx;
88 volatile int e_drain_count;
92 /* arbitrary --- needs benchmarking */
93 #define MAX_ADAPTIVE_SPIN 100
96 CTASSERT(sizeof(ck_epoch_entry_t) == sizeof(struct epoch_context));
97 SYSCTL_NODE(_kern, OID_AUTO, epoch, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
99 SYSCTL_NODE(_kern_epoch, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
103 static counter_u64_t block_count;
105 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, nblocked, CTLFLAG_RW,
106 &block_count, "# of times a thread was in an epoch when epoch_wait was called");
107 static counter_u64_t migrate_count;
109 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, migrations, CTLFLAG_RW,
110 &migrate_count, "# of times thread was migrated to another CPU in epoch_wait");
111 static counter_u64_t turnstile_count;
113 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, ncontended, CTLFLAG_RW,
114 &turnstile_count, "# of times a thread was blocked on a lock in an epoch during an epoch_wait");
115 static counter_u64_t switch_count;
117 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, switches, CTLFLAG_RW,
118 &switch_count, "# of times a thread voluntarily context switched in epoch_wait");
119 static counter_u64_t epoch_call_count;
121 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_calls, CTLFLAG_RW,
122 &epoch_call_count, "# of times a callback was deferred");
123 static counter_u64_t epoch_call_task_count;
125 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_call_tasks, CTLFLAG_RW,
126 &epoch_call_task_count, "# of times a callback task was run");
128 TAILQ_HEAD (threadlist, thread);
130 CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
131 ck_epoch_entry_container)
133 static struct epoch epoch_array[MAX_EPOCHS];
135 DPCPU_DEFINE(struct grouptask, epoch_cb_task);
136 DPCPU_DEFINE(int, epoch_cb_count);
138 static __read_mostly int inited;
139 __read_mostly epoch_t global_epoch;
140 __read_mostly epoch_t global_epoch_preempt;
142 static void epoch_call_task(void *context __unused);
143 static uma_zone_t pcpu_zone_record;
145 static struct sx epoch_sx;
147 #define EPOCH_LOCK() sx_xlock(&epoch_sx)
148 #define EPOCH_UNLOCK() sx_xunlock(&epoch_sx)
152 RB_ENTRY(stackentry) se_node;
153 struct stack se_stack;
157 stackentry_compare(struct stackentry *a, struct stackentry *b)
160 if (a->se_stack.depth > b->se_stack.depth)
162 if (a->se_stack.depth < b->se_stack.depth)
164 for (int i = 0; i < a->se_stack.depth; i++) {
165 if (a->se_stack.pcs[i] > b->se_stack.pcs[i])
167 if (a->se_stack.pcs[i] < b->se_stack.pcs[i])
174 RB_HEAD(stacktree, stackentry) epoch_stacks = RB_INITIALIZER(&epoch_stacks);
175 RB_GENERATE_STATIC(stacktree, stackentry, se_node, stackentry_compare);
177 static struct mtx epoch_stacks_lock;
178 MTX_SYSINIT(epochstacks, &epoch_stacks_lock, "epoch_stacks", MTX_DEF);
180 static bool epoch_trace_stack_print = true;
181 SYSCTL_BOOL(_kern_epoch, OID_AUTO, trace_stack_print, CTLFLAG_RWTUN,
182 &epoch_trace_stack_print, 0, "Print stack traces on epoch reports");
184 static void epoch_trace_report(const char *fmt, ...) __printflike(1, 2);
186 epoch_trace_report(const char *fmt, ...)
189 struct stackentry se, *new;
191 stack_zero(&se.se_stack); /* XXX: is it really needed? */
192 stack_save(&se.se_stack);
194 /* Tree is never reduced - go lockless. */
195 if (RB_FIND(stacktree, &epoch_stacks, &se) != NULL)
198 new = malloc(sizeof(*new), M_STACK, M_NOWAIT);
200 bcopy(&se.se_stack, &new->se_stack, sizeof(struct stack));
202 mtx_lock(&epoch_stacks_lock);
203 new = RB_INSERT(stacktree, &epoch_stacks, new);
204 mtx_unlock(&epoch_stacks_lock);
210 (void)vprintf(fmt, ap);
212 if (epoch_trace_stack_print)
213 stack_print_ddb(&se.se_stack);
217 epoch_trace_enter(struct thread *td, epoch_t epoch, epoch_tracker_t et,
218 const char *file, int line)
222 SLIST_FOREACH(iet, &td->td_epochs, et_tlink) {
223 if (iet->et_epoch != epoch)
225 epoch_trace_report("Recursively entering epoch %s "
226 "at %s:%d, previously entered at %s:%d\n",
227 epoch->e_name, file, line,
228 iet->et_file, iet->et_line);
230 et->et_epoch = epoch;
233 SLIST_INSERT_HEAD(&td->td_epochs, et, et_tlink);
237 epoch_trace_exit(struct thread *td, epoch_t epoch, epoch_tracker_t et,
238 const char *file, int line)
241 if (SLIST_FIRST(&td->td_epochs) != et) {
242 epoch_trace_report("Exiting epoch %s in a not nested order "
243 "at %s:%d. Most recently entered %s at %s:%d\n",
246 SLIST_FIRST(&td->td_epochs)->et_epoch->e_name,
247 SLIST_FIRST(&td->td_epochs)->et_file,
248 SLIST_FIRST(&td->td_epochs)->et_line);
249 /* This will panic if et is not anywhere on td_epochs. */
250 SLIST_REMOVE(&td->td_epochs, et, epoch_tracker, et_tlink);
252 SLIST_REMOVE_HEAD(&td->td_epochs, et_tlink);
255 /* Used by assertions that check thread state before going to sleep. */
257 epoch_trace_list(struct thread *td)
261 SLIST_FOREACH(iet, &td->td_epochs, et_tlink)
262 printf("Epoch %s entered at %s:%d\n", iet->et_epoch->e_name,
263 iet->et_file, iet->et_line);
265 #endif /* EPOCH_TRACE */
268 epoch_init(void *arg __unused)
272 block_count = counter_u64_alloc(M_WAITOK);
273 migrate_count = counter_u64_alloc(M_WAITOK);
274 turnstile_count = counter_u64_alloc(M_WAITOK);
275 switch_count = counter_u64_alloc(M_WAITOK);
276 epoch_call_count = counter_u64_alloc(M_WAITOK);
277 epoch_call_task_count = counter_u64_alloc(M_WAITOK);
279 pcpu_zone_record = uma_zcreate("epoch_record pcpu",
280 sizeof(struct epoch_record), NULL, NULL, NULL, NULL,
281 UMA_ALIGN_PTR, UMA_ZONE_PCPU);
283 GROUPTASK_INIT(DPCPU_ID_PTR(cpu, epoch_cb_task), 0,
284 epoch_call_task, NULL);
285 taskqgroup_attach_cpu(qgroup_softirq,
286 DPCPU_ID_PTR(cpu, epoch_cb_task), NULL, cpu, NULL, NULL,
290 SLIST_INIT(&thread0.td_epochs);
292 sx_init(&epoch_sx, "epoch-sx");
294 global_epoch = epoch_alloc("Global", 0);
295 global_epoch_preempt = epoch_alloc("Global preemptible", EPOCH_PREEMPT);
297 SYSINIT(epoch, SI_SUB_EPOCH, SI_ORDER_FIRST, epoch_init, NULL);
299 #if !defined(EARLY_AP_STARTUP)
301 epoch_init_smp(void *dummy __unused)
305 SYSINIT(epoch_smp, SI_SUB_SMP + 1, SI_ORDER_FIRST, epoch_init_smp, NULL);
309 epoch_ctor(epoch_t epoch)
314 epoch->e_pcpu_record = uma_zalloc_pcpu(pcpu_zone_record, M_WAITOK);
316 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
317 bzero(er, sizeof(*er));
318 ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
319 TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
321 er->er_parent = epoch;
326 epoch_adjust_prio(struct thread *td, u_char prio)
330 sched_prio(td, prio);
335 epoch_alloc(const char *name, int flags)
342 if (__predict_false(!inited))
343 panic("%s called too early in boot", __func__);
348 * Find a free index in the epoch array. If no free index is
349 * found, try to use the index after the last one.
353 * If too many epochs are currently allocated,
356 if (i == MAX_EPOCHS) {
360 if (epoch_array[i].e_in_use == 0)
364 epoch = epoch_array + i;
365 ck_epoch_init(&epoch->e_epoch);
367 epoch->e_flags = flags;
368 epoch->e_name = name;
369 sx_init(&epoch->e_drain_sx, "epoch-drain-sx");
370 mtx_init(&epoch->e_drain_mtx, "epoch-drain-mtx", NULL, MTX_DEF);
373 * Set e_in_use last, because when this field is set the
374 * epoch_call_task() function will start scanning this epoch
377 atomic_store_rel_int(&epoch->e_in_use, 1);
384 epoch_free(epoch_t epoch)
392 MPASS(epoch->e_in_use != 0);
394 epoch_drain_callbacks(epoch);
396 atomic_store_rel_int(&epoch->e_in_use, 0);
398 * Make sure the epoch_call_task() function see e_in_use equal
399 * to zero, by calling epoch_wait() on the global_epoch:
401 epoch_wait(global_epoch);
406 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
409 * Sanity check: none of the records should be in use anymore.
410 * We drained callbacks above and freeing the pcpu records is
413 MPASS(er->er_td == NULL);
414 MPASS(TAILQ_EMPTY(&er->er_tdlist));
417 uma_zfree_pcpu(pcpu_zone_record, epoch->e_pcpu_record);
418 mtx_destroy(&epoch->e_drain_mtx);
419 sx_destroy(&epoch->e_drain_sx);
420 memset(epoch, 0, sizeof(*epoch));
425 static epoch_record_t
426 epoch_currecord(epoch_t epoch)
429 return (zpcpu_get(epoch->e_pcpu_record));
432 #define INIT_CHECK(epoch) \
434 if (__predict_false((epoch) == NULL)) \
439 _epoch_enter_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
441 struct epoch_record *er;
444 MPASS(cold || epoch != NULL);
446 MPASS((vm_offset_t)et >= td->td_kstack &&
447 (vm_offset_t)et + sizeof(struct epoch_tracker) <=
448 td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
451 MPASS(epoch->e_flags & EPOCH_PREEMPT);
454 epoch_trace_enter(td, epoch, et, file, line);
457 THREAD_NO_SLEEPING();
460 et->et_old_priority = td->td_priority;
461 er = epoch_currecord(epoch);
462 /* Record-level tracking is reserved for non-preemptible epochs. */
463 MPASS(er->er_td == NULL);
464 TAILQ_INSERT_TAIL(&er->er_tdlist, et, et_link);
465 ck_epoch_begin(&er->er_record, &et->et_section);
470 epoch_enter(epoch_t epoch)
474 MPASS(cold || epoch != NULL);
477 er = epoch_currecord(epoch);
479 if (er->er_record.active == 0) {
480 MPASS(er->er_td == NULL);
481 er->er_td = curthread;
483 /* We've recursed, just make sure our accounting isn't wrong. */
484 MPASS(er->er_td == curthread);
487 ck_epoch_begin(&er->er_record, NULL);
491 _epoch_exit_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
493 struct epoch_record *er;
500 THREAD_SLEEPING_OK();
501 er = epoch_currecord(epoch);
502 MPASS(epoch->e_flags & EPOCH_PREEMPT);
504 MPASS(et->et_td == td);
506 et->et_td = (void*)0xDEADBEEF;
507 /* Record-level tracking is reserved for non-preemptible epochs. */
508 MPASS(er->er_td == NULL);
510 ck_epoch_end(&er->er_record, &et->et_section);
511 TAILQ_REMOVE(&er->er_tdlist, et, et_link);
513 if (__predict_false(et->et_old_priority != td->td_priority))
514 epoch_adjust_prio(td, et->et_old_priority);
517 epoch_trace_exit(td, epoch, et, file, line);
522 epoch_exit(epoch_t epoch)
527 er = epoch_currecord(epoch);
528 ck_epoch_end(&er->er_record, NULL);
530 MPASS(er->er_td == curthread);
531 if (er->er_record.active == 0)
538 * epoch_block_handler_preempt() is a callback from the CK code when another
539 * thread is currently in an epoch section.
542 epoch_block_handler_preempt(struct ck_epoch *global __unused,
543 ck_epoch_record_t *cr, void *arg __unused)
545 epoch_record_t record;
546 struct thread *td, *owner, *curwaittd;
547 struct epoch_tracker *tdwait;
548 struct turnstile *ts;
549 struct lock_object *lock;
551 int locksheld __unused;
553 record = __containerof(cr, struct epoch_record, er_record);
555 locksheld = td->td_locks;
557 counter_u64_add(block_count, 1);
559 * We lost a race and there's no longer any threads
560 * on the CPU in an epoch section.
562 if (TAILQ_EMPTY(&record->er_tdlist))
565 if (record->er_cpuid != curcpu) {
567 * If the head of the list is running, we can wait for it
568 * to remove itself from the list and thus save us the
569 * overhead of a migration
571 gen = record->er_gen;
574 * We can't actually check if the waiting thread is running
575 * so we simply poll for it to exit before giving up and
580 } while (!TAILQ_EMPTY(&record->er_tdlist) &&
581 gen == record->er_gen &&
582 spincount++ < MAX_ADAPTIVE_SPIN);
585 * If the generation has changed we can poll again
586 * otherwise we need to migrate.
588 if (gen != record->er_gen)
591 * Being on the same CPU as that of the record on which
592 * we need to wait allows us access to the thread
593 * list associated with that CPU. We can then examine the
594 * oldest thread in the queue and wait on its turnstile
595 * until it resumes and so on until a grace period
599 counter_u64_add(migrate_count, 1);
600 sched_bind(td, record->er_cpuid);
602 * At this point we need to return to the ck code
603 * to scan to see if a grace period has elapsed.
604 * We can't move on to check the thread list, because
605 * in the meantime new threads may have arrived that
606 * in fact belong to a different epoch.
611 * Try to find a thread in an epoch section on this CPU
612 * waiting on a turnstile. Otherwise find the lowest
613 * priority thread (highest prio value) and drop our priority
614 * to match to allow it to run.
616 TAILQ_FOREACH(tdwait, &record->er_tdlist, et_link) {
618 * Propagate our priority to any other waiters to prevent us
619 * from starving them. They will have their original priority
620 * restore on exit from epoch_wait().
622 curwaittd = tdwait->et_td;
623 if (!TD_IS_INHIBITED(curwaittd) && curwaittd->td_priority > td->td_priority) {
626 thread_lock(curwaittd);
627 sched_prio(curwaittd, td->td_priority);
628 thread_unlock(curwaittd);
632 if (TD_IS_INHIBITED(curwaittd) && TD_ON_LOCK(curwaittd) &&
633 ((ts = curwaittd->td_blocked) != NULL)) {
635 * We unlock td to allow turnstile_wait to reacquire
636 * the thread lock. Before unlocking it we enter a
637 * critical section to prevent preemption after we
638 * reenable interrupts by dropping the thread lock in
639 * order to prevent curwaittd from getting to run.
644 if (turnstile_lock(ts, &lock, &owner)) {
645 if (ts == curwaittd->td_blocked) {
646 MPASS(TD_IS_INHIBITED(curwaittd) &&
647 TD_ON_LOCK(curwaittd));
649 turnstile_wait(ts, owner,
650 curwaittd->td_tsqueue);
651 counter_u64_add(turnstile_count, 1);
655 turnstile_unlock(ts, lock);
659 KASSERT(td->td_locks == locksheld,
660 ("%d extra locks held", td->td_locks - locksheld));
664 * We didn't find any threads actually blocked on a lock
665 * so we have nothing to do except context switch away.
667 counter_u64_add(switch_count, 1);
668 mi_switch(SW_VOL | SWT_RELINQUISH);
670 * It is important the thread lock is dropped while yielding
671 * to allow other threads to acquire the lock pointed to by
672 * TDQ_LOCKPTR(td). Currently mi_switch() will unlock the
673 * thread lock before returning. Else a deadlock like
674 * situation might happen.
680 epoch_wait_preempt(epoch_t epoch)
689 MPASS(cold || epoch != NULL);
693 locks = curthread->td_locks;
694 MPASS(epoch->e_flags & EPOCH_PREEMPT);
695 if ((epoch->e_flags & EPOCH_LOCKED) == 0)
696 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
697 "epoch_wait() can be long running");
698 KASSERT(!in_epoch(epoch), ("epoch_wait_preempt() called in the middle "
699 "of an epoch section of the same epoch"));
704 old_cpu = PCPU_GET(cpuid);
705 old_pinned = td->td_pinned;
706 old_prio = td->td_priority;
707 was_bound = sched_is_bound(td);
710 sched_bind(td, old_cpu);
712 ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_preempt,
715 /* restore CPU binding, if any */
716 if (was_bound != 0) {
717 sched_bind(td, old_cpu);
719 /* get thread back to initial CPU, if any */
721 sched_bind(td, old_cpu);
724 /* restore pinned after bind */
725 td->td_pinned = old_pinned;
727 /* restore thread priority */
728 sched_prio(td, old_prio);
731 KASSERT(td->td_locks == locks,
732 ("%d residual locks held", td->td_locks - locks));
736 epoch_block_handler(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
743 epoch_wait(epoch_t epoch)
746 MPASS(cold || epoch != NULL);
748 MPASS(epoch->e_flags == 0);
750 ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
755 epoch_call(epoch_t epoch, epoch_callback_t callback, epoch_context_t ctx)
758 ck_epoch_entry_t *cb;
763 /* too early in boot to have epoch set up */
764 if (__predict_false(epoch == NULL))
766 #if !defined(EARLY_AP_STARTUP)
767 if (__predict_false(inited < 2))
772 *DPCPU_PTR(epoch_cb_count) += 1;
773 er = epoch_currecord(epoch);
774 ck_epoch_call(&er->er_record, cb, (ck_epoch_cb_t *)callback);
782 epoch_call_task(void *arg __unused)
784 ck_stack_entry_t *cursor, *head, *next;
785 ck_epoch_record_t *record;
789 int i, npending, total;
791 ck_stack_init(&cb_stack);
793 epoch_enter(global_epoch);
794 for (total = i = 0; i != MAX_EPOCHS; i++) {
795 epoch = epoch_array + i;
797 atomic_load_acq_int(&epoch->e_in_use) == 0))
799 er = epoch_currecord(epoch);
800 record = &er->er_record;
801 if ((npending = record->n_pending) == 0)
803 ck_epoch_poll_deferred(record, &cb_stack);
804 total += npending - record->n_pending;
806 epoch_exit(global_epoch);
807 *DPCPU_PTR(epoch_cb_count) -= total;
810 counter_u64_add(epoch_call_count, total);
811 counter_u64_add(epoch_call_task_count, 1);
813 head = ck_stack_batch_pop_npsc(&cb_stack);
814 for (cursor = head; cursor != NULL; cursor = next) {
815 struct ck_epoch_entry *entry =
816 ck_epoch_entry_container(cursor);
818 next = CK_STACK_NEXT(cursor);
819 entry->function(entry);
824 in_epoch_verbose_preempt(epoch_t epoch, int dump_onfail)
827 struct epoch_tracker *tdwait;
830 MPASS(epoch != NULL);
831 MPASS((epoch->e_flags & EPOCH_PREEMPT) != 0);
833 if (THREAD_CAN_SLEEP())
836 er = epoch_currecord(epoch);
837 TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
838 if (tdwait->et_td == td) {
844 MPASS(td->td_pinned);
845 printf("cpu: %d id: %d\n", curcpu, td->td_tid);
846 TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
847 printf("td_tid: %d ", tdwait->et_td->td_tid);
857 epoch_assert_nocpu(epoch_t epoch, struct thread *td)
863 crit = td->td_critnest > 0;
865 /* Check for a critical section mishap. */
867 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
868 KASSERT(er->er_td != td,
869 ("%s critical section in epoch '%s', from cpu %d",
870 (crit ? "exited" : "re-entered"), epoch->e_name, cpu));
874 #define epoch_assert_nocpu(e, td) do {} while (0)
878 in_epoch_verbose(epoch_t epoch, int dump_onfail)
883 if (__predict_false((epoch) == NULL))
885 if ((epoch->e_flags & EPOCH_PREEMPT) != 0)
886 return (in_epoch_verbose_preempt(epoch, dump_onfail));
889 * The thread being in a critical section is a necessary
890 * condition to be correctly inside a non-preemptible epoch,
891 * so it's definitely not in this epoch.
894 if (td->td_critnest == 0) {
895 epoch_assert_nocpu(epoch, td);
900 * The current cpu is in a critical section, so the epoch record will be
901 * stable for the rest of this function. Knowing that the record is not
902 * active is sufficient for knowing whether we're in this epoch or not,
903 * since it's a pcpu record.
905 er = epoch_currecord(epoch);
906 if (er->er_record.active == 0) {
907 epoch_assert_nocpu(epoch, td);
911 MPASS(er->er_td == td);
916 in_epoch(epoch_t epoch)
918 return (in_epoch_verbose(epoch, 0));
922 epoch_drain_cb(struct epoch_context *ctx)
924 struct epoch *epoch =
925 __containerof(ctx, struct epoch_record, er_drain_ctx)->er_parent;
927 if (atomic_fetchadd_int(&epoch->e_drain_count, -1) == 1) {
928 mtx_lock(&epoch->e_drain_mtx);
930 mtx_unlock(&epoch->e_drain_mtx);
935 epoch_drain_callbacks(epoch_t epoch)
944 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
945 "epoch_drain_callbacks() may sleep!");
947 /* too early in boot to have epoch set up */
948 if (__predict_false(epoch == NULL))
950 #if !defined(EARLY_AP_STARTUP)
951 if (__predict_false(inited < 2))
956 sx_xlock(&epoch->e_drain_sx);
957 mtx_lock(&epoch->e_drain_mtx);
961 old_cpu = PCPU_GET(cpuid);
962 old_pinned = td->td_pinned;
963 was_bound = sched_is_bound(td);
968 epoch->e_drain_count++;
970 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
972 epoch_call(epoch, &epoch_drain_cb, &er->er_drain_ctx);
975 /* restore CPU binding, if any */
976 if (was_bound != 0) {
977 sched_bind(td, old_cpu);
979 /* get thread back to initial CPU, if any */
981 sched_bind(td, old_cpu);
984 /* restore pinned after bind */
985 td->td_pinned = old_pinned;
989 while (epoch->e_drain_count != 0)
990 msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0);
992 mtx_unlock(&epoch->e_drain_mtx);
993 sx_xunlock(&epoch->e_drain_sx);