Skip to contentSkip to conversation

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

PATCH In review Jason J. Herne 4 participants 5 messages Jun 18, 2021 – Jun 21, 2021

Subsystems (3)
lore
Share & tools
lore

Jason J. Herne

author maintainer
#0 lore raw

PATCH v2 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 a NULL pointer. Driver data for the matrix device is never set and so dev_get_drvdata() always returns NULL. When release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and get the correct pointer from the device using container_of. We'll also NULL the global to prevent any future use.

---
 drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +2 -3
index 7dc72cb718b0..40e66cb363d1 100644
@@ -82,9 +82,8 @@ 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);
+	kfree(container_of(dev, struct ap_matrix_dev, device));
+	matrix_dev = NULL;
 }
 
 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
Signature · 1 line
2.21.1
maintainer
#1 lore raw
Quoted 17 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with a NULL pointer. Driver data for the matrix device is never set and so dev_get_drvdata() always returns NULL. When release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and get the correct pointer from the device using container_of. We'll also NULL the global to prevent any future use.

---
  drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -0
index 7dc72cb718b0..40e66cb363d1 100644

drivers/s390/crypto/vfio_ap_drv.c static void vfio_ap_queue_dev_remove(struct ap_device *apdev) · line 85

Quoted 7 lines
drivers/s390/crypto/vfio_ap_drv.c +1 -3
@@ -82,9 +82,8 @@ 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);
+	kfree(container_of(dev, struct ap_matrix_dev, device));

I suppose if we're not going to assume that the release is being called to free the global matrix_dev, then if you are going to retrieve it using container_of(), then maybe we should verify the retrieved pointer is the same as the global matrix_dev?

Quoted 4 lines
drivers/s390/crypto/vfio_ap_drv.c +1 -0
+	matrix_dev = NULL;
  }
 
  static int matrix_bus_match(struct device *dev, struct device_driver *drv)
Reviewed-by
#2 lore raw
Quoted 14 lines · Tony Krowiak
Quoted 11 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with a NULL pointer. Driver data for the matrix device is never set and so dev_get_drvdata() always returns NULL. When release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and get the correct pointer from the device using container_of. We'll also NULL the global to prevent any future use.

  drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
Quoted 15 lines

drivers/s390/crypto/vfio_ap_drv.c static void vfio_ap_queue_dev_remove(struct ap_device *apdev) · line 84

Quoted 10 lines
drivers/s390/crypto/vfio_ap_drv.c +2 -3
index 7dc72cb718b0..40e66cb363d1 100644
+++ b/drivers/s390/crypto/vfio_ap_drv.c
@@ -82,9 +82,8 @@ 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);
+	kfree(container_of(dev, struct ap_matrix_dev, device));

I suppose if we're not going to assume that the release is being called to free the global matrix_dev, then if you are going to retrieve it using container_of(), then maybe we should verify the retrieved pointer is the same as the global matrix_dev?

That seems like overkill to me

Jason

maintainer Reviewed-by
#3 lore raw
Quoted 28 lines · Jason Gunthorpe
Quoted 13 lines · Tony Krowiak
Quoted 11 lines · Jason J. Herne

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with a NULL pointer. Driver data for the matrix device is never set and so dev_get_drvdata() always returns NULL. When release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and get the correct pointer from the device using container_of. We'll also NULL the global to prevent any future use.

   drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
   1 file changed, 2 insertions(+), 3 deletions(-)
Quoted 14 lines

drivers/s390/crypto/vfio_ap_drv.c static void vfio_ap_queue_dev_remove(struct ap_device *apdev) · line 84

Quoted 10 lines
drivers/s390/crypto/vfio_ap_drv.c +2 -3
index 7dc72cb718b0..40e66cb363d1 100644
+++ b/drivers/s390/crypto/vfio_ap_drv.c
@@ -82,9 +82,8 @@ 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);
+	kfree(container_of(dev, struct ap_matrix_dev, device));

I suppose if we're not going to assume that the release is being called to free the global matrix_dev, then if you are going to retrieve it using container_of(), then maybe we should verify the retrieved pointer is the same as the global matrix_dev?

Quoted 1 line

That seems like overkill to me

After thinking about it, it's probably more than overkill as I assume the container_of() function would fail if dev was not contained in matrix_mdev:

Quoted 4 lines

Jason

maintainer Reviewed-by on v2 1/1
#4 lore raw
In reply to Jason J. Herne
Quoted 17 lines · On Fri, 18 Jun 2021 13:12:55 -0400 "Jason J. Herne" <[email protected]>…

vfio_ap_matrix_dev_release is shadowing the global matrix_dev with a NULL pointer. Driver data for the matrix device is never set and so dev_get_drvdata() always returns NULL. When release is called we end up not freeing matrix_dev. The fix is to remove the shadow variable and get the correct pointer from the device using container_of. We'll also NULL the global to prevent any future use.

---
 drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -0
index 7dc72cb718b0..40e66cb363d1 100644

drivers/s390/crypto/vfio_ap_drv.c static void vfio_ap_queue_dev_remove(struct ap_device *apdev) · line 86

Quoted 8 lines
drivers/s390/crypto/vfio_ap_drv.c +2 -3
@@ -82,9 +82,8 @@ 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);
+	kfree(container_of(dev, struct ap_matrix_dev, device));
+	matrix_dev = NULL;

I'm not sure nulling the global here buys us anything (especially after the kfree()). But it does not hurt either, so I'm fine with it. Style wise I think vfio_ap_matrix_dev_destroy() is a better place for the nulling IMHO, as it is dealing with module global state.

Regards,
Halil

Quoted 3 lines
drivers/s390/crypto/vfio_ap_drv.c +0 -0
 }
 
 static int matrix_bus_match(struct device *dev, struct device_driver *drv)