2018-05-13 23:16:04 +00:00
|
|
|
.\"
|
2018-06-22 06:04:22 +00:00
|
|
|
.\" Copyright (C) 2018 Matthew Macy <mmacy@FreeBSD.org>.
|
2018-05-13 23:16:04 +00:00
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice(s), this list of conditions and the following disclaimer as
|
|
|
|
.\" the first lines of this file unmodified other than the possible
|
|
|
|
.\" addition of one or more copyright notices.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice(s), this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
|
|
|
|
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
.\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) 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.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2020-04-30 11:17:29 +00:00
|
|
|
.Dd April 30, 2020
|
2018-05-13 23:16:04 +00:00
|
|
|
.Dt EPOCH 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm epoch ,
|
|
|
|
.Nm epoch_context ,
|
|
|
|
.Nm epoch_alloc ,
|
|
|
|
.Nm epoch_free ,
|
|
|
|
.Nm epoch_enter ,
|
|
|
|
.Nm epoch_exit ,
|
|
|
|
.Nm epoch_wait ,
|
2019-12-28 01:35:32 +00:00
|
|
|
.Nm epoch_enter_preempt ,
|
|
|
|
.Nm epoch_exit_preempt ,
|
|
|
|
.Nm epoch_wait_preempt ,
|
2018-05-13 23:16:04 +00:00
|
|
|
.Nm epoch_call ,
|
2019-06-28 10:38:56 +00:00
|
|
|
.Nm epoch_drain_callbacks ,
|
2018-05-13 23:16:04 +00:00
|
|
|
.Nm in_epoch ,
|
2019-12-28 01:35:32 +00:00
|
|
|
.Nm in_epoch_verbose ,
|
2018-06-25 06:57:10 +00:00
|
|
|
.Nd kernel epoch based reclamation
|
2018-05-13 23:16:04 +00:00
|
|
|
.Sh SYNOPSIS
|
|
|
|
.In sys/param.h
|
|
|
|
.In sys/proc.h
|
|
|
|
.In sys/epoch.h
|
2019-12-28 01:35:32 +00:00
|
|
|
.\" Types
|
|
|
|
.Bd -literal
|
|
|
|
struct epoch; /* Opaque */
|
|
|
|
.Ed
|
|
|
|
.Vt typedef "struct epoch *epoch_t" ;
|
|
|
|
.Bd -literal
|
|
|
|
struct epoch_context {
|
|
|
|
void *data[2];
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Vt typedef "struct epoch_context *epoch_context_t" ;
|
2020-01-22 02:28:39 +00:00
|
|
|
.Vt typedef "void epoch_callback_t(epoch_context_t)" ;
|
2019-12-28 01:35:32 +00:00
|
|
|
.Bd -literal
|
|
|
|
struct epoch_tracker; /* Opaque */
|
|
|
|
.Ed
|
|
|
|
.Vt typedef "struct epoch_tracker *epoch_tracker_t" ;
|
|
|
|
.\" Declarations
|
2018-05-13 23:16:04 +00:00
|
|
|
.Ft epoch_t
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_alloc "const char *name" "int flags"
|
2018-05-13 23:16:04 +00:00
|
|
|
.Ft void
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_free "epoch_t epoch"
|
2018-05-13 23:16:04 +00:00
|
|
|
.Ft void
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_enter "epoch_t epoch"
|
2018-05-18 04:13:58 +00:00
|
|
|
.Ft void
|
2018-05-13 23:16:04 +00:00
|
|
|
.Fn epoch_exit "epoch_t epoch"
|
|
|
|
.Ft void
|
|
|
|
.Fn epoch_wait "epoch_t epoch"
|
|
|
|
.Ft void
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_enter_preempt "epoch_t epoch" "epoch_tracker_t et"
|
|
|
|
.Ft void
|
|
|
|
.Fn epoch_exit_preempt "epoch_t epoch" "epoch_tracker_t et"
|
|
|
|
.Ft void
|
2018-05-18 17:29:43 +00:00
|
|
|
.Fn epoch_wait_preempt "epoch_t epoch"
|
2018-05-18 04:13:58 +00:00
|
|
|
.Ft void
|
2020-01-22 02:28:39 +00:00
|
|
|
.Fn epoch_call "epoch_t epoch" "epoch_callback_t callback" "epoch_context_t ctx"
|
2019-06-28 10:38:56 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn epoch_drain_callbacks "epoch_t epoch"
|
2018-05-13 23:16:04 +00:00
|
|
|
.Ft int
|
2018-07-04 03:54:39 +00:00
|
|
|
.Fn in_epoch "epoch_t epoch"
|
2019-12-28 01:35:32 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn in_epoch_verbose "epoch_t epoch" "int dump_onfail"
|
2018-05-13 23:16:04 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
Epochs are used to guarantee liveness and immutability of data by
|
|
|
|
deferring reclamation and mutation until a grace period has elapsed.
|
2018-06-22 06:04:22 +00:00
|
|
|
Epochs do not have any lock ordering issues.
|
|
|
|
Entering and leaving an epoch section will never block.
|
2018-05-13 23:16:04 +00:00
|
|
|
.Pp
|
|
|
|
Epochs are allocated with
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_alloc .
|
|
|
|
The
|
|
|
|
.Fa name
|
|
|
|
argument is used for debugging convenience when the
|
|
|
|
.Cd EPOCH_TRACE
|
|
|
|
kernel option is configured.
|
|
|
|
By default, epochs do not allow preemption during sections.
|
|
|
|
By default mutexes cannot be held across
|
|
|
|
.Fn epoch_wait_preempt .
|
|
|
|
The
|
|
|
|
.Fa flags
|
|
|
|
specified are formed by
|
|
|
|
.Em OR Ns 'ing
|
|
|
|
the following values:
|
|
|
|
.Bl -tag -offset indent -width Ds
|
|
|
|
.It Dv EPOCH_LOCKED
|
|
|
|
Permit holding mutexes across
|
|
|
|
.Fn epoch_wait_preempt
|
|
|
|
(requires
|
|
|
|
.Dv EPOCH_PREEMPT ) .
|
|
|
|
When doing this one must be cautious of creating a situation where a deadlock
|
|
|
|
is possible.
|
|
|
|
.It Dv EPOCH_PREEMPT
|
|
|
|
The
|
|
|
|
.Vt epoch
|
|
|
|
will allow preemption during sections.
|
|
|
|
Only non-sleepable locks may be acquired during a preemptible epoch.
|
|
|
|
The functions
|
|
|
|
.Fn epoch_enter_preempt ,
|
|
|
|
.Fn epoch_exit_preempt ,
|
|
|
|
and
|
|
|
|
.Fn epoch_wait_preempt
|
|
|
|
must be used in place of
|
|
|
|
.Fn epoch_enter ,
|
|
|
|
.Fn epoch_exit ,
|
|
|
|
and
|
|
|
|
.Fn epoch_wait ,
|
|
|
|
respectively.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
.Vt epoch Ns s
|
|
|
|
are freed with
|
2018-05-13 23:16:04 +00:00
|
|
|
.Fn epoch_free .
|
2019-12-28 01:35:32 +00:00
|
|
|
.Pp
|
2018-05-13 23:16:04 +00:00
|
|
|
Threads indicate the start of an epoch critical section by calling
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_enter
|
|
|
|
(or
|
|
|
|
.Fn epoch_enter_preempt
|
|
|
|
for preemptible epochs).
|
|
|
|
Threads call
|
|
|
|
.Fn epoch_exit
|
|
|
|
(or
|
|
|
|
.Fn epoch_exit_preempt
|
|
|
|
for preemptible epochs)
|
|
|
|
to indicate the end of a critical section.
|
|
|
|
.Vt struct epoch_tracker Ns s
|
|
|
|
are stack objects whose pointers are passed to
|
2018-05-18 17:29:43 +00:00
|
|
|
.Fn epoch_enter_preempt
|
2018-05-13 23:16:04 +00:00
|
|
|
and
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_exit_preempt
|
|
|
|
(much like
|
|
|
|
.Vt struct rm_priotracker ) .
|
|
|
|
.Pp
|
|
|
|
Threads can defer work until a grace period has expired since any thread has
|
|
|
|
entered the epoch either synchronously or asynchronously.
|
|
|
|
.Fn epoch_call
|
|
|
|
defers work asynchronously by invoking the provided
|
|
|
|
.Fa callback
|
|
|
|
at a later time.
|
|
|
|
.Fn epoch_wait
|
|
|
|
(or
|
|
|
|
.Fn epoch_wait_preempt )
|
|
|
|
blocks the current thread until the grace period has expired and the work can be
|
|
|
|
done safely.
|
|
|
|
.Pp
|
|
|
|
Default, non-preemptible epoch wait
|
|
|
|
.Fn ( epoch_wait )
|
|
|
|
is guaranteed to have much shorter completion times relative to
|
|
|
|
preemptible epoch wait
|
|
|
|
.Fn ( epoch_wait_preempt ) .
|
|
|
|
(In the default type, none of the threads in an epoch section will be preempted
|
|
|
|
before completing its section.)
|
|
|
|
.Pp
|
2018-05-13 23:16:04 +00:00
|
|
|
INVARIANTS can assert that a thread is in an epoch by using
|
|
|
|
.Fn in_epoch .
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn in_epoch "epoch"
|
|
|
|
is equivalent to invoking
|
|
|
|
.Fn in_epoch_verbose "epoch" "0" .
|
|
|
|
If
|
|
|
|
.Cd EPOCH_TRACE
|
|
|
|
is enabled,
|
|
|
|
.Fn in_epoch_verbose "epoch" "1"
|
|
|
|
provides additional verbose debugging information.
|
2018-05-13 23:16:04 +00:00
|
|
|
.Pp
|
2018-05-18 17:29:43 +00:00
|
|
|
The epoch API currently does not support sleeping in epoch_preempt sections.
|
2018-06-22 06:04:22 +00:00
|
|
|
A caller should never call
|
2018-05-13 23:16:04 +00:00
|
|
|
.Fn epoch_wait
|
2018-07-04 03:44:36 +00:00
|
|
|
in the middle of an epoch section for the same epoch as this will lead to a deadlock.
|
2018-05-13 23:16:04 +00:00
|
|
|
.Pp
|
2019-06-28 10:38:56 +00:00
|
|
|
The
|
|
|
|
.Fn epoch_drain_callbacks
|
|
|
|
function is used to drain all pending callbacks which have been invoked by prior
|
|
|
|
.Fn epoch_call
|
|
|
|
function calls on the same epoch.
|
|
|
|
This function is useful when there are shared memory structure(s)
|
|
|
|
referred to by the epoch callback(s) which are not refcounted and are
|
|
|
|
rarely freed.
|
|
|
|
The typical place for calling this function is right before freeing or
|
|
|
|
invalidating the shared resource(s) used by the epoch callback(s).
|
|
|
|
This function can sleep and is not optimized for performance.
|
2018-05-13 23:16:04 +00:00
|
|
|
.Sh RETURN VALUES
|
2018-07-04 03:54:39 +00:00
|
|
|
.Fn in_epoch curepoch
|
|
|
|
will return 1 if curthread is in curepoch, 0 otherwise.
|
2018-05-18 17:29:43 +00:00
|
|
|
.Sh CAVEATS
|
|
|
|
One must be cautious when using
|
2019-12-28 01:35:32 +00:00
|
|
|
.Fn epoch_wait_preempt .
|
|
|
|
Threads are pinned during epoch sections, so if a thread in a section is then
|
|
|
|
preempted by a higher priority compute bound thread on that CPU, it can be
|
|
|
|
prevented from leaving the section indefinitely.
|
|
|
|
.Pp
|
|
|
|
Epochs are not a straight replacement for read locks.
|
|
|
|
Callers must use safe list and tailq traversal routines in an epoch (see ck_queue).
|
|
|
|
When modifying a list referenced from an epoch section safe removal
|
|
|
|
routines must be used and the caller can no longer modify a list entry
|
|
|
|
in place.
|
|
|
|
An item to be modified must be handled with copy on write
|
|
|
|
and frees must be deferred until after a grace period has elapsed.
|
2018-05-13 23:16:04 +00:00
|
|
|
.Sh EXAMPLES
|
|
|
|
Async free example:
|
|
|
|
Thread 1:
|
|
|
|
.Bd -literal
|
2018-05-18 04:13:58 +00:00
|
|
|
int
|
|
|
|
in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_laddr *laddr,
|
|
|
|
struct ucred *cred)
|
2018-05-13 23:16:04 +00:00
|
|
|
{
|
2019-12-28 01:35:32 +00:00
|
|
|
/* ... */
|
|
|
|
epoch_enter(net_epoch);
|
2018-05-13 23:16:04 +00:00
|
|
|
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
|
|
|
|
sa = ifa->ifa_addr;
|
|
|
|
if (sa->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
sin = (struct sockaddr_in *)sa;
|
|
|
|
if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
|
|
|
|
ia = (struct in_ifaddr *)ifa;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
epoch_exit(net_epoch);
|
2019-12-28 01:35:32 +00:00
|
|
|
/* ... */
|
2018-05-13 23:16:04 +00:00
|
|
|
}
|
|
|
|
.Ed
|
|
|
|
Thread 2:
|
|
|
|
.Bd -literal
|
|
|
|
void
|
|
|
|
ifa_free(struct ifaddr *ifa)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (refcount_release(&ifa->ifa_refcnt))
|
2020-01-22 02:28:39 +00:00
|
|
|
epoch_call(net_epoch, ifa_destroy, &ifa->ifa_epoch_ctx);
|
2018-05-13 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 04:13:58 +00:00
|
|
|
void
|
|
|
|
if_purgeaddrs(struct ifnet *ifp)
|
2018-05-13 23:16:04 +00:00
|
|
|
{
|
|
|
|
|
2018-06-22 06:04:22 +00:00
|
|
|
/* .... *
|
2018-05-13 23:16:04 +00:00
|
|
|
IF_ADDR_WLOCK(ifp);
|
|
|
|
CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
|
|
|
|
IF_ADDR_WUNLOCK(ifp);
|
|
|
|
ifa_free(ifa);
|
|
|
|
}
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2018-06-22 06:04:22 +00:00
|
|
|
Thread 1 traverses the ifaddr list in an epoch.
|
|
|
|
Thread 2 unlinks with the corresponding epoch safe macro, marks as logically free,
|
|
|
|
and then defers deletion.
|
|
|
|
More general mutation or a synchronous
|
|
|
|
free would have to follow a call to
|
2018-05-13 23:16:04 +00:00
|
|
|
.Fn epoch_wait .
|
2018-10-29 22:10:52 +00:00
|
|
|
.Sh NOTES
|
|
|
|
The
|
|
|
|
.Nm
|
|
|
|
kernel programming interface is under development and is subject to change.
|
2018-05-13 23:16:04 +00:00
|
|
|
.El
|
2020-04-30 11:17:29 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Nm
|
2020-05-01 11:36:39 +00:00
|
|
|
framework first appeared in
|
2020-04-30 11:17:29 +00:00
|
|
|
.Fx 11.0 .
|
2018-05-13 23:16:04 +00:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr locking 9 ,
|
|
|
|
.Xr mtx_pool 9 ,
|
|
|
|
.Xr mutex 9 ,
|
|
|
|
.Xr rwlock 9 ,
|
|
|
|
.Xr sema 9 ,
|
|
|
|
.Xr sleep 9 ,
|
|
|
|
.Xr sx 9 ,
|
|
|
|
.Xr timeout 9
|