Skip to contentSkip to conversation

crypto: qat - clear fw_loader pointer on adf_ae_init() failure

PATCH RESEND In review Ahsan Atta 2 participants 2 messages Nov 13 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Ahsan Atta

author
#0 lore raw

PATCH RESEND 1/1 crypto: qat - clear fw_loader pointer on adf_ae_init() failure

adf_ae_init() stores the newly allocated loader data in accel_dev->fw_loader before calling qat_hal_init(), as the latter publishes the HAL handle through that pointer. If either qat_hal_init() or adf_ae_reset() fails, the error paths free the allocation but leave accel_dev->fw_loader pointing at freed memory, so the device is left holding a dangling pointer.

No code path dereferences that stale pointer today. This is therefore a robustness improvement with no observable failure behind it, hence no Fixes tag.

Clear accel_dev->fw_loader when the allocation is released, and fold the duplicated error handling into a cascading goto chain. Report the error returned by the callee rather than a generic -EFAULT; adf_dev_init() still maps any failure to -EFAULT, so this is a readability improvement and not a functional change.

---
 .../intel/qat/qat_common/adf_accel_engine.c   | 21 ++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/crypto/intel/qat/qat_common/adf_accel_engine.c +14 -7
index bb97bbedbb8a..2db74b055201 100644
@@ -181,6 +181,7 @@ int adf_ae_init(struct adf_accel_dev *accel_dev)
 {
 	struct adf_fw_loader_data *loader_data;
 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
+	int ret;
 
 	if (!hw_device->fw_name)
 		return 0;
@@ -190,18 +191,24 @@ int adf_ae_init(struct adf_accel_dev *accel_dev)
 		return -ENOMEM;
 
 	accel_dev->fw_loader = loader_data;
-	if (qat_hal_init(accel_dev)) {
+	ret = qat_hal_init(accel_dev);
+	if (ret) {
 		dev_err(&GET_DEV(accel_dev), "Failed to init the AEs\n");
-		kfree(loader_data);
-		return -EFAULT;
+		goto err_free_loader_data;
 	}
-	if (adf_ae_reset(accel_dev, 0)) {
+	ret = adf_ae_reset(accel_dev, 0);
+	if (ret) {
 		dev_err(&GET_DEV(accel_dev), "Failed to reset the AEs\n");
-		qat_hal_deinit(loader_data->fw_loader);
-		kfree(loader_data);
-		return -EFAULT;
+		goto err_hal_deinit;
 	}
 	return 0;
+
+err_hal_deinit:
+	qat_hal_deinit(loader_data->fw_loader);
+err_free_loader_data:
+	kfree(loader_data);
+	accel_dev->fw_loader = NULL;
+	return ret;
 }
 
 void adf_ae_shutdown(struct adf_accel_dev *accel_dev)

base-commit: 8ba9044ff0bf4a5789216496019fad614eeafd45

Signature · 1 line
2.50.1
maintainer
#1 lore raw
Quoted 17 lines · Ahsan Atta

adf_ae_init() stores the newly allocated loader data in accel_dev->fw_loader before calling qat_hal_init(), as the latter publishes the HAL handle through that pointer. If either qat_hal_init() or adf_ae_reset() fails, the error paths free the allocation but leave accel_dev->fw_loader pointing at freed memory, so the device is left holding a dangling pointer.

No code path dereferences that stale pointer today. This is therefore a robustness improvement with no observable failure behind it, hence no Fixes tag.

Clear accel_dev->fw_loader when the allocation is released, and fold the duplicated error handling into a cascading goto chain. Report the error returned by the callee rather than a generic -EFAULT; adf_dev_init() still maps any failure to -EFAULT, so this is a readability improvement and not a functional change.

Quoted 6 lines
---
 .../intel/qat/qat_common/adf_accel_engine.c   | 21 ++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

Patch applied. 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