Skip to contentSkip to conversation

crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()

PATCH In review Mert Seftali 3 participants 5 messages Jun 13 – Jul 29

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

PATCH 1/1 crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()

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.

Fixes 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
---
 drivers/crypto/ti/dthev2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ti/dthev2-common.c +1 -1
index a2ad79bec105..cc0244938267 100644
@@ -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);
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Mert Seftali

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.

Fixes 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
---
 drivers/crypto/ti/dthev2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ti/dthev2-common.c +0 -0
index a2ad79bec105..cc0244938267 100644

drivers/crypto/ti/dthev2-common.c struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx) · line 46

Quoted 8 lines
drivers/crypto/ti/dthev2-common.c +1 -1
@@ -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);

LGTM.

Signature · 2 lines
Regards
T Pratham <[email protected]>
maintainer
#2 lore raw
In reply to Mert Seftali
Quoted 10 lines · Mert Seftali

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.

Quoted 8 lines
Fixes 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
---
 drivers/crypto/ti/dthev2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This only fixes the symptom of the problem. But the root goes deeper.

The main issue is that the device can go away in the middle of an operation. The driver needs to be handle it gracefully, and certainly not by crashing the system because the associated memory has been freed.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 4 lines · Herbert Xu

This only fixes the symptom of the problem. But the root goes deeper.

The main issue is that the device can go away in the middle of an operation. [...]

Yeah agreed. my patch only covers the empty-list case the test robot flagged but it doesn't touch the lifetime problem at all.

Doing that one properly is a much bigger change than a one-liner though, and i'd rather take the time to understand the driver's lifecycle and do it right than send you a half-baked fix. So, is the list_first_entry_or_null() change still worth taking as a small correctness fix on its own or would you rather drop it and sort the lifetime issue as a whole?

Thanks,
Mert

maintainer
#4 lore raw
In reply to Herbert Xu
Quoted 23 lines · Herbert Xu
Quoted 10 lines · Mert Seftali

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.

Quoted 8 lines
Fixes 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
---
 drivers/crypto/ti/dthev2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This only fixes the symptom of the problem. But the root goes deeper.

Quoted 6 lines

The main issue is that the device can go away in the middle of an operation. The driver needs to be handle it gracefully, and certainly not by crashing the system because the associated memory has been freed.

Thanks,

This is not possible here as the device is part of the SoC. That being said, I'll implement this handling to have a more correct driver. In the meanwhile, the above empty list fix can be applied.

Signature · 2 lines
Regards
T Pratham <[email protected]>