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 E8B06606A31C for ; Mon, 30 Nov 2026 15:27:07 +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=d2H6TEhv; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=m+S1XPSS 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:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=830wE5EZ6tlFaYaz5gwWoNm1csSD8Ac/+2pKQOIHuYs=; b=d2H6TEhvLS79QL jdXGkYgf2crVhiT9CuJjZ2gtLVs0Ccc18GAsiYG3DW8lYGf4Oa5YqkY2CXn+sD2CBsDrs3XZ1Lv8j axfUSqpEQfdSxCrtx03FIOS6Q4r/L6ZtO9nLJ770Q+q9yJtotZ9+5z1G/FgNxkNWd9tad/aagz2qH egILkoP4vIIbGT6328geUkujc5dQHN5OsTVJtgmBJXEIVx8wBGnrx3WHP3WmCydB6Y1bfv+bjoYcx woFNap45DyI3IYVc+wgGt4zTBh052cUwT6M6AbYGdf90aFeVUxChAdgTj4CsEfsmhqyakpmhIaS0E 1H7kJhc03hOXhEJ4rGdA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY3HF-000000052BO-40DI; Mon, 30 Nov 2026 15:26:49 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY3HC-000000052Au-2kZl for linux-arm-kernel@lists.infradead.org; Mon, 30 Nov 2026 15:26:49 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5EAF9152B; Mon, 30 Nov 2026 07:26:42 -0800 (PST) Received: from LeoBrasDK.cambridge.arm.com (LeoBrasDK.cambridge.arm.com [10.2.212.21]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7BE303F86F; Mon, 30 Nov 2026 07:26:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796052405; bh=c+7YKOcm8Ccuoe0wkhSLtdM5mWLyQ+qf4YPbareK+/U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m+S1XPSS5r1D1qQFViM70kQ+ZdWeoRZYGvXZGthURItGA3kNThwkE4dMD40C8HRrB HShbb8lG5aN44NAyFVBZB3WyamEYW1SZDWDZ9cVF5FEv9fegpGJU2a0DzS0btnKFhe eJNTM+W9H2Q2NTxWiZStzrkpD8nxxssiODR/Fl48= From: Leonardo Bras To: Usama Arif Cc: Leonardo Bras , ljs@kernel.org, maz@kernel.org, oupton@kernel.org, fuad.tabba@linux.dev, joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, riel@surriel.com, shakeel.butt@linux.dev, kernel-team@meta.com, rkeilty@meta.com, atish.patra@linux.dev, dgreid@meta.com Subject: Re: [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages() Date: Mon, 30 Nov 2026 15:26:32 +0000 Message-ID: X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130143039.867874-1-usama.arif@linux.dev> References: <20261130143039.867874-1-usama.arif@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_072646_926253_77A90462 X-CRM114-Status: GOOD ( 36.32 ) X-Spam-Score: -4.4 (----) 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 On Mon, Nov 30, 2026 at 06:30:39AM -0800, Usama Arif wrote: > When fewer than kvm_mmu_cache_min_pages() pages are left, a stage-2 > fault that may need new tables refills the vCPU's memcache to > KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE, 40 on arm64. A map allocates at most > one table per level below the preallocated root, which is > kvm_mmu_cache_min_pages() and at most 3 pages. Only vCPU destruction > frees the memcache, so whatever a refill leaves unused stays allocated: > up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 101 > VMs with 4 vCPUs and 1 GiB each, the memcaches held 986 MiB, against > 25 MiB of stage-2 tables in use. > > Size the memcache at twice the largest kvm_mmu_cache_min_pages() of the > VM's stage-2s instead, Why 2? New heuristic to replace the current one? :P But really, do you have any performance numbers for 1, 2 and 4+ worst-case block split maps? That would be good to have as a reason for picking a multiplier. > so that each refill covers at least two > worst-case maps. With NV, the vCPU's memcache also feeds the shadow > stage-2s, and a memcache's capacity can't change once used, so record > the largest value as each stage-2 is initialised. The VM's stage-2 is > set up at VM creation and the first shadow stage-2s by the first > KVM_ARM_VCPU_INIT, before any vCPU can fault, and later shadow stage-2s > have the same size. kvm_phys_addr_ioremap()'s temporary memcache uses > the same capacity, so arm64 no longer uses the value of > KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE. > > With a 48-bit IPA, the capacity is 4 pages with 16 KiB and 64 KiB pages > and 6 with 4 KiB pages; with the default 40-bit IPA, 2 and 4. On the > host above and on a production host also running 101 VMs with 4 vCPUs > each, both with 64 KiB pages and 48-bit IPA VMs: > > memcaches saved > before after > host above 986 MiB 81 MiB 905 MiB > production host 869 MiB <= 101 MiB >= 768 MiB > > A memcache now refills at most once every two tables, as a capacity of > 4 does with 4 KiB pages and a 48-bit IPA. Measured that way, per fault > for the median vCPU, one VM at a time: > > 40 4 > 2M blocks split under dirty logging 4.9-5.1 us 4.9-5.0 us > 1G block split, then refaulted 2.7-2.8 us 2.8 us > every fault allocating a table 4.0-4.1 us 3.7-4.1 us > Would be nice to have numbers for 16k and 64k as well, as longer tables may just in case longer tables have different impact. If you run them, please share the numbers for the whole operation, instead of a per-fault version, as well as which code you used to test this, so people can reproduce if they want. > Suggested-by: Marc Zyngier > Signed-off-by: Usama Arif > --- > arch/arm64/include/asm/kvm_host.h | 2 ++ > arch/arm64/kvm/mmu.c | 19 +++++++++++++++---- > 2 files changed, 17 insertions(+), 4 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 97332ffcb1300e..e96c6208686240 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -413,6 +413,8 @@ enum kvm_arm_vm_flavor { > > struct kvm_arch { > struct kvm_s2_mmu mmu; > + /* Capacity of the vCPU stage-2 memcaches */ > + int mmu_page_cache_capacity; Why add a new struct member here, if you could instead use your (MULTIPLIER * kvm_mmu_cache_min_pages(mmu) when you need it? You are already calculating kvm_mmu_cache_min_pages() anyway, so it's just one extra operation, see below... > const struct kvm_vm_s2_ops *vm_s2_ops; > > enum kvm_arm_vm_flavor vm_flavor; > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 042dc53267cb36..cd0605cce45d2a 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -1045,7 +1045,7 @@ static void kvm_stage2_destroy(struct kvm_pgtable *pgt) > */ > int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type) > { > - int cpu, err; > + int cpu, err, capacity; > struct kvm_pgtable *pgt; > > /* Initialize the VM ops for the VM instance for the first time */ > @@ -1086,6 +1086,15 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t > goto out_free_pgtable; > > mmu->pgt = pgt; > + > + /* > + * The vCPU memcaches serve all of the VM's stage-2s and can't be > + * resized once used, so make them cover two maps on the deepest one. > + */ > + capacity = 2 * kvm_mmu_cache_min_pages(mmu); > + if (capacity > kvm->arch.mmu_page_cache_capacity) > + kvm->arch.mmu_page_cache_capacity = capacity; > + This would not be needed > if (is_protected_kvm_enabled()) > return 0; > > @@ -1317,8 +1326,9 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > guest_ipa &= PAGE_MASK; > > for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) { > - ret = kvm_mmu_topup_memory_cache(&cache, > - kvm_mmu_cache_min_pages(mmu)); > + ret = __kvm_mmu_topup_memory_cache(&cache, > + kvm->arch.mmu_page_cache_capacity, > + kvm_mmu_cache_min_pages(mmu)); This neither > if (ret) > break; > > @@ -1669,10 +1679,11 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu) > > static int topup_mmu_memcache(struct kvm_s2_mmu *mmu, void *memcache) > { > + int capacity = kvm_s2_mmu_to_kvm(mmu)->arch.mmu_page_cache_capacity; You could have, instead: int min_pages = kvm_mmu_cache_min_pages(mmu); + int capacity = MULTIPLIER * min_pages; Which would even make it more clear to the reader that you are using a multiple instead. > > if (!is_protected_kvm_enabled()) > - return kvm_mmu_topup_memory_cache(memcache, min_pages); > + return __kvm_mmu_topup_memory_cache(memcache, capacity, min_pages); > > return topup_hyp_memcache(memcache, min_pages); It could even be an one-line change if you want :) Thanks! Leo