Masked write protection already skips clear bits in the dirty bitmap. It still walks from the G-stage root for each selected base page, even when several pages share a level-0 table. Reuse the last level-0 PTE to find subsequent selected entries in the same table. Check each selected entry for a leaf and restart the walk when the GFN falls outside the cached table. The anchor is local to one write-protection call under the MMU lock. For a single-bit mask, do one lookup without entering the reuse loop. Preserve the existing address alignment when write-protecting a huge leaf. Tested with RISC-V QEMU TCG (virt, rv64 with H, 4 outer vCPUs, 2 GiB) against Linux v7.4-rc5 (fe6ff8d5d675c). A one-vCPU KVM selftest used 64 pre-populated 2 MiB mappings with eager splitting enabled. It cleared the initially set bitmap, let the guest dirty evenly spaced pages in each word, then timed the next KVM_CLEAR_DIRTY_LOG. Each value is the median of 10 samples per image and bitmap density. Bits per 64-bit word Control (ms) Patched (ms) Time change 1 0.2647 0.2506 -5.3% 2 0.3266 0.2940 -10.0% 4 0.4398 0.3489 -20.7% 8 0.6762 0.4952 -26.8% 16 1.1766 0.6381 -45.8% 32 2.1394 1.0542 -50.7% 64 3.9704 1.7569 -55.7% A separate run with boot order reversed reproduced the gains at 8 and 64 bits per word. The small single-bit difference changed sign between runs. These measurements cover the clear ioctl under QEMU TCG. Both builds passed the exact bitmap checks after guest writes, after clearing, and after rewriting the same pages. Both also passed a test with an unaligned memslot whose bitmap word straddles two level-0 tables. The final candidate passed the partial clear test across a hugepage boundary. Link: https://github.com/torvalds/linux/commit/1b5ea65ffde73fc74499ae22b3ea87d0dbf87fb6 Signed-off-by: Hongtan Zhang --- diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c --- a/arch/riscv/kvm/gstage.c +++ b/arch/riscv/kvm/gstage.c @@ -530,6 +530,8 @@ bool kvm_riscv_gstage_wp_pt_masked(struct kvm_gstage *gstage, gfn_t base_gfn, unsigned long mask) { + gfn_t leaf_anchor_gfn = 0; + pte_t *leaf_anchor = NULL; unsigned long page_size; bool flush = false; bool found_leaf; @@ -538,10 +540,44 @@ gpa_t addr = 0; int ret; + /* A one-bit mask has no subsequent lookup to reuse. */ + if (mask && !(mask & (mask - 1))) { + gpa_t gpa = (base_gfn + __ffs(mask)) << PAGE_SHIFT; + unsigned long leaf_size; + pte_t *entry; + u32 level; + + if (!kvm_riscv_gstage_get_leaf(gstage, gpa, &entry, &level)) + return false; + if (level) { + if (gstage_level_to_page_size(gstage, level, &leaf_size)) + return false; + gpa = ALIGN_DOWN(gpa, leaf_size); + } + return kvm_riscv_gstage_op_pte(gstage, gpa, entry, level, + GSTAGE_OP_WP); + } + while (mask) { addr = (base_gfn + __ffs(mask)) << PAGE_SHIFT; - found_leaf = kvm_riscv_gstage_get_leaf(gstage, addr, &ptep, &ptep_level); + { + gfn_t gfn = addr >> PAGE_SHIFT; + + if (leaf_anchor && gfn / PTRS_PER_PTE == + leaf_anchor_gfn / PTRS_PER_PTE) { + ptep = leaf_anchor + (gfn - leaf_anchor_gfn); + ptep_level = 0; + found_leaf = gstage_pte_leaf(ptep); + } else { + found_leaf = kvm_riscv_gstage_get_leaf(gstage, addr, + &ptep, &ptep_level); + if (found_leaf && !ptep_level) { + leaf_anchor = ptep; + leaf_anchor_gfn = gfn; + } + } + } ret = gstage_level_to_page_size(gstage, ptep_level, &page_size); if (ret) break;