linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
@ 2020-02-24 21:26 Suman Anna
  2020-02-25  5:39 ` Jason Wang
  0 siblings, 1 reply; 7+ messages in thread
From: Suman Anna @ 2020-02-24 21:26 UTC (permalink / raw)
  To: Michael S. Tsirkin, Jason Wang
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc, Suman Anna

The functions vring_new_virtqueue() and __vring_new_virtqueue() are used
with split rings, and any allocations within these functions are managed
outside of the .we_own_ring flag. The commit cbeedb72b97a ("virtio_ring:
allocate desc state for split ring separately") allocates the desc state
within the __vring_new_virtqueue() but frees it only when the .we_own_ring
flag is set. This leads to a memory leak when freeing such allocated
virtqueues with the vring_del_virtqueue() function.

Fix this by moving the desc_state free code outside the flag and only
for split rings. Issue was discovered during testing with remoteproc
and virtio_rpmsg.

Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring separately")
Signed-off-by: Suman Anna <s-anna@ti.com>
---
 drivers/virtio/virtio_ring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 867c7ebd3f10..58b96baa8d48 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
 					 vq->split.queue_size_in_bytes,
 					 vq->split.vring.desc,
 					 vq->split.queue_dma_addr);
-
-			kfree(vq->split.desc_state);
 		}
 	}
+	if (!vq->packed_ring)
+		kfree(vq->split.desc_state);
 	list_del(&_vq->list);
 	kfree(vq);
 }
-- 
2.23.0


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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-02-24 21:26 [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue() Suman Anna
@ 2020-02-25  5:39 ` Jason Wang
  2020-02-25 16:51   ` Suman Anna
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Wang @ 2020-02-25  5:39 UTC (permalink / raw)
  To: Suman Anna, Michael S. Tsirkin
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc


On 2020/2/25 上午5:26, Suman Anna wrote:
> The functions vring_new_virtqueue() and __vring_new_virtqueue() are used
> with split rings, and any allocations within these functions are managed
> outside of the .we_own_ring flag. The commit cbeedb72b97a ("virtio_ring:
> allocate desc state for split ring separately") allocates the desc state
> within the __vring_new_virtqueue() but frees it only when the .we_own_ring
> flag is set. This leads to a memory leak when freeing such allocated
> virtqueues with the vring_del_virtqueue() function.
>
> Fix this by moving the desc_state free code outside the flag and only
> for split rings. Issue was discovered during testing with remoteproc
> and virtio_rpmsg.
>
> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring separately")
> Signed-off-by: Suman Anna <s-anna@ti.com>
> ---
>   drivers/virtio/virtio_ring.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
> index 867c7ebd3f10..58b96baa8d48 100644
> --- a/drivers/virtio/virtio_ring.c
> +++ b/drivers/virtio/virtio_ring.c
> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
>   					 vq->split.queue_size_in_bytes,
>   					 vq->split.vring.desc,
>   					 vq->split.queue_dma_addr);
> -
> -			kfree(vq->split.desc_state);
>   		}
>   	}
> +	if (!vq->packed_ring)
> +		kfree(vq->split.desc_state);


Nitpick, it looks to me it would be more clear if we just free 
desc_state unconditionally here (and remove the kfree for packed above).

Anyway desc_state will be allocated by use even if !we_own_ring.

Thanks


>   	list_del(&_vq->list);
>   	kfree(vq);
>   }


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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-02-25  5:39 ` Jason Wang
@ 2020-02-25 16:51   ` Suman Anna
  2020-02-26  3:13     ` Jason Wang
  0 siblings, 1 reply; 7+ messages in thread
From: Suman Anna @ 2020-02-25 16:51 UTC (permalink / raw)
  To: Jason Wang, Michael S. Tsirkin
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc

Hi Jason,

On 2/24/20 11:39 PM, Jason Wang wrote:
> 
> On 2020/2/25 上午5:26, Suman Anna wrote:
>> The functions vring_new_virtqueue() and __vring_new_virtqueue() are used
>> with split rings, and any allocations within these functions are managed
>> outside of the .we_own_ring flag. The commit cbeedb72b97a ("virtio_ring:
>> allocate desc state for split ring separately") allocates the desc state
>> within the __vring_new_virtqueue() but frees it only when the
>> .we_own_ring
>> flag is set. This leads to a memory leak when freeing such allocated
>> virtqueues with the vring_del_virtqueue() function.
>>
>> Fix this by moving the desc_state free code outside the flag and only
>> for split rings. Issue was discovered during testing with remoteproc
>> and virtio_rpmsg.
>>
>> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring
>> separately")
>> Signed-off-by: Suman Anna <s-anna@ti.com>
>> ---
>>   drivers/virtio/virtio_ring.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
>> index 867c7ebd3f10..58b96baa8d48 100644
>> --- a/drivers/virtio/virtio_ring.c
>> +++ b/drivers/virtio/virtio_ring.c
>> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
>>                        vq->split.queue_size_in_bytes,
>>                        vq->split.vring.desc,
>>                        vq->split.queue_dma_addr);
>> -
>> -            kfree(vq->split.desc_state);
>>           }
>>       }
>> +    if (!vq->packed_ring)
>> +        kfree(vq->split.desc_state);
> 
> 
> Nitpick, it looks to me it would be more clear if we just free
> desc_state unconditionally here (and remove the kfree for packed above).

OK, are you sure you want that to be folded into this patch? It looks to
me a separate cleanup/consolidation patch, and packed desc_state does
not suffer this memleak, and need not be backported into stable kernels.

regards
Suman

> Anyway desc_state will be allocated by use even if !we_own_ring.
> 
> Thanks
> 
> 
>>       list_del(&_vq->list);
>>       kfree(vq);
>>   }
> 


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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-02-25 16:51   ` Suman Anna
@ 2020-02-26  3:13     ` Jason Wang
  2020-02-26 17:01       ` Suman Anna
  2020-03-06  0:27       ` Suman Anna
  0 siblings, 2 replies; 7+ messages in thread
From: Jason Wang @ 2020-02-26  3:13 UTC (permalink / raw)
  To: Suman Anna, Michael S. Tsirkin
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc


On 2020/2/26 上午12:51, Suman Anna wrote:
> Hi Jason,
>
> On 2/24/20 11:39 PM, Jason Wang wrote:
>> On 2020/2/25 上午5:26, Suman Anna wrote:
>>> The functions vring_new_virtqueue() and __vring_new_virtqueue() are used
>>> with split rings, and any allocations within these functions are managed
>>> outside of the .we_own_ring flag. The commit cbeedb72b97a ("virtio_ring:
>>> allocate desc state for split ring separately") allocates the desc state
>>> within the __vring_new_virtqueue() but frees it only when the
>>> .we_own_ring
>>> flag is set. This leads to a memory leak when freeing such allocated
>>> virtqueues with the vring_del_virtqueue() function.
>>>
>>> Fix this by moving the desc_state free code outside the flag and only
>>> for split rings. Issue was discovered during testing with remoteproc
>>> and virtio_rpmsg.
>>>
>>> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring
>>> separately")
>>> Signed-off-by: Suman Anna<s-anna@ti.com>
>>> ---
>>>    drivers/virtio/virtio_ring.c | 4 ++--
>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
>>> index 867c7ebd3f10..58b96baa8d48 100644
>>> --- a/drivers/virtio/virtio_ring.c
>>> +++ b/drivers/virtio/virtio_ring.c
>>> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
>>>                         vq->split.queue_size_in_bytes,
>>>                         vq->split.vring.desc,
>>>                         vq->split.queue_dma_addr);
>>> -
>>> -            kfree(vq->split.desc_state);
>>>            }
>>>        }
>>> +    if (!vq->packed_ring)
>>> +        kfree(vq->split.desc_state);
>> Nitpick, it looks to me it would be more clear if we just free
>> desc_state unconditionally here (and remove the kfree for packed above).
> OK, are you sure you want that to be folded into this patch? It looks to
> me a separate cleanup/consolidation patch, and packed desc_state does
> not suffer this memleak, and need not be backported into stable kernels.
>
> regards
> Suman


Though it's just a small tweak, I'm fine for leaving it for future.

So

Acked-by: Jason Wang <jasowang@redhat.com>

Thanks


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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-02-26  3:13     ` Jason Wang
@ 2020-02-26 17:01       ` Suman Anna
  2020-03-06  0:27       ` Suman Anna
  1 sibling, 0 replies; 7+ messages in thread
From: Suman Anna @ 2020-02-26 17:01 UTC (permalink / raw)
  To: Jason Wang, Michael S. Tsirkin
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc

On 2/25/20 9:13 PM, Jason Wang wrote:
> 
> On 2020/2/26 上午12:51, Suman Anna wrote:
>> Hi Jason,
>>
>> On 2/24/20 11:39 PM, Jason Wang wrote:
>>> On 2020/2/25 上午5:26, Suman Anna wrote:
>>>> The functions vring_new_virtqueue() and __vring_new_virtqueue() are
>>>> used
>>>> with split rings, and any allocations within these functions are
>>>> managed
>>>> outside of the .we_own_ring flag. The commit cbeedb72b97a
>>>> ("virtio_ring:
>>>> allocate desc state for split ring separately") allocates the desc
>>>> state
>>>> within the __vring_new_virtqueue() but frees it only when the
>>>> .we_own_ring
>>>> flag is set. This leads to a memory leak when freeing such allocated
>>>> virtqueues with the vring_del_virtqueue() function.
>>>>
>>>> Fix this by moving the desc_state free code outside the flag and only
>>>> for split rings. Issue was discovered during testing with remoteproc
>>>> and virtio_rpmsg.
>>>>
>>>> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring
>>>> separately")
>>>> Signed-off-by: Suman Anna<s-anna@ti.com>
>>>> ---
>>>>    drivers/virtio/virtio_ring.c | 4 ++--
>>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/drivers/virtio/virtio_ring.c
>>>> b/drivers/virtio/virtio_ring.c
>>>> index 867c7ebd3f10..58b96baa8d48 100644
>>>> --- a/drivers/virtio/virtio_ring.c
>>>> +++ b/drivers/virtio/virtio_ring.c
>>>> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
>>>>                         vq->split.queue_size_in_bytes,
>>>>                         vq->split.vring.desc,
>>>>                         vq->split.queue_dma_addr);
>>>> -
>>>> -            kfree(vq->split.desc_state);
>>>>            }
>>>>        }
>>>> +    if (!vq->packed_ring)
>>>> +        kfree(vq->split.desc_state);
>>> Nitpick, it looks to me it would be more clear if we just free
>>> desc_state unconditionally here (and remove the kfree for packed above).
>> OK, are you sure you want that to be folded into this patch? It looks to
>> me a separate cleanup/consolidation patch, and packed desc_state does
>> not suffer this memleak, and need not be backported into stable kernels.
>>
>> regards
>> Suman
> 
> 
> Though it's just a small tweak, I'm fine for leaving it for future.
> 
> So
> 
> Acked-by: Jason Wang <jasowang@redhat.com>

Thanks Jason, will post a patch for the same once this is merged.

regards
Suman

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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-02-26  3:13     ` Jason Wang
  2020-02-26 17:01       ` Suman Anna
@ 2020-03-06  0:27       ` Suman Anna
  2020-03-08  7:58         ` Michael S. Tsirkin
  1 sibling, 1 reply; 7+ messages in thread
From: Suman Anna @ 2020-03-06  0:27 UTC (permalink / raw)
  To: Jason Wang, Michael S. Tsirkin
  Cc: Tiwei Bie, David S. Miller, virtualization, linux-kernel,
	linux-remoteproc

On 2/25/20 9:13 PM, Jason Wang wrote:
> 
> On 2020/2/26 上午12:51, Suman Anna wrote:
>> Hi Jason,
>>
>> On 2/24/20 11:39 PM, Jason Wang wrote:
>>> On 2020/2/25 上午5:26, Suman Anna wrote:
>>>> The functions vring_new_virtqueue() and __vring_new_virtqueue() are
>>>> used
>>>> with split rings, and any allocations within these functions are
>>>> managed
>>>> outside of the .we_own_ring flag. The commit cbeedb72b97a
>>>> ("virtio_ring:
>>>> allocate desc state for split ring separately") allocates the desc
>>>> state
>>>> within the __vring_new_virtqueue() but frees it only when the
>>>> .we_own_ring
>>>> flag is set. This leads to a memory leak when freeing such allocated
>>>> virtqueues with the vring_del_virtqueue() function.
>>>>
>>>> Fix this by moving the desc_state free code outside the flag and only
>>>> for split rings. Issue was discovered during testing with remoteproc
>>>> and virtio_rpmsg.
>>>>
>>>> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring
>>>> separately")
>>>> Signed-off-by: Suman Anna<s-anna@ti.com>
>>>> ---
>>>>    drivers/virtio/virtio_ring.c | 4 ++--
>>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/drivers/virtio/virtio_ring.c
>>>> b/drivers/virtio/virtio_ring.c
>>>> index 867c7ebd3f10..58b96baa8d48 100644
>>>> --- a/drivers/virtio/virtio_ring.c
>>>> +++ b/drivers/virtio/virtio_ring.c
>>>> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
>>>>                         vq->split.queue_size_in_bytes,
>>>>                         vq->split.vring.desc,
>>>>                         vq->split.queue_dma_addr);
>>>> -
>>>> -            kfree(vq->split.desc_state);
>>>>            }
>>>>        }
>>>> +    if (!vq->packed_ring)
>>>> +        kfree(vq->split.desc_state);
>>> Nitpick, it looks to me it would be more clear if we just free
>>> desc_state unconditionally here (and remove the kfree for packed above).
>> OK, are you sure you want that to be folded into this patch? It looks to
>> me a separate cleanup/consolidation patch, and packed desc_state does
>> not suffer this memleak, and need not be backported into stable kernels.
>>
>> regards
>> Suman
> 
> 
> Though it's just a small tweak, I'm fine for leaving it for future.
> 
> So
> 
> Acked-by: Jason Wang <jasowang@redhat.com>
> 

Mike,
Ping on this. I don't see the patch in -next yet. Can we get this into
the current -rc please?

regards
Suman

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

* Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue()
  2020-03-06  0:27       ` Suman Anna
@ 2020-03-08  7:58         ` Michael S. Tsirkin
  0 siblings, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2020-03-08  7:58 UTC (permalink / raw)
  To: Suman Anna
  Cc: Jason Wang, Tiwei Bie, David S. Miller, virtualization,
	linux-kernel, linux-remoteproc

On Thu, Mar 05, 2020 at 06:27:53PM -0600, Suman Anna wrote:
> On 2/25/20 9:13 PM, Jason Wang wrote:
> > 
> > On 2020/2/26 上午12:51, Suman Anna wrote:
> >> Hi Jason,
> >>
> >> On 2/24/20 11:39 PM, Jason Wang wrote:
> >>> On 2020/2/25 上午5:26, Suman Anna wrote:
> >>>> The functions vring_new_virtqueue() and __vring_new_virtqueue() are
> >>>> used
> >>>> with split rings, and any allocations within these functions are
> >>>> managed
> >>>> outside of the .we_own_ring flag. The commit cbeedb72b97a
> >>>> ("virtio_ring:
> >>>> allocate desc state for split ring separately") allocates the desc
> >>>> state
> >>>> within the __vring_new_virtqueue() but frees it only when the
> >>>> .we_own_ring
> >>>> flag is set. This leads to a memory leak when freeing such allocated
> >>>> virtqueues with the vring_del_virtqueue() function.
> >>>>
> >>>> Fix this by moving the desc_state free code outside the flag and only
> >>>> for split rings. Issue was discovered during testing with remoteproc
> >>>> and virtio_rpmsg.
> >>>>
> >>>> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring
> >>>> separately")
> >>>> Signed-off-by: Suman Anna<s-anna@ti.com>
> >>>> ---
> >>>>    drivers/virtio/virtio_ring.c | 4 ++--
> >>>>    1 file changed, 2 insertions(+), 2 deletions(-)
> >>>>
> >>>> diff --git a/drivers/virtio/virtio_ring.c
> >>>> b/drivers/virtio/virtio_ring.c
> >>>> index 867c7ebd3f10..58b96baa8d48 100644
> >>>> --- a/drivers/virtio/virtio_ring.c
> >>>> +++ b/drivers/virtio/virtio_ring.c
> >>>> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq)
> >>>>                         vq->split.queue_size_in_bytes,
> >>>>                         vq->split.vring.desc,
> >>>>                         vq->split.queue_dma_addr);
> >>>> -
> >>>> -            kfree(vq->split.desc_state);
> >>>>            }
> >>>>        }
> >>>> +    if (!vq->packed_ring)
> >>>> +        kfree(vq->split.desc_state);
> >>> Nitpick, it looks to me it would be more clear if we just free
> >>> desc_state unconditionally here (and remove the kfree for packed above).
> >> OK, are you sure you want that to be folded into this patch? It looks to
> >> me a separate cleanup/consolidation patch, and packed desc_state does
> >> not suffer this memleak, and need not be backported into stable kernels.
> >>
> >> regards
> >> Suman
> > 
> > 
> > Though it's just a small tweak, I'm fine for leaving it for future.
> > 
> > So
> > 
> > Acked-by: Jason Wang <jasowang@redhat.com>
> > 
> 
> Mike,
> Ping on this. I don't see the patch in -next yet. Can we get this into
> the current -rc please?
> 
> regards
> Suman

Yes will queue it shortly, thanks!


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

end of thread, other threads:[~2020-03-08  7:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-24 21:26 [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue() Suman Anna
2020-02-25  5:39 ` Jason Wang
2020-02-25 16:51   ` Suman Anna
2020-02-26  3:13     ` Jason Wang
2020-02-26 17:01       ` Suman Anna
2020-03-06  0:27       ` Suman Anna
2020-03-08  7:58         ` Michael S. Tsirkin

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).