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=EgnhyBjELlnUuGvA94zj6E8kIsL1CDgF7L7a4I3p6pbdHJSEA+Di8t16TNfcO1ZJMacg phJ9vZ3Fo2cvuiUjo2vW1Rxbl16b9fJQi8DKuie7eu2bc0KOSkzy3uexQhlQipbHQrCu7 Oi7mu2IP1pdUvJWwaTEduPh5dMruX0sC68sXDuuzl9KhpY/E88lbVYMe3qdm04Lf69Fj/ Qe0bgfa+9Rqvlag22RggPcd5Rz8Fqwn4hAM3vmUwD91W1OPR6Jmto659JqHkNbA9YwY4F oAjnC1kCSAPEtZresT0DPgShkY/Y1QLYpgH7zZa9avVamYanIChfE+Cn2ziA5/CfKeg== 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=ULTA3qRsA6NkJprQnZgSj+ZhqTxhCNLdf8F78uhki6Nnf59VzEj6QO1mAyVXoR2VUVux rZ7+9XczZEsnsyfHR4gV0js209KciQrDN71OGAzJhXXgZ/YID6oy/30egk8TkA/uebWWP O91qr2toKMNKyVhu5nnrkPCdm4uf0FW/kKXNXOgcyfZrY3DWs9COzImTw2fhUadBRSBkJ 9u7iK5Xk0ol+mqDrX1QcH/XVKdMWwSQYqhY45rjGStB8+vXWp4YIbiCWKRgRmHTDPFMbn gxSdHqW0D6d8UGB0IsPSQRc08NfVl7wWGbX1xelOX+gBRh4GEiCtfGPM1/iF6NG0ygg== 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=DWXGXpdi; 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 EF8A9606A329 for ; Tue, 1 Dec 2026 14:51:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF8A9606A329 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=DWXGXpdi; 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=1796136677; 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=DWXGXpdiB8P6KoA5JhpCmuMznkYDxEH/MdI/CJACjDjhfDcfwutG68xDrm44QZ11hspGDd zaW6Iq3mLLCI75lYsVQgyPoWALm/EKSzxj9NV5PI9EAU8QRR4aiPn32Pbs4yHGfVZDyDdu XZDKaz6gW0UXcdSAlTMANrMN0CCHL8g= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-490-sdEXCFngNKGietbDuAZYgA-1; Tue, 1 Dec 2026 14:51:14 +0000 X-MC-Unique: sdEXCFngNKGietbDuAZYgA-1 X-Mimecast-MFC-AGG-ID: sdEXCFngNKGietbDuAZYgA_1796136673 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a009f82bedso85202965e9.0 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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3REZqSH9e019 +h9/+mYJIcaF0BeQGG/w8quUiMBCwC31dnVgD6r4mYCSIjU1JEAVnDfyLHo6xekH8Qfk9PKNdYu 6C708uJP3M8s+dIQ== X-Received: by 2002:a05:600c:a317:b0:4bb:a5f1:fb56 with SMTP id 5b1f17b1804b1-4bba5f20897mr379859945e9.26.1796136672795; 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: kvm@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 > >