CVS commit: src/sys/opencrypto

2021-08-14 Thread Andrius Varanavicius
Module Name:src
Committed By:   andvar
Date:   Sat Aug 14 20:43:05 UTC 2021

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.h

Log Message:
fix typo in CRK_ALGORITHM_MIN definition to match CRK_ALGORITHM_MAX one.
while here fix few typos in comments.


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.116 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.41 -r1.42 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.115 src/sys/opencrypto/crypto.c:1.116
--- src/sys/opencrypto/crypto.c:1.115	Mon Aug  9 19:57:58 2021
+++ src/sys/opencrypto/crypto.c	Sat Aug 14 20:43:05 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.115 2021/08/09 19:57:58 andvar Exp $ */
+/*	$NetBSD: crypto.c,v 1.116 2021/08/14 20:43:05 andvar Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.115 2021/08/09 19:57:58 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.116 2021/08/14 20:43:05 andvar Exp $");
 
 #include 
 #include 
@@ -1047,7 +1047,7 @@ crypto_kregister(u_int32_t driverid, int
 
 	cap = crypto_checkdriver_lock(driverid);
 	if (cap != NULL &&
-	(CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
+	(CRK_ALGORITHM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
 		/*
 		 * XXX Do some performance testing to determine placing.
 		 * XXX We probably need an auxiliary data structure that

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.41 src/sys/opencrypto/cryptodev.h:1.42
--- src/sys/opencrypto/cryptodev.h:1.41	Mon Aug  9 19:57:58 2021
+++ src/sys/opencrypto/cryptodev.h	Sat Aug 14 20:43:05 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.41 2021/08/09 19:57:58 andvar Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.42 2021/08/14 20:43:05 andvar Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -325,7 +325,7 @@ struct cryptret {
 
 
 /* Asymmetric key operations */
-#define	CRK_ALGORITM_MIN	0
+#define	CRK_ALGORITHM_MIN	0
 #define CRK_MOD_EXP		0
 #define CRK_MOD_EXP_CRT		1
 #define CRK_DSA_SIGN		2
@@ -402,7 +402,7 @@ struct cryptostats {
 	 * accumulate statistics about how long it takes to process
 	 * crypto requests at various points during processing.
 	 */
-	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
+	struct cryptotstat cs_invoke;	/* crypto_dispatch -> crypto_invoke */
 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
 	struct cryptotstat cs_finis;	/* callback -> callback return */
@@ -636,8 +636,8 @@ extern	int crypto_devallowsoft;	/* only 
 /*
  * initialize the crypto framework subsystem (not the pseudo-device).
  * This must be called very early in boot, so the framework is ready
- * to handle registration requests when crpto hardware is autoconfigured.
- * (This declaration doesnt really belong here but there's no header
+ * to handle registration requests when crypto hardware is autoconfigured.
+ * (This declaration doesn't really belong here but there's no header
  * for the raw framework.)
  */
 int	crypto_init(void);



CVS commit: src/sys/opencrypto

2021-04-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Apr  6 03:38:04 UTC 2021

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
Fix ATF failures, sorry.


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.60 src/sys/opencrypto/cryptosoft.c:1.61
--- src/sys/opencrypto/cryptosoft.c:1.60	Mon Apr  5 01:24:50 2021
+++ src/sys/opencrypto/cryptosoft.c	Tue Apr  6 03:38:04 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.60 2021/04/05 01:24:50 knakahara Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.61 2021/04/06 03:38:04 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.60 2021/04/05 01:24:50 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.61 2021/04/06 03:38:04 knakahara Exp $");
 
 #include 
 #include 
@@ -712,7 +712,7 @@ swcr_compdec(struct cryptodesc *crd, con
 	 * copy in a buffer.
 	 */
 
-	data = kmem_alloc(crd->crd_len, KM_NOSLEEP);
+	data = malloc(crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
 	if (data == NULL)
 		return (EINVAL);
 	COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
@@ -723,7 +723,7 @@ swcr_compdec(struct cryptodesc *crd, con
 		result = cxf->decompress(data, crd->crd_len, ,
 	 *res_size);
 
-	kmem_free(data, crd->crd_len);
+	free(data, M_CRYPTO_DATA);
 	if (result == 0)
 		return EINVAL;
 
@@ -1115,7 +1115,7 @@ swcr_freesession_internal(struct swcr_da
 			break;
 		}
 
-		free(swd, M_CRYPTO_DATA);
+		kmem_free(swd, sizeof(*swd));
 	}
 }
 



CVS commit: src/sys/opencrypto

2021-04-04 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr  5 01:24:50 UTC 2021

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
refactor: reduce access to swcr_sessions[i] directly


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.59 src/sys/opencrypto/cryptosoft.c:1.60
--- src/sys/opencrypto/cryptosoft.c:1.59	Mon Apr  5 01:23:15 2021
+++ src/sys/opencrypto/cryptosoft.c	Mon Apr  5 01:24:50 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.59 2021/04/05 01:23:15 knakahara Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.60 2021/04/05 01:24:50 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.59 2021/04/05 01:23:15 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.60 2021/04/05 01:24:50 knakahara Exp $");
 
 #include 
 #include 
@@ -76,6 +76,7 @@ static	int swcr_combined(struct cryptop 
 static	int swcr_process(void *, struct cryptop *, int);
 static	int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
 static	int swcr_freesession(void *, u_int64_t);
+static void swcr_freesession_internal(struct swcr_data *);
 
 static	int swcryptoattach_internal(void);
 
@@ -758,6 +759,7 @@ static int
 swcr_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
 {
 	struct swcr_data **swd;
+	struct swcr_data *first, *tmp;
 	const struct swcr_auth_hash *axf;
 	const struct swcr_enc_xform *txf;
 	const struct swcr_comp_algo *cxf;
@@ -802,15 +804,16 @@ swcr_newsession(void *arg, u_int32_t *si
 		swcr_sessions = newsessions;
 	}
 
-	swd = _sessions[i];
-	*sid = i;
-
+	first = NULL;
+	swd = 
 	while (cri) {
 		*swd = kmem_zalloc(sizeof **swd, KM_NOSLEEP);
 		if (*swd == NULL) {
-			swcr_freesession(NULL, i);
+			if (first != NULL)
+swcr_freesession_internal(first);
 			return ENOBUFS;
-		}
+		} else if (first == NULL)
+			first = *swd;
 
 		switch (cri->cri_alg) {
 		case CRYPTO_DES_CBC:
@@ -850,7 +853,7 @@ swcr_newsession(void *arg, u_int32_t *si
 			error = txf->setkey(&((*swd)->sw_kschedule),
 	cri->cri_key, cri->cri_klen / 8);
 			if (error) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return error;
 			}
 			(*swd)->sw_exf = txf;
@@ -889,13 +892,13 @@ swcr_newsession(void *arg, u_int32_t *si
 		authcommon:
 			(*swd)->sw_ictx = kmem_alloc(axf->ctxsize, KM_NOSLEEP);
 			if ((*swd)->sw_ictx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 
 			(*swd)->sw_octx = kmem_alloc(axf->ctxsize, KM_NOSLEEP);
 			if ((*swd)->sw_octx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 
@@ -933,7 +936,7 @@ swcr_newsession(void *arg, u_int32_t *si
 		auth2common:
 			(*swd)->sw_ictx = kmem_alloc(axf->ctxsize, KM_NOSLEEP);
 			if ((*swd)->sw_ictx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 
@@ -941,7 +944,7 @@ swcr_newsession(void *arg, u_int32_t *si
 			(*swd)->sw_octx = kmem_alloc(cri->cri_klen / 8,
 			KM_NOSLEEP);
 			if ((*swd)->sw_octx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 
@@ -964,7 +967,7 @@ swcr_newsession(void *arg, u_int32_t *si
 		auth3common:
 			(*swd)->sw_ictx = kmem_alloc(axf->ctxsize, KM_NOSLEEP);
 			if ((*swd)->sw_ictx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 
@@ -986,7 +989,7 @@ swcr_newsession(void *arg, u_int32_t *si
 		auth4common:
 			(*swd)->sw_ictx = kmem_alloc(axf->ctxsize, KM_NOSLEEP);
 			if ((*swd)->sw_ictx == NULL) {
-swcr_freesession(NULL, i);
+swcr_freesession_internal(first);
 return ENOBUFS;
 			}
 			axf->Init((*swd)->sw_ictx);
@@ -1010,7 +1013,7 @@ swcr_newsession(void *arg, u_int32_t *si
 			(*swd)->sw_cxf = cxf;
 			break;
 		default:
-			swcr_freesession(NULL, i);
+			swcr_freesession_internal(first);
 			return EINVAL;
 		}
 
@@ -1018,30 +1021,25 @@ swcr_newsession(void *arg, u_int32_t *si
 		cri = cri->cri_next;
 		swd = &((*swd)->sw_next);
 	}
+
+	swcr_sessions[i] = first;
+	*sid = i;
 	return 0;
 }
 
-/*
- * Free a session.
- */
-static int
-swcr_freesession(void *arg, u_int64_t tid)
+static void
+swcr_freesession_internal(struct swcr_data *arg)
 {
-	struct swcr_data *swd;
+	struct swcr_data *swd, *swd0;
 	const struct swcr_enc_xform *txf;
 	const struct swcr_auth_hash *axf;
-	u_int32_t sid = ((u_int32_t) tid) & 0x;
-
-	if (sid > swcr_sesnum || swcr_sessions == NULL ||
-	swcr_sessions[sid] == 

CVS commit: src/sys/opencrypto

2021-04-04 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr  5 01:23:15 UTC 2021

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
refactor: reduce changing swcr_sesnum


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.59 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.58 src/sys/opencrypto/cryptosoft.c:1.59
--- src/sys/opencrypto/cryptosoft.c:1.58	Mon Apr  5 01:22:22 2021
+++ src/sys/opencrypto/cryptosoft.c	Mon Apr  5 01:23:15 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.58 2021/04/05 01:22:22 knakahara Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.59 2021/04/05 01:23:15 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.58 2021/04/05 01:22:22 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.59 2021/04/05 01:23:15 knakahara Exp $");
 
 #include 
 #include 
@@ -775,32 +775,31 @@ swcr_newsession(void *arg, u_int32_t *si
 		i = 1;		/* NB: to silence compiler warning */
 
 	if (swcr_sessions == NULL || i == swcr_sesnum) {
+		u_int32_t newnum;
+		struct swcr_data **newsessions;
+
 		if (swcr_sessions == NULL) {
 			i = 1; /* We leave swcr_sessions[0] empty */
-			swcr_sesnum = CRYPTO_SW_SESSIONS;
+			newnum = CRYPTO_SW_SESSIONS;
 		} else
-			swcr_sesnum *= 2;
+			newnum = swcr_sesnum *= 2;
 
-		swd = kmem_zalloc(swcr_sesnum * sizeof(struct swcr_data *),
+		newsessions = kmem_zalloc(newnum * sizeof(struct swcr_data *),
 		KM_NOSLEEP);
-		if (swd == NULL) {
-			/* Reset session number */
-			if (swcr_sesnum == CRYPTO_SW_SESSIONS)
-swcr_sesnum = 0;
-			else
-swcr_sesnum /= 2;
+		if (newsessions == NULL) {
 			return ENOBUFS;
 		}
 
 		/* Copy existing sessions */
 		if (swcr_sessions) {
-			memcpy(swd, swcr_sessions,
-			(swcr_sesnum / 2) * sizeof(struct swcr_data *));
+			memcpy(newsessions, swcr_sessions,
+			swcr_sesnum * sizeof(struct swcr_data *));
 			kmem_free(swcr_sessions,
-(swcr_sesnum / 2) * sizeof(struct swcr_data *));
+			swcr_sesnum * sizeof(struct swcr_data *));
 		}
 
-		swcr_sessions = swd;
+		swcr_sesnum = newnum;
+		swcr_sessions = newsessions;
 	}
 
 	swd = _sessions[i];



CVS commit: src/sys/opencrypto

2021-04-04 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr  5 01:22:22 UTC 2021

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
use kmem_{z,}alloc() instead of malloc()


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.57 src/sys/opencrypto/cryptosoft.c:1.58
--- src/sys/opencrypto/cryptosoft.c:1.57	Sat Jul  4 18:07:31 2020
+++ src/sys/opencrypto/cryptosoft.c	Mon Apr  5 01:22:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.57 2020/07/04 18:07:31 riastradh Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.58 2021/04/05 01:22:22 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,11 +24,11 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.57 2020/07/04 18:07:31 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.58 2021/04/05 01:22:22 knakahara Exp $");
 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 #include 
@@ -711,7 +711,7 @@ swcr_compdec(struct cryptodesc *crd, con
 	 * copy in a buffer.
 	 */
 
-	data = malloc(crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
+	data = kmem_alloc(crd->crd_len, KM_NOSLEEP);
 	if (data == NULL)
 		return (EINVAL);
 	COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
@@ -722,7 +722,7 @@ swcr_compdec(struct cryptodesc *crd, con
 		result = cxf->decompress(data, crd->crd_len, ,
 	 *res_size);
 
-	free(data, M_CRYPTO_DATA);
+	kmem_free(data, crd->crd_len);
 	if (result == 0)
 		return EINVAL;
 
@@ -781,8 +781,8 @@ swcr_newsession(void *arg, u_int32_t *si
 		} else
 			swcr_sesnum *= 2;
 
-		swd = malloc(swcr_sesnum * sizeof(struct swcr_data *),
-		M_CRYPTO_DATA, M_NOWAIT);
+		swd = kmem_zalloc(swcr_sesnum * sizeof(struct swcr_data *),
+		KM_NOSLEEP);
 		if (swd == NULL) {
 			/* Reset session number */
 			if (swcr_sesnum == CRYPTO_SW_SESSIONS)
@@ -792,13 +792,12 @@ swcr_newsession(void *arg, u_int32_t *si
 			return ENOBUFS;
 		}
 
-		memset(swd, 0, swcr_sesnum * sizeof(struct swcr_data *));
-
 		/* Copy existing sessions */
 		if (swcr_sessions) {
 			memcpy(swd, swcr_sessions,
 			(swcr_sesnum / 2) * sizeof(struct swcr_data *));
-			free(swcr_sessions, M_CRYPTO_DATA);
+			kmem_free(swcr_sessions,
+(swcr_sesnum / 2) * sizeof(struct swcr_data *));
 		}
 
 		swcr_sessions = swd;
@@ -808,12 +807,11 @@ swcr_newsession(void *arg, u_int32_t *si
 	*sid = i;
 
 	while (cri) {
-		*swd = malloc(sizeof **swd, M_CRYPTO_DATA, M_NOWAIT);
+		*swd = kmem_zalloc(sizeof **swd, KM_NOSLEEP);
 		if (*swd == NULL) {
 			swcr_freesession(NULL, i);
 			return ENOBUFS;
 		}
-		memset(*swd, 0, sizeof(struct swcr_data));
 
 		switch (cri->cri_alg) {
 		case CRYPTO_DES_CBC:



CVS commit: src/sys/opencrypto

2020-07-04 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Jul  4 18:07:31 UTC 2020

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
Fix kmem_free size in recent malloc->kmem conversion.

Should address this bracket report that has my name all over it:

https://mail-index.netbsd.org/current-users/2020/07/04/msg039059.html


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.56 src/sys/opencrypto/cryptosoft.c:1.57
--- src/sys/opencrypto/cryptosoft.c:1.56	Mon Jun 29 23:34:48 2020
+++ src/sys/opencrypto/cryptosoft.c	Sat Jul  4 18:07:31 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.56 2020/06/29 23:34:48 riastradh Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.57 2020/07/04 18:07:31 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.56 2020/06/29 23:34:48 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.57 2020/07/04 18:07:31 riastradh Exp $");
 
 #include 
 #include 
@@ -1096,7 +1096,7 @@ swcr_freesession(void *arg, u_int64_t ti
 			}
 			if (swd->sw_octx) {
 explicit_memset(swd->sw_octx, 0, swd->sw_klen);
-kmem_free(swd->sw_octx, axf->ctxsize);
+kmem_free(swd->sw_octx, swd->sw_klen);
 			}
 			break;
 



CVS commit: src/sys/opencrypto

2020-06-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Jun 29 23:34:48 UTC 2020

Modified Files:
src/sys/opencrypto: aesxcbcmac.c aesxcbcmac.h cryptosoft.c
cryptosoft_xform.c files.opencrypto gmac.c gmac.h xform.c

Log Message:
opencrypto: Switch from legacy rijndael API to new aes API.

While here, apply various rijndael->aes renames, reduce the size
of aesxcbc_ctx by 480 bytes, and convert some malloc->kmem.

Leave in the symbol enc_xform_rijndael128 for now, though, so this
doesn't break any kernel ABI.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/opencrypto/aesxcbcmac.c \
src/sys/opencrypto/gmac.h
cvs rdiff -u -r1.1 -r1.2 src/sys/opencrypto/aesxcbcmac.h
cvs rdiff -u -r1.55 -r1.56 src/sys/opencrypto/cryptosoft.c
cvs rdiff -u -r1.28 -r1.29 src/sys/opencrypto/cryptosoft_xform.c
cvs rdiff -u -r1.29 -r1.30 src/sys/opencrypto/files.opencrypto \
src/sys/opencrypto/xform.c
cvs rdiff -u -r1.3 -r1.4 src/sys/opencrypto/gmac.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/opencrypto/aesxcbcmac.c
diff -u src/sys/opencrypto/aesxcbcmac.c:1.2 src/sys/opencrypto/aesxcbcmac.c:1.3
--- src/sys/opencrypto/aesxcbcmac.c:1.2	Mon Sep 26 14:50:54 2016
+++ src/sys/opencrypto/aesxcbcmac.c	Mon Jun 29 23:34:48 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: aesxcbcmac.c,v 1.2 2016/09/26 14:50:54 christos Exp $ */
+/* $NetBSD: aesxcbcmac.c,v 1.3 2020/06/29 23:34:48 riastradh Exp $ */
 
 /*
  * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
@@ -30,11 +30,12 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.2 2016/09/26 14:50:54 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.3 2020/06/29 23:34:48 riastradh Exp $");
 
 #include 
 #include 
-#include 
+
+#include 
 
 #include 
 
@@ -47,24 +48,31 @@ aes_xcbc_mac_init(void *vctx, const uint
 	{ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
 	static const uint8_t k3seed[AES_BLOCKSIZE] =
 	{ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
-	u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
+	struct aesenc r_ks;
 	aesxcbc_ctx *ctx;
 	uint8_t k1[AES_BLOCKSIZE];
 
 	ctx = vctx;
 	memset(ctx, 0, sizeof(*ctx));
 
-	if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks, key, keylen * 8)) == 0)
-		return -1;
-	rijndaelEncrypt(r_ks, ctx->r_nr, k1seed, k1);
-	rijndaelEncrypt(r_ks, ctx->r_nr, k2seed, ctx->k2);
-	rijndaelEncrypt(r_ks, ctx->r_nr, k3seed, ctx->k3);
-	if (rijndaelKeySetupEnc(ctx->r_k1s, k1, AES_BLOCKSIZE * 8) == 0)
-		return -1;
-	if (rijndaelKeySetupEnc(ctx->r_k2s, ctx->k2, AES_BLOCKSIZE * 8) == 0)
-		return -1;
-	if (rijndaelKeySetupEnc(ctx->r_k3s, ctx->k3, AES_BLOCKSIZE * 8) == 0)
-		return -1;
+	switch (keylen) {
+	case 16:
+		ctx->r_nr = aes_setenckey128(_ks, key);
+		break;
+	case 24:
+		ctx->r_nr = aes_setenckey192(_ks, key);
+		break;
+	case 32:
+		ctx->r_nr = aes_setenckey256(_ks, key);
+		break;
+	}
+	aes_enc(_ks, k1seed, k1, ctx->r_nr);
+	aes_enc(_ks, k2seed, ctx->k2, ctx->r_nr);
+	aes_enc(_ks, k3seed, ctx->k3, ctx->r_nr);
+	aes_setenckey128(>r_k1s, k1);
+
+	explicit_memset(_ks, 0, sizeof(r_ks));
+	explicit_memset(k1, 0, sizeof(k1));
 
 	return 0;
 }
@@ -83,7 +91,7 @@ aes_xcbc_mac_loop(void *vctx, const uint
 	if (ctx->buflen == sizeof(ctx->buf)) {
 		for (i = 0; i < sizeof(ctx->e); i++)
 			ctx->buf[i] ^= ctx->e[i];
-		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
+		aes_enc(>r_k1s, ctx->buf, ctx->e, ctx->r_nr);
 		ctx->buflen = 0;
 	}
 	if (ctx->buflen + len < sizeof(ctx->buf)) {
@@ -96,7 +104,7 @@ aes_xcbc_mac_loop(void *vctx, const uint
 		sizeof(ctx->buf) - ctx->buflen);
 		for (i = 0; i < sizeof(ctx->e); i++)
 			ctx->buf[i] ^= ctx->e[i];
-		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
+		aes_enc(>r_k1s, ctx->buf, ctx->e, ctx->r_nr);
 		addr += sizeof(ctx->buf) - ctx->buflen;
 		ctx->buflen = 0;
 	}
@@ -105,7 +113,7 @@ aes_xcbc_mac_loop(void *vctx, const uint
 		memcpy(buf, addr, AES_BLOCKSIZE);
 		for (i = 0; i < sizeof(buf); i++)
 			buf[i] ^= ctx->e[i];
-		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, buf, ctx->e);
+		aes_enc(>r_k1s, buf, ctx->e, ctx->r_nr);
 		addr += AES_BLOCKSIZE;
 	}
 	if (addr < ep) {
@@ -129,7 +137,7 @@ aes_xcbc_mac_result(uint8_t *addr, void 
 			ctx->buf[i] ^= ctx->e[i];
 			ctx->buf[i] ^= ctx->k2[i];
 		}
-		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
+		aes_enc(>r_k1s, ctx->buf, digest, ctx->r_nr);
 	} else {
 		for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
 			ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
@@ -137,7 +145,7 @@ aes_xcbc_mac_result(uint8_t *addr, void 
 			ctx->buf[i] ^= ctx->e[i];
 			ctx->buf[i] ^= ctx->k3[i];
 		}
-		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
+		aes_enc(>r_k1s, ctx->buf, digest, ctx->r_nr);
 	}
 
 	memcpy(addr, digest, sizeof(digest));
Index: src/sys/opencrypto/gmac.h
diff -u src/sys/opencrypto/gmac.h:1.2 src/sys/opencrypto/gmac.h:1.3
--- src/sys/opencrypto/gmac.h:1.2	Thu Jun  9 14:47:42 2011
+++ 

CVS commit: src/sys/opencrypto

2020-06-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Jun 14 23:23:55 UTC 2020

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
swcrypto(4): Simplify iv generation logic with cprng_fast.


To generate a diff of this commit:
cvs rdiff -u -r1.54 -r1.55 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.54 src/sys/opencrypto/cryptosoft.c:1.55
--- src/sys/opencrypto/cryptosoft.c:1.54	Sat Oct 12 00:49:30 2019
+++ src/sys/opencrypto/cryptosoft.c	Sun Jun 14 23:23:55 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.54 2019/10/12 00:49:30 christos Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.55 2020/06/14 23:23:55 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.54 2019/10/12 00:49:30 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.55 2020/06/14 23:23:55 riastradh Exp $");
 
 #include 
 #include 
@@ -112,25 +112,7 @@ swcr_encdec(struct cryptodesc *crd, cons
 		} else if (exf->reinit) {
 			exf->reinit(sw->sw_kschedule, 0, iv);
 		} else {
-			/* Get random IV */
-			for (i = 0;
-			i + sizeof (u_int32_t) <= EALG_MAX_BLOCK_LEN;
-			i += sizeof (u_int32_t)) {
-u_int32_t temp = cprng_fast32();
-
-memcpy(iv + i, , sizeof(u_int32_t));
-			}
-			/*
-			 * What if the block size is not a multiple
-			 * of sizeof (u_int32_t), which is the size of
-			 * what arc4random() returns ?
-			 */
-			if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
-u_int32_t temp = cprng_fast32();
-
-bcopy (, iv + i,
-EALG_MAX_BLOCK_LEN - i);
-			}
+			cprng_fast(iv, EALG_MAX_BLOCK_LEN);
 		}
 
 		/* Do we need to write the IV */



CVS commit: src/sys/opencrypto

2020-04-08 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Wed Apr  8 15:27:18 UTC 2020

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
Revert previous change to use SYSCTL_SETUP since it breaks on macppc.

For some reason, the crypto module fails to link, and this results in
opencrypto sysctl failures.

Should resolve PR kern/55154


To generate a diff of this commit:
cvs rdiff -u -r1.113 -r1.114 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.113 src/sys/opencrypto/crypto.c:1.114
--- src/sys/opencrypto/crypto.c:1.113	Mon Mar 16 21:20:12 2020
+++ src/sys/opencrypto/crypto.c	Wed Apr  8 15:27:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.113 2020/03/16 21:20:12 pgoyette Exp $ */
+/*	$NetBSD: crypto.c,v 1.114 2020/04/08 15:27:18 pgoyette Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.113 2020/03/16 21:20:12 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.114 2020/04/08 15:27:18 pgoyette Exp $");
 
 #include 
 #include 
@@ -383,7 +383,8 @@ int	crypto_userasymcrypto = 1;	/* userla
  */
 int	crypto_devallowsoft = 1;	/* only use hardware crypto */
 
-SYSCTL_SETUP(sysctl_opencrypto_setup, "opencrypto sysctl")
+static void
+sysctl_opencrypto_setup(struct sysctllog **clog)
 {
 	const struct sysctlnode *ocnode;
 	const struct sysctlnode *retqnode, *retkqnode;
@@ -518,6 +519,8 @@ static struct cryptostats cryptostats;
 static	int crypto_timing = 0;
 #endif
 
+static struct sysctllog *sysctl_opencrypto_clog;
+
 static void
 crypto_crp_ret_qs_init(void)
 {
@@ -584,6 +587,8 @@ crypto_init0(void)
 		return crypto_destroy(false);
 	}
 
+	sysctl_opencrypto_setup(_opencrypto_clog);
+
 	return 0;
 }
 
@@ -650,6 +655,9 @@ crypto_destroy(bool exit_kthread)
 		}
 	}
 
+	if (sysctl_opencrypto_clog != NULL)
+		sysctl_teardown(_opencrypto_clog);
+
 	if (crypto_ret_si != NULL)
 		softint_disestablish(crypto_ret_si);
 



CVS commit: src/sys/opencrypto

2020-02-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Feb  1 13:48:09 UTC 2020

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
softint_disestablish does xc_barrier(0) for us already.


To generate a diff of this commit:
cvs rdiff -u -r1.111 -r1.112 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.111 src/sys/opencrypto/crypto.c:1.112
--- src/sys/opencrypto/crypto.c:1.111	Sat Feb  1 12:54:30 2020
+++ src/sys/opencrypto/crypto.c	Sat Feb  1 13:48:08 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.111 2020/02/01 12:54:30 riastradh Exp $ */
+/*	$NetBSD: crypto.c,v 1.112 2020/02/01 13:48:08 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.111 2020/02/01 12:54:30 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.112 2020/02/01 13:48:08 riastradh Exp $");
 
 #include 
 #include 
@@ -644,10 +644,7 @@ crypto_destroy(bool exit_kthread)
 		 * prohibit touch crypto_drivers[] and each element after here.
 		 */
 
-		/*
-		 * Ensure cryptoret_softint() is never scheduled and then wait
-		 * for last softint_execute().
-		 */
+		/* Ensure cryptoret_softint() is never scheduled again.  */
 		for (i = 0; i < ncpu; i++) {
 			struct crypto_crp_ret_qs *qs;
 			struct cpu_info *ci = cpu_lookup(i);
@@ -656,7 +653,6 @@ crypto_destroy(bool exit_kthread)
 			qs->crp_ret_q_exit_flag = true;
 			crypto_put_crp_ret_qs(ci);
 		}
-		xc_barrier(0);
 	}
 
 	if (sysctl_opencrypto_clog != NULL)



CVS commit: src/sys/opencrypto

2020-02-01 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Feb  1 12:54:30 UTC 2020

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
Switch opencrypto to percpu_create.

Can't sleep for allocation in percpu_foreach.


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.110 src/sys/opencrypto/crypto.c:1.111
--- src/sys/opencrypto/crypto.c:1.110	Sun Oct  6 15:11:17 2019
+++ src/sys/opencrypto/crypto.c	Sat Feb  1 12:54:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.110 2019/10/06 15:11:17 uwe Exp $ */
+/*	$NetBSD: crypto.c,v 1.111 2020/02/01 12:54:30 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.110 2019/10/06 15:11:17 uwe Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.111 2020/02/01 12:54:30 riastradh Exp $");
 
 #include 
 #include 
@@ -562,8 +562,8 @@ crypto_init0(void)
 	cryptkop_cache = pool_cache_init(sizeof(struct cryptkop),
 	coherency_unit, 0, 0, "cryptkop", NULL, IPL_NET, NULL, NULL, NULL);
 
-	crypto_crp_qs_percpu = percpu_alloc(sizeof(struct crypto_crp_qs));
-	percpu_foreach(crypto_crp_qs_percpu, crypto_crp_qs_init_pc, NULL);
+	crypto_crp_qs_percpu = percpu_create(sizeof(struct crypto_crp_qs),
+	crypto_crp_qs_init_pc, /*XXX*/NULL, NULL);
 
 	crypto_crp_ret_qs_init();
 



CVS commit: src/sys/opencrypto

2020-01-27 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Jan 27 17:11:27 UTC 2020

Modified Files:
src/sys/opencrypto: compat_crypto_50.c

Log Message:
Remove left-over #includes


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/opencrypto/compat_crypto_50.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/opencrypto/compat_crypto_50.c
diff -u src/sys/opencrypto/compat_crypto_50.c:1.1 src/sys/opencrypto/compat_crypto_50.c:1.2
--- src/sys/opencrypto/compat_crypto_50.c:1.1	Mon Jan 27 17:09:17 2020
+++ src/sys/opencrypto/compat_crypto_50.c	Mon Jan 27 17:11:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: compat_crypto_50.c,v 1.1 2020/01/27 17:09:17 pgoyette Exp $ */
+/*	$NetBSD: compat_crypto_50.c,v 1.2 2020/01/27 17:11:27 pgoyette Exp $ */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -30,28 +30,10 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: compat_crypto_50.c,v 1.1 2020/01/27 17:09:17 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: compat_crypto_50.c,v 1.2 2020/01/27 17:11:27 pgoyette Exp $");
 
 #include 
 #include 
-/* XXX PRG
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-XXX PRG */
 #include  
 #include 
 



CVS commit: src/sys/opencrypto

2020-01-26 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Jan 27 02:56:15 UTC 2020

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
If we get an error from devsw_attach(), don't destroy the error value,
since we need to return it to our caller.  While we're here, improve
the value of the debug message by actually printing the error value.


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.103 src/sys/opencrypto/cryptodev.c:1.104
--- src/sys/opencrypto/cryptodev.c:1.103	Thu Jan 16 15:29:24 2020
+++ src/sys/opencrypto/cryptodev.c	Mon Jan 27 02:56:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.103 2020/01/16 15:29:24 christos Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.104 2020/01/27 02:56:15 pgoyette Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.103 2020/01/16 15:29:24 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.104 2020/01/27 02:56:15 pgoyette Exp $");
 
 #include 
 #include 
@@ -2201,6 +2201,7 @@ crypto_modcmd(modcmd_t cmd, void *arg)
 {
 	int error = 0;
 #ifdef _MODULE
+	int error2;
 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
 #endif
 
@@ -2235,14 +2236,14 @@ crypto_modcmd(modcmd_t cmd, void *arg)
 		error = devsw_attach(crypto_cd.cd_name, NULL, ,
 		_cdevsw, );
 		if (error) {
-			error = config_cfdata_detach(crypto_cfdata);
-			if (error) {
-return error;
+			error2 = config_cfdata_detach(crypto_cfdata);
+			if (error2) {
+return error2;
 			}
 			config_cfattach_detach(crypto_cd.cd_name, _ca);
 			config_cfdriver_detach(_cd);
-			aprint_error("%s: unable to register devsw\n",
-crypto_cd.cd_name);
+			aprint_error("%s: unable to register devsw, error %d\n",
+crypto_cd.cd_name, error);
 
 			return error;
 		}



CVS commit: src/sys/opencrypto

2020-01-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Jan 16 15:29:24 UTC 2020

Modified Files:
src/sys/opencrypto: cryptodev.c ocryptodev.c

Log Message:
Initialize the session variable to an impossible session to prevent compiler
warnings.


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.14 -r1.15 src/sys/opencrypto/ocryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.102 src/sys/opencrypto/cryptodev.c:1.103
--- src/sys/opencrypto/cryptodev.c:1.102	Fri Nov 29 03:30:30 2019
+++ src/sys/opencrypto/cryptodev.c	Thu Jan 16 10:29:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.102 2019/11/29 08:30:30 hikaru Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.103 2020/01/16 15:29:24 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.102 2019/11/29 08:30:30 hikaru Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.103 2020/01/16 15:29:24 christos Exp $");
 
 #include 
 #include 
@@ -1787,6 +1787,7 @@ cryptodev_msession(struct fcrypt *fcr, s
 		s_op.key =		sn_ops->key;
 		s_op.mackeylen =	sn_ops->mackeylen;
 		s_op.mackey =		sn_ops->mackey;
+		s_op.ses =		~0;
 
 		sn_ops->status = cryptodev_session(fcr, _op);
 

Index: src/sys/opencrypto/ocryptodev.c
diff -u src/sys/opencrypto/ocryptodev.c:1.14 src/sys/opencrypto/ocryptodev.c:1.15
--- src/sys/opencrypto/ocryptodev.c:1.14	Wed Dec 11 21:15:43 2019
+++ src/sys/opencrypto/ocryptodev.c	Thu Jan 16 10:29:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ocryptodev.c,v 1.14 2019/12/12 02:15:43 pgoyette Exp $ */
+/*	$NetBSD: ocryptodev.c,v 1.15 2020/01/16 15:29:24 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.14 2019/12/12 02:15:43 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.15 2020/01/16 15:29:24 christos Exp $");
 
 #include 
 #include 
@@ -279,6 +279,7 @@ ocryptodev_msession(struct fcrypt *fcr, 
 		os_op.key =		osn_ops->key;
 		os_op.mackeylen =	osn_ops->mackeylen;
 		os_op.mackey =		osn_ops->mackey;
+		os_op.ses =		~0;
 
 		osn_ops->status = ocryptodev_session(fcr, _op);
 		osn_ops->ses =		os_op.ses;



CVS commit: src/sys/opencrypto

2018-01-08 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jan  8 23:34:56 UTC 2018

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
Fix PR kern/52910. Reported and implemented a patch by Sevan Janiyan, thanks.


To generate a diff of this commit:
cvs rdiff -u -r1.104 -r1.105 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.104 src/sys/opencrypto/crypto.c:1.105
--- src/sys/opencrypto/crypto.c:1.104	Mon Jan  8 23:33:40 2018
+++ src/sys/opencrypto/crypto.c	Mon Jan  8 23:34:56 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.104 2018/01/08 23:33:40 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.105 2018/01/08 23:34:56 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.104 2018/01/08 23:33:40 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.105 2018/01/08 23:34:56 knakahara Exp $");
 
 #include 
 #include 
@@ -360,7 +360,7 @@ sysctl_opencrypto_kq_maxlen(SYSCTLFN_ARG
 }
 
 /*
- * Crypto op and desciptor data structures are allocated
+ * Crypto op and descriptor data structures are allocated
  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
  */
 static pool_cache_t cryptop_cache;



CVS commit: src/sys/opencrypto

2017-11-14 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Nov 14 14:29:33 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
check results of pool_prime.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.95 src/sys/opencrypto/cryptodev.c:1.96
--- src/sys/opencrypto/cryptodev.c:1.95	Thu Jun 15 08:41:18 2017
+++ src/sys/opencrypto/cryptodev.c	Tue Nov 14 09:29:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.95 2017/06/15 12:41:18 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.96 2017/11/14 14:29:33 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.95 2017/06/15 12:41:18 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.96 2017/11/14 14:29:33 christos Exp $");
 
 #include 
 #include 
@@ -2110,6 +2110,8 @@ cryptof_poll(struct file *fp, int events
 void
 cryptoattach(int num)
 {
+	int error;
+
 	crypto_init();
 
 	mutex_init(_mtx, MUTEX_DEFAULT, IPL_NONE);
@@ -2126,8 +2128,9 @@ cryptoattach(int num)
 	 * the negotiation, plus HMAC_SHA1 for the actual SSL records,
 	 * consuming one session here for each algorithm.
 	 */
-	pool_prime(, 64);
-	pool_prime(, 64 * 5);
+	if ((error = pool_prime(, 64)) != 0 ||
+	(error = pool_prime(, 64 * 5)) != 0)
+		panic("%s: can't prime pool: %d", __func__, error);
 }
 
 void	crypto_attach(device_t, device_t, void *);



CVS commit: src/sys/opencrypto

2017-11-09 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Nov  9 22:20:26 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
use PR_NOWAIT.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.101 src/sys/opencrypto/crypto.c:1.102
--- src/sys/opencrypto/crypto.c:1.101	Thu Sep 21 23:04:06 2017
+++ src/sys/opencrypto/crypto.c	Thu Nov  9 17:20:25 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.101 2017/09/22 03:04:06 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.102 2017/11/09 22:20:25 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.101 2017/09/22 03:04:06 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.102 2017/11/09 22:20:25 christos Exp $");
 
 #include 
 #include 
@@ -1655,14 +1655,14 @@ crypto_getreq(int num)
 	}
 	crypto_put_crp_ret_qs(curcpu());
 
-	crp = pool_cache_get(cryptop_cache, 0);
+	crp = pool_cache_get(cryptop_cache, PR_NOWAIT);
 	if (crp == NULL) {
 		return NULL;
 	}
 	memset(crp, 0, sizeof(struct cryptop));
 
 	while (num--) {
-		crd = pool_cache_get(cryptodesc_cache, 0);
+		crd = pool_cache_get(cryptodesc_cache, PR_NOWAIT);
 		if (crd == NULL) {
 			crypto_freereq(crp);
 			return NULL;



CVS commit: src/sys/opencrypto

2017-09-21 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Sep 22 03:04:06 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix opencrypto(9) part of PR kern/52515

percpu data use pointers to TAILQ instead of TAILQ itself.


To generate a diff of this commit:
cvs rdiff -u -r1.100 -r1.101 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.100 src/sys/opencrypto/crypto.c:1.101
--- src/sys/opencrypto/crypto.c:1.100	Mon Jul 31 04:25:45 2017
+++ src/sys/opencrypto/crypto.c	Fri Sep 22 03:04:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.100 2017/07/31 04:25:45 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.101 2017/09/22 03:04:06 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.100 2017/07/31 04:25:45 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.101 2017/09/22 03:04:06 knakahara Exp $");
 
 #include 
 #include 
@@ -105,8 +105,8 @@ static	void *crypto_ret_si;
 TAILQ_HEAD(crypto_crp_q, cryptop);
 TAILQ_HEAD(crypto_crp_kq, cryptkop);
 struct crypto_crp_qs {
-	struct crypto_crp_q crp_q;
-	struct crypto_crp_kq crp_kq;
+	struct crypto_crp_q *crp_q;
+	struct crypto_crp_kq *crp_kq;
 };
 static percpu_t *crypto_crp_qs_percpu;
 
@@ -136,7 +136,7 @@ crypto_crp_q_is_busy_pc(void *p, void *a
 	struct crypto_crp_qs *qs_pc = p;
 	bool *isempty = arg;
 
-	if (!TAILQ_EMPTY(_pc->crp_q) || !TAILQ_EMPTY(_pc->crp_kq))
+	if (!TAILQ_EMPTY(qs_pc->crp_q) || !TAILQ_EMPTY(qs_pc->crp_kq))
 		*isempty = true;
 }
 
@@ -145,8 +145,11 @@ crypto_crp_qs_init_pc(void *p, void *arg
 {
 	struct crypto_crp_qs *qs = p;
 
-	TAILQ_INIT(>crp_q);
-	TAILQ_INIT(>crp_kq);
+	qs->crp_q = kmem_alloc(sizeof(struct crypto_crp_q), KM_SLEEP);
+	qs->crp_kq = kmem_alloc(sizeof(struct crypto_crp_kq), KM_SLEEP);
+
+	TAILQ_INIT(qs->crp_q);
+	TAILQ_INIT(qs->crp_kq);
 }
 
 /*
@@ -1310,7 +1313,7 @@ crypto_dispatch(struct cryptop *crp)
 		 * don't care list order in batch job.
 		 */
 		crp_qs = crypto_get_crp_qs();
-		crp_q = _qs->crp_q;
+		crp_q = crp_qs->crp_q;
 		wasempty  = TAILQ_EMPTY(crp_q);
 		TAILQ_INSERT_TAIL(crp_q, crp, crp_next);
 		crypto_put_crp_qs();
@@ -1325,7 +1328,7 @@ crypto_dispatch(struct cryptop *crp)
 	}
 
 	crp_qs = crypto_get_crp_qs();
-	crp_q = _qs->crp_q;
+	crp_q = crp_qs->crp_q;
 	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(crp->crp_sid));
 	/*
 	 * TODO:
@@ -1402,7 +1405,7 @@ crypto_kdispatch(struct cryptkop *krp)
 	cryptostats.cs_kops++;
 
 	crp_qs = crypto_get_crp_qs();
-	crp_kq = _qs->crp_kq;
+	crp_kq = crp_qs->crp_kq;
 	cap = crypto_checkdriver_lock(krp->krp_hid);
 	/*
 	 * TODO:
@@ -1908,8 +1911,8 @@ cryptointr(void *arg __unused)
 
 	cryptostats.cs_intrs++;
 	crp_qs = crypto_get_crp_qs();
-	crp_q = _qs->crp_q;
-	crp_kq = _qs->crp_kq;
+	crp_q = crp_qs->crp_q;
+	crp_kq = crp_qs->crp_kq;
 	do {
 		/*
 		 * Find the first element in the queue that can be



CVS commit: src/sys/opencrypto

2017-07-30 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jul 31 04:25:45 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
use pool_cache(9) instead of pool(9) as they can be called concurrently.


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.99 src/sys/opencrypto/crypto.c:1.100
--- src/sys/opencrypto/crypto.c:1.99	Mon Jul 31 04:23:48 2017
+++ src/sys/opencrypto/crypto.c	Mon Jul 31 04:25:45 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.99 2017/07/31 04:23:48 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.100 2017/07/31 04:25:45 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.99 2017/07/31 04:23:48 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.100 2017/07/31 04:25:45 knakahara Exp $");
 
 #include 
 #include 
@@ -360,9 +360,9 @@ sysctl_opencrypto_kq_maxlen(SYSCTLFN_ARG
  * Crypto op and desciptor data structures are allocated
  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
  */
-struct pool cryptop_pool;
-struct pool cryptodesc_pool;
-struct pool cryptkop_pool;
+static pool_cache_t cryptop_cache;
+static pool_cache_t cryptodesc_cache;
+static pool_cache_t cryptkop_cache;
 
 int	crypto_usercrypto = 1;		/* userland may open /dev/crypto */
 int	crypto_userasymcrypto = 1;	/* userland may do asym crypto reqs */
@@ -571,12 +571,12 @@ crypto_init0(void)
 	int error;
 
 	mutex_init(_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
-	pool_init(_pool, sizeof(struct cryptop), 0, 0,  
-		  0, "cryptop", NULL, IPL_NET);
-	pool_init(_pool, sizeof(struct cryptodesc), 0, 0,
-		  0, "cryptodesc", NULL, IPL_NET);
-	pool_init(_pool, sizeof(struct cryptkop), 0, 0,
-		  0, "cryptkop", NULL, IPL_NET);
+	cryptop_cache = pool_cache_init(sizeof(struct cryptop),
+	coherency_unit, 0, 0, "cryptop", NULL, IPL_NET, NULL, NULL, NULL);
+	cryptodesc_cache = pool_cache_init(sizeof(struct cryptodesc),
+	coherency_unit, 0, 0, "cryptdesc", NULL, IPL_NET, NULL, NULL, NULL);
+	cryptkop_cache = pool_cache_init(sizeof(struct cryptkop),
+	coherency_unit, 0, 0, "cryptkop", NULL, IPL_NET, NULL, NULL, NULL);
 
 	crypto_crp_qs_percpu = percpu_alloc(sizeof(struct crypto_crp_qs));
 	percpu_foreach(crypto_crp_qs_percpu, crypto_crp_qs_init_pc, NULL);
@@ -702,9 +702,9 @@ crypto_destroy(bool exit_kthread)
 
 	percpu_free(crypto_crp_qs_percpu, sizeof(struct crypto_crp_qs));
 
-	pool_destroy(_pool);
-	pool_destroy(_pool);
-	pool_destroy(_pool);
+	pool_cache_destroy(cryptop_cache);
+	pool_cache_destroy(cryptodesc_cache);
+	pool_cache_destroy(cryptkop_cache);
 
 	mutex_destroy(_drv_mtx);
 
@@ -1624,9 +1624,9 @@ crypto_freereq(struct cryptop *crp)
 
 	while ((crd = crp->crp_desc) != NULL) {
 		crp->crp_desc = crd->crd_next;
-		pool_put(_pool, crd);
+		pool_cache_put(cryptodesc_cache, crd);
 	}
-	pool_put(_pool, crp);
+	pool_cache_put(cryptop_cache, crp);
 }
 
 /*
@@ -1652,14 +1652,14 @@ crypto_getreq(int num)
 	}
 	crypto_put_crp_ret_qs(curcpu());
 
-	crp = pool_get(_pool, 0);
+	crp = pool_cache_get(cryptop_cache, 0);
 	if (crp == NULL) {
 		return NULL;
 	}
 	memset(crp, 0, sizeof(struct cryptop));
 
 	while (num--) {
-		crd = pool_get(_pool, 0);
+		crd = pool_cache_get(cryptodesc_cache, 0);
 		if (crd == NULL) {
 			crypto_freereq(crp);
 			return NULL;
@@ -1691,7 +1691,7 @@ crypto_kfreereq(struct cryptkop *krp)
 		panic("crypto_kfreereq() freeing krp on RETQ\n");
 	}
 
-	pool_put(_pool, krp);
+	pool_cache_put(cryptkop_cache, krp);
 }
 
 /*
@@ -1717,7 +1717,7 @@ crypto_kgetreq(int num __unused, int prf
 	}
 	crypto_put_crp_ret_qs(curcpu());
 
-	krp = pool_get(_pool, prflags);
+	krp = pool_cache_get(cryptkop_cache, prflags);
 	if (krp == NULL) {
 		return NULL;
 	}



CVS commit: src/sys/opencrypto

2017-07-30 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jul 31 04:23:48 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
use kmem_alloc KPI instead of malloc KPI.


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.98 src/sys/opencrypto/crypto.c:1.99
--- src/sys/opencrypto/crypto.c:1.98	Mon Jul 31 04:21:59 2017
+++ src/sys/opencrypto/crypto.c	Mon Jul 31 04:23:48 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.98 2017/07/31 04:21:59 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.99 2017/07/31 04:23:48 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,12 +53,11 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.98 2017/07/31 04:21:59 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.99 2017/07/31 04:23:48 knakahara Exp $");
 
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -474,8 +473,6 @@ sysctl_opencrypto_setup(struct sysctllog
 		   CTL_CREATE, CTL_EOL);
 }
 
-MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
-
 /*
  * Synchronization: read carefully, this is non-trivial.
  *
@@ -590,8 +587,8 @@ crypto_init0(void)
 		return ENOMEM;
 	}
 
-	crypto_drivers = malloc(CRYPTO_DRIVERS_INITIAL *
-	sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
+	crypto_drivers = kmem_zalloc(CRYPTO_DRIVERS_INITIAL *
+	sizeof(struct cryptocap), KM_NOSLEEP);
 	if (crypto_drivers == NULL) {
 		printf("crypto_init: cannot malloc driver table\n");
 		return ENOMEM;
@@ -699,7 +696,8 @@ crypto_destroy(bool exit_kthread)
 
 	mutex_enter(_drv_mtx);
 	if (crypto_drivers != NULL)
-		free(crypto_drivers, M_CRYPTO_DATA);
+		kmem_free(crypto_drivers,
+		crypto_drivers_num * sizeof(struct cryptocap));
 	mutex_exit(_drv_mtx);
 
 	percpu_free(crypto_crp_qs_percpu, sizeof(struct crypto_crp_qs));
@@ -931,8 +929,8 @@ crypto_get_driverid(u_int32_t flags)
 			return -1;
 		}
 
-		newdrv = malloc(2 * crypto_drivers_num *
-		sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
+		newdrv = kmem_zalloc(2 * crypto_drivers_num *
+		sizeof(struct cryptocap), KM_NOSLEEP);
 		if (newdrv == NULL) {
 			mutex_exit(_drv_mtx);
 			printf("crypto: no space to expand driver table!\n");
@@ -941,10 +939,10 @@ crypto_get_driverid(u_int32_t flags)
 
 		memcpy(newdrv, crypto_drivers,
 		crypto_drivers_num * sizeof(struct cryptocap));
+		kmem_free(crypto_drivers,
+		crypto_drivers_num * sizeof(struct cryptocap));
 
 		crypto_drivers_num *= 2;
-
-		free(crypto_drivers, M_CRYPTO_DATA);
 		crypto_drivers = newdrv;
 
 		cap = crypto_checkdriver_uninit(i);



CVS commit: src/sys/opencrypto

2017-07-30 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jul 31 04:21:59 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
pack crypto_drivers variables to struct and add cacheline_aligned qualifier.


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.97 src/sys/opencrypto/crypto.c:1.98
--- src/sys/opencrypto/crypto.c:1.97	Mon Jul 31 04:19:26 2017
+++ src/sys/opencrypto/crypto.c	Mon Jul 31 04:21:59 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.97 2017/07/31 04:19:26 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.98 2017/07/31 04:21:59 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.97 2017/07/31 04:19:26 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.98 2017/07/31 04:21:59 knakahara Exp $");
 
 #include 
 #include 
@@ -85,10 +85,16 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
  * crypto_drivers table with crypto_get_driverid() and then registering
  * each algorithm they support with crypto_register() and crypto_kregister().
  */
-static kmutex_t crypto_drv_mtx;
 /* Don't directly access crypto_drivers[i], use crypto_checkdriver(i). */
-static	struct cryptocap *crypto_drivers;
-static	int crypto_drivers_num;
+static struct {
+	kmutex_t mtx;
+	int num;
+	struct cryptocap *list;
+} crypto_drv __cacheline_aligned;
+#define crypto_drv_mtx		(crypto_drv.mtx)
+#define crypto_drivers_num	(crypto_drv.num)
+#define crypto_drivers		(crypto_drv.list)
+
 static	void *crypto_q_si;
 static	void *crypto_ret_si;
 



CVS commit: src/sys/opencrypto

2017-07-30 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jul 31 04:19:26 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor: remove glue macros for FreeBSD code.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.96 src/sys/opencrypto/crypto.c:1.97
--- src/sys/opencrypto/crypto.c:1.96	Wed Jul 26 06:44:01 2017
+++ src/sys/opencrypto/crypto.c	Mon Jul 31 04:19:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.96 2017/07/26 06:44:01 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.97 2017/07/31 04:19:26 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.96 2017/07/26 06:44:01 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.97 2017/07/31 04:19:26 knakahara Exp $");
 
 #include 
 #include 
@@ -80,20 +80,6 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 			/* XXX for M_XDATA */
 
-/* below are kludges for residual code wrtitten to FreeBSD interfaces */
-  #define SWI_CRYPTO 17
-  #define register_swi(lvl, fn)  \
-  softint_establish(SOFTINT_NET|SOFTINT_MPSAFE, (void (*)(void *))fn, NULL)
-  #define unregister_swi(lvl, fn)  softint_disestablish(softintr_cookie)
-  #define setsoftcrypto(x)			\
-	do{	\
-		kpreempt_disable();		\
-		softint_schedule(x);		\
-		kpreempt_enable();		\
-	}while(0)
-
-int crypto_ret_q_check(struct cryptop *);
-
 /*
  * Crypto drivers register themselves by allocating a slot in the
  * crypto_drivers table with crypto_get_driverid() and then registering
@@ -103,8 +89,7 @@ static kmutex_t crypto_drv_mtx;
 /* Don't directly access crypto_drivers[i], use crypto_checkdriver(i). */
 static	struct cryptocap *crypto_drivers;
 static	int crypto_drivers_num;
-static	void *softintr_cookie;
-
+static	void *crypto_q_si;
 static	void *crypto_ret_si;
 
 /*
@@ -508,7 +493,7 @@ MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "
  *
  * This scheme is not intended for SMP machines.
  */
-static	void cryptointr(void);		/* swi thread to dispatch ops */
+static	void cryptointr(void *);	/* swi thread to dispatch ops */
 static	void cryptoret_softint(void *);	/* kernel thread for callbacks*/
 static	int crypto_destroy(bool);
 static	int crypto_invoke(struct cryptop *crp, int hint);
@@ -607,8 +592,8 @@ crypto_init0(void)
 	}
 	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
 
-	softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
-	if (softintr_cookie == NULL) {
+	crypto_q_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE, cryptointr, NULL);
+	if (crypto_q_si == NULL) {
 		printf("crypto_init: cannot establish request queue handler\n");
 		return crypto_destroy(false);
 	}
@@ -703,8 +688,8 @@ crypto_destroy(bool exit_kthread)
 	if (crypto_ret_si != NULL)
 		softint_disestablish(crypto_ret_si);
 
-	if (softintr_cookie != NULL)
-		unregister_swi(SWI_CRYPTO, cryptointr);
+	if (crypto_q_si != NULL)
+		softint_disestablish(crypto_q_si);
 
 	mutex_enter(_drv_mtx);
 	if (crypto_drivers != NULL)
@@ -1278,8 +1263,11 @@ crypto_unblock(u_int32_t driverid, int w
 		cap->cc_kqblocked = 0;
 	}
 	crypto_driver_unlock(cap);
-	if (needwakeup)
-		setsoftcrypto(softintr_cookie);
+	if (needwakeup) {
+		kpreempt_disable();
+		softint_schedule(crypto_q_si);
+		kpreempt_enable();
+	}
 
 	return 0;
 }
@@ -1323,8 +1311,11 @@ crypto_dispatch(struct cryptop *crp)
 		TAILQ_INSERT_TAIL(crp_q, crp, crp_next);
 		crypto_put_crp_qs();
 		crp_q = NULL;
-		if (wasempty)
-			setsoftcrypto(softintr_cookie);
+		if (wasempty) {
+			kpreempt_disable();
+			softint_schedule(crypto_q_si);
+			kpreempt_enable();
+		}
 
 		return 0;
 	}
@@ -1901,7 +1892,7 @@ unlock:		crypto_driver_unlock(cap);
  * Software interrupt thread to dispatch crypto requests.
  */
 static void
-cryptointr(void)
+cryptointr(void *arg __unused)
 {
 	struct cryptop *crp, *submit, *cnext;
 	struct cryptkop *krp, *knext;



CVS commit: src/sys/opencrypto

2017-07-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Jul 28 17:14:04 UTC 2017

Modified Files:
src/sys/opencrypto: ocryptodev.c

Log Message:
Avoid another userland-controlled integer overflow.

>From Ilja Van Sprundel.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/opencrypto/ocryptodev.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/opencrypto/ocryptodev.c
diff -u src/sys/opencrypto/ocryptodev.c:1.10 src/sys/opencrypto/ocryptodev.c:1.11
--- src/sys/opencrypto/ocryptodev.c:1.10	Fri Jul 28 14:16:29 2017
+++ src/sys/opencrypto/ocryptodev.c	Fri Jul 28 17:14:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $ */
+/*	$NetBSD: ocryptodev.c,v 1.11 2017/07/28 17:14:04 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.11 2017/07/28 17:14:04 riastradh Exp $");
 
 #include 
 #include 
@@ -127,6 +127,11 @@ ocryptof_ioctl(struct file *fp, u_long c
 		break;
 	case CIOCNGSESSION:
 		osgop = (struct ocrypt_sgop *)data;
+		if ((osgop->count <= 0) ||
+		(SIZE_MAX/sizeof(struct osession_n_op) < osgop->count)) {
+			error = EINVAL;
+			break;
+		}
 		osnop = kmem_alloc((osgop->count *
   sizeof(struct osession_n_op)), KM_SLEEP);
 		error = copyin(osgop->sessions, osnop, osgop->count *



CVS commit: src/sys/opencrypto

2017-07-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Jul 28 14:16:29 UTC 2017

Modified Files:
src/sys/opencrypto: ocryptodev.c

Log Message:
Avert userland-controlled integer overflow.

>From Ilja Van Sprundel.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/opencrypto/ocryptodev.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/opencrypto/ocryptodev.c
diff -u src/sys/opencrypto/ocryptodev.c:1.9 src/sys/opencrypto/ocryptodev.c:1.10
--- src/sys/opencrypto/ocryptodev.c:1.9	Fri Jul 28 14:13:56 2017
+++ src/sys/opencrypto/ocryptodev.c	Fri Jul 28 14:16:29 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: ocryptodev.c,v 1.9 2017/07/28 14:13:56 riastradh Exp $ */
+/*	$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.9 2017/07/28 14:13:56 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.10 2017/07/28 14:16:29 riastradh Exp $");
 
 #include 
 #include 
@@ -159,6 +159,11 @@ mbail:
 		break;
 	case OCIOCNCRYPTM:
 		omop = (struct ocrypt_mop *)data;
+		if ((omop->count <= 0) ||
+		(SIZE_MAX/sizeof(struct ocrypt_n_op) <= omop->count)) {
+			error = EINVAL;
+			break;
+		}
 		ocnop = kmem_alloc((omop->count * sizeof(struct ocrypt_n_op)),
 		KM_SLEEP);
 		error = copyin(omop->reqs, ocnop,



CVS commit: src/sys/opencrypto

2017-07-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Jul 28 14:13:56 UTC 2017

Modified Files:
src/sys/opencrypto: ocryptodev.c

Log Message:
Don't disclose uninitialized 32-bit word if cryptodev_session fails.

>From Ilja Van Sprundel.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/opencrypto/ocryptodev.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/opencrypto/ocryptodev.c
diff -u src/sys/opencrypto/ocryptodev.c:1.8 src/sys/opencrypto/ocryptodev.c:1.9
--- src/sys/opencrypto/ocryptodev.c:1.8	Fri Jun  2 09:46:57 2017
+++ src/sys/opencrypto/ocryptodev.c	Fri Jul 28 14:13:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: ocryptodev.c,v 1.8 2017/06/02 09:46:57 knakahara Exp $ */
+/*	$NetBSD: ocryptodev.c,v 1.9 2017/07/28 14:13:56 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -69,7 +69,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.8 2017/06/02 09:46:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ocryptodev.c,v 1.9 2017/07/28 14:13:56 riastradh Exp $");
 
 #include 
 #include 
@@ -245,8 +245,10 @@ ocryptodev_session(struct fcrypt *fcr, s
 	sop.mackeylen = osop->mackeylen;
 	sop.mackey = osop->mackey;
 	res = cryptodev_session(fcr, );
+	if (res)
+		return res;
 	osop->ses = sop.ses;
-	return res;
+	return 0;
 
 }
 



CVS commit: src/sys/opencrypto

2017-07-26 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jul 26 06:44:50 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.h

Log Message:
update locking notes of opencrypto(9)


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.38 src/sys/opencrypto/cryptodev.h:1.39
--- src/sys/opencrypto/cryptodev.h:1.38	Tue Jul 18 06:01:36 2017
+++ src/sys/opencrypto/cryptodev.h	Wed Jul 26 06:44:50 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.38 2017/07/18 06:01:36 knakahara Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.39 2017/07/26 06:44:50 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -670,12 +670,17 @@ extern int	cuio_getptr(struct uio *, int
  * + crypto_drivers[i] and its all members are protected by
  *   crypto_drivers[i].cc_lock (a spin lock)
  *   spin lock as crypto_unblock() can be called in interrupt context
- * + crp_q and crp_kq are procted by crypto_q_mtx (an adaptive lock)
- * + crp_ret_q, crp_ret_kq and crypto_exit_flag are protected by
- *   crypto_ret_q_mtx (a spin lock)
+ * + percpu'ed crp_q and crp_kq are procted by splsoftnet.
+ * + crp_ret_q, crp_ret_kq and crypto_exit_flag that are members of
+ *   struct crypto_crp_ret_qs are protected by crypto_crp_ret_qs.crp_ret_q_mtx
+ *   (a spin lock)
  *   spin lock as crypto_done() can be called in interrupt context
+ *   NOTE:
+ *   It is not known whether crypto_done()(in interrupt context) is called
+ *   in the same CPU as crypto_dispatch() is called.
+ *   So, struct crypto_crp_ret_qs cannot be percpu(9).
  *
  * Locking order:
- * - crypto_q_mtx => crypto_drv_mtx => crypto_drivers[i].cc_lock
+ * - crypto_drv_mtx => crypto_drivers[i].cc_lock
  */
 #endif /* _CRYPTO_CRYPTO_H_ */



CVS commit: src/sys/opencrypto

2017-07-26 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jul 26 06:44:01 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
divide crp_ret_{,k}q by CPU to avoid reordering.

update locking note later.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.95 src/sys/opencrypto/crypto.c:1.96
--- src/sys/opencrypto/crypto.c:1.95	Wed Jul 26 06:40:42 2017
+++ src/sys/opencrypto/crypto.c	Wed Jul 26 06:44:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.95 2017/07/26 06:40:42 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.96 2017/07/26 06:44:01 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.95 2017/07/26 06:40:42 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.96 2017/07/26 06:44:01 knakahara Exp $");
 
 #include 
 #include 
@@ -69,7 +69,9 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 
 #if defined(_KERNEL_OPT)
 #include "opt_ocf.h"
@@ -78,8 +80,6 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 			/* XXX for M_XDATA */
 
-static kmutex_t crypto_ret_q_mtx;
-
 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
   #define SWI_CRYPTO 17
   #define register_swi(lvl, fn)  \
@@ -104,7 +104,6 @@ static kmutex_t crypto_drv_mtx;
 static	struct cryptocap *crypto_drivers;
 static	int crypto_drivers_num;
 static	void *softintr_cookie;
-static	int crypto_exit_flag;
 
 static	void *crypto_ret_si;
 
@@ -166,50 +165,51 @@ crypto_crp_qs_init_pc(void *p, void *arg
  * but have two to avoid type futzing (cryptop vs. cryptkop).  See below
  * for how synchronization is handled.
  */
-static	TAILQ_HEAD(crprethead, cryptop) crp_ret_q =	/* callback queues */
-		TAILQ_HEAD_INITIALIZER(crp_ret_q);
-static	TAILQ_HEAD(krprethead, cryptkop) crp_ret_kq =
-		TAILQ_HEAD_INITIALIZER(crp_ret_kq);
-
-#define DEFINIT_CRYPTO_Q_LEN(name)		\
-	static int crypto_##name##_len = 0
-
-#define DEFINIT_CRYPTO_Q_DROPS(name)		\
-	static int crypto_##name##_drops = 0
+TAILQ_HEAD(crypto_crp_ret_q, cryptop);
+TAILQ_HEAD(crypto_crp_ret_kq, cryptkop);
+struct crypto_crp_ret_qs {
+	kmutex_t crp_ret_q_mtx;
+	bool crp_ret_q_exit_flag;
+
+	struct crypto_crp_ret_q crp_ret_q;
+	int crp_ret_q_len;
+	int crp_ret_q_maxlen; /* queue length limit. <=0 means unlimited. */
+	int crp_ret_q_drops;
+
+	struct crypto_crp_ret_kq crp_ret_kq;
+	int crp_ret_kq_len;
+	int crp_ret_kq_maxlen; /* queue length limit. <=0 means unlimited. */
+	int crp_ret_kq_drops;
+};
+struct crypto_crp_ret_qs **crypto_crp_ret_qs_list;
 
-#define DEFINIT_CRYPTO_Q_MAXLEN(name, defval)		\
-	static int crypto_##name##_maxlen = defval
 
-#define CRYPTO_Q_INC(name)			\
-	do {	\
-		crypto_##name##_len++;		\
-	} while(0);
+static inline struct crypto_crp_ret_qs *
+crypto_get_crp_ret_qs(struct cpu_info *ci)
+{
+	u_int cpuid;
+	struct crypto_crp_ret_qs *qs;
 
-#define CRYPTO_Q_DEC(name)			\
-	do {	\
-		crypto_##name##_len--;		\
-	} while(0);
+	KASSERT(ci != NULL);
 
-#define CRYPTO_Q_INC_DROPS(name)		\
-	do {	\
-		crypto_##name##_drops++;	\
-	} while(0);
+	cpuid = cpu_index(ci);
+	qs = crypto_crp_ret_qs_list[cpuid];
+	mutex_enter(>crp_ret_q_mtx);
+	return qs;
+}
 
-#define CRYPTO_Q_IS_FULL(name)	\
-	(crypto_##name##_maxlen > 0\
-	&& (crypto_##name##_len > crypto_##name##_maxlen))
+static inline void
+crypto_put_crp_ret_qs(struct cpu_info *ci)
+{
+	u_int cpuid;
+	struct crypto_crp_ret_qs *qs;
 
-/*
- * current queue length.
- */
-DEFINIT_CRYPTO_Q_LEN(crp_ret_q);
-DEFINIT_CRYPTO_Q_LEN(crp_ret_kq);
+	KASSERT(ci != NULL);
 
-/*
- * queue dropped count.
- */
-DEFINIT_CRYPTO_Q_DROPS(crp_ret_q);
-DEFINIT_CRYPTO_Q_DROPS(crp_ret_kq);
+	cpuid = cpu_index(ci);
+	qs = crypto_crp_ret_qs_list[cpuid];
+	mutex_exit(>crp_ret_q_mtx);
+}
 
 #ifndef CRYPTO_RET_Q_MAXLEN
 #define CRYPTO_RET_Q_MAXLEN 0
@@ -217,57 +217,152 @@ DEFINIT_CRYPTO_Q_DROPS(crp_ret_kq);
 #ifndef CRYPTO_RET_KQ_MAXLEN
 #define CRYPTO_RET_KQ_MAXLEN 0
 #endif
-/*
- * queue length limit.
- * default value is 0. <=0 means unlimited.
- */
-DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_q, CRYPTO_RET_Q_MAXLEN);
-DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_kq, CRYPTO_RET_KQ_MAXLEN);
 
-/*
- * TODO:
- * make percpu
- */
 static int
 sysctl_opencrypto_q_len(SYSCTLFN_ARGS)
 {
-	int error;
+	int error, len = 0;
+	struct sysctlnode node = *rnode;
+
+	for (int i = 0; i < ncpu; i++) {
+		struct crypto_crp_ret_qs *qs;
+		struct cpu_info *ci = cpu_lookup(i);
 
-	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
+		qs = crypto_get_crp_ret_qs(ci);
+		len += qs->crp_ret_q_len;
+		crypto_put_crp_ret_qs(ci);
+	}

CVS commit: src/sys/opencrypto

2017-07-26 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jul 26 06:40:43 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
make crp_{,k}q percpu to scale crypto_dispatch().

update locking note later.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.94 src/sys/opencrypto/crypto.c:1.95
--- src/sys/opencrypto/crypto.c:1.94	Thu Jul 20 23:07:12 2017
+++ src/sys/opencrypto/crypto.c	Wed Jul 26 06:40:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.94 2017/07/20 23:07:12 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.95 2017/07/26 06:40:42 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.94 2017/07/20 23:07:12 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.95 2017/07/26 06:40:42 knakahara Exp $");
 
 #include 
 #include 
@@ -69,6 +69,7 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 
 #include 
+#include 
 
 #if defined(_KERNEL_OPT)
 #include "opt_ocf.h"
@@ -77,7 +78,6 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 			/* XXX for M_XDATA */
 
-static kmutex_t crypto_q_mtx;
 static kmutex_t crypto_ret_q_mtx;
 
 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
@@ -113,10 +113,52 @@ static	void *crypto_ret_si;
  * cipher) operations and one for asymmetric (e.g. MOD) operations.
  * See below for how synchronization is handled.
  */
-static	TAILQ_HEAD(,cryptop) crp_q =		/* request queues */
-		TAILQ_HEAD_INITIALIZER(crp_q);
-static	TAILQ_HEAD(,cryptkop) crp_kq =
-		TAILQ_HEAD_INITIALIZER(crp_kq);
+TAILQ_HEAD(crypto_crp_q, cryptop);
+TAILQ_HEAD(crypto_crp_kq, cryptkop);
+struct crypto_crp_qs {
+	struct crypto_crp_q crp_q;
+	struct crypto_crp_kq crp_kq;
+};
+static percpu_t *crypto_crp_qs_percpu;
+
+static inline struct crypto_crp_qs *
+crypto_get_crp_qs(int *s)
+{
+
+	KASSERT(s != NULL);
+
+	*s = splsoftnet();
+	return percpu_getref(crypto_crp_qs_percpu);
+}
+
+static inline void
+crypto_put_crp_qs(int *s)
+{
+
+	KASSERT(s != NULL);
+
+	percpu_putref(crypto_crp_qs_percpu);
+	splx(*s);
+}
+
+static void
+crypto_crp_q_is_busy_pc(void *p, void *arg, struct cpu_info *ci __unused)
+{
+	struct crypto_crp_qs *qs_pc = p;
+	bool *isempty = arg;
+
+	if (!TAILQ_EMPTY(_pc->crp_q) || !TAILQ_EMPTY(_pc->crp_kq))
+		*isempty = true;
+}
+
+static void
+crypto_crp_qs_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
+{
+	struct crypto_crp_qs *qs = p;
+
+	TAILQ_INIT(>crp_q);
+	TAILQ_INIT(>crp_kq);
+}
 
 /*
  * There are two queues for processing completed crypto requests; one
@@ -397,7 +439,6 @@ crypto_init0(void)
 {
 
 	mutex_init(_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
-	mutex_init(_q_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
 	pool_init(_pool, sizeof(struct cryptop), 0, 0,  
 		  0, "cryptop", NULL, IPL_NET);
@@ -406,6 +447,9 @@ crypto_init0(void)
 	pool_init(_pool, sizeof(struct cryptkop), 0, 0,
 		  0, "cryptkop", NULL, IPL_NET);
 
+	crypto_crp_qs_percpu = percpu_alloc(sizeof(struct crypto_crp_qs));
+	percpu_foreach(crypto_crp_qs_percpu, crypto_crp_qs_init_pc, NULL);
+
 	crypto_drivers = malloc(CRYPTO_DRIVERS_INITIAL *
 	sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
 	if (crypto_drivers == NULL) {
@@ -462,14 +506,13 @@ crypto_destroy(bool exit_kthread)
 	if (exit_kthread) {
 		struct cryptocap *cap = NULL;
 		uint64_t where;
+		bool is_busy = false;
 
 		/* if we have any in-progress requests, don't unload */
-		mutex_enter(_q_mtx);
-		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq)) {
-			mutex_exit(_q_mtx);
+		percpu_foreach(crypto_crp_qs_percpu, crypto_crp_q_is_busy_pc,
+   _busy);
+		if (is_busy)
 			return EBUSY;
-		}
-		mutex_exit(_q_mtx);
 		/* FIXME:
 		 * prohibit enqueue to crp_q and crp_kq after here.
 		 */
@@ -514,12 +557,13 @@ crypto_destroy(bool exit_kthread)
 		free(crypto_drivers, M_CRYPTO_DATA);
 	mutex_exit(_drv_mtx);
 
+	percpu_free(crypto_crp_qs_percpu, sizeof(struct crypto_crp_qs));
+
 	pool_destroy(_pool);
 	pool_destroy(_pool);
 	pool_destroy(_pool);
 
 	mutex_destroy(_ret_q_mtx);
-	mutex_destroy(_q_mtx);
 	mutex_destroy(_drv_mtx);
 
 	return 0;
@@ -1094,8 +1138,10 @@ crypto_unblock(u_int32_t driverid, int w
 int
 crypto_dispatch(struct cryptop *crp)
 {
-	int result;
+	int result, s;
 	struct cryptocap *cap;
+	struct crypto_crp_qs *crp_qs;
+	struct crypto_crp_q *crp_q;
 
 	KASSERT(crp != NULL);
 
@@ -1118,17 +1164,20 @@ crypto_dispatch(struct cryptop *crp)
 		 *
 		 * don't care list order in batch job.
 		 */
-		mutex_enter(_q_mtx);
-		wasempty  = TAILQ_EMPTY(_q);
-		TAILQ_INSERT_TAIL(_q, crp, 

CVS commit: src/sys/opencrypto

2017-07-20 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jul 20 23:07:12 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix typo


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.93 src/sys/opencrypto/crypto.c:1.94
--- src/sys/opencrypto/crypto.c:1.93	Thu Jul 20 09:31:36 2017
+++ src/sys/opencrypto/crypto.c	Thu Jul 20 23:07:12 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.94 2017/07/20 23:07:12 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.94 2017/07/20 23:07:12 knakahara Exp $");
 
 #include 
 #include 
@@ -421,8 +421,8 @@ crypto_init0(void)
 	}
 
 	/*
-	 * Some encryption devices(such as mvcesa) is attached before
-	 * ipi_sysinit(). That causes assertion in ipi_register() as
+	 * Some encryption devices (such as mvcesa) are attached before
+	 * ipi_sysinit(). That causes an assertion in ipi_register() as
 	 * crypto_ret_si softint uses SOFTINT_RCPU.
 	 */
 	if (config_finalize_register(NULL, crypto_init_finalize) != 0) {



CVS commit: src/sys/opencrypto

2017-07-20 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jul 20 09:31:36 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix panic when using ecryption devices attached earlier than ipi_sysinit().

pointed out and tested by martin@n.o, thanks.


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.92 src/sys/opencrypto/crypto.c:1.93
--- src/sys/opencrypto/crypto.c:1.92	Tue Jul 18 06:01:36 2017
+++ src/sys/opencrypto/crypto.c	Thu Jul 20 09:31:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.93 2017/07/20 09:31:36 knakahara Exp $");
 
 #include 
 #include 
@@ -68,6 +68,7 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 
 #include 
+#include 
 
 #if defined(_KERNEL_OPT)
 #include "opt_ocf.h"
@@ -382,6 +383,8 @@ static void crypto_driver_lock(struct cr
 static void crypto_driver_unlock(struct cryptocap *);
 static void crypto_driver_clear(struct cryptocap *);
 
+static int crypto_init_finalize(device_t);
+
 static struct cryptostats cryptostats;
 #ifdef CRYPTO_TIMING
 static	int crypto_timing = 0;
@@ -417,10 +420,13 @@ crypto_init0(void)
 		return crypto_destroy(false);
 	}
 
-	crypto_ret_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE|SOFTINT_RCPU,
-	_softint, NULL);
-	if (crypto_ret_si == NULL) {
-		printf("crypto_init: cannot establish ret queue handler\n");
+	/*
+	 * Some encryption devices(such as mvcesa) is attached before
+	 * ipi_sysinit(). That causes assertion in ipi_register() as
+	 * crypto_ret_si softint uses SOFTINT_RCPU.
+	 */
+	if (config_finalize_register(NULL, crypto_init_finalize) != 0) {
+		printf("crypto_init: cannot register crypto_init_finalize\n");
 		return crypto_destroy(false);
 	}
 
@@ -429,6 +435,17 @@ crypto_init0(void)
 	return 0;
 }
 
+static int
+crypto_init_finalize(device_t self __unused)
+{
+
+	crypto_ret_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE|SOFTINT_RCPU,
+	_softint, NULL);
+	KASSERT(crypto_ret_si != NULL);
+
+	return 0;
+}
+
 int
 crypto_init(void)
 {



CVS commit: src/sys/opencrypto

2017-07-18 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jul 18 06:01:37 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.h

Log Message:
make cryptoret() context softint to balance dequeuing crypto_ret_q with 
enqueuing it.


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.37 -r1.38 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.91 src/sys/opencrypto/crypto.c:1.92
--- src/sys/opencrypto/crypto.c:1.91	Mon Jun 26 05:34:48 2017
+++ src/sys/opencrypto/crypto.c	Tue Jul 18 06:01:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.91 2017/06/26 05:34:48 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.91 2017/06/26 05:34:48 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.92 2017/07/18 06:01:36 knakahara Exp $");
 
 #include 
 #include 
@@ -67,6 +67,7 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 
 #include 
+#include 
 
 #if defined(_KERNEL_OPT)
 #include "opt_ocf.h"
@@ -77,7 +78,6 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 
 static kmutex_t crypto_q_mtx;
 static kmutex_t crypto_ret_q_mtx;
-static kcondvar_t cryptoret_cv;
 
 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
   #define SWI_CRYPTO 17
@@ -105,6 +105,8 @@ static	int crypto_drivers_num;
 static	void *softintr_cookie;
 static	int crypto_exit_flag;
 
+static	void *crypto_ret_si;
+
 /*
  * There are two queues for crypto requests; one for symmetric (e.g.
  * cipher) operations and one for asymmetric (e.g. MOD) operations.
@@ -368,8 +370,7 @@ MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "
  * This scheme is not intended for SMP machines.
  */
 static	void cryptointr(void);		/* swi thread to dispatch ops */
-static	void cryptoret(void);		/* kernel thread for callbacks*/
-static	struct lwp *cryptothread;
+static	void cryptoret_softint(void *);	/* kernel thread for callbacks*/
 static	int crypto_destroy(bool);
 static	int crypto_invoke(struct cryptop *crp, int hint);
 static	int crypto_kinvoke(struct cryptkop *krp, int hint);
@@ -391,12 +392,10 @@ static struct sysctllog *sysctl_opencryp
 static int
 crypto_init0(void)
 {
-	int error;
 
 	mutex_init(_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(_q_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
-	cv_init(_cv, "crypto_w");
 	pool_init(_pool, sizeof(struct cryptop), 0, 0,  
 		  0, "cryptop", NULL, IPL_NET);
 	pool_init(_pool, sizeof(struct cryptodesc), 0, 0,
@@ -413,11 +412,15 @@ crypto_init0(void)
 	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
 
 	softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
-	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
-	(void (*)(void *))cryptoret, NULL, , "cryptoret");
-	if (error) {
-		printf("crypto_init: cannot start cryptoret thread; error %d",
-			error);
+	if (softintr_cookie == NULL) {
+		printf("crypto_init: cannot establish request queue handler\n");
+		return crypto_destroy(false);
+	}
+
+	crypto_ret_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE|SOFTINT_RCPU,
+	_softint, NULL);
+	if (crypto_ret_si == NULL) {
+		printf("crypto_init: cannot establish ret queue handler\n");
 		return crypto_destroy(false);
 	}
 
@@ -441,6 +444,7 @@ crypto_destroy(bool exit_kthread)
 
 	if (exit_kthread) {
 		struct cryptocap *cap = NULL;
+		uint64_t where;
 
 		/* if we have any in-progress requests, don't unload */
 		mutex_enter(_q_mtx);
@@ -468,20 +472,25 @@ crypto_destroy(bool exit_kthread)
 		 * prohibit touch crypto_drivers[] and each element after here.
 		 */
 
+		/*
+		 * Ensure cryptoret_softint() is never scheduled and then wait
+		 * for last softint_execute().
+		 */
 		mutex_spin_enter(_ret_q_mtx);
-		/* kick the cryptoret thread and wait for it to exit */
 		crypto_exit_flag = 1;
-		cv_signal(_cv);
-
-		while (crypto_exit_flag != 0)
-			cv_wait(_cv, _ret_q_mtx);
 		mutex_spin_exit(_ret_q_mtx);
+		where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
+		xc_wait(where);
 	}
 
 	if (sysctl_opencrypto_clog != NULL)
 		sysctl_teardown(_opencrypto_clog);
 
-	unregister_swi(SWI_CRYPTO, cryptointr);
+	if (crypto_ret_si != NULL)
+		softint_disestablish(crypto_ret_si);
+
+	if (softintr_cookie != NULL)
+		unregister_swi(SWI_CRYPTO, cryptointr);
 
 	mutex_enter(_drv_mtx);
 	if (crypto_drivers != NULL)
@@ -492,8 +501,6 @@ crypto_destroy(bool exit_kthread)
 	pool_destroy(_pool);
 	pool_destroy(_pool);
 
-	cv_destroy(_cv);
-
 	mutex_destroy(_ret_q_mtx);
 	mutex_destroy(_q_mtx);
 	mutex_destroy(_drv_mtx);
@@ -1278,6 +1285,7 @@ 

CVS commit: src/sys/opencrypto

2017-07-06 Thread Ryota Ozaki
Module Name:src
Committed By:   ozaki-r
Date:   Thu Jul  6 08:27:07 UTC 2017

Modified Files:
src/sys/opencrypto: xform.c

Log Message:
Apply C99-style struct initialization to enc_xform, auth_hash and comp_algo


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/opencrypto/xform.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/opencrypto/xform.c
diff -u src/sys/opencrypto/xform.c:1.28 src/sys/opencrypto/xform.c:1.29
--- src/sys/opencrypto/xform.c:1.28	Thu May 26 21:50:03 2011
+++ src/sys/opencrypto/xform.c	Thu Jul  6 08:27:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: xform.c,v 1.28 2011/05/26 21:50:03 drochner Exp $ */
+/*	$NetBSD: xform.c,v 1.29 2017/07/06 08:27:07 ozaki-r Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $	*/
 
@@ -40,7 +40,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xform.c,v 1.28 2011/05/26 21:50:03 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xform.c,v 1.29 2017/07/06 08:27:07 ozaki-r Exp $");
 
 #include 
 #include 
@@ -90,169 +90,292 @@ const u_int8_t hmac_opad_buffer[128] = {
 
 /* Encryption instances */
 const struct enc_xform enc_xform_null = {
-	CRYPTO_NULL_CBC, "NULL",
+	.type		= CRYPTO_NULL_CBC,
+	.name		= "NULL",
 	/* NB: blocksize of 4 is to generate a properly aligned ESP header */
-	4, 0, 0, 256 /* 2048 bits, max key */
+	.blocksize	= 4,
+	.ivsize		= 0,
+	.minkey		= 0,
+	.maxkey		= 256, /* 2048 bits, max key */
 };
 
 const struct enc_xform enc_xform_des = {
-	CRYPTO_DES_CBC, "DES",
-	8, 8, 8, 8
+	.type		= CRYPTO_DES_CBC,
+	.name		= "DES",
+	.blocksize	= 8,
+	.ivsize		= 8,
+	.minkey		= 8,
+	.maxkey		= 8,
 };
 
 const struct enc_xform enc_xform_3des = {
-	CRYPTO_3DES_CBC, "3DES",
-	8, 8, 24, 24
+	.type		= CRYPTO_3DES_CBC,
+	.name		= "3DES",
+	.blocksize	= 8,
+	.ivsize		= 8,
+	.minkey		= 24,
+	.maxkey		= 24,
 };
 
 const struct enc_xform enc_xform_blf = {
-	CRYPTO_BLF_CBC, "Blowfish",
-	8, 8, 5, 56 /* 448 bits, max key */
+	.type		= CRYPTO_BLF_CBC,
+	.name		= "Blowfish",
+	.blocksize	= 8,
+	.ivsize		= 8,
+	.minkey		= 5,
+	.maxkey		= 56, /* 448 bits, max key */
 };
 
 const struct enc_xform enc_xform_cast5 = {
-	CRYPTO_CAST_CBC, "CAST-128",
-	8, 8, 5, 16
+	.type		= CRYPTO_CAST_CBC,
+	.name		= "CAST-128",
+	.blocksize	= 8,
+	.ivsize		= 8,
+	.minkey		= 5,
+	.maxkey		= 16,
 };
 
 const struct enc_xform enc_xform_skipjack = {
-	CRYPTO_SKIPJACK_CBC, "Skipjack",
-	8, 8, 10, 10
+	.type		= CRYPTO_SKIPJACK_CBC,
+	.name		= "Skipjack",
+	.blocksize	= 8,
+	.ivsize		= 8,
+	.minkey		= 10,
+	.maxkey		= 10,
 };
 
 const struct enc_xform enc_xform_rijndael128 = {
-	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
-	16, 16, 16, 32
+	.type		= CRYPTO_RIJNDAEL128_CBC,
+	.name		= "Rijndael-128/AES",
+	.blocksize	= 16,
+	.ivsize		= 16,
+	.minkey		= 16,
+	.maxkey		= 32,
 };
 
 const struct enc_xform enc_xform_arc4 = {
-	CRYPTO_ARC4, "ARC4",
-	1, 0, 1, 32
+	.type		= CRYPTO_ARC4,
+	.name		= "ARC4",
+	.blocksize	= 1,
+	.ivsize		= 0,
+	.minkey		= 1,
+	.maxkey		= 32,
 };
 
 const struct enc_xform enc_xform_camellia = {
-	CRYPTO_CAMELLIA_CBC, "Camellia",
-	16, 16, 8, 32
+	.type		= CRYPTO_CAMELLIA_CBC,
+	.name		= "Camellia",
+	.blocksize	= 16,
+	.ivsize		= 16,
+	.minkey		= 8,
+	.maxkey		= 32,
 };
 
 const struct enc_xform enc_xform_aes_ctr = {
-	CRYPTO_AES_CTR, "AES-CTR",
-	16, 8, 16+4, 32+4
+	.type		= CRYPTO_AES_CTR,
+	.name		= "AES-CTR",
+	.blocksize	= 16,
+	.ivsize		= 8,
+	.minkey		= 16 + 4,
+	.maxkey		= 32 + 4,
 };
 
 const struct enc_xform enc_xform_aes_gcm = {
-	CRYPTO_AES_GCM_16, "AES-GCM",
-	4 /* ??? */, 8, 16+4, 32+4
+	.type		= CRYPTO_AES_GCM_16,
+	.name		= "AES-GCM",
+	.blocksize	= 4, /* ??? */
+	.ivsize		= 8,
+	.minkey		= 16 + 4,
+	.maxkey		= 32 + 4,
 };
 
 const struct enc_xform enc_xform_aes_gmac = {
-	CRYPTO_AES_GMAC, "AES-GMAC",
-	4 /* ??? */, 8, 16+4, 32+4
+	.type		= CRYPTO_AES_GMAC,
+	.name		= "AES-GMAC",
+	.blocksize	= 4, /* ??? */
+	.ivsize		= 8,
+	.minkey		= 16 + 4,
+	.maxkey		= 32 + 4,
 };
 
 /* Authentication instances */
 const struct auth_hash auth_hash_null = {
-	CRYPTO_NULL_HMAC, "NULL-HMAC",
-	0, 0, 12, 64
+	.type		= CRYPTO_NULL_HMAC,
+	.name		= "NULL-HMAC",
+	.keysize	= 0,
+	.hashsize	= 0,
+	.authsize	= 12,
+	.blocksize	= 64,
 };
 
 const struct auth_hash auth_hash_hmac_md5 = {
-	CRYPTO_MD5_HMAC, "HMAC-MD5",
-	16, 16, 16, 64
+	.type		= CRYPTO_MD5_HMAC,
+	.name		= "HMAC-MD5",
+	.keysize	= 16,
+	.hashsize	= 16,
+	.authsize	= 16,
+	.blocksize	= 64,
 };
 
 const struct auth_hash auth_hash_hmac_sha1 = {
-	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
-	20, 20, 20, 64
+	.type		= CRYPTO_SHA1_HMAC,
+	.name		= "HMAC-SHA1",
+	.keysize	= 20,
+	.hashsize	= 20,
+	.authsize	= 20,
+	.blocksize	= 64,
 };
 
 const struct auth_hash auth_hash_hmac_ripemd_160 = {
-	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
-	20, 20, 20, 64
+	.type		= CRYPTO_RIPEMD160_HMAC,
+	.name		= 

CVS commit: src/sys/opencrypto

2017-07-06 Thread Ryota Ozaki
Module Name:src
Committed By:   ozaki-r
Date:   Thu Jul  6 08:22:45 UTC 2017

Modified Files:
src/sys/opencrypto: xform.h

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/opencrypto/xform.h

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

Modified files:

Index: src/sys/opencrypto/xform.h
diff -u src/sys/opencrypto/xform.h:1.19 src/sys/opencrypto/xform.h:1.20
--- src/sys/opencrypto/xform.h:1.19	Thu May 26 21:50:03 2011
+++ src/sys/opencrypto/xform.h	Thu Jul  6 08:22:45 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: xform.h,v 1.19 2011/05/26 21:50:03 drochner Exp $ */
+/*	$NetBSD: xform.h,v 1.20 2017/07/06 08:22:45 ozaki-r Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/xform.h,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: xform.h,v 1.10 2002/04/22 23:10:09 deraadt Exp $	*/
 
@@ -42,8 +42,10 @@ struct auth_hash {
 struct enc_xform {
 	int type;
 	const char *name;
-	u_int16_t blocksize, ivsize;
-	u_int16_t minkey, maxkey;
+	u_int16_t blocksize;
+	u_int16_t ivsize;
+	u_int16_t minkey;
+	u_int16_t maxkey;
 };
 
 struct comp_algo {



CVS commit: src/sys/opencrypto

2017-06-25 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jun 26 05:34:49 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
simplify mutex_enter/exit(crypto_q_mtx), and fix missing exit.


To generate a diff of this commit:
cvs rdiff -u -r1.90 -r1.91 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.90 src/sys/opencrypto/crypto.c:1.91
--- src/sys/opencrypto/crypto.c:1.90	Thu Jun 15 12:45:10 2017
+++ src/sys/opencrypto/crypto.c	Mon Jun 26 05:34:48 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.90 2017/06/15 12:45:10 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.91 2017/06/26 05:34:48 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.90 2017/06/15 12:45:10 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.91 2017/06/26 05:34:48 knakahara Exp $");
 
 #include 
 #include 
@@ -1117,8 +1117,8 @@ crypto_dispatch(struct cryptop *crp)
 		 * to other drivers in cryptointr() later.
 		 */
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
-		mutex_exit(_q_mtx);
-		return 0;
+		result = 0;
+		goto out;
 	}
 
 	if (cap->cc_qblocked != 0) {
@@ -1128,8 +1128,8 @@ crypto_dispatch(struct cryptop *crp)
 		 * it unblocks and the swi thread gets kicked.
 		 */
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
-		mutex_exit(_q_mtx);
-		return 0;
+		result = 0;
+		goto out;
 	}
 
 	/*
@@ -1159,6 +1159,7 @@ crypto_dispatch(struct cryptop *crp)
 		result = 0;
 	}
 
+out:
 	mutex_exit(_q_mtx);
 	return result;
 }
@@ -1186,8 +1187,8 @@ crypto_kdispatch(struct cryptkop *krp)
 	 */
 	if (cap == NULL) {
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
-		mutex_exit(_q_mtx);
-		return 0;
+		result = 0;
+		goto out;
 	}
 
 	if (cap->cc_kqblocked != 0) {
@@ -1197,8 +1198,8 @@ crypto_kdispatch(struct cryptkop *krp)
 		 * it unblocks and the swi thread gets kicked.
 		 */
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
-		mutex_exit(_q_mtx);
-		return 0;
+		result = 0;
+		goto out;
 	}
 
 	crypto_driver_unlock(cap);
@@ -1214,7 +1215,6 @@ crypto_kdispatch(struct cryptkop *krp)
 		crypto_driver_unlock(cap);
 		TAILQ_INSERT_HEAD(_kq, krp, krp_next);
 		cryptostats.cs_kblocks++;
-		mutex_exit(_q_mtx);
 
 		/*
 		 * The krp is enqueued to crp_kq, that is,
@@ -1224,6 +1224,8 @@ crypto_kdispatch(struct cryptkop *krp)
 		result = 0;
 	}
 
+out:
+	mutex_exit(_q_mtx);
 	return result;
 }
 



CVS commit: src/sys/opencrypto

2017-06-23 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Jun 23 11:41:58 UTC 2017

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
fix cryptosoft.c:r1.51 mistake. swcrypto_attach() must not be called from 
module_init_class().

swcrypto_attach() will call softint_establish(), it must be called after cpus
attached. module_init_class() is too early to call softint_establish().


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.51 src/sys/opencrypto/cryptosoft.c:1.52
--- src/sys/opencrypto/cryptosoft.c:1.51	Thu Jun  1 08:49:35 2017
+++ src/sys/opencrypto/cryptosoft.c	Fri Jun 23 11:41:58 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.51 2017/06/01 08:49:35 knakahara Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.52 2017/06/23 11:41:58 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.51 2017/06/01 08:49:35 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.52 2017/06/23 11:41:58 knakahara Exp $");
 
 #include 
 #include 
@@ -77,6 +77,8 @@ static	int swcr_process(void *, struct c
 static	int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
 static	int swcr_freesession(void *, u_int64_t);
 
+static	int swcryptoattach_internal(void);
+
 /*
  * Apply a symmetric encryption/decryption algorithm.
  */
@@ -1326,9 +1328,17 @@ void
 swcryptoattach(int num)
 {
 	/*
-	 * Nothing to do here, initialization is handled by the
-	 * module initialization code in swcrypto_attach() below).
+	 * swcrypto_attach() must be called after attached cpus, because
+	 * it calls softint_establish() through below call path.
+	 * swcr_init() => crypto_get_driverid() => crypto_init()
+	 * => crypto_init0()
+	 * If softint_establish() is called before attached cpus that ncpu == 0,
+	 * the softint handler is established to CPU#0 only.
+	 *
+	 * So, swcrypto_attach() must be called from not module_init_class()
+	 * but config_finalize() when it is built as builtin module.
 	 */
+	swcryptoattach_internal();
 }
 
 void	swcrypto_attach(device_t, device_t, void *);
@@ -1386,42 +1396,56 @@ static struct cfdata swcrypto_cfdata[] =
 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
 };
 
+/*
+ * Internal attach routine.
+ * Don't call before attached cpus.
+ */
 static int
-swcrypto_modcmd(modcmd_t cmd, void *arg)
+swcryptoattach_internal(void)
 {
 	int error;
 
-	switch (cmd) {
-	case MODULE_CMD_INIT:
-		error = config_cfdriver_attach(_cd);
-		if (error) {
-			return error;
-		}
+	error = config_cfdriver_attach(_cd);
+	if (error) {
+		return error;
+	}
+
+	error = config_cfattach_attach(swcrypto_cd.cd_name, _ca);
+	if (error) {
+		config_cfdriver_detach(_cd);
+		aprint_error("%s: unable to register cfattach\n",
+		swcrypto_cd.cd_name);
 
-		error = config_cfattach_attach(swcrypto_cd.cd_name,
+		return error;
+	}
+
+	error = config_cfdata_attach(swcrypto_cfdata, 1);
+	if (error) {
+		config_cfattach_detach(swcrypto_cd.cd_name,
 		_ca);
-		if (error) {
-			config_cfdriver_detach(_cd);
-			aprint_error("%s: unable to register cfattach\n",
-swcrypto_cd.cd_name);
+		config_cfdriver_detach(_cd);
+		aprint_error("%s: unable to register cfdata\n",
+		swcrypto_cd.cd_name);
 
-			return error;
-		}
+		return error;
+	}
 
-		error = config_cfdata_attach(swcrypto_cfdata, 1);
-		if (error) {
-			config_cfattach_detach(swcrypto_cd.cd_name,
-			_ca);
-			config_cfdriver_detach(_cd);
-			aprint_error("%s: unable to register cfdata\n",
-swcrypto_cd.cd_name);
+	(void)config_attach_pseudo(swcrypto_cfdata);
 
-			return error;
-		}
+	return 0;
+}
 
-		(void)config_attach_pseudo(swcrypto_cfdata);
+static int
+swcrypto_modcmd(modcmd_t cmd, void *arg)
+{
+	int error = 0;
 
-		return 0;
+	switch (cmd) {
+	case MODULE_CMD_INIT:
+#ifdef _MODULE
+		error = swcryptoattach_internal();
+#endif
+		return error;
 	case MODULE_CMD_FINI:
 		error = config_cfdata_detach(swcrypto_cfdata);
 		if (error) {



CVS commit: src/sys/opencrypto

2017-06-15 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jun 15 12:45:10 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
Reduce crypto_ret_q_mtx lock regions.

crypto.c does not access the members of crp when the crp is in crp_q or
crp_ret_q. Furthermore, crp_q and crp_ret_q are protected by each mutex,
so the members of crp is not shared. That means crp_flags is not required
mutex in crypto.c.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.89 src/sys/opencrypto/crypto.c:1.90
--- src/sys/opencrypto/crypto.c:1.89	Wed Jun 14 07:38:24 2017
+++ src/sys/opencrypto/crypto.c	Thu Jun 15 12:45:10 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.89 2017/06/14 07:38:24 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.90 2017/06/15 12:45:10 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.89 2017/06/14 07:38:24 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.90 2017/06/15 12:45:10 knakahara Exp $");
 
 #include 
 #include 
@@ -1494,7 +1494,6 @@ crypto_kgetreq(int num __unused, int prf
 void
 crypto_done(struct cryptop *crp)
 {
-	int wasempty;
 
 	KASSERT(crp != NULL);
 
@@ -1520,9 +1519,7 @@ crypto_done(struct cryptop *crp)
   	 	* callback routine does very little (e.g. the
 	 	* /dev/crypto callback method just does a wakeup).
 	 	*/
-		mutex_spin_enter(_ret_q_mtx);
 		crp->crp_flags |= CRYPTO_F_DONE;
-		mutex_spin_exit(_ret_q_mtx);
 
 #ifdef CRYPTO_TIMING
 		if (crypto_timing) {
@@ -1539,7 +1536,6 @@ crypto_done(struct cryptop *crp)
 #endif
 		crp->crp_callback(crp);
 	} else {
-		mutex_spin_enter(_ret_q_mtx);
 		crp->crp_flags |= CRYPTO_F_DONE;
 #if 0
 		if (crp->crp_flags & CRYPTO_F_USER) {
@@ -1555,6 +1551,9 @@ crypto_done(struct cryptop *crp)
 		} else
 #endif
 		{
+			int wasempty;
+
+			mutex_spin_enter(_ret_q_mtx);
 			wasempty = TAILQ_EMPTY(_ret_q);
 			DPRINTF("lid[%u]: queueing %p\n",
 CRYPTO_SESID2LID(crp->crp_sid), crp);
@@ -1567,8 +1566,8 @@ crypto_done(struct cryptop *crp)
 	CRYPTO_SESID2LID(crp->crp_sid), crp);
 cv_signal(_cv);
 			}
+			mutex_spin_exit(_ret_q_mtx);
 		}
-		mutex_spin_exit(_ret_q_mtx);
 	}
 }
 
@@ -1578,7 +1577,6 @@ crypto_done(struct cryptop *crp)
 void
 crypto_kdone(struct cryptkop *krp)
 {
-	int wasempty;
 
 	KASSERT(krp != NULL);
 
@@ -1596,6 +1594,8 @@ crypto_kdone(struct cryptkop *krp)
 	if (krp->krp_flags & CRYPTO_F_CBIMM) {
 		krp->krp_callback(krp);
 	} else {
+		int wasempty;
+
 		mutex_spin_enter(_ret_q_mtx);
 		wasempty = TAILQ_EMPTY(_ret_kq);
 		krp->krp_flags |= CRYPTO_F_ONRETQ;



CVS commit: src/sys/opencrypto

2017-06-15 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jun 15 12:41:18 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c cryptodev.h

Log Message:
Divide crp_devflags from crp_flags to write exclusively.

CRYPTO_F_DQRETQ(new name is CRYPTODEV_F_RET) is used by cryptodev.c only.
It should be divided to other member.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.36 -r1.37 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.94 src/sys/opencrypto/cryptodev.c:1.95
--- src/sys/opencrypto/cryptodev.c:1.94	Thu Jun  8 09:49:46 2017
+++ src/sys/opencrypto/cryptodev.c	Thu Jun 15 12:41:18 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.94 2017/06/08 09:49:46 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.95 2017/06/15 12:41:18 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.94 2017/06/08 09:49:46 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.95 2017/06/15 12:41:18 knakahara Exp $");
 
 #include 
 #include 
@@ -689,7 +689,7 @@ eagain:
 		goto bail;
 	}
 
-	while (!(crp->crp_flags & CRYPTO_F_DQRETQ)) {
+	while (!(crp->crp_devflags & CRYPTODEV_F_RET)) {
 		DPRINTF("cse->sid[%d]: sleeping on cv %p for crp %p\n",
 			(uint32_t)cse->sid, >crp_cv, crp);
 		cv_wait(>crp_cv, _mtx);	/* XXX cv_wait_sig? */
@@ -763,7 +763,7 @@ cryptodev_cb(void *op)
 		mutex_enter(_mtx);
 	}
 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
-		crp->crp_flags |= CRYPTO_F_DQRETQ;
+		crp->crp_devflags |= CRYPTODEV_F_RET;
 		cv_signal(>crp_cv);
 	}
 	mutex_exit(_mtx);
@@ -800,7 +800,7 @@ cryptodevkey_cb(void *op)
 	struct cryptkop *krp = op;
 	
 	mutex_enter(_mtx);
-	krp->krp_flags |= CRYPTO_F_DQRETQ;
+	krp->krp_devflags |= CRYPTODEV_F_RET;
 	cv_signal(>krp_cv);
 	mutex_exit(_mtx);
 	return 0;
@@ -915,7 +915,7 @@ cryptodev_key(struct crypt_kop *kop)
 	}
 
 	mutex_enter(_mtx);
-	while (!(krp->krp_flags & CRYPTO_F_DQRETQ)) {
+	while (!(krp->krp_devflags & CRYPTODEV_F_RET)) {
 		cv_wait(>krp_cv, _mtx);	/* XXX cv_wait_sig? */
 	}
 	mutex_exit(_mtx);

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.36 src/sys/opencrypto/cryptodev.h:1.37
--- src/sys/opencrypto/cryptodev.h:1.36	Tue Jun  6 01:48:33 2017
+++ src/sys/opencrypto/cryptodev.h	Thu Jun 15 12:41:18 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.36 2017/06/06 01:48:33 knakahara Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.37 2017/06/15 12:41:18 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -461,8 +461,10 @@ struct cryptop {
 	 * should always check and use the new
 	 * value on future requests.
 	 */
-	int		crp_flags;	/* Note: must hold mutext to modify */
-
+	int		crp_flags;	/*
+	 * other than crypto.c must not write
+	 * after crypto_dispatch().
+	 */
 #define CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
 #define CRYPTO_F_IOV		0x0002	/* Input/output are uio */
 #define CRYPTO_F_REL		0x0004	/* Must return data in same place */
@@ -473,7 +475,9 @@ struct cryptop {
 #define	CRYPTO_F_ONRETQ		0x0080	/* Request is on return queue */
 #define	CRYPTO_F_USER		0x0100	/* Request is in user context */
 #define	CRYPTO_F_MORE		0x0200	/* more data to follow */
-#define	CRYPTO_F_DQRETQ		0x0400	/* Dequeued from crp_ret_{,k}q */
+
+	int		crp_devflags;	/* other than cryptodev.c must not use. */
+#define	CRYPTODEV_F_RET		0x0001	/* return from crypto.c to cryptodev.c */
 
 	void *		crp_buf;	/* Data to be processed */
 	void *		crp_opaque;	/* Opaque pointer, passed along */
@@ -528,6 +532,7 @@ struct cryptkop {
 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
 	int		(*krp_callback)(struct cryptkop *);
 	int		krp_flags;	/* same values as crp_flags */
+	int		krp_devflags;	/* same values as crp_devflags */
 	kcondvar_t	krp_cv;
 	struct fcrypt 	*fcrp;
 	struct crparam	crk_param[CRK_MAXPARAM];



CVS commit: src/sys/opencrypto

2017-06-14 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jun 14 07:38:24 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
support multiple encryption drivers (port from FreeBSD).


To generate a diff of this commit:
cvs rdiff -u -r1.88 -r1.89 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.88 src/sys/opencrypto/crypto.c:1.89
--- src/sys/opencrypto/crypto.c:1.88	Wed Jun 14 07:36:24 2017
+++ src/sys/opencrypto/crypto.c	Wed Jun 14 07:38:24 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.88 2017/06/14 07:36:24 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.89 2017/06/14 07:38:24 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.88 2017/06/14 07:36:24 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.89 2017/06/14 07:38:24 knakahara Exp $");
 
 #include 
 #include 
@@ -515,9 +515,14 @@ crypto_driver_suitable(struct cryptocap 
 	return true;
 }
 
+#define CRYPTO_ACCEPT_HARDWARE 0x1
+#define CRYPTO_ACCEPT_SOFTWARE 0x2
 /*
  * The algorithm we use here is pretty stupid; just use the
  * first driver that supports all the algorithms we need.
+ * If there are multiple drivers we choose the driver with
+ * the fewest active sessions. We prefer hardware-backed
+ * drivers to software ones.
  *
  * XXX We need more smarts here (in real life too, but that's
  * XXX another story altogether).
@@ -526,9 +531,22 @@ static struct cryptocap *
 crypto_select_driver_lock(struct cryptoini *cri, int hard)
 {
 	u_int32_t hid;
+	int accept;
+	struct cryptocap *cap, *best;
 
+	best = NULL;
+	/*
+	 * hard == 0 can use both hardware and software drivers.
+	 * We use hardware drivers prior to software drivers, so search
+	 * hardware drivers at first time.
+	 */
+	if (hard >= 0)
+		accept = CRYPTO_ACCEPT_HARDWARE;
+	else
+		accept = CRYPTO_ACCEPT_SOFTWARE;
+again:
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		struct cryptocap *cap = crypto_checkdriver(hid);
+		cap = crypto_checkdriver(hid);
 		if (cap == NULL)
 			continue;
 
@@ -545,26 +563,41 @@ crypto_select_driver_lock(struct cryptoi
 		}
 
 		/* Hardware required -- ignore software drivers. */
-		if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE)) {
+		if ((accept & CRYPTO_ACCEPT_SOFTWARE) == 0
+		&& (cap->cc_flags & CRYPTOCAP_F_SOFTWARE)) {
 			crypto_driver_unlock(cap);
 			continue;
 		}
 		/* Software required -- ignore hardware drivers. */
-		if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0) {
+		if ((accept & CRYPTO_ACCEPT_HARDWARE) == 0
+		&& (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0) {
 			crypto_driver_unlock(cap);
 			continue;
 		}
 
 		/* See if all the algorithms are supported. */
 		if (crypto_driver_suitable(cap, cri)) {
-			/* keep holding crypto_driver_lock(cap) */
-			return cap;
+			if (best == NULL) {
+/* keep holding crypto_driver_lock(cap) */
+best = cap;
+continue;
+			} else if (cap->cc_sessions < best->cc_sessions) {
+crypto_driver_unlock(best);
+/* keep holding crypto_driver_lock(cap) */
+best = cap;
+continue;
+			}
 		}
 
 		crypto_driver_unlock(cap);
 	}
+	if (best == NULL && hard == 0
+	&& (accept & CRYPTO_ACCEPT_SOFTWARE) == 0) {
+		accept = CRYPTO_ACCEPT_SOFTWARE;
+		goto again;
+	}
 
-	return NULL;
+	return best;
 }
 
 /*



CVS commit: src/sys/opencrypto

2017-06-14 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jun 14 07:36:24 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_newsession() like FreeBSD.


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.87 src/sys/opencrypto/crypto.c:1.88
--- src/sys/opencrypto/crypto.c:1.87	Wed Jun 14 07:32:19 2017
+++ src/sys/opencrypto/crypto.c	Wed Jun 14 07:36:24 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.87 2017/06/14 07:32:19 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.88 2017/06/14 07:36:24 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.87 2017/06/14 07:32:19 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.88 2017/06/14 07:36:24 knakahara Exp $");
 
 #include 
 #include 
@@ -501,29 +501,34 @@ crypto_destroy(bool exit_kthread)
 	return 0;
 }
 
-/*
- * Create a new session.
- */
-int
-crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
+static bool
+crypto_driver_suitable(struct cryptocap *cap, struct cryptoini *cri)
 {
 	struct cryptoini *cr;
-	struct cryptocap *cap;
-	u_int32_t hid, lid;
-	int err = EINVAL;
 
-	mutex_enter(_drv_mtx);
+	for (cr = cri; cr; cr = cr->cri_next)
+		if (cap->cc_alg[cr->cri_alg] == 0) {
+			DPRINTF("alg %d not supported\n", cr->cri_alg);
+			return false;
+		}
 
-	/*
-	 * The algorithm we use here is pretty stupid; just use the
-	 * first driver that supports all the algorithms we need.
-	 *
-	 * XXX We need more smarts here (in real life too, but that's
-	 * XXX another story altogether).
-	 */
+	return true;
+}
+
+/*
+ * The algorithm we use here is pretty stupid; just use the
+ * first driver that supports all the algorithms we need.
+ *
+ * XXX We need more smarts here (in real life too, but that's
+ * XXX another story altogether).
+ */
+static struct cryptocap *
+crypto_select_driver_lock(struct cryptoini *cri, int hard)
+{
+	u_int32_t hid;
 
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = crypto_checkdriver(hid);
+		struct cryptocap *cap = crypto_checkdriver(hid);
 		if (cap == NULL)
 			continue;
 
@@ -551,45 +556,59 @@ crypto_newsession(u_int64_t *sid, struct
 		}
 
 		/* See if all the algorithms are supported. */
-		for (cr = cri; cr; cr = cr->cri_next)
-			if (cap->cc_alg[cr->cri_alg] == 0) {
-DPRINTF("alg %d not supported\n", cr->cri_alg);
-break;
-			}
+		if (crypto_driver_suitable(cap, cri)) {
+			/* keep holding crypto_driver_lock(cap) */
+			return cap;
+		}
 
-		if (cr == NULL) {
-			/* Ok, all algorithms are supported. */
+		crypto_driver_unlock(cap);
+	}
 
-			/*
-			 * Can't do everything in one session.
-			 *
-			 * XXX Fix this. We need to inject a "virtual" session layer right
-			 * XXX about here.
-			 */
+	return NULL;
+}
 
-			/* Call the driver initialization routine. */
-			lid = hid;		/* Pass the driver ID. */
-			crypto_driver_unlock(cap);
-			err = cap->cc_newsession(cap->cc_arg, , cri);
-			crypto_driver_lock(cap);
-			if (err == 0) {
-(*sid) = hid;
-(*sid) <<= 32;
-(*sid) |= (lid & 0x);
-(cap->cc_sessions)++;
-			} else {
-DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
-	hid, err);
-			}
-			crypto_driver_unlock(cap);
-			goto done;
-			/*break;*/
-		}
+/*
+ * Create a new session.
+ */
+int
+crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
+{
+	struct cryptocap *cap;
+	int err = EINVAL;
 
+	mutex_enter(_drv_mtx);
+
+	cap = crypto_select_driver_lock(cri, hard);
+	if (cap != NULL) {
+		u_int32_t hid, lid;
+
+		hid = cap - crypto_drivers;
+		/*
+		 * Can't do everything in one session.
+		 *
+		 * XXX Fix this. We need to inject a "virtual" session layer right
+		 * XXX about here.
+		 */
+
+		/* Call the driver initialization routine. */
+		lid = hid;		/* Pass the driver ID. */
+		crypto_driver_unlock(cap);
+		err = cap->cc_newsession(cap->cc_arg, , cri);
+		crypto_driver_lock(cap);
+		if (err == 0) {
+			(*sid) = hid;
+			(*sid) <<= 32;
+			(*sid) |= (lid & 0x);
+			(cap->cc_sessions)++;
+		} else {
+			DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
+			hid, err);
+		}
 		crypto_driver_unlock(cap);
 	}
-done:
+
 	mutex_exit(_drv_mtx);
+
 	return err;
 }
 



CVS commit: src/sys/opencrypto

2017-06-14 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jun 14 07:32:19 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
must release cap->cc_lock before calling cap->cc_newsession() because of 
spinlock.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.86 src/sys/opencrypto/crypto.c:1.87
--- src/sys/opencrypto/crypto.c:1.86	Thu Jun  8 00:17:02 2017
+++ src/sys/opencrypto/crypto.c	Wed Jun 14 07:32:19 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.86 2017/06/08 00:17:02 christos Exp $ */
+/*	$NetBSD: crypto.c,v 1.87 2017/06/14 07:32:19 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.86 2017/06/08 00:17:02 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.87 2017/06/14 07:32:19 knakahara Exp $");
 
 #include 
 #include 
@@ -569,7 +569,9 @@ crypto_newsession(u_int64_t *sid, struct
 
 			/* Call the driver initialization routine. */
 			lid = hid;		/* Pass the driver ID. */
+			crypto_driver_unlock(cap);
 			err = cap->cc_newsession(cap->cc_arg, , cri);
+			crypto_driver_lock(cap);
 			if (err == 0) {
 (*sid) = hid;
 (*sid) <<= 32;



CVS commit: src/sys/opencrypto

2017-06-08 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jun  8 09:49:46 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
sanitize in CIOCNCRYPTM and initialize comp_alg in CIOCNGSESSION


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.93 src/sys/opencrypto/cryptodev.c:1.94
--- src/sys/opencrypto/cryptodev.c:1.93	Thu Jun  8 03:02:26 2017
+++ src/sys/opencrypto/cryptodev.c	Thu Jun  8 09:49:46 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.93 2017/06/08 03:02:26 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.94 2017/06/08 09:49:46 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.93 2017/06/08 03:02:26 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.94 2017/06/08 09:49:46 knakahara Exp $");
 
 #include 
 #include 
@@ -1181,6 +1181,12 @@ cryptodev_mop(struct fcrypt *fcr, 
 			}
 		}
 
+		/* sanitize */
+		if (cnop[req].len <= 0) {
+			cnop[req].status = ENOMEM;
+			goto bail;
+		}
+
 		crp = crypto_getreq((cse->txform != NULL) +
 (cse->thash != NULL) +
 (cse->tcomp != NULL));
@@ -1770,12 +1776,14 @@ cryptodev_msession(struct fcrypt *fcr, s
 		struct session_op s_op;
 		s_op.cipher =		sn_ops->cipher;
 		s_op.mac =		sn_ops->mac;
+		s_op.comp_alg =		sn_ops->comp_alg;
 		s_op.keylen =		sn_ops->keylen;
 		s_op.key =		sn_ops->key;
 		s_op.mackeylen =	sn_ops->mackeylen;
 		s_op.mackey =		sn_ops->mackey;
 
 		sn_ops->status = cryptodev_session(fcr, _op);
+
 		sn_ops->ses =		s_op.ses;
 	}
 



CVS commit: src/sys/opencrypto

2017-06-07 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jun  8 03:02:26 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
sanitize count used for kmem_alloc size.

Hmm, who uses CIOCNGSESSION, CIOCNFSESSION, CIOCNCRYPTM or CIOCNFKEYM?


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.92 src/sys/opencrypto/cryptodev.c:1.93
--- src/sys/opencrypto/cryptodev.c:1.92	Fri Jun  2 09:46:57 2017
+++ src/sys/opencrypto/cryptodev.c	Thu Jun  8 03:02:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.92 2017/06/02 09:46:57 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.93 2017/06/08 03:02:26 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.92 2017/06/02 09:46:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.93 2017/06/08 03:02:26 knakahara Exp $");
 
 #include 
 #include 
@@ -281,6 +281,11 @@ cryptof_ioctl(struct file *fp, u_long cm
 		break;
 	case CIOCNGSESSION:
 		sgop = (struct crypt_sgop *)data;
+		if (sgop->count <= 0
+		|| SIZE_MAX / sizeof(struct session_n_op) <= sgop->count) {
+			error = EINVAL;
+			break;
+		}
 		snop = kmem_alloc((sgop->count *
   sizeof(struct session_n_op)), KM_SLEEP);
 		error = copyin(sgop->sessions, snop, sgop->count *
@@ -320,6 +325,11 @@ mbail:
 		fcr->mtime = fcr->atime;
 		mutex_exit(_mtx);
 		sfop = (struct crypt_sfop *)data;
+		if (sfop->count <= 0
+		|| SIZE_MAX / sizeof(u_int32_t) <= sfop->count) {
+			error = EINVAL;
+			break;
+		}
 		sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)), 
 		KM_SLEEP);
 		error = copyin(sfop->sesid, sesid,
@@ -347,6 +357,11 @@ mbail:
 		fcr->mtime = fcr->atime;
 		mutex_exit(_mtx);
 		mop = (struct crypt_mop *)data;
+		if (mop->count <= 0
+		|| SIZE_MAX / sizeof(struct crypt_n_op) <= mop->count) {
+			error = EINVAL;
+			break;
+		}
 		cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
 		KM_SLEEP);
 		error = copyin(mop->reqs, cnop,
@@ -369,6 +384,11 @@ mbail:
 		fcr->mtime = fcr->atime;
 		mutex_exit(_mtx);
 		mkop = (struct crypt_mkop *)data;
+		if (mkop->count <= 0
+		|| SIZE_MAX / sizeof(struct crypt_n_kop) <= mkop->count) {
+			error = EINVAL;
+			break;
+		}
 		knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
 		KM_SLEEP);
 		error = copyin(mkop->reqs, knop,
@@ -390,6 +410,11 @@ mbail:
 		mutex_exit(_mtx);
 		crypt_ret = (struct cryptret *)data;
 		count = crypt_ret->count;
+		if (count <= 0
+		|| SIZE_MAX / sizeof(struct crypt_result) <= count) {
+			error = EINVAL;
+			break;
+		}
 		crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),  
 		KM_SLEEP);
 		error = copyin(crypt_ret->results, crypt_res,



CVS commit: src/sys/opencrypto

2017-06-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Jun  8 00:17:02 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
Put back crypto_checkdriver(); use it when we need to make sure that we
get back a cryptocap that has been initialized.


To generate a diff of this commit:
cvs rdiff -u -r1.85 -r1.86 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.85 src/sys/opencrypto/crypto.c:1.86
--- src/sys/opencrypto/crypto.c:1.85	Tue Jun  6 14:08:23 2017
+++ src/sys/opencrypto/crypto.c	Wed Jun  7 20:17:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.85 2017/06/06 18:08:23 christos Exp $ */
+/*	$NetBSD: crypto.c,v 1.86 2017/06/08 00:17:02 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.85 2017/06/06 18:08:23 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.86 2017/06/08 00:17:02 christos Exp $");
 
 #include 
 #include 
@@ -376,6 +376,7 @@ static	int crypto_kinvoke(struct cryptko
 
 static struct cryptocap *crypto_checkdriver_lock(u_int32_t);
 static struct cryptocap *crypto_checkdriver_uninit(u_int32_t);
+static struct cryptocap *crypto_checkdriver(u_int32_t);
 static void crypto_driver_lock(struct cryptocap *);
 static void crypto_driver_unlock(struct cryptocap *);
 static void crypto_driver_clear(struct cryptocap *);
@@ -454,7 +455,7 @@ crypto_destroy(bool exit_kthread)
 
 		mutex_enter(_drv_mtx);
 		for (i = 0; i < crypto_drivers_num; i++) {
-			cap = crypto_checkdriver_uninit(i);
+			cap = crypto_checkdriver(i);
 			if (cap == NULL)
 continue;
 			if (cap->cc_sessions != 0) {
@@ -522,7 +523,7 @@ crypto_newsession(u_int64_t *sid, struct
 	 */
 
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = crypto_checkdriver_uninit(hid);
+		cap = crypto_checkdriver(hid);
 		if (cap == NULL)
 			continue;
 
@@ -625,6 +626,15 @@ crypto_freesession(u_int64_t sid)
 	return err;
 }
 
+static bool
+crypto_checkdriver_initialized(const struct cryptocap *cap)
+{
+
+	return cap->cc_process != NULL ||
+	(cap->cc_flags & CRYPTOCAP_F_CLEANUP) != 0 ||
+	cap->cc_sessions != 0;
+}
+
 /*
  * Return an unused driver id.  Used by drivers prior to registering
  * support for the algorithms they handle.
@@ -641,12 +651,9 @@ crypto_get_driverid(u_int32_t flags)
 	mutex_enter(_drv_mtx);
 	for (i = 0; i < crypto_drivers_num; i++) {
 		cap = crypto_checkdriver_uninit(i);
-		if (cap == NULL)
+		if (cap == NULL || crypto_checkdriver_initialized(cap))
 			continue;
-		if (cap->cc_process == NULL &&
-		(cap->cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
-		cap->cc_sessions == 0)
-			break;
+		break;
 	}
 
 	/* Out of entries, allocate some more. */
@@ -724,6 +731,25 @@ crypto_checkdriver_uninit(u_int32_t hid)
 	return (hid >= crypto_drivers_num ? NULL : _drivers[hid]);
 }
 
+/*
+ * Use crypto_checkdriver_uninit() instead of crypto_checkdriver() below two
+ * situations
+ * - crypto_drivers[] may not be allocated
+ * - crypto_drivers[hid] may not be initialized
+ */
+static struct cryptocap *
+crypto_checkdriver(u_int32_t hid)
+{
+
+	KASSERT(mutex_owned(_drv_mtx));
+
+	if (crypto_drivers == NULL || hid >= crypto_drivers_num)
+		return NULL;
+
+	struct cryptocap *cap = _drivers[hid];
+	return crypto_checkdriver_initialized(cap) ? cap : NULL;
+}
+
 static inline void
 crypto_driver_lock(struct cryptocap *cap)
 {
@@ -1168,7 +1194,7 @@ crypto_kinvoke(struct cryptkop *krp, int
 
 	mutex_enter(_drv_mtx);
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = crypto_checkdriver_uninit(hid);
+		cap = crypto_checkdriver(hid);
 		if (cap == NULL)
 			continue;
 		crypto_driver_lock(cap);
@@ -1541,7 +1567,7 @@ crypto_getfeat(int *featp)
 	int feat = 0;
 	for (int hid = 0; hid < crypto_drivers_num; hid++) {
 		struct cryptocap *cap;
-		cap = crypto_checkdriver_uninit(hid);
+		cap = crypto_checkdriver(hid);
 		if (cap == NULL)
 			continue;
 



CVS commit: src/sys/opencrypto

2017-06-06 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Jun  6 18:08:23 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
- acquire lock
- use c99 loop indexes
- initialize featp


To generate a diff of this commit:
cvs rdiff -u -r1.84 -r1.85 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.84 src/sys/opencrypto/crypto.c:1.85
--- src/sys/opencrypto/crypto.c:1.84	Tue Jun  6 00:11:41 2017
+++ src/sys/opencrypto/crypto.c	Tue Jun  6 14:08:23 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.84 2017/06/06 04:11:41 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.85 2017/06/06 18:08:23 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.84 2017/06/06 04:11:41 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.85 2017/06/06 18:08:23 christos Exp $");
 
 #include 
 #include 
@@ -1530,34 +1530,36 @@ crypto_kdone(struct cryptkop *krp)
 int
 crypto_getfeat(int *featp)
 {
-	int hid, kalg, feat = 0;
 
-	if (crypto_userasymcrypto == 0)
+	if (crypto_userasymcrypto == 0) {
+		*featp = 0;
 		return 0;
+	}
 
 	mutex_enter(_drv_mtx);
 
-	for (hid = 0; hid < crypto_drivers_num; hid++) {
+	int feat = 0;
+	for (int hid = 0; hid < crypto_drivers_num; hid++) {
 		struct cryptocap *cap;
 		cap = crypto_checkdriver_uninit(hid);
 		if (cap == NULL)
 			continue;
 
+		crypto_driver_lock(cap);
+
 		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
-		crypto_devallowsoft == 0) {
-			crypto_driver_unlock(cap);
-			continue;
-		}
-		if (cap->cc_kprocess == NULL) {
-			crypto_driver_unlock(cap);
-			continue;
-		}
-		for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
+		crypto_devallowsoft == 0)
+			goto unlock;
+
+		if (cap->cc_kprocess == NULL)
+			goto unlock;
+
+		for (int kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
 			if ((cap->cc_kalg[kalg] &
 			CRYPTO_ALG_FLAG_SUPPORTED) != 0)
 feat |=  1 << kalg;
 
-		crypto_driver_unlock(cap);
+unlock:		crypto_driver_unlock(cap);
 	}
 
 	mutex_exit(_drv_mtx);



CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jun  6 04:11:41 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
apply the same fix as crypto.c:r1.83 for crypto_dispatch to crypto_kdispatch.


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.83 src/sys/opencrypto/crypto.c:1.84
--- src/sys/opencrypto/crypto.c:1.83	Tue Jun  6 01:51:39 2017
+++ src/sys/opencrypto/crypto.c	Tue Jun  6 04:11:41 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.83 2017/06/06 01:51:39 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.84 2017/06/06 04:11:41 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.83 2017/06/06 01:51:39 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.84 2017/06/06 04:11:41 knakahara Exp $");
 
 #include 
 #include 
@@ -1097,6 +1097,7 @@ crypto_kdispatch(struct cryptkop *krp)
 
 	cryptostats.cs_kops++;
 
+	mutex_enter(_q_mtx);
 	cap = crypto_checkdriver_lock(krp->krp_hid);
 	/*
 	 * TODO:
@@ -1104,10 +1105,8 @@ crypto_kdispatch(struct cryptkop *krp)
 	 * done crypto_unregister(), this migrate operation is not required.
 	 */
 	if (cap == NULL) {
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
 		mutex_exit(_q_mtx);
-
 		return 0;
 	}
 
@@ -1117,10 +1116,8 @@ crypto_kdispatch(struct cryptkop *krp)
 		 * The driver is blocked, just queue the op until
 		 * it unblocks and the swi thread gets kicked.
 		 */
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
 		mutex_exit(_q_mtx);
-
 		return 0;
 	}
 
@@ -1135,7 +1132,6 @@ crypto_kdispatch(struct cryptkop *krp)
 		crypto_driver_lock(cap);
 		cap->cc_kqblocked = 1;
 		crypto_driver_unlock(cap);
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_HEAD(_kq, krp, krp_next);
 		cryptostats.cs_kblocks++;
 		mutex_exit(_q_mtx);



CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jun  6 01:51:39 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
avoid crp_q reordering as hardware interrupts.

crypto_{,k}invoke() can be called with holding crp_q_mtx now.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.82 src/sys/opencrypto/crypto.c:1.83
--- src/sys/opencrypto/crypto.c:1.82	Tue Jun  6 01:47:23 2017
+++ src/sys/opencrypto/crypto.c	Tue Jun  6 01:51:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.82 2017/06/06 01:47:23 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.83 2017/06/06 01:51:39 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.82 2017/06/06 01:47:23 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.83 2017/06/06 01:51:39 knakahara Exp $");
 
 #include 
 #include 
@@ -1011,6 +1011,8 @@ crypto_dispatch(struct cryptop *crp)
 		 * queue it for the swi thread.  This is desirable
 		 * when the operation is low priority and/or suitable
 		 * for batching.
+		 *
+		 * don't care list order in batch job.
 		 */
 		mutex_enter(_q_mtx);
 		wasempty  = TAILQ_EMPTY(_q);
@@ -1022,6 +1024,7 @@ crypto_dispatch(struct cryptop *crp)
 		return 0;
 	}
 
+	mutex_enter(_q_mtx);
 	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(crp->crp_sid));
 	/*
 	 * TODO:
@@ -1033,10 +1036,8 @@ crypto_dispatch(struct cryptop *crp)
 		 * The driver must be detached, so this request will migrate
 		 * to other drivers in cryptointr() later.
 		 */
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		mutex_exit(_q_mtx);
-
 		return 0;
 	}
 
@@ -1046,10 +1047,8 @@ crypto_dispatch(struct cryptop *crp)
 		 * The driver is blocked, just queue the op until
 		 * it unblocks and the swi thread gets kicked.
 		 */
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		mutex_exit(_q_mtx);
-
 		return 0;
 	}
 
@@ -1069,10 +1068,8 @@ crypto_dispatch(struct cryptop *crp)
 		crypto_driver_lock(cap);
 		cap->cc_qblocked = 1;
 		crypto_driver_unlock(cap);
-		mutex_enter(_q_mtx);
 		TAILQ_INSERT_HEAD(_q, crp, crp_next);
 		cryptostats.cs_blocks++;
-		mutex_exit(_q_mtx);
 
 		/*
 		 * The crp is enqueued to crp_q, that is,
@@ -1082,6 +1079,7 @@ crypto_dispatch(struct cryptop *crp)
 		result = 0;
 	}
 
+	mutex_exit(_q_mtx);
 	return result;
 }
 
@@ -1639,11 +1637,9 @@ cryptointr(void)
 		}
 		if (submit != NULL) {
 			TAILQ_REMOVE(_q, submit, crp_next);
-			mutex_exit(_q_mtx);
 			result = crypto_invoke(submit, hint);
 			/* we must take here as the TAILQ op or kinvoke
 			   may need this mutex below.  sigh. */
-			mutex_enter(_q_mtx);
 			if (result == ERESTART) {
 /*
  * The driver ran out of resources, mark the
@@ -1685,10 +1681,8 @@ cryptointr(void)
 		}
 		if (krp != NULL) {
 			TAILQ_REMOVE(_kq, krp, krp_next);
-			mutex_exit(_q_mtx);
 			result = crypto_kinvoke(krp, 0);
 			/* the next iteration will want the mutex. :-/ */
-			mutex_enter(_q_mtx);
 			if (result == ERESTART) {
 /*
  * The driver ran out of resources, mark the



CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jun  6 01:48:34 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.h

Log Message:
add locking notes.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.35 src/sys/opencrypto/cryptodev.h:1.36
--- src/sys/opencrypto/cryptodev.h:1.35	Tue Jun  6 01:45:57 2017
+++ src/sys/opencrypto/cryptodev.h	Tue Jun  6 01:48:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.35 2017/06/06 01:45:57 knakahara Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.36 2017/06/06 01:48:33 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -646,4 +646,18 @@ extern int	cuio_getptr(struct uio *, int
 #endif
 
 #endif /* _KERNEL */
+/*
+ * Locking notes:
+ * + crypto_drivers itself is protected by crypto_drv_mtx (an adaptive lock)
+ * + crypto_drivers[i] and its all members are protected by
+ *   crypto_drivers[i].cc_lock (a spin lock)
+ *   spin lock as crypto_unblock() can be called in interrupt context
+ * + crp_q and crp_kq are procted by crypto_q_mtx (an adaptive lock)
+ * + crp_ret_q, crp_ret_kq and crypto_exit_flag are protected by
+ *   crypto_ret_q_mtx (a spin lock)
+ *   spin lock as crypto_done() can be called in interrupt context
+ *
+ * Locking order:
+ * - crypto_q_mtx => crypto_drv_mtx => crypto_drivers[i].cc_lock
+ */
 #endif /* _CRYPTO_CRYPTO_H_ */



CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jun  6 01:47:23 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
restructure locks(2/2): crypto_q_mtx can be adaptive now.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.81 src/sys/opencrypto/crypto.c:1.82
--- src/sys/opencrypto/crypto.c:1.81	Tue Jun  6 01:45:57 2017
+++ src/sys/opencrypto/crypto.c	Tue Jun  6 01:47:23 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.81 2017/06/06 01:45:57 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.82 2017/06/06 01:47:23 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.81 2017/06/06 01:45:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.82 2017/06/06 01:47:23 knakahara Exp $");
 
 #include 
 #include 
@@ -393,7 +393,7 @@ crypto_init0(void)
 	int error;
 
 	mutex_init(_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
-	mutex_init(_q_mtx, MUTEX_DEFAULT, IPL_NET);
+	mutex_init(_q_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
 	cv_init(_cv, "crypto_w");
 	pool_init(_pool, sizeof(struct cryptop), 0, 0,  
@@ -442,12 +442,12 @@ crypto_destroy(bool exit_kthread)
 		struct cryptocap *cap = NULL;
 
 		/* if we have any in-progress requests, don't unload */
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq)) {
-			mutex_spin_exit(_q_mtx);
+			mutex_exit(_q_mtx);
 			return EBUSY;
 		}
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 		/* FIXME:
 		 * prohibit enqueue to crp_q and crp_kq after here.
 		 */
@@ -1012,10 +1012,10 @@ crypto_dispatch(struct cryptop *crp)
 		 * when the operation is low priority and/or suitable
 		 * for batching.
 		 */
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		wasempty  = TAILQ_EMPTY(_q);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 		if (wasempty)
 			setsoftcrypto(softintr_cookie);
 
@@ -1033,9 +1033,9 @@ crypto_dispatch(struct cryptop *crp)
 		 * The driver must be detached, so this request will migrate
 		 * to other drivers in cryptointr() later.
 		 */
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		return 0;
 	}
@@ -1046,9 +1046,9 @@ crypto_dispatch(struct cryptop *crp)
 		 * The driver is blocked, just queue the op until
 		 * it unblocks and the swi thread gets kicked.
 		 */
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		return 0;
 	}
@@ -1069,10 +1069,10 @@ crypto_dispatch(struct cryptop *crp)
 		crypto_driver_lock(cap);
 		cap->cc_qblocked = 1;
 		crypto_driver_unlock(cap);
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_HEAD(_q, crp, crp_next);
 		cryptostats.cs_blocks++;
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		/*
 		 * The crp is enqueued to crp_q, that is,
@@ -1106,9 +1106,9 @@ crypto_kdispatch(struct cryptkop *krp)
 	 * done crypto_unregister(), this migrate operation is not required.
 	 */
 	if (cap == NULL) {
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		return 0;
 	}
@@ -1119,9 +1119,9 @@ crypto_kdispatch(struct cryptkop *krp)
 		 * The driver is blocked, just queue the op until
 		 * it unblocks and the swi thread gets kicked.
 		 */
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		return 0;
 	}
@@ -1137,10 +1137,10 @@ crypto_kdispatch(struct cryptkop *krp)
 		crypto_driver_lock(cap);
 		cap->cc_kqblocked = 1;
 		crypto_driver_unlock(cap);
-		mutex_spin_enter(_q_mtx);
+		mutex_enter(_q_mtx);
 		TAILQ_INSERT_HEAD(_kq, krp, krp_next);
 		cryptostats.cs_kblocks++;
-		mutex_spin_exit(_q_mtx);
+		mutex_exit(_q_mtx);
 
 		/*
 		 * The krp is enqueued to crp_kq, that is,
@@ -1583,7 +1583,7 @@ cryptointr(void)
 	int result, hint;
 
 	cryptostats.cs_intrs++;
-	mutex_spin_enter(_q_mtx);
+	mutex_enter(_q_mtx);
 	do {
 		/*
 		 * Find the first element in the queue that can be
@@ -1639,11 +1639,11 @@ cryptointr(void)
 		}
 		if (submit != NULL) {
 			TAILQ_REMOVE(_q, submit, crp_next);
-			mutex_spin_exit(_q_mtx);
+			mutex_exit(_q_mtx);
 			result = crypto_invoke(submit, hint);
 			/* we must take here as the TAILQ op or kinvoke
 			   may need this mutex below.  sigh. */
-			mutex_spin_enter(_q_mtx);
+			mutex_enter(_q_mtx);
 			

CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue Jun  6 01:45:57 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.h

Log Message:
restructure locks(1/2): make relation between lock and data explicit.

+ crypto_drv_mtx protects
  -  whole crypto_drivers
+ crypto_drivers[i].cc_lock (new) protects
  - crypto_drivers[i] itself
  - member of crypto_drivers[i]
+ crypto_q_mtx protects
  - crp_q
  - crp_kq
+ crypto_ret_q_mtx protects
  - crp_ret_q
  - crp_ret_kq
  - crypto_exit_flag

I will add locking note later.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.34 -r1.35 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.80 src/sys/opencrypto/crypto.c:1.81
--- src/sys/opencrypto/crypto.c:1.80	Mon Jun  5 09:09:13 2017
+++ src/sys/opencrypto/crypto.c	Tue Jun  6 01:45:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.80 2017/06/05 09:09:13 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.81 2017/06/06 01:45:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.80 2017/06/05 09:09:13 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.81 2017/06/06 01:45:57 knakahara Exp $");
 
 #include 
 #include 
@@ -374,8 +374,11 @@ static	int crypto_destroy(bool);
 static	int crypto_invoke(struct cryptop *crp, int hint);
 static	int crypto_kinvoke(struct cryptkop *krp, int hint);
 
-static struct cryptocap *crypto_checkdriver(u_int32_t);
+static struct cryptocap *crypto_checkdriver_lock(u_int32_t);
 static struct cryptocap *crypto_checkdriver_uninit(u_int32_t);
+static void crypto_driver_lock(struct cryptocap *);
+static void crypto_driver_unlock(struct cryptocap *);
+static void crypto_driver_clear(struct cryptocap *);
 
 static struct cryptostats cryptostats;
 #ifdef CRYPTO_TIMING
@@ -438,26 +441,33 @@ crypto_destroy(bool exit_kthread)
 	if (exit_kthread) {
 		struct cryptocap *cap = NULL;
 
-		mutex_spin_enter(_ret_q_mtx);
-
 		/* if we have any in-progress requests, don't unload */
+		mutex_spin_enter(_q_mtx);
 		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq)) {
-			mutex_spin_exit(_ret_q_mtx);
+			mutex_spin_exit(_q_mtx);
 			return EBUSY;
 		}
+		mutex_spin_exit(_q_mtx);
+		/* FIXME:
+		 * prohibit enqueue to crp_q and crp_kq after here.
+		 */
 
+		mutex_enter(_drv_mtx);
 		for (i = 0; i < crypto_drivers_num; i++) {
 			cap = crypto_checkdriver_uninit(i);
 			if (cap == NULL)
 continue;
-			if (cap->cc_sessions != 0)
-break;
-		}
-		if (cap != NULL) {
-			mutex_spin_exit(_ret_q_mtx);
-			return EBUSY;
+			if (cap->cc_sessions != 0) {
+mutex_exit(_drv_mtx);
+return EBUSY;
+			}
 		}
+		mutex_exit(_drv_mtx);
+		/* FIXME:
+		 * prohibit touch crypto_drivers[] and each element after here.
+		 */
 
+		mutex_spin_enter(_ret_q_mtx);
 		/* kick the cryptoret thread and wait for it to exit */
 		crypto_exit_flag = 1;
 		cv_signal(_cv);
@@ -516,20 +526,28 @@ crypto_newsession(u_int64_t *sid, struct
 		if (cap == NULL)
 			continue;
 
+		crypto_driver_lock(cap);
+
 		/*
 		 * If it's not initialized or has remaining sessions
 		 * referencing it, skip.
 		 */
 		if (cap->cc_newsession == NULL ||
-		(cap->cc_flags & CRYPTOCAP_F_CLEANUP))
+		(cap->cc_flags & CRYPTOCAP_F_CLEANUP)) {
+			crypto_driver_unlock(cap);
 			continue;
+		}
 
 		/* Hardware required -- ignore software drivers. */
-		if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE))
+		if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE)) {
+			crypto_driver_unlock(cap);
 			continue;
+		}
 		/* Software required -- ignore hardware drivers. */
-		if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
+		if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0) {
+			crypto_driver_unlock(cap);
 			continue;
+		}
 
 		/* See if all the algorithms are supported. */
 		for (cr = cri; cr; cr = cr->cri_next)
@@ -560,9 +578,12 @@ crypto_newsession(u_int64_t *sid, struct
 DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
 	hid, err);
 			}
+			crypto_driver_unlock(cap);
 			goto done;
 			/*break;*/
 		}
+
+		crypto_driver_unlock(cap);
 	}
 done:
 	mutex_exit(_drv_mtx);
@@ -579,14 +600,10 @@ crypto_freesession(u_int64_t sid)
 	struct cryptocap *cap;
 	int err = 0;
 
-	mutex_enter(_drv_mtx);
-
 	/* Determine two IDs. */
-	cap = crypto_checkdriver(CRYPTO_SESID2HID(sid));
-	if (cap == NULL) {
-		err = ENOENT;
-		goto done;
-	}
+	cap = crypto_checkdriver_lock(CRYPTO_SESID2HID(sid));
+	if (cap == NULL)
+		return ENOENT;
 
 	if (cap->cc_sessions)
 		(cap->cc_sessions)--;
@@ -602,10 +619,9 @@ 

CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jun  5 09:09:13 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix reading crp_q without holding crypto_q_mtx


To generate a diff of this commit:
cvs rdiff -u -r1.79 -r1.80 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.79 src/sys/opencrypto/crypto.c:1.80
--- src/sys/opencrypto/crypto.c:1.79	Mon Jun  5 09:07:46 2017
+++ src/sys/opencrypto/crypto.c	Mon Jun  5 09:09:13 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.79 2017/06/05 09:07:46 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.80 2017/06/05 09:09:13 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.79 2017/06/05 09:07:46 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.80 2017/06/05 09:09:13 knakahara Exp $");
 
 #include 
 #include 
@@ -939,7 +939,7 @@ crypto_dispatch(struct cryptop *crp)
 #endif
 
 	if ((crp->crp_flags & CRYPTO_F_BATCH) != 0) {
-		int wasempty = TAILQ_EMPTY(_q);
+		int wasempty;
 		/*
 		 * Caller marked the request as ``ok to delay'';
 		 * queue it for the swi thread.  This is desirable
@@ -947,6 +947,7 @@ crypto_dispatch(struct cryptop *crp)
 		 * for batching.
 		 */
 		mutex_spin_enter(_q_mtx);
+		wasempty  = TAILQ_EMPTY(_q);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		mutex_spin_exit(_q_mtx);
 		if (wasempty)



CVS commit: src/sys/opencrypto

2017-06-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Jun  5 09:07:46 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
use crypto_checkdriver_uninit() when it may touch uninitialized crypto_drivers.


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.78 src/sys/opencrypto/crypto.c:1.79
--- src/sys/opencrypto/crypto.c:1.78	Wed May 31 02:17:49 2017
+++ src/sys/opencrypto/crypto.c	Mon Jun  5 09:07:46 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.78 2017/05/31 02:17:49 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.79 2017/06/05 09:07:46 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.78 2017/05/31 02:17:49 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.79 2017/06/05 09:07:46 knakahara Exp $");
 
 #include 
 #include 
@@ -375,6 +375,7 @@ static	int crypto_invoke(struct cryptop 
 static	int crypto_kinvoke(struct cryptkop *krp, int hint);
 
 static struct cryptocap *crypto_checkdriver(u_int32_t);
+static struct cryptocap *crypto_checkdriver_uninit(u_int32_t);
 
 static struct cryptostats cryptostats;
 #ifdef CRYPTO_TIMING
@@ -446,7 +447,7 @@ crypto_destroy(bool exit_kthread)
 		}
 
 		for (i = 0; i < crypto_drivers_num; i++) {
-			cap = crypto_checkdriver(i);
+			cap = crypto_checkdriver_uninit(i);
 			if (cap == NULL)
 continue;
 			if (cap->cc_sessions != 0)
@@ -511,7 +512,7 @@ crypto_newsession(u_int64_t *sid, struct
 	 */
 
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = crypto_checkdriver(hid);
+		cap = crypto_checkdriver_uninit(hid);
 		if (cap == NULL)
 			continue;
 
@@ -623,7 +624,7 @@ crypto_get_driverid(u_int32_t flags)
 
 	mutex_enter(_drv_mtx);
 	for (i = 0; i < crypto_drivers_num; i++) {
-		cap = crypto_checkdriver(i);
+		cap = crypto_checkdriver_uninit(i);
 		if (cap == NULL)
 			continue;
 		if (cap->cc_process == NULL &&
@@ -657,7 +658,7 @@ crypto_get_driverid(u_int32_t flags)
 		free(crypto_drivers, M_CRYPTO_DATA);
 		crypto_drivers = newdrv;
 
-		cap = crypto_checkdriver(i);
+		cap = crypto_checkdriver_uninit(i);
 		KASSERT(cap != NULL);
 	}
 
@@ -676,8 +677,25 @@ crypto_get_driverid(u_int32_t flags)
 static struct cryptocap *
 crypto_checkdriver(u_int32_t hid)
 {
+
+	KASSERT(crypto_drivers != NULL);
+
+	return (hid >= crypto_drivers_num ? NULL : _drivers[hid]);
+}
+
+/*
+ * Use crypto_checkdriver_uninit() instead of crypto_checkdriver() below two
+ * situations
+ * - crypto_drivers[] may not be allocated
+ * - crypto_drivers[hid] may not be initialized
+ */
+static struct cryptocap *
+crypto_checkdriver_uninit(u_int32_t hid)
+{
+
 	if (crypto_drivers == NULL)
 		return NULL;
+
 	return (hid >= crypto_drivers_num ? NULL : _drivers[hid]);
 }
 
@@ -1087,7 +1105,7 @@ crypto_kinvoke(struct cryptkop *krp, int
 
 	mutex_enter(_drv_mtx);
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = crypto_checkdriver(hid);
+		cap = crypto_checkdriver_uninit(hid);
 		if (cap == NULL)
 			continue;
 		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
@@ -1447,7 +1465,7 @@ crypto_getfeat(int *featp)
 
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
 		struct cryptocap *cap;
-		cap = crypto_checkdriver(hid);
+		cap = crypto_checkdriver_uninit(hid);
 		if (cap == NULL)
 			continue;
 



CVS commit: src/sys/opencrypto

2017-06-02 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Jun  2 09:46:57 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c cryptodev_internal.h ocryptodev.c

Log Message:
rename crypto_mtx to cryptodev_mtx

It is used by cryptodev.c and ocryptodev.c only.


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.2 -r1.3 src/sys/opencrypto/cryptodev_internal.h
cvs rdiff -u -r1.7 -r1.8 src/sys/opencrypto/ocryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.91 src/sys/opencrypto/cryptodev.c:1.92
--- src/sys/opencrypto/cryptodev.c:1.91	Thu May 25 05:24:57 2017
+++ src/sys/opencrypto/cryptodev.c	Fri Jun  2 09:46:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.91 2017/05/25 05:24:57 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.92 2017/06/02 09:46:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.91 2017/05/25 05:24:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.92 2017/06/02 09:46:57 knakahara Exp $");
 
 #include 
 #include 
@@ -98,7 +98,7 @@ __KERNEL_RCSID(0, "$NetBSD: cryptodev.c,
 
 #include "ioconf.h"
 
-kmutex_t crypto_mtx;
+kmutex_t cryptodev_mtx;
 
 struct csession {
 	TAILQ_ENTRY(csession) next;
@@ -247,16 +247,16 @@ cryptof_ioctl(struct file *fp, u_long cm
 	struct fcrypt *criofcr;
 	int criofd;
 
-	mutex_enter(_mtx);
+	mutex_enter(_mtx);
 	getnanotime(>atime);
-	mutex_exit(_mtx);
+	mutex_exit(_mtx);
 
 	switch (cmd) {
 case CRIOGET:   /* XXX deprecated, remove after 5.0 */
 		if ((error = fd_allocfile(, )) != 0)
 			return error;
 		criofcr = pool_get(, PR_WAITOK);
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		TAILQ_INIT(>csessions);
 		TAILQ_INIT(>crp_ret_mq);
 		TAILQ_INIT(>crp_ret_mkq);
@@ -269,7 +269,7 @@ cryptof_ioctl(struct file *fp, u_long cm
 		criofcr->sesn = 1;
 		criofcr->requestid = 1;
 		crypto_refcount++;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		(void)fd_clone(criofp, criofd, (FREAD|FWRITE),
 			  , criofcr);
 		*(u_int32_t *)data = criofd;
@@ -289,9 +289,9 @@ cryptof_ioctl(struct file *fp, u_long cm
 			goto mbail;
 		}
 
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		error = cryptodev_msession(fcr, snop, sgop->count);
 		if (error) {
 			goto mbail;
@@ -303,22 +303,22 @@ mbail:
 		kmem_free(snop, sgop->count * sizeof(struct session_n_op));
 		break;
 	case CIOCFSESSION:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
 		ses = *(u_int32_t *)data;
 		cse = csefind(fcr, ses);
 		if (cse == NULL) {
-			mutex_exit(_mtx);
+			mutex_exit(_mtx);
 			return EINVAL;
 		}
 		csedelete(fcr, cse);
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		error = csefree(cse);
 		break;
 	case CIOCNFSESSION:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		sfop = (struct crypt_sfop *)data;
 		sesid = kmem_alloc((sfop->count * sizeof(u_int32_t)), 
 		KM_SLEEP);
@@ -330,11 +330,11 @@ mbail:
 		kmem_free(sesid, (sfop->count * sizeof(u_int32_t)));
 		break;
 	case CIOCCRYPT:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
 		cop = (struct crypt_op *)data;
 		cse = csefind(fcr, cop->ses);
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		if (cse == NULL) {
 			DPRINTF("csefind failed\n");
 			return EINVAL;
@@ -343,9 +343,9 @@ mbail:
 		DPRINTF("cryptodev_op error = %d\n", error);
 		break;
 	case CIOCNCRYPTM:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		mop = (struct crypt_mop *)data;
 		cnop = kmem_alloc((mop->count * sizeof(struct crypt_n_op)),
 		KM_SLEEP);
@@ -365,9 +365,9 @@ mbail:
 		DPRINTF("cryptodev_key error = %d\n", error);
 		break;
 	case CIOCNFKEYM:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		mkop = (struct crypt_mkop *)data;
 		knop = kmem_alloc((mkop->count * sizeof(struct crypt_n_kop)),
 		KM_SLEEP);
@@ -385,9 +385,9 @@ mbail:
 		error = crypto_getfeat((int *)data);
 		break;
 	case CIOCNCRYPTRETM:
-		mutex_enter(_mtx);
+		mutex_enter(_mtx);
 		fcr->mtime = fcr->atime;
-		mutex_exit(_mtx);
+		mutex_exit(_mtx);
 		crypt_ret = (struct cryptret *)data;
 		count = crypt_ret->count;
 		crypt_res = kmem_alloc((count * sizeof(struct crypt_result)),  
@@ -632,7 +632,7 @@ cryptodev_op(struct csession *cse, struc
 	 * XXX splcrypto() but needed to only if CRYPTO_F_CBIMM,
 	 * XXX disabled on NetBSD since 1.6O due to a race condition.
 	 * XXX But crypto_dispatch went to splcrypto() 

CVS commit: src/sys/opencrypto

2017-06-01 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Jun  1 08:49:35 UTC 2017

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
swcrypto0 was initialized twice. Fix like pseudo network interfaces.

ok by pgoyette@n.o.


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.50 src/sys/opencrypto/cryptosoft.c:1.51
--- src/sys/opencrypto/cryptosoft.c:1.50	Wed May 17 06:33:04 2017
+++ src/sys/opencrypto/cryptosoft.c	Thu Jun  1 08:49:35 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.50 2017/05/17 06:33:04 knakahara Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.51 2017/06/01 08:49:35 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.50 2017/05/17 06:33:04 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.51 2017/06/01 08:49:35 knakahara Exp $");
 
 #include 
 #include 
@@ -1325,8 +1325,10 @@ swcr_init(void)
 void
 swcryptoattach(int num)
 {
-
-	swcr_init();
+	/*
+	 * Nothing to do here, initialization is handled by the
+	 * module initialization code in swcrypto_attach() below).
+	 */
 }
 
 void	swcrypto_attach(device_t, device_t, void *);



CVS commit: src/sys/opencrypto

2017-05-30 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 31 02:17:49 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix: crypto_unregister didn't work.


To generate a diff of this commit:
cvs rdiff -u -r1.77 -r1.78 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.77 src/sys/opencrypto/crypto.c:1.78
--- src/sys/opencrypto/crypto.c:1.77	Mon May 29 09:02:46 2017
+++ src/sys/opencrypto/crypto.c	Wed May 31 02:17:49 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.77 2017/05/29 09:02:46 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.78 2017/05/31 02:17:49 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.77 2017/05/29 09:02:46 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.78 2017/05/31 02:17:49 knakahara Exp $");
 
 #include 
 #include 
@@ -790,7 +790,7 @@ crypto_unregister_locked(u_int32_t drive
 
 	KASSERT(mutex_owned(_drv_mtx));
 
-	if (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)
+	if (alg < CRYPTO_ALGORITHM_MIN || CRYPTO_ALGORITHM_MAX < alg)
 		return EINVAL;
 
 	cap = crypto_checkdriver(driverid);



CVS commit: src/sys/opencrypto

2017-05-29 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon May 29 09:02:46 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
strictly use crypto_checkdriver(i) instead of using crypto_drivers[i] directly.


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.76 src/sys/opencrypto/crypto.c:1.77
--- src/sys/opencrypto/crypto.c:1.76	Thu May 25 05:24:57 2017
+++ src/sys/opencrypto/crypto.c	Mon May 29 09:02:46 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.76 2017/05/25 05:24:57 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.77 2017/05/29 09:02:46 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.76 2017/05/25 05:24:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.77 2017/05/29 09:02:46 knakahara Exp $");
 
 #include 
 #include 
@@ -99,6 +99,7 @@ int crypto_ret_q_check(struct cryptop *)
  * each algorithm they support with crypto_register() and crypto_kregister().
  */
 static kmutex_t crypto_drv_mtx;
+/* Don't directly access crypto_drivers[i], use crypto_checkdriver(i). */
 static	struct cryptocap *crypto_drivers;
 static	int crypto_drivers_num;
 static	void *softintr_cookie;
@@ -373,6 +374,8 @@ static	int crypto_destroy(bool);
 static	int crypto_invoke(struct cryptop *crp, int hint);
 static	int crypto_kinvoke(struct cryptkop *krp, int hint);
 
+static struct cryptocap *crypto_checkdriver(u_int32_t);
+
 static struct cryptostats cryptostats;
 #ifdef CRYPTO_TIMING
 static	int crypto_timing = 0;
@@ -432,6 +435,8 @@ crypto_destroy(bool exit_kthread)
 	int i;
 
 	if (exit_kthread) {
+		struct cryptocap *cap = NULL;
+
 		mutex_spin_enter(_ret_q_mtx);
 
 		/* if we have any in-progress requests, don't unload */
@@ -440,10 +445,14 @@ crypto_destroy(bool exit_kthread)
 			return EBUSY;
 		}
 
-		for (i = 0; i < crypto_drivers_num; i++)
-			if (crypto_drivers[i].cc_sessions != 0)
+		for (i = 0; i < crypto_drivers_num; i++) {
+			cap = crypto_checkdriver(i);
+			if (cap == NULL)
+continue;
+			if (cap->cc_sessions != 0)
 break;
-		if (i < crypto_drivers_num) {
+		}
+		if (cap != NULL) {
 			mutex_spin_exit(_ret_q_mtx);
 			return EBUSY;
 		}
@@ -487,14 +496,12 @@ int
 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
 {
 	struct cryptoini *cr;
+	struct cryptocap *cap;
 	u_int32_t hid, lid;
 	int err = EINVAL;
 
 	mutex_enter(_drv_mtx);
 
-	if (crypto_drivers == NULL)
-		goto done;
-
 	/*
 	 * The algorithm we use here is pretty stupid; just use the
 	 * first driver that supports all the algorithms we need.
@@ -504,26 +511,28 @@ crypto_newsession(u_int64_t *sid, struct
 	 */
 
 	for (hid = 0; hid < crypto_drivers_num; hid++) {
+		cap = crypto_checkdriver(hid);
+		if (cap == NULL)
+			continue;
+
 		/*
 		 * If it's not initialized or has remaining sessions
 		 * referencing it, skip.
 		 */
-		if (crypto_drivers[hid].cc_newsession == NULL ||
-		(crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP))
+		if (cap->cc_newsession == NULL ||
+		(cap->cc_flags & CRYPTOCAP_F_CLEANUP))
 			continue;
 
 		/* Hardware required -- ignore software drivers. */
-		if (hard > 0 &&
-		(crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE))
+		if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE))
 			continue;
 		/* Software required -- ignore hardware drivers. */
-		if (hard < 0 &&
-		(crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
+		if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
 			continue;
 
 		/* See if all the algorithms are supported. */
 		for (cr = cri; cr; cr = cr->cri_next)
-			if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0) {
+			if (cap->cc_alg[cr->cri_alg] == 0) {
 DPRINTF("alg %d not supported\n", cr->cri_alg);
 break;
 			}
@@ -540,13 +549,12 @@ crypto_newsession(u_int64_t *sid, struct
 
 			/* Call the driver initialization routine. */
 			lid = hid;		/* Pass the driver ID. */
-			err = crypto_drivers[hid].cc_newsession(
-	crypto_drivers[hid].cc_arg, , cri);
+			err = cap->cc_newsession(cap->cc_arg, , cri);
 			if (err == 0) {
 (*sid) = hid;
 (*sid) <<= 32;
 (*sid) |= (lid & 0x);
-crypto_drivers[hid].cc_sessions++;
+(cap->cc_sessions)++;
 			} else {
 DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
 	hid, err);
@@ -567,32 +575,24 @@ done:
 int
 crypto_freesession(u_int64_t sid)
 {
-	u_int32_t hid;
+	struct cryptocap *cap;
 	int err = 0;
 
 	mutex_enter(_drv_mtx);
 
-	if (crypto_drivers == NULL) {
-		err = EINVAL;
-		goto done;
-	}
-
 	/* Determine two IDs. */
-	hid = CRYPTO_SESID2HID(sid);
-
-	if (hid >= 

CVS commit: src/sys/opencrypto

2017-05-24 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu May 25 05:24:57 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.c cryptodev.h

Log Message:
add cryptkop alloc/free KPI instead of manipulating cryptkop_pool directly.


To generate a diff of this commit:
cvs rdiff -u -r1.75 -r1.76 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.90 -r1.91 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.33 -r1.34 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.75 src/sys/opencrypto/crypto.c:1.76
--- src/sys/opencrypto/crypto.c:1.75	Wed May 24 10:05:09 2017
+++ src/sys/opencrypto/crypto.c	Thu May 25 05:24:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.75 2017/05/24 10:05:09 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.76 2017/05/25 05:24:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.75 2017/05/24 10:05:09 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.76 2017/05/25 05:24:57 knakahara Exp $");
 
 #include 
 #include 
@@ -1076,7 +1076,7 @@ crypto_kinvoke(struct cryptkop *krp, int
 	/* Sanity checks. */
 	if (krp->krp_callback == NULL) {
 		cv_destroy(>krp_cv);
-		pool_put(_pool, krp);
+		crypto_kfreereq(krp);
 		return EINVAL;
 	}
 
@@ -1262,6 +1262,54 @@ crypto_getreq(int num)
 }
 
 /*
+ * Release a set of asymmetric crypto descriptors.
+ * Currently, support one descriptor only.
+ */
+void
+crypto_kfreereq(struct cryptkop *krp)
+{
+
+	if (krp == NULL)
+		return;
+
+	DPRINTF("krp %p\n", krp);
+
+	/* sanity check */
+	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
+		panic("crypto_kfreereq() freeing krp on RETQ\n");
+	}
+
+	pool_put(_pool, krp);
+}
+
+/*
+ * Acquire a set of asymmetric crypto descriptors.
+ * Currently, support one descriptor only.
+ */
+struct cryptkop *
+crypto_kgetreq(int num __unused, int prflags)
+{
+	struct cryptkop *krp;
+
+	/*
+	 * When crp_ret_kq is full, we restrict here to avoid crp_ret_kq
+	 * overflow by error callback.
+	 */
+	if (CRYPTO_Q_IS_FULL(crp_ret_kq)) {
+		CRYPTO_Q_INC_DROPS(crp_ret_kq);
+		return NULL;
+	}
+
+	krp = pool_get(_pool, prflags);
+	if (krp == NULL) {
+		return NULL;
+	}
+	memset(krp, 0, sizeof(struct cryptkop));
+
+	return krp;
+}
+
+/*
  * Invoke the callback on behalf of the driver.
  */
 void

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.90 src/sys/opencrypto/cryptodev.c:1.91
--- src/sys/opencrypto/cryptodev.c:1.90	Wed May 17 06:33:04 2017
+++ src/sys/opencrypto/cryptodev.c	Thu May 25 05:24:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.90 2017/05/17 06:33:04 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.91 2017/05/25 05:24:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.90 2017/05/17 06:33:04 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.91 2017/05/25 05:24:57 knakahara Exp $");
 
 #include 
 #include 
@@ -855,7 +855,11 @@ cryptodev_key(struct crypt_kop *kop)
 		return EINVAL;
 	}
 
-	krp = pool_get(_pool, PR_WAITOK);
+	krp = crypto_kgetreq(1, PR_WAITOK);
+	if (krp == NULL) {
+		/* limited by opencrypto.crypto_ret_kq.maxlen */
+		return ENOMEM;
+	}
 	(void)memset(krp, 0, sizeof *krp);
 	cv_init(>krp_cv, "crykdev");
 	krp->krp_op = kop->crk_op;
@@ -923,7 +927,7 @@ fail:
 		}
 	}
 	cv_destroy(>krp_cv);
-	pool_put(_pool, krp);
+	crypto_kfreereq(krp);
 	DPRINTF("error=0x%08x\n", error);
 	return error;
 }
@@ -1435,7 +1439,11 @@ cryptodev_mkey(struct fcrypt *fcr, struc
 			continue;
 		}
 
-		krp = pool_get(_pool, PR_WAITOK);
+		krp = crypto_kgetreq(1, PR_WAITOK);
+		if (krp == NULL) {
+			/* limited by opencrypto.crypto_ret_kq.maxlen */
+			continue;
+		}
 		(void)memset(krp, 0, sizeof *krp);
 		cv_init(>krp_cv, "crykdev");
 		krp->krp_op = kop[req].crk_op;
@@ -1493,7 +1501,7 @@ fail:
 	}
 }
 cv_destroy(>krp_cv);
-pool_put(_pool, krp);
+crypto_kfreereq(krp);
 			}
 		}
 		error = 0;
@@ -1912,7 +1920,7 @@ fail:
 	}
 }
 cv_destroy(>krp_cv);
-pool_put(_pool, krp);
+crypto_kfreereq(krp);
 req++;
 			}
 		}
@@ -2012,7 +2020,7 @@ fail:
 }
 			}
 			cv_destroy(>krp_cv);
-			pool_put(_pool, krp);
+			crypto_kfreereq(krp);
 			return 0;
 		}
 	}

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.33 src/sys/opencrypto/cryptodev.h:1.34
--- src/sys/opencrypto/cryptodev.h:1.33	Thu May 25 05:22:55 2017
+++ src/sys/opencrypto/cryptodev.h	Thu May 25 05:24:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.33 

CVS commit: src/sys/opencrypto

2017-05-24 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu May 25 05:22:56 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.h

Log Message:
remove obsoleted declarations.


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.32 src/sys/opencrypto/cryptodev.h:1.33
--- src/sys/opencrypto/cryptodev.h:1.32	Wed May 17 06:33:04 2017
+++ src/sys/opencrypto/cryptodev.h	Thu May 25 05:22:55 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.32 2017/05/17 06:33:04 knakahara Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.33 2017/05/25 05:22:55 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -603,8 +603,6 @@ void	cuio_copyback(struct uio *, int, in
 int	cuio_apply(struct uio *, int, int,
 	int (*f)(void *, void *, unsigned int), void *);
 
-extern	int crypto_ret_q_remove(struct cryptop *);
-extern	int crypto_ret_kq_remove(struct cryptkop *);
 extern	void crypto_freereq(struct cryptop *crp);
 extern	struct cryptop *crypto_getreq(int num);
 



CVS commit: src/sys/opencrypto

2017-05-24 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 24 10:05:09 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c files.opencrypto

Log Message:
make the default values of q_maxlen build parameters.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.25 -r1.26 src/sys/opencrypto/files.opencrypto

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.74 src/sys/opencrypto/crypto.c:1.75
--- src/sys/opencrypto/crypto.c:1.74	Wed May 24 09:57:36 2017
+++ src/sys/opencrypto/crypto.c	Wed May 24 10:05:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.74 2017/05/24 09:57:36 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.75 2017/05/24 10:05:09 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.74 2017/05/24 09:57:36 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.75 2017/05/24 10:05:09 knakahara Exp $");
 
 #include 
 #include 
@@ -131,9 +131,8 @@ static	TAILQ_HEAD(krprethead, cryptkop) 
 #define DEFINIT_CRYPTO_Q_DROPS(name)		\
 	static int crypto_##name##_drops = 0
 
-#define CRYPTO_Q_MAXLEN 0
-#define DEFINIT_CRYPTO_Q_MAXLEN(name)\
-	static int crypto_##name##_maxlen = CRYPTO_Q_MAXLEN
+#define DEFINIT_CRYPTO_Q_MAXLEN(name, defval)		\
+	static int crypto_##name##_maxlen = defval
 
 #define CRYPTO_Q_INC(name)			\
 	do {	\
@@ -166,12 +165,18 @@ DEFINIT_CRYPTO_Q_LEN(crp_ret_kq);
 DEFINIT_CRYPTO_Q_DROPS(crp_ret_q);
 DEFINIT_CRYPTO_Q_DROPS(crp_ret_kq);
 
+#ifndef CRYPTO_RET_Q_MAXLEN
+#define CRYPTO_RET_Q_MAXLEN 0
+#endif
+#ifndef CRYPTO_RET_KQ_MAXLEN
+#define CRYPTO_RET_KQ_MAXLEN 0
+#endif
 /*
  * queue length limit.
  * default value is 0. <=0 means unlimited.
  */
-DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_q);
-DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_kq);
+DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_q, CRYPTO_RET_Q_MAXLEN);
+DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_kq, CRYPTO_RET_KQ_MAXLEN);
 
 /*
  * TODO:

Index: src/sys/opencrypto/files.opencrypto
diff -u src/sys/opencrypto/files.opencrypto:1.25 src/sys/opencrypto/files.opencrypto:1.26
--- src/sys/opencrypto/files.opencrypto:1.25	Sat Nov 19 22:51:30 2011
+++ src/sys/opencrypto/files.opencrypto	Wed May 24 10:05:09 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: files.opencrypto,v 1.25 2011/11/19 22:51:30 tls Exp $
+#	$NetBSD: files.opencrypto,v 1.26 2017/05/24 10:05:09 knakahara Exp $
 #
 #
 
@@ -28,3 +28,4 @@ file	opencrypto/cryptodev.c		crypto
 file	opencrypto/ocryptodev.c		crypto & compat_50
 
 defflag opt_ocf.h	CRYPTO_DEBUG CRYPTO_TIMING
+defparam opt_ocf.h	CRYPTO_RET_Q_MAXLEN CRYPTO_RET_KQ_MAXLEN



CVS commit: src/sys/opencrypto

2017-05-24 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 24 09:57:36 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
implement crypto_ret_q limitation. original code is implemented by hsuenaga@IIJ.


To generate a diff of this commit:
cvs rdiff -u -r1.73 -r1.74 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.73 src/sys/opencrypto/crypto.c:1.74
--- src/sys/opencrypto/crypto.c:1.73	Wed May 24 09:54:35 2017
+++ src/sys/opencrypto/crypto.c	Wed May 24 09:57:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.73 2017/05/24 09:54:35 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.74 2017/05/24 09:57:36 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.73 2017/05/24 09:54:35 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.74 2017/05/24 09:57:36 knakahara Exp $");
 
 #include 
 #include 
@@ -145,6 +145,15 @@ static	TAILQ_HEAD(krprethead, cryptkop) 
 		crypto_##name##_len--;		\
 	} while(0);
 
+#define CRYPTO_Q_INC_DROPS(name)		\
+	do {	\
+		crypto_##name##_drops++;	\
+	} while(0);
+
+#define CRYPTO_Q_IS_FULL(name)	\
+	(crypto_##name##_maxlen > 0\
+	&& (crypto_##name##_len > crypto_##name##_maxlen))
+
 /*
  * current queue length.
  */
@@ -1217,6 +1226,15 @@ crypto_getreq(int num)
 	struct cryptodesc *crd;
 	struct cryptop *crp;
 
+	/*
+	 * When crp_ret_q is full, we restrict here to avoid crp_ret_q overflow
+	 * by error callback.
+	 */
+	if (CRYPTO_Q_IS_FULL(crp_ret_q)) {
+		CRYPTO_Q_INC_DROPS(crp_ret_q);
+		return NULL;
+	}
+
 	crp = pool_get(_pool, 0);
 	if (crp == NULL) {
 		return NULL;



CVS commit: src/sys/opencrypto

2017-05-24 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 24 09:54:35 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
add crypto_ret_{,k}q length sysctl entries and statistics codes.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.72 src/sys/opencrypto/crypto.c:1.73
--- src/sys/opencrypto/crypto.c:1.72	Wed May 24 05:11:29 2017
+++ src/sys/opencrypto/crypto.c	Wed May 24 09:54:35 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.72 2017/05/24 05:11:29 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.73 2017/05/24 09:54:35 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.72 2017/05/24 05:11:29 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.73 2017/05/24 09:54:35 knakahara Exp $");
 
 #include 
 #include 
@@ -125,6 +125,92 @@ static	TAILQ_HEAD(crprethead, cryptop) c
 static	TAILQ_HEAD(krprethead, cryptkop) crp_ret_kq =
 		TAILQ_HEAD_INITIALIZER(crp_ret_kq);
 
+#define DEFINIT_CRYPTO_Q_LEN(name)		\
+	static int crypto_##name##_len = 0
+
+#define DEFINIT_CRYPTO_Q_DROPS(name)		\
+	static int crypto_##name##_drops = 0
+
+#define CRYPTO_Q_MAXLEN 0
+#define DEFINIT_CRYPTO_Q_MAXLEN(name)\
+	static int crypto_##name##_maxlen = CRYPTO_Q_MAXLEN
+
+#define CRYPTO_Q_INC(name)			\
+	do {	\
+		crypto_##name##_len++;		\
+	} while(0);
+
+#define CRYPTO_Q_DEC(name)			\
+	do {	\
+		crypto_##name##_len--;		\
+	} while(0);
+
+/*
+ * current queue length.
+ */
+DEFINIT_CRYPTO_Q_LEN(crp_ret_q);
+DEFINIT_CRYPTO_Q_LEN(crp_ret_kq);
+
+/*
+ * queue dropped count.
+ */
+DEFINIT_CRYPTO_Q_DROPS(crp_ret_q);
+DEFINIT_CRYPTO_Q_DROPS(crp_ret_kq);
+
+/*
+ * queue length limit.
+ * default value is 0. <=0 means unlimited.
+ */
+DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_q);
+DEFINIT_CRYPTO_Q_MAXLEN(crp_ret_kq);
+
+/*
+ * TODO:
+ * make percpu
+ */
+static int
+sysctl_opencrypto_q_len(SYSCTLFN_ARGS)
+{
+	int error;
+
+	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
+	if (error || newp == NULL)
+		return error;
+
+	return 0;
+}
+
+/*
+ * TODO:
+ * make percpu
+ */
+static int
+sysctl_opencrypto_q_drops(SYSCTLFN_ARGS)
+{
+	int error;
+
+	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
+	if (error || newp == NULL)
+		return error;
+
+	return 0;
+}
+
+/*
+ * need to make percpu?
+ */
+static int
+sysctl_opencrypto_q_maxlen(SYSCTLFN_ARGS)
+{
+	int error;
+
+	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
+	if (error || newp == NULL)
+		return error;
+
+	return 0;
+}
+
 /*
  * Crypto op and desciptor data structures are allocated
  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
@@ -152,6 +238,8 @@ int	crypto_devallowsoft = 1;	/* only use
 static void
 sysctl_opencrypto_setup(struct sysctllog **clog)
 {
+	const struct sysctlnode *ocnode;
+	const struct sysctlnode *retqnode, *retkqnode;
 
 	sysctl_createv(clog, 0, NULL, NULL,
 		   CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
@@ -174,6 +262,69 @@ sysctl_opencrypto_setup(struct sysctllog
 			   "asymmetric crypto support"),
 		   NULL, 0, _devallowsoft, 0,
 		   CTL_KERN, CTL_CREATE, CTL_EOL);
+
+	sysctl_createv(clog, 0, NULL, ,
+		   CTLFLAG_PERMANENT,
+		   CTLTYPE_NODE, "opencrypto",
+		   SYSCTL_DESCR("opencrypto related entries"),
+		   NULL, 0, NULL, 0,
+		   CTL_CREATE, CTL_EOL);
+
+	sysctl_createv(clog, 0, , ,
+		   CTLFLAG_PERMANENT,
+		   CTLTYPE_NODE, "crypto_ret_q",
+		   SYSCTL_DESCR("crypto_ret_q related entries"),
+		   NULL, 0, NULL, 0,
+		   CTL_CREATE, CTL_EOL);
+	sysctl_createv(clog, 0, , NULL,
+		   CTLFLAG_PERMANENT|CTLFLAG_READONLY,
+		   CTLTYPE_INT, "len",
+		   SYSCTL_DESCR("Current queue length"),
+		   sysctl_opencrypto_q_len, 0,
+		   (void *)_crp_ret_q_len, 0,
+		   CTL_CREATE, CTL_EOL);
+	sysctl_createv(clog, 0, , NULL,
+		   CTLFLAG_PERMANENT|CTLFLAG_READONLY,
+		   CTLTYPE_INT, "drops",
+		   SYSCTL_DESCR("Crypto requests dropped due to full ret queue"),
+		   sysctl_opencrypto_q_drops, 0,
+		   (void *)_crp_ret_q_drops, 0,
+		   CTL_CREATE, CTL_EOL);
+	sysctl_createv(clog, 0, , NULL,
+		   CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
+		   CTLTYPE_INT, "maxlen",
+		   SYSCTL_DESCR("Maximum allowed queue length"),
+		   sysctl_opencrypto_q_maxlen, 0,
+		   (void *)_crp_ret_q_maxlen, 0,
+		   CTL_CREATE, CTL_EOL);
+
+	sysctl_createv(clog, 0, , ,
+		   CTLFLAG_PERMANENT,
+		   CTLTYPE_NODE, "crypto_ret_kq",
+		   SYSCTL_DESCR("crypto_ret_kq related entries"),
+		   NULL, 0, NULL, 0,
+		   CTL_CREATE, CTL_EOL);
+	sysctl_createv(clog, 0, , NULL,
+		   

CVS commit: src/sys/opencrypto

2017-05-23 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 24 05:11:29 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
initialize sysctl in the same way regardless of module or not.


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.71 src/sys/opencrypto/crypto.c:1.72
--- src/sys/opencrypto/crypto.c:1.71	Wed May 17 12:11:41 2017
+++ src/sys/opencrypto/crypto.c	Wed May 24 05:11:29 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.71 2017/05/17 12:11:41 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.72 2017/05/24 05:11:29 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.71 2017/05/17 12:11:41 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.72 2017/05/24 05:11:29 knakahara Exp $");
 
 #include 
 #include 
@@ -149,7 +149,8 @@ int	crypto_userasymcrypto = 1;	/* userla
  */
 int	crypto_devallowsoft = 1;	/* only use hardware crypto */
 
-SYSCTL_SETUP(sysctl_opencrypto_setup, "sysctl opencrypto subtree setup")
+static void
+sysctl_opencrypto_setup(struct sysctllog **clog)
 {
 
 	sysctl_createv(clog, 0, NULL, NULL,
@@ -212,9 +213,7 @@ static struct cryptostats cryptostats;
 static	int crypto_timing = 0;
 #endif
 
-#ifdef _MODULE
 static struct sysctllog *sysctl_opencrypto_clog;
-#endif
 
 static int
 crypto_init0(void)
@@ -249,9 +248,8 @@ crypto_init0(void)
 		return crypto_destroy(false);
 	}
 
-#ifdef _MODULE
 	sysctl_opencrypto_setup(_opencrypto_clog);
-#endif
+
 	return 0;
 }
 
@@ -294,10 +292,8 @@ crypto_destroy(bool exit_kthread)
 		mutex_spin_exit(_ret_q_mtx);
 	}
 
-#ifdef _MODULE
 	if (sysctl_opencrypto_clog != NULL)
 		sysctl_teardown(_opencrypto_clog);
-#endif
 
 	unregister_swi(SWI_CRYPTO, cryptointr);
 



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 12:11:41 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
decrease the priority of batch crp even if there are more than one batch crp.


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.70 src/sys/opencrypto/crypto.c:1.71
--- src/sys/opencrypto/crypto.c:1.70	Wed May 17 11:04:38 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 12:11:41 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.70 2017/05/17 11:04:38 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.71 2017/05/17 12:11:41 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.70 2017/05/17 11:04:38 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.71 2017/05/17 12:11:41 knakahara Exp $");
 
 #include 
 #include 
@@ -1272,24 +1272,30 @@ cryptointr(void)
 			if (cap->cc_qblocked != 0)
 continue;
 
-			if (submit != NULL) {
-/*
- * We stop on finding another op,
- * regardless whether its for the same
- * driver or not.  We could keep
- * searching the queue but it might be
- * better to just use a per-driver
- * queue instead.
- */
-if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
-	hint = CRYPTO_HINT_MORE;
-break;
-			} else {
-submit = crp;
-if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
-	break;
-/* keep scanning for more are q'd */
+			/*
+			 * skip batch crp until the end of crp_q
+			 */
+			if ((crp->crp_flags & CRYPTO_F_BATCH) != 0) {
+if (submit == NULL) {
+	submit = crp;
+} else {
+	if (CRYPTO_SESID2HID(submit->crp_sid)
+	== hid)
+		hint = CRYPTO_HINT_MORE;
+}
+
+continue;
 			}
+
+			/*
+			 * found first crp which is neither blocked nor batch.
+			 */
+			submit = crp;
+			/*
+			 * batch crp can be processed much later, so clear hint.
+			 */
+			hint = 0;
+			break;
 		}
 		if (submit != NULL) {
 			TAILQ_REMOVE(_q, submit, crp_next);



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 11:04:38 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor cryptointr(), no functional changes.


To generate a diff of this commit:
cvs rdiff -u -r1.69 -r1.70 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.69 src/sys/opencrypto/crypto.c:1.70
--- src/sys/opencrypto/crypto.c:1.69	Wed May 17 11:03:42 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 11:04:38 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.69 2017/05/17 11:03:42 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.70 2017/05/17 11:04:38 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.69 2017/05/17 11:03:42 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.70 2017/05/17 11:04:38 knakahara Exp $");
 
 #include 
 #include 
@@ -1265,26 +1265,30 @@ cryptointr(void)
 submit = crp;
 break;
 			}
-			if (!cap->cc_qblocked) {
-if (submit != NULL) {
-	/*
-	 * We stop on finding another op,
-	 * regardless whether its for the same
-	 * driver or not.  We could keep
-	 * searching the queue but it might be
-	 * better to just use a per-driver
-	 * queue instead.
-	 */
-	if (CRYPTO_SESID2HID(submit->crp_sid)
-	== hid)
-		hint = CRYPTO_HINT_MORE;
+
+			/*
+			 * skip blocked crp regardless of CRYPTO_F_BATCH
+			 */
+			if (cap->cc_qblocked != 0)
+continue;
+
+			if (submit != NULL) {
+/*
+ * We stop on finding another op,
+ * regardless whether its for the same
+ * driver or not.  We could keep
+ * searching the queue but it might be
+ * better to just use a per-driver
+ * queue instead.
+ */
+if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
+	hint = CRYPTO_HINT_MORE;
+break;
+			} else {
+submit = crp;
+if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
 	break;
-} else {
-	submit = crp;
-	if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
-		break;
-	/* keep scanning for more are q'd */
-}
+/* keep scanning for more are q'd */
 			}
 		}
 		if (submit != NULL) {



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 11:03:42 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix cryptointr() can process unexpected request.

If migrate crp is linked after batch crp, "submit" is already set to
the batch crp. So, cryptointr() can process the batch crp instead of
the target migrate crp.


To generate a diff of this commit:
cvs rdiff -u -r1.68 -r1.69 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.68 src/sys/opencrypto/crypto.c:1.69
--- src/sys/opencrypto/crypto.c:1.68	Wed May 17 07:12:50 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 11:03:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.68 2017/05/17 07:12:50 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.69 2017/05/17 11:03:42 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.68 2017/05/17 07:12:50 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.69 2017/05/17 11:03:42 knakahara Exp $");
 
 #include 
 #include 
@@ -1262,8 +1262,7 @@ cryptointr(void)
 			cap = crypto_checkdriver(hid);
 			if (cap == NULL || cap->cc_process == NULL) {
 /* Op needs to be migrated, process it. */
-if (submit == NULL)
-	submit = crp;
+submit = crp;
 break;
 			}
 			if (!cap->cc_qblocked) {



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 07:12:50 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_kdispatch() in a similar way as crypto_dispatch().


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.67 src/sys/opencrypto/crypto.c:1.68
--- src/sys/opencrypto/crypto.c:1.67	Wed May 17 06:53:02 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 07:12:50 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.67 2017/05/17 06:53:02 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.68 2017/05/17 07:12:50 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.67 2017/05/17 06:53:02 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.68 2017/05/17 07:12:50 knakahara Exp $");
 
 #include 
 #include 
@@ -853,36 +853,49 @@ crypto_kdispatch(struct cryptkop *krp)
 	cryptostats.cs_kops++;
 
 	cap = crypto_checkdriver(krp->krp_hid);
-	if (cap && !cap->cc_kqblocked) {
+	/*
+	 * TODO:
+	 * If we can ensure the driver has been valid until the driver is
+	 * done crypto_unregister(), this migrate operation is not required.
+	 */
+	if (cap == NULL) {
+		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
 		mutex_spin_exit(_q_mtx);
-		result = crypto_kinvoke(krp, 0);
-		if (result == ERESTART) {
-			/*
-			 * The driver ran out of resources, mark the
-			 * driver ``blocked'' for cryptop's and put
-			 * the op on the queue.
-			 */
-			mutex_spin_enter(_q_mtx);
-			crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
-			TAILQ_INSERT_HEAD(_kq, krp, krp_next);
-			cryptostats.cs_kblocks++;
-			mutex_spin_exit(_q_mtx);
 
-			/*
-			 * The krp is enqueued to crp_kq, that is,
-			 * no error occurs. So, this function should
-			 * not return error.
-			 */
-			result = 0;
-		}
-	} else {
+		return 0;
+	}
+
+	if (cap->cc_kqblocked != 0) {
 		/*
 		 * The driver is blocked, just queue the op until
 		 * it unblocks and the swi thread gets kicked.
 		 */
 		TAILQ_INSERT_TAIL(_kq, krp, krp_next);
-		result = 0;
 		mutex_spin_exit(_q_mtx);
+
+		return 0;
+	}
+
+	mutex_spin_exit(_q_mtx);
+	result = crypto_kinvoke(krp, 0);
+	if (result == ERESTART) {
+		/*
+		 * The driver ran out of resources, mark the
+		 * driver ``blocked'' for cryptop's and put
+		 * the op on the queue.
+		 */
+		mutex_spin_enter(_q_mtx);
+		crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
+		TAILQ_INSERT_HEAD(_kq, krp, krp_next);
+		cryptostats.cs_kblocks++;
+		mutex_spin_exit(_q_mtx);
+
+		/*
+		 * The krp is enqueued to crp_kq, that is,
+		 * no error occurs. So, this function should
+		 * not return error.
+		 */
+		result = 0;
 	}
 
 	return result;



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 06:53:02 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_dispatch (3/3): do "blocked" operation previously


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.66 src/sys/opencrypto/crypto.c:1.67
--- src/sys/opencrypto/crypto.c:1.66	Wed May 17 06:52:08 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 06:53:02 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.66 2017/05/17 06:52:08 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.67 2017/05/17 06:53:02 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.66 2017/05/17 06:52:08 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.67 2017/05/17 06:53:02 knakahara Exp $");
 
 #include 
 #include 
@@ -792,44 +792,48 @@ crypto_dispatch(struct cryptop *crp)
 	}
 
 	/*
+	 * TODO:
+	 * cap->cc_qblocked should be protected by a spin lock other than
+	 * crypto_q_mtx.
+	 */
+	if (cap->cc_qblocked != 0) {
+		/*
+		 * The driver is blocked, just queue the op until
+		 * it unblocks and the swi thread gets kicked.
+		 */
+		TAILQ_INSERT_TAIL(_q, crp, crp_next);
+		mutex_spin_exit(_q_mtx);
+
+		return 0;
+	}
+
+	/*
 	 * Caller marked the request to be processed
 	 * immediately; dispatch it directly to the
 	 * driver unless the driver is currently blocked.
 	 */
-	if (!cap->cc_qblocked) {
+	mutex_spin_exit(_q_mtx);
+	result = crypto_invoke(crp, 0);
+	if (result == ERESTART) {
+		/*
+		 * The driver ran out of resources, mark the
+		 * driver ``blocked'' for cryptop's and put
+		 * the op on the queue.
+		 */
+		mutex_spin_enter(_q_mtx);
+		crypto_drivers[hid].cc_qblocked = 1;
+		TAILQ_INSERT_HEAD(_q, crp, crp_next);
+		cryptostats.cs_blocks++;
 		mutex_spin_exit(_q_mtx);
-		result = crypto_invoke(crp, 0);
-		if (result == ERESTART) {
-			/*
-			 * The driver ran out of resources, mark the
-			 * driver ``blocked'' for cryptop's and put
-			 * the op on the queue.
-			 */
-			mutex_spin_enter(_q_mtx);
-			crypto_drivers[hid].cc_qblocked = 1;
-			TAILQ_INSERT_HEAD(_q, crp, crp_next);
-			cryptostats.cs_blocks++;
-			mutex_spin_exit(_q_mtx);
 
-			/*
-			 * The crp is enqueued to crp_q, that is,
-			 * no error occurs. So, this function should
-			 * not return error.
-			 */
-			result = 0;
-		}
-		goto out_released;
-	} else {
 		/*
-		 * The driver is blocked, just queue the op until
-		 * it unblocks and the swi thread gets kicked.
+		 * The crp is enqueued to crp_q, that is,
+		 * no error occurs. So, this function should
+		 * not return error.
 		 */
-		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		result = 0;
 	}
 
-	mutex_spin_exit(_q_mtx);
-out_released:
 	return result;
 }
 



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 06:52:08 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_dispatch (2/3): divide migrate operation


To generate a diff of this commit:
cvs rdiff -u -r1.65 -r1.66 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.65 src/sys/opencrypto/crypto.c:1.66
--- src/sys/opencrypto/crypto.c:1.65	Wed May 17 06:50:12 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 06:52:08 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.65 2017/05/17 06:50:12 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.66 2017/05/17 06:52:08 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.65 2017/05/17 06:50:12 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.66 2017/05/17 06:52:08 knakahara Exp $");
 
 #include 
 #include 
@@ -774,13 +774,29 @@ crypto_dispatch(struct cryptop *crp)
 
 	mutex_spin_enter(_q_mtx);
 
+	cap = crypto_checkdriver(hid);
+	/*
+	 * TODO:
+	 * If we can ensure the driver has been valid until the driver is
+	 * done crypto_unregister(), this migrate operation is not required.
+	 */
+	if (cap == NULL) {
+		/*
+		 * The driver must be detached, so this request will migrate
+		 * to other drivers in cryptointr() later.
+		 */
+		TAILQ_INSERT_TAIL(_q, crp, crp_next);
+		mutex_spin_exit(_q_mtx);
+
+		return 0;
+	}
+
 	/*
 	 * Caller marked the request to be processed
 	 * immediately; dispatch it directly to the
 	 * driver unless the driver is currently blocked.
 	 */
-	cap = crypto_checkdriver(hid);
-	if (cap && !cap->cc_qblocked) {
+	if (!cap->cc_qblocked) {
 		mutex_spin_exit(_q_mtx);
 		result = crypto_invoke(crp, 0);
 		if (result == ERESTART) {



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 06:50:12 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_dispatch (1/3): divide batch operation

Processing batch operation at first, crypto_q_mtx's lock region is reduced
because crp does not require crypto_q_mtx.


To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.64 src/sys/opencrypto/crypto.c:1.65
--- src/sys/opencrypto/crypto.c:1.64	Wed May 17 06:33:04 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 06:50:12 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.64 2017/05/17 06:33:04 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.65 2017/05/17 06:50:12 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.64 2017/05/17 06:33:04 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.65 2017/05/17 06:50:12 knakahara Exp $");
 
 #include 
 #include 
@@ -740,12 +740,12 @@ crypto_dispatch(struct cryptop *crp)
 {
 	u_int32_t hid;
 	int result;
+	struct cryptocap *cap;
 
 	KASSERT(crp != NULL);
 
 	hid = CRYPTO_SESID2HID(crp->crp_sid);
 
-	mutex_spin_enter(_q_mtx);
 	DPRINTF("crp %p, alg %d\n", crp, crp->crp_desc->crd_alg);
 
 	cryptostats.cs_ops++;
@@ -754,46 +754,8 @@ crypto_dispatch(struct cryptop *crp)
 	if (crypto_timing)
 		nanouptime(>crp_tstamp);
 #endif
-	if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
-		struct cryptocap *cap;
-		/*
-		 * Caller marked the request to be processed
-		 * immediately; dispatch it directly to the
-		 * driver unless the driver is currently blocked.
-		 */
-		cap = crypto_checkdriver(hid);
-		if (cap && !cap->cc_qblocked) {
-			mutex_spin_exit(_q_mtx);
-			result = crypto_invoke(crp, 0);
-			if (result == ERESTART) {
-/*
- * The driver ran out of resources, mark the
- * driver ``blocked'' for cryptop's and put
- * the op on the queue.
- */
-mutex_spin_enter(_q_mtx);
-crypto_drivers[hid].cc_qblocked = 1;
-TAILQ_INSERT_HEAD(_q, crp, crp_next);
-cryptostats.cs_blocks++;
-mutex_spin_exit(_q_mtx);
 
-/*
- * The crp is enqueued to crp_q, that is,
- * no error occurs. So, this function should
- * not return error.
- */
-result = 0;
-			}
-			goto out_released;
-		} else {
-			/*
-			 * The driver is blocked, just queue the op until
-			 * it unblocks and the swi thread gets kicked.
-			 */
-			TAILQ_INSERT_TAIL(_q, crp, crp_next);
-			result = 0;
-		}
-	} else {
+	if ((crp->crp_flags & CRYPTO_F_BATCH) != 0) {
 		int wasempty = TAILQ_EMPTY(_q);
 		/*
 		 * Caller marked the request as ``ok to delay'';
@@ -801,14 +763,52 @@ crypto_dispatch(struct cryptop *crp)
 		 * when the operation is low priority and/or suitable
 		 * for batching.
 		 */
+		mutex_spin_enter(_q_mtx);
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		mutex_spin_exit(_q_mtx);
-		if (wasempty) {
+		if (wasempty)
 			setsoftcrypto(softintr_cookie);
+
+		return 0;
+	}
+
+	mutex_spin_enter(_q_mtx);
+
+	/*
+	 * Caller marked the request to be processed
+	 * immediately; dispatch it directly to the
+	 * driver unless the driver is currently blocked.
+	 */
+	cap = crypto_checkdriver(hid);
+	if (cap && !cap->cc_qblocked) {
+		mutex_spin_exit(_q_mtx);
+		result = crypto_invoke(crp, 0);
+		if (result == ERESTART) {
+			/*
+			 * The driver ran out of resources, mark the
+			 * driver ``blocked'' for cryptop's and put
+			 * the op on the queue.
+			 */
+			mutex_spin_enter(_q_mtx);
+			crypto_drivers[hid].cc_qblocked = 1;
+			TAILQ_INSERT_HEAD(_q, crp, crp_next);
+			cryptostats.cs_blocks++;
+			mutex_spin_exit(_q_mtx);
+
+			/*
+			 * The crp is enqueued to crp_q, that is,
+			 * no error occurs. So, this function should
+			 * not return error.
+			 */
 			result = 0;
-			goto out_released;
 		}
-
+		goto out_released;
+	} else {
+		/*
+		 * The driver is blocked, just queue the op until
+		 * it unblocks and the swi thread gets kicked.
+		 */
+		TAILQ_INSERT_TAIL(_q, crp, crp_next);
 		result = 0;
 	}
 



CVS commit: src/sys/opencrypto

2017-05-17 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 17 06:33:04 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.c cryptodev.h cryptosoft.c
deflate.c ocryptodev.c

Log Message:
opencrypto: cleanup debug messages.


To generate a diff of this commit:
cvs rdiff -u -r1.63 -r1.64 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.89 -r1.90 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.31 -r1.32 src/sys/opencrypto/cryptodev.h
cvs rdiff -u -r1.49 -r1.50 src/sys/opencrypto/cryptosoft.c
cvs rdiff -u -r1.22 -r1.23 src/sys/opencrypto/deflate.c
cvs rdiff -u -r1.6 -r1.7 src/sys/opencrypto/ocryptodev.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.63 src/sys/opencrypto/crypto.c:1.64
--- src/sys/opencrypto/crypto.c:1.63	Wed May 10 09:45:51 2017
+++ src/sys/opencrypto/crypto.c	Wed May 17 06:33:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.63 2017/05/10 09:45:51 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.64 2017/05/17 06:33:04 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.63 2017/05/10 09:45:51 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.64 2017/05/17 06:33:04 knakahara Exp $");
 
 #include 
 #include 
@@ -363,7 +363,7 @@ crypto_newsession(u_int64_t *sid, struct
 		/* See if all the algorithms are supported. */
 		for (cr = cri; cr; cr = cr->cri_next)
 			if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0) {
-DPRINTF(("crypto_newsession: alg %d not supported\n", cr->cri_alg));
+DPRINTF("alg %d not supported\n", cr->cri_alg);
 break;
 			}
 
@@ -387,8 +387,8 @@ crypto_newsession(u_int64_t *sid, struct
 (*sid) |= (lid & 0x);
 crypto_drivers[hid].cc_sessions++;
 			} else {
-DPRINTF(("%s: crypto_drivers[%d].cc_newsession() failed. error=%d\n",
-	__func__, hid, err));
+DPRINTF("crypto_drivers[%d].cc_newsession() failed. error=%d\n",
+	hid, err);
 			}
 			goto done;
 			/*break;*/
@@ -746,8 +746,7 @@ crypto_dispatch(struct cryptop *crp)
 	hid = CRYPTO_SESID2HID(crp->crp_sid);
 
 	mutex_spin_enter(_q_mtx);
-	DPRINTF(("crypto_dispatch: crp %p, alg %d\n",
-		crp, crp->crp_desc->crd_alg));
+	DPRINTF("crp %p, alg %d\n", crp, crp->crp_desc->crd_alg);
 
 	cryptostats.cs_ops++;
 
@@ -980,7 +979,7 @@ crypto_invoke(struct cryptop *crp, int h
 		/*
 		 * Invoke the driver to process the request.
 		 */
-		DPRINTF(("calling process for %p\n", crp));
+		DPRINTF("calling process for %p\n", crp);
 		return (*process)(arg, crp, hint);
 	} else {
 		struct cryptodesc *crd;
@@ -1015,8 +1014,7 @@ crypto_freereq(struct cryptop *crp)
 
 	if (crp == NULL)
 		return;
-	DPRINTF(("crypto_freereq[%u]: crp %p\n",
-		CRYPTO_SESID2LID(crp->crp_sid), crp));
+	DPRINTF("lid[%u]: crp %p\n", CRYPTO_SESID2LID(crp->crp_sid), crp);
 
 	/* sanity check */
 	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
@@ -1076,8 +1074,7 @@ crypto_done(struct cryptop *crp)
 	if (crypto_timing)
 		crypto_tstat(_done, >crp_tstamp);
 #endif
-	DPRINTF(("crypto_done[%u]: crp %p\n",
-		CRYPTO_SESID2LID(crp->crp_sid), crp));
+	DPRINTF("lid[%u]: crp %p\n", CRYPTO_SESID2LID(crp->crp_sid), crp);
 
 	/*
 	 * Normal case; queue the callback for the thread.
@@ -1123,20 +1120,20 @@ crypto_done(struct cryptop *crp)
 			 * the same context, we can skip enqueueing crp_ret_q
 			 * and cv_signal(_cv).
 			 */
-			DPRINTF(("crypto_done[%u]: crp %p CRYPTO_F_USER\n",
-CRYPTO_SESID2LID(crp->crp_sid), crp));
+			DPRINTF("lid[%u]: crp %p CRYPTO_F_USER\n",
+CRYPTO_SESID2LID(crp->crp_sid), crp);
 		} else
 #endif
 		{
 			wasempty = TAILQ_EMPTY(_ret_q);
-			DPRINTF(("crypto_done[%u]: queueing %p\n",
-CRYPTO_SESID2LID(crp->crp_sid), crp));
+			DPRINTF("lid[%u]: queueing %p\n",
+CRYPTO_SESID2LID(crp->crp_sid), crp);
 			crp->crp_flags |= CRYPTO_F_ONRETQ;
 			TAILQ_INSERT_TAIL(_ret_q, crp, crp_next);
 			if (wasempty) {
-DPRINTF(("crypto_done[%u]: waking cryptoret, "
+DPRINTF("lid[%u]: waking cryptoret, "
 	"crp %p hit empty queue\n.",
-	CRYPTO_SESID2LID(crp->crp_sid), crp));
+	CRYPTO_SESID2LID(crp->crp_sid), crp);
 cv_signal(_cv);
 			}
 		}

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.89 src/sys/opencrypto/cryptodev.c:1.90
--- src/sys/opencrypto/cryptodev.c:1.89	Mon Apr 24 03:29:37 2017
+++ src/sys/opencrypto/cryptodev.c	Wed May 17 06:33:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.89 2017/04/24 03:29:37 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.90 2017/05/17 06:33:04 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@

CVS commit: src/sys/opencrypto

2017-05-10 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 10 09:45:51 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix: crypto_drivers[hid].cc_process() could be called even if it was null.

If a crypto driver is unregistered before calling cyrptointr(),
the crypto_drivers[hid].cc_process is null in spite of the hid
is less than crypto_drivers_num.

reffered to FreeBSD code.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.62 src/sys/opencrypto/crypto.c:1.63
--- src/sys/opencrypto/crypto.c:1.62	Wed May 10 03:26:33 2017
+++ src/sys/opencrypto/crypto.c	Wed May 10 09:45:51 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.62 2017/05/10 03:26:33 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.63 2017/05/10 09:45:51 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.62 2017/05/10 03:26:33 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.63 2017/05/10 09:45:51 knakahara Exp $");
 
 #include 
 #include 
@@ -970,12 +970,10 @@ crypto_invoke(struct cryptop *crp, int h
 
 	hid = CRYPTO_SESID2HID(crp->crp_sid);
 
-	if (hid < crypto_drivers_num) {
+	if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
 		int (*process)(void *, struct cryptop *, int);
 		void *arg;
 
-		if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
-			crypto_freesession(crp->crp_sid);
 		process = crypto_drivers[hid].cc_process;
 		arg = crypto_drivers[hid].cc_arg;
 
@@ -992,6 +990,8 @@ crypto_invoke(struct cryptop *crp, int h
 		 * Driver has unregistered; migrate the session and return
 		 * an error to the caller so they'll resubmit the op.
 		 */
+		crypto_freesession(crp->crp_sid);
+
 		for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
 			crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
 



CVS commit: src/sys/opencrypto

2017-05-09 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 10 03:26:33 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
unify implementation of crypto_unregister() and crypto_unregister_all()


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.61 src/sys/opencrypto/crypto.c:1.62
--- src/sys/opencrypto/crypto.c:1.61	Wed May 10 03:23:26 2017
+++ src/sys/opencrypto/crypto.c	Wed May 10 03:26:33 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.61 2017/05/10 03:23:26 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.62 2017/05/10 03:26:33 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.61 2017/05/10 03:23:26 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.62 2017/05/10 03:26:33 knakahara Exp $");
 
 #include 
 #include 
@@ -613,7 +613,7 @@ crypto_register(u_int32_t driverid, int 
 }
 
 static int
-crypto_unregister_locked(u_int32_t driverid, int alg)
+crypto_unregister_locked(u_int32_t driverid, int alg, bool all)
 {
 	int i;
 	u_int32_t ses;
@@ -626,19 +626,23 @@ crypto_unregister_locked(u_int32_t drive
 		return EINVAL;
 
 	cap = crypto_checkdriver(driverid);
-	if (cap == NULL || cap->cc_alg[alg] == 0)
+	if (cap == NULL || (!all && cap->cc_alg[alg] == 0))
 		return EINVAL;
 
 	cap->cc_alg[alg] = 0;
 	cap->cc_max_op_len[alg] = 0;
 
-	/* Was this the last algorithm ? */
-	for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
-		if (cap->cc_alg[i] != 0) {
+	if (all) {
+		if (alg != CRYPTO_ALGORITHM_MAX)
 			lastalg = false;
-			break;
-		}
-
+	} else {
+		/* Was this the last algorithm ? */
+		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
+			if (cap->cc_alg[i] != 0) {
+lastalg = false;
+break;
+			}
+	}
 	if (lastalg) {
 		ses = cap->cc_sessions;
 		memset(cap, 0, sizeof(struct cryptocap));
@@ -666,7 +670,7 @@ crypto_unregister(u_int32_t driverid, in
 	int err;
 
 	mutex_enter(_drv_mtx);
-	err = crypto_unregister_locked(driverid, alg);
+	err = crypto_unregister_locked(driverid, alg, false);
 	mutex_exit(_drv_mtx);
 
 	return err;
@@ -678,39 +682,20 @@ crypto_unregister(u_int32_t driverid, in
  * around so that subsequent calls using those sessions will
  * correctly detect the driver has been unregistered and reroute
  * requests.
- *
- * XXX careful.  Don't change this to call crypto_unregister() for each
- * XXX registered algorithm unless you drop the mutex across the calls;
- * XXX you can't take it recursively.
  */
 int
 crypto_unregister_all(u_int32_t driverid)
 {
-	int i, err;
-	u_int32_t ses;
-	struct cryptocap *cap;
+	int err, i;
 
 	mutex_enter(_drv_mtx);
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL) {
-		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
-			cap->cc_alg[i] = 0;
-			cap->cc_max_op_len[i] = 0;
-		}
-		ses = cap->cc_sessions;
-		memset(cap, 0, sizeof(struct cryptocap));
-		if (ses != 0) {
-			/*
-			 * If there are pending sessions, just mark as invalid.
-			 */
-			cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
-			cap->cc_sessions = ses;
-		}
-		err = 0;
-	} else
-		err = EINVAL;
-
+	for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
+		err = crypto_unregister_locked(driverid, i, true);
+		if (err)
+			break;
+	}
 	mutex_exit(_drv_mtx);
+
 	return err;
 }
 



CVS commit: src/sys/opencrypto

2017-05-09 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 10 03:23:26 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_unregister()

- separate logic to crypto_unregister_locked()
- refactor cryptocap cleanup condition


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.60 src/sys/opencrypto/crypto.c:1.61
--- src/sys/opencrypto/crypto.c:1.60	Wed May 10 03:15:32 2017
+++ src/sys/opencrypto/crypto.c	Wed May 10 03:23:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.60 2017/05/10 03:15:32 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.61 2017/05/10 03:23:26 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.60 2017/05/10 03:15:32 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.61 2017/05/10 03:23:26 knakahara Exp $");
 
 #include 
 #include 
@@ -612,6 +612,48 @@ crypto_register(u_int32_t driverid, int 
 	return err;
 }
 
+static int
+crypto_unregister_locked(u_int32_t driverid, int alg)
+{
+	int i;
+	u_int32_t ses;
+	struct cryptocap *cap;
+	bool lastalg = true;
+
+	KASSERT(mutex_owned(_drv_mtx));
+
+	if (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)
+		return EINVAL;
+
+	cap = crypto_checkdriver(driverid);
+	if (cap == NULL || cap->cc_alg[alg] == 0)
+		return EINVAL;
+
+	cap->cc_alg[alg] = 0;
+	cap->cc_max_op_len[alg] = 0;
+
+	/* Was this the last algorithm ? */
+	for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
+		if (cap->cc_alg[i] != 0) {
+			lastalg = false;
+			break;
+		}
+
+	if (lastalg) {
+		ses = cap->cc_sessions;
+		memset(cap, 0, sizeof(struct cryptocap));
+		if (ses != 0) {
+			/*
+			 * If there are pending sessions, just mark as invalid.
+			 */
+			cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
+			cap->cc_sessions = ses;
+		}
+	}
+
+	return 0;
+}
+
 /*
  * Unregister a crypto driver. If there are pending sessions using it,
  * leave enough information around so that subsequent calls using those
@@ -621,40 +663,12 @@ crypto_register(u_int32_t driverid, int 
 int
 crypto_unregister(u_int32_t driverid, int alg)
 {
-	int i, err;
-	u_int32_t ses;
-	struct cryptocap *cap;
+	int err;
 
 	mutex_enter(_drv_mtx);
-
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL &&
-	(CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
-	cap->cc_alg[alg] != 0) {
-		cap->cc_alg[alg] = 0;
-		cap->cc_max_op_len[alg] = 0;
-
-		/* Was this the last algorithm ? */
-		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
-			if (cap->cc_alg[i] != 0)
-break;
-
-		if (i == CRYPTO_ALGORITHM_MAX + 1) {
-			ses = cap->cc_sessions;
-			memset(cap, 0, sizeof(struct cryptocap));
-			if (ses != 0) {
-/*
- * If there are pending sessions, just mark as invalid.
- */
-cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
-cap->cc_sessions = ses;
-			}
-		}
-		err = 0;
-	} else
-		err = EINVAL;
-
+	err = crypto_unregister_locked(driverid, alg);
 	mutex_exit(_drv_mtx);
+
 	return err;
 }
 



CVS commit: src/sys/opencrypto

2017-05-09 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed May 10 03:15:32 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
use macro instead of immediate value


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.59 src/sys/opencrypto/crypto.c:1.60
--- src/sys/opencrypto/crypto.c:1.59	Tue May  2 03:17:43 2017
+++ src/sys/opencrypto/crypto.c	Wed May 10 03:15:32 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.59 2017/05/02 03:17:43 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.60 2017/05/10 03:15:32 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.59 2017/05/02 03:17:43 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.60 2017/05/10 03:15:32 knakahara Exp $");
 
 #include 
 #include 
@@ -635,7 +635,7 @@ crypto_unregister(u_int32_t driverid, in
 		cap->cc_max_op_len[alg] = 0;
 
 		/* Was this the last algorithm ? */
-		for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
+		for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++)
 			if (cap->cc_alg[i] != 0)
 break;
 



CVS commit: src/sys/opencrypto

2017-05-01 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Tue May  2 03:17:43 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
add some assertion. tested by ATF net/ipsec/ and crypto/.


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.59 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.58 src/sys/opencrypto/crypto.c:1.59
--- src/sys/opencrypto/crypto.c:1.58	Wed Apr 26 03:29:36 2017
+++ src/sys/opencrypto/crypto.c	Tue May  2 03:17:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.58 2017/04/26 03:29:36 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.59 2017/05/02 03:17:43 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.58 2017/04/26 03:29:36 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.59 2017/05/02 03:17:43 knakahara Exp $");
 
 #include 
 #include 
@@ -739,9 +739,13 @@ crypto_unblock(u_int32_t driverid, int w
 int
 crypto_dispatch(struct cryptop *crp)
 {
-	u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
+	u_int32_t hid;
 	int result;
 
+	KASSERT(crp != NULL);
+
+	hid = CRYPTO_SESID2HID(crp->crp_sid);
+
 	mutex_spin_enter(_q_mtx);
 	DPRINTF(("crypto_dispatch: crp %p, alg %d\n",
 		crp, crp->crp_desc->crd_alg));
@@ -825,6 +829,8 @@ crypto_kdispatch(struct cryptkop *krp)
 	struct cryptocap *cap;
 	int result;
 
+	KASSERT(krp != NULL);
+
 	mutex_spin_enter(_q_mtx);
 	cryptostats.cs_kops++;
 
@@ -873,9 +879,9 @@ crypto_kinvoke(struct cryptkop *krp, int
 	u_int32_t hid;
 	int error;
 
+	KASSERT(krp != NULL);
+
 	/* Sanity checks. */
-	if (krp == NULL)
-		return EINVAL;
 	if (krp->krp_callback == NULL) {
 		cv_destroy(>krp_cv);
 		pool_put(_pool, krp);
@@ -947,13 +953,13 @@ crypto_invoke(struct cryptop *crp, int h
 {
 	u_int32_t hid;
 
+	KASSERT(crp != NULL);
+
 #ifdef CRYPTO_TIMING
 	if (crypto_timing)
 		crypto_tstat(_invoke, >crp_tstamp);
 #endif
 	/* Sanity checks. */
-	if (crp == NULL)
-		return EINVAL;
 	if (crp->crp_callback == NULL) {
 		return EINVAL;
 	}
@@ -1063,6 +1069,8 @@ crypto_done(struct cryptop *crp)
 {
 	int wasempty;
 
+	KASSERT(crp != NULL);
+
 	if (crp->crp_etype != 0)
 		cryptostats.cs_errs++;
 #ifdef CRYPTO_TIMING
@@ -1145,6 +1153,8 @@ crypto_kdone(struct cryptkop *krp)
 {
 	int wasempty;
 
+	KASSERT(krp != NULL);
+
 	if (krp->krp_status != 0)
 		cryptostats.cs_kerrs++;
 		



CVS commit: src/sys/opencrypto

2017-04-25 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Apr 26 03:29:37 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
When crypto request is deferred processing, opencrypto should not return error.

When the crypto device blocks a crypto request, opnecrypto enqueues the request,
that is, the request is just deferred and no error occurs.
The pseudo error causes problems, e.g. ipsec can send wrong ICMP host unreach.

contributed by hsuenaga@IIJ, thanks.


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.57 src/sys/opencrypto/crypto.c:1.58
--- src/sys/opencrypto/crypto.c:1.57	Mon Apr 24 03:29:37 2017
+++ src/sys/opencrypto/crypto.c	Wed Apr 26 03:29:36 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.57 2017/04/24 03:29:37 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.58 2017/04/26 03:29:36 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.57 2017/04/24 03:29:37 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.58 2017/04/26 03:29:36 knakahara Exp $");
 
 #include 
 #include 
@@ -774,6 +774,13 @@ crypto_dispatch(struct cryptop *crp)
 TAILQ_INSERT_HEAD(_q, crp, crp_next);
 cryptostats.cs_blocks++;
 mutex_spin_exit(_q_mtx);
+
+/*
+ * The crp is enqueued to crp_q, that is,
+ * no error occurs. So, this function should
+ * not return error.
+ */
+result = 0;
 			}
 			goto out_released;
 		} else {
@@ -836,6 +843,13 @@ crypto_kdispatch(struct cryptkop *krp)
 			TAILQ_INSERT_HEAD(_kq, krp, krp_next);
 			cryptostats.cs_kblocks++;
 			mutex_spin_exit(_q_mtx);
+
+			/*
+			 * The krp is enqueued to crp_kq, that is,
+			 * no error occurs. So, this function should
+			 * not return error.
+			 */
+			result = 0;
 		}
 	} else {
 		/*



CVS commit: src/sys/opencrypto

2017-04-23 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr 24 03:29:37 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.c cryptodev.h
cryptodev_internal.h

Log Message:
separate crypto_drv_mtx from crypto_mtx.

crypto_mtx is used only for cryptodev.c and ocryptodev.c now.


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.88 -r1.89 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.30 -r1.31 src/sys/opencrypto/cryptodev.h
cvs rdiff -u -r1.1 -r1.2 src/sys/opencrypto/cryptodev_internal.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.56 src/sys/opencrypto/crypto.c:1.57
--- src/sys/opencrypto/crypto.c:1.56	Mon Apr 24 02:04:55 2017
+++ src/sys/opencrypto/crypto.c	Mon Apr 24 03:29:37 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.56 2017/04/24 02:04:55 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.57 2017/04/24 03:29:37 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.56 2017/04/24 02:04:55 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.57 2017/04/24 03:29:37 knakahara Exp $");
 
 #include 
 #include 
@@ -78,7 +78,6 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 static kmutex_t crypto_q_mtx;
 static kmutex_t crypto_ret_q_mtx;
 static kcondvar_t cryptoret_cv;
-kmutex_t crypto_mtx;
 
 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
   #define SWI_CRYPTO 17
@@ -99,6 +98,7 @@ int crypto_ret_q_check(struct cryptop *)
  * crypto_drivers table with crypto_get_driverid() and then registering
  * each algorithm they support with crypto_register() and crypto_kregister().
  */
+static kmutex_t crypto_drv_mtx;
 static	struct cryptocap *crypto_drivers;
 static	int crypto_drivers_num;
 static	void *softintr_cookie;
@@ -221,7 +221,7 @@ crypto_init0(void)
 {
 	int error;
 
-	mutex_init(_mtx, MUTEX_DEFAULT, IPL_NONE);
+	mutex_init(_drv_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(_q_mtx, MUTEX_DEFAULT, IPL_NET);
 	mutex_init(_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
 	cv_init(_cv, "crypto_w");
@@ -301,8 +301,10 @@ crypto_destroy(bool exit_kthread)
 
 	unregister_swi(SWI_CRYPTO, cryptointr);
 
+	mutex_enter(_drv_mtx);
 	if (crypto_drivers != NULL)
 		free(crypto_drivers, M_CRYPTO_DATA);
+	mutex_exit(_drv_mtx);
 
 	pool_destroy(_pool);
 	pool_destroy(_pool);
@@ -312,13 +314,13 @@ crypto_destroy(bool exit_kthread)
 
 	mutex_destroy(_ret_q_mtx);
 	mutex_destroy(_q_mtx);
-	mutex_destroy(_mtx);
+	mutex_destroy(_drv_mtx);
 
 	return 0;
 }
 
 /*
- * Create a new session.  Must be called with crypto_mtx held.
+ * Create a new session.
  */
 int
 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
@@ -327,7 +329,7 @@ crypto_newsession(u_int64_t *sid, struct
 	u_int32_t hid, lid;
 	int err = EINVAL;
 
-	mutex_enter(_mtx);
+	mutex_enter(_drv_mtx);
 
 	if (crypto_drivers == NULL)
 		goto done;
@@ -393,13 +395,13 @@ crypto_newsession(u_int64_t *sid, struct
 		}
 	}
 done:
-	mutex_exit(_mtx);
+	mutex_exit(_drv_mtx);
 	return err;
 }
 
 /*
  * Delete an existing session (or a reserved session on an unregistered
- * driver).  Must be called with crypto_mtx mutex held.
+ * driver).
  */
 int
 crypto_freesession(u_int64_t sid)
@@ -407,7 +409,7 @@ crypto_freesession(u_int64_t sid)
 	u_int32_t hid;
 	int err = 0;
 
-	mutex_enter(_mtx);
+	mutex_enter(_drv_mtx);
 
 	if (crypto_drivers == NULL) {
 		err = EINVAL;
@@ -442,7 +444,7 @@ crypto_freesession(u_int64_t sid)
 		memset(_drivers[hid], 0, sizeof(struct cryptocap));
 
 done:
-	mutex_exit(_mtx);
+	mutex_exit(_drv_mtx);
 	return err;
 }
 
@@ -458,7 +460,7 @@ crypto_get_driverid(u_int32_t flags)
 
 	(void)crypto_init();		/* XXX oh, this is foul! */
 
-	mutex_enter(_mtx);
+	mutex_enter(_drv_mtx);
 	for (i = 0; i < crypto_drivers_num; i++)
 		if (crypto_drivers[i].cc_process == NULL &&
 		(crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
@@ -469,7 +471,7 @@ crypto_get_driverid(u_int32_t flags)
 	if (i == crypto_drivers_num) {
 		/* Be careful about wrap-around. */
 		if (2 * crypto_drivers_num <= crypto_drivers_num) {
-			mutex_exit(_mtx);
+			mutex_exit(_drv_mtx);
 			printf("crypto: driver count wraparound!\n");
 			return -1;
 		}
@@ -477,7 +479,7 @@ crypto_get_driverid(u_int32_t flags)
 		newdrv = malloc(2 * crypto_drivers_num *
 		sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
 		if (newdrv == NULL) {
-			mutex_exit(_mtx);
+			mutex_exit(_drv_mtx);
 			printf("crypto: no space to expand driver table!\n");
 			return -1;
 		}
@@ -498,7 +500,7 @@ crypto_get_driverid(u_int32_t flags)
 	if (bootverbose)
 		printf("crypto: assign driver %u, flags %u\n", i, flags);
 
-	

CVS commit: src/sys/opencrypto

2017-04-23 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr 24 02:04:55 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
reduce crypto_q_mtx lock regions.


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.55 src/sys/opencrypto/crypto.c:1.56
--- src/sys/opencrypto/crypto.c:1.55	Mon Apr 24 01:42:00 2017
+++ src/sys/opencrypto/crypto.c	Mon Apr 24 02:04:55 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.55 2017/04/24 01:42:00 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.56 2017/04/24 02:04:55 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.55 2017/04/24 01:42:00 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.56 2017/04/24 02:04:55 knakahara Exp $");
 
 #include 
 #include 
@@ -85,7 +85,12 @@ kmutex_t crypto_mtx;
   #define register_swi(lvl, fn)  \
   softint_establish(SOFTINT_NET|SOFTINT_MPSAFE, (void (*)(void *))fn, NULL)
   #define unregister_swi(lvl, fn)  softint_disestablish(softintr_cookie)
-  #define setsoftcrypto(x) softint_schedule(x)
+  #define setsoftcrypto(x)			\
+	do{	\
+		kpreempt_disable();		\
+		softint_schedule(x);		\
+		kpreempt_enable();		\
+	}while(0)
 
 int crypto_ret_q_check(struct cryptop *);
 
@@ -718,9 +723,9 @@ crypto_unblock(u_int32_t driverid, int w
 		needwakeup |= cap->cc_kqblocked;
 		cap->cc_kqblocked = 0;
 	}
+	mutex_spin_exit(_q_mtx);
 	if (needwakeup)
 		setsoftcrypto(softintr_cookie);
-	mutex_spin_exit(_q_mtx);
 
 	return 0;
 }
@@ -786,9 +791,9 @@ crypto_dispatch(struct cryptop *crp)
 		 * for batching.
 		 */
 		TAILQ_INSERT_TAIL(_q, crp, crp_next);
+		mutex_spin_exit(_q_mtx);
 		if (wasempty) {
 			setsoftcrypto(softintr_cookie);
-			mutex_spin_exit(_q_mtx);
 			result = 0;
 			goto out_released;
 		}



CVS commit: src/sys/opencrypto

2017-04-23 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Mon Apr 24 01:42:00 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
refactor crypto_unblock(). No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.54 -r1.55 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.54 src/sys/opencrypto/crypto.c:1.55
--- src/sys/opencrypto/crypto.c:1.54	Fri Apr  7 12:17:57 2017
+++ src/sys/opencrypto/crypto.c	Mon Apr 24 01:42:00 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.54 2017/04/07 12:17:57 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.55 2017/04/24 01:42:00 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.54 2017/04/07 12:17:57 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.55 2017/04/24 01:42:00 knakahara Exp $");
 
 #include 
 #include 
@@ -701,30 +701,28 @@ int
 crypto_unblock(u_int32_t driverid, int what)
 {
 	struct cryptocap *cap;
-	int needwakeup, err;
+	int needwakeup = 0;
 
 	mutex_spin_enter(_q_mtx);
 	cap = crypto_checkdriver(driverid);
-	if (cap != NULL) {
-		needwakeup = 0;
-		if (what & CRYPTO_SYMQ) {
-			needwakeup |= cap->cc_qblocked;
-			cap->cc_qblocked = 0;
-		}
-		if (what & CRYPTO_ASYMQ) {
-			needwakeup |= cap->cc_kqblocked;
-			cap->cc_kqblocked = 0;
-		}
-		err = 0;
-		if (needwakeup)
-			setsoftcrypto(softintr_cookie);
-		mutex_spin_exit(_q_mtx);
-	} else {
-		err = EINVAL;
+	if (cap == NULL) {
 		mutex_spin_exit(_q_mtx);
+		return EINVAL;
 	}
 
-	return err;
+	if (what & CRYPTO_SYMQ) {
+		needwakeup |= cap->cc_qblocked;
+		cap->cc_qblocked = 0;
+	}
+	if (what & CRYPTO_ASYMQ) {
+		needwakeup |= cap->cc_kqblocked;
+		cap->cc_kqblocked = 0;
+	}
+	if (needwakeup)
+		setsoftcrypto(softintr_cookie);
+	mutex_spin_exit(_q_mtx);
+
+	return 0;
 }
 
 /*



CVS commit: src/sys/opencrypto

2017-04-18 Thread Maya Rashish
Module Name:src
Committed By:   maya
Date:   Tue Apr 18 17:05:05 UTC 2017

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
Remove duplicate assignment.
We assign the same value unconditionally just before.

from clang static analyzer

XXX surrounding code seems fishy


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.48 src/sys/opencrypto/cryptosoft.c:1.49
--- src/sys/opencrypto/cryptosoft.c:1.48	Thu Apr 13 01:24:34 2017
+++ src/sys/opencrypto/cryptosoft.c	Tue Apr 18 17:05:05 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.48 2017/04/13 01:24:34 ozaki-r Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.49 2017/04/18 17:05:05 maya Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.48 2017/04/13 01:24:34 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.49 2017/04/18 17:05:05 maya Exp $");
 
 #include 
 #include 
@@ -759,7 +759,6 @@ swcr_compdec(struct cryptodesc *crd, con
 	if (result < crd->crd_len) {
 		adj = result - crd->crd_len;
 		if (outtype == CRYPTO_BUF_MBUF) {
-			adj = result - crd->crd_len;
 			m_adj((struct mbuf *)buf, adj);
 		}
 		/* Don't adjust the iov_len, it breaks the kmem_free */



CVS commit: src/sys/opencrypto

2017-04-12 Thread Ryota Ozaki
Module Name:src
Committed By:   ozaki-r
Date:   Thu Apr 13 01:24:34 UTC 2017

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
Fix usage of MD5Final/SHA1Final

Passing NULL as the digest parameter is wrong.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.47 src/sys/opencrypto/cryptosoft.c:1.48
--- src/sys/opencrypto/cryptosoft.c:1.47	Thu Aug 20 14:40:19 2015
+++ src/sys/opencrypto/cryptosoft.c	Thu Apr 13 01:24:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.47 2015/08/20 14:40:19 christos Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.48 2017/04/13 01:24:34 ozaki-r Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.47 2015/08/20 14:40:19 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.48 2017/04/13 01:24:34 ozaki-r Exp $");
 
 #include 
 #include 
@@ -948,7 +948,9 @@ swcr_newsession(void *arg, u_int32_t *si
 			axf = _auth_hash_key_md5;
 			goto auth2common;
 
-		case CRYPTO_SHA1_KPDK:
+		case CRYPTO_SHA1_KPDK: {
+			unsigned char digest[SHA1_DIGEST_LENGTH];
+			CTASSERT(SHA1_DIGEST_LENGTH >= MD5_DIGEST_LENGTH);
 			axf = _auth_hash_key_sha1;
 		auth2common:
 			(*swd)->sw_ictx = malloc(axf->ctxsize,
@@ -971,9 +973,10 @@ swcr_newsession(void *arg, u_int32_t *si
 			axf->Init((*swd)->sw_ictx);
 			axf->Update((*swd)->sw_ictx, cri->cri_key,
 			cri->cri_klen / 8);
-			axf->Final(NULL, (*swd)->sw_ictx);
+			axf->Final(digest, (*swd)->sw_ictx);
 			(*swd)->sw_axf = axf;
 			break;
+		}
 
 		case CRYPTO_MD5:
 			axf = _auth_hash_md5;



CVS commit: src/sys/opencrypto

2017-04-07 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Apr  7 12:17:57 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.c

Log Message:
the processing said "ghastly hacks" is unnecessary now.


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.87 -r1.88 src/sys/opencrypto/cryptodev.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.53 src/sys/opencrypto/crypto.c:1.54
--- src/sys/opencrypto/crypto.c:1.53	Thu Apr  6 09:39:12 2017
+++ src/sys/opencrypto/crypto.c	Fri Apr  7 12:17:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.53 2017/04/06 09:39:12 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.54 2017/04/07 12:17:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.53 2017/04/06 09:39:12 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.54 2017/04/07 12:17:57 knakahara Exp $");
 
 #include 
 #include 
@@ -121,45 +121,6 @@ static	TAILQ_HEAD(krprethead, cryptkop) 
 		TAILQ_HEAD_INITIALIZER(crp_ret_kq);
 
 /*
- * XXX these functions are ghastly hacks for when the submission
- * XXX routines discover a request that was not CBIMM is already
- * XXX done, and must be yanked from the retq (where _done) put it
- * XXX as cryptoret won't get the chance.  The queue is walked backwards
- * XXX as the request is generally the last one queued.
- *
- *	 call with the lock held, or else.
- */
-int
-crypto_ret_q_remove(struct cryptop *crp)
-{
-	struct cryptop * acrp, *next;
-
-	TAILQ_FOREACH_REVERSE_SAFE(acrp, _ret_q, crprethead, crp_next, next) {
-		if (acrp == crp) {
-			TAILQ_REMOVE(_ret_q, crp, crp_next);
-			crp->crp_flags &= (~CRYPTO_F_ONRETQ);
-			return 1;
-		}
-	}
-	return 0;
-}
-
-int
-crypto_ret_kq_remove(struct cryptkop *krp)
-{
-	struct cryptkop * akrp, *next;
-
-	TAILQ_FOREACH_REVERSE_SAFE(akrp, _ret_kq, krprethead, krp_next, next) {
-		if (akrp == krp) {
-			TAILQ_REMOVE(_ret_kq, krp, krp_next);
-			krp->krp_flags &= (~CRYPTO_F_ONRETQ);
-			return 1;
-		}
-	}
-	return 0;
-}
-
-/*
  * Crypto op and desciptor data structures are allocated
  * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
  */

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.87 src/sys/opencrypto/cryptodev.c:1.88
--- src/sys/opencrypto/cryptodev.c:1.87	Fri Apr  7 12:15:51 2017
+++ src/sys/opencrypto/cryptodev.c	Fri Apr  7 12:17:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.87 2017/04/07 12:15:51 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.88 2017/04/07 12:17:57 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.87 2017/04/07 12:15:51 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.88 2017/04/07 12:17:57 knakahara Exp $");
 
 #include 
 #include 
@@ -668,13 +668,6 @@ eagain:
 			(uint32_t)cse->sid, >crp_cv, crp));
 		cv_wait(>crp_cv, _mtx);	/* XXX cv_wait_sig? */
 	}
-	if (crp->crp_flags & CRYPTO_F_ONRETQ) {
-		/* XXX this should never happen now with the CRYPTO_F_USER flag
-		 * changes.
-		 */
-		DPRINTF(("cryptodev_op: DONE, not woken by cryptoret.\n"));
-		(void)crypto_ret_q_remove(crp);
-	}
 	mutex_exit(_mtx);
 	cv_destroy(>crp_cv);
 
@@ -895,10 +888,6 @@ cryptodev_key(struct crypt_kop *kop)
 	while (!(krp->krp_flags & CRYPTO_F_DQRETQ)) {
 		cv_wait(>krp_cv, _mtx);	/* XXX cv_wait_sig? */
 	}
-	if (krp->krp_flags & CRYPTO_F_ONRETQ) {
-		DPRINTF(("cryptodev_key: DONE early, not via cryptoret.\n"));
-		(void)crypto_ret_kq_remove(krp);
-	}
 	mutex_exit(_mtx);
 
 	if (krp->krp_status != 0) {



CVS commit: src/sys/opencrypto

2017-04-07 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Fri Apr  7 12:15:51 UTC 2017

Modified Files:
src/sys/opencrypto: cryptodev.c cryptodev.h

Log Message:
fix race among crypto_done(), cryptoret(), and {cryptodev_op(), 
cryptodev_key()}.

crypto_op() waited to be set CRYPTO_F_DONE with crp->crp_cv.
However, there is context switch chances between being set CRYPTO_F_DONE in
crypto_done() and done cv_signal(crp->crp_cv) in cryptodev_cb(), that is,
cryptodev_op() thread can run to cv_destroy(crp->crp_cv) before cryptoret()
thread is waken up. As a result, cryptodev_cb() can call invalid(destroyed)
cv_signal(crp->crp_cv).

Furthermore, below two implementations cause other races.
- waiting CRYPTO_F_DONE with crp->crp_cv
- context witch chances between set CRYPTO_F_DONE and cv_signal(crp->crp_cv)

So, use other flag(CRYPTO_F_DQRETQ) for cryptodev_op() and cryptodev_key(),
and then call cv_signal(crp->crp_cv) immediately after set CRYPTO_F_DQRETQ.

Tested concurrent over 20 processes with software and hardware drivers.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.29 -r1.30 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.86 src/sys/opencrypto/cryptodev.c:1.87
--- src/sys/opencrypto/cryptodev.c:1.86	Wed Apr  5 08:51:04 2017
+++ src/sys/opencrypto/cryptodev.c	Fri Apr  7 12:15:51 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.86 2017/04/05 08:51:04 knakahara Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.87 2017/04/07 12:15:51 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.86 2017/04/05 08:51:04 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.87 2017/04/07 12:15:51 knakahara Exp $");
 
 #include 
 #include 
@@ -663,7 +663,7 @@ eagain:
 		goto bail;
 	}
 
-	while (!(crp->crp_flags & CRYPTO_F_DONE)) {
+	while (!(crp->crp_flags & CRYPTO_F_DQRETQ)) {
 		DPRINTF(("cryptodev_op[%d]: sleeping on cv %p for crp %p\n",
 			(uint32_t)cse->sid, >crp_cv, crp));
 		cv_wait(>crp_cv, _mtx);	/* XXX cv_wait_sig? */
@@ -744,6 +744,7 @@ cryptodev_cb(void *op)
 		mutex_enter(_mtx);
 	}
 	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
+		crp->crp_flags |= CRYPTO_F_DQRETQ;
 		cv_signal(>crp_cv);
 	}
 	mutex_exit(_mtx);
@@ -780,6 +781,7 @@ cryptodevkey_cb(void *op)
 	struct cryptkop *krp = op;
 	
 	mutex_enter(_mtx);
+	krp->krp_flags |= CRYPTO_F_DQRETQ;
 	cv_signal(>krp_cv);
 	mutex_exit(_mtx);
 	return 0;
@@ -890,7 +892,7 @@ cryptodev_key(struct crypt_kop *kop)
 	}
 
 	mutex_enter(_mtx);
-	while (!(krp->krp_flags & CRYPTO_F_DONE)) {
+	while (!(krp->krp_flags & CRYPTO_F_DQRETQ)) {
 		cv_wait(>krp_cv, _mtx);	/* XXX cv_wait_sig? */
 	}
 	if (krp->krp_flags & CRYPTO_F_ONRETQ) {

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.29 src/sys/opencrypto/cryptodev.h:1.30
--- src/sys/opencrypto/cryptodev.h:1.29	Mon Mar  6 09:59:05 2017
+++ src/sys/opencrypto/cryptodev.h	Fri Apr  7 12:15:51 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.29 2017/03/06 09:59:05 knakahara Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.30 2017/04/07 12:15:51 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -473,6 +473,7 @@ struct cryptop {
 #define	CRYPTO_F_ONRETQ		0x0080	/* Request is on return queue */
 #define	CRYPTO_F_USER		0x0100	/* Request is in user context */
 #define	CRYPTO_F_MORE		0x0200	/* more data to follow */
+#define	CRYPTO_F_DQRETQ		0x0400	/* Dequeued from crp_ret_{,k}q */
 
 	void *		crp_buf;	/* Data to be processed */
 	void *		crp_opaque;	/* Opaque pointer, passed along */



CVS commit: src/sys/opencrypto

2017-04-06 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Apr  6 09:39:12 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix build failure ALL


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.52 src/sys/opencrypto/crypto.c:1.53
--- src/sys/opencrypto/crypto.c:1.52	Wed Apr  5 08:51:04 2017
+++ src/sys/opencrypto/crypto.c	Thu Apr  6 09:39:12 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.52 2017/04/05 08:51:04 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.53 2017/04/06 09:39:12 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.52 2017/04/05 08:51:04 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.53 2017/04/06 09:39:12 knakahara Exp $");
 
 #include 
 #include 
@@ -420,7 +420,7 @@ crypto_newsession(u_int64_t *sid, struct
 crypto_drivers[hid].cc_sessions++;
 			} else {
 DPRINTF(("%s: crypto_drivers[%d].cc_newsession() failed. error=%d\n",
-	__func__, hid, error));
+	__func__, hid, err));
 			}
 			goto done;
 			/*break;*/



CVS commit: src/sys/opencrypto

2017-04-05 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Apr  5 08:51:04 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.c

Log Message:
fix processes accessing /dev/crypto stall when over three processes run with a 
hardware encryption driver

The process has stalled at cv_wait(>crp_cv) because cryptodev_cb()
is not called as cryptoret() kthread keep waiting at cv_wait(_cv).
Previous opencrypto implementation assumes the thread from cryptodev.c
does all processing in the same context, so skips enqueueing and sending
cryptoret_cv. However, the context can be switched, e.g. when we use
a hardware encryption driver.

And add debug messages.


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.85 -r1.86 src/sys/opencrypto/cryptodev.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.51 src/sys/opencrypto/crypto.c:1.52
--- src/sys/opencrypto/crypto.c:1.51	Wed Mar 29 23:02:43 2017
+++ src/sys/opencrypto/crypto.c	Wed Apr  5 08:51:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.51 2017/03/29 23:02:43 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.52 2017/04/05 08:51:04 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.51 2017/03/29 23:02:43 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.52 2017/04/05 08:51:04 knakahara Exp $");
 
 #include 
 #include 
@@ -418,6 +418,9 @@ crypto_newsession(u_int64_t *sid, struct
 (*sid) <<= 32;
 (*sid) |= (lid & 0x);
 crypto_drivers[hid].cc_sessions++;
+			} else {
+DPRINTF(("%s: crypto_drivers[%d].cc_newsession() failed. error=%d\n",
+	__func__, hid, error));
 			}
 			goto done;
 			/*break;*/
@@ -1124,19 +1127,20 @@ crypto_done(struct cryptop *crp)
 	} else {
 		mutex_spin_enter(_ret_q_mtx);
 		crp->crp_flags |= CRYPTO_F_DONE;
-
+#if 0
 		if (crp->crp_flags & CRYPTO_F_USER) {
-			/* the request has completed while
-			 * running in the user context
-			 * so don't queue it - the user
-			 * thread won't sleep when it sees
-			 * the CRYPTO_F_DONE flag.
-			 * This is an optimization to avoid
-			 * unecessary context switches.
+			/*
+			 * TODO:
+			 * If crp->crp_flags & CRYPTO_F_USER and the used
+			 * encryption driver does all the processing in
+			 * the same context, we can skip enqueueing crp_ret_q
+			 * and cv_signal(_cv).
 			 */
 			DPRINTF(("crypto_done[%u]: crp %p CRYPTO_F_USER\n",
 CRYPTO_SESID2LID(crp->crp_sid), crp));
-		} else {
+		} else
+#endif
+		{
 			wasempty = TAILQ_EMPTY(_ret_q);
 			DPRINTF(("crypto_done[%u]: queueing %p\n",
 CRYPTO_SESID2LID(crp->crp_sid), crp));

Index: src/sys/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.85 src/sys/opencrypto/cryptodev.c:1.86
--- src/sys/opencrypto/cryptodev.c:1.85	Thu Jul  7 06:55:43 2016
+++ src/sys/opencrypto/cryptodev.c	Wed Apr  5 08:51:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.85 2016/07/07 06:55:43 msaitoh Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.86 2017/04/05 08:51:04 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.85 2016/07/07 06:55:43 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptodev.c,v 1.86 2017/04/05 08:51:04 knakahara Exp $");
 
 #include 
 #include 
@@ -578,10 +578,10 @@ cryptodev_op(struct csession *cse, struc
 
 
 	crp->crp_ilen = cop->len;
-	/* The reqest is flagged as CRYPTO_F_USER as long as it is running
-	 * in the user IOCTL thread.  This flag lets us skip using the retq for
-	 * the request if it completes immediately. If the request ends up being
-	 * delayed or is not completed immediately the flag is removed.
+	/*
+	 * The request is flagged as CRYPTO_F_USER as long as it is running
+	 * in the user IOCTL thread. However, whether the request completes
+	 * immediately or belatedly is depends on the used encryption driver.
 	 */
 	crp->crp_flags = CRYPTO_F_IOV | (cop->flags & COP_F_BATCH) | CRYPTO_F_USER |
 			flags;
@@ -643,12 +643,9 @@ eagain:
 	mutex_enter(_mtx);
 
 	/* 
-	 * If the request was going to be completed by the
-	 * ioctl thread then it would have been done by now.
-	 * Remove the F_USER flag so crypto_done() is not confused
-	 * if the crypto device calls it after this point.
+	 * Don't touch crp before returned by any error or recieved
+	 * cv_signal(>crp_cv). It is required to restructure locks.
 	 */
-	crp->crp_flags &= ~(CRYPTO_F_USER);
 
 	switch (error) {
 #ifdef notyet	/* don't loop forever -- but EAGAIN not possible here yet */



CVS commit: src/sys/opencrypto

2017-03-29 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Mar 29 23:02:43 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix missing mutex_exit() in crypto_destroy().

crypto_destroy() is called only in error case or unloading module.


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.50 src/sys/opencrypto/crypto.c:1.51
--- src/sys/opencrypto/crypto.c:1.50	Thu Mar 16 05:23:56 2017
+++ src/sys/opencrypto/crypto.c	Wed Mar 29 23:02:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.50 2017/03/16 05:23:56 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.51 2017/03/29 23:02:43 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.50 2017/03/16 05:23:56 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.51 2017/03/29 23:02:43 knakahara Exp $");
 
 #include 
 #include 
@@ -306,14 +306,18 @@ crypto_destroy(bool exit_kthread)
 		mutex_spin_enter(_ret_q_mtx);
 
 		/* if we have any in-progress requests, don't unload */
-		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq))
+		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq)) {
+			mutex_spin_exit(_ret_q_mtx);
 			return EBUSY;
+		}
 
 		for (i = 0; i < crypto_drivers_num; i++)
 			if (crypto_drivers[i].cc_sessions != 0)
 break;
-		if (i < crypto_drivers_num)
+		if (i < crypto_drivers_num) {
+			mutex_spin_exit(_ret_q_mtx);
 			return EBUSY;
+		}
 
 		/* kick the cryptoret thread and wait for it to exit */
 		crypto_exit_flag = 1;



CVS commit: src/sys/opencrypto

2017-03-15 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Mar 16 05:23:56 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix: remove unmatched mutex_exit/enter. must be forgetting to remove at 
crypto.c:r1.41


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.49 src/sys/opencrypto/crypto.c:1.50
--- src/sys/opencrypto/crypto.c:1.49	Thu Feb  9 06:03:29 2017
+++ src/sys/opencrypto/crypto.c	Thu Mar 16 05:23:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.49 2017/02/09 06:03:29 knakahara Exp $ */
+/*	$NetBSD: crypto.c,v 1.50 2017/03/16 05:23:56 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.49 2017/02/09 06:03:29 knakahara Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.50 2017/03/16 05:23:56 knakahara Exp $");
 
 #include 
 #include 
@@ -982,11 +982,8 @@ crypto_invoke(struct cryptop *crp, int h
 		int (*process)(void *, struct cryptop *, int);
 		void *arg;
 
-		if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) {
-			mutex_exit(_mtx);
+		if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
 			crypto_freesession(crp->crp_sid);
-			mutex_enter(_mtx);
-		}
 		process = crypto_drivers[hid].cc_process;
 		arg = crypto_drivers[hid].cc_arg;
 



CVS commit: src/sys/opencrypto

2017-02-08 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Thu Feb  9 06:03:29 UTC 2017

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
make some locks and condvar static


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.48 src/sys/opencrypto/crypto.c:1.49
--- src/sys/opencrypto/crypto.c:1.48	Thu Jul  7 06:55:43 2016
+++ src/sys/opencrypto/crypto.c	Thu Feb  9 06:03:29 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.48 2016/07/07 06:55:43 msaitoh Exp $ */
+/*	$NetBSD: crypto.c,v 1.49 2017/02/09 06:03:29 knakahara Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.48 2016/07/07 06:55:43 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.49 2017/02/09 06:03:29 knakahara Exp $");
 
 #include 
 #include 
@@ -75,9 +75,9 @@ __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1
 #include 
 #include 			/* XXX for M_XDATA */
 
-kmutex_t crypto_q_mtx;
-kmutex_t crypto_ret_q_mtx;
-kcondvar_t cryptoret_cv;
+static kmutex_t crypto_q_mtx;
+static kmutex_t crypto_ret_q_mtx;
+static kcondvar_t cryptoret_cv;
 kmutex_t crypto_mtx;
 
 /* below are kludges for residual code wrtitten to FreeBSD interfaces */



CVS commit: src/sys/opencrypto

2016-09-26 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Sep 26 14:50:54 UTC 2016

Modified Files:
src/sys/opencrypto: aesxcbcmac.c

Log Message:
>From Alexander Nasonov:
- Make constants static: Shrinks code and data size.
- Avoid overflow in limit calculation.
- Use uint8_t instead of u_char to match types
While here:
- Remove unnecessary casts
- s/u_int8_t/uint8_t/g


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/opencrypto/aesxcbcmac.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/opencrypto/aesxcbcmac.c
diff -u src/sys/opencrypto/aesxcbcmac.c:1.1 src/sys/opencrypto/aesxcbcmac.c:1.2
--- src/sys/opencrypto/aesxcbcmac.c:1.1	Tue May 24 15:10:08 2011
+++ src/sys/opencrypto/aesxcbcmac.c	Mon Sep 26 10:50:54 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: aesxcbcmac.c,v 1.1 2011/05/24 19:10:08 drochner Exp $ */
+/* $NetBSD: aesxcbcmac.c,v 1.2 2016/09/26 14:50:54 christos Exp $ */
 
 /*
  * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.1 2011/05/24 19:10:08 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.2 2016/09/26 14:50:54 christos Exp $");
 
 #include 
 #include 
@@ -39,17 +39,20 @@ __KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c
 #include 
 
 int
-aes_xcbc_mac_init(void *vctx, const u_int8_t *key, u_int16_t keylen)
+aes_xcbc_mac_init(void *vctx, const uint8_t *key, u_int16_t keylen)
 {
-	u_int8_t k1seed[AES_BLOCKSIZE] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
-	u_int8_t k2seed[AES_BLOCKSIZE] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
-	u_int8_t k3seed[AES_BLOCKSIZE] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
+	static const uint8_t k1seed[AES_BLOCKSIZE] =
+	{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
+	static const uint8_t k2seed[AES_BLOCKSIZE] =
+	{ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
+	static const uint8_t k3seed[AES_BLOCKSIZE] =
+	{ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
 	u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
 	aesxcbc_ctx *ctx;
-	u_int8_t k1[AES_BLOCKSIZE];
+	uint8_t k1[AES_BLOCKSIZE];
 
-	ctx = (aesxcbc_ctx *)vctx;
-	memset(ctx, 0, sizeof(aesxcbc_ctx));
+	ctx = vctx;
+	memset(ctx, 0, sizeof(*ctx));
 
 	if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks, key, keylen * 8)) == 0)
 		return -1;
@@ -67,14 +70,14 @@ aes_xcbc_mac_init(void *vctx, const u_in
 }
 
 int
-aes_xcbc_mac_loop(void *vctx, const u_int8_t *addr, u_int16_t len)
+aes_xcbc_mac_loop(void *vctx, const uint8_t *addr, u_int16_t len)
 {
-	u_int8_t buf[AES_BLOCKSIZE];
+	uint8_t buf[AES_BLOCKSIZE];
 	aesxcbc_ctx *ctx;
-	const u_int8_t *ep;
+	const uint8_t *ep;
 	int i;
 
-	ctx = (aesxcbc_ctx *)vctx;
+	ctx = vctx;
 	ep = addr + len;
 
 	if (ctx->buflen == sizeof(ctx->buf)) {
@@ -98,7 +101,7 @@ aes_xcbc_mac_loop(void *vctx, const u_in
 		ctx->buflen = 0;
 	}
 	/* due to the special processing for M[n], "=" case is not included */
-	while (addr + AES_BLOCKSIZE < ep) {
+	while (ep - addr > AES_BLOCKSIZE) {
 		memcpy(buf, addr, AES_BLOCKSIZE);
 		for (i = 0; i < sizeof(buf); i++)
 			buf[i] ^= ctx->e[i];
@@ -113,13 +116,13 @@ aes_xcbc_mac_loop(void *vctx, const u_in
 }
 
 void
-aes_xcbc_mac_result(u_int8_t *addr, void *vctx)
+aes_xcbc_mac_result(uint8_t *addr, void *vctx)
 {
-	u_char digest[AES_BLOCKSIZE];
+	uint8_t digest[AES_BLOCKSIZE];
 	aesxcbc_ctx *ctx;
 	int i;
 
-	ctx = (aesxcbc_ctx *)vctx;
+	ctx = vctx;
 
 	if (ctx->buflen == sizeof(ctx->buf)) {
 		for (i = 0; i < sizeof(ctx->buf); i++) {



CVS commit: src/sys/opencrypto

2016-01-22 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Fri Jan 22 22:35:27 UTC 2016

Modified Files:
src/sys/opencrypto: cryptodev.h

Log Message:
Needs sys/time.h for struct timespec.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/cryptodev.h
diff -u src/sys/opencrypto/cryptodev.h:1.26 src/sys/opencrypto/cryptodev.h:1.27
--- src/sys/opencrypto/cryptodev.h:1.26	Sat Nov 28 03:06:45 2015
+++ src/sys/opencrypto/cryptodev.h	Fri Jan 22 22:35:27 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.h,v 1.26 2015/11/28 03:06:45 pgoyette Exp $ */
+/*	$NetBSD: cryptodev.h,v 1.27 2016/01/22 22:35:27 dholland Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/
 /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/
 
@@ -87,6 +87,7 @@
 
 #include 
 #include 
+#include 
 
 /* Some initial values */
 #define CRYPTO_DRIVERS_INITIAL	4



CVS commit: src/sys/opencrypto

2015-11-27 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Nov 28 03:40:43 UTC 2015

Modified Files:
src/sys/opencrypto: crypto.c

Log Message:
fix the build


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/opencrypto/crypto.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/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.46 src/sys/opencrypto/crypto.c:1.47
--- src/sys/opencrypto/crypto.c:1.46	Fri Nov 27 22:06:45 2015
+++ src/sys/opencrypto/crypto.c	Fri Nov 27 22:40:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.46 2015/11/28 03:06:45 pgoyette Exp $ */
+/*	$NetBSD: crypto.c,v 1.47 2015/11/28 03:40:43 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.46 2015/11/28 03:06:45 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.47 2015/11/28 03:40:43 christos Exp $");
 
 #include 
 #include 
@@ -247,7 +247,7 @@ static	int crypto_timing = 0;
 #endif
 
 #ifdef _MODULE
-	static struct sysctllog *sysctl_opencrypto_clog;
+static struct sysctllog *sysctl_opencrypto_clog;
 #endif
 
 static int
@@ -324,8 +324,10 @@ crypto_destroy(bool exit_kthread)
 		mutex_spin_exit(_ret_q_mtx);
 	}
 
+#ifdef _MODULE
 	if (sysctl_opencrypto_clog != NULL)
 		sysctl_teardown(_opencrypto_clog);
+#endif
 
 	unregister_swi(SWI_CRYPTO, cryptointr);
 



CVS commit: src/sys/opencrypto

2015-11-27 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Nov 28 03:06:46 UTC 2015

Modified Files:
src/sys/opencrypto: crypto.c cryptodev.h

Log Message:
Re-work the module init and destroy code to allow it to be unloaded and
then reloaded.

Should fix PR kern/49842


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/opencrypto/crypto.c
cvs rdiff -u -r1.25 -r1.26 src/sys/opencrypto/cryptodev.h

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

Modified files:

Index: src/sys/opencrypto/crypto.c
diff -u src/sys/opencrypto/crypto.c:1.45 src/sys/opencrypto/crypto.c:1.46
--- src/sys/opencrypto/crypto.c:1.45	Tue Feb 25 18:30:12 2014
+++ src/sys/opencrypto/crypto.c	Sat Nov 28 03:06:45 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: crypto.c,v 1.45 2014/02/25 18:30:12 pooka Exp $ */
+/*	$NetBSD: crypto.c,v 1.46 2015/11/28 03:06:45 pgoyette Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $	*/
 /*	$OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $	*/
 
@@ -53,7 +53,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.45 2014/02/25 18:30:12 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.46 2015/11/28 03:06:45 pgoyette Exp $");
 
 #include 
 #include 
@@ -97,6 +97,7 @@ int crypto_ret_q_check(struct cryptop *)
 static	struct cryptocap *crypto_drivers;
 static	int crypto_drivers_num;
 static	void *softintr_cookie;
+static	int crypto_exit_flag;
 
 /*
  * There are two queues for crypto requests; one for symmetric (e.g.
@@ -236,7 +237,7 @@ MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "
 static	void cryptointr(void);		/* swi thread to dispatch ops */
 static	void cryptoret(void);		/* kernel thread for callbacks*/
 static	struct lwp *cryptothread;
-static	void crypto_destroy(void);
+static	int crypto_destroy(bool);
 static	int crypto_invoke(struct cryptop *crp, int hint);
 static	int crypto_kinvoke(struct cryptkop *krp, int hint);
 
@@ -269,7 +270,7 @@ crypto_init0(void)
 	sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
 	if (crypto_drivers == NULL) {
 		printf("crypto_init: cannot malloc driver table\n");
-		return 0;
+		return ENOMEM;
 	}
 	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
 
@@ -279,7 +280,7 @@ crypto_init0(void)
 	if (error) {
 		printf("crypto_init: cannot start cryptoret thread; error %d",
 			error);
-		crypto_destroy();
+		return crypto_destroy(false);
 	}
 
 #ifdef _MODULE
@@ -288,21 +289,60 @@ crypto_init0(void)
 	return 0;
 }
 
-void
+int
 crypto_init(void)
 {
 	static ONCE_DECL(crypto_init_once);
 
-	RUN_ONCE(_init_once, crypto_init0);
+	return RUN_ONCE(_init_once, crypto_init0);
 }
 
-static void
-crypto_destroy(void)
+static int
+crypto_destroy(bool exit_kthread)
 {
-	/* XXX no wait to reclaim zones */
+	int i;
+
+	if (exit_kthread) {
+		mutex_spin_enter(_ret_q_mtx);
+
+		/* if we have any in-progress requests, don't unload */
+		if (!TAILQ_EMPTY(_q) || !TAILQ_EMPTY(_kq))
+			return EBUSY;
+
+		for (i = 0; i < crypto_drivers_num; i++)
+			if (crypto_drivers[i].cc_sessions != 0)
+break;
+		if (i < crypto_drivers_num)
+			return EBUSY;
+
+		/* kick the cryptoret thread and wait for it to exit */
+		crypto_exit_flag = 1;
+		cv_signal(_cv);
+
+		while (crypto_exit_flag != 0)
+			cv_wait(_cv, _ret_q_mtx);
+		mutex_spin_exit(_ret_q_mtx);
+	}
+
+	if (sysctl_opencrypto_clog != NULL)
+		sysctl_teardown(_opencrypto_clog);
+
+	unregister_swi(SWI_CRYPTO, cryptointr);
+
 	if (crypto_drivers != NULL)
 		free(crypto_drivers, M_CRYPTO_DATA);
-	unregister_swi(SWI_CRYPTO, cryptointr);
+
+	pool_destroy(_pool);
+	pool_destroy(_pool);
+	pool_destroy(_pool);
+
+	cv_destroy(_cv);
+
+	mutex_destroy(_ret_q_mtx);
+	mutex_destroy(_q_mtx);
+	mutex_destroy(_mtx);
+
+	return 0;
 }
 
 /*
@@ -441,7 +481,7 @@ crypto_get_driverid(u_int32_t flags)
 	struct cryptocap *newdrv;
 	int i;
 
-	crypto_init();		/* XXX oh, this is foul! */
+	(void)crypto_init();		/* XXX oh, this is foul! */
 
 	mutex_enter(_mtx);
 	for (i = 0; i < crypto_drivers_num; i++)
@@ -1306,6 +1346,17 @@ cryptoret(void)
 
 		/* drop before calling any callbacks. */
 		if (crp == NULL && krp == NULL) {
+
+/* Check for the exit condition. */
+			if (crypto_exit_flag != 0) {
+
+			/* Time to die. */
+crypto_exit_flag = 0;
+			cv_broadcast(_cv);
+mutex_spin_exit(_ret_q_mtx);
+			kthread_exit(0);
+			}
+
 			cryptostats.cs_rets++;
 			cv_wait(_cv, _ret_q_mtx);
 			continue;
@@ -1345,19 +1396,21 @@ MODULE(MODULE_CLASS_MISC, opencrypto, NU
 static int
 opencrypto_modcmd(modcmd_t cmd, void *opaque)
 {
+	int error = 0;
 
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 #ifdef _MODULE
-		crypto_init();
+		error = crypto_init();
 #endif
-		return 0;
+		break;
 	case MODULE_CMD_FINI:
 #ifdef _MODULE
-		sysctl_teardown(_opencrypto_clog);
+		error = crypto_destroy(true);
 #endif
-		return 0;
+		break;
 	default:
-		return ENOTTY;
+		error = ENOTTY;
 	}
+	return error;
 

CVS commit: src/sys/opencrypto

2015-03-26 Thread Patrick Welche
Module Name:src
Committed By:   prlw1
Date:   Thu Mar 26 17:40:16 UTC 2015

Modified Files:
src/sys/opencrypto: cryptodev.c deflate.c

Log Message:
Trivial printf format changes and typo fix


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.21 -r1.22 src/sys/opencrypto/deflate.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.82 src/sys/opencrypto/cryptodev.c:1.83
--- src/sys/opencrypto/cryptodev.c:1.82	Thu Nov 27 20:30:05 2014
+++ src/sys/opencrypto/cryptodev.c	Thu Mar 26 17:40:16 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.82 2014/11/27 20:30:05 christos Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.83 2015/03/26 17:40:16 prlw1 Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.82 2014/11/27 20:30:05 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.83 2015/03/26 17:40:16 prlw1 Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -1709,7 +1709,7 @@ cryptodev_session(struct fcrypt *fcr, st
 
 	error = crypto_newsession(sid, crihead, crypto_devallowsoft);
 	if (!error) {
-		DPRINTF((cyrptodev_session: got session %d\n, (uint32_t)sid));
+		DPRINTF((cryptodev_session: got session %d\n, (uint32_t)sid));
 		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
 		cria.cri_key, cria.cri_klen, (txform ? sop-cipher : 0), sop-mac,
 		(tcomp ? sop-comp_alg : 0), txform, thash, tcomp);

Index: src/sys/opencrypto/deflate.c
diff -u src/sys/opencrypto/deflate.c:1.21 src/sys/opencrypto/deflate.c:1.22
--- src/sys/opencrypto/deflate.c:1.21	Sun Jul  3 01:01:06 2011
+++ src/sys/opencrypto/deflate.c	Thu Mar 26 17:40:16 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: deflate.c,v 1.21 2011/07/03 01:01:06 mrg Exp $ */
+/*	$NetBSD: deflate.c,v 1.22 2015/03/26 17:40:16 prlw1 Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/deflate.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
 
@@ -35,7 +35,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: deflate.c,v 1.21 2011/07/03 01:01:06 mrg Exp $);
+__KERNEL_RCSID(0, $NetBSD: deflate.c,v 1.22 2015/03/26 17:40:16 prlw1 Exp $);
 
 #include sys/types.h
 #include sys/malloc.h
@@ -88,7 +88,7 @@ deflate_global(u_int8_t *data, u_int32_t
 	int error, i, j;
 	struct deflate_buf buf[ZBUF];
 
-	DPRINTF((deflate_global: size %d\n, size));
+	DPRINTF((deflate_global: size %u\n, size));
 
 	memset(zbuf, 0, sizeof(z_stream));
 	zbuf.next_in = data;	/* data that is going to be processed */
@@ -236,7 +236,7 @@ gzip_global(u_int8_t *data, u_int32_t si
 	u_int32_t crc;
 	u_int32_t isize = 0, icrc = 0;
 
-	DPRINTF((gzip_global: decomp %d, size %d\n, decomp, size));
+	DPRINTF((gzip_global: decomp %d, size %u\n, decomp, size));
 
 	memset(zbuf, 0, sizeof(z_stream));
 	zbuf.zalloc = ocf_zalloc;
@@ -245,13 +245,13 @@ gzip_global(u_int8_t *data, u_int32_t si
 
 	if (!decomp) {
 		/* compress */
-		DPRINTF((gzip_global: compress malloc %d + %d + %d = %d\n,
+		DPRINTF((gzip_global: compress malloc %u + %zu + %u = %zu\n,
 size, sizeof(gzip_header), GZIP_TAIL_SIZE,
 size + sizeof(gzip_header) + GZIP_TAIL_SIZE));
 
 		buf[0].size = size;
 		crc = crc32(0, data, size);
-		DPRINTF((gzip_compress: size %d, crc 0x%x\n, size, crc));
+		DPRINTF((gzip_compress: size %u, crc 0x%x\n, size, crc));
 		zbuf.avail_in = size;	/* Total length of data to be processed */
 		zbuf.next_in = data;	/* data that is going to be processed */
 	} else {
@@ -259,7 +259,7 @@ gzip_global(u_int8_t *data, u_int32_t si
 		/* check the gzip header */
 		if (size = sizeof(gzip_header) + GZIP_TAIL_SIZE) {
 			/* Not enough data for the header  tail */
-			DPRINTF((gzip_global: not enough data (%d)\n,
+			DPRINTF((gzip_global: not enough data (%u)\n,
 	size));
 			return 0;
 		}
@@ -284,7 +284,7 @@ gzip_global(u_int8_t *data, u_int32_t si
 		memcpy(icrc, data[size-2*sizeof(uint32_t)], sizeof(uint32_t));
 		LE32TOH(icrc);
 
-		DPRINTF((gzip_global: isize = %d (%02x %02x %02x %02x)\n,
+		DPRINTF((gzip_global: isize = %u (%02x %02x %02x %02x)\n,
 isize,
 data[size-4],
 data[size-3],
@@ -306,7 +306,7 @@ gzip_global(u_int8_t *data, u_int32_t si
 		return 0;
 	zbuf.next_out = buf[0].out;
 	zbuf.avail_out = buf[0].size;
-	DPRINTF((zbuf avail_in %d, avail_out %d\n,
+	DPRINTF((zbuf avail_in %u, avail_out %u\n,
 			zbuf.avail_in, zbuf.avail_out));
 	i = 1;
 
@@ -319,11 +319,11 @@ gzip_global(u_int8_t *data, u_int32_t si
 		goto bad2;
 	}
 	for (;;) {
-		DPRINTF((pre: %s in:%d out:%d\n, decomp ? deflate() : inflate(), 
+		DPRINTF((pre: %s in:%u out:%u\n, decomp ? deflate() : inflate(), 
 zbuf.avail_in, zbuf.avail_out));
 

CVS commit: src/sys/opencrypto

2014-11-27 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Nov 27 20:30:21 UTC 2014

Modified Files:
src/sys/opencrypto: cryptosoft_xform.c

Log Message:
simplify, no functional change


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/opencrypto/cryptosoft_xform.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/opencrypto/cryptosoft_xform.c
diff -u src/sys/opencrypto/cryptosoft_xform.c:1.26 src/sys/opencrypto/cryptosoft_xform.c:1.27
--- src/sys/opencrypto/cryptosoft_xform.c:1.26	Sat Feb  2 16:06:31 2013
+++ src/sys/opencrypto/cryptosoft_xform.c	Thu Nov 27 15:30:21 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft_xform.c,v 1.26 2013/02/02 21:06:31 christos Exp $ */
+/*	$NetBSD: cryptosoft_xform.c,v 1.27 2014/11/27 20:30:21 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $	*/
 
@@ -40,7 +40,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(1, $NetBSD: cryptosoft_xform.c,v 1.26 2013/02/02 21:06:31 christos Exp $);
+__KERNEL_RCSID(1, $NetBSD: cryptosoft_xform.c,v 1.27 2014/11/27 20:30:21 christos Exp $);
 
 #include crypto/blowfish/blowfish.h
 #include crypto/cast128/cast128.h
@@ -427,17 +427,14 @@ static int
 des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
 	des_key_schedule *p;
-	int err;
 
 	p = malloc(sizeof (des_key_schedule),
-		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
-	if (p != NULL) {
-		des_set_key((des_cblock *)__UNCONST(key), p[0]);
-		err = 0;
-	} else
-		err = ENOMEM;
+	M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
 	*sched = (u_int8_t *) p;
-	return err;
+	if (p == NULL)
+		return ENOMEM;
+	des_set_key((des_cblock *)__UNCONST(key), p[0]);
+	return 0;
 }
 
 static void
@@ -470,19 +467,16 @@ static int
 des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
 	des_key_schedule *p;
-	int err;
 
 	p = malloc(3*sizeof (des_key_schedule),
 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
-	if (p != NULL) {
-		des_set_key((des_cblock *)__UNCONST(key +  0), p[0]);
-		des_set_key((des_cblock *)__UNCONST(key +  8), p[1]);
-		des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
-		err = 0;
-	} else
-		err = ENOMEM;
 	*sched = (u_int8_t *) p;
-	return err;
+	if (p == NULL)
+		return ENOMEM;
+	des_set_key((des_cblock *)__UNCONST(key +  0), p[0]);
+	des_set_key((des_cblock *)__UNCONST(key +  8), p[1]);
+	des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
+	return 0;
 }
 
 static void
@@ -510,16 +504,13 @@ blf_decrypt(void *key, u_int8_t *blk)
 static int
 blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
-	int err;
 
 	*sched = malloc(sizeof(BF_KEY),
 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
-	if (*sched != NULL) {
-		BF_set_key((BF_KEY *) *sched, len, key);
-		err = 0;
-	} else
-		err = ENOMEM;
-	return err;
+	if (*sched == NULL)
+		return ENOMEM;
+	BF_set_key((BF_KEY *) *sched, len, key);
+	return 0;
 }
 
 static void
@@ -545,16 +536,13 @@ cast5_decrypt(void *key, u_int8_t *blk)
 static int
 cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
-	int err;
 
 	*sched = malloc(sizeof(cast128_key), M_CRYPTO_DATA,
 	   M_NOWAIT|M_ZERO);
-	if (*sched != NULL) {
-		cast128_setkey((cast128_key *)*sched, key, len);
-		err = 0;
-	} else
-		err = ENOMEM;
-	return err;
+	if (*sched == NULL)
+		return ENOMEM;
+	cast128_setkey((cast128_key *)*sched, key, len);
+	return 0;
 }
 
 static void
@@ -580,7 +568,6 @@ skipjack_decrypt(void *key, u_int8_t *bl
 static int
 skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
-	int err;
 
 	/* NB: allocate all the memory that's needed at once */
 	/* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
@@ -589,21 +576,19 @@ skipjack_setkey(u_int8_t **sched, const 
 	*sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
 		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
 
-	if (*sched != NULL) {
+	if (*sched == NULL)
+		return ENOMEM;
 
-		u_int8_t** key_tables = (u_int8_t**) *sched;
-		u_int8_t* table = (u_int8_t*) key_tables[10];
-		int k;
-
-		for (k = 0; k  10; k++) {
-			key_tables[k] = table;
-			table += 0x100;
-		}
-		subkey_table_gen(key, (u_int8_t **) *sched);
-		err = 0;
-	} else
-		err = ENOMEM;
-	return err;
+	u_int8_t** key_tables = (u_int8_t**) *sched;
+	u_int8_t* table = (u_int8_t*) key_tables[10];
+	int k;
+
+	for (k = 0; k  10; k++) {
+		key_tables[k] = table;
+		table += 0x100;
+	}
+	subkey_table_gen(key, (u_int8_t **) *sched);
+	return 0;
 }
 
 static void
@@ -630,18 +615,15 @@ rijndael128_decrypt(void *key, u_int8_t 
 static int
 rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
 {
-	int err;
 
 	if (len != 16  len != 24  len != 32)
 		return EINVAL;
 	*sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
 	M_NOWAIT|M_ZERO);
-	if (*sched != NULL) {
-		rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
-		err = 0;
-	} else
-		err = ENOMEM;
-	return err;
+	if (*sched == 

CVS commit: src/sys/opencrypto

2014-11-27 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Nov 27 20:30:05 UTC 2014

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
Return ENOSPC instead of ENOMEM when there is no room in the buffer to
store results. ENOMEM in this subsystem means we cannot allocate more
requests or internal buffers for xforms.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.81 src/sys/opencrypto/cryptodev.c:1.82
--- src/sys/opencrypto/cryptodev.c:1.81	Fri Sep  5 05:23:40 2014
+++ src/sys/opencrypto/cryptodev.c	Thu Nov 27 15:30:05 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.81 2014/09/05 09:23:40 matt Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.82 2014/11/27 20:30:05 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.81 2014/09/05 09:23:40 matt Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.82 2014/11/27 20:30:05 christos Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -695,7 +695,7 @@ eagain:
 	/* let the user know how much data was returned */
 	if (crp-crp_olen) {
 		if (crp-crp_olen  (cop-dst_len ? cop-dst_len : cop-len)) {
-			error = ENOMEM;
+			error = ENOSPC;
 			goto bail;
 		}
 		dst_len = cop-dst_len = crp-crp_olen;



CVS commit: src/sys/opencrypto

2014-09-05 Thread Matt Thomas
Module Name:src
Committed By:   matt
Date:   Fri Sep  5 09:23:40 UTC 2014

Modified Files:
src/sys/opencrypto: cryptodev.c ocryptodev.c

Log Message:
Try not to use f_data, use f_fcrypt to get a correctly typed pointer.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/sys/opencrypto/cryptodev.c
cvs rdiff -u -r1.5 -r1.6 src/sys/opencrypto/ocryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.80 src/sys/opencrypto/cryptodev.c:1.81
--- src/sys/opencrypto/cryptodev.c:1.80	Mon Aug  4 14:17:18 2014
+++ src/sys/opencrypto/cryptodev.c	Fri Sep  5 09:23:40 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.80 2014/08/04 14:17:18 skrll Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.81 2014/09/05 09:23:40 matt Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.80 2014/08/04 14:17:18 skrll Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.81 2014/09/05 09:23:40 matt Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -220,7 +220,7 @@ cryptof_write(file_t *fp, off_t *poff,
 int
 cryptof_ioctl(struct file *fp, u_long cmd, void *data)
 {
-	struct fcrypt *fcr = fp-f_data;
+	struct fcrypt *fcr = fp-f_fcrypt;
 	struct csession *cse;
 	struct session_op *sop;
 	struct session_n_op *snop;
@@ -942,7 +942,7 @@ fail:
 static int
 cryptof_close(struct file *fp)
 {
-	struct fcrypt *fcr = fp-f_data;
+	struct fcrypt *fcr = fp-f_fcrypt;
 	struct csession *cse;
 
 	mutex_enter(crypto_mtx);
@@ -953,7 +953,7 @@ cryptof_close(struct file *fp)
 		mutex_enter(crypto_mtx);
 	}
 	seldestroy(fcr-sinfo);
-	fp-f_data = NULL;
+	fp-f_fcrypt = NULL;
 	crypto_refcount--;
 	mutex_exit(crypto_mtx);
 
@@ -2037,7 +2037,7 @@ fail:
 static int  
 cryptof_stat(struct file *fp, struct stat *st)
 {
-	struct fcrypt *fcr = fp-f_data;
+	struct fcrypt *fcr = fp-f_fcrypt;
 
 	(void)memset(st, 0, sizeof(*st));
 
@@ -2056,7 +2056,7 @@ cryptof_stat(struct file *fp, struct sta
 static int  
 cryptof_poll(struct file *fp, int events)
 {
-	struct fcrypt *fcr = (struct fcrypt *)fp-f_data;
+	struct fcrypt *fcr = fp-f_fcrypt;
 	int revents = 0;
 
 	if (!(events  (POLLIN | POLLRDNORM))) {

Index: src/sys/opencrypto/ocryptodev.c
diff -u src/sys/opencrypto/ocryptodev.c:1.5 src/sys/opencrypto/ocryptodev.c:1.6
--- src/sys/opencrypto/ocryptodev.c:1.5	Wed Jan  1 16:06:01 2014
+++ src/sys/opencrypto/ocryptodev.c	Fri Sep  5 09:23:40 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ocryptodev.c,v 1.5 2014/01/01 16:06:01 pgoyette Exp $ */
+/*	$NetBSD: ocryptodev.c,v 1.6 2014/09/05 09:23:40 matt Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -69,7 +69,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: ocryptodev.c,v 1.5 2014/01/01 16:06:01 pgoyette Exp $);
+__KERNEL_RCSID(0, $NetBSD: ocryptodev.c,v 1.6 2014/09/05 09:23:40 matt Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -109,7 +109,7 @@ static int	ocryptodev_msession(struct fc
 int
 ocryptof_ioctl(struct file *fp, u_long cmd, void *data)
 {
-	struct fcrypt *fcr = fp-f_data;
+	struct fcrypt *fcr = fp-f_fcrypt;
 	struct csession *cse;
 	struct osession_op *osop;
 	struct osession_n_op *osnop;



CVS commit: src/sys/opencrypto

2014-08-04 Thread Nick Hudson
Module Name:src
Committed By:   skrll
Date:   Mon Aug  4 14:17:19 UTC 2014

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
At least crypto_mtx needs initialisation here.  Spotted during PR/49065
investigation.


To generate a diff of this commit:
cvs rdiff -u -r1.79 -r1.80 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.79 src/sys/opencrypto/cryptodev.c:1.80
--- src/sys/opencrypto/cryptodev.c:1.79	Fri Jul 25 08:10:40 2014
+++ src/sys/opencrypto/cryptodev.c	Mon Aug  4 14:17:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.79 2014/07/25 08:10:40 dholland Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.80 2014/08/04 14:17:18 skrll Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.79 2014/07/25 08:10:40 dholland Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.80 2014/08/04 14:17:18 skrll Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -2085,6 +2085,8 @@ void	cryptoattach(int);
 void
 cryptoattach(int num)
 {
+	crypto_init();
+
 	pool_init(fcrpl, sizeof(struct fcrypt), 0, 0, 0, fcrpl,
 	NULL, IPL_NET);	/* XXX IPL_NET (splcrypto) */
 	pool_init(csepl, sizeof(struct csession), 0, 0, 0, csepl,



CVS commit: src/sys/opencrypto

2014-07-02 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jul  2 18:58:42 UTC 2014

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
If we register with pmf on attach, deregister on detach.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.45 src/sys/opencrypto/cryptosoft.c:1.46
--- src/sys/opencrypto/cryptosoft.c:1.45	Sat Jun 21 17:34:30 2014
+++ src/sys/opencrypto/cryptosoft.c	Wed Jul  2 18:58:42 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.45 2014/06/21 17:34:30 christos Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.46 2014/07/02 18:58:42 riastradh Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptosoft.c,v 1.45 2014/06/21 17:34:30 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptosoft.c,v 1.46 2014/07/02 18:58:42 riastradh Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -1343,6 +1343,7 @@ int	swcrypto_detach(device_t, int);
 int
 swcrypto_detach(device_t self, int flag)
 {
+	pmf_device_deregister(self);
 	if (swcr_id = 0)
 		crypto_unregister_all(swcr_id);
 	return 0;



CVS commit: src/sys/opencrypto

2014-06-21 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Jun 21 17:34:30 UTC 2014

Modified Files:
src/sys/opencrypto: cryptosoft.c

Log Message:
register with pmf.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/opencrypto/cryptosoft.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/opencrypto/cryptosoft.c
diff -u src/sys/opencrypto/cryptosoft.c:1.44 src/sys/opencrypto/cryptosoft.c:1.45
--- src/sys/opencrypto/cryptosoft.c:1.44	Wed Jan  1 11:06:01 2014
+++ src/sys/opencrypto/cryptosoft.c	Sat Jun 21 13:34:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptosoft.c,v 1.44 2014/01/01 16:06:01 pgoyette Exp $ */
+/*	$NetBSD: cryptosoft.c,v 1.45 2014/06/21 17:34:30 christos Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $	*/
 /*	$OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $	*/
 
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptosoft.c,v 1.44 2014/01/01 16:06:01 pgoyette Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptosoft.c,v 1.45 2014/06/21 17:34:30 christos Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -1333,6 +1333,9 @@ swcrypto_attach(device_t parent, device_
 {
 
 	swcr_init();
+
+	if (!pmf_device_register(self, NULL, NULL))
+		aprint_error_dev(self, couldn't establish power handler\n);
 }
 
 int	swcrypto_detach(device_t, int);



CVS commit: src/sys/opencrypto

2014-01-31 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Jan 31 18:11:32 UTC 2014

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
Replace home-grown config with standardized calls to
config_{init,fini}_component()


To generate a diff of this commit:
cvs rdiff -u -r1.75 -r1.76 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.75 src/sys/opencrypto/cryptodev.c:1.76
--- src/sys/opencrypto/cryptodev.c:1.75	Fri Jan 24 15:11:09 2014
+++ src/sys/opencrypto/cryptodev.c	Fri Jan 31 18:11:32 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.75 2014/01/24 15:11:09 pgoyette Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.76 2014/01/31 18:11:32 pgoyette Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.75 2014/01/24 15:11:09 pgoyette Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.76 2014/01/31 18:11:32 pgoyette Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -2138,20 +2138,7 @@ CFATTACH_DECL2_NEW(crypto, 0, crypto_mat
 NULL, NULL, NULL);
 
 #ifdef _MODULE
-static int cryptoloc[] = { -1, -1 };
-
-static struct cfdata crypto_cfdata[] = {
-	{
-		.cf_name = crypto,
-		.cf_atname = crypto,
-		.cf_unit = 0,
-		.cf_fstate = 0,
-		.cf_loc = cryptoloc,
-		.cf_flags = 0,
-		.cf_pspec = NULL,
-	},
-	{ NULL, NULL, 0, 0, NULL, 0, NULL }
-};
+#include ioconf.c
 #endif
 
 static int
@@ -2159,66 +2146,46 @@ crypto_modcmd(modcmd_t cmd, void *arg)
 {
 	int error = 0;
 #ifdef _MODULE
+	device_t dev;
 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
 #endif
 
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 #ifdef _MODULE
-
-		error = config_cfdriver_attach(crypto_cd);
-		if (error) {
-			return error;
-		}
-
-		error = config_cfattach_attach(crypto_cd.cd_name, crypto_ca);
-		if (error) {
-			config_cfdriver_detach(crypto_cd);
-			aprint_error(%s: unable to register cfattach\n,
-crypto_cd.cd_name);
-
-			return error;
-		}
-
-		error = config_cfdata_attach(crypto_cfdata, 1);
-		if (error) {
-			config_cfattach_detach(crypto_cd.cd_name, crypto_ca);
-			config_cfdriver_detach(crypto_cd);
-			aprint_error(%s: unable to register cfdata\n,
-crypto_cd.cd_name);
-
+		error = config_init_component(cfdriver_ioconf_crypto,
+	  cfattach_ioconf_crypto,
+	  cfdata_ioconf_crypto);
+		if (error)
 			return error;
-		}
 
 		error = devsw_attach(crypto_cd.cd_name, NULL, bmajor,
-		crypto_cdevsw, cmajor);
+ crypto_cdevsw, cmajor);
 		if (error) {
-			error = config_cfdata_detach(crypto_cfdata);
-			if (error) {
-return error;
-			}
-			config_cfattach_detach(crypto_cd.cd_name, crypto_ca);
-			config_cfdriver_detach(crypto_cd);
+			config_fini_component(cfdriver_ioconf_crypto,
+	  cfattach_ioconf_crypto,
+	  cfdata_ioconf_crypto);
 			aprint_error(%s: unable to register devsw\n,
 crypto_cd.cd_name);
 
 			return error;
 		}
 
-		(void)config_attach_pseudo(crypto_cfdata);
+		dev = config_attach_pseudo(cfdata_ioconf_crypto);
+		if (dev)
+			aprint_normal_dev(dev, attached\n);
 #endif
 
 		return error;
 	case MODULE_CMD_FINI:
 #ifdef _MODULE
-		error = config_cfdata_detach(crypto_cfdata);
+		error = devsw_detach(NULL, crypto_cdevsw);
 		if (error) {
 			return error;
 		}
-
-		config_cfattach_detach(crypto_cd.cd_name, crypto_ca);
-		config_cfdriver_detach(crypto_cd);
-		devsw_detach(NULL, crypto_cdevsw);
+		error = config_fini_component(cfdriver_ioconf_crypto,
+	  cfattach_ioconf_crypto,
+	  cfdata_ioconf_crypto);
 #endif
 
 		return error;



CVS commit: src/sys/opencrypto

2014-01-24 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Jan 24 15:11:09 UTC 2014

Modified Files:
src/sys/opencrypto: cryptodev.c

Log Message:
As requested by mrg@, since there is still a small window during which
the in-module ref-counting can fail, completely disable auto-unload.


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/sys/opencrypto/cryptodev.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/opencrypto/cryptodev.c
diff -u src/sys/opencrypto/cryptodev.c:1.74 src/sys/opencrypto/cryptodev.c:1.75
--- src/sys/opencrypto/cryptodev.c:1.74	Tue Jan 21 20:33:01 2014
+++ src/sys/opencrypto/cryptodev.c	Fri Jan 24 15:11:09 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: cryptodev.c,v 1.74 2014/01/21 20:33:01 pgoyette Exp $ */
+/*	$NetBSD: cryptodev.c,v 1.75 2014/01/24 15:11:09 pgoyette Exp $ */
 /*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.4.2.4 2003/06/03 00:09:02 sam Exp $	*/
 /*	$OpenBSD: cryptodev.c,v 1.53 2002/07/10 22:21:30 mickey Exp $	*/
 
@@ -64,7 +64,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.74 2014/01/21 20:33:01 pgoyette Exp $);
+__KERNEL_RCSID(0, $NetBSD: cryptodev.c,v 1.75 2014/01/24 15:11:09 pgoyette Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -2224,7 +2224,12 @@ crypto_modcmd(modcmd_t cmd, void *arg)
 		return error;
 #ifdef _MODULE
 	case MODULE_CMD_AUTOUNLOAD:
+#if 0	/*
+	 * XXX Completely disable auto-unload for now, since there is still
+	 * XXX a (small) window where in-module ref-counting doesn't help
+	 */
 		if (crypto_refcount != 0)
+#endif
 			return EBUSY;
 	/* FALLTHROUGH */
 #endif



  1   2   >