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=1796382490; b=ZIBIXn6Kqci7PTYv6OzwjCQQFDMJJ/m3J4NuSJr8W5kLumDNu+j1SFMLj/jVJawZfAE1 bLe25CD7/UL5/6RaS3W6ojVW5NtHspAQnOcZ2z/Diwp5iELaQHnhoajLh+hTpTm8IKHSL lsta0ZrKOK3JQuvMGJBk8OUTl534ZjV4L5f7hBPX3zYVYLhFYoSoTbCZLhImCL2P+PFpm V72/yVFrcZ8UCwx96T8CkwENNmDI0ls7CjzxlnsUcWZSd53Zi/UM3MeUIvwWgima6lRY5 FYXJ31cWUariDJNSUwJZUe/CEssldUvlFMkvJ+SYpUdSklqnSQ3jtZoA9bZtmPpmsdg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382490; 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=SELRXF3zzH26aY+WrPeraHrucwXSlgmLrHLbDhS3AjI=; b=hbBpVTKk5nYBnAoNDNqE8udvNz+V6n3Fdx4Z9si1igXdFzMyAP4qcH9HOPR3cWiSsarj Lnn0R/h/VoDG9rg7rGjsMWcEynRDpP71TTTIhjyfu4cBzNmiabIfnDylDT+oomiAsHJLo HWNxSI0VtPRV6PD2CnRvYM8Oh4SsdoS+o1Rx0aT+JLIRJMfA26ch7ZjH1Rna0i/G8iNyk qHNUdP//NWL3TzWrzKoedDBGTb3gHyIkZ9ILMVDpx8E14joL24/3aeENr8qlI/65dm5MZ WVOKuA30wwcLn4P8UJzAsoPecLHmfAcWkxtw2CfhiT/PRpwCFIme2RdK/pCHzH6GdMQ== 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="yPqS/0PQ"; 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 B81A6606A32C; Fri, 4 Dec 2026 11:08:10 +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=yPqS/0PQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 347641F00931; Fri, 4 Dec 2026 11:08:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382490; bh=SELRXF3zzH26aY+WrPeraHrucwXSlgmLrHLbDhS3AjI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=yPqS/0PQ0dEMMZ7un+Wgm/0CBUSQCB9RO+S8QJz9ImirbZXPreQhwLilTEjCunrpI +ve/rpxJsxkxwHdnF1bRTbKlC9GOnjzz2IMpRVFPMKGolFp7gn60wbYTkaR9eai1pU PSsxPj2+kt9RfPvViphgYcopwrCKkzkWA+XZ3RLM= 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.18 477/703] KVM: PPC: Book3S HV: Ensure that calls to idr_alloc are synchronized Date: Fri, 4 Dec 2026 19:06:16 +0900 Message-ID: <20261204100640.490539049@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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.18-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);