git: fd46838fdd1b - stable/13 - cryptodev: Use a private malloc type (M_CRYPTODEV) instead of M_XDATA.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 11 May 2022 00:03:36 UTC
The branch stable/13 has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=fd46838fdd1b58ae5b4db281a555e21569834932

commit fd46838fdd1b58ae5b4db281a555e21569834932
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-01-24 23:27:39 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-05-10 23:46:49 +0000

    cryptodev: Use a private malloc type (M_CRYPTODEV) instead of M_XDATA.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D33991
    
    (cherry picked from commit 8f3f3fdf73a98e819d5f73c1da3286bd608e8208)
---
 sys/opencrypto/cryptodev.c | 50 +++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/sys/opencrypto/cryptodev.c b/sys/opencrypto/cryptodev.c
index 7e4cecff3980..1c6afdee0913 100644
--- a/sys/opencrypto/cryptodev.c
+++ b/sys/opencrypto/cryptodev.c
@@ -367,6 +367,8 @@ SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
     &warninterval,
     "Delay in seconds between warnings of deprecated /dev/crypto algorithms");
 
+static MALLOC_DEFINE(M_CRYPTODEV, "cryptodev", "/dev/crypto data buffers");
+
 /*
  * Check a crypto identifier to see if it requested
  * a software device/driver.  This can be done either
@@ -627,7 +629,7 @@ cse_create(struct fcrypt *fcr, struct session2_op *sop)
 			goto bail;
 		}
 
-		key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
+		key = malloc(csp.csp_cipher_klen, M_CRYPTODEV, M_WAITOK);
 		error = copyin(sop->key, key, csp.csp_cipher_klen);
 		if (error) {
 			CRYPTDEB("invalid key");
@@ -649,7 +651,8 @@ cse_create(struct fcrypt *fcr, struct session2_op *sop)
 		}
 
 		if (csp.csp_auth_klen != 0) {
-			mackey = malloc(csp.csp_auth_klen, M_XDATA, M_WAITOK);
+			mackey = malloc(csp.csp_auth_klen, M_CRYPTODEV,
+			    M_WAITOK);
 			error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
 			if (error) {
 				CRYPTDEB("invalid mac key");
@@ -699,7 +702,7 @@ cse_create(struct fcrypt *fcr, struct session2_op *sop)
 		goto bail;
 	}
 
-	cse = malloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
+	cse = malloc(sizeof(struct csession), M_CRYPTODEV, M_WAITOK | M_ZERO);
 	mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
 	refcount_init(&cse->refs, 1);
 	cse->key = key;
@@ -729,8 +732,8 @@ cse_create(struct fcrypt *fcr, struct session2_op *sop)
 	sop->crid = crypto_ses2hid(cse->cses);
 bail:
 	if (error) {
-		free(key, M_XDATA);
-		free(mackey, M_XDATA);
+		free(key, M_CRYPTODEV);
+		free(mackey, M_CRYPTODEV);
 	}
 	return (error);
 }
@@ -761,10 +764,10 @@ cse_free(struct csession *cse)
 	crypto_freesession(cse->cses);
 	mtx_destroy(&cse->lock);
 	if (cse->key)
-		free(cse->key, M_XDATA);
+		free(cse->key, M_CRYPTODEV);
 	if (cse->mackey)
-		free(cse->mackey, M_XDATA);
-	free(cse, M_XDATA);
+		free(cse->mackey, M_CRYPTODEV);
+	free(cse, M_CRYPTODEV);
 }
 
 static bool
@@ -790,17 +793,18 @@ cod_alloc(struct csession *cse, size_t aad_len, size_t len)
 {
 	struct cryptop_data *cod;
 
-	cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
+	cod = malloc(sizeof(struct cryptop_data), M_CRYPTODEV, M_WAITOK |
+	    M_ZERO);
 
 	cod->cse = cse;
 	if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
 		if (aad_len != 0)
-			cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
-		cod->buf = malloc(len, M_XDATA, M_WAITOK);
+			cod->aad = malloc(aad_len, M_CRYPTODEV, M_WAITOK);
+		cod->buf = malloc(len, M_CRYPTODEV, M_WAITOK);
 	} else
-		cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
+		cod->buf = malloc(aad_len + len, M_CRYPTODEV, M_WAITOK);
 	if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
-		cod->obuf = malloc(len, M_XDATA, M_WAITOK);
+		cod->obuf = malloc(len, M_CRYPTODEV, M_WAITOK);
 	return (cod);
 }
 
@@ -808,10 +812,10 @@ static void
 cod_free(struct cryptop_data *cod)
 {
 
-	free(cod->aad, M_XDATA);
-	free(cod->obuf, M_XDATA);
-	free(cod->buf, M_XDATA);
-	free(cod, M_XDATA);
+	free(cod->aad, M_CRYPTODEV);
+	free(cod->obuf, M_CRYPTODEV);
+	free(cod->buf, M_CRYPTODEV);
+	free(cod, M_CRYPTODEV);
 }
 
 static int
@@ -1313,7 +1317,7 @@ cryptodev_key(struct crypt_kop *kop)
 		return (EINVAL);
 	}
 
-	krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
+	krp = malloc(sizeof(*krp), M_CRYPTODEV, M_WAITOK | M_ZERO);
 	krp->krp_op = kop->crk_op;
 	krp->krp_status = kop->crk_status;
 	krp->krp_iparams = kop->crk_iparams;
@@ -1334,7 +1338,7 @@ cryptodev_key(struct crypt_kop *kop)
 		size = (krp->krp_param[i].crp_nbits + 7) / 8;
 		if (size == 0)
 			continue;
-		krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
+		krp->krp_param[i].crp_p = malloc(size, M_CRYPTODEV, M_WAITOK);
 		if (i >= krp->krp_iparams)
 			continue;
 		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
@@ -1379,9 +1383,9 @@ fail:
 		kop->crk_status = krp->krp_status;
 		for (i = 0; i < CRK_MAXPARAM; i++) {
 			if (krp->krp_param[i].crp_p)
-				free(krp->krp_param[i].crp_p, M_XDATA);
+				free(krp->krp_param[i].crp_p, M_CRYPTODEV);
 		}
-		free(krp, M_XDATA);
+		free(krp, M_CRYPTODEV);
 	}
 	return (error);
 }
@@ -1421,7 +1425,7 @@ fcrypt_dtor(void *data)
 		cse_free(cse);
 	}
 	mtx_destroy(&fcr->lock);
-	free(fcr, M_XDATA);
+	free(fcr, M_CRYPTODEV);
 }
 
 static int
@@ -1430,7 +1434,7 @@ crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
 	struct fcrypt *fcr;
 	int error;
 
-	fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
+	fcr = malloc(sizeof(struct fcrypt), M_CRYPTODEV, M_WAITOK | M_ZERO);
 	TAILQ_INIT(&fcr->csessions);
 	mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
 	error = devfs_set_cdevpriv(fcr, fcrypt_dtor);