Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795928768; b=IgRH5SvtgednD6orOm4zgVdK+dP+mWK/D3bEZdlNyTLiLMyJEN/sdzABGDHs52f+RsPj iwSqFmZF8fvrV9TZriykPrwHSuqZaRCq2vcFdG9ae4h9wT0yPplQCu/zr/Jwu6wiqNo3o FbEACyRkyp8v1PVpYRUk+jP5bCxY11NR9qKAoY0CXQsNxVUggv8gJshe82O/N+ERKy7wE Fbgbd4aaSfxkbQv28Hx8ktNkm6cVvMhWUNb1K8oMJFcWzuSZApeyofCGodtJPbkhqWc+F lIjPfOq/G5MJhBw9aaW8mi6anYD5192/jSC7frDEWvP0UCVdRkwhA5XPIpyhmTMLCjQ== 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=w98IfQK0d2VHoN7iXXkEXp0zMNLuuIqjiMsbEZiJrGdTiV1I8m2WWN4fLKM6oqUmEEVJ aSeBLO/RYIY5DBqaHyAyInbLZ7PhUkVI5hZGg5Bp4TbEnFCczZsNpxhYdIxYVlZNTjo6N HE1W7z3olb8xKvQu4mMe1ZCkM6kCgig01c3kdvACrEOAlk2zyBc78z9htcLKrYaToMznT 2QlEW6EYI5fe0u5qQ10uPTbrW1vBWiulir48YLaGDldRB0LYRgVxWC7bszVijfTsEAMFc n9FJcBJgs5oSNXDf7JRgC0i7gL0ZlNyF9gu3XdaeTTMa+QjALYVnE+oxSNuEWyHZitQ== 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.169 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 B441B606A31C for ; Sun, 29 Nov 2026 05:06:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B441B606A31C 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=MS3DBnAX Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2f07a960666so70230705ad.1 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=F1C7zaZGTAUU9R0dvoFe1NkFghYZ/lNHdtAy0ot1uckqssQpsDAOTFKq9jQ1tGC4HD kXsX4Vtt+PfYHX22raLbpKL5WiiQ/yp7pIDlHaLLaTF0qEX2/dUffdGSO6MjjfjtwVXM 1aNmU4DYgyqpKrIsGqvhYbjv3LcMA8wX+mgeO0Tp6vdnsKIlwg2xNqJLse6HHXhvSmhY qyzDUD5SWMfnYQ3WkjscyKJoZr8d+T3I+2VlUgAYERdzSDucuLsts01KnGJ6GvnrsHRz w7qkUT1EMXm27wKUQWRdMq8oqQxHKlxeSWsA2gNqbC/MPBl576GSDRauRy8brKuYWB2c xOhw== X-Forwarded-Encrypted: i=1; AL+hErHx7sWx0gK/olQ72/K8LE9DuoBqajISWUKgydCvJCyGvroVGrCaEFxz0p0jmg855nZkfGw=@vger.kernel.org X-Gm-Message-State: APuU4Z8Bsj4bhE4KZixWiIXdkpWYWtS6jM7EAFke21Xsx46abfKq8SUv huezXfzNfktaRNHKyIEf1UwA4CrlmW5ZK794x9cUYsLa+QIdPTmYt+I/ X-Gm-Gg: AZFdTZfPNQOf8YunjbXCnIDa0ChosfP4wAhi2cnaDXhp07NlW3WphPg9fPgPnHtvBGu s/CLAGDodpV1jb7DO/HlIE+Iu9U0MAfablu3b6v1sMkc3su1K9BmzLI225xkeZm9gHc4D6ly8hL gjeGPY9x2yl0dYf28sPBeS5XYR2h5Nwbm68FzmNd6vZxY6UvGjGhXPgKDYwD+Y4dQrOdRLYrEgB OaAWo9XMk2Df6e3jGkxJoWwxg9V4XgZeVRA4AZ+dN/mcabMVo0c5WJvHHc30qDvPPy3swR62mzW Qws9GxriRsNgS5X81ecEJE/Agdbq2HIsUXM244EO2Q77VmMq6GRKlAe6umXpxwi9OImQE65Cs9S RLz5LJhJjbl9G3A+U9FlKyvE+82s7QgXLOuprxjRXfNOIHfdE5q/6n5VuRTr2yzmzHemh1ykBPP kV58AMAD4OVUTT+auxjKrcrIW0rQKFJsGPyALLhAjQZ2t97MFPai3m2n2POpWMLm9ZVgjRZQC3Q 5sw5egs4gE29s3fSyZhu5XEqkhNn0f6/ikl/uXLY8broVLsPEzoU5pIOp9pTFPDn5uSGsVbew+8 DMxRZ1NNarphHcaRPQJTaaR15wWaJy4= 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: kvm@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