Skip to contentSkip to conversation

vsock/virtio: prevent workers from using deleted virtqueues

PATCH In review Weiming Shi 5 participants 8 messages Jul 27 – Jul 29

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vsock/virtio: prevent workers from using deleted virtqueues

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:
 virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796)
 virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332)
 virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701)
 process_one_work (kernel/workqueue.c:3314)
 worker_thread (kernel/workqueue.c:3478)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...
Freed by task 141:
 kfree (mm/slub.c:6566)
 vp_del_vq (drivers/virtio/virtio_pci_common.c:259)
 vp_del_vqs (drivers/virtio/virtio_pci_common.c:285)
 virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912)
 virtio_device_freeze (drivers/virtio/virtio.c:658)
 virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601)
 pci_pm_freeze (drivers/pci/pci-driver.c:1098)
 device_suspend (drivers/base/power/main.c:1968)
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5
---
 net/vmw_vsock/virtio_transport.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +8 -6
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
 	struct virtqueue *vq;
 	bool added = false;
 
-	vq = vsock->vqs[VSOCK_VQ_TX];
 	mutex_lock(&vsock->tx_lock);
 
 	if (!vsock->tx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_TX];
+
 	do {
 		struct sk_buff *skb;
 		unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, event_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_EVENT];
-
 	mutex_lock(&vsock->event_lock);
 
 	if (!vsock->event_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_EVENT];
+
 	do {
 		struct virtio_vsock_event *event;
 		unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, rx_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_RX];
-
 	mutex_lock(&vsock->rx_lock);
 
 	if (!vsock->rx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_RX];
+
 	do {
 		virtqueue_disable_cb(vq);
 		for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 	mutex_unlock(&vsock->rx_lock);
 }
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Incorrect flush_work() ordering in virtio_vsock_remove() allows rx_work to be queued by a delayed send_pkt_work after rx_work has already been flushed, leading to a use-after-free when vsock is destroyed. --

--- Patch [1]: [PATCH] vsock/virtio: prevent workers from using deleted virtqueues --- commit fac9b600b273994b7593c604a6d3d0352d8dbee3
Author: Weiming Shi <[email protected]>

vsock/virtio: prevent workers from using deleted virtqueues

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. This patch reads each worker's virtqueue under its mutex after confirming the queue is running, preventing use-after-free of deleted virtqueues.

Quoted 4 lines
net/vmw_vsock/virtio_transport.c +0 -0
index 57f2d6ec3ffc6..79cf19f589439 100644

[ ... ]

net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 697

Quoted 10 lines
net/vmw_vsock/virtio_transport.c +2 -1
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 	mutex_unlock(&vsock->rx_lock);
 }

[Severity: High]
This is a pre-existing issue, but is there a use-after-free risk in virtio_vsock_remove() due to the flush_work() ordering?

In virtio_vsock_remove(), the flush sequence drains rx_work before send_pkt_work:

    flush_work(&vsock->rx_work);
    flush_work(&vsock->tx_work);
    flush_work(&vsock->event_work);
    flush_work(&vsock->send_pkt_work);
    ...
    kfree(vsock);

Meanwhile, virtio_transport_send_pkt_work() can queue rx_work outside the tx_lock:

out:

    mutex_unlock(&vsock->tx_lock);

    if (restart_rx)
        queue_work(virtio_vsock_workqueue, &vsock->rx_work);

If virtio_vsock_remove() flushes rx_work (which returns immediately if it has not been queued yet), and then blocks waiting on send_pkt_work to finish, could send_pkt_work queue rx_work just before completing?

This would leave rx_work pending in the workqueue after virtio_vsock_remove() frees the vsock object, leading to memory corruption when the work executes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 58 lines · On Tue, Jul 28, 2026 at 03:58:23AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Incorrect flush_work() ordering in virtio_vsock_remove() allows rx_work to be queued by a delayed send_pkt_work after rx_work has already been flushed, leading to a use-after-free when vsock is destroyed. --

--- Patch [1]: [PATCH] vsock/virtio: prevent workers from using deleted virtqueues --- commit fac9b600b273994b7593c604a6d3d0352d8dbee3
Author: Weiming Shi <[email protected]>

vsock/virtio: prevent workers from using deleted virtqueues

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. This patch reads each worker's virtqueue under its mutex after confirming the queue is running, preventing use-after-free of deleted virtqueues.

Quoted 4 lines
net/vmw_vsock/virtio_transport.c +0 -0
index 57f2d6ec3ffc6..79cf19f589439 100644

[ ... ]

net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 697

Quoted 10 lines
net/vmw_vsock/virtio_transport.c +2 -1
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 	mutex_unlock(&vsock->rx_lock);
 }

[Severity: High]
This is a pre-existing issue, but is there a use-after-free risk in virtio_vsock_remove() due to the flush_work() ordering?

In virtio_vsock_remove(), the flush sequence drains rx_work before send_pkt_work:

   flush_work(&vsock->rx_work);
   flush_work(&vsock->tx_work);
   flush_work(&vsock->event_work);
   flush_work(&vsock->send_pkt_work);
   ...
   kfree(vsock);

Meanwhile, virtio_transport_send_pkt_work() can queue rx_work outside the tx_lock:

out:

   mutex_unlock(&vsock->tx_lock);

   if (restart_rx)
       queue_work(virtio_vsock_workqueue, &vsock->rx_work);
Quoted 6 lines

If virtio_vsock_remove() flushes rx_work (which returns immediately if it has not been queued yet), and then blocks waiting on send_pkt_work to finish, could send_pkt_work queue rx_work just before completing?

This would leave rx_work pending in the workqueue after virtio_vsock_remove() frees the vsock object, leading to memory corruption when the work executes.

This seems a real issue, would you like to fix it? In a separate patch, since it's pre-existing.

Thanks,
Stefano

Quoted 4 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#3 lore raw
In reply to Weiming Shi
Quoted 109 lines · Weiming Shi

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:
 virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796)
 virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332)
 virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701)
 process_one_work (kernel/workqueue.c:3314)
 worker_thread (kernel/workqueue.c:3478)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...
Freed by task 141:
 kfree (mm/slub.c:6566)
 vp_del_vq (drivers/virtio/virtio_pci_common.c:259)
 vp_del_vqs (drivers/virtio/virtio_pci_common.c:285)
 virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912)
 virtio_device_freeze (drivers/virtio/virtio.c:658)
 virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601)
 pci_pm_freeze (drivers/pci/pci-driver.c:1098)
 device_suspend (drivers/base/power/main.c:1968)
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5
---
 net/vmw_vsock/virtio_transport.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +8 -6
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
 	struct virtqueue *vq;
 	bool added = false;
 
-	vq = vsock->vqs[VSOCK_VQ_TX];
 	mutex_lock(&vsock->tx_lock);
 
 	if (!vsock->tx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_TX];
+
 	do {
 		struct sk_buff *skb;
 		unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, event_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_EVENT];
-
 	mutex_lock(&vsock->event_lock);
 
 	if (!vsock->event_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_EVENT];
+
 	do {
 		struct virtio_vsock_event *event;
 		unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, rx_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_RX];
-
 	mutex_lock(&vsock->rx_lock);
 
 	if (!vsock->rx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_RX];
+
 	do {
 		virtqueue_disable_cb(vq);
 		for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 	mutex_unlock(&vsock->rx_lock);
 }
Signature · 1 line
2.55.0

Not a strong opinion from me, but since this last hunk is the one that fixes the bug and the other hunks are moreso hardening, maybe break these out into two patches?

Besides, that all looks good to me.

#4 lore raw
Quoted 101 lines · Bobby Eshleman
Quoted 100 lines · Weiming Shi

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:
 virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796)
 virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332)
 virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701)
 process_one_work (kernel/workqueue.c:3314)
 worker_thread (kernel/workqueue.c:3478)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...
Freed by task 141:
 kfree (mm/slub.c:6566)
 vp_del_vq (drivers/virtio/virtio_pci_common.c:259)
 vp_del_vqs (drivers/virtio/virtio_pci_common.c:285)
 virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912)
 virtio_device_freeze (drivers/virtio/virtio.c:658)
 virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601)
 pci_pm_freeze (drivers/pci/pci-driver.c:1098)
 device_suspend (drivers/base/power/main.c:1968)
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5
---
 net/vmw_vsock/virtio_transport.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +6 -5
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
 	struct virtqueue *vq;
 	bool added = false;
 
-	vq = vsock->vqs[VSOCK_VQ_TX];
 	mutex_lock(&vsock->tx_lock);
 
 	if (!vsock->tx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_TX];
+
 	do {
 		struct sk_buff *skb;
 		unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, event_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_EVENT];
-
 	mutex_lock(&vsock->event_lock);
 
 	if (!vsock->event_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_EVENT];
+
 	do {
 		struct virtio_vsock_event *event;
 		unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, rx_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_RX];
-
 	mutex_lock(&vsock->rx_lock);
 
 	if (!vsock->rx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_RX];
+
 	do {
 		virtqueue_disable_cb(vq);
 		for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
Quoted 13 lines
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +2 -2
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 	mutex_unlock(&vsock->rx_lock);
 }
--

2.55.0

Not a strong opinion from me, but since this last hunk is the one that fixes the bug and the other hunks are moreso hardening, maybe break these out into two patches?

I think also the other hunks fix an issue, but I agree on the split since IMO we are fixing 2 different commits. Commit b917507e5ad9 ("vsock/virtio: stop workers during the .remove()") was before freeze/resume added by commit bd50c5dc182b ("vsock/virtio: add support for device suspend/resume"). Only after that one we can have the false -> true transition of *_run variables.

So IMO hunks 1-3 should have Fixes: bd50c5dc182b ... and hunk 4 should have Fixes: b917507e5ad9 ...

That said, it's not a strong opinion here too, but if you prefer a single patch, please add both Fixes.

Thanks,
Stefano

Quoted 5 lines

Besides, that all looks good to me.

#5 lore raw
Quoted 124 lines · Stefano Garzarella <[email protected]> 于2026年7月29日周三 22:47写道:
Quoted 101 lines · Bobby Eshleman
Quoted 100 lines · Weiming Shi

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:
 virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796)
 virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332)
 virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701)
 process_one_work (kernel/workqueue.c:3314)
 worker_thread (kernel/workqueue.c:3478)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...
Freed by task 141:
 kfree (mm/slub.c:6566)
 vp_del_vq (drivers/virtio/virtio_pci_common.c:259)
 vp_del_vqs (drivers/virtio/virtio_pci_common.c:285)
 virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912)
 virtio_device_freeze (drivers/virtio/virtio.c:658)
 virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601)
 pci_pm_freeze (drivers/pci/pci-driver.c:1098)
 device_suspend (drivers/base/power/main.c:1968)
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5
---
 net/vmw_vsock/virtio_transport.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +6 -5
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
     struct virtqueue *vq;
     bool added = false;
 
-    vq = vsock->vqs[VSOCK_VQ_TX];
     mutex_lock(&vsock->tx_lock);
 
     if (!vsock->tx_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_TX];
+
     do {
             struct sk_buff *skb;
             unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
             container_of(work, struct virtio_vsock, event_work);
     struct virtqueue *vq;
 
-    vq = vsock->vqs[VSOCK_VQ_EVENT];
-
     mutex_lock(&vsock->event_lock);
 
     if (!vsock->event_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_EVENT];
+
     do {
             struct virtio_vsock_event *event;
             unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
             container_of(work, struct virtio_vsock, rx_work);
     struct virtqueue *vq;
 
-    vq = vsock->vqs[VSOCK_VQ_RX];
-
     mutex_lock(&vsock->rx_lock);
 
     if (!vsock->rx_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_RX];
+
     do {
             virtqueue_disable_cb(vq);
             for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
     } while (!virtqueue_enable_cb(vq));
 
Quoted 13 lines
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +2 -2
 out:
-    if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+    if (vsock->rx_run &&
+        vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
             virtio_vsock_rx_fill(vsock);
     mutex_unlock(&vsock->rx_lock);
 }
--

2.55.0

Not a strong opinion from me, but since this last hunk is the one that fixes the bug and the other hunks are moreso hardening, maybe break these out into two patches?

I think also the other hunks fix an issue, but I agree on the split since IMO we are fixing 2 different commits. Commit b917507e5ad9 ("vsock/virtio: stop workers during the .remove()") was before freeze/resume added by commit bd50c5dc182b ("vsock/virtio: add support for device suspend/resume"). Only after that one we can have the false -> true transition of *_run variables.

Quoted 15 lines

So IMO hunks 1-3 should have Fixes: bd50c5dc182b ... and hunk 4 should have Fixes: b917507e5ad9 ...

That said, it's not a strong opinion here too, but if you prefer a single patch, please add both Fixes.

Thanks,
Stefano

Quoted 5 lines

Besides, that all looks good to me.

Thanks for your review. v2 sent.

Best,
Weiming Shi

#6 lore raw
Quoted 138 lines · Weiming Shi <[email protected]> 于2026年7月30日周四 03:05写道:
Quoted 136 lines · Stefano Garzarella <[email protected]> 于2026年7月29日周三 22:47写道:
Quoted 101 lines · Bobby Eshleman
Quoted 100 lines · Weiming Shi

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:
 virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796)
 virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332)
 virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701)
 process_one_work (kernel/workqueue.c:3314)
 worker_thread (kernel/workqueue.c:3478)
 kthread (kernel/kthread.c:436)
 ret_from_fork (arch/x86/kernel/process.c:158)
 ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...
Freed by task 141:
 kfree (mm/slub.c:6566)
 vp_del_vq (drivers/virtio/virtio_pci_common.c:259)
 vp_del_vqs (drivers/virtio/virtio_pci_common.c:285)
 virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912)
 virtio_device_freeze (drivers/virtio/virtio.c:658)
 virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601)
 pci_pm_freeze (drivers/pci/pci-driver.c:1098)
 device_suspend (drivers/base/power/main.c:1968)
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5
---
 net/vmw_vsock/virtio_transport.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +6 -5
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
     struct virtqueue *vq;
     bool added = false;
 
-    vq = vsock->vqs[VSOCK_VQ_TX];
     mutex_lock(&vsock->tx_lock);
 
     if (!vsock->tx_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_TX];
+
     do {
             struct sk_buff *skb;
             unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
             container_of(work, struct virtio_vsock, event_work);
     struct virtqueue *vq;
 
-    vq = vsock->vqs[VSOCK_VQ_EVENT];
-
     mutex_lock(&vsock->event_lock);
 
     if (!vsock->event_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_EVENT];
+
     do {
             struct virtio_vsock_event *event;
             unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
             container_of(work, struct virtio_vsock, rx_work);
     struct virtqueue *vq;
 
-    vq = vsock->vqs[VSOCK_VQ_RX];
-
     mutex_lock(&vsock->rx_lock);
 
     if (!vsock->rx_run)
             goto out;
 
+    vq = vsock->vqs[VSOCK_VQ_RX];
+
     do {
             virtqueue_disable_cb(vq);
             for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
     } while (!virtqueue_enable_cb(vq));
 
Quoted 13 lines
Quoted 9 lines
net/vmw_vsock/virtio_transport.c +2 -2
 out:
-    if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+    if (vsock->rx_run &&
+        vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
             virtio_vsock_rx_fill(vsock);
     mutex_unlock(&vsock->rx_lock);
 }
--

2.55.0

Not a strong opinion from me, but since this last hunk is the one that fixes the bug and the other hunks are moreso hardening, maybe break these out into two patches?

I think also the other hunks fix an issue, but I agree on the split since IMO we are fixing 2 different commits. Commit b917507e5ad9 ("vsock/virtio: stop workers during the .remove()") was before freeze/resume added by commit bd50c5dc182b ("vsock/virtio: add support for device suspend/resume"). Only after that one we can have the false -> true transition of *_run variables.

So IMO hunks 1-3 should have Fixes: bd50c5dc182b ... and hunk 4 should have Fixes: b917507e5ad9 ...

That said, it's not a strong opinion here too, but if you prefer a single patch, please add both Fixes.

Thanks,
Stefano

Quoted 3 lines

Besides, that all looks good to me.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

Thanks for your review. v2 sent.

Best,
Weiming Shi

Sorry for the noise; I resent the series as v3 with proper threading and no code changes.

https://lore.kernel.org/all/[email protected]/

#7 lore raw
In reply to Weiming Shi
Quoted 101 lines · Weiming Shi

The RX, TX and event workers read their virtqueue pointers before taking the mutex that protects the queue and its run flag. A work item delayed across freeze and restore can therefore retain a pointer deleted by virtio_vsock_vqs_del(), observe the run flag for the replacement queues, and use the freed pointer.

RX has an additional path: when rx_run is clear, the common exit still refills the RX queue. A queued worker can consequently call virtqueue_add_sgs() immediately after freeze deletes the virtqueues.

BUG: KASAN: slab-use-after-free in virtqueue_add_sgs
Read of size 4 by task kworker/2:1
Workqueue: virtio_vsock virtio_transport_rx_work
Call Trace:

virtqueue_add_sgs (drivers/virtio/virtio_ring.c:2796) virtio_vsock_rx_fill (net/vmw_vsock/virtio_transport.c:332) virtio_transport_rx_work (net/vmw_vsock/virtio_transport.c:701) process_one_work (kernel/workqueue.c:3314) worker_thread (kernel/workqueue.c:3478)
kthread (kernel/kthread.c:436)
ret_from_fork (arch/x86/kernel/process.c:158) ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
...

Freed by task 141:

kfree (mm/slub.c:6566)
vp_del_vq (drivers/virtio/virtio_pci_common.c:259) vp_del_vqs (drivers/virtio/virtio_pci_common.c:285) virtio_vsock_freeze (net/vmw_vsock/virtio_transport.c:912) virtio_device_freeze (drivers/virtio/virtio.c:658) virtio_pci_freeze (drivers/virtio/virtio_pci_common.c:601) pci_pm_freeze (drivers/pci/pci-driver.c:1098) device_suspend (drivers/base/power/main.c:1968)

Kernel panic - not syncing: KASAN: panic_on_warn set ...

Read each worker's virtqueue under its mutex after confirming that the queue is running, and only refill RX while RX is running.

Fixes b917507e5ad9 ("vsock/virtio: stop workers during the .remove()")
Assisted-by OpenAI-Codex:gpt-5

---
net/vmw_vsock/virtio_transport.c | 14 ++++++++------

1 file changed, 8 insertions(+), 6 deletions(-)
net/vmw_vsock/virtio_transport.c +6 -5
index 57f2d6ec3ffc..79cf19f58943 100644
@@ -346,12 +346,13 @@ static void virtio_transport_tx_work(struct work_struct *work)
 	struct virtqueue *vq;
 	bool added = false;
 
-	vq = vsock->vqs[VSOCK_VQ_TX];
 	mutex_lock(&vsock->tx_lock);
 
 	if (!vsock->tx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_TX];
+
 	do {
 		struct sk_buff *skb;
 		unsigned int len;
@@ -451,13 +452,13 @@ static void virtio_transport_event_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, event_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_EVENT];
-
 	mutex_lock(&vsock->event_lock);
 
 	if (!vsock->event_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_EVENT];
+
 	do {
 		struct virtio_vsock_event *event;
 		unsigned int len;
@@ -634,13 +635,13 @@ static void virtio_transport_rx_work(struct work_struct *work)
 		container_of(work, struct virtio_vsock, rx_work);
 	struct virtqueue *vq;
 
-	vq = vsock->vqs[VSOCK_VQ_RX];
-
 	mutex_lock(&vsock->rx_lock);
 
 	if (!vsock->rx_run)
 		goto out;
 
+	vq = vsock->vqs[VSOCK_VQ_RX];
+
 	do {
 		virtqueue_disable_cb(vq);
 		for (;;) {
@@ -689,7 +690,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 

out:

net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 693

Quoted 2 lines
net/vmw_vsock/virtio_transport.c +1 -1
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (vsock->rx_run &&

Instead of adding this check, what about adding a new label...

Quoted 2 lines
net/vmw_vsock/virtio_transport.c +1 -0
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
		virtio_vsock_rx_fill(vsock);

out_nofill:

Quoted 1 line
	mutex_unlock(&vsock->rx_lock);

And use it when we check vsock->rx_run ?

Since we are taking the mutex, we don't need to check it again.

The rest LGTM, but I agree with Bobby about the split, since the Fixes tag here is confusing. I'll reply there.

Thanks,
Stefano