Fix waserror/lock order
authorDavide Libenzi <dlibenzi@google.com>
Sat, 17 Oct 2015 22:01:42 +0000 (15:01 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 29 Oct 2015 21:19:10 +0000 (17:19 -0400)
Fix waserror/lock order in order to enforce the acquire before waserror
pattern.

Signed-off-by: Davide Libenzi <dlibenzi@google.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/src/net/arp.c
kern/src/net/ipifc.c
kern/src/net/netif.c
kern/src/net/tcp.c

index 384e7f4..8505edf 100644 (file)
@@ -405,11 +405,11 @@ arpenter(struct Fs *fs, int version, uint8_t * ip, uint8_t * mac, int n,
                        while (bp) {
                                next = bp->list;
                                if (ifc != NULL) {
+                                       rlock(&ifc->rwlock);
                                        if (waserror()) {
                                                runlock(&ifc->rwlock);
                                                nexterror();
                                        }
-                                       rlock(&ifc->rwlock);
                                        if (ifc->m != NULL)
                                                ifc->m->bwrite(ifc, bp, version, ip);
                                        else
index 4a13e2c..c249103 100644 (file)
@@ -205,11 +205,11 @@ static char *ipifcunbind(struct Ipifc *ifc)
        ERRSTACK(1);
        char *err;
 
+       wlock(&ifc->rwlock);
        if (waserror()) {
                wunlock(&ifc->rwlock);
                nexterror();
        }
-       wlock(&ifc->rwlock);
 
        /* dissociate routes */
        if (ifc->m != NULL && ifc->m->unbindonclose == 0)
@@ -701,11 +701,11 @@ static char *ipifcconnect(struct conv *c, char **argv, int argc)
        if (ifc->m == NULL)
                return "ipifc not yet bound to device";
 
+       wlock(&ifc->rwlock);
        if (waserror()) {
                wunlock(&ifc->rwlock);
                nexterror();
        }
-       wlock(&ifc->rwlock);
        while (ifc->lifc) {
                err = ipifcremlifc(ifc, ifc->lifc);
                if (err)
@@ -1482,11 +1482,11 @@ void ipifcaddmulti(struct conv *c, uint8_t * ma, uint8_t * ia)
                if ((*p)->inuse == 0)
                        continue;
                ifc = (struct Ipifc *)(*p)->ptcl;
+               wlock(&ifc->rwlock);
                if (waserror()) {
                        wunlock(&ifc->rwlock);
                        nexterror();
                }
-               wlock(&ifc->rwlock);
                for (lifc = ifc->lifc; lifc; lifc = lifc->next)
                        if (ipcmp(ia, lifc->local) == 0)
                                addselfcache(f, ifc, lifc, ma, Rmulti);
@@ -1525,11 +1525,11 @@ void ipifcremmulti(struct conv *c, uint8_t * ma, uint8_t * ia)
                        continue;
 
                ifc = (struct Ipifc *)(*p)->ptcl;
+               wlock(&ifc->rwlock);
                if (waserror()) {
                        wunlock(&ifc->rwlock);
                        nexterror();
                }
-               wlock(&ifc->rwlock);
                for (lifc = ifc->lifc; lifc; lifc = lifc->next)
                        if (ipcmp(ia, lifc->local) == 0)
                                remselfcache(f, ifc, lifc, ma);
index 9d71dad..837cb67 100644 (file)
@@ -389,12 +389,12 @@ long netifwrite(struct ether *nif, struct chan *c, void *a, long n)
        memmove(buf, a, n);
        buf[n] = 0;
 
+       qlock(&nif->qlock);
        if (waserror()) {
                qunlock(&nif->qlock);
                nexterror();
        }
 
-       qlock(&nif->qlock);
        f = nif->f[NETID(c->qid.path)];
        if ((p = matchtoken(buf, "connect")) != 0) {
                type = strtol(p, 0, 0); /* allows any base, though usually hex */
index 6dea5ce..b5306c9 100644 (file)
@@ -564,11 +564,11 @@ void tcpkick(void *x)
 
        tcb = (Tcpctl *) s->ptcl;
 
+       qlock(&s->qlock);
        if (waserror()) {
                qunlock(&s->qlock);
                nexterror();
        }
-       qlock(&s->qlock);
 
        switch (tcb->state) {
                case Syn_sent:
@@ -613,11 +613,11 @@ void tcpacktimer(void *v)
        s = v;
        tcb = (Tcpctl *) s->ptcl;
 
+       qlock(&s->qlock);
        if (waserror()) {
                qunlock(&s->qlock);
                nexterror();
        }
-       qlock(&s->qlock);
        if (tcb->state != Closed) {
                tcb->flags |= FORCE;
                tcprcvwin(s);
@@ -2736,11 +2736,11 @@ void tcpkeepalive(void *v)
 
        s = v;
        tcb = (Tcpctl *) s->ptcl;
+       qlock(&s->qlock);
        if (waserror()) {
                qunlock(&s->qlock);
                nexterror();
        }
-       qlock(&s->qlock);
        if (tcb->state != Closed) {
                if (--(tcb->kacounter) <= 0) {
                        localclose(s, errno_to_string(ETIMEDOUT));
@@ -2822,11 +2822,11 @@ void tcptimeout(void *arg)
        tpriv = s->p->priv;
        tcb = (Tcpctl *) s->ptcl;
 
+       qlock(&s->qlock);
        if (waserror()) {
                qunlock(&s->qlock);
                nexterror();
        }
-       qlock(&s->qlock);
        switch (tcb->state) {
                default:
                        tcb->backoff++;