Skip to contentSkip to conversation

dma: idxd: cdev: Fix uninitialized use of sva in idxd_cdev_open

PATCH Applied Purva Yeshi 4 participants 7 messages Apr 10, 2025 – Apr 18, 2025

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

PATCH 1/1 dma: idxd: cdev: Fix uninitialized use of sva in idxd_cdev_open

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Initialize sva to NULL at declaration and add a check using IS_ERR_OR_NULL() before unbinding the device. This ensures the function does not use an invalid or uninitialized pointer during cleanup.

---
 drivers/dma/idxd/cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +2 -2
index ff94ee892339..7bd031a60894 100644
@@ -222,7 +222,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	struct idxd_wq *wq;
 	struct device *dev, *fdev;
 	int rc = 0;
-	struct iommu_sva *sva;
+	struct iommu_sva *sva = NULL;
 	unsigned int pasid;
 	struct idxd_cdev *idxd_cdev;
 
@@ -317,7 +317,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	if (device_user_pasid_enabled(idxd))
 		idxd_xa_pasid_remove(ctx);
 failed_get_pasid:
-	if (device_user_pasid_enabled(idxd))
+	if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva))
 		iommu_sva_unbind_device(sva);
 failed:
 	mutex_unlock(&wq->wq_lock);
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 35 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Initialize sva to NULL at declaration and add a check using IS_ERR_OR_NULL() before unbinding the device. This ensures the function does not use an invalid or uninitialized pointer during cleanup.

---
 drivers/dma/idxd/cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +1 -1
index ff94ee892339..7bd031a60894 100644
@@ -222,7 +222,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	struct idxd_wq *wq;
 	struct device *dev, *fdev;
 	int rc = 0;
-	struct iommu_sva *sva;
+	struct iommu_sva *sva = NULL;
 	unsigned int pasid;
 	struct idxd_cdev *idxd_cdev;
 

drivers/dma/idxd/cdev.c static int idxd_cdev_open(struct inode *inode, struct file *filp) · line 320

Quoted 5 lines
drivers/dma/idxd/cdev.c +1 -1
@@ -317,7 +317,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	if (device_user_pasid_enabled(idxd))
 		idxd_xa_pasid_remove(ctx);
 failed_get_pasid:
-	if (device_user_pasid_enabled(idxd))
+	if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva))

Optional: I would change this to only checking for the validity of 'sva', the other condition would be true if 'sva' is valid.

But for consistency with the condition above, I am not opposed to the way this patch is written:

Cheers,

Signature · 1 line
Vinicius
#2 lore raw
Quoted 46 lines · Vinicius Costa Gomes
Quoted 35 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Initialize sva to NULL at declaration and add a check using IS_ERR_OR_NULL() before unbinding the device. This ensures the function does not use an invalid or uninitialized pointer during cleanup.

---
  drivers/dma/idxd/cdev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +1 -1
index ff94ee892339..7bd031a60894 100644
@@ -222,7 +222,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
  	struct idxd_wq *wq;
  	struct device *dev, *fdev;
  	int rc = 0;
-	struct iommu_sva *sva;
+	struct iommu_sva *sva = NULL;
  	unsigned int pasid;
  	struct idxd_cdev *idxd_cdev;
 

drivers/dma/idxd/cdev.c static int idxd_cdev_open(struct inode *inode, struct file *filp) · line 320

Quoted 5 lines
drivers/dma/idxd/cdev.c +1 -1
@@ -317,7 +317,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
  	if (device_user_pasid_enabled(idxd))
  		idxd_xa_pasid_remove(ctx);
  failed_get_pasid:
-	if (device_user_pasid_enabled(idxd))
+	if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva))

Optional: I would change this to only checking for the validity of 'sva', the other condition would be true if 'sva' is valid.

Quoted 7 lines

But for consistency with the condition above, I am not opposed to the way this patch is written:

Cheers,

Hi Vinicius,

Thank you for the review and the Acked-by tag. I appreciate your feedback on the conditional check.

Best regards,
Purva

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Purva Yeshi
Quoted 11 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Quoted 6 lines

Initialize sva to NULL at declaration and add a check using IS_ERR_OR_NULL() before unbinding the device. This ensures the function does not use an invalid or uninitialized pointer during cleanup.

Quoted 26 lines
---
 drivers/dma/idxd/cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +2 -2
index ff94ee892339..7bd031a60894 100644
@@ -222,7 +222,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	struct idxd_wq *wq;
 	struct device *dev, *fdev;
 	int rc = 0;
-	struct iommu_sva *sva;
+	struct iommu_sva *sva = NULL;
 	unsigned int pasid;
 	struct idxd_cdev *idxd_cdev;
 
@@ -317,7 +317,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	if (device_user_pasid_enabled(idxd))
 		idxd_xa_pasid_remove(ctx);
 failed_get_pasid:
-	if (device_user_pasid_enabled(idxd))
+	if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva))
 		iommu_sva_unbind_device(sva);
 failed:
 	mutex_unlock(&wq->wq_lock);
#4 lore raw
Quoted 14 lines · Dave Jiang
Quoted 11 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Quoted 8 lines
Quoted 6 lines

Initialize sva to NULL at declaration and add a check using IS_ERR_OR_NULL() before unbinding the device. This ensures the function does not use an invalid or uninitialized pointer during cleanup.

Hi Dave,

Thank you for the review and the Reviewed-by tag.

Quoted 17 lines
Quoted 17 lines
---
  drivers/dma/idxd/cdev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma/idxd/cdev.c +1 -1
index ff94ee892339..7bd031a60894 100644
@@ -222,7 +222,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
  	struct idxd_wq *wq;
  	struct device *dev, *fdev;
  	int rc = 0;
-	struct iommu_sva *sva;
+	struct iommu_sva *sva = NULL;
  	unsigned int pasid;
  	struct idxd_cdev *idxd_cdev;
 
Quoted 10 lines

drivers/dma/idxd/cdev.c static int idxd_cdev_open(struct inode *inode, struct file *filp) · line 323

Quoted 9 lines
drivers/dma/idxd/cdev.c +1 -1
@@ -317,7 +317,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
  	if (device_user_pasid_enabled(idxd))
  		idxd_xa_pasid_remove(ctx);
  failed_get_pasid:
-	if (device_user_pasid_enabled(idxd))
+	if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva))
  		iommu_sva_unbind_device(sva);
  failed:
  	mutex_unlock(&wq->wq_lock);

Best regards,
Purva

maintainer Applied
#5 lore raw
In reply to Purva Yeshi
Quoted 4 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

Quoted 8 lines

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

[...]

Applied, thanks!

[1/1] dma: idxd: cdev: Fix uninitialized use of sva in idxd_cdev_open

      commit: 97994333de2b8062d2df4e6ce0dc65c2dc0f40dc

Best regards,

Signature · 1 line
~Vinod
#6 lore raw
Quoted 13 lines · Vinod Koul
Quoted 11 lines · Purva Yeshi

Fix Smatch-detected issue:
drivers/dma/idxd/cdev.c:321 idxd_cdev_open() error:
uninitialized symbol 'sva'.

'sva' pointer may be used uninitialized in error handling paths. Specifically, if PASID support is enabled and iommu_sva_bind_device() returns an error, the code jumps to the cleanup label and attempts to call iommu_sva_unbind_device(sva) without ensuring that sva was successfully assigned. This triggers a Smatch warning about an uninitialized symbol.

Quoted 8 lines
Quoted 1 line

[...]

Applied, thanks!

[1/1] dma: idxd: cdev: Fix uninitialized use of sva in idxd_cdev_open

       commit: 97994333de2b8062d2df4e6ce0dc65c2dc0f40dc

Best regards,

Hi Vinod,

Thank you for applying the patch!

Best regards,
Purva