Skip to contentSkip to conversation

virtio_ring: Fix mem leak with vring_new_virtqueue()

PATCH In review Suman Anna 3 participants 7 messages Feb 24, 2020 – Mar 8, 2020 virtio core

lore
Share & tools
lore

Suman Anna

author
#0 lore raw

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.

Fixes cbeedb72b97a ("virtio_ring: allocate desc state for split ring separately")
---
 drivers/virtio/virtio_ring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +2 -2
index 867c7ebd3f10..58b96baa8d48 100644
@@ -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
#1 lore raw
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 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")
---
  drivers/virtio/virtio_ring.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 867c7ebd3f10..58b96baa8d48 100644

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 -0
  	list_del(&_vq->list);
  	kfree(vq);
  }

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#2 lore raw

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

---
  drivers/virtio/virtio_ring.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +0 -2
index 867c7ebd3f10..58b96baa8d48 100644
@@ -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_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

Quoted 9 lines

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

Thanks

Quoted 3 lines
drivers/virtio/virtio_ring.c +0 -0
       list_del(&_vq->list);
       kfree(vq);
   }

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Acked-by
#3 lore raw
Quoted 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 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")

---
   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 -0
index 867c7ebd3f10..58b96baa8d48 100644

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

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_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

Thanks

author
#4 lore raw
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 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")

---
   drivers/virtio/virtio_ring.c | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 867c7ebd3f10..58b96baa8d48 100644
@@ -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_state unconditionally 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_state does 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

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

author
#5 lore raw
In reply to Jason Wang
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 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")

---
   drivers/virtio/virtio_ring.c | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 867c7ebd3f10..58b96baa8d48 100644
@@ -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_state unconditionally 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_state does 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

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

regards
Suman

#6 lore raw
Quoted 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 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")

---
   drivers/virtio/virtio_ring.c | 4 ++--

   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_ring.c +0 -0
index 867c7ebd3f10..58b96baa8d48 100644
@@ -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_state unconditionally 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_state does 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

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