Source-Changes-HG archive

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

[src/trunk]: src/sys/netipsec Use kmem(9) instead of malloc/free



details:   https://anonhg.NetBSD.org/src/rev/61247fd34cd1
branches:  trunk
changeset: 823951:61247fd34cd1
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Tue May 16 03:05:28 2017 +0000

description:
Use kmem(9) instead of malloc/free

Some of non-sleepable allocations can be replaced with sleepable ones.
To make it clear that the replacements are possible, some assertions
are addded.

diffstat:

 sys/netipsec/ipsec.c   |   27 ++++++----
 sys/netipsec/key.c     |  127 +++++++++++++++++++++---------------------------
 sys/netipsec/keysock.c |    6 +-
 3 files changed, 76 insertions(+), 84 deletions(-)

diffs (truncated from 520 to 300 lines):

diff -r 6fca27520bbf -r 61247fd34cd1 sys/netipsec/ipsec.c
--- a/sys/netipsec/ipsec.c      Tue May 16 02:59:22 2017 +0000
+++ b/sys/netipsec/ipsec.c      Tue May 16 03:05:28 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ipsec.c,v 1.89 2017/05/15 09:55:29 ozaki-r Exp $       */
+/*     $NetBSD: ipsec.c,v 1.90 2017/05/16 03:05:28 ozaki-r Exp $       */
 /*     $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $       */
 /*     $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.89 2017/05/15 09:55:29 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.90 2017/05/16 03:05:28 ozaki-r Exp $");
 
 /*
  * IPsec controller part.
@@ -45,7 +45,6 @@
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/domain.h>
 #include <sys/protosw.h>
@@ -58,6 +57,8 @@
 #include <sys/sysctl.h>
 #include <sys/proc.h>
 #include <sys/kauth.h>
+#include <sys/cpu.h>
+#include <sys/kmem.h>
 
 #include <net/if.h>
 #include <net/route.h>
@@ -1263,7 +1264,8 @@
 static void
 ipsec_delpcbpolicy(struct inpcbpolicy *p)
 {
-       free(p, M_SECA);
+
+       kmem_free(p, sizeof(*p));
 }
 
 /* initialize policy in PCB */
@@ -1272,14 +1274,11 @@
 {
        struct inpcbpolicy *new;
 
+       KASSERT(!cpu_softintr_p());
        KASSERT(so != NULL);
        KASSERT(policy != NULL);
 
-       new = malloc(sizeof(*new), M_SECA, M_NOWAIT|M_ZERO);
-       if (new == NULL) {
-               ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
-               return ENOBUFS;
-       }
+       new = kmem_zalloc(sizeof(*new), KM_SLEEP);
 
        if (IPSEC_PRIVILEGED_SO(so))
                new->priv = 1;
@@ -1353,7 +1352,7 @@
         */
        q = &newchain;
        for (p = src->req; p; p = p->next) {
-               *q = malloc(sizeof(**q), M_SECA, M_NOWAIT|M_ZERO);
+               *q = kmem_zalloc(sizeof(**q), KM_SLEEP);
                if (*q == NULL)
                        goto fail;
                (*q)->next = NULL;
@@ -1382,7 +1381,7 @@
 fail:
        for (q = &newchain; *q; q = &r) {
                r = (*q)->next;
-               free(*q, M_SECA);
+               kmem_free(*q, sizeof(**q));
        }
        return NULL;
 }
@@ -1401,6 +1400,8 @@
        struct secpolicy *newsp = NULL;
        int error;
 
+       KASSERT(!cpu_softintr_p());
+
        /* sanity check. */
        if (policy == NULL || *policy == NULL || request == NULL)
                return EINVAL;
@@ -1474,6 +1475,8 @@
        const struct sadb_x_policy *xpl;
        struct secpolicy **policy;
 
+       KASSERT(!cpu_softintr_p());
+
        /* sanity check. */
        if (inp == NULL || request == NULL)
                return EINVAL;
@@ -1564,6 +1567,8 @@
        const struct sadb_x_policy *xpl;
        struct secpolicy **policy;
 
+       KASSERT(!cpu_softintr_p());
+
        /* sanity check. */
        if (in6p == NULL || request == NULL)
                return EINVAL;
diff -r 6fca27520bbf -r 61247fd34cd1 sys/netipsec/key.c
--- a/sys/netipsec/key.c        Tue May 16 02:59:22 2017 +0000
+++ b/sys/netipsec/key.c        Tue May 16 03:05:28 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: key.c,v 1.126 2017/05/16 02:59:22 ozaki-r Exp $        */
+/*     $NetBSD: key.c,v 1.127 2017/05/16 03:05:28 ozaki-r Exp $        */
 /*     $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $        */
 /*     $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $   */
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.126 2017/05/16 02:59:22 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.127 2017/05/16 03:05:28 ozaki-r Exp $");
 
 /*
  * This code is referd to RFC 2367
@@ -65,6 +65,8 @@
 #include <sys/psref.h>
 #include <sys/lwp.h>
 #include <sys/workqueue.h>
+#include <sys/kmem.h>
+#include <sys/cpu.h>
 
 #include <net/if.h>
 #include <net/route.h>
@@ -1387,12 +1389,12 @@
                }
 
                nextisr = isr->next;
-               KFREE(isr);
+               kmem_intr_free(isr, sizeof(*isr));
                isr = nextisr;
        }
     }
 
-       KFREE(sp);
+       kmem_intr_free(sp, sizeof(*sp));
 
        splx(s);
 }
@@ -1457,12 +1459,8 @@
 {
        struct secpolicy *newsp = NULL;
 
-       newsp = (struct secpolicy *)
-               malloc(sizeof(struct secpolicy), M_SECA, M_NOWAIT|M_ZERO);
-       if (newsp) {
-               newsp->refcnt = 1;
-               newsp->req = NULL;
-       }
+       newsp = kmem_zalloc(sizeof(struct secpolicy), KM_SLEEP);
+       newsp->refcnt = 1;
 
        KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
            "DP from %s:%u return SP:%p\n", where, tag, newsp);
@@ -1479,6 +1477,7 @@
 {
        struct secpolicy *newsp;
 
+       KASSERT(!cpu_softintr_p());
        KASSERT(xpl0 != NULL);
        KASSERT(len >= sizeof(*xpl0));
 
@@ -1544,13 +1543,7 @@
                }
 
                /* allocate request buffer */
-               KMALLOC(*p_isr, struct ipsecrequest *, sizeof(**p_isr));
-               if ((*p_isr) == NULL) {
-                       ipseclog((LOG_DEBUG,
-                           "key_msg2sp: No more memory.\n"));
-                       *error = ENOBUFS;
-                       goto free_exit;
-               }
+               *p_isr = kmem_alloc(sizeof(**p_isr), KM_SLEEP);
                memset(*p_isr, 0, sizeof(**p_isr));
 
                /* set values */
@@ -1862,6 +1855,7 @@
        struct secpolicy *newsp;
        int error;
 
+       KASSERT(!cpu_softintr_p());
        KASSERT(so != NULL);
        KASSERT(m != NULL);
        KASSERT(mhp != NULL);
@@ -1957,7 +1951,7 @@
        }
 
        if ((newsp->id = key_getnewspid()) == 0) {
-               KFREE(newsp);
+               kmem_free(newsp, sizeof(*newsp));
                return key_senderror(so, m, ENOBUFS);
        }
 
@@ -1973,12 +1967,12 @@
        /* sanity check on addr pair */
        if (((const struct sockaddr *)(src0 + 1))->sa_family !=
                        ((const struct sockaddr *)(dst0+ 1))->sa_family) {
-               KFREE(newsp);
+               kmem_free(newsp, sizeof(*newsp));
                return key_senderror(so, m, EINVAL);
        }
        if (((const struct sockaddr *)(src0 + 1))->sa_len !=
                        ((const struct sockaddr *)(dst0+ 1))->sa_len) {
-               KFREE(newsp);
+               kmem_free(newsp, sizeof(*newsp));
                return key_senderror(so, m, EINVAL);
        }
 
@@ -2876,22 +2870,20 @@
 key_newsah(const struct secasindex *saidx)
 {
        struct secashead *newsah;
+       int i;
 
        KASSERT(saidx != NULL);
 
-       newsah = (struct secashead *)
-               malloc(sizeof(struct secashead), M_SECA, M_NOWAIT|M_ZERO);
-       if (newsah != NULL) {
-               int i;
-               for (i = 0; i < sizeof(newsah->savtree)/sizeof(newsah->savtree[0]); i++)
-                       LIST_INIT(&newsah->savtree[i]);
-               newsah->saidx = *saidx;
-
-               /* add to saidxtree */
-               newsah->state = SADB_SASTATE_MATURE;
-               LIST_INSERT_HEAD(&sahtree, newsah, chain);
-       }
-       return(newsah);
+       newsah = kmem_zalloc(sizeof(struct secashead), KM_SLEEP);
+       for (i = 0; i < __arraycount(newsah->savtree); i++)
+               LIST_INIT(&newsah->savtree[i]);
+       newsah->saidx = *saidx;
+
+       /* add to saidxtree */
+       newsah->state = SADB_SASTATE_MATURE;
+       LIST_INSERT_HEAD(&sahtree, newsah, chain);
+
+       return newsah;
 }
 
 /*
@@ -2905,9 +2897,10 @@
        int s;
        int zombie = 0;
 
+       KASSERT(!cpu_softintr_p());
        KASSERT(sah != NULL);
 
-       s = splsoftnet();       /*called from softclock()*/
+       s = splsoftnet();
 
        /* searching all SA registerd in the secindex. */
        SASTATE_ANY_FOREACH(state) {
@@ -2935,7 +2928,7 @@
        if (__LIST_CHAINED(sah))
                LIST_REMOVE(sah, chain);
 
-       KFREE(sah);
+       kmem_free(sah, sizeof(*sah));
 
        splx(s);
        return;
@@ -2961,17 +2954,13 @@
        struct secasvar *newsav;
        const struct sadb_sa *xsa;
 
+       KASSERT(!cpu_softintr_p());
        KASSERT(m != NULL);
        KASSERT(mhp != NULL);
        KASSERT(mhp->msg != NULL);
        KASSERT(sah != NULL);
 
-       KMALLOC(newsav, struct secasvar *, sizeof(struct secasvar));
-       if (newsav == NULL) {
-               ipseclog((LOG_DEBUG, "key_newsa: No more memory.\n"));
-               *errp = ENOBUFS;
-               goto done;
-       }
+       newsav = kmem_alloc(sizeof(struct secasvar), KM_SLEEP);
        memset(newsav, 0, sizeof(struct secasvar));
 
        switch (mhp->msg->sadb_msg_type) {
@@ -2991,28 +2980,24 @@
        case SADB_ADD:
                /* sanity check */
                if (mhp->ext[SADB_EXT_SA] == NULL) {
-                       KFREE(newsav), newsav = NULL;
                        ipseclog((LOG_DEBUG, "key_newsa: invalid message is passed.\n"));



Home | Main Index | Thread Index | Old Index