[PATCH] nvme-auth: use kzalloc_flex() in nvme_auth_alloc_key()

From: Thorsten Blum

Date: Thu Mar 05 2026 - 06:38:50 EST


Use kzalloc_flex() in nvme_auth_alloc_key() and remove the helper
nvme_auth_key_struct_size(). In nvme_auth_transform_key(), use
struct_size() directly.

Signed-off-by: Thorsten Blum <thorsten.blum@xxxxxxxxx>
---
drivers/nvme/common/auth.c | 14 +++-----------
include/linux/nvme-auth.h | 1 -
2 files changed, 3 insertions(+), 12 deletions(-)

diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index e07e7d4bf8b6..20aac5e29ca4 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -153,14 +153,6 @@ size_t nvme_auth_hmac_hash_len(u8 hmac_id)
}
EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);

-u32 nvme_auth_key_struct_size(u32 key_len)
-{
- struct nvme_dhchap_key key;
-
- return struct_size(&key, key, key_len);
-}
-EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size);
-
struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
u8 key_hash)
{
@@ -218,9 +210,9 @@ EXPORT_SYMBOL_GPL(nvme_auth_extract_key);

struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash)
{
- u32 num_bytes = nvme_auth_key_struct_size(len);
- struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL);
+ struct nvme_dhchap_key *key;

+ key = kzalloc_flex(*key, key, len);
if (key) {
key->len = len;
key->hash = hash;
@@ -251,7 +243,7 @@ struct nvme_dhchap_key *nvme_auth_transform_key(
return ERR_PTR(-ENOKEY);
}
if (key->hash == 0) {
- key_len = nvme_auth_key_struct_size(key->len);
+ key_len = struct_size(key, key, key->len);
transformed_key = kmemdup(key, key_len, GFP_KERNEL);
if (!transformed_key)
return ERR_PTR(-ENOMEM);
diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
index 60e069a6757f..f508cce5ed83 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -24,7 +24,6 @@ const char *nvme_auth_digest_name(u8 hmac_id);
size_t nvme_auth_hmac_hash_len(u8 hmac_id);
u8 nvme_auth_hmac_id(const char *hmac_name);

-u32 nvme_auth_key_struct_size(u32 key_len);
struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
u8 key_hash);
void nvme_auth_free_key(struct nvme_dhchap_key *key);

base-commit: c107785c7e8dbabd1c18301a1c362544b5786282
--
Thorsten Blum <thorsten.blum@xxxxxxxxx>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4