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=1796381097; b=y2H0y1GoG+TVs8reOdHCaDU+30jimleOM1DbtzyKFIhn6OKComjmmwRzHGlLNEjZdZkm IjPfP7ZhANvjqYnxo1laXsNqDTciVFljMDIuXjOSoxs6R9NU3DvsjwTmPis3+A8+drrSb I7SHqUOuxRWOveW/dC/G6P74mtaVekiDQv7ui/mTnxno0ItSfpXhj+vGqilkRflsJ+7XQ 3sLQS5fNaeWjxYZCpoFNl/BvjAhrrdBGW1WnEmGctQGttUZFLLMdi7yPmuBZc0YSCJbUm G+dbk7al83Z8cEFo8+3FeMqae6kFCX5VEhkpwiB1mUJzKHnFZGOZ+h4nOOVrcvevV7g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381097; 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=aEp4pRj7mr0TLj3YYhG9XSg6W5bbKANg39kmJQAVJaQ=; b=dkCMvADIaNFlqwtX9LaIkbjrHTXR4wzsx5c1xBigv6Xh58QwV1RV/TXgvQKeuKUnDM2B UncpD/5gsxNVJ62JEPi/d0o4hC188yAgAgW2Tm0OkNaLXBfUrzNGVHw+MVAt5/oSTmMsc O2MU6pSTz7yth/eHeps+JM2lwaAuouR3mj2YCAruLw7yR8vdbd+EIdJrvFX/QNg6LgKjh wXggoqbJvZECUC81yz52LV0KKFOqj97ckJV7rBe62sYB1M3eYpWJ7g6GU/hoGkKTn1cKn EC3KQvUDQeIVpKMoLFrAyMEfK6SCUjuCBkNsDpstPGWjWpIGaZeAJUIfj4zAuKsrltg== 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=IvBKoL1l; 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 27458606A322; Fri, 4 Dec 2026 10:44:57 +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=IvBKoL1l Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96DBC1F000C2; Fri, 4 Dec 2026 10:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381097; bh=aEp4pRj7mr0TLj3YYhG9XSg6W5bbKANg39kmJQAVJaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=IvBKoL1l2QKDOVihSJ4fGfSSMZZkv+uTTBE56q45w39glslmqHWvIWIlkFpAgOw5T mqQ/i35rO20zc+0V3IN2p1jeTSudwAVbOeLISrAF4Ytuy8kXlmMBd80tLQYp+wlkSe nRdsHvU8A+4jm+B3Bh9JbPX/F/wJUNwz4n5GdPZg= 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 7.3 530/742] KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized Date: Fri, 4 Dec 2026 19:06:34 +0900 Message-ID: <20261204100649.250340675@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@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 7.3-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 @@ -702,20 +702,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); @@ -862,21 +848,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);