2018-05-10 17:55:24 +00:00
|
|
|
/*-
|
2018-05-13 23:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2018-05-10 17:55:24 +00:00
|
|
|
* Copyright (c) 2018, Matthew Macy <mmacy@freebsd.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2018-05-13 23:24:48 +00:00
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2018-05-10 17:55:24 +00:00
|
|
|
*
|
2018-05-13 23:24:48 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
2018-05-10 17:55:24 +00:00
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2018-05-13 23:24:48 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2018-05-10 17:55:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/counter.h>
|
|
|
|
#include <sys/epoch.h>
|
|
|
|
#include <sys/gtaskqueue.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
2018-05-17 19:57:07 +00:00
|
|
|
#include <sys/pcpu.h>
|
2018-05-10 17:55:24 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/sched.h>
|
2019-06-28 10:38:56 +00:00
|
|
|
#include <sys/sx.h>
|
2018-05-10 17:55:24 +00:00
|
|
|
#include <sys/smp.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/turnstile.h>
|
2019-09-25 18:26:31 +00:00
|
|
|
#ifdef EPOCH_TRACE
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
#include <sys/stack.h>
|
|
|
|
#include <sys/tree.h>
|
|
|
|
#endif
|
2018-05-10 17:55:24 +00:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_extern.h>
|
|
|
|
#include <vm/vm_kern.h>
|
2018-07-06 06:20:03 +00:00
|
|
|
#include <vm/uma.h>
|
2018-05-10 17:55:24 +00:00
|
|
|
|
|
|
|
#include <ck_epoch.h>
|
|
|
|
|
2018-05-13 23:24:48 +00:00
|
|
|
static MALLOC_DEFINE(M_EPOCH, "epoch", "epoch based reclamation");
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
#ifdef __amd64__
|
|
|
|
#define EPOCH_ALIGN CACHE_LINE_SIZE*2
|
|
|
|
#else
|
|
|
|
#define EPOCH_ALIGN CACHE_LINE_SIZE
|
|
|
|
#endif
|
|
|
|
|
2018-11-13 23:20:55 +00:00
|
|
|
TAILQ_HEAD (epoch_tdlist, epoch_tracker);
|
2018-11-13 19:02:11 +00:00
|
|
|
typedef struct epoch_record {
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_record_t er_record;
|
2019-06-28 10:38:56 +00:00
|
|
|
struct epoch_context er_drain_ctx;
|
|
|
|
struct epoch *er_parent;
|
2018-11-13 19:02:11 +00:00
|
|
|
volatile struct epoch_tdlist er_tdlist;
|
|
|
|
volatile uint32_t er_gen;
|
|
|
|
uint32_t er_cpuid;
|
|
|
|
} __aligned(EPOCH_ALIGN) *epoch_record_t;
|
|
|
|
|
|
|
|
struct epoch {
|
|
|
|
struct ck_epoch e_epoch __aligned(EPOCH_ALIGN);
|
|
|
|
epoch_record_t e_pcpu_record;
|
|
|
|
int e_idx;
|
|
|
|
int e_flags;
|
2019-06-28 10:38:56 +00:00
|
|
|
struct sx e_drain_sx;
|
|
|
|
struct mtx e_drain_mtx;
|
|
|
|
volatile int e_drain_count;
|
2019-09-25 18:26:31 +00:00
|
|
|
const char *e_name;
|
2018-11-13 19:02:11 +00:00
|
|
|
};
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
/* arbitrary --- needs benchmarking */
|
2018-08-09 05:18:27 +00:00
|
|
|
#define MAX_ADAPTIVE_SPIN 100
|
2018-05-17 19:57:07 +00:00
|
|
|
#define MAX_EPOCHS 64
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-05-17 19:50:55 +00:00
|
|
|
CTASSERT(sizeof(ck_epoch_entry_t) == sizeof(struct epoch_context));
|
2018-05-10 17:55:24 +00:00
|
|
|
SYSCTL_NODE(_kern, OID_AUTO, epoch, CTLFLAG_RW, 0, "epoch information");
|
|
|
|
SYSCTL_NODE(_kern_epoch, OID_AUTO, stats, CTLFLAG_RW, 0, "epoch stats");
|
|
|
|
|
|
|
|
/* Stats. */
|
|
|
|
static counter_u64_t block_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, nblocked, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&block_count, "# of times a thread was in an epoch when epoch_wait was called");
|
2018-05-10 17:55:24 +00:00
|
|
|
static counter_u64_t migrate_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, migrations, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&migrate_count, "# of times thread was migrated to another CPU in epoch_wait");
|
2018-05-10 17:55:24 +00:00
|
|
|
static counter_u64_t turnstile_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, ncontended, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&turnstile_count, "# of times a thread was blocked on a lock in an epoch during an epoch_wait");
|
2018-05-10 17:55:24 +00:00
|
|
|
static counter_u64_t switch_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, switches, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&switch_count, "# of times a thread voluntarily context switched in epoch_wait");
|
2018-05-18 01:52:51 +00:00
|
|
|
static counter_u64_t epoch_call_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_calls, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&epoch_call_count, "# of times a callback was deferred");
|
2018-05-18 01:52:51 +00:00
|
|
|
static counter_u64_t epoch_call_task_count;
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
SYSCTL_COUNTER_U64(_kern_epoch_stats, OID_AUTO, epoch_call_tasks, CTLFLAG_RW,
|
2018-05-30 03:39:57 +00:00
|
|
|
&epoch_call_task_count, "# of times a callback task was run");
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-05-30 03:39:57 +00:00
|
|
|
TAILQ_HEAD (threadlist, thread);
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-05-17 19:50:55 +00:00
|
|
|
CK_STACK_CONTAINER(struct ck_epoch_entry, stack_entry,
|
|
|
|
ck_epoch_entry_container)
|
2018-07-04 02:47:16 +00:00
|
|
|
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch_t allepochs[MAX_EPOCHS];
|
2018-05-17 19:57:07 +00:00
|
|
|
|
2018-05-17 21:39:15 +00:00
|
|
|
DPCPU_DEFINE(struct grouptask, epoch_cb_task);
|
|
|
|
DPCPU_DEFINE(int, epoch_cb_count);
|
2018-05-17 19:57:07 +00:00
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
static __read_mostly int inited;
|
2018-05-17 19:57:07 +00:00
|
|
|
static __read_mostly int epoch_count;
|
2018-05-13 23:24:48 +00:00
|
|
|
__read_mostly epoch_t global_epoch;
|
2018-05-18 17:29:43 +00:00
|
|
|
__read_mostly epoch_t global_epoch_preempt;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-05-17 19:57:07 +00:00
|
|
|
static void epoch_call_task(void *context __unused);
|
2018-07-06 06:20:03 +00:00
|
|
|
static uma_zone_t pcpu_zone_record;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2019-09-25 18:26:31 +00:00
|
|
|
#ifdef EPOCH_TRACE
|
|
|
|
struct stackentry {
|
|
|
|
RB_ENTRY(stackentry) se_node;
|
|
|
|
struct stack se_stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
stackentry_compare(struct stackentry *a, struct stackentry *b)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (a->se_stack.depth > b->se_stack.depth)
|
|
|
|
return (1);
|
|
|
|
if (a->se_stack.depth < b->se_stack.depth)
|
|
|
|
return (-1);
|
|
|
|
for (int i = 0; i < a->se_stack.depth; i++) {
|
|
|
|
if (a->se_stack.pcs[i] > b->se_stack.pcs[i])
|
|
|
|
return (1);
|
|
|
|
if (a->se_stack.pcs[i] < b->se_stack.pcs[i])
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_HEAD(stacktree, stackentry) epoch_stacks = RB_INITIALIZER(&epoch_stacks);
|
|
|
|
RB_GENERATE_STATIC(stacktree, stackentry, se_node, stackentry_compare);
|
|
|
|
|
|
|
|
static struct mtx epoch_stacks_lock;
|
|
|
|
MTX_SYSINIT(epochstacks, &epoch_stacks_lock, "epoch_stacks", MTX_DEF);
|
|
|
|
|
2019-12-06 16:34:04 +00:00
|
|
|
static bool epoch_trace_stack_print = true;
|
|
|
|
SYSCTL_BOOL(_kern_epoch, OID_AUTO, trace_stack_print, CTLFLAG_RWTUN,
|
|
|
|
&epoch_trace_stack_print, 0, "Print stack traces on epoch reports");
|
|
|
|
|
2019-09-25 18:26:31 +00:00
|
|
|
static void epoch_trace_report(const char *fmt, ...) __printflike(1, 2);
|
|
|
|
static inline void
|
|
|
|
epoch_trace_report(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
struct stackentry se, *new;
|
|
|
|
|
|
|
|
stack_zero(&se.se_stack); /* XXX: is it really needed? */
|
|
|
|
stack_save(&se.se_stack);
|
|
|
|
|
|
|
|
/* Tree is never reduced - go lockless. */
|
|
|
|
if (RB_FIND(stacktree, &epoch_stacks, &se) != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
new = malloc(sizeof(*new), M_STACK, M_NOWAIT);
|
|
|
|
if (new != NULL) {
|
|
|
|
bcopy(&se.se_stack, &new->se_stack, sizeof(struct stack));
|
|
|
|
|
|
|
|
mtx_lock(&epoch_stacks_lock);
|
|
|
|
new = RB_INSERT(stacktree, &epoch_stacks, new);
|
|
|
|
mtx_unlock(&epoch_stacks_lock);
|
|
|
|
if (new != NULL)
|
|
|
|
free(new, M_STACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void)vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
2019-12-06 16:34:04 +00:00
|
|
|
if (epoch_trace_stack_print)
|
|
|
|
stack_print_ddb(&se.se_stack);
|
2019-09-25 18:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
epoch_trace_enter(struct thread *td, epoch_t epoch, epoch_tracker_t et,
|
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
epoch_tracker_t iet;
|
|
|
|
|
|
|
|
SLIST_FOREACH(iet, &td->td_epochs, et_tlink)
|
|
|
|
if (iet->et_epoch == epoch)
|
|
|
|
epoch_trace_report("Recursively entering epoch %s "
|
2019-12-06 16:34:04 +00:00
|
|
|
"at %s:%d, previously entered at %s:%d\n",
|
|
|
|
epoch->e_name, file, line,
|
|
|
|
iet->et_file, iet->et_line);
|
2019-09-25 18:26:31 +00:00
|
|
|
et->et_epoch = epoch;
|
|
|
|
et->et_file = file;
|
|
|
|
et->et_line = line;
|
|
|
|
SLIST_INSERT_HEAD(&td->td_epochs, et, et_tlink);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
epoch_trace_exit(struct thread *td, epoch_t epoch, epoch_tracker_t et,
|
|
|
|
const char *file, int line)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (SLIST_FIRST(&td->td_epochs) != et) {
|
2019-12-06 16:34:04 +00:00
|
|
|
epoch_trace_report("Exiting epoch %s in a not nested order "
|
|
|
|
"at %s:%d. Most recently entered %s at %s:%d\n",
|
2019-09-25 18:26:31 +00:00
|
|
|
epoch->e_name,
|
2019-12-06 16:34:04 +00:00
|
|
|
file, line,
|
2019-09-25 18:26:31 +00:00
|
|
|
SLIST_FIRST(&td->td_epochs)->et_epoch->e_name,
|
|
|
|
SLIST_FIRST(&td->td_epochs)->et_file,
|
|
|
|
SLIST_FIRST(&td->td_epochs)->et_line);
|
|
|
|
/* This will panic if et is not anywhere on td_epochs. */
|
|
|
|
SLIST_REMOVE(&td->td_epochs, et, epoch_tracker, et_tlink);
|
|
|
|
} else
|
|
|
|
SLIST_REMOVE_HEAD(&td->td_epochs, et_tlink);
|
|
|
|
}
|
2019-10-15 21:24:25 +00:00
|
|
|
|
|
|
|
/* Used by assertions that check thread state before going to sleep. */
|
|
|
|
void
|
|
|
|
epoch_trace_list(struct thread *td)
|
|
|
|
{
|
|
|
|
epoch_tracker_t iet;
|
|
|
|
|
|
|
|
SLIST_FOREACH(iet, &td->td_epochs, et_tlink)
|
|
|
|
printf("Epoch %s entered at %s:%d\n", iet->et_epoch->e_name,
|
|
|
|
iet->et_file, iet->et_line);
|
|
|
|
}
|
2019-09-25 18:26:31 +00:00
|
|
|
#endif /* EPOCH_TRACE */
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
static void
|
|
|
|
epoch_init(void *arg __unused)
|
|
|
|
{
|
2018-07-06 06:20:03 +00:00
|
|
|
int cpu;
|
2018-05-11 04:54:12 +00:00
|
|
|
|
|
|
|
block_count = counter_u64_alloc(M_WAITOK);
|
|
|
|
migrate_count = counter_u64_alloc(M_WAITOK);
|
|
|
|
turnstile_count = counter_u64_alloc(M_WAITOK);
|
|
|
|
switch_count = counter_u64_alloc(M_WAITOK);
|
2018-05-18 02:57:39 +00:00
|
|
|
epoch_call_count = counter_u64_alloc(M_WAITOK);
|
|
|
|
epoch_call_task_count = counter_u64_alloc(M_WAITOK);
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-11-13 23:57:34 +00:00
|
|
|
pcpu_zone_record = uma_zcreate("epoch_record pcpu",
|
|
|
|
sizeof(struct epoch_record), NULL, NULL, NULL, NULL,
|
|
|
|
UMA_ALIGN_PTR, UMA_ZONE_PCPU);
|
2018-05-17 19:57:07 +00:00
|
|
|
CPU_FOREACH(cpu) {
|
2018-11-13 23:57:34 +00:00
|
|
|
GROUPTASK_INIT(DPCPU_ID_PTR(cpu, epoch_cb_task), 0,
|
|
|
|
epoch_call_task, NULL);
|
|
|
|
taskqgroup_attach_cpu(qgroup_softirq,
|
Make taskqgroup_attach{,_cpu}(9) work across architectures
So far, intr_{g,s}etaffinity(9) take a single int for identifying
a device interrupt. This approach doesn't work on all architectures
supported, as a single int isn't sufficient to globally specify a
device interrupt. In particular, with multiple interrupt controllers
in one system as found on e. g. arm and arm64 machines, an interrupt
number as returned by rman_get_start(9) may be only unique relative
to the bus and, thus, interrupt controller, a certain device hangs
off from.
In turn, this makes taskqgroup_attach{,_cpu}(9) and - internal to
the gtaskqueue implementation - taskqgroup_attach_deferred{,_cpu}()
not work across architectures. Yet in turn, iflib(4) as gtaskqueue
consumer so far doesn't fit architectures where interrupt numbers
aren't globally unique.
However, at least for intr_setaffinity(..., CPU_WHICH_IRQ, ...) as
employed by the gtaskqueue implementation to bind an interrupt to a
particular CPU, using bus_bind_intr(9) instead is equivalent from
a functional point of view, with bus_bind_intr(9) taking the device
and interrupt resource arguments required for uniquely specifying a
device interrupt.
Thus, change the gtaskqueue implementation to employ bus_bind_intr(9)
instead and intr_{g,s}etaffinity(9) to take the device and interrupt
resource arguments required respectively. This change also moves
struct grouptask from <sys/_task.h> to <sys/gtaskqueue.h> and wraps
struct gtask along with the gtask_fn_t typedef into #ifdef _KERNEL
as userland likes to include <sys/_task.h> or indirectly drags it
in - for better or worse also with _KERNEL defined -, which with
device_t and struct resource dependencies otherwise is no longer
as easily possible now.
The userland inclusion problem probably can be improved a bit by
introducing a _WANT_TASK (as well as a _WANT_MOUNT) akin to the
existing _WANT_PRISON etc., which is orthogonal to this change,
though, and likely needs an exp-run.
While at it:
- Change the gt_cpu member in the grouptask structure to be of type
int as used elswhere for specifying CPUs (an int16_t may be too
narrow sooner or later),
- move the gtaskqueue_enqueue_fn typedef from <sys/gtaskqueue.h> to
the gtaskqueue implementation as it's only used and needed there,
- change the GTASK_INIT macro to use "gtask" rather than "task" as
argument given that it actually operates on a struct gtask rather
than a struct task, and
- let subr_gtaskqueue.c consistently use __func__ to print functions
names.
Reported by: mmel
Reviewed by: mmel
Differential Revision: https://reviews.freebsd.org/D19139
2019-02-12 21:23:59 +00:00
|
|
|
DPCPU_ID_PTR(cpu, epoch_cb_task), NULL, cpu, NULL, NULL,
|
2018-11-13 23:57:34 +00:00
|
|
|
"epoch call task");
|
2018-05-17 19:57:07 +00:00
|
|
|
}
|
2019-10-14 04:17:56 +00:00
|
|
|
#ifdef EPOCH_TRACE
|
2019-09-25 18:26:31 +00:00
|
|
|
SLIST_INIT(&thread0.td_epochs);
|
2019-10-14 04:17:56 +00:00
|
|
|
#endif
|
2018-05-10 17:55:24 +00:00
|
|
|
inited = 1;
|
2019-09-25 18:26:31 +00:00
|
|
|
global_epoch = epoch_alloc("Global", 0);
|
|
|
|
global_epoch_preempt = epoch_alloc("Global preemptible", EPOCH_PREEMPT);
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
2019-11-22 23:23:40 +00:00
|
|
|
SYSINIT(epoch, SI_SUB_EPOCH, SI_ORDER_FIRST, epoch_init, NULL);
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-06-23 07:14:08 +00:00
|
|
|
#if !defined(EARLY_AP_STARTUP)
|
|
|
|
static void
|
|
|
|
epoch_init_smp(void *dummy __unused)
|
|
|
|
{
|
|
|
|
inited = 2;
|
|
|
|
}
|
|
|
|
SYSINIT(epoch_smp, SI_SUB_SMP + 1, SI_ORDER_FIRST, epoch_init_smp, NULL);
|
|
|
|
#endif
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
static void
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch_ctor(epoch_t epoch)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
|
|
|
epoch_record_t er;
|
2018-07-06 06:20:03 +00:00
|
|
|
int cpu;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch->e_pcpu_record = uma_zalloc_pcpu(pcpu_zone_record, M_WAITOK);
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
|
|
|
|
bzero(er, sizeof(*er));
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_register(&epoch->e_epoch, &er->er_record, NULL);
|
2018-05-10 17:55:24 +00:00
|
|
|
TAILQ_INIT((struct threadlist *)(uintptr_t)&er->er_tdlist);
|
2018-07-06 06:20:03 +00:00
|
|
|
er->er_cpuid = cpu;
|
2019-06-28 10:38:56 +00:00
|
|
|
er->er_parent = epoch;
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
static void
|
|
|
|
epoch_adjust_prio(struct thread *td, u_char prio)
|
|
|
|
{
|
|
|
|
|
|
|
|
thread_lock(td);
|
|
|
|
sched_prio(td, prio);
|
|
|
|
thread_unlock(td);
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
epoch_t
|
2019-09-25 18:26:31 +00:00
|
|
|
epoch_alloc(const char *name, int flags)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
|
|
|
epoch_t epoch;
|
|
|
|
|
|
|
|
if (__predict_false(!inited))
|
|
|
|
panic("%s called too early in boot", __func__);
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch = malloc(sizeof(struct epoch), M_EPOCH, M_ZERO | M_WAITOK);
|
2018-05-10 17:55:24 +00:00
|
|
|
ck_epoch_init(&epoch->e_epoch);
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch_ctor(epoch);
|
2018-05-30 03:39:57 +00:00
|
|
|
MPASS(epoch_count < MAX_EPOCHS - 2);
|
2018-05-18 01:52:51 +00:00
|
|
|
epoch->e_flags = flags;
|
2018-05-17 19:57:07 +00:00
|
|
|
epoch->e_idx = epoch_count;
|
2019-09-25 18:26:31 +00:00
|
|
|
epoch->e_name = name;
|
2019-06-28 10:38:56 +00:00
|
|
|
sx_init(&epoch->e_drain_sx, "epoch-drain-sx");
|
|
|
|
mtx_init(&epoch->e_drain_mtx, "epoch-drain-mtx", NULL, MTX_DEF);
|
2018-05-17 19:57:07 +00:00
|
|
|
allepochs[epoch_count++] = epoch;
|
2018-05-10 17:55:24 +00:00
|
|
|
return (epoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
epoch_free(epoch_t epoch)
|
|
|
|
{
|
|
|
|
|
2019-06-28 10:38:56 +00:00
|
|
|
epoch_drain_callbacks(epoch);
|
2018-05-17 19:57:07 +00:00
|
|
|
allepochs[epoch->e_idx] = NULL;
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_wait(global_epoch);
|
2018-07-06 06:20:03 +00:00
|
|
|
uma_zfree_pcpu(pcpu_zone_record, epoch->e_pcpu_record);
|
2019-06-28 10:38:56 +00:00
|
|
|
mtx_destroy(&epoch->e_drain_mtx);
|
|
|
|
sx_destroy(&epoch->e_drain_sx);
|
2018-05-10 17:55:24 +00:00
|
|
|
free(epoch, M_EPOCH);
|
|
|
|
}
|
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
static epoch_record_t
|
|
|
|
epoch_currecord(epoch_t epoch)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (zpcpu_get_cpu(epoch->e_pcpu_record, curcpu));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INIT_CHECK(epoch) \
|
|
|
|
do { \
|
|
|
|
if (__predict_false((epoch) == NULL)) \
|
|
|
|
return; \
|
|
|
|
} while (0)
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
void
|
2019-09-25 18:26:31 +00:00
|
|
|
_epoch_enter_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
2018-11-13 19:02:11 +00:00
|
|
|
struct epoch_record *er;
|
2018-11-13 23:45:38 +00:00
|
|
|
struct thread *td;
|
2018-11-13 19:02:11 +00:00
|
|
|
|
|
|
|
MPASS(cold || epoch != NULL);
|
|
|
|
MPASS(epoch->e_flags & EPOCH_PREEMPT);
|
2018-11-13 23:45:38 +00:00
|
|
|
td = curthread;
|
2019-10-21 23:12:14 +00:00
|
|
|
MPASS((vm_offset_t)et >= td->td_kstack &&
|
2019-10-22 18:05:15 +00:00
|
|
|
(vm_offset_t)et + sizeof(struct epoch_tracker) <=
|
2019-10-21 23:12:14 +00:00
|
|
|
td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
|
|
|
|
|
|
|
|
INIT_CHECK(epoch);
|
2019-09-25 18:26:31 +00:00
|
|
|
#ifdef EPOCH_TRACE
|
|
|
|
epoch_trace_enter(td, epoch, et, file, line);
|
|
|
|
#endif
|
2018-11-13 23:45:38 +00:00
|
|
|
et->et_td = td;
|
2019-10-29 17:28:25 +00:00
|
|
|
THREAD_NO_SLEEPING();
|
2018-11-13 19:02:11 +00:00
|
|
|
critical_enter();
|
2018-11-13 23:45:38 +00:00
|
|
|
sched_pin();
|
2018-11-13 19:02:11 +00:00
|
|
|
td->td_pre_epoch_prio = td->td_priority;
|
|
|
|
er = epoch_currecord(epoch);
|
2018-11-13 23:20:55 +00:00
|
|
|
TAILQ_INSERT_TAIL(&er->er_tdlist, et, et_link);
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_begin(&er->er_record, &et->et_section);
|
2018-11-13 19:02:11 +00:00
|
|
|
critical_exit();
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
void
|
2018-11-13 19:02:11 +00:00
|
|
|
epoch_enter(epoch_t epoch)
|
2018-05-17 19:57:07 +00:00
|
|
|
{
|
2018-11-13 19:02:11 +00:00
|
|
|
epoch_record_t er;
|
2018-05-17 19:57:07 +00:00
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
MPASS(cold || epoch != NULL);
|
|
|
|
INIT_CHECK(epoch);
|
|
|
|
critical_enter();
|
|
|
|
er = epoch_currecord(epoch);
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_begin(&er->er_record, NULL);
|
2018-05-17 19:57:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
void
|
2019-09-25 18:26:31 +00:00
|
|
|
_epoch_exit_preempt(epoch_t epoch, epoch_tracker_t et EPOCH_FILE_LINE)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
2018-11-13 19:02:11 +00:00
|
|
|
struct epoch_record *er;
|
2018-11-13 23:45:38 +00:00
|
|
|
struct thread *td;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
INIT_CHECK(epoch);
|
2018-11-13 23:45:38 +00:00
|
|
|
td = curthread;
|
2018-11-13 19:02:11 +00:00
|
|
|
critical_enter();
|
2018-11-13 23:45:38 +00:00
|
|
|
sched_unpin();
|
2019-10-29 17:28:25 +00:00
|
|
|
THREAD_SLEEPING_OK();
|
2018-11-13 19:02:11 +00:00
|
|
|
er = epoch_currecord(epoch);
|
|
|
|
MPASS(epoch->e_flags & EPOCH_PREEMPT);
|
2018-11-13 23:20:55 +00:00
|
|
|
MPASS(et != NULL);
|
2018-11-13 23:45:38 +00:00
|
|
|
MPASS(et->et_td == td);
|
2018-11-13 23:20:55 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
et->et_td = (void*)0xDEADBEEF;
|
2018-11-13 19:02:11 +00:00
|
|
|
#endif
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_end(&er->er_record, &et->et_section);
|
2018-11-13 23:20:55 +00:00
|
|
|
TAILQ_REMOVE(&er->er_tdlist, et, et_link);
|
2018-11-13 19:02:11 +00:00
|
|
|
er->er_gen++;
|
|
|
|
if (__predict_false(td->td_pre_epoch_prio != td->td_priority))
|
2018-11-13 23:45:38 +00:00
|
|
|
epoch_adjust_prio(td, td->td_pre_epoch_prio);
|
2018-11-13 19:02:11 +00:00
|
|
|
critical_exit();
|
2019-09-25 18:26:31 +00:00
|
|
|
#ifdef EPOCH_TRACE
|
|
|
|
epoch_trace_exit(td, epoch, et, file, line);
|
|
|
|
#endif
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
void
|
2018-11-13 19:02:11 +00:00
|
|
|
epoch_exit(epoch_t epoch)
|
2018-05-17 19:57:07 +00:00
|
|
|
{
|
2018-11-13 19:02:11 +00:00
|
|
|
epoch_record_t er;
|
2018-05-17 19:57:07 +00:00
|
|
|
|
2018-11-13 19:02:11 +00:00
|
|
|
INIT_CHECK(epoch);
|
|
|
|
er = epoch_currecord(epoch);
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_end(&er->er_record, NULL);
|
2018-11-13 19:02:11 +00:00
|
|
|
critical_exit();
|
2018-05-17 19:57:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
/*
|
2018-11-13 23:57:34 +00:00
|
|
|
* epoch_block_handler_preempt() is a callback from the CK code when another
|
|
|
|
* thread is currently in an epoch section.
|
2018-05-10 17:55:24 +00:00
|
|
|
*/
|
|
|
|
static void
|
2018-11-13 23:57:34 +00:00
|
|
|
epoch_block_handler_preempt(struct ck_epoch *global __unused,
|
|
|
|
ck_epoch_record_t *cr, void *arg __unused)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
|
|
|
epoch_record_t record;
|
2018-07-04 02:47:16 +00:00
|
|
|
struct thread *td, *owner, *curwaittd;
|
2018-11-13 23:20:55 +00:00
|
|
|
struct epoch_tracker *tdwait;
|
2018-05-10 17:55:24 +00:00
|
|
|
struct turnstile *ts;
|
|
|
|
struct lock_object *lock;
|
2018-05-11 04:54:12 +00:00
|
|
|
int spincount, gen;
|
2018-06-24 18:57:06 +00:00
|
|
|
int locksheld __unused;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-11-14 00:12:04 +00:00
|
|
|
record = __containerof(cr, struct epoch_record, er_record);
|
2018-05-10 17:55:24 +00:00
|
|
|
td = curthread;
|
2018-06-24 18:57:06 +00:00
|
|
|
locksheld = td->td_locks;
|
2018-05-10 17:55:24 +00:00
|
|
|
spincount = 0;
|
|
|
|
counter_u64_add(block_count, 1);
|
2018-08-09 05:18:27 +00:00
|
|
|
/*
|
|
|
|
* We lost a race and there's no longer any threads
|
|
|
|
* on the CPU in an epoch section.
|
|
|
|
*/
|
|
|
|
if (TAILQ_EMPTY(&record->er_tdlist))
|
|
|
|
return;
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
if (record->er_cpuid != curcpu) {
|
|
|
|
/*
|
|
|
|
* If the head of the list is running, we can wait for it
|
|
|
|
* to remove itself from the list and thus save us the
|
|
|
|
* overhead of a migration
|
|
|
|
*/
|
2018-08-09 05:18:27 +00:00
|
|
|
gen = record->er_gen;
|
|
|
|
thread_unlock(td);
|
|
|
|
/*
|
|
|
|
* We can't actually check if the waiting thread is running
|
|
|
|
* so we simply poll for it to exit before giving up and
|
|
|
|
* migrating.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
cpu_spinwait();
|
|
|
|
} while (!TAILQ_EMPTY(&record->er_tdlist) &&
|
|
|
|
gen == record->er_gen &&
|
|
|
|
spincount++ < MAX_ADAPTIVE_SPIN);
|
|
|
|
thread_lock(td);
|
|
|
|
/*
|
|
|
|
* If the generation has changed we can poll again
|
|
|
|
* otherwise we need to migrate.
|
|
|
|
*/
|
|
|
|
if (gen != record->er_gen)
|
2018-05-10 17:55:24 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Being on the same CPU as that of the record on which
|
|
|
|
* we need to wait allows us access to the thread
|
|
|
|
* list associated with that CPU. We can then examine the
|
|
|
|
* oldest thread in the queue and wait on its turnstile
|
|
|
|
* until it resumes and so on until a grace period
|
|
|
|
* elapses.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
counter_u64_add(migrate_count, 1);
|
|
|
|
sched_bind(td, record->er_cpuid);
|
|
|
|
/*
|
|
|
|
* At this point we need to return to the ck code
|
|
|
|
* to scan to see if a grace period has elapsed.
|
|
|
|
* We can't move on to check the thread list, because
|
|
|
|
* in the meantime new threads may have arrived that
|
|
|
|
* in fact belong to a different epoch.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
2018-05-30 03:39:57 +00:00
|
|
|
* Try to find a thread in an epoch section on this CPU
|
2018-05-10 17:55:24 +00:00
|
|
|
* waiting on a turnstile. Otherwise find the lowest
|
|
|
|
* priority thread (highest prio value) and drop our priority
|
|
|
|
* to match to allow it to run.
|
|
|
|
*/
|
2018-07-04 02:47:16 +00:00
|
|
|
TAILQ_FOREACH(tdwait, &record->er_tdlist, et_link) {
|
2018-05-11 04:54:12 +00:00
|
|
|
/*
|
|
|
|
* Propagate our priority to any other waiters to prevent us
|
|
|
|
* from starving them. They will have their original priority
|
|
|
|
* restore on exit from epoch_wait().
|
|
|
|
*/
|
2018-07-04 02:47:16 +00:00
|
|
|
curwaittd = tdwait->et_td;
|
|
|
|
if (!TD_IS_INHIBITED(curwaittd) && curwaittd->td_priority > td->td_priority) {
|
2018-05-17 19:41:58 +00:00
|
|
|
critical_enter();
|
|
|
|
thread_unlock(td);
|
2018-07-04 02:47:16 +00:00
|
|
|
thread_lock(curwaittd);
|
|
|
|
sched_prio(curwaittd, td->td_priority);
|
|
|
|
thread_unlock(curwaittd);
|
2018-05-17 19:41:58 +00:00
|
|
|
thread_lock(td);
|
|
|
|
critical_exit();
|
2018-05-11 04:54:12 +00:00
|
|
|
}
|
2018-07-04 02:47:16 +00:00
|
|
|
if (TD_IS_INHIBITED(curwaittd) && TD_ON_LOCK(curwaittd) &&
|
|
|
|
((ts = curwaittd->td_blocked) != NULL)) {
|
2018-05-10 17:55:24 +00:00
|
|
|
/*
|
2018-11-13 23:57:34 +00:00
|
|
|
* We unlock td to allow turnstile_wait to reacquire
|
|
|
|
* the thread lock. Before unlocking it we enter a
|
|
|
|
* critical section to prevent preemption after we
|
|
|
|
* reenable interrupts by dropping the thread lock in
|
|
|
|
* order to prevent curwaittd from getting to run.
|
2018-05-10 17:55:24 +00:00
|
|
|
*/
|
|
|
|
critical_enter();
|
|
|
|
thread_unlock(td);
|
2019-07-24 23:04:59 +00:00
|
|
|
|
|
|
|
if (turnstile_lock(ts, &lock, &owner)) {
|
|
|
|
if (ts == curwaittd->td_blocked) {
|
|
|
|
MPASS(TD_IS_INHIBITED(curwaittd) &&
|
|
|
|
TD_ON_LOCK(curwaittd));
|
|
|
|
critical_exit();
|
|
|
|
turnstile_wait(ts, owner,
|
|
|
|
curwaittd->td_tsqueue);
|
|
|
|
counter_u64_add(turnstile_count, 1);
|
|
|
|
thread_lock(td);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-10 17:55:24 +00:00
|
|
|
turnstile_unlock(ts, lock);
|
2019-07-24 23:04:59 +00:00
|
|
|
}
|
2018-05-10 17:55:24 +00:00
|
|
|
thread_lock(td);
|
|
|
|
critical_exit();
|
2018-06-24 18:57:06 +00:00
|
|
|
KASSERT(td->td_locks == locksheld,
|
|
|
|
("%d extra locks held", td->td_locks - locksheld));
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We didn't find any threads actually blocked on a lock
|
2018-05-11 04:54:12 +00:00
|
|
|
* so we have nothing to do except context switch away.
|
2018-05-10 17:55:24 +00:00
|
|
|
*/
|
|
|
|
counter_u64_add(switch_count, 1);
|
2019-12-15 21:26:50 +00:00
|
|
|
mi_switch(SW_VOL | SWT_RELINQUISH);
|
2019-12-18 09:30:32 +00:00
|
|
|
/*
|
|
|
|
* It is important the thread lock is dropped while yielding
|
|
|
|
* to allow other threads to acquire the lock pointed to by
|
|
|
|
* TDQ_LOCKPTR(td). Currently mi_switch() will unlock the
|
|
|
|
* thread lock before returning. Else a deadlock like
|
|
|
|
* situation might happen.
|
|
|
|
*/
|
2018-05-10 17:55:24 +00:00
|
|
|
thread_lock(td);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_wait_preempt(epoch_t epoch)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
int was_bound;
|
|
|
|
int old_cpu;
|
|
|
|
int old_pinned;
|
|
|
|
u_char old_prio;
|
2018-06-24 18:57:06 +00:00
|
|
|
int locks __unused;
|
2018-05-18 18:27:17 +00:00
|
|
|
|
|
|
|
MPASS(cold || epoch != NULL);
|
2018-05-10 17:55:24 +00:00
|
|
|
INIT_CHECK(epoch);
|
|
|
|
td = curthread;
|
2018-06-24 18:57:06 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
locks = curthread->td_locks;
|
|
|
|
MPASS(epoch->e_flags & EPOCH_PREEMPT);
|
|
|
|
if ((epoch->e_flags & EPOCH_LOCKED) == 0)
|
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
|
|
|
|
"epoch_wait() can be long running");
|
2018-11-13 23:57:34 +00:00
|
|
|
KASSERT(!in_epoch(epoch), ("epoch_wait_preempt() called in the middle "
|
|
|
|
"of an epoch section of the same epoch"));
|
2018-06-24 18:57:06 +00:00
|
|
|
#endif
|
2018-05-10 17:55:24 +00:00
|
|
|
DROP_GIANT();
|
2020-01-13 14:30:19 +00:00
|
|
|
thread_lock(td);
|
2018-05-10 17:55:24 +00:00
|
|
|
|
|
|
|
old_cpu = PCPU_GET(cpuid);
|
|
|
|
old_pinned = td->td_pinned;
|
|
|
|
old_prio = td->td_priority;
|
|
|
|
was_bound = sched_is_bound(td);
|
|
|
|
sched_unbind(td);
|
|
|
|
td->td_pinned = 0;
|
|
|
|
sched_bind(td, old_cpu);
|
|
|
|
|
2018-11-13 23:57:34 +00:00
|
|
|
ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler_preempt,
|
|
|
|
NULL);
|
2018-05-10 17:55:24 +00:00
|
|
|
|
|
|
|
/* restore CPU binding, if any */
|
|
|
|
if (was_bound != 0) {
|
|
|
|
sched_bind(td, old_cpu);
|
|
|
|
} else {
|
|
|
|
/* get thread back to initial CPU, if any */
|
|
|
|
if (old_pinned != 0)
|
|
|
|
sched_bind(td, old_cpu);
|
|
|
|
sched_unbind(td);
|
|
|
|
}
|
|
|
|
/* restore pinned after bind */
|
|
|
|
td->td_pinned = old_pinned;
|
|
|
|
|
|
|
|
/* restore thread priority */
|
|
|
|
sched_prio(td, old_prio);
|
|
|
|
thread_unlock(td);
|
|
|
|
PICKUP_GIANT();
|
2018-05-14 00:14:00 +00:00
|
|
|
KASSERT(td->td_locks == locks,
|
2018-05-30 03:39:57 +00:00
|
|
|
("%d residual locks held", td->td_locks - locks));
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
static void
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_block_handler(struct ck_epoch *g __unused, ck_epoch_record_t *c __unused,
|
2018-05-30 03:39:57 +00:00
|
|
|
void *arg __unused)
|
2018-05-18 01:52:51 +00:00
|
|
|
{
|
|
|
|
cpu_spinwait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_wait(epoch_t epoch)
|
2018-05-18 01:52:51 +00:00
|
|
|
{
|
|
|
|
|
2018-05-18 18:27:17 +00:00
|
|
|
MPASS(cold || epoch != NULL);
|
|
|
|
INIT_CHECK(epoch);
|
2018-05-18 17:29:43 +00:00
|
|
|
MPASS(epoch->e_flags == 0);
|
2018-05-18 01:52:51 +00:00
|
|
|
critical_enter();
|
2018-05-18 17:29:43 +00:00
|
|
|
ck_epoch_synchronize_wait(&epoch->e_epoch, epoch_block_handler, NULL);
|
2018-05-18 01:52:51 +00:00
|
|
|
critical_exit();
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:55:24 +00:00
|
|
|
void
|
|
|
|
epoch_call(epoch_t epoch, epoch_context_t ctx, void (*callback) (epoch_context_t))
|
|
|
|
{
|
2018-07-04 02:47:16 +00:00
|
|
|
epoch_record_t er;
|
2018-05-17 19:50:55 +00:00
|
|
|
ck_epoch_entry_t *cb;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
|
|
|
cb = (void *)ctx;
|
2018-05-11 04:54:12 +00:00
|
|
|
|
2018-05-13 23:24:48 +00:00
|
|
|
MPASS(callback);
|
|
|
|
/* too early in boot to have epoch set up */
|
2018-05-17 19:50:55 +00:00
|
|
|
if (__predict_false(epoch == NULL))
|
|
|
|
goto boottime;
|
2018-06-23 07:14:08 +00:00
|
|
|
#if !defined(EARLY_AP_STARTUP)
|
|
|
|
if (__predict_false(inited < 2))
|
|
|
|
goto boottime;
|
|
|
|
#endif
|
2018-05-17 19:50:55 +00:00
|
|
|
|
2018-05-11 04:54:12 +00:00
|
|
|
critical_enter();
|
2018-05-17 21:39:15 +00:00
|
|
|
*DPCPU_PTR(epoch_cb_count) += 1;
|
2018-07-06 06:20:03 +00:00
|
|
|
er = epoch_currecord(epoch);
|
2018-11-14 00:12:04 +00:00
|
|
|
ck_epoch_call(&er->er_record, cb, (ck_epoch_cb_t *)callback);
|
2018-05-11 04:54:12 +00:00
|
|
|
critical_exit();
|
2018-05-17 19:50:55 +00:00
|
|
|
return;
|
2018-05-30 03:39:57 +00:00
|
|
|
boottime:
|
2018-05-17 19:50:55 +00:00
|
|
|
callback(ctx);
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-05-17 19:57:07 +00:00
|
|
|
epoch_call_task(void *arg __unused)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
2018-05-17 19:57:07 +00:00
|
|
|
ck_stack_entry_t *cursor, *head, *next;
|
|
|
|
ck_epoch_record_t *record;
|
2018-07-06 06:20:03 +00:00
|
|
|
epoch_record_t er;
|
2018-05-10 17:55:24 +00:00
|
|
|
epoch_t epoch;
|
2018-05-17 19:57:07 +00:00
|
|
|
ck_stack_t cb_stack;
|
|
|
|
int i, npending, total;
|
2018-05-10 17:55:24 +00:00
|
|
|
|
2018-05-17 19:57:07 +00:00
|
|
|
ck_stack_init(&cb_stack);
|
|
|
|
critical_enter();
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_enter(global_epoch);
|
2018-05-17 19:57:07 +00:00
|
|
|
for (total = i = 0; i < epoch_count; i++) {
|
|
|
|
if (__predict_false((epoch = allepochs[i]) == NULL))
|
|
|
|
continue;
|
2018-07-06 06:20:03 +00:00
|
|
|
er = epoch_currecord(epoch);
|
2018-11-14 00:12:04 +00:00
|
|
|
record = &er->er_record;
|
2018-05-17 19:57:07 +00:00
|
|
|
if ((npending = record->n_pending) == 0)
|
|
|
|
continue;
|
|
|
|
ck_epoch_poll_deferred(record, &cb_stack);
|
|
|
|
total += npending - record->n_pending;
|
2018-05-11 04:54:12 +00:00
|
|
|
}
|
2018-05-18 17:29:43 +00:00
|
|
|
epoch_exit(global_epoch);
|
2018-05-17 21:39:15 +00:00
|
|
|
*DPCPU_PTR(epoch_cb_count) -= total;
|
2018-05-17 19:57:07 +00:00
|
|
|
critical_exit();
|
|
|
|
|
2018-05-18 01:52:51 +00:00
|
|
|
counter_u64_add(epoch_call_count, total);
|
|
|
|
counter_u64_add(epoch_call_task_count, 1);
|
|
|
|
|
2018-05-17 19:57:07 +00:00
|
|
|
head = ck_stack_batch_pop_npsc(&cb_stack);
|
|
|
|
for (cursor = head; cursor != NULL; cursor = next) {
|
2018-05-17 19:50:55 +00:00
|
|
|
struct ck_epoch_entry *entry =
|
2018-11-13 23:57:34 +00:00
|
|
|
ck_epoch_entry_container(cursor);
|
2018-05-30 03:39:57 +00:00
|
|
|
|
2018-05-17 19:57:07 +00:00
|
|
|
next = CK_STACK_NEXT(cursor);
|
2018-05-17 19:50:55 +00:00
|
|
|
entry->function(entry);
|
2018-05-11 04:54:12 +00:00
|
|
|
}
|
2018-05-10 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-07-04 02:47:16 +00:00
|
|
|
in_epoch_verbose(epoch_t epoch, int dump_onfail)
|
|
|
|
{
|
2018-11-13 23:20:55 +00:00
|
|
|
struct epoch_tracker *tdwait;
|
2018-07-04 02:47:16 +00:00
|
|
|
struct thread *td;
|
|
|
|
epoch_record_t er;
|
|
|
|
|
|
|
|
td = curthread;
|
2019-10-29 17:28:25 +00:00
|
|
|
if (THREAD_CAN_SLEEP())
|
2018-07-04 02:47:16 +00:00
|
|
|
return (0);
|
|
|
|
if (__predict_false((epoch) == NULL))
|
|
|
|
return (0);
|
|
|
|
critical_enter();
|
2018-07-06 06:20:03 +00:00
|
|
|
er = epoch_currecord(epoch);
|
2018-07-04 02:47:16 +00:00
|
|
|
TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
|
|
|
|
if (tdwait->et_td == td) {
|
|
|
|
critical_exit();
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
if (dump_onfail) {
|
|
|
|
MPASS(td->td_pinned);
|
|
|
|
printf("cpu: %d id: %d\n", curcpu, td->td_tid);
|
|
|
|
TAILQ_FOREACH(tdwait, &er->er_tdlist, et_link)
|
|
|
|
printf("td_tid: %d ", tdwait->et_td->td_tid);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
critical_exit();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
in_epoch(epoch_t epoch)
|
2018-05-10 17:55:24 +00:00
|
|
|
{
|
2018-07-04 02:47:16 +00:00
|
|
|
return (in_epoch_verbose(epoch, 0));
|
|
|
|
}
|
2018-11-13 22:58:38 +00:00
|
|
|
|
2019-06-28 10:38:56 +00:00
|
|
|
static void
|
|
|
|
epoch_drain_cb(struct epoch_context *ctx)
|
|
|
|
{
|
|
|
|
struct epoch *epoch =
|
|
|
|
__containerof(ctx, struct epoch_record, er_drain_ctx)->er_parent;
|
|
|
|
|
|
|
|
if (atomic_fetchadd_int(&epoch->e_drain_count, -1) == 1) {
|
|
|
|
mtx_lock(&epoch->e_drain_mtx);
|
|
|
|
wakeup(epoch);
|
|
|
|
mtx_unlock(&epoch->e_drain_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
epoch_drain_callbacks(epoch_t epoch)
|
|
|
|
{
|
|
|
|
epoch_record_t er;
|
|
|
|
struct thread *td;
|
|
|
|
int was_bound;
|
|
|
|
int old_pinned;
|
|
|
|
int old_cpu;
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
|
|
|
|
"epoch_drain_callbacks() may sleep!");
|
|
|
|
|
|
|
|
/* too early in boot to have epoch set up */
|
|
|
|
if (__predict_false(epoch == NULL))
|
|
|
|
return;
|
|
|
|
#if !defined(EARLY_AP_STARTUP)
|
|
|
|
if (__predict_false(inited < 2))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
DROP_GIANT();
|
|
|
|
|
|
|
|
sx_xlock(&epoch->e_drain_sx);
|
|
|
|
mtx_lock(&epoch->e_drain_mtx);
|
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
thread_lock(td);
|
|
|
|
old_cpu = PCPU_GET(cpuid);
|
|
|
|
old_pinned = td->td_pinned;
|
|
|
|
was_bound = sched_is_bound(td);
|
|
|
|
sched_unbind(td);
|
|
|
|
td->td_pinned = 0;
|
|
|
|
|
|
|
|
CPU_FOREACH(cpu)
|
|
|
|
epoch->e_drain_count++;
|
|
|
|
CPU_FOREACH(cpu) {
|
|
|
|
er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu);
|
|
|
|
sched_bind(td, cpu);
|
|
|
|
epoch_call(epoch, &er->er_drain_ctx, &epoch_drain_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore CPU binding, if any */
|
|
|
|
if (was_bound != 0) {
|
|
|
|
sched_bind(td, old_cpu);
|
|
|
|
} else {
|
|
|
|
/* get thread back to initial CPU, if any */
|
|
|
|
if (old_pinned != 0)
|
|
|
|
sched_bind(td, old_cpu);
|
|
|
|
sched_unbind(td);
|
|
|
|
}
|
|
|
|
/* restore pinned after bind */
|
|
|
|
td->td_pinned = old_pinned;
|
|
|
|
|
|
|
|
thread_unlock(td);
|
|
|
|
|
|
|
|
while (epoch->e_drain_count != 0)
|
|
|
|
msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0);
|
|
|
|
|
|
|
|
mtx_unlock(&epoch->e_drain_mtx);
|
|
|
|
sx_xunlock(&epoch->e_drain_sx);
|
|
|
|
|
|
|
|
PICKUP_GIANT();
|
|
|
|
}
|