Skip to contentSkip to conversation

s390/vfio-ap: Fix module unload memory leak of matrix_dev

PATCH Superseded Jason J. Herne 3 participants 5 messages Jun 18, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason J. Herne

author maintainer
#0 lore raw

PATCH 1/1 s390/vfio-ap: Fix module unload memory leak of matrix_dev

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with driver data that never gets set. So when release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and just free the global.

---
 drivers/s390/crypto/vfio_ap_drv.c | 2 --
 1 file changed, 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -2
index 7dc72cb718b0..6d3eea838e18 100644
@@ -82,8 +82,6 @@ static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
 
 static void vfio_ap_matrix_dev_release(struct device *dev)
 {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
 	kfree(matrix_dev);
 }
 
Signature · 1 line
2.21.1
#1 lore raw
Quoted 4 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with driver data that never gets set. So when release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and just free the global.

I would clarify this commit message to say that the drv_data of the matrix_dev is never set and so dev_get_drvdata() always returns NULL

And I would suggest to use

container_of(dev, struct ap_matrix_dev, dev)

instead of the global variable, and probably NULL the global too..

Jason

author maintainer
#2 lore raw
Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with driver data that never gets set. So when release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and just free the global.

I would clarify this commit message to say that the drv_data of the matrix_dev is never set and so dev_get_drvdata() always returns NULL

Quoted 7 lines

And I would suggest to use

container_of(dev, struct ap_matrix_dev, dev)

instead of the global variable, and probably NULL the global too..

The use of driver_data seems to have been completely erroneous here. In this case the global matrix_dev is the top level struct. It is not contained in anything. matrix_dev is created upon module load, and it is freed when the module exits.

So I don't think using container_of makes sense. Unless I've misunderstood your suggestion?

Signature · 1 line
-- Jason J. Herne ([email protected])
#3 lore raw
Quoted 18 lines · Jason J. Herne
Quoted 9 lines · Jason Gunthorpe
Quoted 4 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with driver data that never gets set. So when release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and just free the global.

I would clarify this commit message to say that the drv_data of the matrix_dev is never set and so dev_get_drvdata() always returns NULL

Quoted 7 lines

And I would suggest to use

container_of(dev, struct ap_matrix_dev, dev)

instead of the global variable, and probably NULL the global too..

Quoted 7 lines

The use of driver_data seems to have been completely erroneous here. In this case the global matrix_dev is the top level struct. It is not contained in anything. matrix_dev is created upon module load, and it is freed when the module exits.

So I don't think using container_of makes sense. Unless I've misunderstood your suggestion?

	matrix_dev = kzalloc(sizeof(*matrix_dev), GFP_KERNEL);
	matrix_dev->device.release = vfio_ap_matrix_dev_release;
	ret = device_register(&matrix_dev->device);

"dev" is contained inside matrix_dev which is why we should use container of to go from a struct device pointer back to the containing matrix_dev pointer

Jason

maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Jason J. Herne
Quoted 23 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with driver data that never gets set. So when release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and just free the global.

---
  drivers/s390/crypto/vfio_ap_drv.c | 2 --
  1 file changed, 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -2
index 7dc72cb718b0..6d3eea838e18 100644
@@ -82,8 +82,6 @@ static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
 
  static void vfio_ap_matrix_dev_release(struct device *dev)
  {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
  	kfree(matrix_dev);
  }