Re: [PATCH] vhost/vsock: wait for RCU grace period before rehashing guest_cid

From: Stefano Garzarella

Date: Thu Dec 03 2026 - 05:43:51 EST


On Thu, Dec 03, 2026 at 06:22:39AM +0000, Krzysztof Woś wrote:
vhost_vsock_set_cid() (drivers/vhost/vsock.c:832-867) updates a struct
vhost_vsock's guest_cid and re-inserts its embedded struct hlist_node
hash into the global 256-bucket RCU hash table vhost_vsock_hash
(DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8), line 46) by calling
hash_del_rcu(&vsock->hash) followed immediately by
hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid) with no
intervening synchronize_rcu() grace period.

In addition:

1. No early return when other == vsock: Re-invoking
VHOST_VSOCK_SET_GUEST_CID with the device's existing CID falls
through to hash_del_rcu() + hash_add_rcu() on the same bucket without
a grace period.
2. Unsynchronized plain accesses to vsock->guest_cid:
vhost_vsock_set_cid() performs a plain store (vsock->guest_cid =
guest_cid;, line 862) while vhost_vsock_get() (line 81, under
rcu_read_lock()) and vhost_vsock_handle_tx_kick() (line 575, under
vq->mutex only) perform plain loads without READ_ONCE()/WRITE_ONCE().
(By contrast, vsock->ever_started in the same file already uses
READ_ONCE() at line 311 and WRITE_ONCE() at line 640.)

Overwriting vsock->hash.next via hash_add_rcu() before active RCU
readers have exited their read-side critical sections violates the core
hlist_del_rcu() contract: readers currently positioned on the rehashed
node are silently redirected into the destination bucket (or back to the
head of the same bucket). As a result, concurrent lockless lookups for
unrelated live VMs sharing the same 8-bit hash bucket can falsely return
NULL—causing dropped packets (-ENODEV), missed cancel_pkt purges,
erroneous ECONNRESET teardown of healthy VMs' sockets in
vhost_vsock_reset_orphans(), or repeated/looping traversal within an
rcu_read_lock() critical section.

Fix summary:

- Return 0 immediately when other == vsock (eliminates redundant
same-CID rehash).
- Serialize per-device CID updates with vsock->dev.mutex.
- When reassigning an already-hashed vsock (vsock->guest_cid != 0):
unlink via hash_del_rcu(), clear vsock->guest_cid to 0 (WRITE_ONCE),
release vhost_vsock_mutex, call synchronize_rcu(), re-check
vsock_find_cid(guest_cid) and (under vhost_vsock_mutex)
vhost_vsock_get(guest_cid, vsock->net) before re-inserting. Clearing
vsock->guest_cid to 0 while unlinked ensures that if the re-check
loses a race (-EADDRINUSE), vhost_vsock_dev_release() will not
double-delete &vsock->hash.
- Use READ_ONCE()/WRITE_ONCE() for vsock->guest_cid on lockless
read/write paths.

1 commit for all of those fixes?

BTW it seems your LLMs are founding the same issue in the last weeks, so, please, before posting a patch, can you check if there are patches fixing the same issue?

https://lore.kernel.org/netdev/20261129050514.1278990-1-phind.uet@xxxxxxxxx/
https://lore.kernel.org/netdev/179602872770.13688.17108715679213800277@xxxxxxx/

Please test those patches already posted, and comment on them if you think they are not complete.

Stefano