KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap
PATCH 1/1 KVM: arm64: nv: Fix guest stage-2 mapping leak on sub-block unmap
When a shadow stage-2 block mapping (for example, 2M at PMD level) is partially invalidated by a smaller guest unmap such as a 4K TLBI IPAS2E1IS, kvm_pgtable_stage2_unmap() shatters and clears the entire block descriptor in mmu->pgt. However, kvm_remove_guest_s2_mappings() only removes a struct kvm_guest_s2_mapping node when its entire [nested_start, nested.last] span lies within the unmap range. As a result, every sub-block invalidation followed by a re-fault leaks a stale block-sized tracking node in mmu->guest_s2_mappings and kvm->arch.mmu.guest_s2_mappings until the shadow MMU is destroyed, allowing an L1 guest to grow host kernel memory and degrade subsequent TLBI IPAS2E1IS walks to O(N) under write_lock(&kvm->mmu_lock).
Conversely, unconditionally removing any overlapping tracking node on a sub-block unmap would be unsafe when a shadow block mapping has been split into a child table (such as on a page-granule fault under dirty logging or a racing shared walk returning -EAGAIN), because sibling sub-page PTEs outside the unmapped range remain live in mmu->pgt and still rely on the block-sized tracking node for host MMU notifier unmaps.
Since stage2_unmap_walker() unlinks empty child page tables in KVM_PGTABLE_WALK_TABLE_POST as soon as their last valid entry is cleared, check mmu->pgt via kvm_pgtable_get_leaf() on partial-overlap
candidates: when no valid entry or split sub-table remains at or below the recorded mapping granule (!kvm_pte_valid(pte) && kvm_granule_size(level) >= map_size), remove and free the tracking node; otherwise retain it to cover surviving split sub-entries.
In addition, suppress exact-match duplicate insertions in kvm_record_guest_s2_mapping() when concurrent faults or -EAGAIN retries record an identical (nested, canonical) range on the same shadow MMU.
--- arch/arm64/kvm/nested.c | 68 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 7 deletions(-)
arch/arm64/kvm/nested.c +61 -7
@@ -896,6 +896,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)
@@ -915,9 +939,32 @@ 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); -interval_tree_insert (&mapping->nested, &mmu->guest_s2_mappings); -interval_tree_insert (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); + scoped_guard(spinlock, &kvm->arch.guest_s2_tracking_lock) { +if (!guest_s2_mapping_exists (mmu, mapping)) { +interval_tree_insert (&mapping->nested, + &mmu->guest_s2_mappings); +interval_tree_insert (&mapping->canonical, + &kvm->arch.mmu.guest_s2_mappings); + mapping =NULL ; + } + } + +kfree (mapping); +} + +static bool guest_s2_mapping_unmapped (struct kvm_s2_mmu *mmu, +gpa_t nested_start,size_t map_size) +{ +kvm_pte_t pte; +s8 level; + +if (!mmu->pgt) +return true; + +if (kvm_pgtable_get_leaf (mmu->pgt, nested_start, &pte, &level,0 )) +return false; + +return !kvm_pte_valid (pte) &&kvm_granule_size (level) >= map_size; } void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
@@ -937,17 +984,24 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, node =interval_tree_iter_first (&mmu->guest_s2_mappings, nipa, nipa_end);while (node) {unsigned long nested_start; +size_t map_size; next =interval_tree_iter_next (node, nipa, nipa_end); mapping = container_of(node, struct kvm_guest_s2_mapping, nested);/* -* Tracking must be conservative on removal, only remove -* mappings that are within the unmap range. +* A sub-block unmap tears down an unsplit shadow block entry +* in mmu->pgt, whereas a split block may still have valid +* sub-page entries outside [nipa, nipa_end]. Remove the +* tracking entry when it lies within the unmap range or no +* mappings remain across its span in mmu->pgt. */ nested_start =s2_mapping_to_nested_start (mapping); -if (nipa <= nested_start && nipa_end >= mapping->nested.last) { -interval_tree_remove (&mapping->nested, &mmu->guest_s2_mappings); + map_size = mapping->nested.last - nested_start +1 ; +if ((nipa <= nested_start && nipa_end >= mapping->nested.last) || +guest_s2_mapping_unmapped (mmu, nested_start, map_size)) { +interval_tree_remove (&mapping->nested, + &mmu->guest_s2_mappings);interval_tree_remove (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);kfree (mapping);
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Quoted 3 lines · Colton Lewis
When a shadow stage-2 block mapping (for example, 2M at PMD level) is partially invalidated by a smaller guest unmap such as a 4K TLBI IPAS2E1IS,
kvm_pgtable_stage2_unmap()shatters and clears the entire
"shatters" sounds like it breaks the block into page mappings, so maybe just "clears the entire block.."?
Quoted 1 line
block descriptor in
mmu->pgt. However,kvm_remove_guest_s2_mappings()
nit: I think "the shadow page table" is clearer than "mmu->pgt"
Quoted 3 lines
only removes a struct
kvm_guest_s2_mappingnode when its entire [nested_start, nested.last] span lies within the unmap range. As a result, every sub-block invalidation followed by a re-fault leaks a
Doesn't the block tracking node get leaked regardless of whether a re-fault happens?
Quoted 10 lines
stale block-sized tracking node in
mmu->guest_s2_mappingsandkvm->arch.mmu.guest_s2_mappingsuntil the shadow MMU is destroyed, allowing an L1 guest to grow host kernel memory and degrade subsequent TLBI IPAS2E1IS walks to O(N) underwrite_lock(&kvm->mmu_lock).Conversely, unconditionally removing any overlapping tracking node on a sub-block unmap would be unsafe when a shadow block mapping has been split into a child table (such as on a page-granule fault under dirty logging or a racing shared walk returning
-EAGAIN), because sibling sub-page PTEs outside the unmapped range remain live inmmu->pgtand
What are sub-page PTEs? :P
Quoted 7 lines
still rely on the block-sized tracking node for host MMU notifier unmaps.
Since
stage2_unmap_walker()unlinks empty child page tables inKVM_PGTABLE_WALK_TABLE_POSTas soon as their last valid entry is cleared, checkmmu->pgtviakvm_pgtable_get_leaf()on partial-overlap
candidates: when no valid entry or split sub-table remains at or below
You use "or" here. ^^
Quoted 1 line
the recorded mapping granule (!kvm_pte_valid(pte) &&
but this is an AND? ^^
so I don't quite understand.
Quoted 2 lines
kvm_granule_size(level)>=map_size), remove and free the tracking node; otherwise retain it to cover surviving split sub-entries.
"sub-entries" doesn't really make sense to me.. Maybe "... to cover other entries within the range"?
For this third paragraph can you improve it by stating how the problem is fixed upfront? The first two paragraphs describe the problem, then you started to talk about stage2_unmap_walker all of a sudden (which I'm not sure how much it helps), and the fix itself is not emphasised. The combination makes it pretty difficult to get what the patch is doing.
Quoted 4 lines
In addition, suppress exact-match duplicate insertions in
kvm_record_guest_s2_mapping()when concurrent faults or-EAGAINretries record an identical (nested, canonical) range on the same shadow MMU.
I believe this is better split into its own patch, it's doing another thing.
Quoted 3 lines
Fixes 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal")Fixes c2778d14cd2f ("KVM: arm64: nv: Track guest stage-2 mapping creation")
Since there isn't a real bug in the original implementation do we need these tags?
Quoted 48 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/nested.c | 68 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 7 deletions(-)arch/arm64/kvm/nested.c +25
-3@@ -896,6 +896,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)@@ -915,9 +939,32 @@ 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); -interval_tree_insert (&mapping->nested, &mmu->guest_s2_mappings); -interval_tree_insert (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); + scoped_guard(spinlock, &kvm->arch.guest_s2_tracking_lock) {
arch/arm64/kvm/nested.c void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa, · line 952
Quoted 10 lines
arch/arm64/kvm/nested.c +10
-0+if (!guest_s2_mapping_exists (mmu, mapping)) { +interval_tree_insert (&mapping->nested, + &mmu->guest_s2_mappings); +interval_tree_insert (&mapping->canonical, + &kvm->arch.mmu.guest_s2_mappings); + mapping =NULL ; + } + } + +kfree (mapping);
In my understanding kfree() can be called while holding locks, so we don't need the extra level of indentation.
Quoted 22 lines
arch/arm64/kvm/nested.c +15
-0+} + +static bool guest_s2_mapping_unmapped (struct kvm_s2_mmu *mmu, +gpa_t nested_start,size_t map_size) +{ +kvm_pte_t pte; +s8 level; + +if (!mmu->pgt) +return true; + +if (kvm_pgtable_get_leaf (mmu->pgt, nested_start, &pte, &level,0 )) +return false; + +return !kvm_pte_valid (pte) &&kvm_granule_size (level) >= map_size; } void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,@@ -937,17 +984,24 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, node =interval_tree_iter_first (&mmu->guest_s2_mappings, nipa, nipa_end);while (node) {unsigned long nested_start;
arch/arm64/kvm/nested.c void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, · line 994
Quoted 10 lines
arch/arm64/kvm/nested.c +3
-2+size_t map_size; next =interval_tree_iter_next (node, nipa, nipa_end); mapping = container_of(node, struct kvm_guest_s2_mapping, nested);/* -* Tracking must be conservative on removal, only remove -* mappings that are within the unmap range. +* A sub-block unmap tears down an unsplit shadow block entry +* in mmu->pgt, whereas a split block may still have valid
Does "split block" make sense? Either it is a block mapping or a page mapping.
arch/arm64/kvm/nested.c void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, · line 995
Quoted 1 line
arch/arm64/kvm/nested.c +1
-0+* sub-page entries outside [nipa, nipa_end]. Remove the
Same "sub-page entries" wording issue here.
Thanks,
Wei-Lin Chang
Quoted 19 lines
arch/arm64/kvm/nested.c +7
-2+* tracking entry when it lies within the unmap range or no +* mappings remain across its span in mmu->pgt. */ nested_start =s2_mapping_to_nested_start (mapping); -if (nipa <= nested_start && nipa_end >= mapping->nested.last) { -interval_tree_remove (&mapping->nested, &mmu->guest_s2_mappings); + map_size = mapping->nested.last - nested_start +1 ; +if ((nipa <= nested_start && nipa_end >= mapping->nested.last) || +guest_s2_mapping_unmapped (mmu, nested_start, map_size)) { +interval_tree_remove (&mapping->nested, + &mmu->guest_s2_mappings);interval_tree_remove (&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);kfree (mapping);base-commit:
4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccdSignature · 1 line
2.56.1.809.ga0b61035b5-goog
Quoted 4 lines · Wei-Lin Chang
Quoted 1 line
result, every sub-block invalidation followed by a re-fault leaks a
Doesn't the block tracking node get leaked regardless of whether a re-fault happens?
Yes, the stale entry is left behind on the first TLBI. Re-faulting is just what lets L1 accumulate additional entries.
Quoted 5 lines
For this third paragraph can you improve it by stating how the problem is fixed upfront? The first two paragraphs describe the problem, then you started to talk about
stage2_unmap_walkerall of a sudden (which I'm not sure how much it helps), and the fix itself is not emphasised. The combination makes it pretty difficult to get what the patch is doing.
Agreed, the commit message was unclear. In v2 I will drop the kvm_remove_guest_s2_mappings() approach in favor of ignoring TLBI IPAS2E1 when the guest-supplied TTL is smaller than the mapped granule (as Marc suggested), and rewrite the commit messages to be shorter and clearer.
Quoted 6 lines
Quoted 3 lines
In addition, suppress exact-match duplicate insertions in
kvm_record_guest_s2_mapping()when concurrent faults or-EAGAINretries record an identical (nested, canonical) range on the same shadow MMU.I believe this is better split into its own patch, it's doing another thing.
Makes sense. I will split it into a separate patch in v2.
Quoted 5 lines
Quoted 2 lines
Fixes 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal")Fixes c2778d14cd2f ("KVM: arm64: nv: Track guest stage-2 mapping creation")Since there isn't a real bug in the original implementation do we need these tags?
Good point. I will drop the Fixes tag on the duplicate-avoidance patch and point the TTL patch at the commit that added FEAT_TTL handling instead.
Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/nested.c +6
-0+ scoped_guard(spinlock, &kvm->arch.guest_s2_tracking_lock) { +if (!guest_s2_mapping_exists (mmu, mapping)) { +interval_tree_insert (&mapping->nested, + &mmu->guest_s2_mappings); +interval_tree_insert (&mapping->canonical, + &kvm->arch.mmu.guest_s2_mappings);
Quoted 8 lines
arch/arm64/kvm/nested.c void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa, · line 952
Quoted 5 lines
arch/arm64/kvm/nested.c +5
-0+ mapping =NULL ; + } + } + +kfree (mapping);In my understanding
kfree()can be called while holding locks, so we don't need the extra level of indentation.
Yes, kfree() is safe under the spinlock. I will keep guard(spinlock) and call kfree() directly there.
Marc Zyngier
maintainerQuoted 4 lines · Colton Lewis
When a shadow stage-2 block mapping (for example, 2M at PMD level) is partially invalidated by a smaller guest unmap such as a 4K TLBI IPAS2E1IS,
What does that mean? If we have a 2MB shadow mapping, then the guest cannot have something smaller in its own PTs. And how do you get a "4K TLBI IPAS2E1IS"? TTL == {4kB, L3}? If that's the case, we have every right to *ignore* this TLBI, because it cannot possibly reflect the original state of the PTs. If this is an unqualified address (no TTL), then the whole block *must* go.
There is no such thing as "partial invalidation".
Quoted 2 lines
kvm_pgtable_stage2_unmap()shatters and clears the entire block descriptor inmmu->pgt.
Shatters? Or clears the entire block? It cannot be "and" because the two are polar opposites.
Quoted 3 lines
However,
kvm_remove_guest_s2_mappings()
only removes a structkvm_guest_s2_mappingnode when its entire [nested_start, nested.last] span lies within the unmap range.
What do you call "the unmap range"? What we have removed from the shadow S2? What the guest gives us as part of the TLBI instruction? Something else?
Quoted 4 lines
As a
result, every sub-block invalidation followed by a re-fault leaks a stale block-sized tracking node inmmu->guest_s2_mappingsandkvm->arch.mmu.guest_s2_mappingsuntil the shadow MMU is destroyed,
Leaks? A leak is something that is still allocated, but not linked to an existing data structure, and therefore not recoverable. This looks to me like exact opposite of a leak...
Quoted 2 lines
allowing an L1 guest to grow host kernel memory and degrade subsequent TLBI IPAS2E1IS walks to O(N) under
write_lock(&kvm->mmu_lock).
Honestly, this wall of text is not an explanation. That's just a whole lot of LLM gunk that you have generously copy pasted.
Quoted 8 lines
Conversely, unconditionally removing any overlapping tracking node on a sub-block unmap would be unsafe when a shadow block mapping has been split into a child table (such as on a page-granule fault under dirty logging or a racing shared walk returning
-EAGAIN), because sibling sub-page PTEs outside the unmapped range remain live inmmu->pgtand still rely on the block-sized tracking node for host MMU notifier unmaps.
-ENOPARSE. Sub-page PTEs sounds like a great idea.
I stopped reading here. Please come back with an actual description of the problem that you will have produced yourself, or provide the original LLM report (it cannot be much worse than the above).
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
Quoted 3 lines · Marc Zyngier
I stopped reading here. Please come back with an actual description of the problem that you will have produced yourself, or provide the original LLM report (it cannot be much worse than the above).
Sorry about that. I will clean up the slop.