From da3037b3ad11f31d8bd9f2a437cd74391852d53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=99=AF=E5=AE=87?= <2537738252@qq.com> Date: Tue, 20 Jan 2026 18:19:58 +0800 Subject: [PATCH 1/2] solve virtio_inject_irq's concurrency issue --- tools/virtio.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/virtio.c b/tools/virtio.c index bedb60b..e9882bf 100644 --- a/tools/virtio.c +++ b/tools/virtio.c @@ -918,10 +918,19 @@ void virtio_inject_irq(VirtQueue *vq) { } } volatile struct device_res *res; - while (is_queue_full(virtio_bridge->res_front, virtio_bridge->res_rear, - MAX_REQ)) - ; + + // virtio_bridge is a global resource located in shared memory. + // Access to critical resources such as res_front and res_rear requires locking. + + // Since the shared resources related to res_list are only accessed + // at one specific code location, a lock before polling is_queue_full + // is enough to ensure thread safety and performance. pthread_mutex_lock(&RES_MUTEX); + + while (is_queue_full(virtio_bridge->res_front, + virtio_bridge->res_rear, + MAX_REQ)) { + } unsigned int res_rear = virtio_bridge->res_rear; res = &virtio_bridge->res_list[res_rear]; res->irq_id = vq->dev->irq_id; From be3d02653f1698b0d6d08a4a2aabdbb1b4fe47d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=99=AF=E5=AE=87?= <2537738252@qq.com> Date: Tue, 20 Jan 2026 18:32:35 +0800 Subject: [PATCH 2/2] clang-format --- tools/virtio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/virtio.c b/tools/virtio.c index e9882bf..1f6dffb 100644 --- a/tools/virtio.c +++ b/tools/virtio.c @@ -920,15 +920,15 @@ void virtio_inject_irq(VirtQueue *vq) { volatile struct device_res *res; // virtio_bridge is a global resource located in shared memory. - // Access to critical resources such as res_front and res_rear requires locking. + // Access to critical resources such as res_front and res_rear requires + // locking. // Since the shared resources related to res_list are only accessed // at one specific code location, a lock before polling is_queue_full // is enough to ensure thread safety and performance. pthread_mutex_lock(&RES_MUTEX); - while (is_queue_full(virtio_bridge->res_front, - virtio_bridge->res_rear, + while (is_queue_full(virtio_bridge->res_front, virtio_bridge->res_rear, MAX_REQ)) { } unsigned int res_rear = virtio_bridge->res_rear;