From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 293EF606A333 for ; Thu, 3 Dec 2026 18:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=e3OnOkig; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Hm3Ir11R DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Unsubscribe-Post:List-Owner; bh=9fN1imcPNMA9sa7LQ5xbkmQgu0GosUB8v3s58QPXWTw=; b=e3OnOkigWJ2hIquRPhgYffcoY7 capi16p0/4vo5wY7C4lyjevCDD05PxSjmuLq5EDnG2YfpvGmsGEeTTBtQ8ZW1dhg6tuLd1c5ufyu/ l2gz/lV4NdmEaeKDus/69j/kRSoVLrxpwhAMtk9CNyRsPH1Es2vNQc8rwvyRschPGOW8ZI5zPd68g PhoriygH0PBDegIKQ6iNKWLN4YeBRKnLOmnhzd4JDT9U8pcVZogJcxNDl0GI/5M8DArrWxjmJmXH6 LgWs614qeeiYc3UtlgIPQZ5QFuLBhByHPm7KR/N5Hkbcl7CHatmjeiyNYko+fKN6mlgnvh8bh0g+y sWxco2VQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZBoF-0000000FcGX-1y1j; Thu, 03 Dec 2026 18:45:35 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZBoE-0000000FcGH-0XVA for linux-arm-kernel@lists.infradead.org; Thu, 03 Dec 2026 18:45:34 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 621B042371; Thu, 3 Dec 2026 18:45:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9A201F00893; Thu, 3 Dec 2026 18:45:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796323533; bh=9fN1imcPNMA9sa7LQ5xbkmQgu0GosUB8v3s58QPXWTw=; h=From:Date:Subject:To:Cc; b=Hm3Ir11R511GOeDsSkhj73xI0ovGFzDw3fJZFOK2VrOqjJgA4tM4b8qR5w5qnCu4S CeVkzmAZn9A/ICOZscvUL2ofFM6paB6Zo1zA5MEnAZPQJSpPmm7M/djT3R0aMoGw7x nKwWlZ2vOMUcfwrkfXt2av8GJ7otKC733yRAB4V7yoJ3nrFSZrhOLUqAPVYyZkNCbC v+wEQohKr4BK+J9kSLbFRtN6CKJL0J9tFbvngfN5BFQ2F2RkE0yeyeFkL8TTzkiKT0 ibv53pVurQFqEmaOknAB5DUH57DgsPEyqjs5jEHXH8WMRY/xo5B7iAUg1feQSo7THf cqAGVpjpAw2uw== From: "Lorenzo Stoakes (ARM)" Date: Thu, 03 Dec 2026 18:45:24 +0000 Subject: [PATCH v2] KVM: arm64: Unconditionally top up the mmu memcache on user fault X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-fix-nv-cache-topup-v2-1-afc8fc1f87a2@kernel.org> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/32NQQ6CMBBFr0Jm7RhmNBVccQ/DopYpNJqWtNhoC He3cgCX7+f/91dIEp0kuFYrRMkuueAL8KECM2k/CrqhMHDNiogVWvdGn9FoMwkuYX7N2Oo72aH htqYzlOEcpbR26a0vPLm0hPjZPzL90r+6TEiomU17Us2FrXQPiV6exxBH6Ldt+wJTduu8tQAAA A== X-Change-ID: 20261126-fix-nv-cache-topup-9ab1fd829014 To: Marc Zyngier , Oliver Upton , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Christoffer Dall , Jintack Lim , Leonardo Bras , Wei-Lin Chang , Yanan Wang Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Usama Arif , ljs@kernel.org, stable@vger.kernel.org X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4336; i=ljs@kernel.org; h=from:subject:message-id; bh=JX4vhZgYY/8ESPnWKBO7wfMj+B4G9plTxEuMqJe5Vuk=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIFd5zwjw1/XxqWc+nbu5Z2fw39BWJinPc7BI/x9mgEc x7ofqHfUcrCIMbFICumyPL8i/j+IJGweZ0X/N1g5rAygQxh4OIUgInIOTEybEr9zmgrmjrZdp3e AyOrl4cMP9atNbtzj73g6R1ZzfCck4wMPep7H+up/ju8ryXgTY8El8rxHadSBU4dD1mudI292fA GLwA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The S2 user fault path tops up the MMU memcache (used for page table pre-allocation as the point at which page tables are installed may not allocate) except when: * It's a permission fault AND * dirty page logging is disabled AND * pKVM is disabled At which point it is assumed no page table will be allocated. However this is not safe - if the mapping size differs from that at which the permission fault was taken, we must install a new mapping, which may require a page table. This can happen when: * A guest hypervisor replaces a block in its own S2 with a table without issuing a TLBI (likely due to a bug) - the shadow S2 mapping size is clamped to that of the guest's S2, so the shadow block has to be collapsed. * A guest hypervisor does issue the TLBI, but races with another vCPU which has already taken a permission fault on the shadow block. * Nested virtualisation isn't in use at all, but the VMM replaces a THP with 64K hugetlb pages while a permission fault on it is in flight. With the memcache empty, kvm_mmu_memory_cache_alloc() hits its guest-triggerable WARN_ON(!nobjs) and falls back to a GFP_ATOMIC allocation under mmu_lock, with a BUG_ON() if that fails. Therefore, top up the memcache unconditionally. This is cheap, as the top up does nothing if the cache already holds enough objects. Note that dirty logging had a similar issue, addressed in commit 56fd1fc526e6 ("KVM: arm64: Top up stage-2 memcache for dirty logging faults"). Local testing with dirty logging disabled indicated no performance impact. kvm_page_table_test was modified to take 65,536 permission faults with dirty logging disabled, and run on an Apple M2 with the kernel under test running as an NV guest. The median time across ~70 runs was 1.059s without this patch and 1.062s with it. A control stage in the same runs, whose code this patch does not touch, showed a similar level of variation so this is clearly within noise levels. Fixes: 7d894834a305 ("KVM: arm64: Add usage of stage 2 fault lookup level in user_mem_abort()") Fixes: fd276e71d1e7 ("KVM: arm64: nv: Handle shadow stage 2 page faults") Cc: stable@vger.kernel.org Signed-off-by: Lorenzo Stoakes (ARM) Reviewed-by: Leonardo Bras --- v2: * Switched to always topping up as per Usama. * Got performance numbers as per Leo. * Tweaked commit message to be more succinct. * Retitled the patch as it is no longer NV-specific. * Added a Fixes tag for the non-NV case, which predates NV support. v1: https://lore.kernel.org/r/20261126-fix-nv-cache-topup-v1-1-a22c936872fe@kernel.org --- arch/arm64/kvm/mmu.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index a0d4981ebb99..4ef32bedb327 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -2248,28 +2248,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, struct kvm_s2_fault_result *result) { - bool perm_fault = kvm_s2_fault_is_perm(s2fd); struct kvm_s2_fault_vma_info s2vi = {}; enum kvm_pgtable_prot prot; void *memcache; int ret; - /* - * Permission faults just need to update the existing leaf entry, - * and so normally don't require allocations from the memcache. The - * only exception to this is when dirty logging is enabled at runtime - * and a fault needs to collapse a block entry into a table. - * Under pKVM a permission fault can also collapse pages into a block, - * which needs a fresh mapping object, and the hypervisor requires the - * min-pages memcache even when the install allocates nothing. - */ memcache = get_mmu_memcache(s2fd->vcpu); - if (!perm_fault || memslot_is_logging(s2fd->memslot) || - is_protected_kvm_enabled()) { - ret = topup_mmu_memcache(s2fd->mmu, memcache); - if (ret) - return ret; - } + ret = topup_mmu_memcache(s2fd->mmu, memcache); + if (ret) + return ret; ret = kvm_s2_fault_pin_pfn(s2fd, &s2vi); if (ret == -EHWPOISON) { --- base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c change-id: 20261126-fix-nv-cache-topup-9ab1fd829014 Cheers, -- Lorenzo Stoakes (ARM)