Skip to contentSkip to conversation

hw/net/virtio-net: fix memory leak in timer_del()

PATCH In review Zheng Huang 2 participants 2 messages Mar 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net/virtio-net: fix memory leak in timer_del()

Hi,
This patch addresses a memory leak bug in the usages of timer_del(). The issue arisesfrom the incorrect use of the ambiguous timer API timer_del(), which does not free the timer object. The leak sanitizer report this issue during fuzzing. The correct API timer_free() freed the timer object instead.

Also I'd like to ask for a way to fix all 100+ wrong usages. In my opinion, the best way to fix this is to hide to timer_del() API and eliminate all usages of it.

---
 hw/net/virtio-net.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +3 -3
index de87cfadff..ca403821e0 100644
@@ -422,7 +422,7 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
             }
         } else {
             if (q->tx_timer) {
-                timer_del(q->tx_timer);
+                timer_free(q->tx_timer);
             } else {
                 qemu_bh_cancel(q->tx_bh);
             }
@@ -2844,7 +2844,7 @@ static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
 
     if (q->tx_waiting) {
         /* We already have queued packets, immediately flush */
-        timer_del(q->tx_timer);
+        timer_free(q->tx_timer);
         virtio_net_tx_timer(q);
     } else {
         /* re-arm timer to flush it (and more) on next tick */
@@ -3982,7 +3982,7 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queue_pairs = 1;
-    timer_del(n->announce_timer.tm);
+    timer_free(n->announce_timer.tm);
     n->announce_timer.round = 0;
     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
 
Signature · 1 line
2.34.1
#1 lore raw

hw/net/virtio-net.c static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) · line 425

Quoted 7 lines · Zheng Huang

+++ b/hw/net/virtio-net.c

hw/net/virtio-net.c +1 -1
@@ -422,7 +422,7 @@ static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
             }
         } else {
             if (q->tx_timer) {
-                timer_del(q->tx_timer);
+                timer_free(q->tx_timer);

* free()'ing a timer object while setting status in '_set_status()' function does not seem right, probably timer_del() is intended here.

Quoted 10 lines
hw/net/virtio-net.c +1 -1
@@ -2844,7 +2844,7 @@ static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
 
     if (q->tx_waiting) {
         /* We already have queued packets, immediately flush */
-        timer_del(q->tx_timer);
+        timer_free(q->tx_timer);
         virtio_net_tx_timer(q);
     } else {
         /* re-arm timer to flush it (and more) on next tick */

hw/net/virtio-net.c static void virtio_net_reset(VirtIODevice *vdev) · line 3987

Quoted 7 lines
hw/net/virtio-net.c +1 -1
@@ -3982,7 +3982,7 @@ static void virtio_net_reset(VirtIODevice *vdev)
     n->nobcast = 0;
     /* multiqueue is disabled by default */
     n->curr_queue_pairs = 1;
-    timer_del(n->announce_timer.tm);
+    timer_free(n->announce_timer.tm);
     n->announce_timer.round = 0;
     n->status &= ~VIRTIO_NET_S_ANNOUNCE;

* This timer_free() usage does not seem right, likely timer_del() is intended here. And a memory leak is happening due to another reason.

Thank you.
---
- Prasad