From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383945; b=uzsYDzbB6bWfkpwAuqg4H/lhDs1A0kx8N/M73tqdLqweNAgwtCPWLXaSw+py33g0baD+ DN5ZzyEQa8hkgLlWklyNulVaLnsaV9fl0kMCqSVBzT0GO4iezlBYvQMb9rLjgnTPRvTfn 5mHvGG09rDziGVrC3//Pvz+DxBdCb1FV7vqhZgaUZic1DQCLGzTEy93xfLTeC+cn59m/y iqJ5MYzpgVSrKBsIbWI2kUeDC49r0CLBTiDXbrZKMIluM/z2hIogjegNlDKBKzwrDHE2V Znnj/8k41t6o1dg1/xysOMO6Jaw1NvAULrj4fvsNh+GoHy9SdYyNiQU+0Mhz+mJO2cQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383945; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=8LB04rlzFF00dAScTkHI20dx/0SWU+pZawIGef+Sv+c=; b=fKsVO0V7RruGVE4E3RPObAs1Smibo6llnilXykeZnycGpLPPbe/8+D0RjC6b8jLkLfZs n+5oz23feLoU+t28UGEMVD2uQUKy47mQQ67QxyTRdKVBQoSoC5+aXdqBdEx7Z7r0tFBUp H0I6zBpB9Zz6d8yLsr3MVWeHT1pXcwbRSvFnsAginOAzhE74a1ea/1zBW4zrJjBwzXccP +EJPm08tN1DToI9n+anpbkdw5U9T7FLvBnnImG40wfDpwxSlqkIo/tDQJhCK0JKgxJWS8 7CujCo4sCrK2RzszPzVXB4P0g/pMHSz0HR75eY+NYxMDvTtIy7jkimSZty3YSTgXa7A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=NWVIqbD7; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77D75606A32C; Fri, 4 Dec 2026 11:32:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=NWVIqbD7 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A35A71F00897; Fri, 4 Dec 2026 11:32:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383945; bh=8LB04rlzFF00dAScTkHI20dx/0SWU+pZawIGef+Sv+c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NWVIqbD7NhtzZbUUG1N5qUKkth9+XlS71yvNjE6fX9bVB/Nh7ybZjbxG8Rm6f5PUU BcwbrKNKODelR4IwOMVMtVuxPeZS95OAdiuzpoYhNAKEXUg1iZK3BlAYulQbZe8xpg f3q65rw6btgRoOU06DEPEoMwUMymYwa5VCdhZ2dc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Ritesh Harjani (IBM)" , Gautam Menghani , Madhavan Srinivasan Subject: [PATCH 6.12 415/555] KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized Date: Fri, 4 Dec 2026 19:07:38 +0900 Message-ID: <20261204100625.532248581@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Gautam Menghani commit d8c6ab0aeeca7cc2adfcf0ce1f796c6a86314ec0 upstream. Calls to idr_alloc() in __prealloc_nested() are not currently synchronized. According to the documentation [1], the caller should provide their own locking to prevent concurrent modifications to the idr to prevent bugs. __prealloc_nested() calls idr_alloc() with GFP_KERNEL which can sleep and mmu_lock is a spinlock. So idr_alloc() cannot be moved under mmu_lock. In such cases the idr Documentation says: If you need to take a lock while allocating a new ID from the IDR, you may need to pass a restrictive set of GFP flags, which can lead to the IDR being unable to allocate memory. To work around this, you can call idr_preload() before taking the lock, and then idr_preload_end() after the allocation. Hence preload the idr memory with idr_preload() before taking mmu_lock, which allows us to use idr_alloc(..., GFP_NOWAIT) under the spinlock. This also allows to kill __prealloc_nested() and __add_nested(), as they are not needed anymore with idr_preload(). [1]: lib/idr.c Fixes: c0f00a18e2a8 ("KVM: PPC: Book3S HV Nested: Change nested guest lookup to use idr") Cc: stable@vger.kernel.org # 5.19+ Co-developed-by: Ritesh Harjani (IBM) Signed-off-by: Ritesh Harjani (IBM) Signed-off-by: Gautam Menghani Signed-off-by: Madhavan Srinivasan Link: https://patch.msgid.link/20261012082922.218799-2-gautam@linux.ibm.com Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/kvm/book3s_hv_nested.c | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) --- a/arch/powerpc/kvm/book3s_hv_nested.c +++ b/arch/powerpc/kvm/book3s_hv_nested.c @@ -700,20 +700,6 @@ static struct kvm_nested_guest *__find_n return idr_find(&kvm->arch.kvm_nested_guest_idr, lpid); } -static bool __prealloc_nested(struct kvm *kvm, int lpid) -{ - if (idr_alloc(&kvm->arch.kvm_nested_guest_idr, - NULL, lpid, lpid + 1, GFP_KERNEL) != lpid) - return false; - return true; -} - -static void __add_nested(struct kvm *kvm, int lpid, struct kvm_nested_guest *gp) -{ - if (idr_replace(&kvm->arch.kvm_nested_guest_idr, gp, lpid)) - WARN_ON(1); -} - static void __remove_nested(struct kvm *kvm, int lpid) { idr_remove(&kvm->arch.kvm_nested_guest_idr, lpid); @@ -860,21 +846,21 @@ struct kvm_nested_guest *kvmhv_get_neste if (!newgp) return NULL; - if (!__prealloc_nested(kvm, l1_lpid)) { - kvmhv_release_nested(newgp); - return NULL; - } - + idr_preload(GFP_KERNEL); spin_lock(&kvm->mmu_lock); gp = __find_nested(kvm, l1_lpid); if (!gp) { - __add_nested(kvm, l1_lpid, newgp); - ++newgp->refcnt; - gp = newgp; - newgp = NULL; + if (idr_alloc(&kvm->arch.kvm_nested_guest_idr, newgp, + l1_lpid, l1_lpid + 1, GFP_NOWAIT) >= 0) { + ++newgp->refcnt; + gp = newgp; + newgp = NULL; + } } - ++gp->refcnt; + if (gp) + ++gp->refcnt; spin_unlock(&kvm->mmu_lock); + idr_preload_end(); if (newgp) kvmhv_release_nested(newgp);