Author: hselasky
Date: Mon Aug 10 10:46:08 2020
New Revision: 364073
URL: https://svnweb.freebsd.org/changeset/base/364073

Log:
  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.
  
  Reviewed by:          jmg@
  Discussed with:               bz@
  Differential Revision:        https://reviews.freebsd.org/D24914
  MFC after:            1 week
  Sponsored by:         Mellanox Technologies

Modified:
  head/sys/netinet/in.c
  head/sys/netinet/in_mcast.c
  head/sys/netinet/in_var.h
  head/sys/netinet6/in6_ifattach.c
  head/sys/netinet6/in6_mcast.c
  head/sys/netinet6/in6_var.h

Modified: head/sys/netinet/in.c
==============================================================================
--- head/sys/netinet/in.c       Mon Aug 10 10:40:19 2020        (r364072)
+++ head/sys/netinet/in.c       Mon Aug 10 10:46:08 2020        (r364073)
@@ -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: head/sys/netinet/in_mcast.c
==============================================================================
--- head/sys/netinet/in_mcast.c Mon Aug 10 10:40:19 2020        (r364072)
+++ head/sys/netinet/in_mcast.c Mon Aug 10 10:46:08 2020        (r364073)
@@ -224,18 +224,37 @@ 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 task inm_free_task;
 static struct in_multi_head inm_free_list = SLIST_HEAD_INITIALIZER();
 static void inm_release_task(void *arg __unused, int pending __unused);
 
 static void
 inm_init(void *arg __unused)
 {
-       TASK_INIT(&free_task, 0, inm_release_task, NULL);
+       TASK_INIT(&inm_free_task, 0, inm_release_task, NULL);
 }
 SYSINIT(inm_init, SI_SUB_TASKQ, SI_ORDER_ANY, inm_init, NULL);
 
 void
+inm_release_wait(void *arg __unused)
+{
+
+       /*
+        * Make sure all pending multicast addresses are freed before
+        * the VNET or network device is destroyed:
+        */
+       taskqueue_drain(taskqueue_inm_free, &inm_free_task);
+}
+#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)
 {
 
@@ -244,7 +263,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: head/sys/netinet/in_var.h
==============================================================================
--- head/sys/netinet/in_var.h   Mon Aug 10 10:40:19 2020        (r364072)
+++ head/sys/netinet/in_var.h   Mon Aug 10 10:46:08 2020        (r364073)
@@ -450,6 +450,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: head/sys/netinet6/in6_ifattach.c
==============================================================================
--- head/sys/netinet6/in6_ifattach.c    Mon Aug 10 10:40:19 2020        
(r364072)
+++ head/sys/netinet6/in6_ifattach.c    Mon Aug 10 10:46:08 2020        
(r364073)
@@ -871,7 +871,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: head/sys/netinet6/in6_mcast.c
==============================================================================
--- head/sys/netinet6/in6_mcast.c       Mon Aug 10 10:40:19 2020        
(r364072)
+++ head/sys/netinet6/in6_mcast.c       Mon Aug 10 10:46:08 2020        
(r364073)
@@ -539,10 +539,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: head/sys/netinet6/in6_var.h
==============================================================================
--- head/sys/netinet6/in6_var.h Mon Aug 10 10:40:19 2020        (r364072)
+++ head/sys/netinet6/in6_var.h Mon Aug 10 10:46:08 2020        (r364073)
@@ -870,7 +870,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-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to