linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev
@ 2021-06-18 17:12 Jason J. Herne
  2021-06-18 18:11 ` Tony Krowiak
  2021-06-21 10:04 ` Halil Pasic
  0 siblings, 2 replies; 5+ messages in thread
From: Jason J. Herne @ 2021-06-18 17:12 UTC (permalink / raw)
  To: linux-s390; +Cc: linux-kernel, pasic, akrowiak, jgg

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.

Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
---
 drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
index 7dc72cb718b0..40e66cb363d1 100644
--- a/drivers/s390/crypto/vfio_ap_drv.c
+++ 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));
+	matrix_dev = NULL;
 }
 
 static int matrix_bus_match(struct device *dev, struct device_driver *drv)
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev
  2021-06-18 17:12 [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev Jason J. Herne
@ 2021-06-18 18:11 ` Tony Krowiak
  2021-06-18 18:23   ` Jason Gunthorpe
  2021-06-21 10:04 ` Halil Pasic
  1 sibling, 1 reply; 5+ messages in thread
From: Tony Krowiak @ 2021-06-18 18:11 UTC (permalink / raw)
  To: Jason J. Herne, linux-s390; +Cc: linux-kernel, pasic, jgg



On 6/18/21 1:12 PM, Jason J. Herne wrote:
> 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.
>
> Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
> ---
>   drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
> index 7dc72cb718b0..40e66cb363d1 100644
> --- a/drivers/s390/crypto/vfio_ap_drv.c
> +++ 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?

> +	matrix_dev = NULL;
>   }
>   
>   static int matrix_bus_match(struct device *dev, struct device_driver *drv)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev
  2021-06-18 18:11 ` Tony Krowiak
@ 2021-06-18 18:23   ` Jason Gunthorpe
  2021-06-18 20:05     ` Tony Krowiak
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Gunthorpe @ 2021-06-18 18:23 UTC (permalink / raw)
  To: Tony Krowiak; +Cc: Jason J. Herne, linux-s390, linux-kernel, pasic

On Fri, Jun 18, 2021 at 02:11:23PM -0400, Tony Krowiak wrote:
> 
> 
> On 6/18/21 1:12 PM, Jason J. Herne wrote:
> > 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.
> > 
> > Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
> >   drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
> >   1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
> > 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

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev
  2021-06-18 18:23   ` Jason Gunthorpe
@ 2021-06-18 20:05     ` Tony Krowiak
  0 siblings, 0 replies; 5+ messages in thread
From: Tony Krowiak @ 2021-06-18 20:05 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: Jason J. Herne, linux-s390, linux-kernel, pasic



On 6/18/21 2:23 PM, Jason Gunthorpe wrote:
> On Fri, Jun 18, 2021 at 02:11:23PM -0400, Tony Krowiak wrote:
>>
>> On 6/18/21 1:12 PM, Jason J. Herne wrote:
>>> 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.
>>>
>>> Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
>>>    drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
>>>    1 file changed, 2 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
>>> 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

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:

Reviewed-by: Tony Krowiak <akrowiak@linux.ibm.com>

>
> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
>
> Jason


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev
  2021-06-18 17:12 [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev Jason J. Herne
  2021-06-18 18:11 ` Tony Krowiak
@ 2021-06-21 10:04 ` Halil Pasic
  1 sibling, 0 replies; 5+ messages in thread
From: Halil Pasic @ 2021-06-21 10:04 UTC (permalink / raw)
  To: Jason J. Herne; +Cc: linux-s390, linux-kernel, akrowiak, jgg

On Fri, 18 Jun 2021 13:12:55 -0400
"Jason J. Herne" <jjherne@linux.ibm.com> wrote:

> 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.
> 
> Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
> ---
>  drivers/s390/crypto/vfio_ap_drv.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
> index 7dc72cb718b0..40e66cb363d1 100644
> --- a/drivers/s390/crypto/vfio_ap_drv.c
> +++ 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));
> +	matrix_dev = NULL;

Reviewed-by: Halil Pasic <pasic@linux.ibm.com>

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


>  }
>  
>  static int matrix_bus_match(struct device *dev, struct device_driver *drv)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-06-21 10:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-18 17:12 [PATCH v2] s390/vfio-ap: Fix module unload memory leak of matrix_dev Jason J. Herne
2021-06-18 18:11 ` Tony Krowiak
2021-06-18 18:23   ` Jason Gunthorpe
2021-06-18 20:05     ` Tony Krowiak
2021-06-21 10:04 ` Halil Pasic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).