[PATCH v2 1/7] crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()

From: T Pratham

Date: Thu Aug 27 2026 - 09:37:39 EST


From: Mert Seftali <mertsftl@xxxxxxxxx>

dthe_get_dev() fetches a device from the global device list with
list_first_entry() and then checks the result for NULL. However,
list_first_entry() never returns NULL: on an empty list it returns a
bogus pointer computed from the list head. The NULL check is therefore
dead code, and an empty list would be treated as a valid entry and
moved around as if it were a real device.

Use list_first_entry_or_null() so the existing NULL check works as
intended and an empty list is handled gracefully.

[pratham:]
Add null checks on dev_data in callers of dthe_get_dev().

Fixes: 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <error27@xxxxxxxxx>
Closes: https://lore.kernel.org/r/202606111933.69GGTKxr-lkp@xxxxxxxxx/
Signed-off-by: Mert Seftali <mertsftl@xxxxxxxxx>
Co-developed-by: T Pratham <t-pratham@xxxxxx>
Signed-off-by: T Pratham <t-pratham@xxxxxx>
---
drivers/crypto/ti/dthev2-aes.c | 12 ++++++++++--
drivers/crypto/ti/dthev2-common.c | 2 +-
2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index eb5cd902dfb59..4fdd24ee91637 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -112,6 +112,9 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
struct dthe_data *dev_data = dthe_get_dev(ctx);

+ if (!dev_data)
+ return -ENODEV;
+
ctx->dev_data = dev_data;
ctx->keylen = 0;

@@ -124,6 +127,9 @@ static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm)
struct dthe_data *dev_data = dthe_get_dev(ctx);
const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm));

+ if (!dev_data)
+ return -ENODEV;
+
ctx->dev_data = dev_data;
ctx->keylen = 0;

@@ -571,10 +577,12 @@ static int dthe_aead_init_tfm(struct crypto_aead *tfm)
{
struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
struct dthe_data *dev_data = dthe_get_dev(ctx);
+ const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm));

- ctx->dev_data = dev_data;
+ if (!dev_data)
+ return -ENODEV;

- const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm));
+ ctx->dev_data = dev_data;

ctx->aead_fb = crypto_alloc_sync_aead(alg_name, 0,
CRYPTO_ALG_NEED_FALLBACK);
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index a2ad79bec105a..cc02449382673 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -40,7 +40,7 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)
return ctx->dev_data;

spin_lock_bh(&dthe_dev_list.lock);
- dev_data = list_first_entry(&dthe_dev_list.dev_list, struct dthe_data, list);
+ dev_data = list_first_entry_or_null(&dthe_dev_list.dev_list, struct dthe_data, list);
if (dev_data)
list_move_tail(&dev_data->list, &dthe_dev_list.dev_list);
spin_unlock_bh(&dthe_dev_list.lock);
--
2.34.1