[PATCH] RISC-V: KVM: Age the whole range in kvm_age_gfn() and kvm_test_age_gfn()
Li Jinyue
jinyue.li at linux.alibaba.com
Tue Nov 24 00:48:23 PST 2026
kvm_age_gfn() and kvm_test_age_gfn() expect the MMU notifier range to
cover exactly one G-stage leaf. They warn when the size is not
PAGE_SIZE, PMD_SIZE or PUD_SIZE, and then only look at the leaf that
maps range->start.
The range is the host mapping being aged, clipped to the memslot, so
there is no such guarantee. folio_referenced_one() ages all PTEs of a
PTE-mapped large folio in one call, and a PMD-mapped THP backing a
memslot smaller than PMD_SIZE is clipped to the memslot size. In both
cases the range spans several G-stage leaves. syzkaller hit the second
case on a 6.6-based kernel with THP-backed guest memory split into
memslots of a few pages, followed by MADV_PAGEOUT. The line number
below is from that kernel; the same code in mainline is at
arch/riscv/kvm/mmu.c:362 and :382:
WARNING: CPU: 1 PID: 7943 at arch/riscv/kvm/mmu.c:586 kvm_age_gfn+0x14c/0x20a
...
kvm_age_gfn+0x14c/0x20a
kvm_mmu_notifier_clear_flush_young+0x186/0x45c
__mmu_notifier_clear_flush_young+0xc0/0x17a
folio_referenced_one+0x4b8/0x81e
rmap_walk_anon+0x400/0x9d0
folio_referenced+0x216/0x3ae
shrink_folio_list+0x148a/0x31c4
reclaim_folio_list.constprop.0+0x11c/0x3a0
reclaim_pages+0x2c6/0x420
madvise_cold_or_pageout_pte_range+0xff6/0x16e0
Besides the warning, guest accesses to any page in the range other than
the first one are missed, so reclaim may treat recently used guest
memory as old.
Add kvm_riscv_gstage_test_clear_young(), which walks the range the same
way kvm_riscv_gstage_wp_range() does and tests or clears the accessed
bit of every leaf it finds, and use it in both handlers. arm64 had the
same problem and fixed it in commit df6556adf27b ("KVM: arm64: Correctly
handle page aging notifiers for unaligned memslot").
Found by syzkaller.
Fixes: 9955371cc014 ("RISC-V: KVM: Implement MMU notifiers")
Cc: stable at vger.kernel.org
Signed-off-by: Li Jinyue <jinyue.li at linux.alibaba.com>
---
arch/riscv/include/asm/kvm_gstage.h | 3 +++
arch/riscv/kvm/gstage.c | 30 +++++++++++++++++++++++++++++
arch/riscv/kvm/mmu.c | 26 ++++++-------------------
3 files changed, 39 insertions(+), 20 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index aaf080ba1b77..04e6133665c2 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -82,6 +82,9 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
bool kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end);
+bool kvm_riscv_gstage_test_clear_young(struct kvm_gstage *gstage, gpa_t start,
+ gpa_t end, bool mkold);
+
bool kvm_riscv_gstage_wp_pt_masked(struct kvm_gstage *gstage, gfn_t base_gfn,
unsigned long mask);
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index be70607a33ea..7cc66880acde 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -485,6 +485,36 @@ bool kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end
return flush;
}
+bool kvm_riscv_gstage_test_clear_young(struct kvm_gstage *gstage, gpa_t start,
+ gpa_t end, bool mkold)
+{
+ int ret;
+ pte_t *ptep;
+ u32 ptep_level;
+ bool found_leaf;
+ gpa_t addr = start;
+ unsigned long page_size;
+ bool young = false;
+
+ while (addr < end) {
+ found_leaf = kvm_riscv_gstage_get_leaf(gstage, addr, &ptep, &ptep_level);
+ ret = gstage_level_to_page_size(gstage, ptep_level, &page_size);
+ if (ret)
+ break;
+
+ if (found_leaf) {
+ if (mkold)
+ young |= ptep_test_and_clear_young(NULL, 0, ptep);
+ else
+ young |= pte_young(ptep_get(ptep));
+ }
+
+ addr = ALIGN(addr + 1, page_size);
+ }
+
+ return young;
+}
+
static inline void clear_huge_mask(unsigned long *mask, unsigned long page_size,
gfn_t base_gfn, gpa_t addr)
{
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 6b77891d4582..26b85a0dcdb7 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -351,42 +351,28 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
{
- pte_t *ptep;
- u32 ptep_level = 0;
- u64 size = (range->end - range->start) << PAGE_SHIFT;
struct kvm_gstage gstage;
if (!kvm->arch.pgd)
return false;
- WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
-
kvm_riscv_gstage_init(&gstage, kvm);
- if (!kvm_riscv_gstage_get_leaf(&gstage, range->start << PAGE_SHIFT,
- &ptep, &ptep_level))
- return false;
-
- return ptep_test_and_clear_young(NULL, 0, ptep);
+ return kvm_riscv_gstage_test_clear_young(&gstage,
+ range->start << PAGE_SHIFT,
+ range->end << PAGE_SHIFT, true);
}
bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
{
- pte_t *ptep;
- u32 ptep_level = 0;
- u64 size = (range->end - range->start) << PAGE_SHIFT;
struct kvm_gstage gstage;
if (!kvm->arch.pgd)
return false;
- WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
-
kvm_riscv_gstage_init(&gstage, kvm);
- if (!kvm_riscv_gstage_get_leaf(&gstage, range->start << PAGE_SHIFT,
- &ptep, &ptep_level))
- return false;
-
- return pte_young(ptep_get(ptep));
+ return kvm_riscv_gstage_test_clear_young(&gstage,
+ range->start << PAGE_SHIFT,
+ range->end << PAGE_SHIFT, false);
}
static bool fault_supports_gstage_huge_mapping(struct kvm_memory_slot *memslot,
--
2.25.1
More information about the linux-riscv
mailing list