git: e18c5816ea56 - main - domains: use queue(9) SLIST for linked list of domains

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

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

commit e18c5816ea56e64f3e792d0e02ef3c23f0f54047
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: use queue(9) SLIST for linked list of domains
---
 sys/kern/uipc_debug.c        |  2 +-
 sys/kern/uipc_domain.c       | 19 ++++---------------
 sys/net/if.c                 |  8 +++++---
 sys/net/route/route_tables.c |  6 +++---
 sys/sys/domain.h             |  5 +++--
 5 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/sys/kern/uipc_debug.c b/sys/kern/uipc_debug.c
index 3f54e3e46f26..62c8138c3bb3 100644
--- a/sys/kern/uipc_debug.c
+++ b/sys/kern/uipc_debug.c
@@ -248,7 +248,7 @@ db_print_domain(struct domain *d, const char *domain_name, int indent)
 
 	db_print_indent(indent);
 	db_printf("dom_protosw: %p   ", d->dom_protosw);
-	db_printf("dom_next: %p\n", d->dom_next);
+	db_printf("dom_next: %p\n", d->dom_next.sle_next);
 
 	db_print_indent(indent);
 	db_printf("dom_rtattach: %p   ", d->dom_rtattach);
diff --git a/sys/kern/uipc_domain.c b/sys/kern/uipc_domain.c
index 334a7a036daa..b12aefbd2a2d 100644
--- a/sys/kern/uipc_domain.c
+++ b/sys/kern/uipc_domain.c
@@ -71,7 +71,7 @@ static void domainfinalize(void *);
 SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
     NULL);
 
-struct domain *domains;		/* registered protocol domains */
+struct domainhead domains = SLIST_HEAD_INITIALIZER(&domains);
 int domain_init_status = 0;
 static struct mtx dom_mtx;		/* domain list lock */
 MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
@@ -282,8 +282,7 @@ domain_add(struct domain *dp)
 		return;
 	atomic_set_rel_int(&dp->dom_flags, DOMF_SUPPORTED);
 	mtx_lock(&dom_mtx);
-	dp->dom_next = domains;
-	domains = dp;
+	SLIST_INSERT_HEAD(&domains, dp, dom_next);
 
 	KASSERT(domain_init_status >= 1,
 	    ("attempt to domain_add(%s) before domaininit()",
@@ -304,17 +303,7 @@ domain_remove(struct domain *dp)
 		return;
 
 	mtx_lock(&dom_mtx);
-	if (domains == dp) {
-		domains = dp->dom_next;
-	} else {
-		struct domain *curr;
-		for (curr = domains; curr != NULL; curr = curr->dom_next) {
-			if (curr->dom_next == dp) {
-				curr->dom_next = dp->dom_next;
-				break;
-			}
-		}
-	}
+	SLIST_REMOVE(&domains, dp, domain, dom_next);
 	mtx_unlock(&dom_mtx);
 }
 
@@ -345,7 +334,7 @@ pffinddomain(int family)
 {
 	struct domain *dp;
 
-	for (dp = domains; dp != NULL; dp = dp->dom_next)
+	SLIST_FOREACH(dp, &domains, dom_next)
 		if (dp->dom_family == family)
 			return (dp);
 	return (NULL);
diff --git a/sys/net/if.c b/sys/net/if.c
index 886d3f7833c0..ffaf7d004511 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -999,7 +999,7 @@ if_attachdomain1(struct ifnet *ifp)
 
 	/* address family dependent data region */
 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
-	for (dp = domains; dp; dp = dp->dom_next) {
+	SLIST_FOREACH(dp, &domains, dom_next) {
 		if (dp->dom_ifattach)
 			ifp->if_afdata[dp->dom_family] =
 			    (*dp->dom_ifattach)(ifp);
@@ -1244,7 +1244,9 @@ finish_vnet_shutdown:
 	i = ifp->if_afdata_initialized;
 	ifp->if_afdata_initialized = 0;
 	IF_AFDATA_UNLOCK(ifp);
-	for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
+	if (i == 0)
+		return (0);
+	SLIST_FOREACH(dp, &domains, dom_next) {
 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
 			(*dp->dom_ifdetach)(ifp,
 			    ifp->if_afdata[dp->dom_family]);
@@ -4369,7 +4371,7 @@ if_getmtu_family(if_t ifp, int family)
 {
 	struct domain *dp;
 
-	for (dp = domains; dp; dp = dp->dom_next) {
+	SLIST_FOREACH(dp, &domains, dom_next) {
 		if (dp->dom_family == family && dp->dom_ifmtu != NULL)
 			return (dp->dom_ifmtu((struct ifnet *)ifp));
 	}
diff --git a/sys/net/route/route_tables.c b/sys/net/route/route_tables.c
index f5fe4b5d2a81..614c28ded0cf 100644
--- a/sys/net/route/route_tables.c
+++ b/sys/net/route/route_tables.c
@@ -216,7 +216,7 @@ grow_rtables(uint32_t num_tables)
 		    V_rt_numfibs * (AF_MAX + 1) * sizeof(void *));
 
 	/* Populate the remainders */
-	for (dom = domains; dom; dom = dom->dom_next) {
+	SLIST_FOREACH(dom, &domains, dom_next) {
 		if (dom->dom_rtattach == NULL)
 			continue;
 		family = dom->dom_family;
@@ -252,7 +252,7 @@ grow_rtables(uint32_t num_tables)
 
 #ifdef FIB_ALGO
 	/* Attach fib algo to the new rtables */
-	for (dom = domains; dom; dom = dom->dom_next) {
+	SLIST_FOREACH(dom, &domains, dom_next) {
 		if (dom->dom_rtattach != NULL)
 			fib_setup_family(dom->dom_family, num_tables);
 	}
@@ -296,7 +296,7 @@ rtables_destroy(const void *unused __unused)
 	int family;
 
 	RTABLES_LOCK();
-	for (dom = domains; dom; dom = dom->dom_next) {
+	SLIST_FOREACH(dom, &domains, dom_next) {
 		if (dom->dom_rtdetach == NULL)
 			continue;
 		family = dom->dom_family;
diff --git a/sys/sys/domain.h b/sys/sys/domain.h
index 2f3a698a369d..9b0e28137e18 100644
--- a/sys/sys/domain.h
+++ b/sys/sys/domain.h
@@ -34,6 +34,7 @@
 
 #ifndef _SYS_DOMAIN_H_
 #define _SYS_DOMAIN_H_
+#include <sys/queue.h>
 
 /*
  * Structure per communications domain.
@@ -48,6 +49,7 @@ struct	socket;
 struct	rib_head;
 
 struct domain {
+	SLIST_ENTRY(domain) dom_next;
 	int	dom_family;		/* AF_xxx */
 	u_int	dom_nprotosw;		/* length of dom_protosw[] */
 	char	*dom_name;
@@ -57,7 +59,6 @@ struct domain {
 		(struct mbuf *, struct mbuf **, int);
 	void	(*dom_dispose)		/* dispose of internalized rights */
 		(struct socket *);
-	struct	domain *dom_next;
 	struct rib_head *(*dom_rtattach)	/* initialize routing table */
 		(uint32_t);
 	void	(*dom_rtdetach)		/* clean up routing table */
@@ -76,7 +77,7 @@ struct domain {
 
 #ifdef _KERNEL
 extern int	domain_init_status;
-extern struct	domain *domains;
+extern SLIST_HEAD(domainhead, domain) domains;
 void		domain_add(struct domain *);
 void		domain_remove(struct domain *);
 void		domain_init(struct domain *);