]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_epoch.c
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / sys / kern / subr_epoch.c
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/sx.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49 #include <sys/turnstile.h>
50 #include <vm/vm.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
53 #include <vm/uma.h>
54
55 #include <ck_epoch.h>
56
57 #ifdef __amd64__
58 #define EPOCH_ALIGN CACHE_LINE_SIZE*2
59 #else
60 #define EPOCH_ALIGN CACHE_LINE_SIZE
61 #endif
62
63 TAILQ_HEAD (epoch_tdlist, epoch_tracker);
64 typedef struct epoch_record {
65         ck_epoch_record_t er_record;
66         volatile struct epoch_tdlist er_tdlist;
67         volatile uint32_t er_gen;
68         uint32_t er_cpuid;
69         /* fields above are part of KBI and cannot be modified */
70         struct epoch_context er_drain_ctx;
71         struct epoch *er_parent;
72 } __aligned(EPOCH_ALIGN)     *epoch_record_t;
73
74 struct epoch {
75         struct ck_epoch e_epoch __aligned(EPOCH_ALIGN);
76         epoch_record_t e_pcpu_record;
77         int     e_in_use;
78         int     e_flags;
79         /* fields above are part of KBI and cannot be modified */
80         struct sx e_drain_sx;
81         struct mtx e_drain_mtx;
82         volatile int e_drain_count;
83 };
84
85 /* arbitrary --- needs benchmarking */
86 #define MAX_ADAPTIVE_SPIN 100
87 #define MAX_EPOCHS 64
88
89 CTASSERT(sizeof(ck_epoch_entry_t) == sizeof(struct epoch_context));
90 SYSCTL_NODE(_kern, OID_AUTO, epoch, CTLFLAG_RW, 0, "epoch information");
91 SYSCTL_NODE(_kern_epoch, OID_AUTO, stats, CTLFLAG_RW, 0, "epoch stats");
92
93 /* Stats. */
94 static counter_u64_t block_count;
95
96 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, nblocked, CTLFLAG_RW,
97     &block_count, "# of times a thread was in an epoch when epoch_wait was called");
98 static counter_u64_t migrate_count;
99
100 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, migrations, CTLFLAG_RW,
101     &migrate_count, "# of times thread was migrated to another CPU in epoch_wait");
102 static counter_u64_t turnstile_count;
103
104 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, ncontended, CTLFLAG_RW,
105     &turnstile_count, "# of times a thread was blocked on a lock in an epoch during an epoch_wait");
106 static counter_u64_t switch_count;
107
108 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, switches, CTLFLAG_RW,
109     &switch_count, "# of times a thread voluntarily context switched in epoch_wait");
110 static counter_u64_t epoch_call_count;
111
112 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_calls, CTLFLAG_RW,
113     &epoch_call_count, "# of times a callback was deferred");
114 static counter_u64_t epoch_call_task_count;
115
116 SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_call_tasks, CTLFLAG_RW,
117     &epoch_call_task_count, "# of times a callback task was run");
118
119 TAILQ_HEAD (threadlist, thread);
120
121 CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
122     ck_epoch_entry_container)
123
124 static struct epoch epoch_array[MAX_EPOCHS];
125
126 DPCPU_DEFINE(struct grouptask, epoch_cb_task);
127 DPCPU_DEFINE(int, epoch_cb_count);
128
129 static __read_mostly int inited;
130 __read_mostly epoch_t global_epoch;
131 __read_mostly epoch_t global_epoch_preempt;
132
133 static void epoch_call_task(void *context __unused);
134 static  uma_zone_t pcpu_zone_record;
135
136 static struct sx epoch_sx;
137
138 #define EPOCH_LOCK() sx_xlock(&epoch_sx)
139 #define EPOCH_UNLOCK() sx_xunlock(&epoch_sx)
140
141 static void
142 epoch_init(void *arg __unused)
143 {
144         int cpu;
145
146         block_count = counter_u64_alloc(M_WAITOK);
147         migrate_count = counter_u64_alloc(M_WAITOK);
148         turnstile_count = counter_u64_alloc(M_WAITOK);
149         switch_count = counter_u64_alloc(M_WAITOK);
150         epoch_call_count = counter_u64_alloc(M_WAITOK);
151         epoch_call_task_count = counter_u64_alloc(M_WAITOK);
152
153         pcpu_zone_record = uma_zcreate("epoch_record pcpu",
154             sizeof(struct epoch_record), NULL, NULL, NULL, NULL,
155             UMA_ALIGN_PTR, UMA_ZONE_PCPU);
156         CPU_FOREACH(cpu) {
157                 GROUPTASK_INIT(DPCPU_ID_PTR(cpu, epoch_cb_task), 0,
158                     epoch_call_task, NULL);
159                 taskqgroup_attach_cpu(qgroup_softirq,
160                     DPCPU_ID_PTR(cpu, epoch_cb_task), NULL, cpu, -1,
161                     "epoch call task");
162         }
163         sx_init(&epoch_sx, "epoch-sx");
164         inited = 1;
165         global_epoch = epoch_alloc(0);
166         global_epoch_preempt = epoch_alloc(EPOCH_PREEMPT);
167 }
168 SYSINIT(epoch, SI_SUB_TASKQ + 1, SI_ORDER_FIRST, epoch_init, NULL);
169
170 #if !defined(EARLY_AP_STARTUP)
171 static void
172 epoch_init_smp(void *dummy __unused)
173 {
174         inited = 2;
175 }
176 SYSINIT(epoch_smp, SI_SUB_SMP + 1, SI_ORDER_FIRST, epoch_init_smp, NULL);
177 #endif
178
179 static void
180 epoch_ctor(epoch_t epoch)
181 {
182         epoch_record_t er;
183         int cpu;
184
185         epoch->e_pcpu_record = uma_zalloc_pcpu(pcpu_zone_record, M_WAITOK);
186         CPU_FOREACH(cpu) {
187                 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
188                 bzero(er, sizeof(*er));
189                 ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
190                 TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
191                 er->er_cpuid = cpu;
192                 er->er_parent = epoch;
193         }
194 }
195
196 static void
197 epoch_adjust_prio(struct thread *td, u_char prio)
198 {
199
200         thread_lock(td);
201         sched_prio(td, prio);
202         thread_unlock(td);
203 }
204
205 epoch_t
206 epoch_alloc(int flags)
207 {
208         epoch_t epoch;
209         int i;
210
211         if (__predict_false(!inited))
212                 panic("%s called too early in boot", __func__);
213
214         EPOCH_LOCK();
215
216         /*
217          * Find a free index in the epoch array. If no free index is
218          * found, try to use the index after the last one.
219          */
220         for (i = 0;; i++) {
221                 /*
222                  * If too many epochs are currently allocated,
223                  * return NULL.
224                  */
225                 if (i == MAX_EPOCHS) {
226                         epoch = NULL;
227                         goto done;
228                 }
229                 if (epoch_array[i].e_in_use == 0)
230                         break;
231         }
232
233         epoch = epoch_array + i;
234         ck_epoch_init(&epoch->e_epoch);
235         epoch_ctor(epoch);
236         epoch->e_flags = flags;
237         sx_init(&epoch->e_drain_sx, "epoch-drain-sx");
238         mtx_init(&epoch->e_drain_mtx, "epoch-drain-mtx", NULL, MTX_DEF);
239
240         /*
241          * Set e_in_use last, because when this field is set the
242          * epoch_call_task() function will start scanning this epoch
243          * structure.
244          */
245         atomic_store_rel_int(&epoch->e_in_use, 1);
246 done:
247         EPOCH_UNLOCK();
248         return (epoch);
249 }
250
251 void
252 epoch_free(epoch_t epoch)
253 {
254
255         EPOCH_LOCK();
256
257         MPASS(epoch->e_in_use != 0);
258
259         epoch_drain_callbacks(epoch);
260
261         atomic_store_rel_int(&epoch->e_in_use, 0);
262         /*
263          * Make sure the epoch_call_task() function see e_in_use equal
264          * to zero, by calling epoch_wait() on the global_epoch:
265          */
266         epoch_wait(global_epoch);
267         uma_zfree_pcpu(pcpu_zone_record, epoch->e_pcpu_record);
268         mtx_destroy(&epoch->e_drain_mtx);
269         sx_destroy(&epoch->e_drain_sx);
270         memset(epoch, 0, sizeof(*epoch));
271
272         EPOCH_UNLOCK();
273 }
274
275 static epoch_record_t
276 epoch_currecord(epoch_t epoch)
277 {
278
279         return (zpcpu_get_cpu(epoch->e_pcpu_record, curcpu));
280 }
281
282 #define INIT_CHECK(epoch)                                       \
283         do {                                                    \
284                 if (__predict_false((epoch) == NULL))           \
285                         return;                                 \
286         } while (0)
287
288 void
289 epoch_enter_preempt(epoch_t epoch, epoch_tracker_t et)
290 {
291         struct epoch_record *er;
292         struct thread *td;
293
294         MPASS(cold || epoch != NULL);
295         INIT_CHECK(epoch);
296         MPASS(epoch->e_flags & EPOCH_PREEMPT);
297 #ifdef EPOCH_TRACKER_DEBUG
298         et->et_magic_pre = EPOCH_MAGIC0;
299         et->et_magic_post = EPOCH_MAGIC1;
300 #endif
301         td = curthread;
302         et->et_td = td;
303         td->td_epochnest++;
304         critical_enter();
305         sched_pin();
306
307         td->td_pre_epoch_prio = td->td_priority;
308         er = epoch_currecord(epoch);
309         TAILQ_INSERT_TAIL(&er->er_tdlist, et, et_link);
310         ck_epoch_begin(&er->er_record, &et->et_section);
311         critical_exit();
312 }
313
314 void
315 epoch_enter(epoch_t epoch)
316 {
317         struct thread *td;
318         epoch_record_t er;
319
320         MPASS(cold || epoch != NULL);
321         INIT_CHECK(epoch);
322         td = curthread;
323
324         td->td_epochnest++;
325         critical_enter();
326         er = epoch_currecord(epoch);
327         ck_epoch_begin(&er->er_record, NULL);
328 }
329
330 void
331 epoch_exit_preempt(epoch_t epoch, epoch_tracker_t et)
332 {
333         struct epoch_record *er;
334         struct thread *td;
335
336         INIT_CHECK(epoch);
337         td = curthread;
338         critical_enter();
339         sched_unpin();
340         MPASS(td->td_epochnest);
341         td->td_epochnest--;
342         er = epoch_currecord(epoch);
343         MPASS(epoch->e_flags & EPOCH_PREEMPT);
344         MPASS(et != NULL);
345         MPASS(et->et_td == td);
346 #ifdef EPOCH_TRACKER_DEBUG
347         MPASS(et->et_magic_pre == EPOCH_MAGIC0);
348         MPASS(et->et_magic_post == EPOCH_MAGIC1);
349         et->et_magic_pre = 0;
350         et->et_magic_post = 0;
351 #endif
352 #ifdef INVARIANTS
353         et->et_td = (void*)0xDEADBEEF;
354 #endif
355         ck_epoch_end(&er->er_record, &et->et_section);
356         TAILQ_REMOVE(&er->er_tdlist, et, et_link);
357         er->er_gen++;
358         if (__predict_false(td->td_pre_epoch_prio != td->td_priority))
359                 epoch_adjust_prio(td, td->td_pre_epoch_prio);
360         critical_exit();
361 }
362
363 void
364 epoch_exit(epoch_t epoch)
365 {
366         struct thread *td;
367         epoch_record_t er;
368
369         INIT_CHECK(epoch);
370         td = curthread;
371         MPASS(td->td_epochnest);
372         td->td_epochnest--;
373         er = epoch_currecord(epoch);
374         ck_epoch_end(&er->er_record, NULL);
375         critical_exit();
376 }
377
378 /*
379  * epoch_block_handler_preempt() is a callback from the CK code when another
380  * thread is currently in an epoch section.
381  */
382 static void
383 epoch_block_handler_preempt(struct ck_epoch *global __unused,
384     ck_epoch_record_t *cr, void *arg __unused)
385 {
386         epoch_record_t record;
387         struct thread *td, *owner, *curwaittd;
388         struct epoch_tracker *tdwait;
389         struct turnstile *ts;
390         struct lock_object *lock;
391         int spincount, gen;
392         int locksheld __unused;
393
394         record = __containerof(cr, struct epoch_record, er_record);
395         td = curthread;
396         locksheld = td->td_locks;
397         spincount = 0;
398         counter_u64_add(block_count, 1);
399         /*
400          * We lost a race and there's no longer any threads
401          * on the CPU in an epoch section.
402          */
403         if (TAILQ_EMPTY(&record->er_tdlist))
404                 return;
405
406         if (record->er_cpuid != curcpu) {
407                 /*
408                  * If the head of the list is running, we can wait for it
409                  * to remove itself from the list and thus save us the
410                  * overhead of a migration
411                  */
412                 gen = record->er_gen;
413                 thread_unlock(td);
414                 /*
415                  * We can't actually check if the waiting thread is running
416                  * so we simply poll for it to exit before giving up and
417                  * migrating.
418                  */
419                 do {
420                         cpu_spinwait();
421                 } while (!TAILQ_EMPTY(&record->er_tdlist) &&
422                                  gen == record->er_gen &&
423                                  spincount++ < MAX_ADAPTIVE_SPIN);
424                 thread_lock(td);
425                 /*
426                  * If the generation has changed we can poll again
427                  * otherwise we need to migrate.
428                  */
429                 if (gen != record->er_gen)
430                         return;
431                 /*
432                  * Being on the same CPU as that of the record on which
433                  * we need to wait allows us access to the thread
434                  * list associated with that CPU. We can then examine the
435                  * oldest thread in the queue and wait on its turnstile
436                  * until it resumes and so on until a grace period
437                  * elapses.
438                  *
439                  */
440                 counter_u64_add(migrate_count, 1);
441                 sched_bind(td, record->er_cpuid);
442                 /*
443                  * At this point we need to return to the ck code
444                  * to scan to see if a grace period has elapsed.
445                  * We can't move on to check the thread list, because
446                  * in the meantime new threads may have arrived that
447                  * in fact belong to a different epoch.
448                  */
449                 return;
450         }
451         /*
452          * Try to find a thread in an epoch section on this CPU
453          * waiting on a turnstile. Otherwise find the lowest
454          * priority thread (highest prio value) and drop our priority
455          * to match to allow it to run.
456          */
457         TAILQ_FOREACH(tdwait, &record->er_tdlist, et_link) {
458                 /*
459                  * Propagate our priority to any other waiters to prevent us
460                  * from starving them. They will have their original priority
461                  * restore on exit from epoch_wait().
462                  */
463                 curwaittd = tdwait->et_td;
464                 if (!TD_IS_INHIBITED(curwaittd) && curwaittd->td_priority > td->td_priority) {
465                         critical_enter();
466                         thread_unlock(td);
467                         thread_lock(curwaittd);
468                         sched_prio(curwaittd, td->td_priority);
469                         thread_unlock(curwaittd);
470                         thread_lock(td);
471                         critical_exit();
472                 }
473                 if (TD_IS_INHIBITED(curwaittd) && TD_ON_LOCK(curwaittd) &&
474                     ((ts = curwaittd->td_blocked) != NULL)) {
475                         /*
476                          * We unlock td to allow turnstile_wait to reacquire
477                          * the thread lock. Before unlocking it we enter a
478                          * critical section to prevent preemption after we
479                          * reenable interrupts by dropping the thread lock in
480                          * order to prevent curwaittd from getting to run.
481                          */
482                         critical_enter();
483                         thread_unlock(td);
484
485                         if (turnstile_lock(ts, &lock, &owner)) {
486                                 if (ts == curwaittd->td_blocked) {
487                                         MPASS(TD_IS_INHIBITED(curwaittd) &&
488                                             TD_ON_LOCK(curwaittd));
489                                         critical_exit();
490                                         turnstile_wait(ts, owner,
491                                             curwaittd->td_tsqueue);
492                                         counter_u64_add(turnstile_count, 1);
493                                         thread_lock(td);
494                                         return;
495                                 }
496                                 turnstile_unlock(ts, lock);
497                         }
498                         thread_lock(td);
499                         critical_exit();
500                         KASSERT(td->td_locks == locksheld,
501                             ("%d extra locks held", td->td_locks - locksheld));
502                 }
503         }
504         /*
505          * We didn't find any threads actually blocked on a lock
506          * so we have nothing to do except context switch away.
507          */
508         counter_u64_add(switch_count, 1);
509         mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
510
511         /*
512          * Release the thread lock while yielding to
513          * allow other threads to acquire the lock
514          * pointed to by TDQ_LOCKPTR(td). Else a
515          * deadlock like situation might happen. (HPS)
516          */
517         thread_unlock(td);
518         thread_lock(td);
519 }
520
521 void
522 epoch_wait_preempt(epoch_t epoch)
523 {
524         struct thread *td;
525         int was_bound;
526         int old_cpu;
527         int old_pinned;
528         u_char old_prio;
529         int locks __unused;
530
531         MPASS(cold || epoch != NULL);
532         INIT_CHECK(epoch);
533         td = curthread;
534 #ifdef INVARIANTS
535         locks = curthread->td_locks;
536         MPASS(epoch->e_flags & EPOCH_PREEMPT);
537         if ((epoch->e_flags & EPOCH_LOCKED) == 0)
538                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
539                     "epoch_wait() can be long running");
540         KASSERT(!in_epoch(epoch), ("epoch_wait_preempt() called in the middle "
541             "of an epoch section of the same epoch"));
542 #endif
543         DROP_GIANT();
544         thread_lock(td);
545
546         old_cpu = PCPU_GET(cpuid);
547         old_pinned = td->td_pinned;
548         old_prio = td->td_priority;
549         was_bound = sched_is_bound(td);
550         sched_unbind(td);
551         td->td_pinned = 0;
552         sched_bind(td, old_cpu);
553
554         ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_preempt,
555             NULL);
556
557         /* restore CPU binding, if any */
558         if (was_bound != 0) {
559                 sched_bind(td, old_cpu);
560         } else {
561                 /* get thread back to initial CPU, if any */
562                 if (old_pinned != 0)
563                         sched_bind(td, old_cpu);
564                 sched_unbind(td);
565         }
566         /* restore pinned after bind */
567         td->td_pinned = old_pinned;
568
569         /* restore thread priority */
570         sched_prio(td, old_prio);
571         thread_unlock(td);
572         PICKUP_GIANT();
573         KASSERT(td->td_locks == locks,
574             ("%d residual locks held", td->td_locks - locks));
575 }
576
577 static void
578 epoch_block_handler(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
579     void *arg __unused)
580 {
581         cpu_spinwait();
582 }
583
584 void
585 epoch_wait(epoch_t epoch)
586 {
587
588         MPASS(cold || epoch != NULL);
589         INIT_CHECK(epoch);
590         MPASS(epoch->e_flags == 0);
591         critical_enter();
592         ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
593         critical_exit();
594 }
595
596 void
597 epoch_call(epoch_t epoch, epoch_context_t ctx, void (*callback) (epoch_context_t))
598 {
599         epoch_record_t er;
600         ck_epoch_entry_t *cb;
601
602         cb = (void *)ctx;
603
604         MPASS(callback);
605         /* too early in boot to have epoch set up */
606         if (__predict_false(epoch == NULL))
607                 goto boottime;
608 #if !defined(EARLY_AP_STARTUP)
609         if (__predict_false(inited < 2))
610                 goto boottime;
611 #endif
612
613         critical_enter();
614         *DPCPU_PTR(epoch_cb_count) += 1;
615         er = epoch_currecord(epoch);
616         ck_epoch_call(&er->er_record, cb, (ck_epoch_cb_t *)callback);
617         critical_exit();
618         return;
619 boottime:
620         callback(ctx);
621 }
622
623 static void
624 epoch_call_task(void *arg __unused)
625 {
626         ck_stack_entry_t *cursor, *head, *next;
627         ck_epoch_record_t *record;
628         epoch_record_t er;
629         epoch_t epoch;
630         ck_stack_t cb_stack;
631         int i, npending, total;
632
633         ck_stack_init(&cb_stack);
634         critical_enter();
635         epoch_enter(global_epoch);
636         for (total = i = 0; i != MAX_EPOCHS; i++) {
637                 epoch = epoch_array + i;
638                 if (__predict_false(
639                     atomic_load_acq_int(&epoch->e_in_use) == 0))
640                         continue;
641                 er = epoch_currecord(epoch);
642                 record = &er->er_record;
643                 if ((npending = record->n_pending) == 0)
644                         continue;
645                 ck_epoch_poll_deferred(record, &cb_stack);
646                 total += npending - record->n_pending;
647         }
648         epoch_exit(global_epoch);
649         *DPCPU_PTR(epoch_cb_count) -= total;
650         critical_exit();
651
652         counter_u64_add(epoch_call_count, total);
653         counter_u64_add(epoch_call_task_count, 1);
654
655         head = ck_stack_batch_pop_npsc(&cb_stack);
656         for (cursor = head; cursor != NULL; cursor = next) {
657                 struct ck_epoch_entry *entry =
658                     ck_epoch_entry_container(cursor);
659
660                 next = CK_STACK_NEXT(cursor);
661                 entry->function(entry);
662         }
663 }
664
665 int
666 in_epoch_verbose(epoch_t epoch, int dump_onfail)
667 {
668         struct epoch_tracker *tdwait;
669         struct thread *td;
670         epoch_record_t er;
671
672         td = curthread;
673         if (td->td_epochnest == 0)
674                 return (0);
675         if (__predict_false((epoch) == NULL))
676                 return (0);
677         critical_enter();
678         er = epoch_currecord(epoch);
679         TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
680                 if (tdwait->et_td == td) {
681                         critical_exit();
682                         return (1);
683                 }
684 #ifdef INVARIANTS
685         if (dump_onfail) {
686                 MPASS(td->td_pinned);
687                 printf("cpu: %d id: %d\n", curcpu, td->td_tid);
688                 TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
689                         printf("td_tid: %d ", tdwait->et_td->td_tid);
690                 printf("\n");
691         }
692 #endif
693         critical_exit();
694         return (0);
695 }
696
697 int
698 in_epoch(epoch_t epoch)
699 {
700         return (in_epoch_verbose(epoch, 0));
701 }
702
703 static void
704 epoch_drain_cb(struct epoch_context *ctx)
705 {
706         struct epoch *epoch =
707             __containerof(ctx, struct epoch_record, er_drain_ctx)->er_parent;
708
709         if (atomic_fetchadd_int(&epoch->e_drain_count, -1) == 1) {
710                 mtx_lock(&epoch->e_drain_mtx);
711                 wakeup(epoch);
712                 mtx_unlock(&epoch->e_drain_mtx);
713         }
714 }
715
716 void
717 epoch_drain_callbacks(epoch_t epoch)
718 {
719         epoch_record_t er;
720         struct thread *td;
721         int was_bound;
722         int old_pinned;
723         int old_cpu;
724         int cpu;
725
726         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
727             "epoch_drain_callbacks() may sleep!");
728
729         /* too early in boot to have epoch set up */
730         if (__predict_false(epoch == NULL))
731                 return;
732 #if !defined(EARLY_AP_STARTUP)
733         if (__predict_false(inited < 2))
734                 return;
735 #endif
736         DROP_GIANT();
737
738         sx_xlock(&epoch->e_drain_sx);
739         mtx_lock(&epoch->e_drain_mtx);
740
741         td = curthread;
742         thread_lock(td);
743         old_cpu = PCPU_GET(cpuid);
744         old_pinned = td->td_pinned;
745         was_bound = sched_is_bound(td);
746         sched_unbind(td);
747         td->td_pinned = 0;
748
749         CPU_FOREACH(cpu)
750                 epoch->e_drain_count++;
751         CPU_FOREACH(cpu) {
752                 er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
753                 sched_bind(td, cpu);
754                 epoch_call(epoch, &er->er_drain_ctx, &epoch_drain_cb);
755         }
756
757         /* restore CPU binding, if any */
758         if (was_bound != 0) {
759                 sched_bind(td, old_cpu);
760         } else {
761                 /* get thread back to initial CPU, if any */
762                 if (old_pinned != 0)
763                         sched_bind(td, old_cpu);
764                 sched_unbind(td);
765         }
766         /* restore pinned after bind */
767         td->td_pinned = old_pinned;
768
769         thread_unlock(td);
770
771         while (epoch->e_drain_count != 0)
772                 msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0);
773
774         mtx_unlock(&epoch->e_drain_mtx);
775         sx_xunlock(&epoch->e_drain_sx);
776
777         PICKUP_GIANT();
778 }
779
780 /* for binary compatibility */
781
782 struct epoch_tracker_KBI {
783         void *datap[3];
784 #ifdef EPOCH_TRACKER_DEBUG
785         int datai[5];
786 #else
787         int datai[1];
788 #endif
789 } __aligned(sizeof(void *));
790
791 CTASSERT(sizeof(struct epoch_tracker_KBI) >= sizeof(struct epoch_tracker));
792
793 void
794 epoch_enter_preempt_KBI(epoch_t epoch, epoch_tracker_t et)
795 {
796         epoch_enter_preempt(epoch, et);
797 }
798
799 void
800 epoch_exit_preempt_KBI(epoch_t epoch, epoch_tracker_t et)
801 {
802         epoch_exit_preempt(epoch, et);
803 }
804
805 void
806 epoch_enter_KBI(epoch_t epoch)
807 {
808         epoch_enter(epoch);
809 }
810
811 void
812 epoch_exit_KBI(epoch_t epoch)
813 {
814         epoch_exit(epoch);
815 }