From nobody Fri Oct 2 13:10:33 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 913843AA1B8; Fri, 31 Jul 2026 09:16:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489371; cv=none; b=biLe8CKkeW8GGtEXR6eEVlPa/1ULnfgex7+fhDsHO1PuH/njl0BYTtopmn+evfOKgIQ4ksOhF0Pa4Dpy7qx3XLZyjtxeDjH6oTl4FmIQ2A+j7ESVhsyWQK6oPd46ckiV3GvuEMhhtX5v5Vaz6MaH6w0jLmTSRwIZdsmBhMa1nXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489371; c=relaxed/simple; bh=0pD2O5ILyVl2jiaArY9AYOfqYCR0C+6Xaf5xqUPWbMs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=o4VpPvGnG+p/qyLZMysQiXs1cRyYKRHLW5U4jq2FlV/KX4kwZhPMFSLIuzWHEKBAR4imgAVNa6iKoRi1WZiYVGnyFH6EJq/AL8pcBB4FgtY4LvrFbtRfUuUm9JYG6olbWug/h7Af2kd8/xOjqGF12N5zfvIfb9vQiKy07sJJKYU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4hBL5s0h1gz8Xrrj; Fri, 31 Jul 2026 17:16:01 +0800 (CST) Received: from szxl2zmapp07.zte.com.cn ([10.1.32.52]) by mse-fl2.zte.com.cn with SMTP id 66V9Foe5064452; Fri, 31 Jul 2026 17:15:50 +0800 (+08) (envelope-from wang.yechao255@zte.com.cn) Received: from localhost.localdomain (unknown [10.234.74.162]) by smtp (Zmail) with SMTP; Fri, 31 Jul 2026 17:15:52 +0800 X-Zmail-TransId: 3e816a6c67c8010-3c14a X-Zmail-LocalSMTP: 1 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 X-Zmail-RealSender: wang.yechao255@zte.com.cn From: Wang Yechao To: Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Alexandre Ghiti , Wang Yechao Subject: [PATCH v5 1/5] RISC-V: KVM: Add the split page cache for ioctl context Date: Fri, 31 Jul 2026 17:12:06 +0800 Message-ID: <20260731091215.1549430-2-wang.yechao255@zte.com.cn> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> References: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl2.zte.com.cn 66V9Foe5064452 X-TLS: YES X-ENVELOPE-SENDER: wang.yechao255@zte.com.cn X-SOURCE-IP: 10.5.228.133 unknown Fri, 31 Jul 2026 17:16:01 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A6C67D1.000/4hBL5s0h1gz8Xrrj Content-Type: text/plain; charset="utf-8" Add the split page cache for dirty logging enablement and the KVM_CLEAR_DIRTY_LOG ioctl. Signed-off-by: Wang Yechao Reviewed-by: Anup Patel --- arch/riscv/include/asm/kvm_host.h | 1 + arch/riscv/kvm/mmu.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index e2d5808169e44..71e455d166905 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -86,6 +86,7 @@ struct kvm_arch { pgd_t *pgd; phys_addr_t pgd_phys; unsigned long pgd_levels; + struct kvm_mmu_memory_cache pgd_split_page_cache; =20 /* Guest Timer */ struct kvm_guest_timer timer; diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 8a0aa5e0e216e..2017d292f4ca7 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -677,6 +677,7 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm) kvm->arch.pgd =3D page_to_virt(pgd_page); kvm->arch.pgd_phys =3D page_to_phys(pgd_page); kvm->arch.pgd_levels =3D kvm_riscv_gstage_max_pgd_levels; + kvm->arch.pgd_split_page_cache.gfp_zero =3D __GFP_ZERO; =20 return 0; } @@ -704,6 +705,8 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm) =20 if (pgd) free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size)); + + kvm_mmu_free_memory_cache(&kvm->arch.pgd_split_page_cache); } =20 void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu) --=20 2.39.3 From nobody Fri Oct 2 13:10:33 2026 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2392D3A5E67; Fri, 31 Jul 2026 09:16:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489369; cv=none; b=go7cUB442l1qkdQgS64OH3eEvhuWcigY9opkeSdQGetsIDWKji5dHnUI/NWEPAKCprnyvy1n8qUm0siJBgZzmFYBGFPIgOMioVoyGFhQTiwljOcQjPHxbyDvLKkWSi1xFMF20qlgmLSgpvbX9J6FyHVkZWhLyaVku6gIbVj6uq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489369; c=relaxed/simple; bh=FmXBZ4+SwjgjmDrKn5shZQdMYdKEugzSqLh1Rznu6X8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=R6nLElsE1rboJWp7u6eGHBHLQEPoAgbJAXN4EbZArG+wCJ47BGPzd2p0BsfPaVERf4ihjf2tOfVjaq7Jj6JLKn8JiYPCy/Axi6W9y4Y1Q9DGcXQ65hDgmP1ujAhgOJV7pI8eL9NEsTj+G3HZJYb/uvzJ/rjS0Prcbp07hLf5Vn0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4hBL5t2p4Lz57HS0; Fri, 31 Jul 2026 17:16:02 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl1.zte.com.cn with SMTP id 66V9FqND003139; Fri, 31 Jul 2026 17:15:52 +0800 (+08) (envelope-from wang.yechao255@zte.com.cn) Received: from localhost.localdomain (unknown [10.234.74.162]) by smtp (Zmail) with SMTP; Fri, 31 Jul 2026 17:15:54 +0800 X-Zmail-TransId: 3e816a6c67ca010-3c160 X-Zmail-LocalSMTP: 1 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 X-Zmail-RealSender: wang.yechao255@zte.com.cn From: Wang Yechao To: Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Alexandre Ghiti , Wang Yechao Subject: [PATCH v5 2/5] RISC-V: KVM: Split huge pages when dirty logging is enabled Date: Fri, 31 Jul 2026 17:12:07 +0800 Message-ID: <20260731091215.1549430-3-wang.yechao255@zte.com.cn> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> References: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 66V9FqND003139 X-TLS: YES X-ENVELOPE-SENDER: wang.yechao255@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 31 Jul 2026 17:16:02 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A6C67D2.000/4hBL5t2p4Lz57HS0 Content-Type: text/plain; charset="utf-8" Split huge pages eagerly when enabling dirty logging. The goal is to avoid doing it while faulting on write-protected pages, which negatively impacts guest performance. The benefits of eager page splitting are the same as in x86 and arm64, added with commit a3fe5dbda0a4 ("KVM: x86/mmu: Split huge pages mapped by the TDP MMU when dirty logging is enabled") and commit e7bf7a490c68 ("KVM: arm64: Split huge pages when dirty logging is enabled") Signed-off-by: Wang Yechao Reviewed-by: Anup Patel --- arch/riscv/include/asm/kvm_gstage.h | 6 +-- arch/riscv/kvm/gstage.c | 23 ++++++--- arch/riscv/kvm/mmu.c | 76 +++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/k= vm_gstage.h index 21e2019df0cf5..f726279780177 100644 --- a/arch/riscv/include/asm/kvm_gstage.h +++ b/arch/riscv/include/asm/kvm_gstage.h @@ -64,9 +64,9 @@ int kvm_riscv_gstage_map_page(struct kvm_gstage *gstage, bool page_rdonly, bool page_exec, struct kvm_gstage_mapping *out_map); =20 -int kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage, - struct kvm_mmu_memory_cache *pcache, - gpa_t addr, u32 target_level, bool flush); +bool kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage, + struct kvm_mmu_memory_cache *pcache, + gpa_t addr, u32 target_level, bool flush); =20 enum kvm_riscv_gstage_op { GSTAGE_OP_NOP =3D 0, /* Nothing */ diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c index b0474fcf065aa..01679aef53289 100644 --- a/arch/riscv/kvm/gstage.c +++ b/arch/riscv/kvm/gstage.c @@ -307,19 +307,20 @@ static inline unsigned long make_child_pte(unsigned l= ong huge_pte, int index, return child_pte; } =20 -int kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage, - struct kvm_mmu_memory_cache *pcache, - gpa_t addr, u32 target_level, bool flush) +bool kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage, + struct kvm_mmu_memory_cache *pcache, + gpa_t addr, u32 target_level, bool flush) { u32 current_level =3D gstage->pgd_levels - 1; pte_t *next_ptep =3D (pte_t *)gstage->pgd; unsigned long huge_pte, child_pte; unsigned long child_page_size; + bool need_flush =3D false; pte_t *ptep; int i, ret; =20 if (!pcache) - return -ENOMEM; + return false; =20 while(current_level > target_level) { ptep =3D (pte_t *)&next_ptep[gstage_pte_index(gstage, addr, current_leve= l)]; @@ -337,27 +338,35 @@ int kvm_riscv_gstage_split_huge(struct kvm_gstage *gs= tage, =20 ret =3D gstage_level_to_page_size(gstage, current_level - 1, &child_page= _size); if (ret) - return ret; + return need_flush; =20 next_ptep =3D kvm_mmu_memory_cache_alloc(pcache); if (!next_ptep) - return -ENOMEM; + return need_flush; =20 for (i =3D 0; i < PTRS_PER_PTE; i++) { child_pte =3D make_child_pte(huge_pte, i, child_page_size); set_pte((pte_t *)&next_ptep[i], __pte(child_pte)); } =20 + /* + * Ensure the writes to the child PTEs are visible before + * linking the new page table to the parent PTE. + */ + smp_wmb(); + set_pte(ptep, pfn_pte(PFN_DOWN(__pa(next_ptep)), __pgprot(_PAGE_TABLE))); =20 if (flush) gstage_tlb_flush(gstage, current_level, addr); + else + need_flush =3D true; =20 current_level--; } =20 - return 0; + return need_flush; } =20 bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr, diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 2017d292f4ca7..2362eeb6c08a5 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -98,6 +98,62 @@ void kvm_riscv_mmu_iounmap(struct kvm *kvm, gpa_t gpa, u= nsigned long size) size >> PAGE_SHIFT); } =20 +static bool need_topup_split_caches_or_resched(struct kvm *kvm, int count) +{ + struct kvm_mmu_memory_cache *cache; + + if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) + return true; + + cache =3D &kvm->arch.pgd_split_page_cache; + return kvm_mmu_memory_cache_nr_free_objects(cache) < count; +} + +static bool mmu_split_huge_pages(struct kvm_gstage *gstage, + phys_addr_t start, phys_addr_t end) +{ + struct kvm *kvm =3D gstage->kvm; + struct kvm_mmu_memory_cache *pcache =3D &kvm->arch.pgd_split_page_cache; + phys_addr_t addr =3D ALIGN_DOWN(start, PMD_SIZE); + phys_addr_t last_flush_gfn =3D addr >> PAGE_SHIFT; + int count =3D gstage->pgd_levels; + bool flush =3D false; + int ret; + + lockdep_assert_held_write(&kvm->mmu_lock); + + while (addr < end) { + if (need_topup_split_caches_or_resched(kvm, count)) { + if (flush) { + kvm_flush_remote_tlbs_range(kvm, last_flush_gfn, + (addr >> PAGE_SHIFT) - last_flush_gfn); + last_flush_gfn =3D addr >> PAGE_SHIFT; + flush =3D false; + } + + write_unlock(&kvm->mmu_lock); + cond_resched(); + + ret =3D kvm_mmu_topup_memory_cache(pcache, count); + if (ret) { + kvm_err("Failed to toup split page cache\n"); + write_lock(&kvm->mmu_lock); + return flush; + } + write_lock(&kvm->mmu_lock); + } + + if (!kvm->arch.pgd) + return flush; + + flush |=3D kvm_riscv_gstage_split_huge(gstage, pcache, addr, 0, false); + + addr +=3D PMD_SIZE; + } + + return flush; +} + void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn_offset, @@ -152,6 +208,25 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm, size >> PAGE_SHIFT); } =20 +static void mmu_split_memory_region(struct kvm *kvm, int slot) +{ + struct kvm_memslots *slots =3D kvm_memslots(kvm); + struct kvm_memory_slot *memslot =3D id_to_memslot(slots, slot); + phys_addr_t start =3D memslot->base_gfn << PAGE_SHIFT; + phys_addr_t end =3D (memslot->base_gfn + memslot->npages) << PAGE_SHIFT; + struct kvm_gstage gstage; + bool flush; + + kvm_riscv_gstage_init(&gstage, kvm); + + write_lock(&kvm->mmu_lock); + flush =3D mmu_split_huge_pages(&gstage, start, end); + write_unlock(&kvm->mmu_lock); + + if (flush) + kvm_flush_remote_tlbs_memslot(kvm, memslot); +} + void kvm_arch_commit_memory_region(struct kvm *kvm, struct kvm_memory_slot *old, const struct kvm_memory_slot *new, @@ -165,6 +240,7 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, if (kvm_dirty_log_manual_protect_and_init_set(kvm)) return; mmu_wp_memory_region(kvm, new->id); + mmu_split_memory_region(kvm, new->id); } } =20 --=20 2.39.3 From nobody Fri Oct 2 13:10:33 2026 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D853B27F7; Fri, 31 Jul 2026 09:16:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489374; cv=none; b=JgnSu5xEgJmn1W/SEyQMcWAqzXNnkWrf3/+1vXxpaLgz9jSE0wSUHmWPFHyVl0hUbUQ9Y7IMovmpxSW/r1NH3qZT6aVbgwozEIvy5HXFAVGnpGdCegfJ73tAvwe7HZ/OMDulcNImeWIgsHL9ttj1T1hM68Z5Wl6it1nAP3u810w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489374; c=relaxed/simple; bh=Cyta+rVK/Or3/NdrZhNGcMVhyiJUOjlkGQGciqPITB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EGKAWLXAmFGuDizQMwVTUaH1AqXg3huPDg7GFLSj1atWi2+d5+8TmdguHyjl/h248jsk8qWj4nZsjqT1a6t6EHHNPQ645xhsr26xCyZXhzDSu3otWjQzeYZ62auXYd5y+4lhQUC02Eq6Y/btPu+JVhhlbcEr3LZadaXsnLLZ5bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4hBL5z5jTnz57HS5; Fri, 31 Jul 2026 17:16:07 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl1.zte.com.cn with SMTP id 66V9Fsox003236; Fri, 31 Jul 2026 17:15:54 +0800 (+08) (envelope-from wang.yechao255@zte.com.cn) Received: from localhost.localdomain (unknown [10.234.74.162]) by smtp (Zmail) with SMTP; Fri, 31 Jul 2026 17:15:56 +0800 X-Zmail-TransId: 3e816a6c67cc010-3c182 X-Zmail-LocalSMTP: 1 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 X-Zmail-RealSender: wang.yechao255@zte.com.cn From: Wang Yechao To: Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Alexandre Ghiti , Wang Yechao Subject: [PATCH v5 3/5] RISC-V: KVM: Remove redundant TLB flush operations Date: Fri, 31 Jul 2026 17:12:08 +0800 Message-ID: <20260731091215.1549430-4-wang.yechao255@zte.com.cn> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> References: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 66V9Fsox003236 X-TLS: YES X-ENVELOPE-SENDER: wang.yechao255@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 31 Jul 2026 17:16:07 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A6C67D7.001/4hBL5z5jTnz57HS5 Content-Type: text/plain; charset="utf-8" The function kvm_arch_mmu_enable_log_dirty_pt_masked() is invoked from two distinct call paths: kvm_clear_dirty_log_protect() kvm_arch_mmu_enable_log_dirty_pt_masked() kvm_vm_ioctl_reset_dirty_pages() kvm_dirty_ring_reset() kvm_reset_dirty_gfn() kvm_arch_mmu_enable_log_dirty_pt_masked() In both scenarios, the caller already performs a remote TLB flush after dirty logging is enabled, so the TLB flush inside kvm_arch_mmu_enable_log_dirty_pt_masked() is unnecessary. Remove it. Signed-off-by: Wang Yechao Reviewed-by: Anup Patel --- arch/riscv/kvm/mmu.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 2362eeb6c08a5..5a93cffa9ad7f 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -163,14 +163,15 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct k= vm *kvm, phys_addr_t start =3D (base_gfn + __ffs(mask)) << PAGE_SHIFT; phys_addr_t end =3D (base_gfn + __fls(mask) + 1) << PAGE_SHIFT; struct kvm_gstage gstage; - bool flush; =20 kvm_riscv_gstage_init(&gstage, kvm); =20 - flush =3D kvm_riscv_gstage_wp_range(&gstage, start, end); - if (flush) - kvm_flush_remote_tlbs_range(kvm, start >> PAGE_SHIFT, - (end - start) >> PAGE_SHIFT); + kvm_riscv_gstage_wp_range(&gstage, start, end); + + /* + * remote TLB flush is not needed here since callers of + * kvm_arch_mmu_enable_log_dirty_pt_masked() already do it. + */ } =20 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *mems= lot) --=20 2.39.3 From nobody Fri Oct 2 13:10:33 2026 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68B053AAF6F; Fri, 31 Jul 2026 09:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489375; cv=none; b=PWDt9FBVYcU6/bkec+aOEElk7v+i6IExxDQ7S2Qsio3lCNwClOodVNCO9q7CjZfy2girxRjUX4JcFZSNnq99PQBOEUjLT+wXkqFy+pb88/EmlQn7gDTHOidgQUECd562w9HHL0XVJE0ASnoNiKlrK0orgE1H8wh4eYgQPdDZrlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489375; c=relaxed/simple; bh=dsvX28XBTPZiireTMv30DmBsv3mMZny4/maajGR9AD4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RAFUHdxM4K2QvGtWRYCyH4Z/iTBlP7MWZlwLx2V2nGL0Z/fAMRacjl2qOUD1TYbImPOLHMGVgJqpwggG3uQIeBBzr4DFvf0edVt6dr/q69WQFXWsMDHNoMZRKjzb+DPw3n5hWP5iJUxA1Zqna+9HS9LbPOF4e2ZRY5h2lOG3Ur0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4hBL605Vscz57HS3; Fri, 31 Jul 2026 17:16:08 +0800 (CST) Received: from szxlzmapp01.zte.com.cn ([10.5.231.85]) by mse-fl1.zte.com.cn with SMTP id 66V9FuDq003267; Fri, 31 Jul 2026 17:15:56 +0800 (+08) (envelope-from wang.yechao255@zte.com.cn) Received: from localhost.localdomain (unknown [10.234.74.162]) by smtp (Zmail) with SMTP; Fri, 31 Jul 2026 17:15:58 +0800 X-Zmail-TransId: 3e816a6c67ce010-3c1a3 X-Zmail-LocalSMTP: 1 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 X-Zmail-RealSender: wang.yechao255@zte.com.cn From: Wang Yechao To: Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Alexandre Ghiti , Wang Yechao Subject: [PATCH v5 4/5] RISC-V: KVM: Split huge pages during KVM_CLEAR_DIRTY_LOG Date: Fri, 31 Jul 2026 17:12:09 +0800 Message-ID: <20260731091215.1549430-5-wang.yechao255@zte.com.cn> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> References: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 66V9FuDq003267 X-TLS: YES X-ENVELOPE-SENDER: wang.yechao255@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 31 Jul 2026 17:16:08 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A6C67D8.001/4hBL605Vscz57HS3 Content-Type: text/plain; charset="utf-8" Split huge pages on the range specified using KVM_CLEAR_DIRTY_LOG. And do not split when enabling dirty logging if KVM_DIRTY_LOG_INITIALLY_SET is set. Signed-off-by: Wang Yechao Reviewed-by: Anup Patel --- arch/riscv/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 5a93cffa9ad7f..722637a85bdf9 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -168,6 +168,9 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm= *kvm, =20 kvm_riscv_gstage_wp_range(&gstage, start, end); =20 + if (kvm_dirty_log_manual_protect_and_init_set(kvm)) + mmu_split_huge_pages(&gstage, start, end); + /* * remote TLB flush is not needed here since callers of * kvm_arch_mmu_enable_log_dirty_pt_masked() already do it. --=20 2.39.3 From nobody Fri Oct 2 13:10:33 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5AA23B3898; Fri, 31 Jul 2026 09:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489378; cv=none; b=Bba9yN/DiKeaVHncRP+bfsQLFxqEQPKNHObwcBwREZXTcV5neq+C4LBkmqNI0zRmO8wQg0tiJgnNFh/ObQO5YDHLXN8OwLA6uyanPywvxpmQ2eRP7DMrzNQlw9JW6cVWoGuYUwtOVFaPhz87JNWMgjcpndqqulKi+on9PatZ1S0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785489378; c=relaxed/simple; bh=O9Q6ljWlKbtdwbmOJflZu2hULQ61r+gRzn0UmxwZUQ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DQzr12Bx4aNw9Y2hw11Iq4tj56S/iRscJLIrQcF6mubzzbXnVaV5OfqjykjKSPGkQ6aopkSqJwFphuxYTqWaj0xGm9DWlYp+avjWFCaF1oy9ZQJW8VX+kYiCixc4fmO+DGVsYDcLqNnpDjbrBy0zdrTTYUlAM0SqQSLrD0DCoGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4hBL652B62z8Xrnt; Fri, 31 Jul 2026 17:16:13 +0800 (CST) Received: from szxlzmapp01.zte.com.cn ([10.5.231.85]) by mse-fl1.zte.com.cn with SMTP id 66V9Fxm8003341; Fri, 31 Jul 2026 17:15:59 +0800 (+08) (envelope-from wang.yechao255@zte.com.cn) Received: from localhost.localdomain (unknown [10.234.74.162]) by smtp (Zmail) with SMTP; Fri, 31 Jul 2026 17:16:01 +0800 X-Zmail-TransId: 3e816a6c67d0010-3c1bd X-Zmail-LocalSMTP: 1 X-ZMAIL-USEORIGINALEMLTOOUTBOUND: 1 X-Zmail-RealSender: wang.yechao255@zte.com.cn From: Wang Yechao To: Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Alexandre Ghiti , Wang Yechao , linux-doc Subject: [PATCH v5 5/5] RISC-V: KVM: Add the eager_page_split module parameter Date: Fri, 31 Jul 2026 17:12:10 +0800 Message-ID: <20260731091215.1549430-6-wang.yechao255@zte.com.cn> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> References: <20260731091215.1549430-1-wang.yechao255@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MAIL: mse-fl1.zte.com.cn 66V9Fxm8003341 X-TLS: YES X-ENVELOPE-SENDER: wang.yechao255@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 31 Jul 2026 17:16:13 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A6C67DD.000/4hBL652B62z8Xrnt Content-Type: text/plain; charset="utf-8" Add an eager_page_split module parameter for RISC-V KVM, following the same approach as on x86. This parameter controls whether eager page splitting is enabled. The default value is on. When eager page splitting is enabled, KVM proactively splits large pages (huge pages) into smaller pages when needed for dirty logging or other operations. Disabling it can be beneficial for VM workloads that rarely perform writes, or that only write to a small region of memory, as it allows huge pages to remain intact for read accesses. Signed-off-by: Wang Yechao Reviewed-by: Anup Patel --- Documentation/admin-guide/kernel-parameters.txt | 7 +++++-- arch/riscv/kvm/mmu.c | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index b5493a7f8f228..d78eda7ee23a5 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3047,7 +3047,7 @@ Kernel parameters Default is 0 (don't ignore, but inject #GP) =20 kvm.eager_page_split=3D - [KVM,X86] Controls whether or not KVM will try to + [KVM,X86,RISCV] Controls whether or not KVM will try to proactively split all huge pages during dirty logging. Eager page splitting reduces interruptions to vCPU execution by eliminating the write-protection faults @@ -3067,7 +3067,10 @@ Kernel parameters the KVM_CLEAR_DIRTY ioctl, and only for the pages being cleared. =20 - Eager page splitting is only supported when kvm.tdp_mmu=3DY. + On x86, eager page splitting is only supported when + kvm.tdp_mmu=3DY. + + On RISCV, eager page splitting is supported by default. =20 Default is Y (on). =20 diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 722637a85bdf9..4314753fc47a8 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -16,6 +16,9 @@ #include #include =20 +static bool __read_mostly eager_page_split =3D true; +module_param(eager_page_split, bool, 0644); + static void mmu_wp_memory_region(struct kvm *kvm, int slot) { struct kvm_memslots *slots =3D kvm_memslots(kvm); @@ -168,8 +171,10 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kv= m *kvm, =20 kvm_riscv_gstage_wp_range(&gstage, start, end); =20 - if (kvm_dirty_log_manual_protect_and_init_set(kvm)) - mmu_split_huge_pages(&gstage, start, end); + if (kvm_dirty_log_manual_protect_and_init_set(kvm)) { + if (READ_ONCE(eager_page_split)) + mmu_split_huge_pages(&gstage, start, end); + } =20 /* * remote TLB flush is not needed here since callers of @@ -244,7 +249,9 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, if (kvm_dirty_log_manual_protect_and_init_set(kvm)) return; mmu_wp_memory_region(kvm, new->id); - mmu_split_memory_region(kvm, new->id); + + if (READ_ONCE(eager_page_split)) + mmu_split_memory_region(kvm, new->id); } } =20 --=20 2.39.3