KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries
v2 cover KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries
When an L1 guest issues TLBI IPAS2E1 with a TTL hint smaller than the actual mapping, KVM clears the entire block from the shadow stage-2 page table while leaving the stale tracking entry in guest_s2_mappings. Patch 1 drops undersized TTL hints in compute_tlb_inval_range() and falls through to the unhinted path so the unmap and tracking removal cover the same range. This follows Marc's suggestion [2] on Dan Carpenter's sub-page TTL patch [1] (which Patch 1 overlaps with and subsumes), fixing WARN_ON(size & ~PAGE_MASK) on 16kB/64kB hosts as well.
Patch 2 avoids inserting identical duplicate entries in kvm_record_guest_s2_mapping().
Tool-generated content:
- Initial bug discovery: automated LLM-assisted audit of the arm64 nested stage-2 tracking code.
- Patch and text drafting: interactive Gemini session (patches, commit messages, and cover letter).
Changes since v1:
- Drop the kvm_remove_guest_s2_mappings() change; discard undersized TTL hints in compute_tlb_inval_range() and fall through to the unhinted path (Marc Zyngier).
- Split duplicate suppression in kvm_record_guest_s2_mapping() into a separate patch, call kfree() under guard(spinlock), and drop the Fixes tag (Wei-Lin Chang).
- Rewrite the commit messages and cover letter.
Link to v1: https://lore.kernel.org/r/[email protected]
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
Colton Lewis (2):
KVM: arm64: nv: Discard undersized TTL hints in
compute_tlb_inval_range()
KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries
arch/arm64/kvm/nested.c | 105 ++++++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 21 deletions(-)
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v2 1/2 KVM: arm64: nv: Discard undersized TTL hints in compute_tlb_inval_range()
When an L1 guest issues TLBI IPAS2E1 with a non-zero TTL hint, compute_tlb_inval_range() takes it at face value. If the hinted size is smaller than the actual mapping (for example, a 4kB L3 hint on a 2MB block), kvm_pgtable_stage2_unmap() clears the entire block while kvm_remove_guest_s2_mappings() leaves its stale tracking entry behind in guest_s2_mappings. On 16kB or 64kB hosts, a 4kB hint also triggers WARN_ON(size & ~PAGE_MASK) in kvm_stage2_unmap_range().
A TTL hint smaller than PAGE_SIZE or the mapped entry is wrong. Check it against PAGE_SIZE, the shadow stage-2 leaf size, and the guest block size encoded in KVM_NV_GUEST_MAP_SZ. If it is too small, ignore the hint and fall through to the unhinted invalidation path.
When falling back to the maximum mapping extent with stage-2 disabled, size it from PAGE_SIZE rather than VTCR_EL2.TG0 so 1:1 shadow blocks are still covered.
--- arch/arm64/kvm/nested.c | 76 +++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 21 deletions(-)
arch/arm64/kvm/nested.c +55 -21
@@ -610,6 +610,19 @@ static u8 pgshift_level_to_ttl(u16 shift, s8 level)return ttl; } +static u8 vtcr_tg0_to_ttl_gran (struct kvm_s2_mmu *mmu) +{ +switch (vtcr_to_tg0_pgsize (kvm_s2_mmu_to_kvm (mmu), mmu->tlb_vtcr)) { +case SZ_4K : +return TLBI_TTL_TG_4K <<2 ; +case SZ_16K : +return TLBI_TTL_TG_16K <<2 ; +case SZ_64K : +default :/* IMPDEF: treat any other value as 64k */ +return TLBI_TTL_TG_64K <<2 ; + } +} +/* * Compute the equivalent of the TTL field by parsing the shadow PT. The * granule size is extracted from the cached VTCR_EL2.TG0 while the level is
@@ -624,18 +637,7 @@ static u8 get_guest_mapping_ttl(struct kvm_s2_mmu *mmu, u64 addr)lockdep_assert_held_write (&kvm_s2_mmu_to_kvm (mmu)->mmu_lock); -switch (tg0_size) { -case SZ_4K : - ttl = (TLBI_TTL_TG_4K <<2 ); -break ; -case SZ_16K : - ttl = (TLBI_TTL_TG_16K <<2 ); -break ; -case SZ_64K : -default :/* IMPDEF: treat any other value as 64k */ - ttl = (TLBI_TTL_TG_64K <<2 ); -break ; - } + ttl =vtcr_tg0_to_ttl_gran (mmu); tmp = addr;
@@ -686,29 +688,61 @@ static u8 get_guest_mapping_ttl(struct kvm_s2_mmu *mmu, u64 addr)return 0 ; } +static bool tlbi_ttl_too_small (struct kvm_s2_mmu *mmu,u64 addr, +unsigned long max_size) +{ +u8 guest_level; +kvm_pte_t pte; +s8 level; + +lockdep_assert_held_write (&kvm_s2_mmu_to_kvm (mmu)->mmu_lock); + +if (max_size <PAGE_SIZE ) +return true; + +if (kvm_pgtable_get_leaf (mmu->pgt, addr, &pte, &level,0 )) +return false; + +if (!kvm_pte_valid (pte)) +return false; + +if (kvm_granule_size (level) > max_size) +return true; + + guest_level =FIELD_GET (KVM_NV_GUEST_MAP_SZ , pte); +if (guest_level && +__ttl_to_size (vtcr_tg0_to_ttl_gran (mmu) | guest_level) > max_size) +return true; + +return false; +} +unsigned long compute_tlb_inval_range (struct kvm_s2_mmu *mmu,u64 val) { +u64 addr = (val &GENMASK_ULL (35 ,0 )) <<12 ;struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); +u8 ttl =FIELD_GET (TLBI_TTL_MASK , val);unsigned long max_size; -u8 ttl; - - ttl =FIELD_GET (TLBI_TTL_MASK , val); -if (!ttl || !kvm_has_feat (kvm,ID_AA64MMFR2_EL1 ,TTL ,IMP )) { -/* No TTL, check the shadow S2 for a hint */ -u64 addr = (val &GENMASK_ULL (35 ,0 )) <<12 ; - ttl =get_guest_mapping_ttl (mmu, addr); +if (ttl &&kvm_has_feat (kvm,ID_AA64MMFR2_EL1 ,TTL ,IMP )) { + max_size =__ttl_to_size (ttl); +if (max_size && !tlbi_ttl_too_small (mmu, addr, max_size)) +return max_size; } +/* No usable TTL hint, check the shadow S2 for a hint */ + ttl =get_guest_mapping_ttl (mmu, addr); +/* * Don't use the default 1GB fallback, as we can adapt to the * max mapping size we allow at S2. */ max_size =__ttl_to_size (ttl); -if (!max_size) { +if (max_size <PAGE_SIZE ) {/* Compute the maximum extent of the invalidation */ -switch (vtcr_to_tg0_pgsize (kvm, mmu->tlb_vtcr)) { +switch (mmu->nested_stage2_enabled ? +vtcr_to_tg0_pgsize (kvm, mmu->tlb_vtcr) :PAGE_SIZE ) {case SZ_4K : max_size =SZ_1G ;break ;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH v2 2/2 KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries
In reply to Colton Lewiskvm_record_guest_s2_mapping() unconditionally inserts a new entry into both guest_s2_mappings interval trees. Parallel stage-2 faults on the same IPA or retries after -EAGAIN can therefore insert identical entries for the same nested and canonical IPA range.
Check mmu->guest_s2_mappings under guest_s2_tracking_lock before inserting, and free the new entry if an identical one is already present.
--- arch/arm64/kvm/nested.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
arch/arm64/kvm/nested.c +29 -0
@@ -930,6 +930,30 @@ static unsigned long s2_mapping_to_nested_start(struct kvm_guest_s2_mapping *mapreturn mapping->nested.start & ~S2_MMU_IDX_MASK ; } +static bool guest_s2_mapping_exists (struct kvm_s2_mmu *mmu, +struct kvm_guest_s2_mapping *mapping) +{ +struct interval_tree_node *node; + +for (node =interval_tree_iter_first (&mmu->guest_s2_mappings, + mapping->nested.start, + mapping->nested.last); + node; + node =interval_tree_iter_next (node, mapping->nested.start, + mapping->nested.last)) { +struct kvm_guest_s2_mapping *curr; + + curr = container_of(node, struct kvm_guest_s2_mapping, nested); +if (curr->nested.start == mapping->nested.start && + curr->nested.last == mapping->nested.last && + curr->canonical.start == mapping->canonical.start && + curr->canonical.last == mapping->canonical.last) +return true; + } + +return false; +} +void kvm_record_guest_s2_mapping (struct kvm_s2_mmu *mmu,gpa_t canonical_ipa,gpa_t nested_ipa,size_t map_size,struct kvm_guest_s2_mapping *mapping)
@@ -950,6 +974,11 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,tag_s2_mapping_mmu (mapping, mmu);guard (spinlock)(&kvm->arch.guest_s2_tracking_lock); +if (guest_s2_mapping_exists (mmu, mapping)) { +kfree (mapping); +return ; + } +interval_tree_insert (&mapping->nested, &mmu->guest_s2_mappings);interval_tree_insert (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); }
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewRe: [PATCH v2 2/2] KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: arm64: nv: missing Fixes trailer for kvm_record_guest_s2_mapping() leak --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: arm64: nv: missing Fixes trailer for kvm_record_guest_s2_mapping() leak
--
commit 948b2b35956480d91765ab46d7a685fc1f8ebecc
Author: Colton Lewis <[email protected]>
Quoted 7 lines
KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries
kvm_record_guest_s2_mapping()unconditionally inserts a new entry into bothguest_s2_mappingsinterval trees. Parallel stage-2 faults on the same IPA or retries after-EAGAINcan therefore insert identical entries for the same nested and canonical IPA range.
Quoted 5 lines
Check
mmu->guest_s2_mappingsunderguest_s2_tracking_lockbefore inserting, and free the new entry if an identical one is already present.Signed-off-by Colton Lewis <[email protected]>
[Severity: Low]
This fixes unconditional insertion in kvm_record_guest_s2_mapping(). Should the commit message include a Fixes tag:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2