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=1796202432; b=PqoyYzVC1C9pMVeU8sec8RZea9xaI9NxnZlW4aZ4Gl9ovo4+V2eImVTs1bZApeFRcE4N wsV8jZj4IFsh0BlHa4lFy2DVrUfwJbR+M9o/4QdfXbzpIq+OPwnVjQ4sgm0SKMNMcSJ6s lQc0/UUW6dCGDja0P3NyECKNipL/IVdTA7DI4j7WGq/gNuZ2/dsUl5oSSVhgiNuqsMJoJ HcE1eltMnEShVvd3RnAF2S6iIg6TuH0Kfl4C1/PjW3x6Wt08CUgE8AtkeegjDRijURP+R w8Wkgdl+31vi6cT9y7MZ6KBIzQ0/oHg2EGe8+ODJiP5twMKBOKdHMvvk/wVyA8oz2RA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796202432; 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=58+0OR09zcy3kXqZHHhq3ZZaT6fRes2GGWeXUfu+EJ4=; b=khpyPfU+1JBSoJ3cAn7jTjEz0FwV3B5Wq8tGC72f6sMvCHxNGUQVVdCYeT6OXk005POA akA3dI1xeazOEbZpxpDrRGMu3fijmwItxWm3FXYlA/EWN9c5DO3XUiz0M8BKJ2409XKFx fLOhBhinHy8yL6wei3vlTSih4/1Crr71Q0M3tZhWzwIATbmJws4Nd2u3odV6AS9h07Q4D TTdgxKUU/lFI1iLMq4j1TRQkn+goJIL+X9b0cgJkD0XE0FRDlJlLgzPewUhZF0/KqY5Mn zPzmIrWzZazMhOO/7ySfJYDXLae8oB51rXVIEPcMbB3ljj31xU7SeNx/c9XgQ5wQuaA== 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=AauPcS4Z; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=YxZ6vMLj; 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 23F58606A322 for ; Wed, 2 Dec 2026 09:07:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 23F58606A322 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=AauPcS4Z; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=YxZ6vMLj DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796202431; 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=58+0OR09zcy3kXqZHHhq3ZZaT6fRes2GGWeXUfu+EJ4=; b=AauPcS4ZiTwjNdSamsrwS3ByLfjXSzKgJrto7rVg3vwhmAv5xz0o4H4+1li2fCEKRVD+3n IHYYahTHxjlwjd/mvg2T5TGBmrFCoz7Nop2vlcmGiFBx6aCjmT64dzUyYZXeiRsrx1gR43 ke94375ZhU1FXovP0FORGp5YpwLEH1I= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-528-13ySTnI0OzCKoPTlfwXKug-1; Wed, 2 Dec 2026 09:06:42 +0000 X-MC-Unique: 13ySTnI0OzCKoPTlfwXKug-1 X-Mimecast-MFC-AGG-ID: 13ySTnI0OzCKoPTlfwXKug_1796202401 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-48e0461e040so10812096f8f.0 for ; Wed, 02 Dec 2026 01:06:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796202401; x=1796807201; 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=58+0OR09zcy3kXqZHHhq3ZZaT6fRes2GGWeXUfu+EJ4=; b=YxZ6vMLj4UZfijm1tf6SSFdpBZaJMtqK9Hn620geCvPIyEwRenDzZ+PXE2oeLFAz87 ofLT8GQVmfN4wpWEY11gfk09FXxwIf7PHUJBE8IhbMjTWAjoYgPzb4ucG1hAjj22Y+1G CiIxRlHacdFPH+H4Db4I/KpuO8i3EZKmXiqbYb9FPIev+F0sc99pbbbO8CRqHHkw39WX 0ny49Pfy5e9mVR2PJaEuXdGCavEQhXXK46zK7r1OKpB5vja+9m2hn5mfTuco+aMT9uNN nvJXEZE0PHH/Ko8fUxy1onOZi/oRnkFv0kkuG1mPlq4Ydb/yIAM8o/32f/0vPaBfXIRR 5a6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796202401; x=1796807201; 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=58+0OR09zcy3kXqZHHhq3ZZaT6fRes2GGWeXUfu+EJ4=; b=WHrpsUaQbASSQyxclfTeCJHWyD+24W2A87mXNl8V3yZSNoCTgP3vs132WOMCWrFzsU qD+S2L0WKBw04AaPcbeTd/yrUdmEFfHtQcRsqh7F/rruAbjASZnaYIg1+/QXcCz72UnC aK1nETudByVuw0s+KufeMmx+l2q8F298f4p1Vds4rRcA3IMx+z07CbbT/k0l9mXhqHYB PZnIB63o2S1brAygaMexgbHqGCDTBHUkqbP4CbAoi8M4NEuAZFNUjndhA+m8XnVd25rY ZAvwaLE1FW5IFR9h6KYlMukmXiW3IWLiYkaYIsCPlSlUS0YaK0mLECp6F+Q+uoKt6qvp JTMg== X-Forwarded-Encrypted: i=1; AL+hErExGo1T6fTMdz+yzgLtJ70Qs6R2bRln22DVzAiChLcbvu0WJUFMX/Bqxc2y+E51oXQiKvWmMBGWnZTMe8U=@vger.kernel.org X-Gm-Message-State: APuU4Z9vLv5ogvbP89YyC1uo+6tRN3KwL7Z+JVVnW0qw0HYg+Sfvve6I 5JuHfXANI0tvrT+N/d/otME+8ajbbw61YDLdHDTJgvSmu+jYSWr2S8NuztZW6v/p0s2Dtzh6LX6 iOSmsU2lpmPcDiblG/53Wk/+mlAtUVgujJaKTVBpfxgFxVZcHuS50TANKZ/rlOt+wJWFTTZMzTg == X-Gm-Gg: AZFdTZdHX2TDgdoKc7Vfg99LOn28y4q27Z7qjfnqvBEC3LVX6R3el1HovDyrFBD8v01 k7ujvU7XZTU0azpkgM9jHQMlqIXOPxlv1VgyBW1JyfwT1cORwvnLYYHKp3BVozb7Pl71NXXAcUv 9bHcCV0uld4XfGcvo6AbX+YHn6/rM7VT4vWuNrbb9f5l3iBb/Or0jUoeyUqPTb6j4PZvIbImH6V zUORazAi7v2haZP2VslnDg9f3PdwRj98+8WYvUnYZ9mjDD046EiFOuFXHG992FcPEa4gd7txTWg JGuFmL3ueSKSPoj7qUdEfqHigcQWI33CfdpZZiTUcfrsyscvQB0oXJO11oW6gnMzBYb12aPaO7L c1EdLe5+IpLqxYhNsQlnX226HJyGOS8QVBvg/hXuQcmEv9wdv5VXKspyqN2OuwdWesY0VrRHZok sYcdWrznFeUjisSA== X-Received: by 2002:a05:6000:402b:b0:4a4:f86e:d97 with SMTP id ffacd0b85a97d-4abaa976a91mr73060543f8f.1.1796202400920; Wed, 02 Dec 2026 01:06:40 -0800 (PST) X-Received: by 2002:a05:6000:402b:b0:4a4:f86e:d97 with SMTP id ffacd0b85a97d-4abaa976a91mr73060509f8f.1.1796202400337; Wed, 02 Dec 2026 01:06:40 -0800 (PST) Received: from sgarzare-redhat (ip190-139-192-82.pool-bba.aruba.it. [82.192.139.190]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b10b2c9sm78356058f8f.31.2026.12.02.01.06.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 01:06:39 -0800 (PST) Date: Wed, 2 Dec 2026 10:06:33 +0100 From: Stefano Garzarella To: Bilal Hashim Cc: Stefan Hajnoczi , "Michael S. Tsirkin" , 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: 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; format=flowed Content-Disposition: inline In-Reply-To: <179614298864.2872.3567081270371906851@126.com> 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); >