From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=67.231.153.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796173406; b=KLrIHK7j+i3onQOxQh/+yG/Za/k05AGCJM/CF5K8aZwWi+W9wpg8On4L/0mdLc3q6k54 3IGNWxQdwVH+71uBk/vh8j4Ne5P2X9aW9uROrPAfWdCOsPif9N+WUpbqdrRugz2eht8e4 KG4wd+FnsYS9mL7Xx/RUO0P9DRHqqKfhh0rPKBChapZRvZporitTnMf78I+doSqSr2+lT uaqMDpok1kHU4qQfrrsY8sRGNMtqKqfrUGJUaCRyBh6d/pfkVw2fBTshbIpCCp8QhXCjK 4v5uyBqOwm/AyOTpyCf6mo5maLNLDToFBNk6qnxNrcjfN3N/nJJ3C5xmwanxYaAzcgA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796173406; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Date:From:To: CC:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:X-Originating-IP:X-Proofpoint-GUID: X-Authority-Analysis:X-Proofpoint-Spam-Details-Enc: X-Proofpoint-ORIG-GUID:X-Proofpoint-Spam-Info: X-Proofpoint-Virus-Version; bh=tedgiw8k7GBB7CQo9rWVmok9rgBEF0I2gLX5LEw2EvQ=; b=gTA9mg9crwnG8A+wOZOLNqLUSpLz7577BVJgex7xc7yKCU29Q8tv57UeKq7x7SyyQT4k vJ85HrgvHqqB6iSe5AlB77+DjvEP2TzqnA9xZ6WCIwI4+M3jd/6mUHEDn1bt4BJuPrTt5 l3VTTWnHvZ6qtTFQm0pAqMptDuMP6lokMS+8OZ4Y62RAQoYxSW1UCLPB9+ZxSHGUCSyd1 FWtwob/dNePUqIBrpSVronaoUAGhfi2aWYW2pxnP7NAbrHv9UIJpNdMTGWlOIIGD7WN62 ELiTu5GDyodFvPzoR2jJYAEZiMvfF+MTpd2gQaTvhVy2L3uvtzvcs59HOQ3lJn/rkZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass header.d=meta.com header.i=@meta.com header.a=rsa-sha256 header.s=pps82601-s2048-2026-q3 header.b=S0TIUv7x; arc=none smtp.remote-ip=67.231.153.30 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EE5C606A31A for ; Wed, 2 Dec 2026 01:03:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3EE5C606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=meta.com header.i=@meta.com header.a=rsa-sha256 header.s=pps82601-s2048-2026-q3 header.b=S0TIUv7x Received: from pps.filterd (m0528005.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B1NUuVn3921365; Tue, 1 Dec 2026 17:03:23 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to; s=pps82601-s2048-2026-q3; bh=tedgiw8k7GB B7CQo9rWVmok9rgBEF0I2gLX5LEw2EvQ=; b=S0TIUv7xnyhCNx3LD0ZnlCn/qEG gHGq9RLObpwqt9CgPOL3PqC5sUvjaU58C+TSAXAxjSSr1k1VSJkZudp1mYd2fGzN wly6cm4eOC0wBFKibvAIaYNWE6RS2S+pD170GcdVGFQXFl6hio/rWGjx9l8lnFzu KF1wz4u7GVM8YInEtLGJTiwLcIiwwmwawC0tUjKV30BC1RSvLwJqF6UPZbFBiP7l woIAK7HMsGDtXXbVzCotXtUEnp7YvQttpst3fT549O3xbwoWc1K1Xqm9TFjZj1So 3QVWfQo1XnmPNdPo8LHLLqbfwhaxBLA7kjBrI72Go1O1UY6BnVTdryMhc1g== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4ja472tfbq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 01 Dec 2026 17:03:23 -0800 (PST) Received: from localhost (2620:10d:c0a8:1b::30) by mail.thefacebook.com (2620:10d:c0a9:6f::237c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.56; Wed, 2 Dec 2026 01:03:23 +0000 Date: Tue, 1 Dec 2026 17:03:22 -0800 From: Bobby Eshleman To: Nguyen Dinh Phi CC: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , Eugenio =?iso-8859-1?Q?P=E9rez?= , Asias He , , , Subject: Re: [PATCH net v2] vhost/vsock: annotate lockless accesses to guest_cid Message-ID: References: <20261129050514.1278990-1-phind.uet@gmail.com> 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-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20261129050514.1278990-1-phind.uet@gmail.com> X-Proofpoint-GUID: rvUi3YTvGfaCJHqXq3HzbmpH0h2b3Q4P X-Authority-Analysis: v=2.4 cv=Valir1p9 c=1 sm=1 tr=0 ts=6b0f6e5b cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=kj9zAlcOel0A:10 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=7x6HtfJdh03M6CCDgxCd:22 a=jCddH8ec0KUNCymVuxII:22 a=VwQbUJbxAAAA:8 a=pGLkceISAAAA:8 a=VabnemYjAAAA:8 a=NBae5QDkSdey3HmEMDsA:9 a=CjuIK1q_8ugA:10 a=gKebqoRLp9LExxC7YDUY:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAyMDAwMyBTYWx0ZWRfX/uX8bLrjg+Jm dZdamlqRj3xF2GPSXEVNfg9fyUb1qGj+EM++Ba9eDkfnTjVSpgpzm2QTJRE4+ONodObR1tAERDB U6QxZifoakREX6AtUM/o5oz3xYHk+pd3BTKdzVFlgFfl3wcZ3gJb8iofR6Dc2K9NoXsdRBx7H2Z bH9xXXBOsXDdMAzUHYXvJLokzGWCeArIuFQv5fU3oqvF3inNMhK+brMy3c4w2lNYAbsaEfyFLZ4 GgL5r73tYqXxMrHYrOU1rBV9n24MpuSQNgk3N5g+pbtnH085P0USVnxvi5CK7tQdDCZZ3lRHmfI eSIG5Qx/xuu3uiSZH7uxg6r2tAjGjMVTSozk9RNrLqsLOU6U0+ZHr9Dzd4lYn+HcIHOVaeIHJ3D mDW8rGbMRAxWiHtIvhHBG8q1/F9tlmQhmJQ6QrY2q1gGepoyONUCioOdPfcYe8+3UYUkt0GmU/g xUwBwlE0CNP84HZzduw== X-Proofpoint-ORIG-GUID: rvUi3YTvGfaCJHqXq3HzbmpH0h2b3Q4P X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAyMDAwMyBTYWx0ZWRfX+v2DL0Ujpd5N m1k1y4voibyzkdu3q7emL8wMRML7vP9eu73s08qYY5J04yl1cnBGO2XQ8B0mXrQmmF/W8eXo2lr Xocu7wRFEw53tA56rYoQg0jUNQD5A9E= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_05,2026-12-01_01,2025-10-01_01 On Sun, Nov 29, 2026 at 01:05:12PM +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 > > 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. > + */ nit: I'm not sure a u32 can tear and I'm not sure I'd bother mentioning refactoring as a core concern here, but not worth respinning this to remove that part of the comment. And should probably be net-next, but again probably not worth the resend for these nits. Besides that, LGTM. Thanks! Reviewed-by: Bobby Eshleman > + 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 >