Re: [PATCH v2 1/2] KVM: arm64: nv: Discard undersized TTL hints in compute_tlb_inval_range()

From: Wei-Lin Chang

Date: Wed Nov 25 2026 - 04:56:46 EST


On Tue, Nov 24, 2026 at 10:16:16PM +0000, Colton Lewis wrote:
> On Tue, 24 Nov 2026 20:42:19 +0000, Marc Zyngier <maz@xxxxxxxxxx> wrote:
> > On Tue, 24 Nov 2026 20:22:15 +0000,
> > Colton Lewis <coltonlewis@xxxxxxxxxx> wrote:
> > > [1] only catches TTL hints smaller than host PAGE_SIZE. It does not
> > > catch a hint that is >= PAGE_SIZE but smaller than the mapped block,
> > > such as a 4kB L3 hint on a 2MB block on a 4kB host.
> >
> > Why do you think this makes *any* difference? The hint passed by the
> > guest is utter bollocks, and the best thing would be to not invalidate
> > anything. It's just too invasive to do elegantly.
>
> Architecturally [1] is fine for the TLB, and before guest_s2_mappings
> landed in v7.4-rc1 clearing the block was harmless. The problem with [1]
> is a host memory leak in kvm_pgtable_stage2_unmap_tracked():
>
> 1. L2 faults in a 2MB block. kvm_s2_fault_map() installs a 2MB block PTE
> and kvm_record_guest_s2_mapping() inserts a 2MB
> struct kvm_guest_s2_mapping into both interval trees.
> 2. L1 runs TLBI IPAS2E1IS with a 4kB L3 TTL hint (4kB == PAGE_SIZE, so
> [1] returns 4kB).
> 3. kvm_pgtable_stage2_unmap(..., SZ_4K) clears the entire 2MB block PTE.
> 4. kvm_remove_guest_s2_mappings(..., SZ_4K) skips removing the 2MB entry
> because a 4kB range does not enclose it.
> 5. L2 touches the address again, faults because the PTE was cleared, and
> allocates another 2MB tracking entry.
>
> Looping steps 2-5 lets an L1 guest leak one struct kvm_guest_s2_mapping
> per iteration until a larger unmap or VM teardown (I measured 832kB of
> slab_unreclaimable growth over 8192 iterations) and turns later TLBIs on
> that range into linear walks under write_lock(&kvm->mmu_lock). Patch 2
> avoids exact duplicates for the same (nested, canonical) pair, but
> remapping the nested IPA to different canonical IPAs still leaves each
> stale entry behind.
>
> If you prefer not to touch compute_tlb_inval_range() beyond [1], another
> option is keeping [1] as-is and dropping the cleared tracking entry in
> kvm_remove_guest_s2_mappings() when a partial overlap leaves no valid
> mapping beneath it (zero cost on valid hints).

For the other option, how can we recognize when a tracking entry can be
safely removed?
i.e. No more shadow mapping in the tracked nipa range.

We don't even know for certain what shadow mappings exist in the first
place.

Thanks,
Wei-Lin Chang