From: Wei-Lin Chang <weilin.chang@arm.com>
To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org
Cc: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
Fuad Tabba <fuad.tabba@linux.dev>,
Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Itaru Kitayama <itaru.kitayama@fujitsu.com>,
Wang Han <wanghan@linux.alibaba.com>,
Shuai Xue <xueshuai@linux.alibaba.com>,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Wei-Lin Chang <weilin.chang@arm.com>
Subject: [PATCH v6 5/7] KVM: arm64: nv: Avoid full shadow stage-2 unmap
Date: Tue, 15 Sep 2026 16:43:03 +0100 [thread overview]
Message-ID: <20260915154305.3852871-6-weilin.chang@arm.com> (raw)
In-Reply-To: <20260915154305.3852871-1-weilin.chang@arm.com>
With guest stage-2 tracking in place, we can improve MMU notifier unmaps
from unmapping all existing shadow stage-2 mappings to only unmapping
the ones affected by the given canonical IPA range.
Signed-off-by: Wei-Lin Chang <weilin.chang@arm.com>
---
arch/arm64/include/asm/kvm_nested.h | 2 ++
arch/arm64/kvm/mmu.c | 7 ++--
arch/arm64/kvm/nested.c | 50 +++++++++++++++++++++++++++--
3 files changed, 53 insertions(+), 6 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 954f532bc11b..0f899addafa8 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -171,6 +171,8 @@ extern int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu,
struct kvm_s2_trans *trans);
extern int kvm_inject_s2_fault(struct kvm_vcpu *vcpu, u64 esr_el2);
extern void kvm_nested_s2_wp(struct kvm *kvm);
+extern void kvm_nested_unmap_cipa_range(struct kvm *kvm, gpa_t cipa,
+ size_t unmap_size, bool may_block);
extern void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block);
extern void kvm_nested_s2_flush(struct kvm *kvm);
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index c46e92d67f04..b8ba2d09ed96 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -2525,8 +2525,9 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
range->may_block);
-
- kvm_nested_s2_unmap(kvm, range->may_block);
+ kvm_nested_unmap_cipa_range(kvm, range->start << PAGE_SHIFT,
+ (range->end - range->start) << PAGE_SHIFT,
+ range->may_block);
return false;
}
@@ -2808,7 +2809,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
write_lock(&kvm->mmu_lock);
kvm_stage2_unmap_range(&kvm->arch.mmu, gpa, size, true);
- kvm_nested_s2_unmap(kvm, true);
+ kvm_nested_unmap_cipa_range(kvm, gpa, size, true);
write_unlock(&kvm->mmu_lock);
}
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 92a9a32f9f2b..4cbb4f1b8b35 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -906,9 +906,8 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa,
gpa_t nipa_end = nipa + size - 1;
/*
- * Guest s2 tracking interval trees are only accessed while holding the
- * mmu_lock, hence we don't have to take guest_s2_tracking_lock if the
- * mmu_lock is held for write.
+ * See kvm_nested_unmap_cipa_range() for why guest_s2_tracking_lock
+ * isn't taken here.
*/
lockdep_assert_held_write(&kvm->mmu_lock);
@@ -1342,6 +1341,51 @@ void kvm_nested_s2_wp(struct kvm *kvm)
kvm_invalidate_vncr_ipa(kvm, 0, BIT(kvm->arch.mmu.pgt->ia_bits));
}
+void kvm_nested_unmap_cipa_range(struct kvm *kvm, gpa_t cipa, size_t unmap_size,
+ bool may_block)
+{
+ gpa_t cipa_end = cipa + unmap_size - 1;
+ struct kvm_guest_s2_mapping *mapping;
+ struct interval_tree_node *node;
+ size_t mapping_size;
+
+ /*
+ * Guest s2 tracking interval trees are only accessed while holding the
+ * mmu_lock, hence we don't have to take guest_s2_tracking_lock if the
+ * mmu_lock is held for write. This saves us from having to manually
+ * lock/unlock guest_s2_tracking_lock below around
+ * cond_resched_rwlock_write().
+ */
+ lockdep_assert_held_write(&kvm->mmu_lock);
+
+ if (!kvm->arch.nested_mmus_size)
+ return;
+
+ while ((node = interval_tree_iter_first(&kvm->arch.mmu.guest_s2_mappings,
+ cipa, cipa_end))) {
+ mapping = container_of(node, struct kvm_guest_s2_mapping, canonical);
+ mapping_size = mapping->nested.last - mapping->nested.start + 1;
+
+ /* We could race against MMU teardown, which frees mmu->pgt. */
+ if (mapping->nested_mmu->pgt) {
+ if (WARN_ON_ONCE(kvm_pgtable_stage2_unmap(mapping->nested_mmu->pgt,
+ mapping->nested.start,
+ mapping_size)))
+ return;
+
+ interval_tree_remove(&mapping->nested,
+ &mapping->nested_mmu->guest_s2_mappings);
+ }
+ interval_tree_remove(node, &kvm->arch.mmu.guest_s2_mappings);
+ kfree(mapping);
+
+ if (may_block)
+ cond_resched_rwlock_write(&kvm->mmu_lock);
+ }
+
+ kvm_invalidate_vncr_ipa(kvm, cipa, cipa + unmap_size);
+}
+
void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block)
{
int i;
--
2.43.0
next prev parent reply other threads:[~2026-09-15 15:43 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 15:42 [PATCH v6 0/7] KVM: arm64: nv: Implement nested stage-2 reverse map Wei-Lin Chang
2026-09-15 15:42 ` [PATCH v6 1/7] KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map() Wei-Lin Chang
2026-09-15 15:43 ` [PATCH v6 2/7] KVM: arm64: nv: Introduce guest stage-2 tracking structures Wei-Lin Chang
2026-09-15 15:43 ` [PATCH v6 3/7] KVM: arm64: nv: Track guest stage-2 mapping creation Wei-Lin Chang
2026-09-15 15:43 ` [PATCH v6 4/7] KVM: arm64: nv: Track guest stage-2 mapping removal Wei-Lin Chang
2026-09-15 15:43 ` Wei-Lin Chang [this message]
2026-09-15 15:43 ` [PATCH v6 6/7] KVM: arm64: nv: Drop kvm_s2_mmu pointer from kvm_guest_s2_mapping Wei-Lin Chang
2026-09-15 15:43 ` [PATCH v6 7/7] KVM: arm64: Refactor kvm_unmap_gfn_range() with common variables Wei-Lin Chang
2026-09-15 21:49 ` [PATCH v6 0/7] KVM: arm64: nv: Implement nested stage-2 reverse map Itaru Kitayama
2026-09-15 23:22 ` Wei-Lin Chang
2026-09-15 23:27 ` Itaru Kitayama
2026-09-16 7:08 ` Marc Zyngier
2026-09-17 6:51 ` Itaru Kitayama
2026-09-17 7:56 ` Marc Zyngier
2026-09-17 21:46 ` Itaru Kitayama
2026-09-17 13:10 ` Wei-Lin Chang
2026-09-15 22:49 ` Oliver Upton
2026-09-16 4:58 ` Itaru Kitayama
2026-09-16 7:04 ` Marc Zyngier
2026-09-16 10:08 ` Wei-Lin Chang
2026-09-20 1:33 ` Shuai Xue
2026-09-24 20:41 ` Marc Zyngier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260915154305.3852871-6-weilin.chang@arm.com \
--to=weilin.chang@arm.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=itaru.kitayama@fujitsu.com \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=wanghan@linux.alibaba.com \
--cc=will@kernel.org \
--cc=xueshuai@linux.alibaba.com \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®