From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063403; b=TaSJVX7FV05lMcBYpnYDA03G7vbmyUQIl9d4GBTUY2SwYEHwvQXq95V6YFn3+yY18/ej 99LL2i9/HYi7z9bXH85GhtHx9Ck8fsQAceTT47+6Wz+LzGG1zYHrqpgD6XYb3/Pzh4tG2 JyOlxLcTSVEjkKdHsv4VF8XQ6u1iEdiYsG5tsFZ90bqboyrhjLfJYJy2omVHIQLtS0S2i Eb1u0w54skcsvu9a8/pCoWPmpM2SZ3hVRKE1jDHB063aA9IlVLk4OAkGi4Q5F902kwSK/ Dgo6RzXHRx40rCZ/TilgEGjFSnp4sOfdX3ERvfqPnGLexxzrHBXTjz/80GpLqlVWhVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063403; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Disposition:Content-Transfer-Encoding; bh=TzraQdW/D4wUYHP0l3BdHGNdMrzcOkRpDKi09QGbTv4=; b=NZZzjzJKtVKP1W8CQHmUncFrTusjT8VDGuPwtFaE+VkIgxjLR9tjrHJZWppJpK235Vlv rQomqTz6JuDYIBL2tnCNKyOrGxxofZmhkn5bXhL2f030Yy0GOhFpXFsXhtySe35uevcOG PyURuHPUcqz/RovyX9LhrYK2SeQPGtGRCDmDLcoi47J5P+FMv/PWiLPq9+EBdaRQoRYin JNg2o1+CUYIrdobuSO9IgR7t11bi7Tj/KdmpBLkCQeHrDMnQMrszzgPGz+DvMS4WBfmlR BA4v/GIGYJG1/UnXVXXq1WlSi7M2BTKxD1jL0FXB3hdNfZG8EiTgNwI5Hcdj1ksbI9g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=R+T9WQMI; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 42AC4606A32E for ; Mon, 30 Nov 2026 18:30:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 42AC4606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=R+T9WQMI 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 3EC031595; Mon, 30 Nov 2026 10:29:57 -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 C93683F86F; Mon, 30 Nov 2026 10:29:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796063400; bh=csxIz7e+JVyFTWjKR1n4x1WQ6+39aWC9exaqHRPW7y0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R+T9WQMIqYip3+4maOLMBIil8cl9lr7nM8GnynsTrG+UJM0Z4m3fcNUGJD/0Srrlk o5MD+q5fPxyjE/HeLKbFI13+g9k9THfdMnz4UnkThAs311HRssA+wjccTPgsGsV+Jd d5t2P+k2OTIqZ75rvc6kfm7Acdk1s3BURLD8VlfQ= 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 18:29:48 +0000 Message-ID: X-Mailer: git-send-email 2.56.1 In-Reply-To: <73b7f0a4-9cee-4fad-ae61-667dafd035f9@linux.dev> References: <20261130143039.867874-1-usama.arif@linux.dev> <73b7f0a4-9cee-4fad-ae61-667dafd035f9@linux.dev> 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-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8bit On Mon, Nov 30, 2026 at 05:31:19PM +0000, Usama Arif wrote: > > > On 30/11/2026 07:26, Leonardo Bras wrote: > > 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. > > > > 2 is the smallest multiple that does what Marc asked for on v1, a > capacity that covers more than one mapping per top-up [1]. > > I put numbers in the commit message below and in [2] for using > a fixed value of 40 vs 2 x kvm_mmu_cache_min_pages() and didnt see a > regression so didnt see a reason to test other multipliers, as that would > just be wasting memory without any performance benefits? You tested for 2x, I asked if you also tested 1x, 2x, 4x, and so on. I am not 100% sure Marc asked for a multiple like that, but even if it was, he seems to be worried about performance on block splitting and re-faulting (as you tested). Having 1x, 2x and 4x at least would help us better understand the kind of impact that would have. > > >> 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. > > > > I tested above with 4K, 16K and 64K base pages and the numbers are > similar, i.e. no regression found. > > Full disclosure, the benchmark was generated by LLM and I didn't > want to put LLM crap in reply, but I can share the raw results and > benchmark if you would like. I think it's now even more important that we see the tests, as the LLM-generated one could be measuring stuff wrong. Also, the results are important, mainly if they are composed with the multiplier in the previous comment. > > >> 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... > > Because a memcache has to be topped up with the same capacity every > time, and with NV the vCPU's memcache is topped up for more than one > stage-2. Ah, I see, the cache is the same based on a vcpu, and the S2_mmu depend on it being either the canonical mmu or a nested one. I had previously misread and thought that you were saving the capacity at the s2_mmu instead of kvm_arch. I still think there is a better way of doing this, maybe calculating the maximum levels a current pagesize could have. Somethink like: capacity = VTCR_EL2_SL0_TO_LVLS(0b10); Since a guest never have a smaller pagesize, this could be safe, and ends up being compile-time as well. [...] > > I tried your MULTIPLIER * min_pages version under QEMU with > kvm-arm.mode=nested: 4K pages, an IPA limit of 52 bits, and a 40-bit > IPA VM whose L1 runs an L2. In all six runs, across two tests, L2's > first fault that needs a refill hits that WARN_ON_ONCE() and KVM_RUN > fails with EIO. Out of curiosity, is it fully KVM, or are you using QEMU to emulate a 4k-pagesize host? Is it hitting with which value for MULTIPLIER? Ah, you mean based in the version that multiplies kvm_mmu_cache_min_pages(), yeah, as I mentioned above, you were right in not using the min for that. Thanks! Leo