git: 77223d98b6f8 - main - ip_mroute: refactor epoch-basd locking

From: Wojciech Macek <wma_at_FreeBSD.org>
Date: Wed, 02 Feb 2022 05:49:25 UTC
The branch main has been updated by wma:

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

commit 77223d98b6f84eea73191fd28d99d5b9ec0739f7
Author:     Wojciech Macek <wma@FreeBSD.org>
AuthorDate: 2022-01-25 09:10:44 +0000
Commit:     Wojciech Macek <wma@FreeBSD.org>
CommitDate: 2022-02-02 05:48:05 +0000

    ip_mroute: refactor epoch-basd locking
    
    Remove duplicated epoch_enter and epoch_exit in IP inp/outp routines.
    Remove unnecessary macros as well.
    
    Obtained from:          Semihalf
    Spponsored by:          Stormshield
    Reviewed by:            glebius
    Differential revision:  https://reviews.freebsd.org/D34030
---
 sys/netinet/ip_input.c  |  6 ------
 sys/netinet/ip_mroute.c | 26 ++++++++++----------------
 sys/netinet/ip_mroute.h |  9 +--------
 sys/netinet/ip_output.c |  5 -----
 sys/netinet/raw_ip.c    | 10 +++-------
 5 files changed, 14 insertions(+), 42 deletions(-)

diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
index 8fd26e4ca861..7c365b2e51b8 100644
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -82,7 +82,6 @@ __FBSDID("$FreeBSD$");
 #include <machine/in_cksum.h>
 #include <netinet/ip_carp.h>
 #include <netinet/in_rss.h>
-#include <netinet/ip_mroute.h>
 
 #include <netipsec/ipsec_support.h>
 
@@ -448,7 +447,6 @@ ip_direct_input(struct mbuf *m)
 void
 ip_input(struct mbuf *m)
 {
-	MROUTER_RLOCK_TRACKER;
 	struct ip *ip = NULL;
 	struct in_ifaddr *ia = NULL;
 	struct ifaddr *ifa;
@@ -744,7 +742,6 @@ passin:
 		ia = NULL;
 	}
 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
-		MROUTER_RLOCK();
 		/*
 		 * RFC 3927 2.7: Do not forward multicast packets from
 		 * IN_LINKLOCAL.
@@ -759,7 +756,6 @@ passin:
 			 * must be discarded, else it may be accepted below.
 			 */
 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
-				MROUTER_RUNLOCK();
 				IPSTAT_INC(ips_cantforward);
 				m_freem(m);
 				return;
@@ -771,12 +767,10 @@ passin:
 			 * host belongs to their destination groups.
 			 */
 			if (ip->ip_p == IPPROTO_IGMP) {
-				MROUTER_RUNLOCK();
 				goto ours;
 			}
 			IPSTAT_INC(ips_forward);
 		}
-		MROUTER_RUNLOCK();
 		/*
 		 * Assume the packet is for us, to avoid prematurely taking
 		 * a lock on the in_multi hash. Protocols must perform
diff --git a/sys/netinet/ip_mroute.c b/sys/netinet/ip_mroute.c
index fdca39b5dbb9..c678acd650c0 100644
--- a/sys/netinet/ip_mroute.c
+++ b/sys/netinet/ip_mroute.c
@@ -300,7 +300,7 @@ VNET_DEFINE_STATIC(struct ifnet *, multicast_register_if);
 static u_long	X_ip_mcast_src(int);
 static int	X_ip_mforward(struct ip *, struct ifnet *, struct mbuf *,
 		    struct ip_moptions *);
-static int	X_ip_mrouter_done(void *);
+static int	X_ip_mrouter_done(void);
 static int	X_ip_mrouter_get(struct socket *, struct sockopt *);
 static int	X_ip_mrouter_set(struct socket *, struct sockopt *);
 static int	X_legal_vif_num(int);
@@ -431,7 +431,7 @@ X_ip_mrouter_set(struct socket *so, struct sockopt *sopt)
 	break;
 
     case MRT_DONE:
-	error = ip_mrouter_done(NULL);
+	error = ip_mrouter_done();
 	break;
 
     case MRT_ADD_VIF:
@@ -734,20 +734,15 @@ ip_mrouter_init(struct socket *so, int version)
  * Disable multicast forwarding.
  */
 static int
-X_ip_mrouter_done(void *locked)
+X_ip_mrouter_done(void)
 {
     struct ifnet *ifp;
     u_long i;
     vifi_t vifi;
     struct bw_upcall *bu;
 
-    if (V_ip_mrouter == NULL) {
-	if (locked) {
-		struct epoch_tracker *mrouter_et = locked;
-		MROUTER_RUNLOCK_PARAM(mrouter_et);
-	}
-	return EINVAL;
-    }
+    if (V_ip_mrouter == NULL)
+	return (EINVAL);
 
     /*
      * Detach/disable hooks to the reset of the system.
@@ -756,12 +751,11 @@ X_ip_mrouter_done(void *locked)
     atomic_subtract_int(&ip_mrouter_cnt, 1);
     V_mrt_api_config = 0;
 
-    if (locked) {
-	struct epoch_tracker *mrouter_et = locked;
-	MROUTER_RUNLOCK_PARAM(mrouter_et);
-    }
-
-    MROUTER_WAIT();
+    /*
+     * Wait for all epoch sections to complete to ensure
+     * V_ip_mrouter = NULL is visible to others.
+     */
+    epoch_wait_preempt(net_epoch_preempt);
 
     /* Stop and drain task queue */
     taskqueue_block(V_task_queue);
diff --git a/sys/netinet/ip_mroute.h b/sys/netinet/ip_mroute.h
index 016d026d184c..5ad25b485dfc 100644
--- a/sys/netinet/ip_mroute.h
+++ b/sys/netinet/ip_mroute.h
@@ -363,16 +363,9 @@ struct sockopt;
 
 extern int	(*ip_mrouter_set)(struct socket *, struct sockopt *);
 extern int	(*ip_mrouter_get)(struct socket *, struct sockopt *);
-extern int	(*ip_mrouter_done)(void *);
+extern int	(*ip_mrouter_done)(void);
 extern int	(*mrt_ioctl)(u_long, caddr_t, int);
 
-#define	MROUTER_RLOCK_TRACKER	struct epoch_tracker mrouter_et
-#define	MROUTER_RLOCK_PARAM_PTR		&mrouter_et
-#define	MROUTER_RLOCK()	epoch_enter_preempt(net_epoch_preempt, &mrouter_et)
-#define	MROUTER_RUNLOCK()	epoch_exit_preempt(net_epoch_preempt, &mrouter_et)
-#define	MROUTER_RUNLOCK_PARAM(param)	epoch_exit_preempt(net_epoch_preempt, param)
-#define	MROUTER_WAIT()	epoch_wait_preempt(net_epoch_preempt)
-
 #endif /* _KERNEL */
 
 #endif /* _NETINET_IP_MROUTE_H_ */
diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c
index f203bc165e61..818d115d8a2d 100644
--- a/sys/netinet/ip_output.c
+++ b/sys/netinet/ip_output.c
@@ -82,7 +82,6 @@ __FBSDID("$FreeBSD$");
 #include <netinet/in_var.h>
 #include <netinet/ip_var.h>
 #include <netinet/ip_options.h>
-#include <netinet/ip_mroute.h>
 
 #include <netinet/udp.h>
 #include <netinet/udp_var.h>
@@ -321,7 +320,6 @@ int
 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
     struct ip_moptions *imo, struct inpcb *inp)
 {
-	MROUTER_RLOCK_TRACKER;
 	struct ip *ip;
 	struct ifnet *ifp = NULL;	/* keep compiler happy */
 	struct mbuf *m0;
@@ -609,7 +607,6 @@ again:
 			 * above, will be forwarded by the ip_input() routine,
 			 * if necessary.
 			 */
-			MROUTER_RLOCK();
 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
 				/*
 				 * If rsvp daemon is not running, do not
@@ -621,12 +618,10 @@ again:
 					imo = NULL;
 				if (ip_mforward &&
 				    ip_mforward(ip, ifp, m, imo) != 0) {
-					MROUTER_RUNLOCK();
 					m_freem(m);
 					goto done;
 				}
 			}
-			MROUTER_RUNLOCK();
 		}
 
 		/*
diff --git a/sys/netinet/raw_ip.c b/sys/netinet/raw_ip.c
index 229716918875..fb6718dfd28e 100644
--- a/sys/netinet/raw_ip.c
+++ b/sys/netinet/raw_ip.c
@@ -119,7 +119,7 @@ VNET_DEFINE(struct socket *, ip_mrouter);
  */
 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
-int (*ip_mrouter_done)(void *locked);
+int (*ip_mrouter_done)(void);
 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
 		   struct ip_moptions *);
 int (*mrt_ioctl)(u_long, caddr_t, int);
@@ -879,19 +879,15 @@ static void
 rip_detach(struct socket *so)
 {
 	struct inpcb *inp;
-	MROUTER_RLOCK_TRACKER;
 
 	inp = sotoinpcb(so);
 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
 	    ("rip_detach: not closed"));
 
-	/* Disable mrouter first, lock released inside ip_mrouter_done */
-	MROUTER_RLOCK();
+	/* Disable mrouter first */
 	if (so == V_ip_mrouter && ip_mrouter_done)
-		ip_mrouter_done(MROUTER_RLOCK_PARAM_PTR);
-	else
-		MROUTER_RUNLOCK();
+		ip_mrouter_done();
 
 	INP_WLOCK(inp);
 	INP_HASH_WLOCK(&V_ripcbinfo);