* [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid
@ 2026-11-17 16:48 Nguyen Dinh Phi
2026-11-18 6:33 ` Michael S. Tsirkin
2026-11-19 1:38 ` sashiko-bot
0 siblings, 2 replies; 3+ messages in thread
From: Nguyen Dinh Phi @ 2026-11-17 16:48 UTC (permalink / raw)
To: Stefan Hajnoczi, Stefano Garzarella, Bobby Eshleman,
Michael S. Tsirkin, Jason Wang, Eugenio Pérez, Asias He
Cc: Nguyen Dinh Phi, kvm, virtualization, netdev, linux-kernel
vhost_vsock.guest_cid is written by vhost_vsock_set_cid() under
vhost_vsock_mutex, but It is also read without that mutex in
vhost_vsock_handle_tx_kick(), and vhost_vsock_get().
KCSAN will report data-race if we issue VHOST_VSOCK_SET_GUEST_CID
ioctl while the device has TX traffic. For example:
write to 0xffff88810f1596ac of 4 bytes by task 7914 on cpu 1:
vhost_vsock_dev_ioctl+0x25f/0x720
__x64_sys_ioctl+0xc2/0x110
x64_sys_call+0x299/0x20a0
do_syscall_64+0xc9/0x510
entry_SYSCALL_64_after_hwframe+0x76/0x7e
read to 0xffff88810f1596ac of 4 bytes by task 7910 on cpu 3:
vhost_vsock_handle_tx_kick+0x41b/0x740
vhost_run_work_list+0x97/0xc0
vhost_task_fn+0x98/0x1f0
ret_from_fork+0x2e2/0x3c0
ret_from_fork_asm+0x1a/0x30
value changed: 0x0000004d -> 0x00000032
Both readers load the CID once and works correctly with either the
old or the new value.
Mark the store with WRITE_ONCE() and the two lockless loads with
READ_ONCE(), so KCSAN knows the race is intentional.
Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
---
drivers/vhost/vsock.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 88a310390eb0..f26c59013075 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)
hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid,
lockdep_is_held(&vhost_vsock_mutex)) {
- u32 other_cid = vsock->guest_cid;
+ u32 other_cid = READ_ONCE(vsock->guest_cid);
/* Skip instances that have no CID yet */
if (other_cid == 0)
@@ -572,7 +572,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
hdr = virtio_vsock_hdr(skb);
/* Only accept correctly addressed packets */
- if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid &&
+ if (le64_to_cpu(hdr->src_cid) == READ_ONCE(vsock->guest_cid) &&
le64_to_cpu(hdr->dst_cid) ==
vhost_transport_get_local_cid())
virtio_transport_recv_pkt(&vhost_transport, skb,
@@ -859,7 +859,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
if (vsock->guest_cid)
hash_del_rcu(&vsock->hash);
- vsock->guest_cid = guest_cid;
+ WRITE_ONCE(vsock->guest_cid, guest_cid);
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
mutex_unlock(&vhost_vsock_mutex);
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid
2026-11-17 16:48 [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid Nguyen Dinh Phi
@ 2026-11-18 6:33 ` Michael S. Tsirkin
2026-11-19 1:38 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-11-18 6:33 UTC (permalink / raw)
To: Nguyen Dinh Phi
Cc: Stefan Hajnoczi, Stefano Garzarella, Bobby Eshleman, Jason Wang,
Eugenio Pérez, Asias He, kvm, virtualization, netdev,
linux-kernel
On Wed, Nov 18, 2026 at 12:48:43AM +0800, Nguyen Dinh Phi wrote:
> vhost_vsock.guest_cid is written by vhost_vsock_set_cid() under
> vhost_vsock_mutex, but It is also read without that mutex in
> vhost_vsock_handle_tx_kick(), and vhost_vsock_get().
>
> KCSAN will report data-race if we issue VHOST_VSOCK_SET_GUEST_CID
> ioctl while the device has TX traffic. For example:
>
> write to 0xffff88810f1596ac of 4 bytes by task 7914 on cpu 1:
> vhost_vsock_dev_ioctl+0x25f/0x720
> __x64_sys_ioctl+0xc2/0x110
> x64_sys_call+0x299/0x20a0
> do_syscall_64+0xc9/0x510
> entry_SYSCALL_64_after_hwframe+0x76/0x7e
>
> read to 0xffff88810f1596ac of 4 bytes by task 7910 on cpu 3:
> vhost_vsock_handle_tx_kick+0x41b/0x740
> vhost_run_work_list+0x97/0xc0
> vhost_task_fn+0x98/0x1f0
> ret_from_fork+0x2e2/0x3c0
> ret_from_fork_asm+0x1a/0x30
>
> value changed: 0x0000004d -> 0x00000032
>
> Both readers load the CID once and works correctly with either the
> old or the new value.
>
> Mark the store with WRITE_ONCE() and the two lockless loads with
> READ_ONCE(), so KCSAN knows the race is intentional.
You make it sounds like it is just KCSAN false positive.
Is it?
if yes, this should be data_race. If no, document why.
>
> Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
> Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
> ---
> drivers/vhost/vsock.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 88a310390eb0..f26c59013075 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c
> @@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net)
>
> hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid,
> lockdep_is_held(&vhost_vsock_mutex)) {
> - u32 other_cid = vsock->guest_cid;
> + u32 other_cid = READ_ONCE(vsock->guest_cid);
>
> /* Skip instances that have no CID yet */
> if (other_cid == 0)
> @@ -572,7 +572,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
> hdr = virtio_vsock_hdr(skb);
>
> /* Only accept correctly addressed packets */
> - if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid &&
> + if (le64_to_cpu(hdr->src_cid) == READ_ONCE(vsock->guest_cid) &&
> le64_to_cpu(hdr->dst_cid) ==
> vhost_transport_get_local_cid())
> virtio_transport_recv_pkt(&vhost_transport, skb,
Will prevent compiler from hoisting the reads out of the loops.
Does it matter?
> @@ -859,7 +859,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
> if (vsock->guest_cid)
> hash_del_rcu(&vsock->hash);
>
> - vsock->guest_cid = guest_cid;
> + WRITE_ONCE(vsock->guest_cid, guest_cid);
> hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
> mutex_unlock(&vhost_vsock_mutex);
>
> --
> 2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid
2026-11-17 16:48 [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid Nguyen Dinh Phi
2026-11-18 6:33 ` Michael S. Tsirkin
@ 2026-11-19 1:38 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-19 1:38 UTC (permalink / raw)
To: Nguyen Dinh Phi; +Cc: kvm, virtualization, Michael S. Tsirkin, Eugenio Perez
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] vhost/vsock: hlist_node reuse in vhost_vsock_set_cid() breaks
RCU readers [1]
--
commit acdb8244e7111ba9c993f9a479a6926d4b658392
Author: Nguyen Dinh Phi <phind.uet@gmail.com>
vhost/vsock: annotate lockless accesses to guest_cid
This commit annotates the write to vhost_vsock.guest_cid in
vhost_vsock_set_cid() with WRITE_ONCE() and the lockless reads in
vhost_vsock_get() and vhost_vsock_handle_tx_kick() with READ_ONCE() to
avoid KCSAN data-race reports.
> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 88a310390eb0a..f26c590130759 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c
[ ... ]
> @@ -859,7 +859,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
> if (vsock->guest_cid)
> hash_del_rcu(&vsock->hash);
>
> - vsock->guest_cid = guest_cid;
> + WRITE_ONCE(vsock->guest_cid, guest_cid);
> hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
> mutex_unlock(&vhost_vsock_mutex);
[Severity: High]
This problem wasn't introduced by this patch, but can reusing vsock->hash
here without waiting for an RCU grace period break concurrent RCU readers
[1]?
When VHOST_VSOCK_SET_GUEST_CID changes an existing CID, hash_del_rcu()
unlinks vsock->hash from the old bucket and hash_add_rcu() immediately
overwrites vsock->hash.next to insert it into the new bucket. A
concurrent vhost_vsock_get() traversing the old bucket via
hash_for_each_possible_rcu() can follow the updated next pointer into the
wrong bucket, skipping the rest of the original chain (causing CID lookup
failures and dropped packets) or looping indefinitely if the node is
re-added to the same bucket.
[1] https://sashiko.dev/#/bug/linux-0431a358-6215-4016-8a71-f047effc16b0
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117164916.2332953-1-phind.uet@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 1:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 16:48 [PATCH net] vhost/vsock: annotate lockless accesses to guest_cid Nguyen Dinh Phi
2026-11-18 6:33 ` Michael S. Tsirkin
2026-11-19 1:38 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox