From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796136678; b=4uW9drAIi1dTnc9Tgo56h8ZT0OdO0Pv+Yl7w98kiF5aEyxPNYBrGLaOpVvl4TBHN6B3F Dzy/8jcOEugenfMUtiooNep92NqviMGeiWcldHpLIcscEExUQR4AM9CjimgSFmJoO3s8O ozw/5zYTJtqMNgmCF/h7H/b7IIrM83zpEDDr9NW9E37NkaWMGl4w2RLtlT4AxAfxV+AFs sFi24CzDlKLPFZBvi3vmeL6X0OfMIhIMBry0/HughHGs8MUOytgUBkeF6J3htCJFNXhxk GB3gEtqXB5698g0wpvTYO+dPakCrnHhcVcSn3SKlXS4jVOtiR315HB8oKRcXzLX4l7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796136678; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=3CkRdn7pqmIUov4glZxvgilZLocm0oTDbNUGWK3g4t0=; b=4OMBV/xO3opycqlKcWQ+OqRStWAOqePyQzhzjPxUGJnILFfHNs5ONtohHDmYHsuzDSdS 48/UspngObwIYCiucEC1znuYtRhWgEC5s9WxNo67Ysj4SvKlc6Ilo6Qe5iHHruOWIRqkF 13syRZqaHlTXSnffFxU85M0Xb6FbCnthUxHHfTvMHUpGsC0aRRAL5JnC+57wl4FeSc2i4 mugpEjhEGRy22Pu2dBt6zBD/uAy8oHqWj5Zx+hrD1s6viYyn3NRRGMENG5UOvLn/eaEKA BPxvIPuOX7PyqdQ3fReJBN0c+ZZMbS6cas1d8Rm5KGqCBYe/HdX5nBfMJfqbVgoSQ3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gJ1S25OD; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=iCyvpADZ; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 8A823606A325 for ; Tue, 1 Dec 2026 14:51:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8A823606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gJ1S25OD; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=iCyvpADZ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796136676; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=3CkRdn7pqmIUov4glZxvgilZLocm0oTDbNUGWK3g4t0=; b=gJ1S25OD1a+b7nrudWBv0dEnj7q4pMsd0Avf+FADtFlMJvnG4LvDnBCNcD5XkxQqaqto+T yR8xClVvQuXCipiXoaaLFaOvfNmM6O9ygVoNfk3Y5dOPuuMQHgTPW9N9hs7xJ/5wwfUz6i WyTzBZ/ZkuIr/cMoqHfei9jt9gfyPUo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-600-soOd75t7PnCEdjD_s_bwAw-1; Tue, 1 Dec 2026 14:51:14 +0000 X-MC-Unique: soOd75t7PnCEdjD_s_bwAw-1 X-Mimecast-MFC-AGG-ID: soOd75t7PnCEdjD_s_bwAw_1796136673 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4bbc325f62cso13058005e9.2 for ; Tue, 01 Dec 2026 06:51:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796136673; x=1796741473; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=3CkRdn7pqmIUov4glZxvgilZLocm0oTDbNUGWK3g4t0=; b=iCyvpADZx/TAZ7GN5Mf/jR74p1rjpt/cPO0/LBPWvhZ2FOo+XDpUkCOR/HJS9esngK kHh2PtjVVwS7MqajZyRXM3PYQ5SDJp3v/2UKYIPAzEJ2ThBydtwd4tRMbBi2E0yRJbRQ IXW0IUjQbPF0CwdkB3IJusw5rU03FGl4SVFk58QVjK8U/Tu1NiXy1LVbciWGD1MG4WKw yTnf2jY0IQHd/QkNJTOFC8RhMFbjIwyHwcajT7d3q9NlDCvzhxJe5jcfoJRiEggDN0EM gPJ5Y80epA9ADLlG2ApUsQeia2mStLnxRXU1c1FLkO+86WmvtVYZWlJee6EK2ITi22aU AsyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796136673; x=1796741473; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3CkRdn7pqmIUov4glZxvgilZLocm0oTDbNUGWK3g4t0=; b=hwQ72vovT5/C0219mcaXW/XhBGhqo4JBZaKHctpYt0Hbd0+cA5jjvasL7tt/UwjXG4 sDPYOUG4B6QCaH4SpAGmLXbYgON1yQfmcRk+t4A2UWZa5G6PtQ+r0lRZ1xTZ9UTz8tAX KXQRtxK0AQxbjlFC5uH5B2e8Mi0Wm69kdWyZtBkVmwNJe4vGdFRU1oB/esG97GdmKdX6 QVyeywFhxV22KyrYz9a2IVAT6u9BVYJJLqQ5CfI4KiCco4qHcGWFF9TQVnHdswWvdOK6 /QzsA2ng8JT/Z0/FQDpFWQ66NbFyP/qlEvEhhejOf2anHwau57k4BzhcKQeyYdxB8zUf lF7A== X-Forwarded-Encrypted: i=1; AL+hErEbbRbgP/ptKBN+jPdTWlO/NMyOcWGBXg5YBawdfXO3Vry8665IahfscbzyUGnz3atiOUDAA08=@vger.kernel.org X-Gm-Message-State: APuU4Z/axWQC8zxxerASMp6lqhruFb06Z6e7rFwLT27jMKRpLaG7ZrSE EC29DcmGMjxeZI9C9/Tft1zlrP9lvwk5EKIzHZbePET6vBN1L8fB2ZqONkjNV/gRKnYuIkf4+uk 6g9hS9ni/IPi8+fyHxuGII86UflW61JHmzKz/RKwxsDmVKqSuw8KiFOci4w== X-Gm-Gg: AZFdTZe7rp6ifEs6k2qYLoJMMwH4T5naphT/8pzqFde7vhmFQjYWm9OSTaqN8Y7eOit /mHIUYDv8aqx930LvijLf3VJQwxEvJGXf73tfQv7GzKyq6GYKjgb87FiJWhIiojYPbvQTR0Yt5V TxNIFOCmy+IuUEEAi6JKS/l4IYDdQ+7itQU43mJK01cWRkEcFcGJ6SDg2jF+RS+/KT4aLxsvT9y B9G3L/tudzROO6pwJhb79VkQdTodDnHwTGZF/18an6uwciMWqy852EFapsV8GCihUHO2GZQbIGh a84CpKg28bIYnxBZIIOV+iGSDvPxXfZ+ui682MR60rnOCpAzs+8E8GjKbTdw1+Hyp6yHzT0OMb0 Sr8IlondN+MK5iDuLimv98ITV0ZOR4Hfol0tcel7+DB0WZMoCQdCJeBYDaMSclwZXbDgcOgeS+N 0EvDI2LcWhfOwSBg== X-Received: by 2002:a05:600c:a317:b0:4bb:a5f1:fb56 with SMTP id 5b1f17b1804b1-4bba5f20897mr379859845e9.26.1796136672771; Tue, 01 Dec 2026 06:51:12 -0800 (PST) X-Received: by 2002:a05:600c:a317:b0:4bb:a5f1:fb56 with SMTP id 5b1f17b1804b1-4bba5f20897mr379859245e9.26.1796136672267; Tue, 01 Dec 2026 06:51:12 -0800 (PST) Received: from sgarzare-redhat (ip190-139-192-82.pool-bba.aruba.it. [82.192.139.190]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853ca452sm969084525e9.15.2026.12.01.06.51.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:51:10 -0800 (PST) Date: Tue, 1 Dec 2026 15:51:00 +0100 From: Stefano Garzarella To: Nguyen Dinh Phi Cc: Stefan Hajnoczi , Bobby Eshleman , "Michael S. Tsirkin" , Jason Wang , Eugenio =?utf-8?B?UMOpcmV6?= , Asias He , kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org 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: netdev@vger.kernel.org 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; format=flowed Content-Disposition: inline In-Reply-To: <20261129050514.1278990-1-phind.uet@gmail.com> 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. So should this be sent to net-next if it's not fixing an issue, but just as cleanup? Anyway, I'm not asking that for now, let's wait for net maintainers. The changes are okay: Reviewed-by: Stefano Garzarella > >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 > >