Source-Changes-HG archive

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

[src/trunk]: src/sys/net Replace tun_lock with mutex(9). XXX: too far from b...



details:   https://anonhg.NetBSD.org/src/rev/a3072d41afe7
branches:  trunk
changeset: 773092:a3072d41afe7
user:      rmind <rmind%NetBSD.org@localhost>
date:      Sat Jan 28 01:02:27 2012 +0000

description:
Replace tun_lock with mutex(9).  XXX: too far from being MP-safe yet.

diffstat:

 sys/net/if_tun.c |  81 +++++++++++++++++++++++++++++--------------------------
 sys/net/if_tun.h |   4 +-
 2 files changed, 45 insertions(+), 40 deletions(-)

diffs (truncated from 335 to 300 lines):

diff -r 9cf4336efe56 -r a3072d41afe7 sys/net/if_tun.c
--- a/sys/net/if_tun.c  Sat Jan 28 00:18:30 2012 +0000
+++ b/sys/net/if_tun.c  Sat Jan 28 01:02:27 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_tun.c,v 1.114 2011/10/28 22:08:14 dyoung Exp $      */
+/*     $NetBSD: if_tun.c,v 1.115 2012/01/28 01:02:27 rmind Exp $       */
 
 /*
  * Copyright (c) 1988, Julian Onions <jpo%cs.nott.ac.uk@localhost>
@@ -15,7 +15,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.114 2011/10/28 22:08:14 dyoung Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.115 2012/01/28 01:02:27 rmind Exp $");
 
 #include "opt_inet.h"
 
@@ -36,6 +36,7 @@
 #include <sys/conf.h>
 #include <sys/kauth.h>
 #include <sys/simplelock.h>
+#include <sys/mutex.h>
 #include <sys/cpu.h>
 
 #include <net/if.h>
@@ -126,7 +127,7 @@
                if (unit == tp->tun_unit)
                        break;
        if (tp)
-               simple_lock(&tp->tun_lock);
+               mutex_enter(&tp->tun_lock);
        simple_unlock(&tun_softc_lock);
 
        return (tp);
@@ -167,7 +168,7 @@
                tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
 
                tp->tun_unit = unit;
-               simple_lock_init(&tp->tun_lock);
+               mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET);
                selinit(&tp->tun_rsel);
                selinit(&tp->tun_wsel);
        } else {
@@ -224,9 +225,12 @@
        struct tun_softc *tp = (void *)ifp;
        int s, zombie = 0;
 
+       IF_PURGE(&ifp->if_snd);
+       ifp->if_flags &= ~IFF_RUNNING;
+
        s = splnet();
        simple_lock(&tun_softc_lock);
-       simple_lock(&tp->tun_lock);
+       mutex_enter(&tp->tun_lock);
        LIST_REMOVE(tp, tun_list);
        if (tp->tun_flags & TUN_OPEN) {
                /* Hang on to storage until last close */
@@ -236,16 +240,13 @@
        }
        simple_unlock(&tun_softc_lock);
 
-       IF_PURGE(&ifp->if_snd);
-       ifp->if_flags &= ~IFF_RUNNING;
-
        if (tp->tun_flags & TUN_RWAIT) {
                tp->tun_flags &= ~TUN_RWAIT;
                wakeup((void *)tp);
        }
        selnotify(&tp->tun_rsel, 0, 0);
 
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
        splx(s);
 
        if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
@@ -259,6 +260,7 @@
                seldestroy(&tp->tun_wsel);
                softint_disestablish(tp->tun_osih);
                softint_disestablish(tp->tun_isih);
+               mutex_destroy(&tp->tun_lock);
                free(tp, M_DEVBUF);
        }
 
@@ -302,7 +304,7 @@
        tp->tun_flags |= TUN_OPEN;
        TUNDEBUG("%s: open\n", ifp->if_xname);
 out:
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
 out_nolock:
        splx(s);
        return (error);
@@ -327,6 +329,7 @@
                seldestroy(&tp->tun_wsel);
                softint_disestablish(tp->tun_osih);
                softint_disestablish(tp->tun_isih);
+               mutex_destroy(&tp->tun_lock);
                free(tp, M_DEVBUF);
                goto out_nolock;
        }
@@ -338,6 +341,12 @@
 
        tp->tun_flags &= ~TUN_OPEN;
 
+       tp->tun_pgid = 0;
+       selnotify(&tp->tun_rsel, 0, 0);
+
+       TUNDEBUG ("%s: closed\n", ifp->if_xname);
+       mutex_exit(&tp->tun_lock);
+
        /*
         * junk all pending output
         */
@@ -361,11 +370,6 @@
                        }
                }
        }
-       tp->tun_pgid = 0;
-       selnotify(&tp->tun_rsel, 0, 0);
-
-       TUNDEBUG ("%s: closed\n", ifp->if_xname);
-       simple_unlock(&tp->tun_lock);
 out_nolock:
        splx(s);
        return (0);
@@ -382,7 +386,7 @@
 
        TUNDEBUG("%s: tuninit\n", ifp->if_xname);
 
-       simple_lock(&tp->tun_lock);
+       mutex_enter(&tp->tun_lock);
        ifp->if_flags |= IFF_UP | IFF_RUNNING;
 
        tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
@@ -420,9 +424,7 @@
                }
 #endif /* INET6 */
        }
-
-       simple_unlock(&tp->tun_lock);
-       return;
+       mutex_exit(&tp->tun_lock);
 }
 
 /*
@@ -499,13 +501,12 @@
        ALTQ_DECL(struct altq_pktattr pktattr;)
 
        s = splnet();
-       simple_lock(&tp->tun_lock);
+       mutex_enter(&tp->tun_lock);
        TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
 
        if ((tp->tun_flags & TUN_READY) != TUN_READY) {
                TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
                          tp->tun_flags);
-               m_freem (m0);
                error = EHOSTDOWN;
                goto out;
        }
@@ -548,11 +549,10 @@
                        af = mtod(m0,uint32_t *);
                        *af = htonl(dst->sa_family);
                } else {
-#ifdef INET     
+#ifdef INET
                        if (dst->sa_family != AF_INET)
 #endif
                        {
-                               m_freem(m0);
                                error = EAFNOSUPPORT;
                                goto out;
                        }
@@ -563,6 +563,7 @@
                if (error) {
                        ifp->if_collisions++;
                        error = EAFNOSUPPORT;
+                       m0 = NULL;
                        goto out;
                }
                mlen = m0->m_pkthdr.len;
@@ -571,7 +572,6 @@
                break;
 #endif
        default:
-               m_freem(m0);
                error = EAFNOSUPPORT;
                goto out;
        }
@@ -585,9 +585,13 @@
 
        selnotify(&tp->tun_rsel, 0, 0);
 out:
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
        splx(s);
-       return (0);
+
+       if (error && m0) {
+               m_freem(m0);
+       }
+       return 0;
 }
 
 static void
@@ -711,7 +715,7 @@
        }
 
 out:
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
 out_nolock:
        splx(s);
        return (error);
@@ -758,7 +762,7 @@
                                goto out;
                        }
                        tp->tun_flags |= TUN_RWAIT;
-                       if (ltsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
+                       if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
                                        "tunread", 0, &tp->tun_lock) != 0) {
                                error = EINTR;
                                goto out_nolock;
@@ -782,7 +786,7 @@
                }
        } while (m0 == 0);
 
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
        splx(s);
 
        /* Copy the mbuf chain */
@@ -804,7 +808,7 @@
        return (error);
 
 out:
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
 out_nolock:
        splx(s);
        return (error);
@@ -834,7 +838,7 @@
        }
 
        /* Unlock until we've got the data */
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
        splx(s);
 
        ifp = &tp->tun_if;
@@ -933,7 +937,7 @@
        bpf_mtap_af(ifp, dst.sa_family, top);
 
        s = splnet();
-       simple_lock(&tp->tun_lock);
+       mutex_enter(&tp->tun_lock);
        if ((tp->tun_flags & TUN_INITED) == 0) {
                /* Interface was destroyed */
                error = ENXIO;
@@ -942,9 +946,10 @@
        if (IF_QFULL(ifq)) {
                IF_DROP(ifq);
                ifp->if_collisions++;
+               mutex_exit(&tp->tun_lock);
                m_freem(top);
                error = ENOBUFS;
-               goto out;
+               goto out_nolock;
        }
 
        IF_ENQUEUE(ifq, top);
@@ -952,7 +957,7 @@
        ifp->if_ibytes += tlen;
        schednetisr(isr);
 out:
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
 out_nolock:
        splx(s);
 out0:
@@ -976,7 +981,7 @@
        if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
                return;
 
-       simple_lock(&tp->tun_lock);
+       mutex_enter(&tp->tun_lock);
        if (!IF_IS_EMPTY(&ifp->if_snd)) {
                if (tp->tun_flags & TUN_RWAIT) {
                        tp->tun_flags &= ~TUN_RWAIT;
@@ -987,7 +992,7 @@
 
                selnotify(&tp->tun_rsel, 0, 0);
        }
-       simple_unlock(&tp->tun_lock);
+       mutex_exit(&tp->tun_lock);
 }
 #endif /* ALTQ */
 /*
@@ -1027,7 +1032,7 @@



Home | Main Index | Thread Index | Old Index