git: 9b967bd65de6 - main - domains: allow domains to be unloaded

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Sun, 14 Aug 2022 09:22:40 UTC
The branch main has been updated by melifaro:

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

commit 9b967bd65de6647aed68a141dc34f9b223a2593c
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2022-08-12 13:36:53 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2022-08-14 09:22:33 +0000

    domains: allow domains to be unloaded
    
    Add domain_remove() SYSUNINT callback that removes the domain
     from the domain list if it has DOMF_UNLOADABLE flag set.
    This change is required to support netlink ( D36002 ).
    
    Reviewed by:    glebius
    Differential Revision: https://reviews.freebsd.org/D36173
---
 sys/kern/uipc_domain.c | 23 +++++++++++++++++++++++
 sys/sys/domain.h       |  4 ++++
 2 files changed, 27 insertions(+)

diff --git a/sys/kern/uipc_domain.c b/sys/kern/uipc_domain.c
index c70b3150cbf0..2cae08be089a 100644
--- a/sys/kern/uipc_domain.c
+++ b/sys/kern/uipc_domain.c
@@ -239,6 +239,29 @@ domain_add(void *data)
 	mtx_unlock(&dom_mtx);
 }
 
+void
+domain_remove(void *data)
+{
+	struct domain *dp = (struct domain *)data;
+
+	if ((dp->dom_flags & DOMF_UNLOADABLE) == 0)
+		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;
+			}
+		}
+	}
+	mtx_unlock(&dom_mtx);
+}
+
 /* ARGSUSED*/
 static void
 domaininit(void *dummy)
diff --git a/sys/sys/domain.h b/sys/sys/domain.h
index 409372e91915..3a1c30e163ed 100644
--- a/sys/sys/domain.h
+++ b/sys/sys/domain.h
@@ -71,11 +71,13 @@ 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
 extern int	domain_init_status;
 extern struct	domain *domains;
 void		domain_add(void *);
+void		domain_remove(void *);
 void		domain_init(void *);
 #ifdef VIMAGE
 void		vnet_domain_init(void *);
@@ -85,6 +87,8 @@ void		vnet_domain_uninit(void *);
 #define	DOMAIN_SET(name)						\
 	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);
 #endif /* _KERNEL */