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

From: Krzysztof Woś

Date: Thu Dec 03 2026 - 08:42:07 EST


On Thu, Dec 03, 2026 at 11:43:29AM +0100, Stefano Garzarella wrote:
> 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?
>
> Please test those patches already posted, and comment on them if you
> think they are not complete.

Hi Stefano,

My sincere apologies for the duplicate submission and for bundling
multiple changes into a single commit. This is one of my first kernel
submissions: I checked torvalds/linux HEAD (d06ce6f1eb5e) without
checking net-next and lore.kernel.org/netdev first, and I should also
have split distinct changes into separate commits. Going forward I will
always check net-next and lore/Patchwork before posting.

Please drop/ignore this patch in Patchwork in favor of Nguyen Dinh Phi's
merged net-next commit c68d8b4cb2e7 ("vhost/vsock: annotate lockless
accesses to guest_cid") and Bilal Hashim's v2 thread
(179614298864.2872.3567081270371906851@xxxxxxx).

Looking through Bilal's v2 and Michael's hlist_nulls follow-up there,
two small notes in case they help that discussion:

1. On Michael's question whether synchronize_rcu() runs on every VM
boot: on initial VM boot vsock->guest_cid is 0 (kzalloc'd in
vhost_vsock_dev_open()), so synchronize_rcu() is skipped and only
triggers if user space calls VHOST_VSOCK_SET_GUEST_CID again to
reassign an already-configured fd. Still, holding global
vhost_vsock_mutex across synchronize_rcu() blocks unrelated VMs'
vhost_vsock_dev_release() and SET_GUEST_CID calls during that grace
period, and leaving vsock->guest_cid non-zero across synchronize_rcu()
lets vhost_vsock_handle_tx_kick() keep accepting TX packets with the
unlinked CID until guest_cid is updated.

2. On Michael's hlist_nulls sketch: get_nulls_value(node) != bucket
catches cross-bucket moves at end-of-chain, without needing
synchronize_rcu(), if other == vsock returns early. One subtlety is
same-bucket CID reassignment (e.g. CID 3 -> 259, both hashing to the
same 8-bit bucket): if two instances in the same bucket are
repeatedly re-added at head via hlist_nulls_add_head_rcu() without a
grace period, a concurrent RCU reader positioned on them can bounce
between the two nodes without reaching the nulls marker. Also,
hlist_nulls_add_head_rcu() updates n->next via WRITE_ONCE() before
publishing head->first, so rehashing an existing live node (unlike a
freshly allocated SLAB_TYPESAFE_BY_RCU node) needs smp_wmb() between
updating guest_cid and overwriting n->next.

Thanks, and sorry again for the noise,
Krzysztof Woś