git: 244e1aeaec9a - main - domains: merge domain_init() into domain_add()

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 30 Aug 2022 02:24:55 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=244e1aeaec9a6f34e27377a35bdf39ddd4c72191

commit 244e1aeaec9a6f34e27377a35bdf39ddd4c72191
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-08-30 02:15:01 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-08-30 02:15:01 +0000

    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.
---
 sys/kern/uipc_domain.c | 71 +++++++-------------------------------------------
 sys/sys/domain.h       |  7 +----
 2 files changed, 11 insertions(+), 67 deletions(-)

diff --git a/sys/kern/uipc_domain.c b/sys/kern/uipc_domain.c
index b12aefbd2a2d..5fb602226bae 100644
--- a/sys/kern/uipc_domain.c
+++ b/sys/kern/uipc_domain.c
@@ -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)
diff --git a/sys/sys/domain.h b/sys/sys/domain.h
index 9b0e28137e18..4570f25201fa 100644
--- a/sys/sys/domain.h
+++ b/sys/sys/domain.h
@@ -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_ */