From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796069715; b=06ANTfRYSrfxGoBegVy+z+mg0PQooTLg/o1k+T4yjVcGzqwqsF9/ctyn0bneM+n6kdWv SPzRxrJSRoJIRjGI778pCcqTTuf3XvnxllL+uYd7OGH7gVrHpm5v3PdTCqKvjr69keZ4A 9xSBkNFq816sxFPkBwYIu3QAN51NVw+B3dKdZcE73nfo1GbLm70vJoOZk1iohq4eg5Cod MadWvU1ZyFnZWd01zYsTOnmeQ5Q1wfErD77Nbjt7xvhv8nDzuMz40qjUtgXk7fi6SWaPv wij6FFYcVaYIsgxz2aKNyxFWoEcR7sqjxlqNy1mq7kJ1tV2y10Xwnqqn1+WdZb+TpJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796069715; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=ijomXG5NKJhGL0Cyjwvvz3VRs4Nkel7WvtnXaVXs1xc=; b=SHEkVtQsEJCUSeARWl37of93E5onSjwvJ+Wta3mZgHpNj2ErRYEFGlNLDCfOuISMXM+H pKEf+9SvheFT1fruNgKmsSO8WfFV9BLQmuEhLdpcBc31JpFiDW2XzyfevOarTiXJxFXaJ 1JCuK63lnfazH8Ck5Qh6crTq8ZWAtAWjiotV3RxMsXFt/SYZrXGR9KjKQyovpzXeUJ1zJ s31ogU9X+oFX1Y+RqeN3KY1lQIXSrD48lkUNiLL8ugV6kkImtGgCFzGArC2T7291qXRfx 8FRGCzRiyyeSg6jtespiix/PYUTfVq1Yppm/n+iwzhMVBFt75rSU5fwwxMlhLpivYFQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=duddh5+l; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38FED606A331 for ; Mon, 30 Nov 2026 20:15:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=duddh5+l Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86C9F1F00898; Mon, 30 Nov 2026 20:15:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796069714; bh=ijomXG5NKJhGL0Cyjwvvz3VRs4Nkel7WvtnXaVXs1xc=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=duddh5+lkrVvKnRqXtt2hC9HUpfnP3a6khOpa85q417Dv0S9FYOIlca8goiTK60AQ sgr1DlruaNnovOkz9ZSgT42QqGLL8sOv47XIwtOfrfGVl6eA8hQfG268S7rIz/kW+K 3PDCTKlxpUcXsii/GPOZ6VLQfQgMd+8Hj8l5jsrEsQNHrYDUGV+2NIyZf14o9Uat1u 8wtmGS8xs1KNGteNcGWAjiYKLOpay5nU4vAY7tZ17fdQQxGGb3IAf8VfmWWfZT+2zW xEqI5snBkLoQhWO2X56kvzb+yclnO4PmjtdHd4OmoYGHHpxSEaN52V/GMnECeRZiGj pEd3sP0TOX16Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2] KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages() Reply-To: sashiko-reviews@lists.linux.dev To: "Usama Arif" Cc: kvmarm@lists.linux.dev, "Oliver Upton" , "Marc Zyngier" In-Reply-To: <25bc8bdf-53b4-40ad-ad56-db71aedd0d23@linux.dev> References: <20261130143039.867874-1-usama.arif@linux.dev> <73b7f0a4-9cee-4fad-ae61-667dafd035f9@linux.dev> <25bc8bdf-53b4-40ad-ad56-db71aedd0d23@linux.dev> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 20:15:14 +0000 X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] KVM: arm64: missing Signed-off-by trailer in commit message - [Medium] KVM: arm64: unedited email reply in changelog and mismatched commit title - [Low] KVM: arm64: redundant KVM_MMU_CACHE_CAPACITY leaves arch macro stale at 40 -- commit 0cb2b4c3c55d9c290742d9fed15dbc30c21664cb Author: Usama Arif > KVM: arm64: Size the stage-2 memcache from kvm_mmu_cache_min_pages() > > On 30/11/2026 10:29, Leonardo Bras wrote: > > 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 m= ost > >>>> 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 allocat= ed: > >>>> up to 2.5 MiB per vCPU with 64 KiB pages. On a 64 KiB host running 1= 01 > >>>> 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 wo= uld > >> just be wasting memory without any performance benefits? > > > > You tested for 2x, I asked if you also tested 1x, 2x, 4x, and so on. > > Ah sorry, maybe I am confused. > > So the reason I am saying I only did 2x is because Marc asked for a capac= ity > that covers more than one map per top-up (so that ruled out 1x), > and using 2x didnt show any regression for block splitting and faults, > so I didnt see a reason to test for 4x as thats just allocating more memo= ry > without any benefits. > > > > > I am not 100% sure Marc asked for a multiple like that, but even if it = was, > > Ah no not at all, Marc just said he wants more than one map per top-up. > > 2x was something I did. I asked if 2x made sense in [1]. > > > he seems to be worried about performance on block splitting and re-faul= ting > > (as you tested). Having 1x, 2x and 4x at least would help us better > > understand the kind of impact that would have. > > > > Sure! I have attached it below. > > >> > >>>> 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 pa= ges > >>>> 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 <=3D 101 MiB >=3D 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 fau= lt > >>>> 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 tabl= es > >>> 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-fa= ult > >>> 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. > > > > Sure, the tests are at [2] and [3]. You need [4] if you want to iterate > quickly. > > I ran them on one Grace host (72 CPU). The minimum is the 3 pages with > 4K and 2 with 16K and 64K, so the capacities are: > > 40 4x 2x 1x > 4K 40 12 6 3 > 16K, 64K 40 8 4 2 > > Each run creates 16 VMs with 4 vCPUs, one VM at a time. Each vCPU times > its KVM_RUN with CLOCK_MONOTONIC until the guest exits through MMIO at > the end of the phase, and the programs check that every store landed. > Each cell is the median of the 16 VMs, given as the range over 3 rounds. > > split kvmsplit. Each vCPU writes one page of every huge page in its > 1 GiB share, so stage-2 maps them as blocks. Then dirty > logging is enabled (no eager splitting) and each vCPU writes > another page of every block, so each write splits a block and > allocates a table: 512 2M blocks per vCPU with 4K, 32 32M > blocks with 16K, 2 512M blocks with 64K. Time per VM: the > slowest vCPU's split. > sparse kvmsparse. 8000 one-page memslots per vCPU, one last-level > table span apart, so every fault takes a table and the > memcache refills as often as it can. Time per VM: from the > first KVM_RUN to the last vCPU's exit. > > ms per VM 40 4x 2x 1x > 4K split 2.59-2.82 2.60-2.68 2.60-2.66 2.65-2.68 > sparse 31.4-32.5 30.4-31.5 30.2-31.9 30.3-32.1 > 16K split 0.157-0.184 0.197-0.205 0.198-0.206 0.197-0.205 > sparse 36.1-37.4 36.0-36.6 35.6-37.9 35.6-36.5 > 64K split 0.025-0.026 0.026 0.026-0.027 0.028-0.029 > sparse 51.9-52.3 50.2-51.6 49.8-50.8 50.4-52.4 > > The capacity makes no difference beyond the noise, down to 1x. > > >> > >>>> 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 mis= read > > 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 =3D VTCR_EL2_SL0_TO_LVLS(0b10); > > > > Since a guest never have a smaller pagesize, this could be safe, and en= ds > > up being compile-time as well. > > > > Agree that a host-wide value would be nicer: it drops the struct member > and the reasoning about when the shadow stage-2s are initialised, and > it can be compile-time. SL0 =3D 0b10 isn't the deepest stage-2 with 16K > pages, though: with LPA2 and a 52-bit IPA it has 4 levels (SL0 =3D > 0b11). > > How about we size the cache for the deepest stage-2 KVM can > create, the one for a 52-bit IPA? No stage-2 can be deeper: neither a > VM's IPA nor the shadow stage-2s' can exceed kvm_ipa_limit, and > kvm_ipa_limit is at most 52 bits. > > Something like below maybe? [Severity: High] The commit message is missing a top-level Signed-off-by: trailer from the patch author (the only Signed-off-by: line is inside a nested email quote from an earlier version). [Severity: Medium] The commit message is an unedited mailing-list reply containing quoted thread history and an obsolete inline diff. In addition, the commit subject and quoted changelog still describe sizing the cache per-VM from kvm_mmu_cache_min_pages(), whereas the patch sizes it from a host-wide 52-bit IPA compile-time constant (2 * (stage2_pgtable_levels(52) - 1)). > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 1db10d8d6db73..f95131280c04f 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -1287,6 +1287,9 @@ int topup_hyp_memcache(struct kvm_hyp_memcache *mc,= unsigned long min_pages) > kvm_host_pa, mc); > } > =20 > +/* Two maps on the deepest stage-2, which has a 52-bit IPA */ > +#define KVM_MMU_CACHE_CAPACITY (2 * (stage2_pgtable_levels(52) - 1)) > + > /** > * kvm_phys_addr_ioremap - map a device range to guest IPA > * > @@ -1315,8 +1318,8 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_add= r_t guest_ipa, > guest_ipa &=3D PAGE_MASK; > =20 > for (addr =3D guest_ipa; addr < guest_ipa + size; addr +=3D PAGE_SIZE) { > - ret =3D kvm_mmu_topup_memory_cache(&cache, > - kvm_mmu_cache_min_pages(mmu)); > + ret =3D __kvm_mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_CAPACITY, > + kvm_mmu_cache_min_pages(mmu)); > if (ret) > break; > =20 > @@ -1664,7 +1667,7 @@ static int topup_mmu_memcache(struct kvm_s2_mmu *mm= u, void *memcache) > int min_pages =3D kvm_mmu_cache_min_pages(mmu); > =20 > if (!is_protected_kvm_enabled()) > - return kvm_mmu_topup_memory_cache(memcache, min_pages); > + return __kvm_mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_CAPACITY, = min_pages); > =20 > return topup_hyp_memcache(memcache, min_pages); > } [Severity: Low] This isn't a bug, but now that the capacity is a host-wide compile-time constant rather than a per-VM value, could KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE in arch/arm64/include/asm/kvm_types.h be updated instead of adding KVM_MMU_CACHE_CAPACITY? kvm_phys_addr_ioremap() and topup_mmu_memcache() are the only arm64 callers of kvm_mmu_topup_memory_cache(), so bypassing it leaves KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE unused at its stale value of 40. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/25bc8bdf-53b4-40ad-= ad56-db71aedd0d23@linux.dev?part=3D1