summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOliver Upton <oupton@kernel.org>2025-11-19 14:11:50 -0800
committerOliver Upton <oupton@kernel.org>2025-11-19 14:11:50 -0800
commit156f70afcfecfc45be5fdc2e4adebc5ea70a93b0 (patch)
tree98d34ba699f417b12b3a49b3cf894ca5ce43c1dc
parent85f329df293119d6ba1a26453d109556631081a4 (diff)
downloadkernel-156f70afcfecfc45be5fdc2e4adebc5ea70a93b0.tar.gz
KVM: arm64: Only drop references on empty tables in stage2_free_walker
A subsequent change to the way KVM frees stage-2s will invoke the free walker on sub-ranges of the VM's IPA space, meaning there's potential for only partially visiting a table's PTEs. Split the leaf and table visitors and only drop references on a table when the page count reaches 1, implying there are no valid PTEs that need to be visited. Invalidate the table PTE to avoid traversing the stale reference. Link: https://msgid.link/20251113052452.975081-2-rananta@google.com Signed-off-by: Oliver Upton <oupton@kernel.org>
-rw-r--r--arch/arm64/kvm/hyp/pgtable.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index c351b4abd5db..6d6a23f7dedb 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -1535,20 +1535,46 @@ size_t kvm_pgtable_stage2_pgd_size(u64 vtcr)
return kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
}
-static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
- enum kvm_pgtable_walk_flags visit)
+static int stage2_free_leaf(const struct kvm_pgtable_visit_ctx *ctx)
{
struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
- if (!stage2_pte_is_counted(ctx->old))
+ mm_ops->put_page(ctx->ptep);
+ return 0;
+}
+
+static int stage2_free_table_post(const struct kvm_pgtable_visit_ctx *ctx)
+{
+ struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
+ kvm_pte_t *childp = kvm_pte_follow(ctx->old, mm_ops);
+
+ if (mm_ops->page_count(childp) != 1)
return 0;
+ /*
+ * Drop references and clear the now stale PTE to avoid rewalking the
+ * freed page table.
+ */
mm_ops->put_page(ctx->ptep);
+ mm_ops->put_page(childp);
+ kvm_clear_pte(ctx->ptep);
+ return 0;
+}
- if (kvm_pte_table(ctx->old, ctx->level))
- mm_ops->put_page(kvm_pte_follow(ctx->old, mm_ops));
+static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
+ enum kvm_pgtable_walk_flags visit)
+{
+ if (!stage2_pte_is_counted(ctx->old))
+ return 0;
- return 0;
+ switch (visit) {
+ case KVM_PGTABLE_WALK_LEAF:
+ return stage2_free_leaf(ctx);
+ case KVM_PGTABLE_WALK_TABLE_POST:
+ return stage2_free_table_post(ctx);
+ default:
+ return -EINVAL;
+ }
}
void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt)
.3' of git://git.kernel.org/pub/scm/linux/kernel/g...Linus Torvalds 2023-02-17of: Use preferred of_property_read_* functionsRob Herring 2023-02-17of: Use of_property_present() helperRob Herring 2023-02-02of: Make OF framebuffer device names uniqueMichal Suchanek 2022-10-16of/platform: use of_address_count() helperYang Yingliang 2022-05-25Merge tag 'drm-next-2022-05-25' of git://anongit.freedesktop.org/drm/drmLinus Torvalds 2022-05-25Merge tag 'devicetree-for-5.19' of git://git.kernel.org/pub/scm/linux/kernel/...Linus Torvalds 2022-04-25mtd: phram: Allow probing via reserved-memoryVincent Whitchurch 2022-04-20of: Create platform devices for OF framebuffersThomas Zimmermann 2022-04-04of/platform: Drop static setup of IRQ resource from DT coreLad Prabhakar 2022-02-04misc: open-dice: Add driver to expose DICE data to userspaceDavid Brazdil 2021-12-16of: Move simple-framebuffer device handling from simplefb to ofHector Martin 2021-11-11of: Support using 'mask' in making device bus idRob Herring 2021-11-03Merge tag 'drivers-5.16' of git://git.kernel.org/pub/scm/linux/kernel/git/soc...Linus Torvalds 2021-10-19ARM: 9119/1: amba: Properly handle device probe without IRQ domainWang Kefeng 2021-10-14soc: qcom: smem: Support reserved-memory descriptionBjorn Andersson 2021-06-08iommu: Drop unnecessary of_iommu.h includesRob Herring 2021-03-27of: Add missing 'Return' section in kerneldoc commentsRob Herring 2021-03-23of: platform: Demote kernel-doc abuseLee Jones 2021-02-24Merge tag 'char-misc-5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/g...Linus Torvalds