[PATCH 1/2] RISC-V: KVM: Move hfence type check out of loop

Wang Yechao <[email protected]> Tue, 7 Jul 2026 19:37:06 +0800
Newsgroups org.infradead.lists.kvm-riscv,org.infradead.lists.linux-riscv,org.kernel.vger.kvm,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
Currently, the check for `data` and `data->type` is performed inside
the loop iterating over each VCPU. Since these values are invariant
during the loop, this check is redundant and adds unnecessary
overhead.

Move the validation to the beginning of the function, returning early
if the hfence data or its type is invalid. This avoids repeated
checks and reduces CPU cycles, which is particularly beneficial for
SMP guests with a large number of VCPUs.

Signed-off-by: Wang Yechao <[email protected]>
---
 arch/riscv/kvm/tlb.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
index 993b25ea94d67..a67b44a4d1d7a 100644
--- a/arch/riscv/kvm/tlb.c
+++ b/arch/riscv/kvm/tlb.c
@@ -335,6 +335,9 @@ static void make_xfence_request(struct kvm *kvm,
 	unsigned int actual_req = req;
 	DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS);
 
+	if (!data || !data->type)
+		return;
+
 	bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		if (hbase != -1UL) {
@@ -347,9 +350,6 @@ static void make_xfence_request(struct kvm *kvm,
 
 		bitmap_set(vcpu_mask, i, 1);
 
-		if (!data || !data->type)
-			continue;
-
 		/*
 		 * Enqueue hfence data to VCPU hfence queue. If we don't
 		 * have space in the VCPU hfence queue then fallback to
-- 
2.43.5


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv