vhost/vsock: wait for RCU grace period before rehashing CID
Subsystems (6)
PATCH 1/1 vhost/vsock: wait for RCU grace period before rehashing CID
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.
--- drivers/vhost/vsock.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +11 -4
@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_ghash_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 vhreturn -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);
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] vhost/vsock: Fixes trailer cites initial driver commit instead of 834e772c8db0 --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] vhost/vsock: Fixes trailer cites initial driver commit instead of 834e772c8db0
--
commit 77ca38e1302ecde9680d0e62c90492c21e199dba
Author: Bilal Hashim <[email protected]>
Quoted 25 lines
vhost/vsock: wait for RCU grace period before rehashing CID
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.Fix this by returning early when the CID is unchanged, calling
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash, and annotating concurrent accesses tovsock->guest_cidwith WRITE_ONCE()/READ_ONCE().
Quoted 5 lines
Found by manual code inspection.
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")Signed-off-by Bilal Hashim <[email protected]>
[Severity: Low]
In 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko"), vhost_vsock instances were kept on a spinlock-protected list (vhost_vsock_list), and vhost_vsock_set_cid() only updated vsock->guest_cid under vhost_vsock_lock without unlinking or relinking any node. The RCU hashtable vhost_vsock_hash and the hash_del_rcu()/hash_add_rcu() rehash sequence in vhost_vsock_set_cid() were introduced later by 834e772c8db0 ("vhost/vsock:
fix use-after-free in network stack callers").
Should the Fixes tag be updated to:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Stefano Garzarella
maintainerQuoted 18 lines · Bilal Hashim
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.
Quoted 6 lines
Fix this by returning early when the CID is unchanged, calling
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash, and annotating concurrent accesses tovsock->guest_cidwith WRITE_ONCE()/READ_ONCE().Found by manual code inspection.
mmm, really, it seems related to https://lore.kernel.org/all/[email protected]/
no?
Also why mixing READ_ONCE/WRITE_ONCE in this patch?
FYI that is fixed by https://lore.kernel.org/all/[email protected]/
Please, sync with Nguyen about how to handle both patches. The best will be to have a single series, or just rebase this on top of the other patch that I guess is going to be merged.
Stefano
Quoted 50 lines
Fixes 433fc58e6bf2 ("VSOCK: Introduce vhost_vsock.ko")Signed-off-by Bilal Hashim <[email protected]>---
drivers/vhost/vsock.c| 15 +++++++++++----1 file changed, 11 insertions(+), 4 deletions(-)drivers/vhost/vsock.c +11
-4@@ -78,7 +78,7 @@ static struct vhost_vsock *vhost_vsock_ghash_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 vhreturn -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);
PATCH v2 1/1 vhost/vsock: wait for RCU grace period before rehashing CID
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 and calling synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid and re-inserting into vhost_vsock_hash.
Found by manual code inspection.
---
Changes in v2:
- Drop READ_ONCE()/WRITE_ONCE() changes on vsock->guest_cid, which are already addressed separately by Nguyen's patch (thanks to Stefano Garzarella for pointing this out).
- Update Fixes: tag to 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers"), which introduced vhost_vsock_hash.
--- drivers/vhost/vsock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/vsock.c +8 -1
@@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)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;hash_add_rcu (vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 22 lines
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.Fix this by returning early when the CID is unchanged and calling
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash.
Quoted 5 lines
Found by manual code inspection.
Fixes 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")Signed-off-by Bilal Hashim <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Michael S. Tsirkin
maintainerQuoted 22 lines · Bilal Hashim
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.Fix this by returning early when the CID is unchanged and calling
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash.
Quoted 5 lines
Found by manual code inspection.
Fixes 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")Signed-off-by Bilal Hashim <[email protected]>
Some process comments:
- pls include net/net-next [PATCH ...] to indicate the tree this is against
- send new versions as a new thread not as reply to the old one
Quoted 32 lines
---
Changes in v2:
- Drop READ_ONCE()/WRITE_ONCE() changes onvsock->guest_cid, which are already addressed separately by Nguyen's patch (thanks to Stefano Garzarella for pointing this out).
- Update Fixes: tag to834e772c8db0("vhost/vsock: fix use-after-free in network stack callers"), which introducedvhost_vsock_hash.--- drivers/vhost/vsock.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/vhost/vsock.c +8
-1@@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)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;hash_add_rcu (vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Stefano Garzarella
maintainerQuoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Bilal Hashim
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.Fix this by returning early when the CID is unchanged and calling
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash.Found by manual code inspection.
Quoted 8 lines
Quoted 3 lines
Fixes 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")Signed-off-by Bilal Hashim <[email protected]>Some process comments:
- pls includenet/net-next[PATCH ...] to indicate the tree this is against
- send new versions as a new thread not as reply to the old one
- answer reviewer comments/questions before posting a new version
Stefano
Stefano Garzarella
maintainerQuoted 18 lines · Bilal Hashim
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.
Quoted 1 line
Fix this by returning early when the CID is unchanged and calling
Is this another optimization to be fixed in another patch?
I mean why this 2 changes should be in a single patch?
Also, this maybe can be combined when we check other, I mean something like this:
mutex_lock(&vhost_vsock_mutex);
other = vhost_vsock_get(guest_cid, vsock->net);
if (other) {
int ret = other == vsock ? 0 : -EADDRINUSE;
mutex_unlock(&vhost_vsock_mutex);
return ret;
}But again, this should go in another patch IMHO.
Quoted 4 lines
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash.Found by manual code inspection.
Since you just ignored me on v1:
mmm, really, it seems related to https://lore.kernel.org/all/[email protected]/
no?
Quoted 22 lines
Fixes 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")Signed-off-by Bilal Hashim <[email protected]>---
Changes in v2:
- Drop READ_ONCE()/WRITE_ONCE() changes onvsock->guest_cid, which are already addressed separately by Nguyen's patch (thanks to Stefano Garzarella for pointing this out).
- Update Fixes: tag to834e772c8db0("vhost/vsock: fix use-after-free in network stack callers"), which introducedvhost_vsock_hash.
---drivers/vhost/vsock.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)drivers/vhost/vsock.c +0
-0@@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)return -EADDRINUSE ; }
drivers/vhost/vsock.c static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) · line 866
Quoted 10 lines
drivers/vhost/vsock.c +7
-1-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 ();
We have vhost_vsock_mutex held here, is this okay?
Stefano
Quoted 5 lines
drivers/vhost/vsock.c +1
-0+ } vsock->guest_cid = guest_cid;hash_add_rcu (vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Michael S. Tsirkin
maintainerQuoted 85 lines · Stefano Garzarella
Quoted 18 lines · Bilal Hashim
In
vhost_vsock_set_cid(), when avhost_vsockinstance already has a CID assigned,hash_del_rcu(&vsock->hash)is immediately followed byhash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid)without waiting for an RCU grace period.Reusing an
hlist_nodethat was just removed withhash_del_rcu()overwrites its next pointer while concurrent RCU readers invhost_vsock_get()(called fromvhost_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 activevhost_vsockinstances (for example causingvhost_vsock_reset_orphans()to reset healthy connections orvhost_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 viaVHOST_VSOCK_SET_GUEST_CID.Quoted 1 line
Fix this by returning early when the CID is unchanged and calling
Is this another optimization to be fixed in another patch?
I mean why this 2 changes should be in a single patch?
Also, this maybe can be combined when we check
other, I mean something like this:mutex_lock(&vhost_vsock_mutex); other = vhost_vsock_get(guest_cid, vsock->net); if (other) { int ret = other == vsock ? 0 : -EADDRINUSE; mutex_unlock(&vhost_vsock_mutex); return ret; }But again, this should go in another patch IMHO.
Quoted 4 lines
synchronize_rcu()afterhash_del_rcu()before modifyingvsock->guest_cidand re-inserting intovhost_vsock_hash.Found by manual code inspection.
Since you just ignored me on v1:
mmm, really, it seems related to
https://lore.kernel.org/all/[email protected]/
no?Quoted 22 lines
Fixes 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers")Signed-off-by Bilal Hashim <[email protected]>---
Changes in v2:
- Drop READ_ONCE()/WRITE_ONCE() changes onvsock->guest_cid, which are already addressed separately by Nguyen's patch (thanks to Stefano Garzarella for pointing this out).
- Update Fixes: tag to834e772c8db0("vhost/vsock: fix use-after-free in network stack callers"), which introducedvhost_vsock_hash.
---drivers/vhost/vsock.c| 9 ++++++++-1 file changed, 8 insertions(+), 1 deletion(-)drivers/vhost/vsock.c +0
-0@@ -856,8 +856,15 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)return -EADDRINUSE ; }drivers/vhost/vsock.c static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) · line 866
Quoted 10 lines
drivers/vhost/vsock.c +7
-1-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 ();We have
vhost_vsock_mutexheld here, is this okay?
Quoted 7 lines
Stefano
drivers/vhost/vsock.c static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) · line 870
Quoted 5 lines
drivers/vhost/vsock.c +1
-0+ } vsock->guest_cid = guest_cid;hash_add_rcu (vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Btw is it ok to hold srcu here? It's easily 20-100ms. does this happen on each vm boot? maybe just use nulls? E.g. below (untested)? Feel free to use.
---
drivers/vhost/vsock.c +31 -8
@@ -15,6 +15,8 @@#include <linux/virtio_vsock.h> #include <linux/vhost.h> #include <linux/hashtable.h> +#include <linux/list_nulls.h> +#include <linux/rculist_nulls.h> #include <net/af_vsock.h> #include "vhost.h"
@@ -43,7 +45,14 @@ enum {/* Used to track all the vhost_vsock instances on the system. */ static DEFINE_MUTEX (vhost_vsock_mutex); -static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8); +#define VHOST_VSOCK_HASH_BITS 8 +#define VHOST_VSOCK_HASH_SIZE (1 << VHOST_VSOCK_HASH_BITS) +static struct hlist_nulls_head vhost_vsock_hash[VHOST_VSOCK_HASH_SIZE ]; + +static inline u32 vhost_vsock_hash_key (u32 cid) +{ +return hash_min (cid,VHOST_VSOCK_HASH_BITS ); +}struct vhost_vsock {struct vhost_dev dev;
@@ -52,7 +61,7 @@ struct vhost_vsock {netns_tracker ns_tracker;/* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */ -struct hlist_node hash; +struct hlist_nulls_node hash;struct vhost_work send_pkt_work;struct sk_buff_head send_pkt_queue;/* host->guest pending packets */
@@ -75,9 +84,12 @@ static u32 vhost_transport_get_local_cid(void)static struct vhost_vsock *vhost_vsock_get (u32 guest_cid,struct net *net) {struct vhost_vsock *vsock; +struct hlist_nulls_node *node; +u32 bucket =vhost_vsock_hash_key (guest_cid); +struct hlist_nulls_head *head = &vhost_vsock_hash[bucket]; -hash_for_each_possible_rcu (vhost_vsock_hash, vsock, hash, guest_cid, -lockdep_is_held (&vhost_vsock_mutex)) { +begin: +hlist_nulls_for_each_entry_rcu (vsock, node, head, hash) {u32 other_cid = vsock->guest_cid;/* Skip instances that have no CID yet */
@@ -88,6 +100,13 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net) vsock_net_check_mode(net, vsock->net))return vsock; } +/* +* If we ended on a nulls marker for a different bucket, +* there was a concurrent move to another chain. +* Restart as in Documentation/RCU/rculist_nulls.rst. +*/ +if (get_nulls_value (node) != bucket) + goto begin;return NULL ; }
@@ -802,7 +821,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)mutex_lock (&vhost_vsock_mutex);if (vsock->guest_cid) -hash_del_rcu (&vsock->hash); +hlist_nulls_del_rcu (&vsock->hash);mutex_unlock (&vhost_vsock_mutex);/* Wait for other CPUs to finish using vsock */
@@ -859,10 +878,11 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid) }if (vsock->guest_cid) -hash_del_rcu (&vsock->hash); +hlist_nulls_del_rcu (&vsock->hash); vsock->guest_cid = guest_cid; -hash_add_rcu (vhost_vsock_hash, &vsock->hash, vsock->guest_cid); +hlist_nulls_add_head_rcu (&vsock->hash, + &vhost_vsock_hash[vhost_vsock_hash_key (vsock->guest_cid)]);mutex_unlock (&vhost_vsock_mutex);return 0 ;
@@ -1040,7 +1060,10 @@ static struct miscdevice vhost_vsock_misc = {static int __init vhost_vsock_init (void ) { -int ret; +int i, ret; + +for (i =0 ; i <VHOST_VSOCK_HASH_SIZE ; i++) +INIT_HLIST_NULLS_HEAD (&vhost_vsock_hash[i], i); ret =vsock_core_register (&vhost_transport.transport,VSOCK_TRANSPORT_F_H2G );