Module Name:    src
Committed By:   ozaki-r
Date:           Tue May 16 03:05:28 UTC 2017

Modified Files:
        src/sys/netipsec: ipsec.c key.c keysock.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/netipsec/ipsec.c
cvs rdiff -u -r1.126 -r1.127 src/sys/netipsec/key.c
cvs rdiff -u -r1.54 -r1.55 src/sys/netipsec/keysock.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/netipsec/ipsec.c
diff -u src/sys/netipsec/ipsec.c:1.89 src/sys/netipsec/ipsec.c:1.90
--- src/sys/netipsec/ipsec.c:1.89	Mon May 15 09:55:29 2017
+++ src/sys/netipsec/ipsec.c	Tue May 16 03:05:28 2017
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.
 
 #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 @@ __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.
 #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 @@ ipsec6_setspidx_ipaddr(struct mbuf *m, s
 static void
 ipsec_delpcbpolicy(struct inpcbpolicy *p)
 {
-	free(p, M_SECA);
+
+	kmem_free(p, sizeof(*p));
 }
 
 /* initialize policy in PCB */
@@ -1272,14 +1274,11 @@ ipsec_init_policy(struct socket *so, str
 {
 	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 @@ ipsec_deepcopy_policy(const struct secpo
 	 */
 	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 @@ ipsec_deepcopy_policy(const struct secpo
 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 @@ ipsec_set_policy(
 	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 @@ ipsec4_set_policy(struct inpcb *inp, int
 	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 @@ ipsec6_set_policy(struct in6pcb *in6p, i
 	const struct sadb_x_policy *xpl;
 	struct secpolicy **policy;
 
+	KASSERT(!cpu_softintr_p());
+
 	/* sanity check. */
 	if (in6p == NULL || request == NULL)
 		return EINVAL;

Index: src/sys/netipsec/key.c
diff -u src/sys/netipsec/key.c:1.126 src/sys/netipsec/key.c:1.127
--- src/sys/netipsec/key.c:1.126	Tue May 16 02:59:22 2017
+++ src/sys/netipsec/key.c	Tue May 16 03:05:28 2017
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: key.c,v 1.12
 #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 @@ key_delsp(struct secpolicy *sp)
 		}
 
 		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 @@ key_newsp(const char* where, int tag)
 {
 	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 @@ key_msg2sp(const struct sadb_x_policy *x
 {
 	struct secpolicy *newsp;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(xpl0 != NULL);
 	KASSERT(len >= sizeof(*xpl0));
 
@@ -1544,13 +1543,7 @@ key_msg2sp(const struct sadb_x_policy *x
 		}
 
 		/* 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 @@ key_spdadd(struct socket *so, struct mbu
 	struct secpolicy *newsp;
 	int error;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(so != NULL);
 	KASSERT(m != NULL);
 	KASSERT(mhp != NULL);
@@ -1957,7 +1951,7 @@ key_spdadd(struct socket *so, struct mbu
 	}
 
 	if ((newsp->id = key_getnewspid()) == 0) {
-		KFREE(newsp);
+		kmem_free(newsp, sizeof(*newsp));
 		return key_senderror(so, m, ENOBUFS);
 	}
 
@@ -1973,12 +1967,12 @@ key_spdadd(struct socket *so, struct mbu
 	/* 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 @@ static struct secashead *
 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 @@ key_delsah(struct secashead *sah)
 	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 @@ key_delsah(struct secashead *sah)
 	if (__LIST_CHAINED(sah))
 		LIST_REMOVE(sah, chain);
 
-	KFREE(sah);
+	kmem_free(sah, sizeof(*sah));
 
 	splx(s);
 	return;
@@ -2961,17 +2954,13 @@ key_newsav(struct mbuf *m, const struct 
 	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 @@ key_newsav(struct mbuf *m, const struct 
 	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"));
 			*errp = EINVAL;
-			goto done;
+			goto error;
 		}
 		xsa = (const struct sadb_sa *)mhp->ext[SADB_EXT_SA];
 		newsav->spi = xsa->sadb_sa_spi;
 		newsav->seq = mhp->msg->sadb_msg_seq;
 		break;
 	default:
-		KFREE(newsav), newsav = NULL;
 		*errp = EINVAL;
-		goto done;
+		goto error;
 	}
 
 	/* copy sav values */
 	if (mhp->msg->sadb_msg_type != SADB_GETSPI) {
 		*errp = key_setsaval(newsav, m, mhp);
-		if (*errp) {
-			KFREE(newsav), newsav = NULL;
-			goto done;
-		}
+		if (*errp)
+			goto error;
 	}
 
 	/* reset created */
@@ -3025,11 +3010,16 @@ key_newsav(struct mbuf *m, const struct 
 	newsav->state = SADB_SASTATE_LARVAL;
 	LIST_INSERT_TAIL(&sah->savtree[SADB_SASTATE_LARVAL], newsav,
 			secasvar, chain);
-done:
 	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
 	    "DP from %s:%u return SA:%p\n", where, tag, newsav);
-
 	return newsav;
+
+error:
+	KASSERT(*errp != 0);
+	kmem_free(newsav, sizeof(*newsav));
+	KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
+	    "DP from %s:%u return SA:NULL\n", where, tag);
+	return NULL;
 }
 
 /*
@@ -3075,7 +3065,7 @@ key_delsav(struct secasvar *sav)
 		sav->replay = NULL;
 	}
 	if (sav->lft_c != NULL) {
-		KFREE(sav->lft_c);
+		kmem_intr_free(sav->lft_c, sizeof(*(sav->lft_c)));
 		sav->lft_c = NULL;
 	}
 	if (sav->lft_h != NULL) {
@@ -3087,7 +3077,7 @@ key_delsav(struct secasvar *sav)
 		sav->lft_s = NULL;
 	}
 
-	KFREE(sav);
+	kmem_intr_free(sav, sizeof(*sav));
 
 	return;
 }
@@ -3190,6 +3180,7 @@ key_setsaval(struct secasvar *sav, struc
 {
 	int error = 0;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(m != NULL);
 	KASSERT(mhp != NULL);
 	KASSERT(mhp->msg != NULL);
@@ -3348,13 +3339,7 @@ key_setsaval(struct secasvar *sav, struc
 	sav->created = time_uptime;
 
 	/* make lifetime for CURRENT */
-	KMALLOC(sav->lft_c, struct sadb_lifetime *,
-	    sizeof(struct sadb_lifetime));
-	if (sav->lft_c == NULL) {
-		ipseclog((LOG_DEBUG, "key_setsaval: No more memory.\n"));
-		error = ENOBUFS;
-		goto fail;
-	}
+	sav->lft_c = kmem_alloc(sizeof(struct sadb_lifetime), KM_SLEEP);
 
 	sav->lft_c->sadb_lifetime_len =
 	    PFKEY_UNIT64(sizeof(struct sadb_lifetime));
@@ -3418,7 +3403,7 @@ key_setsaval(struct secasvar *sav, struc
 		sav->key_enc = NULL;
 	}
 	if (sav->lft_c != NULL) {
-		KFREE(sav->lft_c);
+		kmem_free(sav->lft_c, sizeof(*(sav->lft_c)));
 		sav->lft_c = NULL;
 	}
 	if (sav->lft_h != NULL) {
@@ -4743,7 +4728,7 @@ key_timehandler_work(struct work *wk, vo
 		if (now - acq->created > key_blockacq_lifetime
 		 && __LIST_CHAINED(acq)) {
 			LIST_REMOVE(acq, chain);
-			KFREE(acq);
+			kmem_free(acq, sizeof(*acq));
 		}
 	}
     }
@@ -4757,7 +4742,7 @@ key_timehandler_work(struct work *wk, vo
 		if (now - acq->created > key_blockacq_lifetime
 		 && __LIST_CHAINED(acq)) {
 			LIST_REMOVE(acq, chain);
-			KFREE(acq);
+			kmem_free(acq, sizeof(*acq));
 		}
 	}
     }
@@ -4898,6 +4883,7 @@ key_getspi(struct socket *so, struct mbu
 	u_int16_t reqid;
 	int error;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(so != NULL);
 	KASSERT(m != NULL);
 	KASSERT(mhp != NULL);
@@ -5265,6 +5251,7 @@ key_update(struct socket *so, struct mbu
 	u_int16_t reqid;
 	int error;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(so != NULL);
 	KASSERT(m != NULL);
 	KASSERT(mhp != NULL);
@@ -6428,7 +6415,7 @@ key_newacq(const struct secasindex *said
 	struct secacq *newacq;
 
 	/* get new entry */
-	KMALLOC(newacq, struct secacq *, sizeof(struct secacq));
+	newacq = kmem_intr_alloc(sizeof(struct secacq), KM_NOSLEEP);
 	if (newacq == NULL) {
 		ipseclog((LOG_DEBUG, "key_newacq: No more memory.\n"));
 		return NULL;
@@ -6477,7 +6464,7 @@ key_newspacq(const struct secpolicyindex
 	struct secspacq *acq;
 
 	/* get new entry */
-	KMALLOC(acq, struct secspacq *, sizeof(struct secspacq));
+	acq = kmem_intr_alloc(sizeof(struct secspacq), KM_NOSLEEP);
 	if (acq == NULL) {
 		ipseclog((LOG_DEBUG, "key_newspacq: No more memory.\n"));
 		return NULL;
@@ -6644,6 +6631,7 @@ key_register(struct socket *so, struct m
 {
 	struct secreg *reg, *newreg = 0;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(so != NULL);
 	KASSERT(m != NULL);
 	KASSERT(mhp != NULL);
@@ -6666,11 +6654,7 @@ key_register(struct socket *so, struct m
 	}
 
 	/* create regnode */
-	KMALLOC(newreg, struct secreg *, sizeof(*newreg));
-	if (newreg == NULL) {
-		ipseclog((LOG_DEBUG, "key_register: No more memory.\n"));
-		return key_senderror(so, m, ENOBUFS);
-	}
+	newreg = kmem_alloc(sizeof(*newreg), KM_SLEEP);
 	memset(newreg, 0, sizeof(*newreg));
 
 	newreg->so = so;
@@ -6794,6 +6778,7 @@ key_freereg(struct socket *so)
 	struct secreg *reg;
 	int i;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(so != NULL);
 
 	/*
@@ -6806,7 +6791,7 @@ key_freereg(struct socket *so)
 			if (reg->so == so
 			 && __LIST_CHAINED(reg)) {
 				LIST_REMOVE(reg, chain);
-				KFREE(reg);
+				kmem_free(reg, sizeof(*reg));
 				break;
 			}
 		}

Index: src/sys/netipsec/keysock.c
diff -u src/sys/netipsec/keysock.c:1.54 src/sys/netipsec/keysock.c:1.55
--- src/sys/netipsec/keysock.c:1.54	Thu Apr 27 09:43:52 2017
+++ src/sys/netipsec/keysock.c	Tue May 16 03:05:28 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: keysock.c,v 1.54 2017/04/27 09:43:52 ozaki-r Exp $	*/
+/*	$NetBSD: keysock.c,v 1.55 2017/05/16 03:05:28 ozaki-r Exp $	*/
 /*	$FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $	*/
 /*	$KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $	*/
 
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.54 2017/04/27 09:43:52 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.55 2017/05/16 03:05:28 ozaki-r Exp $");
 
 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
 
@@ -49,6 +49,7 @@ __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/systm.h>
+#include <sys/cpu.h>
 
 #include <net/raw_cb.h>
 #include <net/route.h>
@@ -459,6 +460,7 @@ key_detach(struct socket *so)
 	struct keycb *kp = (struct keycb *)sotorawcb(so);
 	int s;
 
+	KASSERT(!cpu_softintr_p());
 	KASSERT(solocked(so));
 	KASSERT(kp != NULL);
 

Reply via email to