From: Colton Lewis <coltonlewis@google.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>
Cc: Wei-Lin Chang <weilin.chang@arm.com>,
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>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries
Date: Wed, 2 Dec 2026 00:13:34 +0000 [thread overview]
Message-ID: <20261202001335.2306714-2-coltonlewis@google.com> (raw)
In-Reply-To: <20261202001335.2306714-1-coltonlewis@google.com>
kvm_pgtable_stage2_unmap_tracked() can be called with a size smaller than
a tracked block mapping (for example, from a TLBI RIPAS2E1IS range or a
TLBI IPAS2E1IS with a 4kB TTL hint on a 2MB block).
kvm_pgtable_stage2_unmap() clears the entire block in the shadow stage-2
page table, but kvm_remove_guest_s2_mappings() only removes tracking
entries fully enclosed by the unmap range, leaving a stale
kvm_guest_s2_mapping entry behind in guest_s2_mappings.
Partially overlapping entries cannot be removed unconditionally, because
the tracked range may instead contain smaller page mappings in a child
table (such as under dirty logging) that remain valid outside the
unmapped range.
On a partial overlap, look up nested_start in the shadow page table with
kvm_pgtable_get_leaf() and remove the tracking entry when no valid PTE or
child table remains at its tracked granule size (!kvm_pte_valid(pte) &&
kvm_granule_size(level) >= map_size). Since kvm_pgtable_stage2_unmap()
has already unlinked any empty child tables, a single lookup at
nested_start is enough to tell whether the entire tracked range is empty.
Fixes: 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal")
Signed-off-by: Colton Lewis <coltonlewis@google.com>
---
arch/arm64/kvm/nested.c | 37 ++++++++++++++++++++++++++++++++-----
1 file changed, 32 insertions(+), 5 deletions(-)
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 78d5b18dbd2d6..3dcf2ea54873e 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -928,6 +928,27 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa,
interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings);
}
+/*
+ * Each tracked range [nested_start, nested_start + map_size - 1] is aligned
+ * to one host stage-2 granule. After kvm_pgtable_stage2_unmap() unlinks any
+ * empty child tables, an invalid leaf at or above that granule level means
+ * neither a block mapping nor a child table remains in the tracked range.
+ */
+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,
size_t size)
{
@@ -944,18 +965,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;
+ unsigned long nested_start, nested_end;
+ 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.
+ * Remove the tracking entry if it is fully enclosed by the
+ * unmap range, or if a partial unmap left no shadow mappings
+ * anywhere in its tracked range.
*/
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);
+ nested_end = mapping->nested.last;
+ map_size = nested_end - nested_start + 1;
+ if ((nipa <= nested_start && nipa_end >= nested_end) ||
+ 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);
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-02 0:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 0:13 [PATCH v3 0/2] KVM: arm64: nv: Fix stale and duplicate guest S2 tracking entries Colton Lewis
2026-12-02 0:13 ` Colton Lewis [this message]
2026-12-02 0:13 ` [PATCH v3 2/2] KVM: arm64: nv: Avoid duplicate guest stage-2 mapping entries Colton Lewis
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=20261202001335.2306714-2-coltonlewis@google.com \
--to=coltonlewis@google.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.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=weilin.chang@arm.com \
--cc=will@kernel.org \
--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