From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795928768; b=y+FtkXqixivXn35kW/q7iy2m70sSQbgRlVIfO7NGl7pBDuW+7W0s/crNEdLtrCiXdoCa wr5nPyE36QtBwlH79A9WIqK4qRZagX1E41JK3HxOf4KXuwV+ZcMCtrUnes/FHt9nxcJwg +dXDUdqCemiDxlmnNtGPSu7jdqoFlb1AkNO8JNfv6MWh9ge3+XT47jqJ8CAhiDN//shsE q6R3auP53Rb+SsEWcktXng5MXRBOP2XeVhSs69RHpPjkBxuKVJtWv7zDf70URiTWqxa6H ILSY5xXvjYUNUyN2Kgg5cQKCOzgHIF/aUV2kUqfki46s1OlBON0untHJk+Tk8oYHVvA== 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=oFfjjBYDsi0ghoI1Z0HrCEfulR25XScCtfK28cwwrTKUleAT7zqDzbI6ddX/Gpw5gdnC bTwKa4SG014rK+hoSUTIVwgzJoe81wdDUJ9MOQW4lFcGRBUio/yk5YABCQqQfh7iYCRkP gM1QUXIc8PX8DIN1sGv47fWzuaQCHRpQlArKqzunlcppC07SpbZT/Dd+Bm+IpSkfsdY2s cDf/pVNkJkSiOdLUOq8JZSN+yRdJbEwgA36cl1YtlcfgurrFrfTKMoHZ8dXKpozI5lQ2w 9qmRdEs/l4Pbik+uUL+XUgLyHRTT64eaZuZWtsVopmzDnvb52xaTCGvdt0MZBTWrf4w== 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=MS3DBnAX; arc=none smtp.remote-ip=209.85.214.181 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 B5FF4606A324 for ; Sun, 29 Nov 2026 05:06:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B5FF4606A324 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, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=MS3DBnAX Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2f04244c3d0so73170325ad.3 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=vger.kernel.org; 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=MS3DBnAXxNkEbW++9/cMoRgXs0lV3/pYBBaw8RO43qye1fBpM235FWCxBGfEmWe4LW jsZ6sMrL6rdHQRRNKRWnBWfUYNr5piwbr7zGzR/rljLRAzD6wJMss14e+L9Fm6PoqdI3 jKY39ihrbNcZri0/b1KB6LkYxecqkZ9yuwFq7oisHCpFpdWUjSJ+6NKN+TDO0jkEqLwu wbS38OeCDeMuL2u/or86FqevXSrnvEigzrqbQ2Wpt6pcsDmSfFX/tolfePe+V5Lgocvn bUt4XrnozY/TeGbP4w7Deh5M7t5ZWaEMGkmG9xP6UQ+tlesLD5y52BfZjGMS+sVjU+qh EpWQ== 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=A+nNY1f7i8/N4HFUga7zvUBcotWe3Job+TRPRnNuNksuyf75Gw79ZBr0yLipms8dRI g3YE3ClAj9ShAxtDtYcMQou7q7aoO10NQ62X3KALXRR+zPXfbFe5xirhB9bDOsXuaELn jA/xlbkvo2jXxwxIjM3hDoJ1o+d9IoqTqscpFMGZlpk8kfFOMBwGIDIljq0YQZZHJ1Lc ZNwzeJ8/NJwyztuwShWmhEDB0THmMhfUOEGNFrw+pGKVjsC1abj4XQITBiClpTXv6XWL UiHMdi5REO6aCFD2DHPauZmktbP/EHIKhqrzOHZwGz8f2yEP2mArPfFNmFlQ6a8Ht2Jf Qg/Q== X-Forwarded-Encrypted: i=1; AL+hErFbnR9MAawsJL3VN5p4n9uR5BqrQBIr4gN4u5QdHL3OTJ+T2GaxI5WRlTn6TbAOgn7SaDxcN3k=@vger.kernel.org X-Gm-Message-State: APuU4Z+BxYTiIQs4aCqd0qeAdYm4Fazjg7JBjXVjCzfDusVi+tX0NjXH CVf6guu+oCqyOxMj6OOgd53w10MJYmEp1I7gpOsHSaMfF14nNV7oMbJm X-Gm-Gg: AZFdTZc9Hub4Nj6djWEZM2SF55hjPhBt2atv99XehRkQgmVizBnHIRclD39NP1fQyUk 6MDM01Ot0rw+fh+jWTPhcpSOIT1MoxWUxihqf/XswrCCMDjqxc+isEX/dnObKHdEbapAtcQWNkj v3ygRzer9NNsIpdETLpHt4MP4+KT4aZ5UqirsZlpCiGdDitAKXTChCBrd4LPa+//f980XOQjUf/ q/6kz8IrnayN2mLSwDVpuF16tA7YKzEl2rDTa/CG1pQlB5DjDah41yLuToLXvGYTQ3md8kPKNxj cz+O+4dpjPqxkxzACXU4pCRYTNknqztttRQnCKKW+Q+U5I0Fy5XhRiLvJ5ouOHwYu64sBTT5KF0 HKvQF9JkJ0d7KuS7x4e5pabmUPvsg/vieiymjHLa3TK9VIFGA0QxfWHHvDKynqJvreMivMcZkRF PraV7B48aJVt9jje8OhqVHKeLCpK5YICxdlJbZBrbgXzQBBIO8luQsIBgkTV1zHqta4sxoVm4rK HNYpekPwlHqD4SR1OioicFbWYSyc7+PVGifQrOoOGbCYCcfpWafUsOfWPSDQb+fgdniCmtod3eO nIEQImuypLxXh5g5EuoMSOJihP8VN04= 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: netdev@vger.kernel.org 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