Skip to contentSkip to conversation

s390/vfio_ap: fix memory leak in vfio_ap device driver

PATCH Superseded Tony Krowiak 3 participants 5 messages Mar 15, 2023 – Mar 16, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tony Krowiak

author maintainer
#0 lore raw

PATCH 1/1 s390/vfio_ap: fix memory leak in vfio_ap device driver

The device release callback function invoked to release the matrix device uses the dev_get_drvdata(device *dev) function to retrieve the pointer to the vfio_matrix_dev object in order to free its storage. The problem is, this object is not stored as drvdata with the device; since the kfree function will accept a NULL pointer, the memory for the vfio_matrix_dev object is never freed.

Since the device being released is contained within the vfio_matrix_dev object, the container_of macro will be used to retrieve its pointer.

Fixes 1fde573413b5 ("s390: vfio-ap: base implementation of VFIO AP device driver")
---
 drivers/s390/crypto/vfio_ap_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +3 -2
index 997b524bdd2b..15e9de9f4574 100644
@@ -54,8 +54,9 @@ static struct ap_driver vfio_ap_drv = {
 
 static void vfio_ap_matrix_dev_release(struct device *dev)
 {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
+	struct ap_matrix_dev *matrix_dev = container_of(dev,
+							struct ap_matrix_dev,
+							device);
 	kfree(matrix_dev);
 }
 
Signature · 1 line
2.31.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Tony Krowiak

The device release callback function invoked to release the matrix device uses the dev_get_drvdata(device *dev) function to retrieve the pointer to the vfio_matrix_dev object in order to free its storage. The problem is, this object is not stored as drvdata with the device; since the kfree function will accept a NULL pointer, the memory for the vfio_matrix_dev object is never freed.

Since the device being released is contained within the vfio_matrix_dev object, the container_of macro will be used to retrieve its pointer.

Fixes 1fde573413b5 ("s390: vfio-ap: base implementation of VFIO AP

device driver")

---
 drivers/s390/crypto/vfio_ap_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -0
index 997b524bdd2b..15e9de9f4574 100644

drivers/s390/crypto/vfio_ap_drv.c static struct ap_driver vfio_ap_drv = { · line 61

Quoted 10 lines
drivers/s390/crypto/vfio_ap_drv.c +3 -2
@@ -54,8 +54,9 @@ static struct ap_driver vfio_ap_drv = {
 
 static void vfio_ap_matrix_dev_release(struct device *dev)
 {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
+	struct ap_matrix_dev *matrix_dev = container_of(dev,
+							struct ap_matrix_dev,
+							device);
 	kfree(matrix_dev);
 }

I needed some indirections to follow what exactly happens here and how you fix it, but finally I got it.

maintainer
#3 lore raw
In reply to Tony Krowiak
Quoted 21 lines · Tony Krowiak

The device release callback function invoked to release the matrix device uses the dev_get_drvdata(device *dev) function to retrieve the pointer to the vfio_matrix_dev object in order to free its storage. The problem is, this object is not stored as drvdata with the device; since the kfree function will accept a NULL pointer, the memory for the vfio_matrix_dev object is never freed.

Since the device being released is contained within the vfio_matrix_dev object, the container_of macro will be used to retrieve its pointer.

Fixes 1fde573413b5 ("s390: vfio-ap: base implementation of VFIO AP device driver")
---
 drivers/s390/crypto/vfio_ap_drv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -0
index 997b524bdd2b..15e9de9f4574 100644

drivers/s390/crypto/vfio_ap_drv.c static struct ap_driver vfio_ap_drv = { · line 60

Quoted 9 lines
drivers/s390/crypto/vfio_ap_drv.c +3 -2
@@ -54,8 +54,9 @@ static struct ap_driver vfio_ap_drv = {
 
 static void vfio_ap_matrix_dev_release(struct device *dev)
 {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
+	struct ap_matrix_dev *matrix_dev = container_of(dev,
+							struct ap_matrix_dev,
+							device);
 	kfree(matrix_dev);

Could you keep this code more readable, including adding the missing blank line after the declaration, please? Something like:

static void vfio_ap_matrix_dev_release(struct device *dev)

{
	struct ap_matrix_dev *matrix_dev;

	matrix_dev = container_of(dev, struct ap_matrix_dev, device);
	kfree(matrix_dev);
}

Thanks!

author maintainer
#4 lore raw
Quoted 35 lines · Heiko Carstens
Quoted 21 lines · Tony Krowiak

The device release callback function invoked to release the matrix device uses the dev_get_drvdata(device *dev) function to retrieve the pointer to the vfio_matrix_dev object in order to free its storage. The problem is, this object is not stored as drvdata with the device; since the kfree function will accept a NULL pointer, the memory for the vfio_matrix_dev object is never freed.

Since the device being released is contained within the vfio_matrix_dev object, the container_of macro will be used to retrieve its pointer.

Fixes 1fde573413b5 ("s390: vfio-ap: base implementation of VFIO AP device driver")
---
  drivers/s390/crypto/vfio_ap_drv.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +0 -0
index 997b524bdd2b..15e9de9f4574 100644

drivers/s390/crypto/vfio_ap_drv.c static struct ap_driver vfio_ap_drv = { · line 60

Quoted 9 lines
drivers/s390/crypto/vfio_ap_drv.c +3 -2
@@ -54,8 +54,9 @@ static struct ap_driver vfio_ap_drv = {
 
  static void vfio_ap_matrix_dev_release(struct device *dev)
  {
-	struct ap_matrix_dev *matrix_dev = dev_get_drvdata(dev);
-
+	struct ap_matrix_dev *matrix_dev = container_of(dev,
+							struct ap_matrix_dev,
+							device);
  	kfree(matrix_dev);

Could you keep this code more readable, including adding the missing blank line after the declaration, please? Something like:

static void vfio_ap_matrix_dev_release(struct device *dev)

Quoted 8 lines
{
	struct ap_matrix_dev *matrix_dev;

	matrix_dev = container_of(dev, struct ap_matrix_dev, device);
	kfree(matrix_dev);
}

Thanks!

Will do.