From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 425A44534B9; Thu, 7 May 2026 20:22:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778185369; cv=none; b=a1aeXN38Pdztl0xU3Vg+dXFCIs6Lx8uwz6IuayLhMliqkpelVFhbK1vxeHXt2O2JazUKCyK/iRHQAG8l7iA+ZEfCXGh3BfwYpdDovT0TXpgOueAE+yDHeo6LMWZv0FQ7ecc+6QBiEtSbyzFoTen3puDS4JSaR/ymP0lHZJuPLDA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778185369; c=relaxed/simple; bh=3Tv8SP7AnWASbuC11+vKkTBpZCOcHskSD+AA8VifsNE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ULEDPI5mR4ssPAThpE+S1PNiUA3KGu1mP19GSLGzVyO26Hk0fwLG29AvgAcdUvrS7EU5AjAIwOi5ZwGZ5O1+qpl7QH7yI5kpYgjl0AlyLCCM352MC6qrmyGSOPX92A1mBizF8a5uO4yypm0JTh4CpLNQUKJcMHQKyN5GBEuQ8k0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mesFkbnZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mesFkbnZ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 21227C2BCFF; Thu, 7 May 2026 20:22:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778185369; bh=3Tv8SP7AnWASbuC11+vKkTBpZCOcHskSD+AA8VifsNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=mesFkbnZAQuzWOKn0YAKeg+kNk95QR6MHiqkqL2v8xwB01b/5s+iATr00O328L9T5 oHiMnQD+gBEKwrrSlnnFsCS/dfCJNRNd3TaPwwXlMZmfK3nc1hEDDw4/47gnpVuaaT RbAetGbtYXfj/gkoC7Sz1wmc2EDFKWD+h6O/EwOlq59rp52/Imn1xsPR5JVPPjA8cl 67dZTb+Rv5t6Hc8MeBYDPuCx25IufB1+1oMH29TuP25bejpehzuXgaOfKJerUVOtux uKv3OUo/eoeilV87magdwm1MiJ9cNRtJEmR+4kiJJpwCuKvjyn0tFxf1+3E03CXMGT gYrUy1zKdSjNw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F5F0CD379F; Thu, 7 May 2026 20:22:49 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Thu, 07 May 2026 13:22:25 -0700 Subject: [PATCH v6 06/43] KVM: x86/mmu: Bug the VM if gmem attributes are queried to determine max mapping level Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260507-gmem-inplace-conversion-v6-6-91ab5a8b19a4@google.com> References: <20260507-gmem-inplace-conversion-v6-0-91ab5a8b19a4@google.com> In-Reply-To: <20260507-gmem-inplace-conversion-v6-0-91ab5a8b19a4@google.com> To: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, liam@infradead.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Jason Gunthorpe , Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, Ackerley Tng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778185365; l=1802; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=zSMKUjOxxa82tr34hHB/lIyi0dgSFlYMksc47el8QGc=; b=hJ896vjTW0uUAF9uO3rBIOiH5NHAk0DL09eR9y0euiFWur9ijMeWBDnfJ2NtS5VCQ0U7KoL+q r6BoruioCdBDfuBf5VaYFPrfITfiOTnp6SwjOLdc8yMs137ID9Bj4ms X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Endpoint-Received: by B4 Relay for ackerleytng@google.com/20260225 with auth_id=649 X-Original-From: Ackerley Tng Reply-To: ackerleytng@google.com From: Ackerley Tng When the maximum mapping level is queried, KVM's MMU lock is held, and while the MMU lock is held, guest_memfd cannot take the filemap_invalidate_lock() to look up the current shared/private state of the gfn, for these reasons: + The MMU lock is a spinlock or rwlock and cannot be held while taking a lock that can sleep. + In guest_memfd's code paths (such as truncate), the filemap_invalidate_lock() is held while taking the MMU lock, and taking the locks in reverse order would introduce a AB-BA deadlock. Currently, the maximum mapping level is only queried from guest_memfd in the process of recovering huge pages, if dirty logging is disabled on a memslot. Dirty logging is not currently supported for guest_memfd, and guest_memfd memslots also cannot be updated. For now, bug the VM if guest_memfd needs to be queried to determine the maximum mapping level. This guard can be removed if/when support is added. Signed-off-by: Ackerley Tng --- arch/x86/kvm/mmu/mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a80a876ab4ad6..153bcc5369985 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3357,6 +3357,15 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, struct kvm_page_fault *fault, max_level = fault->max_level; is_private = fault->is_private; } else { + /* + * Memory attributes cannot be obtained from guest_memfd while + * the MMU lock is held. + */ + if (KVM_BUG_ON(static_call_query(__kvm_get_memory_attributes) == + kvm_gmem_get_memory_attributes, kvm)) { + return 0; + } + max_level = PG_LEVEL_NUM; is_private = kvm_mem_is_private(kvm, gfn); } -- 2.54.0.563.g4f69b47b94-goog