Author: hselasky
Date: Wed Aug 19 13:23:52 2020
New Revision: 364388
URL: https://svnweb.freebsd.org/changeset/base/364388

Log:
  MFC r364072, r364073 and r364102:
  Use proper prototype for SYSINIT() functions.
  Mark the unused argument using the __unused macro.
  
  Make sure the multicast release tasks are properly drained when
  destroying a VNET or a network interface.
  
  Else the inm release tasks, both IPv4 and IPv6 may cause a panic
  accessing a freed VNET or network interface.
  
  Use a static initializer for the multicast free tasks.
  This makes the SYSINIT() function updated in r364072 superfluous.
  
  Differential Revision:        https://reviews.freebsd.org/D24914
  Sponsored by: Mellanox Technologies

Modified:
  stable/12/sys/netinet/in.c
  stable/12/sys/netinet/in_mcast.c
  stable/12/sys/netinet/in_var.h
  stable/12/sys/netinet6/in6_ifattach.c
  stable/12/sys/netinet6/in6_mcast.c
  stable/12/sys/netinet6/in6_var.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/netinet/in.c
==============================================================================
--- stable/12/sys/netinet/in.c  Wed Aug 19 13:09:31 2020        (r364387)
+++ stable/12/sys/netinet/in.c  Wed Aug 19 13:23:52 2020        (r364388)
@@ -998,6 +998,13 @@ in_ifdetach(struct ifnet *ifp)
        in_pcbpurgeif0(&V_ulitecbinfo, ifp);
        in_purgemaddrs(ifp);
        IN_MULTI_UNLOCK();
+
+       /*
+        * Make sure all multicast deletions invoking if_ioctl() are
+        * completed before returning. Else we risk accessing a freed
+        * ifnet structure pointer.
+        */
+       inm_release_wait(NULL);
 }
 
 /*

Modified: stable/12/sys/netinet/in_mcast.c
==============================================================================
--- stable/12/sys/netinet/in_mcast.c    Wed Aug 19 13:09:31 2020        
(r364387)
+++ stable/12/sys/netinet/in_mcast.c    Wed Aug 19 13:23:52 2020        
(r364388)
@@ -222,16 +222,28 @@ inm_is_ifp_detached(const struct in_multi *inm)
 }
 #endif
 
-static struct task free_task;
+/*
+ * Interface detach can happen in a taskqueue thread context, so we must use a
+ * dedicated thread to avoid deadlocks when draining inm_release tasks.
+ */
+TASKQUEUE_DEFINE_THREAD(inm_free);
 static struct in_multi_head inm_free_list = SLIST_HEAD_INITIALIZER();
 static void inm_release_task(void *arg __unused, int pending __unused);
+static struct task inm_free_task = TASK_INITIALIZER(0, inm_release_task, NULL);
 
-static void
-inm_init(void)
+void
+inm_release_wait(void *arg __unused)
 {
-       TASK_INIT(&free_task, 0, inm_release_task, NULL);
+
+       /*
+        * Make sure all pending multicast addresses are freed before
+        * the VNET or network device is destroyed:
+        */
+       taskqueue_drain(taskqueue_inm_free, &inm_free_task);
 }
-SYSINIT(inm_init, SI_SUB_TASKQ, SI_ORDER_ANY, inm_init, NULL);
+#ifdef VIMAGE
+VNET_SYSUNINIT(inm_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 
inm_release_wait, NULL);
+#endif
 
 void
 inm_release_list_deferred(struct in_multi_head *inmh)
@@ -242,7 +254,7 @@ inm_release_list_deferred(struct in_multi_head *inmh)
        mtx_lock(&in_multi_free_mtx);
        SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
        mtx_unlock(&in_multi_free_mtx);
-       taskqueue_enqueue(taskqueue_thread, &free_task);
+       taskqueue_enqueue(taskqueue_inm_free, &inm_free_task);
 }
 
 void

Modified: stable/12/sys/netinet/in_var.h
==============================================================================
--- stable/12/sys/netinet/in_var.h      Wed Aug 19 13:09:31 2020        
(r364387)
+++ stable/12/sys/netinet/in_var.h      Wed Aug 19 13:23:52 2020        
(r364388)
@@ -449,6 +449,7 @@ void        inm_print(const struct in_multi *);
 int    inm_record_source(struct in_multi *inm, const in_addr_t);
 void   inm_release_deferred(struct in_multi *);
 void   inm_release_list_deferred(struct in_multi_head *);
+void   inm_release_wait(void *);
 struct in_multi *
 in_addmulti(struct in_addr *, struct ifnet *);
 int    in_joingroup(struct ifnet *, const struct in_addr *,

Modified: stable/12/sys/netinet6/in6_ifattach.c
==============================================================================
--- stable/12/sys/netinet6/in6_ifattach.c       Wed Aug 19 13:09:31 2020        
(r364387)
+++ stable/12/sys/netinet6/in6_ifattach.c       Wed Aug 19 13:23:52 2020        
(r364388)
@@ -878,7 +878,7 @@ in6_purgemaddrs(struct ifnet *ifp)
         * completed before returning. Else we risk accessing a freed
         * ifnet structure pointer.
         */
-       in6m_release_wait();
+       in6m_release_wait(NULL);
 }
 
 void

Modified: stable/12/sys/netinet6/in6_mcast.c
==============================================================================
--- stable/12/sys/netinet6/in6_mcast.c  Wed Aug 19 13:09:31 2020        
(r364387)
+++ stable/12/sys/netinet6/in6_mcast.c  Wed Aug 19 13:23:52 2020        
(r364388)
@@ -514,17 +514,10 @@ in6m_release(struct in6_multi *inm)
  * dedicated thread to avoid deadlocks when draining in6m_release tasks.
  */
 TASKQUEUE_DEFINE_THREAD(in6m_free);
-static struct task in6m_free_task;
 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
 static void in6m_release_task(void *arg __unused, int pending __unused);
+static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, 
NULL);
 
-static void
-in6m_init(void)
-{
-       TASK_INIT(&in6m_free_task, 0, in6m_release_task, NULL);
-}
-SYSINIT(in6m_init, SI_SUB_TASKQ, SI_ORDER_ANY, in6m_init, NULL);
-
 void
 in6m_release_list_deferred(struct in6_multi_head *inmh)
 {
@@ -537,10 +530,18 @@ in6m_release_list_deferred(struct in6_multi_head *inmh
 }
 
 void
-in6m_release_wait(void)
+in6m_release_wait(void *arg __unused)
 {
+
+       /*
+        * Make sure all pending multicast addresses are freed before
+        * the VNET or network device is destroyed:
+        */
        taskqueue_drain_all(taskqueue_in6m_free);
 }
+#ifdef VIMAGE
+VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 
in6m_release_wait, NULL);
+#endif
 
 void
 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)

Modified: stable/12/sys/netinet6/in6_var.h
==============================================================================
--- stable/12/sys/netinet6/in6_var.h    Wed Aug 19 13:09:31 2020        
(r364387)
+++ stable/12/sys/netinet6/in6_var.h    Wed Aug 19 13:23:52 2020        
(r364388)
@@ -868,7 +868,7 @@ void        in6m_commit(struct in6_multi *);
 void   in6m_print(const struct in6_multi *);
 int    in6m_record_source(struct in6_multi *, const struct in6_addr *);
 void   in6m_release_list_deferred(struct in6_multi_head *);
-void   in6m_release_wait(void);
+void   in6m_release_wait(void *);
 void   ip6_freemoptions(struct ip6_moptions *);
 int    ip6_getmoptions(struct inpcb *, struct sockopt *);
 int    ip6_setmoptions(struct inpcb *, struct sockopt *);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to