virtio_ring: Fix mem leak with vring_new_virtqueue()
PATCH 1/1 virtio_ring: Fix mem leak with vring_new_virtqueue()
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.
--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +2 -2
@@ -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); }
Signature · 1 line
2.23.0
Quoted 23 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ring separately")Signed-off-by Suman Anna <[email protected]>--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-0
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 9 lines
drivers/virtio/virtio_ring.c +2
-2@@ -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
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2212
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-0list_del (&_vq->list);kfree (vq); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Hi Jason,
Quoted 32 lines · Jason Wang
Quoted 30 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ringseparately")
Signed-off-by Suman Anna <[email protected]>---
drivers/virtio/virtio_ring.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-2@@ -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);
Quoted 8 lines
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 4 lines
drivers/virtio/virtio_ring.c +2
-0} } + if (!vq->packed_ring) + kfree(vq->split.desc_state);Nitpick, it looks to me it would be more clear if we just free
desc_stateunconditionally 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
Quoted 9 lines
Anyway
desc_statewill be allocated by use even if !we_own_ring.Thanks
Quoted 3 lines
drivers/virtio/virtio_ring.c +0
-0list_del(&_vq->list); kfree(vq); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Jason Wang
Acked-byQuoted 40 lines · Suman Anna
Hi Jason,
Quoted 21 lines · Jason Wang
Quoted 20 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ringseparately")
Signed-off-by Suman Anna<[email protected]>--- drivers/virtio/virtio_ring.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 16 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +0
-0drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 9 lines
drivers/virtio/virtio_ring.c +2
-2@@ -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_stateunconditionally here (and remove the kfree for packed above).
Quoted 6 lines
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_statedoes 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
Thanks
Quoted 55 lines · Jason Wang
Quoted 45 lines · Suman Anna
Hi Jason,
Quoted 34 lines · Jason Wang
Quoted 33 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ringseparately")
Signed-off-by Suman Anna<[email protected]>---
drivers/virtio/virtio_ring.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-0@@ -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);Quoted 8 lines
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 6 lines
drivers/virtio/virtio_ring.c +2
-2- - 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_stateunconditionally here (and remove the kfree for packed above).Quoted 6 lines
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_statedoes not suffer this memleak, and need not be backported into stable kernels.regards
Suman
Quoted 5 lines
Though it's just a small tweak, I'm fine for leaving it for future.
So
Acked-by Jason Wang <[email protected]>
Thanks Jason, will post a patch for the same once this is merged.
regards
Suman
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 55 lines · Jason Wang
Quoted 45 lines · Suman Anna
Hi Jason,
Quoted 34 lines · Jason Wang
Quoted 33 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ringseparately")
Signed-off-by Suman Anna<[email protected]>---
drivers/virtio/virtio_ring.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-0@@ -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);Quoted 8 lines
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 6 lines
drivers/virtio/virtio_ring.c +2
-2- - 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_stateunconditionally here (and remove the kfree for packed above).Quoted 6 lines
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_statedoes not suffer this memleak, and need not be backported into stable kernels.regards
Suman
Quoted 6 lines
Though it's just a small tweak, I'm fine for leaving it for future.
So
Acked-by Jason Wang <[email protected]>
Mike,
Ping on this. I don't see the patch in -next yet. Can we get this into the current -rc please?
regards
Suman
Michael S. Tsirkin
maintainerQuoted 63 lines · Suman Anna
Quoted 55 lines · Jason Wang
Quoted 45 lines · Suman Anna
Hi Jason,
Quoted 34 lines · Jason Wang
Quoted 33 lines · 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 commitcbeedb72b97a("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 thevring_del_virtqueue()function.Fix this by moving the
desc_statefree code outside the flag and only for split rings. Issue was discovered during testing with remoteproc andvirtio_rpmsg.Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ringseparately")
Signed-off-by Suman Anna<[email protected]>---
drivers/virtio/virtio_ring.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/virtio/virtio_ring.c +0
-0@@ -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);Quoted 8 lines
drivers/virtio/virtio_ring.c void vring_del_virtqueue(struct virtqueue *_vq) · line 2209
Quoted 6 lines
drivers/virtio/virtio_ring.c +2
-2- - 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_stateunconditionally here (and remove the kfree for packed above).Quoted 6 lines
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_statedoes not suffer this memleak, and need not be backported into stable kernels.regards
SumanQuoted 6 lines
Though it's just a small tweak, I'm fine for leaving it for future.
So
Acked-by Jason Wang <[email protected]>
Quoted 6 lines
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!
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization