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

This commit is contained in:
knakahara 2017-05-25 05:24:57 +00:00
parent cce0fc1eef
commit ecc8a11166
3 changed files with 71 additions and 18 deletions

View File

@ -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 <sys/cdefs.h>
__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 <sys/param.h>
#include <sys/reboot.h>
@ -1076,7 +1076,7 @@ crypto_kinvoke(struct cryptkop *krp, int hint)
/* Sanity checks. */
if (krp->krp_callback == NULL) {
cv_destroy(&krp->krp_cv);
pool_put(&cryptkop_pool, krp);
crypto_kfreereq(krp);
return EINVAL;
}
@ -1261,6 +1261,54 @@ crypto_getreq(int num)
return crp;
}
/*
* 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(&cryptkop_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(&cryptkop_pool, prflags);
if (krp == NULL) {
return NULL;
}
memset(krp, 0, sizeof(struct cryptkop));
return krp;
}
/*
* Invoke the callback on behalf of the driver.
*/

View File

@ -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 <sys/cdefs.h>
__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 <sys/param.h>
#include <sys/systm.h>
@ -855,7 +855,11 @@ cryptodev_key(struct crypt_kop *kop)
return EINVAL;
}
krp = pool_get(&cryptkop_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->krp_cv, "crykdev");
krp->krp_op = kop->crk_op;
@ -923,7 +927,7 @@ fail:
}
}
cv_destroy(&krp->krp_cv);
pool_put(&cryptkop_pool, krp);
crypto_kfreereq(krp);
DPRINTF("error=0x%08x\n", error);
return error;
}
@ -1435,7 +1439,11 @@ cryptodev_mkey(struct fcrypt *fcr, struct crypt_n_kop *kop, int count)
continue;
}
krp = pool_get(&cryptkop_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->krp_cv, "crykdev");
krp->krp_op = kop[req].crk_op;
@ -1493,7 +1501,7 @@ fail:
}
}
cv_destroy(&krp->krp_cv);
pool_put(&cryptkop_pool, krp);
crypto_kfreereq(krp);
}
}
error = 0;
@ -1912,7 +1920,7 @@ fail:
}
}
cv_destroy(&krp->krp_cv);
pool_put(&cryptkop_pool, krp);
crypto_kfreereq(krp);
req++;
}
}
@ -2012,7 +2020,7 @@ fail:
}
}
cv_destroy(&krp->krp_cv);
pool_put(&cryptkop_pool, krp);
crypto_kfreereq(krp);
return 0;
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: cryptodev.h,v 1.33 2017/05/25 05:22:55 knakahara Exp $ */
/* $NetBSD: cryptodev.h,v 1.34 2017/05/25 05:24:57 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 $ */
@ -606,16 +606,13 @@ int cuio_apply(struct uio *, int, int,
extern void crypto_freereq(struct cryptop *crp);
extern struct cryptop *crypto_getreq(int num);
extern void crypto_kfreereq(struct cryptkop *);
extern struct cryptkop *crypto_kgetreq(int, int);
extern int crypto_usercrypto; /* userland may do crypto requests */
extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
extern int crypto_devallowsoft; /* only use hardware crypto */
/*
* Asymmetric operations are allocated in cryptodev.c but can be
* freed in crypto.c.
*/
extern struct pool cryptkop_pool;
/*
* initialize the crypto framework subsystem (not the pseudo-device).
* This must be called very early in boot, so the framework is ready