From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.228 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796048512; b=LBZ/PZLo/cKRkYId8d20biNlQEK3ZKbS7+UHn33dMZgteW+a2QJS54OX2Sl5MrMa80oU WxwTnJxSNZIku/YAsMSbTFP9U/rbZH8SFizE0PW67luTY0tQbQrUV7JAuen5F7A66kPKJ /EYPC/uACiDfMIkiOVuPmdmtCLJ3+zpmoibCrPPJEkBWXzXHgyf/44KfxfjvVdv5TQ958 e+bHSDkcemHaWo/zEBRyNyRlJVWBv5k2HwfKSD5PDvZotBi4FZsPSHxATScvL9Y7xalMP +T14uJdq3RYzDuZR+oiFros51U0sLkDT+ZBn/XX9/Bw0bSk0+56akYJPn+TBnYmVvqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796048512; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=hW1z4L47PO0DfSLTXdqfipqR2lR7UCj7K3VH8W4X2o0=; b=EQPEaMLiBHNkqRI34su4IFhQIZl448+PB4S7ZhfMWXZTgJ5mf/+LJ+LapeKyPMGdfnPT PtD7NoGOzBClwPcgPvTbA9xc4kZcov7+tUTi0if9xHtKaE1PS65mfDk9G+uIvAgDGt9dI 6R3t0ETcuOcBJzCp0jwi0XwT+hdCoiArveLBkBzHf1Ky/3rh9pxLOX68AY+Uvg4BV90IF Cx1T5FNw+g6Hln8f1doZuXVridSsHIx8/x/sgDvdC/FNH1PknhQBG/rE3yXkMjslugr/H mdjW9GUgKkWohTgZuUkKTgh6pH89gvSDc5obQ1oil/BcazRPXw9bztIGdIC0XMVbS3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=FdfrwtYf; arc=none smtp.remote-ip=91.218.175.228 Received: from mta0.migadu.com (out-228.mta0.migadu.com [91.218.175.228]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01B9A606A320 for ; Mon, 30 Nov 2026 14:21:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 01B9A606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=FdfrwtYf X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=4DesUca42Svh+9bRcmVt/qvOAVO86EEzKqADyUSNI+Q=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796048509; v=1; x=1796653309; b=FdfrwtYf5ZSZ5KlZIeRXwkZ2y20/IG5hsj+m4M/IXd2T+0EjxYCyy3qpY5osuA/cpnC2590B aYo8b1CqHhuscOTFQBbcDyHrNXVuo3y3/JlwTtTwcZAfO26KiYjLdPRQLpCu5OGjf+ZU0ozcBky Jn+e/sHEupdqk5tIRCV8bWi8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id e733805e3097b1d1; Mon, 30 Nov 2026 14:21:48 +0000 X-Mizu-Trace-ID: e733805e3097b1d1 X-Migadu-Flow: FLOW_OUT From: Usama Arif To: Usama Arif Cc: Marc Zyngier , Lorenzo Stoakes , Leonardo Bras , 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] KVM: arm64: Reduce the stage-2 page cache on 64 KiB hosts Date: Mon, 30 Nov 2026 06:21:44 -0800 Message-ID: <20261130142146.3827951-1-usama.arif@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Fri, 27 Nov 2026 14:28:32 +0000 Usama Arif wrote: > With twice the minimum, each refill covers at least two worst-case > maps. I asked a LLM to comeup with a way to time for block splitting and pagefaults > and I found it reasonable, I have attached the results in the commit message. > > Does a factor of 2 sound reasonable to everyone? > > If this looks ok, happy to send as v2. > I will send the below as v2 now, hopefully thats ok with everyone! Thanks, Usama > > >From dad1d43f1c2dcb8ffbc8084ec19467731e8548f4 Mon Sep 17 00:00:00 2001 > From: Usama Arif > Date: Fri, 27 Nov 2026 06:07:35 -0800 > Subject: [PATCH] KVM: arm64: Size the stage-2 memcache from > kvm_mmu_cache_min_pages() > > 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, 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 > > Suggested-by: Marc Zyngier > Signed-off-by: Usama Arif > --- > arch/arm64/include/asm/kvm_host.h | 2 ++ > arch/arm64/include/asm/kvm_types.h | 1 + > arch/arm64/kvm/mmu.c | 19 +++++++++++++++---- > 3 files changed, 18 insertions(+), 4 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 97332ffcb1300e..515869c190c66a 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, see kvm_init_stage2_mmu() */ > + int mmu_page_cache_capacity; > const struct kvm_vm_s2_ops *vm_s2_ops; > > enum kvm_arm_vm_flavor vm_flavor; > diff --git a/arch/arm64/include/asm/kvm_types.h b/arch/arm64/include/asm/kvm_types.h > index 9a126b9e2d7c99..68472a593578f3 100644 > --- a/arch/arm64/include/asm/kvm_types.h > +++ b/arch/arm64/include/asm/kvm_types.h > @@ -2,6 +2,7 @@ > #ifndef _ASM_ARM64_KVM_TYPES_H > #define _ASM_ARM64_KVM_TYPES_H > > +/* Enables the generic MMU memory caches; arm64 passes its own capacity. */ > #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40 > > #endif /* _ASM_ARM64_KVM_TYPES_H */ > 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; > + > 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)); > 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; > int min_pages = kvm_mmu_cache_min_pages(mmu); > > 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); > } > -- > 2.53.1-Meta > > >