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=1796389361; b=yz9Z1Bap32ZZgZUcs2FdDO14YnKP5OGDfucyWOjR85pVtYTr5ONt4pUMOMzVb0455C31 3muA7Eg96DyK1ZiuLWIHEyRu4nSPjjNCWTGMXAqTIvQnqFpxRiNAtJQxt1oBT/Tz3w6vU am/2N4B3cYBlBGyslKfHcqQ3s2Hil56ftAaIiwvuIIA70qcMLbvdvHJ2gr+9JZVWpe+8z aRjkhqac7nEzX9Y9nqwCCVR9EtoVxjUj6BOyNU+TjpHWjxs1Ln2Es7pUePAq6e/d2kANB hYVT2E0PtRJllE8qk3+HByqrmkD8qXYv+mbAn5EoFJ5O6LCTJUKfi37h3KDPvlcB/0A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389361; 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=zjR8EhfrouOCJqoXPkNnWTDz2KAFeApUyzS6tzFvKcM=; b=nb1XL3PFRbFqZyPpyknfMLrYePOZkdKhFgM5+q1Znx4DYyPb+7Xp+Idu9o4FLuBlIB4g 9VAkQw0RImnVUW7PqUHCr99mOpaqTmrBhyfYuqBcvRHDfbWGB363ReoOZA/UtV19pGMXC vLU/AuK/eGPlPoytTIdNlBB864fzelv0DSHMvVR8m4NUwQot7E8OD9g5xUFNSaW0JyluB gCm3v7pBoobIsC8a0WgcpErVwSsdApAHYrtQbro5GoXhs73dhYwK3+tDSknVThkuDTkU8 l3Ak7tTqlyqseG0SCRnoKIw5Uiignu01W9ZRf5IIxcqB7oZ4NmhG8qo1FAD+TGVJx3A== 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="1yuyQ/xl"; 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 1BF21606A322; Fri, 4 Dec 2026 13:02:41 +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=1yuyQ/xl Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6640D1F000CE; Fri, 4 Dec 2026 13:02:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796389360; bh=zjR8EhfrouOCJqoXPkNnWTDz2KAFeApUyzS6tzFvKcM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=1yuyQ/xl7WfXL55jPiDrKHGdhD7rVdWwg58XGChv+8q/uGSPiq80C5jXiASPTGNL3 LAKILOGIL77eDpFjCeCTS2/jEvlndkt0cR8xfTapxj/KaDVcmcBHwsQq4R5fv8+oLs YAcMvs6Rt3Im4hhlEyYdP7T5KXmy3v/0nXrGzyok= 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.1 0962/1059] KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized Date: Fri, 4 Dec 2026 19:08:13 +0900 Message-ID: <20261204100708.936043067@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-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 @@ -670,20 +670,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); @@ -830,21 +816,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);