Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Don't hold global locks if NET_MPSAFE is enabled



details:   https://anonhg.NetBSD.org/src/rev/9f09adc187dc
branches:  trunk
changeset: 348420:9f09adc187dc
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Tue Oct 18 07:30:30 2016 +0000

description:
Don't hold global locks if NET_MPSAFE is enabled

If NET_MPSAFE is enabled, don't hold KERNEL_LOCK and softnet_lock in
part of the network stack such as IP forwarding paths. The aim of the
change is to make it easy to test the network stack without the locks
and reduce our local diffs.

By default (i.e., if NET_MPSAFE isn't enabled), the locks are held
as they used to be.

Reviewed by knakahara@

diffstat:

 sys/net/if.c             |   8 +++---
 sys/net/if_ethersubr.c   |  14 +++++++++++-
 sys/net/route.c          |   9 ++++---
 sys/netinet/if_arp.c     |  15 ++++++++++++-
 sys/netinet/in.c         |   9 ++++++-
 sys/netinet/ip_flow.c    |  46 +++++++++++++++++++++++++++--------------
 sys/netinet/ip_input.c   |  25 +++++++++++------------
 sys/netinet6/in6.c       |   9 ++++++-
 sys/netinet6/ip6_flow.c  |  45 +++++++++++++++++++++++++++-------------
 sys/netinet6/ip6_input.c |  52 ++++++++++++++++++++++++++++++++++++++---------
 sys/netinet6/nd6.c       |  16 ++++++++++++-
 sys/netinet6/nd6_nbr.c   |  13 ++++++++++-
 12 files changed, 187 insertions(+), 74 deletions(-)

diffs (truncated from 892 to 300 lines):

diff -r 56189cd14892 -r 9f09adc187dc sys/net/if.c
--- a/sys/net/if.c      Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/net/if.c      Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if.c,v 1.358 2016/10/03 11:06:06 ozaki-r Exp $ */
+/*     $NetBSD: if.c,v 1.359 2016/10/18 07:30:30 ozaki-r Exp $ */
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.358 2016/10/03 11:06:06 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.359 2016/10/18 07:30:30 ozaki-r Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_inet.h"
@@ -1622,12 +1622,12 @@
        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
        IFADDR_WRITER_REMOVE(ifa);
        IFADDR_ENTRY_DESTROY(ifa);
-#if notyet
+#ifdef NET_MPSAFE
        pserialize_perform(ifnet_psz);
 #endif
        IFNET_UNLOCK();
 
-#if notyet
+#ifdef NET_MPSAFE
        psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
 #endif
        ifafree(ifa);
diff -r 56189cd14892 -r 9f09adc187dc sys/net/if_ethersubr.c
--- a/sys/net/if_ethersubr.c    Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/net/if_ethersubr.c    Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_ethersubr.c,v 1.228 2016/10/03 11:06:06 ozaki-r Exp $       */
+/*     $NetBSD: if_ethersubr.c,v 1.229 2016/10/18 07:30:30 ozaki-r Exp $       */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.228 2016/10/03 11:06:06 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.229 2016/10/18 07:30:30 ozaki-r Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -246,17 +246,23 @@
 
 #ifdef INET
        case AF_INET:
+#ifndef NET_MPSAFE
                KERNEL_LOCK(1, NULL);
+#endif
                if (m->m_flags & M_BCAST)
                        (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
                else if (m->m_flags & M_MCAST)
                        ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
                else if ((error = arpresolve(ifp, rt, m, dst, edst,
                    sizeof(edst))) != 0) {
+#ifndef NET_MPSAFE
                        KERNEL_UNLOCK_ONE(NULL);
+#endif
                        return error == EWOULDBLOCK ? 0 : error;
                }
+#ifndef NET_MPSAFE
                KERNEL_UNLOCK_ONE(NULL);
+#endif
                /* If broadcasting on a simplex interface, loopback a copy */
                if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
                        mcopy = m_copy(m, 0, (int)M_COPYALL);
@@ -884,7 +890,11 @@
        }
 
        if (__predict_true(pktq)) {
+#ifdef NET_MPSAFE
+               const u_int h = curcpu()->ci_index;
+#else
                const uint32_t h = pktq_rps_hash(m);
+#endif
                if (__predict_false(!pktq_enqueue(pktq, m, h))) {
                        m_freem(m);
                }
diff -r 56189cd14892 -r 9f09adc187dc sys/net/route.c
--- a/sys/net/route.c   Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/net/route.c   Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: route.c,v 1.174 2016/08/05 00:52:02 ozaki-r Exp $      */
+/*     $NetBSD: route.c,v 1.175 2016/10/18 07:30:30 ozaki-r Exp $      */
 
 /*-
  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
@@ -93,10 +93,11 @@
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
 #include "opt_route.h"
+#include "opt_net_mpsafe.h"
 #endif
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: route.c,v 1.174 2016/08/05 00:52:02 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: route.c,v 1.175 2016/10/18 07:30:30 ozaki-r Exp $");
 
 #include <sys/param.h>
 #ifdef RTFLUSH_DEBUG
@@ -431,7 +432,7 @@
        return NULL;
 }
 
-#ifdef DEBUG
+#if defined(DEBUG) && !defined(NET_MPSAFE)
 /*
  * Check the following constraint for each rtcache:
  *   if a rtcache holds a rtentry, the rtentry's refcnt is more than zero,
@@ -460,7 +461,7 @@
        KASSERT(rt->rt_refcnt > 0);
 
        rt->rt_refcnt--;
-#ifdef DEBUG
+#if defined(DEBUG) && !defined(NET_MPSAFE)
        if (rt_getkey(rt) != NULL)
                rtcache_check_rtrefcnt(rt_getkey(rt)->sa_family);
 #endif
diff -r 56189cd14892 -r 9f09adc187dc sys/netinet/if_arp.c
--- a/sys/netinet/if_arp.c      Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/netinet/if_arp.c      Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_arp.c,v 1.230 2016/10/11 13:59:30 roy Exp $ */
+/*     $NetBSD: if_arp.c,v 1.231 2016/10/18 07:30:31 ozaki-r Exp $     */
 
 /*-
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -68,11 +68,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.230 2016/10/11 13:59:30 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.231 2016/10/18 07:30:31 ozaki-r Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
 #include "opt_inet.h"
+#include "opt_net_mpsafe.h"
 #endif
 
 #ifdef INET
@@ -918,8 +919,10 @@
        int s;
        int arplen;
 
+#ifndef NET_MPSAFE
        mutex_enter(softnet_lock);
        KERNEL_LOCK(1, NULL);
+#endif
        for (;;) {
                struct ifnet *rcvif;
 
@@ -971,8 +974,12 @@
                m_freem(m);
        }
 out:
+#ifndef NET_MPSAFE
        KERNEL_UNLOCK_ONE(NULL);
        mutex_exit(softnet_lock);
+#else
+       return; /* XXX gcc */
+#endif
 }
 
 /*
@@ -1532,7 +1539,11 @@
 arp_dad_stoptimer(struct dadq *dp)
 {
 
+#ifdef NET_MPSAFE
+       callout_halt(&dp->dad_timer_ch, NULL);
+#else
        callout_halt(&dp->dad_timer_ch, softnet_lock);
+#endif
 }
 
 static void
diff -r 56189cd14892 -r 9f09adc187dc sys/netinet/in.c
--- a/sys/netinet/in.c  Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/netinet/in.c  Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: in.c,v 1.186 2016/10/01 17:17:20 roy Exp $     */
+/*     $NetBSD: in.c,v 1.187 2016/10/18 07:30:31 ozaki-r Exp $ */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: in.c,v 1.186 2016/10/01 17:17:20 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: in.c,v 1.187 2016/10/18 07:30:31 ozaki-r Exp $");
 
 #include "arp.h"
 
@@ -99,6 +99,7 @@
 #include "opt_inet.h"
 #include "opt_inet_conf.h"
 #include "opt_mrouting.h"
+#include "opt_net_mpsafe.h"
 #endif
 
 #include <sys/param.h>
@@ -734,9 +735,13 @@
 {
        int error;
 
+#ifndef NET_MPSAFE
        mutex_enter(softnet_lock);
+#endif
        error = in_control0(so, cmd, data, ifp);
+#ifndef NET_MPSAFE
        mutex_exit(softnet_lock);
+#endif
 
        return error;
 }
diff -r 56189cd14892 -r 9f09adc187dc sys/netinet/ip_flow.c
--- a/sys/netinet/ip_flow.c     Tue Oct 18 06:40:06 2016 +0000
+++ b/sys/netinet/ip_flow.c     Tue Oct 18 07:30:30 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_flow.c,v 1.76 2016/08/01 10:22:53 knakahara Exp $   */
+/*     $NetBSD: ip_flow.c,v 1.77 2016/10/18 07:30:31 ozaki-r Exp $     */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,11 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.76 2016/08/01 10:22:53 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.77 2016/10/18 07:30:31 ozaki-r Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_net_mpsafe.h"
+#endif
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -471,9 +475,11 @@
        /* We can allow enqueuing another work at this point */
        atomic_swap_uint(&ipflow_work_enqueued, 0);
 
+#ifndef NET_MPSAFE
        mutex_enter(softnet_lock);
+       KERNEL_LOCK(1, NULL);
+#endif
        mutex_enter(&ipflow_lock);
-       KERNEL_LOCK(1, NULL);
        for (ipf = TAILQ_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
                next_ipf = TAILQ_NEXT(ipf, ipf_list);
                if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
@@ -490,9 +496,11 @@
                        ipf->ipf_uses = 0;
                }
        }
+       mutex_exit(&ipflow_lock);
+#ifndef NET_MPSAFE
        KERNEL_UNLOCK_ONE(NULL);
-       mutex_exit(&ipflow_lock);
        mutex_exit(softnet_lock);
+#endif
 }
 
 void
@@ -513,17 +521,16 @@
        struct ipflow *ipf;
        size_t hash;
 
+#ifndef NET_MPSAFE
+       KERNEL_LOCK(1, NULL);
+#endif
        mutex_enter(&ipflow_lock);
 
        /*
         * Don't create cache entries for ICMP messages.
         */
-       if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP) {
-               mutex_exit(&ipflow_lock);
-               return;
-       }
-
-       KERNEL_LOCK(1, NULL);
+       if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
+               goto out;
 
        /*
         * See if an existing flow struct exists.  If so remove it from its
@@ -566,8 +573,10 @@
        IPFLOW_INSERT(hash, ipf);
 



Home | Main Index | Thread Index | Old Index