Skip to contentSkip to conversation

vhost/vsock: annotate lockless accesses to guest_cid

PATCH New Nguyen Dinh Phi 2 participants 2 messages Nov 29

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

PATCH v2 1/1 vhost/vsock: annotate lockless accesses to guest_cid

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

This is a concurrent data race under the Linux kernel memory model, as guest_cid can change while lockless readers access it. It does not cause incorrect behavior, though: each reader loads the CID once and works safely with either the old or the new value, making the outcome equivalent to the read occurring just before or just after the ioctl.

Annotate the store with WRITE_ONCE() and the two lockless loads with READ_ONCE(). This also guards against compiler re-fetching or tearing if the surrounding code is refactored in the future.

Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")

---
Changes in v2:
- Document that READ_ONCE()/WRITE_ONCE() are preferred over data_race() to guard against problems if the code is refactored.
- Add a comment at the store in vhost_vsock_set_cid().
v1: https://lore.kernel.org/netdev/[email protected]/

 drivers/vhost/vsock.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/vhost/vsock.c +7 -3
index 3de1f1920717..03a3b833640d 100644
@@ -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,11 @@ 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;
+	/* Read locklessly by vhost_vsock_get() and the TX kick handler, keep
+	 * both sides marked so a reader can't tear or re-fetch it if the code
+	 * is refactored.
+	 */
+	WRITE_ONCE(vsock->guest_cid, guest_cid);
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
 	mutex_unlock(&vhost_vsock_mutex);
 
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

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
Quoted 7 lines

read to 0xffff88810f1596ac of 4 bytes by task 7910 on cpu 3:
[ ... ]
READ_ONCE(). This also guards against compiler re-fetching or tearing if the surrounding code is refactored in the future.

Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1