From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795928768; b=UkOtZ0tvzDoV1daWw/UFHLPBhBa4h0amyA0MZ1ynbEh+Hbisa5pjv5PLLO/S7wyUa/XE pPVPN+li30yuZzPD9A2+1V7Jb8x2XpeP+3WIDdVFl0nFFcaurfJoPFZg2rYjShYtDrJUh lT6ZjH4Q1NfIzru56BiUmqbZWj9Vm3SfnbeuwdIIvCaG5YqP45uXB0Hx4yMq9tN8+Wm+4 BdGHF6r4yXeZU5en0WEjeXa8bdO4ijo1fiqLb29NpHFYy/p272FVVk2mzx5qETjOlFTCz y6ji/n04als+97vQA3FPgM2QMwDbbfkCXrtQff7GIAwODAbW6CtxIj67rXLzgJ1rKFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795928768; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=7mrJ+TdMdK0gcrljimlLxwwgrPF439AYLKoPKyyMQRs=; b=DC9QNWhRKJw51ThPXw73a3+FQEvFlAOapakhfgmzMfVSRniCBnPZC8olxmpEmomi9eru G4OJ9og6MgwGfoKBDJWJyX1N0ZHEO3k6PKZgFxszqlRE/4z/njePe4UFcTrGTQsLMxH7V eSJF9G1gTVIRtC9csn9kNM8rKJiBocujopNelSopVxo2ZkkLRmX6qtBlYwmWTtY6Mv/kf 6bYpWAnPYYGwiO3goNsZI1V1D9/YQuBT+GXG86XFaE5E9dGTNKB13mTG/FQisd9m0Kklf ThWn8dmJ1JlVnVsO2G+tPjKka6MF1fETOLnP1zHW3WympdmflEXkb93xf7EsWM94JTA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=stV45aqX; arc=none smtp.remote-ip=209.85.214.176 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B54A5606A323 for ; Sun, 29 Nov 2026 05:06:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B54A5606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=stV45aqX Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2f048476ab9so64992695ad.0 for ; Sat, 28 Nov 2026 21:06:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795928767; x=1796533567; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=7mrJ+TdMdK0gcrljimlLxwwgrPF439AYLKoPKyyMQRs=; b=stV45aqXcHKU9ooGk1hFJi/KMyAf8HDC1R0QN2c7kU3qcO4+DZz7k8/HJ7zKkGAven 6biO3KVuyMM+Bps2asRbIjIf6iLasG0gYVLopiAsDQD+83klc95+rlZ2PqZB2kyuX40+ 8KiCKEPsbXKksaG9KXpW6gCzDnkRWUsB2R71tR8pMWaO0RgovOpfVU5BAJ4antf/LcQz Hpp2HgPNpw6c7CbJ/6qjumETpEFBi4rt8n71mWC6hMpGOAaJop2oGOXVS9n+V6sOjh6Y oYkh8AhCLB5X9oHtu7158Cct38O7OuoD4jfeyKj3TLRTkB9cSVSUkUv+S66QPrDz0CM5 r8fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795928767; x=1796533567; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7mrJ+TdMdK0gcrljimlLxwwgrPF439AYLKoPKyyMQRs=; b=WhpDVf1o2AGhbrYZ+FR0iTk3J1VSyLdaw6TPvqg1y83WGz73oIZ0I/2RxZ0urpBPEv n4FG8NqdI/S4SG2KWumGQTFfxxg35B/xvOEGRT5v20/EMcfPcK4y0F4S/8YlGSzkSCTg 3OCuqmBOZ/PfdEZvv0jXxwHwuEqkx1xiIat1LMLCqBbeifT7JnnHgHrNJtPLDtjvev15 BOG8g3INlGhndCtLanfgXq9E5SFIG81WuWAXBan8FWWMrEDqU5u0TDjsukSE+JNzrTGl vs648xpjMXK0fcPeZmMf+ywvmp7x04rdk5up0JCOGyVVXVSeC2Y2OXpFw1mRTLEz2POt onCQ== X-Forwarded-Encrypted: i=1; AL+hErHc6ErindLe5XVGO7lhJXg5saOfRB4v60ih8uJNEuyMXaLqN87srNTTyBddXIhOuVLDnvk4519z4F41YuD9Yw==@lists.linux.dev X-Gm-Message-State: APuU4Z8FQ2CQRHhf7DGTHCUMci2duv3gs3y6S2O9GoT4iwhsPVyHOXz3 Py3bEAXWePzr0gfc2fQn9VfQjTd8rqZ5wdfmZNT9t8DKmPz+GgdnKITc X-Gm-Gg: AZFdTZeeWQzSeWyX06WowuiV5gKB7jzX2XFfNcln1IeB2rBdb6ueba0IC6Movz7eppg eohccJwBOwsyYRcbMF5iKNyzutJq4hD8zgedMsJVSQZ1Dhfh+gq09B8qowvzC+Q20DmZj75CLex x4jH4LN90mqsTDIK2lwaQ0ROcAHi/AOtzbhRzlF3G0ejNmqV7mjMor4LUph2k9ffxNwKt9bwCvD /MeajV+A7IK8Cf0ydZJx9OEJ7z20yY7PJ3xqNt/9HJz8E7cWZPlQHiVR6umr2WcyRCdLR3iAi97 5SfUxYaplBX/+j8gOfWNkRackKBsjSyVv8xY3DOF0b8UaCj6GRn2DnMdYLPA5f6NhlaIsqxAezU nd9xdJ3pK0ent9JBFGWxZKFfixCSrnCniz5W7GIP0EPIwpocGT9c2ePz5AW2VlexMr2O7caq3NY uUvvl86R7OgEH59GpxlcYl/71YBr4SeXcFsJebjBSdDoeYane6ndV8CctpFkrgTQiowgTGAat6o wiKDu0Zg6rHkCNbqIpvE0c6OoABy13V20/KhVCY0uwLv5pElvgnUcQ6S2Zc09UT5DiK7Eo01jkR 3Y/sS+J3wgPCKks1mjYxK+BcNUq2mlQ= X-Received: by 2002:a17:903:1c2:b0:2f0:5bc6:6bc8 with SMTP id d9443c01a7336-2f05bc66da3mr280864505ad.25.1795928766960; Sat, 28 Nov 2026 21:06:06 -0800 (PST) Received: from phi.nguyendp ([116.86.30.120]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f15663fsm143032385ad.76.2026.11.28.21.06.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 21:06:06 -0800 (PST) From: Nguyen Dinh Phi To: Stefan Hajnoczi , Stefano Garzarella , Bobby Eshleman , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Asias He Cc: Nguyen Dinh Phi , kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org Subject: [PATCH net v2] vhost/vsock: annotate lockless accesses to guest_cid Date: Sun, 29 Nov 2026 13:05:12 +0800 Message-ID: <20261129050514.1278990-1-phind.uet@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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") Signed-off-by: Nguyen Dinh Phi --- 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/20261117164916.2332953-1-phind.uet@gmail.com/ drivers/vhost/vsock.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 3de1f1920717..03a3b833640d 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,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); -- 2.53.0