domains: merge domain_init() into domain_add()

domain_init() called at SI_SUB_PROTO_DOMAIN/SI_ORDER_SECOND is always
called right after domain_add(), that had been called at SI_ORDER_FIRST.
Note that protocols aren't initialized yet at this point, since they are
usually scheduled to initialize at SI_ORDER_THIRD.

After this merge it becomes clear that DOMF_SUPPORTED / DOMF_INITED
can be garbage collected as they are set & checked in the same function.

For initialization of the domain system itself it is now clear that
domaininit() can be garbage collected and static initializer is enough.
This commit is contained in:
Gleb Smirnoff 2022-08-29 19:15:01 -07:00
parent e18c5816ea
commit 244e1aeaec
2 changed files with 11 additions and 67 deletions

View File

@ -52,27 +52,8 @@ __FBSDID("$FreeBSD$");
#include <net/vnet.h>
/*
* System initialization
*
* Note: domain initialization takes place on a per domain basis
* as a result of traversing a SYSINIT linker set. Most likely,
* each domain would want to call DOMAIN_SET(9) itself, which
* would cause the domain to be added just after domaininit()
* is called during startup.
*
* See DOMAIN_SET(9) for details on its use.
*/
static void domaininit(void *);
SYSINIT(domain, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, domaininit, NULL);
static void domainfinalize(void *);
SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
NULL);
struct domainhead domains = SLIST_HEAD_INITIALIZER(&domains);
int domain_init_status = 0;
int domain_init_status = 1;
static struct mtx dom_mtx; /* domain list lock */
MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
@ -250,48 +231,26 @@ pr_init(struct domain *dom, struct protosw *pr)
* XXX can't fail at this time.
*/
void
domain_init(struct domain *dp)
domain_add(struct domain *dp)
{
struct protosw *pr;
int flags;
MPASS(IS_DEFAULT_VNET(curvnet));
flags = atomic_load_acq_int(&dp->dom_flags);
if ((flags & DOMF_SUPPORTED) == 0)
if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0)
return;
MPASS((flags & DOMF_INITED) == 0);
for (int i = 0; i < dp->dom_nprotosw; i++)
if ((pr = dp->dom_protosw[i]) != NULL)
pr_init(dp, pr);
atomic_set_rel_int(&dp->dom_flags, DOMF_INITED);
}
/*
* Add a new protocol domain to the list of supported domains
* Note: you cant unload it again because a socket may be using it.
* XXX can't fail at this time.
*/
void
domain_add(struct domain *dp)
{
if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0)
return;
atomic_set_rel_int(&dp->dom_flags, DOMF_SUPPORTED);
mtx_lock(&dom_mtx);
SLIST_INSERT_HEAD(&domains, dp, dom_next);
KASSERT(domain_init_status >= 1,
("attempt to domain_add(%s) before domaininit()",
dp->dom_name));
#ifndef INVARIANTS
if (domain_init_status < 1)
printf("WARNING: attempt to domain_add(%s) before "
"domaininit()\n", dp->dom_name);
#ifdef INVARIANTS
struct domain *tmp;
SLIST_FOREACH(tmp, &domains, dom_next)
MPASS(tmp->dom_family != dp->dom_family);
#endif
SLIST_INSERT_HEAD(&domains, dp, dom_next);
mtx_unlock(&dom_mtx);
}
@ -307,18 +266,6 @@ domain_remove(struct domain *dp)
mtx_unlock(&dom_mtx);
}
/* ARGSUSED*/
static void
domaininit(void *dummy)
{
mtx_lock(&dom_mtx);
KASSERT(domain_init_status == 0, ("domaininit called too late!"));
domain_init_status = 1;
mtx_unlock(&dom_mtx);
}
/* ARGSUSED*/
static void
domainfinalize(void *dummy)
{
@ -328,6 +275,8 @@ domainfinalize(void *dummy)
domain_init_status = 2;
mtx_unlock(&dom_mtx);
}
SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
NULL);
struct domain *
pffinddomain(int family)

View File

@ -71,8 +71,6 @@ struct domain {
};
/* dom_flags */
#define DOMF_SUPPORTED 0x0001 /* System supports this domain. */
#define DOMF_INITED 0x0002 /* Initialized in the default vnet. */
#define DOMF_UNLOADABLE 0x0004 /* Can be unloaded */
#ifdef _KERNEL
@ -80,7 +78,6 @@ extern int domain_init_status;
extern SLIST_HEAD(domainhead, domain) domains;
void domain_add(struct domain *);
void domain_remove(struct domain *);
void domain_init(struct domain *);
#ifdef VIMAGE
void vnet_domain_init(void *);
void vnet_domain_uninit(void *);
@ -90,9 +87,7 @@ void vnet_domain_uninit(void *);
SYSINIT(domain_add_ ## name, SI_SUB_PROTO_DOMAIN, \
SI_ORDER_FIRST, domain_add, & name ## domain); \
SYSUNINIT(domain_remove_ ## name, SI_SUB_PROTO_DOMAIN, \
SI_ORDER_FIRST, domain_remove, & name ## domain); \
SYSINIT(domain_init_ ## name, SI_SUB_PROTO_DOMAIN, \
SI_ORDER_SECOND, domain_init, & name ## domain);
SI_ORDER_FIRST, domain_remove, & name ## domain);
#endif /* _KERNEL */
#endif /* !_SYS_DOMAIN_H_ */