From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796203222; b=Pg6z7tNcN8xGGgrYxxm8KN3vi5ojnDRHfHrO8PY2thlZwp57Qn06SbmW+gPWB+NSUl+/ 7YWILMpjqWVTCfIDT7ebK34fIwqRWRu6GYVVDjUXj80lRPjH1Byz6jXTQPSikwts650W8 aQyJp92+mC8w6eg1q+0XG+hyp//ioJTNoSnWo2BiVc9vyuQ0xFhEQh74MhIQxmM9thdV+ CS98n4vkPDjZ5WykmONmzUontDA1Q/LKw3PMxXWkTfgFFQ7gvx3VwY5uZp2+qZPAKIkwN Gcyer6tmZK8UQ+ueX3Mua6f2ueXIfl6QnbPqAvZHn/kUT/BAk4asbDdy1NTZKXjWMdQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796203222; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=brOZv5SI49T3h4s5d6eVa693ftZ2QdBnGV3MaU86/gM=; b=Vy6LmC/CD8wfPgELcWF1cVxRAXsb/B2Sj25LLKdebDLJ9xDeEEaxN/pDsiOxr2z6a+mV QrhbxzCGczwrlVzbkRH5Xaz7JNEPUX9sPy+siSrucMgjwtsjrx2h30oAKNMsZVqJBv7WW eR/dky4yrY6apmmqbNjc6N6oq6Eo892tDzLlIcbQzi3YOWh0Uw6DtZ/tKICAEl5wYbTCs dBK+XWBsU66Z+qq9MMUbYhaBUdbKz7l7FtWBUagTe0AeOOMkUdNxX9k1N/Nyig3mM7La8 99N2ziONDILI+wszn9m6jD7DlzBO5l9tbWiAWfiUqCkEPqfNNjPe4h83nV1x9soElRA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=N3H7PbHW; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b="JC1icL/8"; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B13F606A325 for ; Wed, 2 Dec 2026 09:20:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7B13F606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=N3H7PbHW; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=JC1icL/8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796203220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=brOZv5SI49T3h4s5d6eVa693ftZ2QdBnGV3MaU86/gM=; b=N3H7PbHWgKVLbWbEYKVV1XDz1X2xWgdjlDesmdZDK22PqxtJnl6t7tR4owDttbLcrL9lKj PBKM9Zzm+H5+QKF4k04TojsuxXz7i0r/aqdF5dZXhRUGCMQfqDRMhRNTAdSs5zer6DlRrt dAsaCcOegrQzoDm6l9AwZdzflX6ijn4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-_Cqe-ytuMEmdV-WLCTsuzg-1; Wed, 2 Dec 2026 09:20:18 +0000 X-MC-Unique: _Cqe-ytuMEmdV-WLCTsuzg-1 X-Mimecast-MFC-AGG-ID: _Cqe-ytuMEmdV-WLCTsuzg_1796203217 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4bb818d8dcfso64138745e9.3 for ; Wed, 02 Dec 2026 01:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796203217; x=1796808017; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=brOZv5SI49T3h4s5d6eVa693ftZ2QdBnGV3MaU86/gM=; b=JC1icL/8OQxc45X31xNng7c09AhY71/lUu86I22YdcmhGPbifbko/77lMS7WbDf8MA vlZA/Oc5WZVKB3tbsvTIFyxrGRqCqhubDjEetbyTquUDwg8VCPET0foP+NG41Qn/NTcP /+XNGU56NhH4Imx1PqzXR/2MPuTdCP9ps/YFcAhUcaEjht7yaCk+G6MGdCbgmI2zwPJR mcW/6uVQiZ67a03IOgDGLa+aJQ9pCrcTlYc3nagAbYe77wt76pUktB8oBYLbNOrWC1Y3 qBwE9NRA+ArXXFcvEa+eIjxxqlOQU/Ck6Y+BQRLTw16x+m2OTdCU++g5PHE7F2I1N2Ge MwOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796203217; x=1796808017; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=brOZv5SI49T3h4s5d6eVa693ftZ2QdBnGV3MaU86/gM=; b=qVpFsKF64/ag7PuEc05oihEZSJKvG/t3Jv05bZ+Hngrn2C1jUP4k0FOup3HOF3cMYb C9wajYVG18UkJ69bRwNZT0NwLFun4jmNZdEo1xci276V+2TyOUZAHWnUBxdJaXH6hkSk f5GjkX8yVijIbO6LryKnNnqkQpIyV7o5uWBm6wRoBRF0jEZ70lGBz+7NixMmjQUO/dOW 1H8VF4mJ9VtgftFVOUzyIG0+9O+NA4WMm2WDFgUzmG2WVdC5ieZ8ff9DdzWE9j7jzM8d Z7Jfwl8tFWcGwGABvrsjx87s/B8ulqW533cpFP3ALSDDqL8SQfskomJPny4UbU6f3dLU f4dQ== X-Forwarded-Encrypted: i=1; AL+hErHVFuhDFRY7fdKwfgpUWwaYvQIpO2+C4cbR1uqfJkWny4q+bYZShqRVVgHMWui8S0MFzDsyDSVyOHo1Us8=@vger.kernel.org X-Gm-Message-State: APuU4Z/a699ziD4eHhylqsdG0t3Oh4rKRjWsnMP5NV96ZOUwoLQNzr3B /wNF/C9In+LJnT7JhEED+MnkBElJwy5XXIBJHE8wfnVq9TmonbtPXeEp38drkHIqO4G+PKRRprD qsZsl+o2pLc0O1T6zmtnbaBBFPp1OXN0W5Fx6EORP0GGX+caT43qkCjW4LWebL9Jz1A== X-Gm-Gg: AZFdTZcBkjwUOdSCOH6cQ7EK68BhLtCjlKRTHybLSCZKHIxC2GioJIEiOt6MRdgqZLC 0GzSq03bqEUZNHa/sEMxSBO+r0w3fDmdWlX1g4JpRW1KGIflnxRcMe9R/QUZedJT3Ft+dhZsilL 7jlH2GmwsQd569UZRClImxSh31VZyaLVGj67WXB6AUe03btLIEkBpuqUreHC60byvND+pi98i8C clR3XspQT2S8iNb6UrVH7FcDaI579qnuDvsvZPz+dK5RZ7BXPQesgzWLfMGbkMtfA1J3Lv5yEcc O9XsaDbBLHrhiz3s8euymvIHBtOk0tXg4K4anJyesiP/aiuwj9KBZTFN6mkU8rK1nirgZUdIOOD 365Hn4JQP/H4gQdpy2mjPpCr/+Hh2Hrp84n2XG5GLyQ== X-Received: by 2002:a05:600c:4f10:b0:4a1:88de:9e8b with SMTP id 5b1f17b1804b1-4bb88807909mr876468215e9.5.1796203216809; Wed, 02 Dec 2026 01:20:16 -0800 (PST) X-Received: by 2002:a05:600c:4f10:b0:4a1:88de:9e8b with SMTP id 5b1f17b1804b1-4bb88807909mr876467515e9.5.1796203216240; Wed, 02 Dec 2026 01:20:16 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:153a:4c00:6468:7acf:febf:a046]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906640a2sm666962485e9.0.2026.12.02.01.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 01:20:14 -0800 (PST) Date: Wed, 2 Dec 2026 04:20:12 -0500 From: "Michael S. Tsirkin" To: Stefano Garzarella Cc: Bilal Hashim , Stefan Hajnoczi , Jason Wang , Eugenio Perez , phind.uet@gmail.com, kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] vhost/vsock: wait for RCU grace period before rehashing CID Message-ID: <20261202041047-mutt-send-email-mst@kernel.org> References: <179602872770.13688.17108715679213800277@126.com> <179614298864.2872.3567081270371906851@126.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Dec 02, 2026 at 10:06:33AM +0100, Stefano Garzarella wrote: > On Wed, Dec 02, 2026 at 12:36:28AM +0800, Bilal Hashim wrote: > > 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 > > 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. > > > synchronize_rcu() after hash_del_rcu() before modifying vsock->guest_cid > > and re-inserting into vhost_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/sashiko-outbox-222286@kernel.org/ > no? > > > > > Fixes: 834e772c8db0 ("vhost/vsock: fix use-after-free in network stack callers") > > Cc: stable@vger.kernel.org > > Signed-off-by: Bilal Hashim > > --- > > 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(-) > > > > diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c > > index 88a3103..622b9cc 100644 > > --- a/drivers/vhost/vsock.c > > +++ b/drivers/vhost/vsock.c > > @@ -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(); > > We have vhost_vsock_mutex held here, is this okay? > > Stefano > > > + } > > > > 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. Signed-off-by: Michael S. Tsirkin --- diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 0cb87971d7491..28fa2220f2588 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #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);