* [RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
The current G-stage unmap implementation retains unused page tables
until the VM is destroyed.
This series introduces a recursive page table walker, similar to the
arm64 stage-2 page table walker, as a basis for reclaiming unused
G-stage page tables.
Different reclamation policies are used depending on the unmap context:
- Memslot removal: reclaim fully covered page tables that are empty
after unmapping.
- MMU notifier invalidation: reclaim fully covered page tables that
were already empty before unmapping.
- I/O unmapping: keep the existing behavior, as the current use case
only unmaps individual IMSIC pages.
A similar selective reclamation approach has been proposed for the
x86 TDP MMU [1].
Testing
=======
Tests were performed on a RISC-V KVM host running under QEMU TCG
with 2 vCPUs and 2 GiB of memory.
Performance
-----------
Each memslot_perf_test subtest was run in three interleaved
baseline/patched pairs with the slot limit set to 32.
The following table shows the mean time per iteration and the
range of individual paired percentage changes.
Test Before After Change Range
----------------------------------------------------------------------
Map 15.710 s 16.338 s +4.00% -2.32% to +8.47%
Single-page unmap 11.664 s 12.006 s +2.93% -4.16% to +8.04%
Chunked unmap 45.990 ms 45.299 ms -1.50% -2.37% to -1.01%
Move active 15.414 ms 15.186 ms -1.48% -6.86% to +1.50%
Move inactive 13.599 ms 12.688 ms -6.70% -12.57% to -2.70%
RW 169.537 s 172.291 s +1.62% -3.76% to +5.73%
Positive values indicate increased execution time. The RW subtest
completed one iteration per run.
Page table reclamation and the additional TLB synchronization
required before freeing detached page tables introduce overhead
that may affect performance in some workloads.
Memory reclamation
------------------
With one 4 KiB mapping per 2 MiB of GPA space, memslot removal reduced
G-stage page table memory usage from 2084 KiB to 32 KiB for a 1 GiB
memslot, and from 16448 KiB to 32 KiB for an 8 GiB memslot.
For a 1 GiB sparse mapping repeatedly invalidated and accessed again,
the remaining page table memory usage was:
Hot region Before After Reduction
------------------------------------------------
10% 2084 KiB 240 KiB 88.5%
50% 2084 KiB 1060 KiB 49.1%
90% 2084 KiB 1876 KiB 10.0%
Memory usage was measured with additional G-stage page table accounting.
RFC notes
------------------
The mean single-page unmap time increased, although the results
varied across runs. Obtaining consistent performance results under
QEMU TCG has been difficult. I will try to address this issue in
the next revision.
To mitigate the single-page unmap overhead, the current implementation
retains a separate leaf lookup path in kvm_riscv_gstage_unmap_range().
Suggestions on reducing this overhead while sharing the recursive
walker would be appreciated.
Feedback on the reclamation policies and other aspects of the design
is also very welcome.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Tan Chi (3):
KVM: riscv: Introduce a recursive G-stage unmap walker
KVM: riscv: Reclaim G-stage page tables on memslot removal
KVM: riscv: Selectively reclaim G-stage page tables on invalidation
arch/riscv/include/asm/kvm_gstage.h | 14 ++-
arch/riscv/kvm/gstage.c | 180 ++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +-
3 files changed, 174 insertions(+), 28 deletions(-)
base-commit: bc069e58fa1c1da46f7601840f2079a381370893
--
2.53.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
The current G-stage unmap implementation retains unused page tables
until the VM is destroyed.
This series introduces a recursive page table walker, similar to the
arm64 stage-2 page table walker, as a basis for reclaiming unused
G-stage page tables.
Different reclamation policies are used depending on the unmap context:
- Memslot removal: reclaim fully covered page tables that are empty
after unmapping.
- MMU notifier invalidation: reclaim fully covered page tables that
were already empty before unmapping.
- I/O unmapping: keep the existing behavior, as the current use case
only unmaps individual IMSIC pages.
A similar selective reclamation approach has been proposed for the
x86 TDP MMU [1].
Testing
=======
Tests were performed on a RISC-V KVM host running under QEMU TCG
with 2 vCPUs and 2 GiB of memory.
Performance
-----------
Each memslot_perf_test subtest was run in three interleaved
baseline/patched pairs with the slot limit set to 32.
The following table shows the mean time per iteration and the
range of individual paired percentage changes.
Test Before After Change Range
----------------------------------------------------------------------
Map 15.710 s 16.338 s +4.00% -2.32% to +8.47%
Single-page unmap 11.664 s 12.006 s +2.93% -4.16% to +8.04%
Chunked unmap 45.990 ms 45.299 ms -1.50% -2.37% to -1.01%
Move active 15.414 ms 15.186 ms -1.48% -6.86% to +1.50%
Move inactive 13.599 ms 12.688 ms -6.70% -12.57% to -2.70%
RW 169.537 s 172.291 s +1.62% -3.76% to +5.73%
Positive values indicate increased execution time. The RW subtest
completed one iteration per run.
Page table reclamation and the additional TLB synchronization
required before freeing detached page tables introduce overhead
that may affect performance in some workloads.
Memory reclamation
------------------
With one 4 KiB mapping per 2 MiB of GPA space, memslot removal reduced
G-stage page table memory usage from 2084 KiB to 32 KiB for a 1 GiB
memslot, and from 16448 KiB to 32 KiB for an 8 GiB memslot.
For a 1 GiB sparse mapping repeatedly invalidated and accessed again,
the remaining page table memory usage was:
Hot region Before After Reduction
------------------------------------------------
10% 2084 KiB 240 KiB 88.5%
50% 2084 KiB 1060 KiB 49.1%
90% 2084 KiB 1876 KiB 10.0%
Memory usage was measured with additional G-stage page table accounting.
RFC notes
------------------
The mean single-page unmap time increased, although the results
varied across runs. Obtaining consistent performance results under
QEMU TCG has been difficult. I will try to address this issue in
the next revision.
To mitigate the single-page unmap overhead, the current implementation
retains a separate leaf lookup path in kvm_riscv_gstage_unmap_range().
Suggestions on reducing this overhead while sharing the recursive
walker would be appreciated.
Feedback on the reclamation policies and other aspects of the design
is also very welcome.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Tan Chi (3):
KVM: riscv: Introduce a recursive G-stage unmap walker
KVM: riscv: Reclaim G-stage page tables on memslot removal
KVM: riscv: Selectively reclaim G-stage page tables on invalidation
arch/riscv/include/asm/kvm_gstage.h | 14 ++-
arch/riscv/kvm/gstage.c | 180 ++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +-
3 files changed, 174 insertions(+), 28 deletions(-)
base-commit: bc069e58fa1c1da46f7601840f2079a381370893
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
The current G-stage unmap implementation retains unused page tables
until the VM is destroyed.
This series introduces a recursive page table walker, similar to the
arm64 stage-2 page table walker, as a basis for reclaiming unused
G-stage page tables.
Different reclamation policies are used depending on the unmap context:
- Memslot removal: reclaim fully covered page tables that are empty
after unmapping.
- MMU notifier invalidation: reclaim fully covered page tables that
were already empty before unmapping.
- I/O unmapping: keep the existing behavior, as the current use case
only unmaps individual IMSIC pages.
A similar selective reclamation approach has been proposed for the
x86 TDP MMU [1].
Testing
=======
Tests were performed on a RISC-V KVM host running under QEMU TCG
with 2 vCPUs and 2 GiB of memory.
Performance
-----------
Each memslot_perf_test subtest was run in three interleaved
baseline/patched pairs with the slot limit set to 32.
The following table shows the mean time per iteration and the
range of individual paired percentage changes.
Test Before After Change Range
----------------------------------------------------------------------
Map 15.710 s 16.338 s +4.00% -2.32% to +8.47%
Single-page unmap 11.664 s 12.006 s +2.93% -4.16% to +8.04%
Chunked unmap 45.990 ms 45.299 ms -1.50% -2.37% to -1.01%
Move active 15.414 ms 15.186 ms -1.48% -6.86% to +1.50%
Move inactive 13.599 ms 12.688 ms -6.70% -12.57% to -2.70%
RW 169.537 s 172.291 s +1.62% -3.76% to +5.73%
Positive values indicate increased execution time. The RW subtest
completed one iteration per run.
Page table reclamation and the additional TLB synchronization
required before freeing detached page tables introduce overhead
that may affect performance in some workloads.
Memory reclamation
------------------
With one 4 KiB mapping per 2 MiB of GPA space, memslot removal reduced
G-stage page table memory usage from 2084 KiB to 32 KiB for a 1 GiB
memslot, and from 16448 KiB to 32 KiB for an 8 GiB memslot.
For a 1 GiB sparse mapping repeatedly invalidated and accessed again,
the remaining page table memory usage was:
Hot region Before After Reduction
------------------------------------------------
10% 2084 KiB 240 KiB 88.5%
50% 2084 KiB 1060 KiB 49.1%
90% 2084 KiB 1876 KiB 10.0%
Memory usage was measured with additional G-stage page table accounting.
RFC notes
------------------
The mean single-page unmap time increased, although the results
varied across runs. Obtaining consistent performance results under
QEMU TCG has been difficult. I will try to address this issue in
the next revision.
To mitigate the single-page unmap overhead, the current implementation
retains a separate leaf lookup path in kvm_riscv_gstage_unmap_range().
Suggestions on reducing this overhead while sharing the recursive
walker would be appreciated.
Feedback on the reclamation policies and other aspects of the design
is also very welcome.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Tan Chi (3):
KVM: riscv: Introduce a recursive G-stage unmap walker
KVM: riscv: Reclaim G-stage page tables on memslot removal
KVM: riscv: Selectively reclaim G-stage page tables on invalidation
arch/riscv/include/asm/kvm_gstage.h | 14 ++-
arch/riscv/kvm/gstage.c | 180 ++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +-
3 files changed, 174 insertions(+), 28 deletions(-)
base-commit: bc069e58fa1c1da46f7601840f2079a381370893
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker
2026-11-29 14:22 ` Tan Chi
(?)
@ 2026-11-29 14:22 ` Tan Chi
-1 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
To support subsequent page table reclamation, introduce a recursive
G-stage page table walker, similar to the arm64 stage-2 page table
walker. Walk adjacent PTEs directly within each page table instead
of repeatedly walking from the root.
Initial tests with memslot_perf_test under QEMU TCG showed a 6-10%
increase in single-page unmap time with the recursive walker.
Retaining the existing leaf lookup path in preliminary tests brought
the unmap time close to the baseline. Since single-page unmapping
does not benefit from walking adjacent PTEs, handle it separately
and move the recursive walk into a separate function.
Unwind the recursive walk before dropping mmu_lock for rescheduling,
as page table pointers may become invalid while the lock is released.
Resume the walk from the updated address after reacquiring the lock.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/kvm/gstage.c | 119 ++++++++++++++++++++++++++++++++--------
1 file changed, 95 insertions(+), 24 deletions(-)
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index f4ed659b11e9..dca3cd1c35e4 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -437,48 +437,119 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
return flush;
}
-bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+/*
+ * Walk the G-stage page tables recursively to unmap the given range.
+ * Walk adjacent PTEs directly within each table to avoid repeated
+ * lookups from the root page table.
+ *
+ * Return:
+ * 0 - The walk completed.
+ * -EAGAIN - Rescheduling is needed.
+ * Other negative values indicate errors.
+ */
+static int gstage_unmap_walk(struct kvm_gstage *gstage,
+ pte_t *table, u32 level,
+ gpa_t *addr, gpa_t end,
+ bool may_block, bool *flush)
{
- int ret;
- pte_t *ptep;
- u32 ptep_level;
- bool found_leaf;
unsigned long page_size;
- gpa_t addr = start, end = start + size;
- bool flush = false;
+ pte_t *ptep, *child;
+ gpa_t child_end;
+ pte_t pte;
+ int ret;
- 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;
+ ret = gstage_level_to_page_size(gstage, level, &page_size);
+ if (ret)
+ return ret;
- if (!found_leaf) {
- addr = ALIGN(addr + 1, page_size);
- } else {
- if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
- flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep,
- ptep_level, GSTAGE_OP_CLEAR);
- else {
+ ptep = &table[gstage_pte_index(gstage, *addr, level)];
+
+ for (; *addr < end; ptep++) {
+ pte = ptep_get(ptep);
+
+ if (!pte_val(pte)) {
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (gstage_pte_leaf(pte)) {
+ if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
+ *flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
+ level, GSTAGE_OP_CLEAR);
+ } else {
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
- addr, end, page_size);
+ *addr, end, page_size);
}
- addr += page_size;
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (level) {
+ child = (pte_t *)gstage_pte_page_vaddr(pte);
+ child_end = min(end, ALIGN(*addr + 1, page_size));
+ ret = gstage_unmap_walk(gstage, child, level - 1,
+ addr, child_end, may_block, flush);
+ if (ret < 0)
+ return ret;
+ } else {
+ *addr = ALIGN(*addr + 1, page_size);
}
+ /*
+ * Unwind the walk before dropping mmu_lock, as the
+ * page table pointers may become invalid while the lock is released.
+ */
+ if (*addr < end && may_block &&
+ (need_resched() ||
+ rwlock_needbreak(&gstage->kvm->mmu_lock)))
+ return -EAGAIN;
+ }
+
+ return 0;
+}
+
+static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ int ret;
+ gpa_t addr = start, end = start + size;
+ bool flush = false;
+
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ may_block = false;
+
+ while (addr < end) {
+ ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
+ gstage->pgd_levels - 1,
+ &addr, end, may_block, &flush);
+
+ if (ret != -EAGAIN || addr >= end)
+ break;
+
/*
* If the range is too large, release the kvm->mmu_lock
* to prevent starvation and lockup detector warnings.
*/
- if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
- cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
+ cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
}
return flush;
}
+bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ pte_t *ptep;
+ u32 level;
+
+ if (size == PAGE_SIZE && IS_ALIGNED(start, PAGE_SIZE)) {
+ if (!kvm_riscv_gstage_get_leaf(gstage, start, &ptep, &level))
+ return false;
+
+ if (!level)
+ return kvm_riscv_gstage_op_pte(gstage, start, ptep,
+ level, GSTAGE_OP_CLEAR);
+ }
+
+ return gstage_unmap_range_recursive(gstage, start, size,
+ may_block);
+}
+
/*
* Clear every G-stage mapping and free all page tables below the PGD.
* kvm_riscv_gstage_unmap_range() only clears leaf PTEs and leaves the
--
2.53.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
To support subsequent page table reclamation, introduce a recursive
G-stage page table walker, similar to the arm64 stage-2 page table
walker. Walk adjacent PTEs directly within each page table instead
of repeatedly walking from the root.
Initial tests with memslot_perf_test under QEMU TCG showed a 6-10%
increase in single-page unmap time with the recursive walker.
Retaining the existing leaf lookup path in preliminary tests brought
the unmap time close to the baseline. Since single-page unmapping
does not benefit from walking adjacent PTEs, handle it separately
and move the recursive walk into a separate function.
Unwind the recursive walk before dropping mmu_lock for rescheduling,
as page table pointers may become invalid while the lock is released.
Resume the walk from the updated address after reacquiring the lock.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/kvm/gstage.c | 119 ++++++++++++++++++++++++++++++++--------
1 file changed, 95 insertions(+), 24 deletions(-)
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index f4ed659b11e9..dca3cd1c35e4 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -437,48 +437,119 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
return flush;
}
-bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+/*
+ * Walk the G-stage page tables recursively to unmap the given range.
+ * Walk adjacent PTEs directly within each table to avoid repeated
+ * lookups from the root page table.
+ *
+ * Return:
+ * 0 - The walk completed.
+ * -EAGAIN - Rescheduling is needed.
+ * Other negative values indicate errors.
+ */
+static int gstage_unmap_walk(struct kvm_gstage *gstage,
+ pte_t *table, u32 level,
+ gpa_t *addr, gpa_t end,
+ bool may_block, bool *flush)
{
- int ret;
- pte_t *ptep;
- u32 ptep_level;
- bool found_leaf;
unsigned long page_size;
- gpa_t addr = start, end = start + size;
- bool flush = false;
+ pte_t *ptep, *child;
+ gpa_t child_end;
+ pte_t pte;
+ int ret;
- 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;
+ ret = gstage_level_to_page_size(gstage, level, &page_size);
+ if (ret)
+ return ret;
- if (!found_leaf) {
- addr = ALIGN(addr + 1, page_size);
- } else {
- if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
- flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep,
- ptep_level, GSTAGE_OP_CLEAR);
- else {
+ ptep = &table[gstage_pte_index(gstage, *addr, level)];
+
+ for (; *addr < end; ptep++) {
+ pte = ptep_get(ptep);
+
+ if (!pte_val(pte)) {
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (gstage_pte_leaf(pte)) {
+ if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
+ *flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
+ level, GSTAGE_OP_CLEAR);
+ } else {
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
- addr, end, page_size);
+ *addr, end, page_size);
}
- addr += page_size;
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (level) {
+ child = (pte_t *)gstage_pte_page_vaddr(pte);
+ child_end = min(end, ALIGN(*addr + 1, page_size));
+ ret = gstage_unmap_walk(gstage, child, level - 1,
+ addr, child_end, may_block, flush);
+ if (ret < 0)
+ return ret;
+ } else {
+ *addr = ALIGN(*addr + 1, page_size);
}
+ /*
+ * Unwind the walk before dropping mmu_lock, as the
+ * page table pointers may become invalid while the lock is released.
+ */
+ if (*addr < end && may_block &&
+ (need_resched() ||
+ rwlock_needbreak(&gstage->kvm->mmu_lock)))
+ return -EAGAIN;
+ }
+
+ return 0;
+}
+
+static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ int ret;
+ gpa_t addr = start, end = start + size;
+ bool flush = false;
+
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ may_block = false;
+
+ while (addr < end) {
+ ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
+ gstage->pgd_levels - 1,
+ &addr, end, may_block, &flush);
+
+ if (ret != -EAGAIN || addr >= end)
+ break;
+
/*
* If the range is too large, release the kvm->mmu_lock
* to prevent starvation and lockup detector warnings.
*/
- if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
- cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
+ cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
}
return flush;
}
+bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ pte_t *ptep;
+ u32 level;
+
+ if (size == PAGE_SIZE && IS_ALIGNED(start, PAGE_SIZE)) {
+ if (!kvm_riscv_gstage_get_leaf(gstage, start, &ptep, &level))
+ return false;
+
+ if (!level)
+ return kvm_riscv_gstage_op_pte(gstage, start, ptep,
+ level, GSTAGE_OP_CLEAR);
+ }
+
+ return gstage_unmap_range_recursive(gstage, start, size,
+ may_block);
+}
+
/*
* Clear every G-stage mapping and free all page tables below the PGD.
* kvm_riscv_gstage_unmap_range() only clears leaf PTEs and leaves the
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
To support subsequent page table reclamation, introduce a recursive
G-stage page table walker, similar to the arm64 stage-2 page table
walker. Walk adjacent PTEs directly within each page table instead
of repeatedly walking from the root.
Initial tests with memslot_perf_test under QEMU TCG showed a 6-10%
increase in single-page unmap time with the recursive walker.
Retaining the existing leaf lookup path in preliminary tests brought
the unmap time close to the baseline. Since single-page unmapping
does not benefit from walking adjacent PTEs, handle it separately
and move the recursive walk into a separate function.
Unwind the recursive walk before dropping mmu_lock for rescheduling,
as page table pointers may become invalid while the lock is released.
Resume the walk from the updated address after reacquiring the lock.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/kvm/gstage.c | 119 ++++++++++++++++++++++++++++++++--------
1 file changed, 95 insertions(+), 24 deletions(-)
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index f4ed659b11e9..dca3cd1c35e4 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -437,48 +437,119 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
return flush;
}
-bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+/*
+ * Walk the G-stage page tables recursively to unmap the given range.
+ * Walk adjacent PTEs directly within each table to avoid repeated
+ * lookups from the root page table.
+ *
+ * Return:
+ * 0 - The walk completed.
+ * -EAGAIN - Rescheduling is needed.
+ * Other negative values indicate errors.
+ */
+static int gstage_unmap_walk(struct kvm_gstage *gstage,
+ pte_t *table, u32 level,
+ gpa_t *addr, gpa_t end,
+ bool may_block, bool *flush)
{
- int ret;
- pte_t *ptep;
- u32 ptep_level;
- bool found_leaf;
unsigned long page_size;
- gpa_t addr = start, end = start + size;
- bool flush = false;
+ pte_t *ptep, *child;
+ gpa_t child_end;
+ pte_t pte;
+ int ret;
- 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;
+ ret = gstage_level_to_page_size(gstage, level, &page_size);
+ if (ret)
+ return ret;
- if (!found_leaf) {
- addr = ALIGN(addr + 1, page_size);
- } else {
- if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
- flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep,
- ptep_level, GSTAGE_OP_CLEAR);
- else {
+ ptep = &table[gstage_pte_index(gstage, *addr, level)];
+
+ for (; *addr < end; ptep++) {
+ pte = ptep_get(ptep);
+
+ if (!pte_val(pte)) {
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (gstage_pte_leaf(pte)) {
+ if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
+ *flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
+ level, GSTAGE_OP_CLEAR);
+ } else {
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
- addr, end, page_size);
+ *addr, end, page_size);
}
- addr += page_size;
+ *addr = ALIGN(*addr + 1, page_size);
+ } else if (level) {
+ child = (pte_t *)gstage_pte_page_vaddr(pte);
+ child_end = min(end, ALIGN(*addr + 1, page_size));
+ ret = gstage_unmap_walk(gstage, child, level - 1,
+ addr, child_end, may_block, flush);
+ if (ret < 0)
+ return ret;
+ } else {
+ *addr = ALIGN(*addr + 1, page_size);
}
+ /*
+ * Unwind the walk before dropping mmu_lock, as the
+ * page table pointers may become invalid while the lock is released.
+ */
+ if (*addr < end && may_block &&
+ (need_resched() ||
+ rwlock_needbreak(&gstage->kvm->mmu_lock)))
+ return -EAGAIN;
+ }
+
+ return 0;
+}
+
+static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ int ret;
+ gpa_t addr = start, end = start + size;
+ bool flush = false;
+
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ may_block = false;
+
+ while (addr < end) {
+ ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
+ gstage->pgd_levels - 1,
+ &addr, end, may_block, &flush);
+
+ if (ret != -EAGAIN || addr >= end)
+ break;
+
/*
* If the range is too large, release the kvm->mmu_lock
* to prevent starvation and lockup detector warnings.
*/
- if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
- cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
+ cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
}
return flush;
}
+bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
+ gpa_t start, gpa_t size, bool may_block)
+{
+ pte_t *ptep;
+ u32 level;
+
+ if (size == PAGE_SIZE && IS_ALIGNED(start, PAGE_SIZE)) {
+ if (!kvm_riscv_gstage_get_leaf(gstage, start, &ptep, &level))
+ return false;
+
+ if (!level)
+ return kvm_riscv_gstage_op_pte(gstage, start, ptep,
+ level, GSTAGE_OP_CLEAR);
+ }
+
+ return gstage_unmap_range_recursive(gstage, start, size,
+ may_block);
+}
+
/*
* Clear every G-stage mapping and free all page tables below the PGD.
* kvm_riscv_gstage_unmap_range() only clears leaf PTEs and leaves the
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 2/3] KVM: riscv: Reclaim G-stage page tables on memslot removal
2026-11-29 14:22 ` Tan Chi
(?)
@ 2026-11-29 14:22 ` Tan Chi
-1 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
G-stage unmapping clears leaf PTEs but retains the underlying page
tables until the VM is destroyed, leaving unused page tables allocated
even after memslot removal.
Extend the recursive unmap walker to detach fully covered page tables
that are empty after unmapping. Collect detached page tables in a list,
invalidate cached non-leaf PTEs and defer freeing the tables through RCU.
Introduce the NONE and ALL reclamation modes and enable reclamation
for memslot removal.
Keep reclamation disabled for I/O unmapping, which currently only
unmaps individual IMSIC pages and does not cover an entire page table.
Leave MMU notifier invalidation unchanged for now.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 12 ++++-
arch/riscv/kvm/gstage.c | 78 +++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +--
3 files changed, 84 insertions(+), 14 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 011525c83055..97fc376ecebe 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -9,6 +9,15 @@
#include <linux/kvm_types.h>
+/*
+ * NONE: Do not reclaim page tables.
+ * ALL: Reclaim fully covered tables that become empty after unmapping.
+ */
+enum kvm_gstage_reclaim_mode {
+ KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_ALL,
+};
+
struct kvm_gstage {
struct kvm *kvm;
unsigned long flags;
@@ -78,7 +87,8 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
pte_t *ptep, u32 ptep_level, enum kvm_riscv_gstage_op op);
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block);
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode);
bool kvm_riscv_gstage_unmap_all(struct kvm_gstage *gstage);
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index dca3cd1c35e4..c8b8297b6610 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -36,6 +36,28 @@ static void gstage_free_page_table(pte_t *table)
call_rcu(&virt_to_page(table)->rcu_head, gstage_free_page_table_rcu);
}
+static void gstage_free_page_tables(struct list_head *free_list)
+{
+ struct page *page, *next;
+
+ list_for_each_entry_safe(page, next, free_list, lru) {
+ list_del(&page->lru);
+ gstage_free_page_table(page_address(page));
+ }
+}
+
+static void gstage_reclaim_tables(struct kvm_gstage *gstage,
+ struct list_head *free_list)
+{
+ /*
+ * Invalidate cached non-leaf PTEs before freeing the
+ * detached page tables.
+ */
+ kvm_flush_remote_tlbs(gstage->kvm);
+
+ gstage_free_page_tables(free_list);
+}
+
static inline unsigned long gstage_pte_index(struct kvm_gstage *gstage,
gpa_t addr, u32 level)
{
@@ -442,21 +464,29 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
* Walk adjacent PTEs directly within each table to avoid repeated
* lookups from the root page table.
*
+ * Detached tables are added to @free_list for deferred freeing after
+ * TLB synchronization.
+ *
* Return:
- * 0 - The walk completed.
+ * 1 - The caller may reclaim this table if fully covered.
+ * 0 - The caller must not reclaim this table.
* -EAGAIN - Rescheduling is needed.
* Other negative values indicate errors.
*/
static int gstage_unmap_walk(struct kvm_gstage *gstage,
pte_t *table, u32 level,
gpa_t *addr, gpa_t end,
- bool may_block, bool *flush)
+ bool may_block, bool *flush,
+ enum kvm_gstage_reclaim_mode reclaim_mode,
+ struct list_head *free_list)
{
unsigned long page_size;
pte_t *ptep, *child;
gpa_t child_end;
pte_t pte;
int ret;
+ bool full;
+ bool reclaimable = true;
ret = gstage_level_to_page_size(gstage, level, &page_size);
if (ret)
@@ -474,6 +504,7 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
} else {
+ reclaimable = false;
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
*addr, end, page_size);
}
@@ -482,16 +513,32 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
} else if (level) {
child = (pte_t *)gstage_pte_page_vaddr(pte);
child_end = min(end, ALIGN(*addr + 1, page_size));
+
+ full = IS_ALIGNED(*addr, page_size) && (end - *addr) >= page_size;
+
ret = gstage_unmap_walk(gstage, child, level - 1,
- addr, child_end, may_block, flush);
+ addr, child_end, may_block,
+ flush, reclaim_mode, free_list);
if (ret < 0)
return ret;
+
+ if (reclaim_mode != KVM_GSTAGE_RECLAIM_NONE &&
+ full && ret == 1) {
+ set_pte(ptep, __pte(0));
+ list_add_tail(&virt_to_page(child)->lru, free_list);
+ } else {
+ reclaimable = false;
+ }
} else {
+ reclaimable = false;
*addr = ALIGN(*addr + 1, page_size);
}
/*
- * Unwind the walk before dropping mmu_lock, as the
+ * Let completed walks return normally so that their callers can
+ * reclaim fully covered tables before rescheduling.
+ *
+ * Otherwise, unwind the walk before dropping mmu_lock, as the
* page table pointers may become invalid while the lock is released.
*/
if (*addr < end && may_block &&
@@ -500,23 +547,33 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
return -EAGAIN;
}
- return 0;
+ return reclaimable;
}
static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
int ret;
gpa_t addr = start, end = start + size;
bool flush = false;
+ LIST_HEAD(free_list);
- if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) {
may_block = false;
+ reclaim_mode = KVM_GSTAGE_RECLAIM_NONE;
+ }
while (addr < end) {
ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
gstage->pgd_levels - 1,
- &addr, end, may_block, &flush);
+ &addr, end, may_block, &flush,
+ reclaim_mode, &free_list);
+
+ if (!list_empty(&free_list)) {
+ gstage_reclaim_tables(gstage, &free_list);
+ flush = false;
+ }
if (ret != -EAGAIN || addr >= end)
break;
@@ -532,7 +589,8 @@ static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
}
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
pte_t *ptep;
u32 level;
@@ -547,7 +605,7 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
}
return gstage_unmap_range_recursive(gstage, start, size,
- may_block);
+ may_block, reclaim_mode);
}
/*
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 7758e3f03bd7..9653a7c1e7cd 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -93,7 +93,8 @@ void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size)
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_NONE);
write_unlock(&kvm->mmu_lock);
if (flush)
@@ -210,7 +211,8 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_ALL);
write_unlock(&kvm->mmu_lock);
if (flush)
kvm_flush_remote_tlbs_range(kvm, gpa >> PAGE_SHIFT,
@@ -342,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block);
+ range->may_block, KVM_GSTAGE_RECLAIM_NONE);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 2/3] KVM: riscv: Reclaim G-stage page tables on memslot removal
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
G-stage unmapping clears leaf PTEs but retains the underlying page
tables until the VM is destroyed, leaving unused page tables allocated
even after memslot removal.
Extend the recursive unmap walker to detach fully covered page tables
that are empty after unmapping. Collect detached page tables in a list,
invalidate cached non-leaf PTEs and defer freeing the tables through RCU.
Introduce the NONE and ALL reclamation modes and enable reclamation
for memslot removal.
Keep reclamation disabled for I/O unmapping, which currently only
unmaps individual IMSIC pages and does not cover an entire page table.
Leave MMU notifier invalidation unchanged for now.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 12 ++++-
arch/riscv/kvm/gstage.c | 78 +++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +--
3 files changed, 84 insertions(+), 14 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 011525c83055..97fc376ecebe 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -9,6 +9,15 @@
#include <linux/kvm_types.h>
+/*
+ * NONE: Do not reclaim page tables.
+ * ALL: Reclaim fully covered tables that become empty after unmapping.
+ */
+enum kvm_gstage_reclaim_mode {
+ KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_ALL,
+};
+
struct kvm_gstage {
struct kvm *kvm;
unsigned long flags;
@@ -78,7 +87,8 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
pte_t *ptep, u32 ptep_level, enum kvm_riscv_gstage_op op);
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block);
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode);
bool kvm_riscv_gstage_unmap_all(struct kvm_gstage *gstage);
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index dca3cd1c35e4..c8b8297b6610 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -36,6 +36,28 @@ static void gstage_free_page_table(pte_t *table)
call_rcu(&virt_to_page(table)->rcu_head, gstage_free_page_table_rcu);
}
+static void gstage_free_page_tables(struct list_head *free_list)
+{
+ struct page *page, *next;
+
+ list_for_each_entry_safe(page, next, free_list, lru) {
+ list_del(&page->lru);
+ gstage_free_page_table(page_address(page));
+ }
+}
+
+static void gstage_reclaim_tables(struct kvm_gstage *gstage,
+ struct list_head *free_list)
+{
+ /*
+ * Invalidate cached non-leaf PTEs before freeing the
+ * detached page tables.
+ */
+ kvm_flush_remote_tlbs(gstage->kvm);
+
+ gstage_free_page_tables(free_list);
+}
+
static inline unsigned long gstage_pte_index(struct kvm_gstage *gstage,
gpa_t addr, u32 level)
{
@@ -442,21 +464,29 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
* Walk adjacent PTEs directly within each table to avoid repeated
* lookups from the root page table.
*
+ * Detached tables are added to @free_list for deferred freeing after
+ * TLB synchronization.
+ *
* Return:
- * 0 - The walk completed.
+ * 1 - The caller may reclaim this table if fully covered.
+ * 0 - The caller must not reclaim this table.
* -EAGAIN - Rescheduling is needed.
* Other negative values indicate errors.
*/
static int gstage_unmap_walk(struct kvm_gstage *gstage,
pte_t *table, u32 level,
gpa_t *addr, gpa_t end,
- bool may_block, bool *flush)
+ bool may_block, bool *flush,
+ enum kvm_gstage_reclaim_mode reclaim_mode,
+ struct list_head *free_list)
{
unsigned long page_size;
pte_t *ptep, *child;
gpa_t child_end;
pte_t pte;
int ret;
+ bool full;
+ bool reclaimable = true;
ret = gstage_level_to_page_size(gstage, level, &page_size);
if (ret)
@@ -474,6 +504,7 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
} else {
+ reclaimable = false;
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
*addr, end, page_size);
}
@@ -482,16 +513,32 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
} else if (level) {
child = (pte_t *)gstage_pte_page_vaddr(pte);
child_end = min(end, ALIGN(*addr + 1, page_size));
+
+ full = IS_ALIGNED(*addr, page_size) && (end - *addr) >= page_size;
+
ret = gstage_unmap_walk(gstage, child, level - 1,
- addr, child_end, may_block, flush);
+ addr, child_end, may_block,
+ flush, reclaim_mode, free_list);
if (ret < 0)
return ret;
+
+ if (reclaim_mode != KVM_GSTAGE_RECLAIM_NONE &&
+ full && ret == 1) {
+ set_pte(ptep, __pte(0));
+ list_add_tail(&virt_to_page(child)->lru, free_list);
+ } else {
+ reclaimable = false;
+ }
} else {
+ reclaimable = false;
*addr = ALIGN(*addr + 1, page_size);
}
/*
- * Unwind the walk before dropping mmu_lock, as the
+ * Let completed walks return normally so that their callers can
+ * reclaim fully covered tables before rescheduling.
+ *
+ * Otherwise, unwind the walk before dropping mmu_lock, as the
* page table pointers may become invalid while the lock is released.
*/
if (*addr < end && may_block &&
@@ -500,23 +547,33 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
return -EAGAIN;
}
- return 0;
+ return reclaimable;
}
static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
int ret;
gpa_t addr = start, end = start + size;
bool flush = false;
+ LIST_HEAD(free_list);
- if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) {
may_block = false;
+ reclaim_mode = KVM_GSTAGE_RECLAIM_NONE;
+ }
while (addr < end) {
ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
gstage->pgd_levels - 1,
- &addr, end, may_block, &flush);
+ &addr, end, may_block, &flush,
+ reclaim_mode, &free_list);
+
+ if (!list_empty(&free_list)) {
+ gstage_reclaim_tables(gstage, &free_list);
+ flush = false;
+ }
if (ret != -EAGAIN || addr >= end)
break;
@@ -532,7 +589,8 @@ static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
}
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
pte_t *ptep;
u32 level;
@@ -547,7 +605,7 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
}
return gstage_unmap_range_recursive(gstage, start, size,
- may_block);
+ may_block, reclaim_mode);
}
/*
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 7758e3f03bd7..9653a7c1e7cd 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -93,7 +93,8 @@ void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size)
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_NONE);
write_unlock(&kvm->mmu_lock);
if (flush)
@@ -210,7 +211,8 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_ALL);
write_unlock(&kvm->mmu_lock);
if (flush)
kvm_flush_remote_tlbs_range(kvm, gpa >> PAGE_SHIFT,
@@ -342,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block);
+ range->may_block, KVM_GSTAGE_RECLAIM_NONE);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 2/3] KVM: riscv: Reclaim G-stage page tables on memslot removal
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
G-stage unmapping clears leaf PTEs but retains the underlying page
tables until the VM is destroyed, leaving unused page tables allocated
even after memslot removal.
Extend the recursive unmap walker to detach fully covered page tables
that are empty after unmapping. Collect detached page tables in a list,
invalidate cached non-leaf PTEs and defer freeing the tables through RCU.
Introduce the NONE and ALL reclamation modes and enable reclamation
for memslot removal.
Keep reclamation disabled for I/O unmapping, which currently only
unmaps individual IMSIC pages and does not cover an entire page table.
Leave MMU notifier invalidation unchanged for now.
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 12 ++++-
arch/riscv/kvm/gstage.c | 78 +++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +--
3 files changed, 84 insertions(+), 14 deletions(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 011525c83055..97fc376ecebe 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -9,6 +9,15 @@
#include <linux/kvm_types.h>
+/*
+ * NONE: Do not reclaim page tables.
+ * ALL: Reclaim fully covered tables that become empty after unmapping.
+ */
+enum kvm_gstage_reclaim_mode {
+ KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_ALL,
+};
+
struct kvm_gstage {
struct kvm *kvm;
unsigned long flags;
@@ -78,7 +87,8 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
pte_t *ptep, u32 ptep_level, enum kvm_riscv_gstage_op op);
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block);
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode);
bool kvm_riscv_gstage_unmap_all(struct kvm_gstage *gstage);
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index dca3cd1c35e4..c8b8297b6610 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -36,6 +36,28 @@ static void gstage_free_page_table(pte_t *table)
call_rcu(&virt_to_page(table)->rcu_head, gstage_free_page_table_rcu);
}
+static void gstage_free_page_tables(struct list_head *free_list)
+{
+ struct page *page, *next;
+
+ list_for_each_entry_safe(page, next, free_list, lru) {
+ list_del(&page->lru);
+ gstage_free_page_table(page_address(page));
+ }
+}
+
+static void gstage_reclaim_tables(struct kvm_gstage *gstage,
+ struct list_head *free_list)
+{
+ /*
+ * Invalidate cached non-leaf PTEs before freeing the
+ * detached page tables.
+ */
+ kvm_flush_remote_tlbs(gstage->kvm);
+
+ gstage_free_page_tables(free_list);
+}
+
static inline unsigned long gstage_pte_index(struct kvm_gstage *gstage,
gpa_t addr, u32 level)
{
@@ -442,21 +464,29 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
* Walk adjacent PTEs directly within each table to avoid repeated
* lookups from the root page table.
*
+ * Detached tables are added to @free_list for deferred freeing after
+ * TLB synchronization.
+ *
* Return:
- * 0 - The walk completed.
+ * 1 - The caller may reclaim this table if fully covered.
+ * 0 - The caller must not reclaim this table.
* -EAGAIN - Rescheduling is needed.
* Other negative values indicate errors.
*/
static int gstage_unmap_walk(struct kvm_gstage *gstage,
pte_t *table, u32 level,
gpa_t *addr, gpa_t end,
- bool may_block, bool *flush)
+ bool may_block, bool *flush,
+ enum kvm_gstage_reclaim_mode reclaim_mode,
+ struct list_head *free_list)
{
unsigned long page_size;
pte_t *ptep, *child;
gpa_t child_end;
pte_t pte;
int ret;
+ bool full;
+ bool reclaimable = true;
ret = gstage_level_to_page_size(gstage, level, &page_size);
if (ret)
@@ -474,6 +504,7 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
} else {
+ reclaimable = false;
WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n",
*addr, end, page_size);
}
@@ -482,16 +513,32 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
} else if (level) {
child = (pte_t *)gstage_pte_page_vaddr(pte);
child_end = min(end, ALIGN(*addr + 1, page_size));
+
+ full = IS_ALIGNED(*addr, page_size) && (end - *addr) >= page_size;
+
ret = gstage_unmap_walk(gstage, child, level - 1,
- addr, child_end, may_block, flush);
+ addr, child_end, may_block,
+ flush, reclaim_mode, free_list);
if (ret < 0)
return ret;
+
+ if (reclaim_mode != KVM_GSTAGE_RECLAIM_NONE &&
+ full && ret == 1) {
+ set_pte(ptep, __pte(0));
+ list_add_tail(&virt_to_page(child)->lru, free_list);
+ } else {
+ reclaimable = false;
+ }
} else {
+ reclaimable = false;
*addr = ALIGN(*addr + 1, page_size);
}
/*
- * Unwind the walk before dropping mmu_lock, as the
+ * Let completed walks return normally so that their callers can
+ * reclaim fully covered tables before rescheduling.
+ *
+ * Otherwise, unwind the walk before dropping mmu_lock, as the
* page table pointers may become invalid while the lock is released.
*/
if (*addr < end && may_block &&
@@ -500,23 +547,33 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
return -EAGAIN;
}
- return 0;
+ return reclaimable;
}
static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
int ret;
gpa_t addr = start, end = start + size;
bool flush = false;
+ LIST_HEAD(free_list);
- if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
+ if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) {
may_block = false;
+ reclaim_mode = KVM_GSTAGE_RECLAIM_NONE;
+ }
while (addr < end) {
ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
gstage->pgd_levels - 1,
- &addr, end, may_block, &flush);
+ &addr, end, may_block, &flush,
+ reclaim_mode, &free_list);
+
+ if (!list_empty(&free_list)) {
+ gstage_reclaim_tables(gstage, &free_list);
+ flush = false;
+ }
if (ret != -EAGAIN || addr >= end)
break;
@@ -532,7 +589,8 @@ static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
}
bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
- gpa_t start, gpa_t size, bool may_block)
+ gpa_t start, gpa_t size, bool may_block,
+ enum kvm_gstage_reclaim_mode reclaim_mode)
{
pte_t *ptep;
u32 level;
@@ -547,7 +605,7 @@ bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
}
return gstage_unmap_range_recursive(gstage, start, size,
- may_block);
+ may_block, reclaim_mode);
}
/*
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 7758e3f03bd7..9653a7c1e7cd 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -93,7 +93,8 @@ void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size)
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_NONE);
write_unlock(&kvm->mmu_lock);
if (flush)
@@ -210,7 +211,8 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
kvm_riscv_gstage_init(&gstage, kvm);
write_lock(&kvm->mmu_lock);
- flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false);
+ flush = kvm_riscv_gstage_unmap_range(&gstage, gpa, size, false,
+ KVM_GSTAGE_RECLAIM_ALL);
write_unlock(&kvm->mmu_lock);
if (flush)
kvm_flush_remote_tlbs_range(kvm, gpa >> PAGE_SHIFT,
@@ -342,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block);
+ range->may_block, KVM_GSTAGE_RECLAIM_NONE);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation
2026-11-29 14:22 ` Tan Chi
(?)
@ 2026-11-29 14:22 ` Tan Chi
-1 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
Unlike memslot removal, MMU notifier invalidation may be followed by
accesses to the same guest physical addresses. Reclaiming page tables
whenever they become empty may therefore lead to repeated allocation
and freeing of page tables.
However, retaining empty page tables also requires subsequent
invalidations to traverse them even when there are no mappings left
to remove.
Introduce the EMPTY reclamation mode to reclaim fully covered page
table subtrees that contain no leaf mappings before unmapping, while
retaining page tables whose leaf mappings are removed by the current
walk.
Determine whether a page table can be reclaimed during the existing
walk without an additional scan.
A similar approach has been proposed for the x86 TDP MMU [1].
To compare EMPTY and ALL, create a 1 GiB sparse mapping with one
4 KiB mapping per 2 MiB of GPA space. Repeatedly invalidate the
entire range and access only the hot region after each invalidation.
The number of L0 page table allocations across six hot-region
remappings was:
Hot region EMPTY ALL
--------------------------------
10% 0 306
50% 0 1530
90% 0 2760
Both policies achieved the same page table memory usage after
hot-region remapping in these tests, but EMPTY avoided repeated
allocation of hot page tables.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 2 ++
arch/riscv/kvm/gstage.c | 3 +++
arch/riscv/kvm/mmu.c | 2 +-
3 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 97fc376ecebe..ffc14b2842b0 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -11,10 +11,12 @@
/*
* NONE: Do not reclaim page tables.
+ * EMPTY: Reclaim fully covered tables that were already empty.
* ALL: Reclaim fully covered tables that become empty after unmapping.
*/
enum kvm_gstage_reclaim_mode {
KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_EMPTY,
KVM_GSTAGE_RECLAIM_ALL,
};
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index c8b8297b6610..25f1bfae157f 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -500,6 +500,9 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
if (!pte_val(pte)) {
*addr = ALIGN(*addr + 1, page_size);
} else if (gstage_pte_leaf(pte)) {
+ if (reclaim_mode == KVM_GSTAGE_RECLAIM_EMPTY)
+ reclaimable = false;
+
if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 9653a7c1e7cd..44ad0515e7b8 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -344,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block, KVM_GSTAGE_RECLAIM_NONE);
+ range->may_block, KVM_GSTAGE_RECLAIM_EMPTY);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
Unlike memslot removal, MMU notifier invalidation may be followed by
accesses to the same guest physical addresses. Reclaiming page tables
whenever they become empty may therefore lead to repeated allocation
and freeing of page tables.
However, retaining empty page tables also requires subsequent
invalidations to traverse them even when there are no mappings left
to remove.
Introduce the EMPTY reclamation mode to reclaim fully covered page
table subtrees that contain no leaf mappings before unmapping, while
retaining page tables whose leaf mappings are removed by the current
walk.
Determine whether a page table can be reclaimed during the existing
walk without an additional scan.
A similar approach has been proposed for the x86 TDP MMU [1].
To compare EMPTY and ALL, create a 1 GiB sparse mapping with one
4 KiB mapping per 2 MiB of GPA space. Repeatedly invalidate the
entire range and access only the hot region after each invalidation.
The number of L0 page table allocations across six hot-region
remappings was:
Hot region EMPTY ALL
--------------------------------
10% 0 306
50% 0 1530
90% 0 2760
Both policies achieved the same page table memory usage after
hot-region remapping in these tests, but EMPTY avoided repeated
allocation of hot page tables.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 2 ++
arch/riscv/kvm/gstage.c | 3 +++
arch/riscv/kvm/mmu.c | 2 +-
3 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 97fc376ecebe..ffc14b2842b0 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -11,10 +11,12 @@
/*
* NONE: Do not reclaim page tables.
+ * EMPTY: Reclaim fully covered tables that were already empty.
* ALL: Reclaim fully covered tables that become empty after unmapping.
*/
enum kvm_gstage_reclaim_mode {
KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_EMPTY,
KVM_GSTAGE_RECLAIM_ALL,
};
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index c8b8297b6610..25f1bfae157f 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -500,6 +500,9 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
if (!pte_val(pte)) {
*addr = ALIGN(*addr + 1, page_size);
} else if (gstage_pte_leaf(pte)) {
+ if (reclaim_mode == KVM_GSTAGE_RECLAIM_EMPTY)
+ reclaimable = false;
+
if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 9653a7c1e7cd..44ad0515e7b8 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -344,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block, KVM_GSTAGE_RECLAIM_NONE);
+ range->may_block, KVM_GSTAGE_RECLAIM_EMPTY);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation
@ 2026-11-29 14:22 ` Tan Chi
0 siblings, 0 replies; 13+ messages in thread
From: Tan Chi @ 2026-11-29 14:22 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv, linux-riscv,
Tan Chi
Unlike memslot removal, MMU notifier invalidation may be followed by
accesses to the same guest physical addresses. Reclaiming page tables
whenever they become empty may therefore lead to repeated allocation
and freeing of page tables.
However, retaining empty page tables also requires subsequent
invalidations to traverse them even when there are no mappings left
to remove.
Introduce the EMPTY reclamation mode to reclaim fully covered page
table subtrees that contain no leaf mappings before unmapping, while
retaining page tables whose leaf mappings are removed by the current
walk.
Determine whether a page table can be reclaimed during the existing
walk without an additional scan.
A similar approach has been proposed for the x86 TDP MMU [1].
To compare EMPTY and ALL, create a 1 GiB sparse mapping with one
4 KiB mapping per 2 MiB of GPA space. Repeatedly invalidate the
entire range and access only the hot region after each invalidation.
The number of L0 page table allocations across six hot-region
remappings was:
Hot region EMPTY ALL
--------------------------------
10% 0 306
50% 0 1530
90% 0 2760
Both policies achieved the same page table memory usage after
hot-region remapping in these tests, but EMPTY avoided repeated
allocation of hot page tables.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
arch/riscv/include/asm/kvm_gstage.h | 2 ++
arch/riscv/kvm/gstage.c | 3 +++
arch/riscv/kvm/mmu.c | 2 +-
3 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 97fc376ecebe..ffc14b2842b0 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -11,10 +11,12 @@
/*
* NONE: Do not reclaim page tables.
+ * EMPTY: Reclaim fully covered tables that were already empty.
* ALL: Reclaim fully covered tables that become empty after unmapping.
*/
enum kvm_gstage_reclaim_mode {
KVM_GSTAGE_RECLAIM_NONE,
+ KVM_GSTAGE_RECLAIM_EMPTY,
KVM_GSTAGE_RECLAIM_ALL,
};
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index c8b8297b6610..25f1bfae157f 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -500,6 +500,9 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
if (!pte_val(pte)) {
*addr = ALIGN(*addr + 1, page_size);
} else if (gstage_pte_leaf(pte)) {
+ if (reclaim_mode == KVM_GSTAGE_RECLAIM_EMPTY)
+ reclaimable = false;
+
if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
level, GSTAGE_OP_CLEAR);
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 9653a7c1e7cd..44ad0515e7b8 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -344,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
kvm_riscv_gstage_init(&gstage, kvm);
flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
(range->end - range->start) << PAGE_SHIFT,
- range->may_block, KVM_GSTAGE_RECLAIM_NONE);
+ range->may_block, KVM_GSTAGE_RECLAIM_EMPTY);
if (flush)
kvm_flush_remote_tlbs_range(kvm, range->start,
range->end - range->start);
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker
2026-11-29 14:22 ` Tan Chi
(?)
(?)
@ 2026-11-29 14:37 ` sashiko-bot
-1 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-29 14:37 UTC (permalink / raw)
To: Tan Chi; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: riscv: use-after-free of gstage->pgd in
gstage_unmap_range_recursive()
--
commit 5d921baa27d30ba8a457e5d017135853833a0eb5
Author: Tan Chi <tanchi25@mails.ucas.ac.cn>
KVM: riscv: Introduce a recursive G-stage unmap walker
This commit introduces a recursive G-stage page table walker to unmap
address ranges by walking adjacent PTEs within each page table level,
while keeping a fast path for single-page unmaps.
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index f4ed659b11e94..dca3cd1c35e41 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
[ ... ]
> +static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage,
> + gpa_t start, gpa_t size, bool may_block)
> +{
> + int ret;
> + gpa_t addr = start, end = start + size;
> + bool flush = false;
> +
> + if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL)
> + may_block = false;
> +
> + while (addr < end) {
> + ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd,
> + gstage->pgd_levels - 1,
> + &addr, end, may_block, &flush);
> +
> + if (ret != -EAGAIN || addr >= end)
> + break;
> +
> /*
> * If the range is too large, release the kvm->mmu_lock
> * to prevent starvation and lockup detector warnings.
> */
> - if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end)
> - cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
> + cond_resched_rwlock_write(&gstage->kvm->mmu_lock);
> }
>
> return flush;
> }
[Severity: High]
Can gstage_unmap_range_recursive() access a freed root PGD after
cond_resched_rwlock_write() drops and reacquires kvm->mmu_lock?
kvm_unmap_gfn_range() snapshots kvm->arch.pgd into gstage.pgd once via
kvm_riscv_gstage_init() before calling kvm_riscv_gstage_unmap_range().
When cond_resched_rwlock_write(&gstage->kvm->mmu_lock) releases
mmu_lock and schedules out, a concurrent kvm_mmu_notifier_release() can
tear down and free the root PGD:
CPU0 CPU1
kvm_unmap_gfn_range()
kvm_riscv_gstage_init(&gstage, kvm)
gstage->pgd = kvm->arch.pgd
...
gstage_unmap_range_recursive()
gstage_unmap_walk() -> -EAGAIN
cond_resched_rwlock_write()
write_unlock(&kvm->mmu_lock)
kvm_mmu_notifier_release()
...
kvm_riscv_mmu_free_pgd()
write_lock(&kvm->mmu_lock)
kvm_riscv_gstage_unmap_all()
WRITE_ONCE(kvm->arch.pgd, NULL)
write_unlock(&kvm->mmu_lock)
call_rcu(...,
kvm_riscv_mmu_free_pgd_rcu)
schedule()
/* RCU grace period completes, PGD pages freed */
write_lock(&kvm->mmu_lock)
gstage_unmap_walk(..., gstage->pgd, ...)
ptep_get(ptep) <-- use-after-free
Before this change, kvm_riscv_gstage_get_leaf() read gstage->pgd too,
Wait: actually both old and new read gstage->pgd, or rather after
reacquiring mmu_lock, checking !gstage->kvm->arch.pgd (like
mmu_split_huge_pages() does after cond_resched_rwlock_write()) would
avoid walking the stale gstage->pgd pointer.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129142252.171879-1-tanchi25@mails.ucas.ac.cn?part=1
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2026-11-29 14:37 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 14:22 [RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:37 ` sashiko-bot
2026-11-29 14:22 ` [RFC PATCH 2/3] KVM: riscv: Reclaim G-stage page tables on memslot removal Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` [RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation Tan Chi
2026-11-29 14:22 ` Tan Chi
2026-11-29 14:22 ` Tan Chi
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.