Skip to contentSkip to conversation

nvme-auth: use SHASH_DESC_ON_STACK

PATCH Applied Hannes Reinecke 3 participants 6 messages May 7, 2025 – May 9, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover nvme-auth: use SHASH_DESC_ON_STACK

From: Hannes Reinecke <[email protected]>

Hi all,

we should be using SHASH_DESC_ON_STACK in the nvme authentication code to save some memory allocations.

Hannes Reinecke (2):
nvme-auth: use SHASH_DESC_ON_STACK
nvmet-auth: use SHASH_DESC_ON_STACK

 drivers/nvme/common/auth.c | 15 ++-------------
 drivers/nvme/target/auth.c | 12 ++----------
 2 files changed, 4 insertions(+), 23 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 nvme-auth: use SHASH_DESC_ON_STACK

Use SHASH_DESC_ON_STACK to avoid explicit allocation.

---
 drivers/nvme/common/auth.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/nvme/common/auth.c +2 -13
index 2c092ec8c0a9..3b6d759bcdf2 100644
@@ -242,7 +242,7 @@ struct nvme_dhchap_key *nvme_auth_transform_key(
 {
 	const char *hmac_name;
 	struct crypto_shash *key_tfm;
-	struct shash_desc *shash;
+	SHASH_DESC_ON_STACK(shash, key_tfm);
 	struct nvme_dhchap_key *transformed_key;
 	int ret, key_len;
 
@@ -267,19 +267,11 @@ struct nvme_dhchap_key *nvme_auth_transform_key(
 	if (IS_ERR(key_tfm))
 		return ERR_CAST(key_tfm);
 
-	shash = kmalloc(sizeof(struct shash_desc) +
-			crypto_shash_descsize(key_tfm),
-			GFP_KERNEL);
-	if (!shash) {
-		ret = -ENOMEM;
-		goto out_free_key;
-	}
-
 	key_len = crypto_shash_digestsize(key_tfm);
 	transformed_key = nvme_auth_alloc_key(key_len, key->hash);
 	if (!transformed_key) {
 		ret = -ENOMEM;
-		goto out_free_shash;
+		goto out_free_key;
 	}
 
 	shash->tfm = key_tfm;
@@ -299,15 +291,12 @@ struct nvme_dhchap_key *nvme_auth_transform_key(
 	if (ret < 0)
 		goto out_free_transformed_key;
 
-	kfree(shash);
 	crypto_free_shash(key_tfm);
 
 	return transformed_key;
 
 out_free_transformed_key:
 	nvme_auth_free_key(transformed_key);
-out_free_shash:
-	kfree(shash);
 out_free_key:
 	crypto_free_shash(key_tfm);
 
Signature · 1 line
2.35.3
#3 lore raw

PATCH 2/2 nvmet-auth: use SHASH_DESC_ON_STACK

In reply to Hannes Reinecke

Use SHASH_DESC_ON_STACK to avoid explicit allocation.

---
 drivers/nvme/target/auth.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/nvme/target/auth.c +2 -10
index cef8d77f477b..5dc9f3097c4c 100644
@@ -290,7 +290,7 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
 			 unsigned int shash_len)
 {
 	struct crypto_shash *shash_tfm;
-	struct shash_desc *shash;
+	SHASH_DESC_ON_STACK(shash, shash_tfm);
 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
 	const char *hash_name;
 	u8 *challenge = req->sq->dhchap_c1;
@@ -342,19 +342,13 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
 						    req->sq->dhchap_c1,
 						    challenge, shash_len);
 		if (ret)
-			goto out_free_challenge;
+			goto out;
 	}
 
 	pr_debug("ctrl %d qid %d host response seq %u transaction %d\n",
 		 ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1,
 		 req->sq->dhchap_tid);
 
-	shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(shash_tfm),
-			GFP_KERNEL);
-	if (!shash) {
-		ret = -ENOMEM;
-		goto out_free_challenge;
-	}
 	shash->tfm = shash_tfm;
 	ret = crypto_shash_init(shash);
 	if (ret)
@@ -389,8 +383,6 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
 		goto out;
 	ret = crypto_shash_final(shash, response);
 out:
-	kfree(shash);
-out_free_challenge:
 	if (challenge != req->sq->dhchap_c1)
 		kfree(challenge);
 out_free_response:
Signature · 1 line
2.35.3