Netdev List
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Stefan Hajnoczi <stefanha@redhat.com>,
	Stefano Garzarella <sgarzare@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowangio@gmail.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID
Date: Mon, 30 Nov 2026 16:52:07 +0800	[thread overview]
Message-ID: <179602872770.13688.17108715679213800277@126.com> (raw)

In vhost_vsock_set_cid(), when a vhost_vsock instance already has a CID
assigned, hash_del_rcu(&vsock->hash) is immediately followed by
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) without
waiting for an RCU grace period.

Reusing an hlist_node that was just removed with hash_del_rcu() overwrites
its next pointer while concurrent RCU readers in vhost_vsock_get()
(called from vhost_transport_send_pkt(), vhost_transport_cancel_pkt(),
vhost_vsock_reset_orphans(), etc.) may still be traversing that bucket.
This can cause RCU readers to:
- jump to another hash bucket or terminate early, missing unrelated active
  vhost_vsock instances (for example causing vhost_vsock_reset_orphans()
  to reset healthy connections or vhost_transport_send_pkt() to return
  -ENODEV); or
- loop indefinitely inside an RCU read-side critical section (RCU stall /
  soft lockup) when two instances in the same bucket are repeatedly
  rehashed via VHOST_VSOCK_SET_GUEST_CID.

Fix this by returning early when the CID is unchanged, calling
synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid
and re-inserting into vhost_vsock_hash, and annotating concurrent accesses
to vsock->guest_cid with WRITE_ONCE()/READ_ONCE().

Found by manual code inspection.

Fixes: 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 drivers/vhost/vsock.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_g
 
 	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(s
 		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,
@@ -856,10 +856,17 @@ static int vhost_vsock_set_cid(struct vh
 		return -EADDRINUSE;
 	}
 
-	if (vsock->guest_cid)
+	if (vsock->guest_cid == guest_cid) {
+		mutex_unlock(&vhost_vsock_mutex);
+		return 0;
+	}
+
+	if (vsock->guest_cid) {
 		hash_del_rcu(&vsock->hash);
+		synchronize_rcu();
+	}
 
-	vsock->guest_cid = guest_cid;
+	WRITE_ONCE(vsock->guest_cid, guest_cid);
 	hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
 	mutex_unlock(&vhost_vsock_mutex);
 


             reply	other threads:[~2026-11-30  8:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:52 Bilal Hashim [this message]
2026-12-01 14:56 ` [PATCH] vhost/vsock: wait for RCU grace period before rehashing CID Stefano Garzarella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179602872770.13688.17108715679213800277@126.com \
    --to=aeesec@126.com \
    --cc=eperezma@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox