Skip to contentSkip to conversation

LoongArch: KVM: Add PV TLB flush support

PATCH Superseded Tao Cui 2 participants 9 messages Jun 2

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

v3 cover LoongArch: KVM: Add PV TLB flush support

From: Tao Cui <[email protected]>

This series implements paravirtualized TLB flush for LoongArch KVM guests.

In multi-vCPU KVM guests, remote TLB flushes broadcast IPIs to all target vCPUs, including those preempted by the host. Sending IPIs to preempted vCPUs causes unnecessary VM exits and grows with vCPU count, becoming severe in overcommitted deployments.

Reuse the existing steal-time shared memory page by adding a new KVM_VCPU_FLUSH_TLB flag to the preempted byte. On the guest side, skip IPIs to preempted vCPUs and set the flag via cmpxchg instead. On the host side, when re-entering a vCPU in kvm_update_stolen_time(), check and clear the flag; if set, drop the VPID to trigger a full TLB flush on the next VM entry. No new shared memory page, hypercall, or Kconfig option is needed.

Testing: boot a 32-vCPU guest with the following QEMU command, then run the selftest benchmark inside the guest with sleep idle (PV helps) and busy-spin (PV cannot optimize) modes respectively:

  /root/qemu_branch_origin/build/qemu-system-loongarch64 \
    -m 4G -smp 32 --cpu la464 --machine virt \
    -bios /root/bios_loongarch64/QEMU_EFI.fd \
    -kernel /boot/vmlinuz-7.1.0-rc5-next-20260529-pvtlb-v3-fix+ \
    -initrd /tmp/ramdisk_test.gz \
    -serial mon:stdio \
    -netdev tap,id=net0,ifname=tap0,script=no,downscript=no \
    -device virtio-net-pci,netdev=net0 \
    -append "root=/dev/ram rdinit=/sbin/init console=ttyS0,115200" -nographic

# PV TLB flush enabled (idle threads sleep, vCPUs get preempted) guest# ./tlb_bench 1 31 50000 0

# Baseline (idle threads busy-spin, all vCPUs stay active) guest# ./tlb_bench 1 31 50000 1

With PV TLB flush (sleep idle): ~152,285 ns/flush
Without PV TLB flush (busy-spin): ~481,045 ns/flush

Improvement: 68.3% latency reduction (~3.2x throughput increase)

Tao Cui (4):
LoongArch: KVM: Preserve auto-enabled PV features on userspace
override
LoongArch: KVM: Add PV TLB flush support via steal-time shared memory
LoongArch: KVM: Implement guest-side PV TLB flush
KVM: selftests: loongarch: Add PV TLB flush performance test

 arch/loongarch/include/asm/kvm_host.h         |   3 +
 arch/loongarch/include/asm/kvm_para.h         |   9 +
 arch/loongarch/include/asm/paravirt.h         |  21 ++
 arch/loongarch/include/uapi/asm/kvm.h         |   1 +
 arch/loongarch/include/uapi/asm/kvm_para.h    |   1 +
 arch/loongarch/kernel/paravirt.c              |  54 +++++
 arch/loongarch/kernel/smp.c                   |  30 ++-
 arch/loongarch/kvm/trace.h                    |  15 ++
 arch/loongarch/kvm/vcpu.c                     |  50 ++++-
 arch/loongarch/kvm/vm.c                       |   4 +
 .../kvm/loongarch/pv_tlb_flush_test.c         | 194 ++++++++++++++++++
 11 files changed, 375 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c

---
Changes in v3:
- Host side: replace amswap_db.w with amand_db.w to atomically read and clear only the preempted byte, preserving the pad bytes for future UAPI use. Issue a normal load (unsafe_get_user) before the atomic amand_db.w to avoid operating on stale cache data.
- Host side: move the pv_auto_features OR operation before the compatibility check in kvm_loongarch_cpucfg_set_attr() so that userspace does not need updating for pure kernel-internal PV feature additions.
- Selftest: add input validation, error checking on pthread_create, and cleanup handling on failure.

Changes in v2:
- Host side: replace non-atomic unsafe_get_user + unsafe_put_user with atomic amswap_db.w inline assembly. This fixes two issues:
1) unsafe_put_user failure could skip the TLB flush entirely
2) non-atomic read+write race with guest-side try_cmpxchg could cause FLUSH_TLB requests to be lost
- Guest side: consolidate two separate READ_ONCE calls into a single READ_ONCE to eliminate a TOCTOU race where the host could clear preempted between the two reads. Also switch from byte-sized try_cmpxchg to 32-bit try_cmpxchg on the aligned word containing preempted.

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/4 LoongArch: KVM: Preserve auto-enabled PV features on userspace override

From: Tao Cui <[email protected]>

When userspace (e.g., QEMU) sets PV features via KVM_SET_DEVICE_ATTR with CPUCFG_KVM_FEATURE, it completely replaces the kernel-default pv_features. This causes features auto-enabled by the kernel (such as KVM_FEATURE_STEAL_TIME) to be lost if userspace doesn't explicitly include them.

Add a pv_auto_features field to struct kvm_arch that saves the kernel-default PV features set during kvm_vm_init_features(). When userspace overrides pv_features, OR the saved auto features back to preserve them.

---
 arch/loongarch/include/asm/kvm_host.h | 2 ++
 arch/loongarch/kvm/vcpu.c             | 7 +++++++
 arch/loongarch/kvm/vm.c               | 1 +
 3 files changed, 10 insertions(+)
arch/loongarch/include/asm/kvm_host.h +2 -0
index 776bc487a705..903cb67ca817 100644
@@ -127,6 +127,8 @@ struct kvm_arch {
 	struct kvm_phyid_map  *phyid_map;
 	/* Enabled PV features */
 	unsigned long pv_features;
+	/* PV features auto-enabled by kernel */
+	unsigned long pv_auto_features;
 	/* Supported KVM features */
 	unsigned long kvm_features;
 
arch/loongarch/kvm/vcpu.c +7 -0
index e28084c49e68..47a59ce9e561 100644
@@ -1165,6 +1165,13 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,
 		if (val & ~valid)
 			return -EINVAL;
 
+		/*
+		 * Auto-enabled features (e.g. PV TLB flush) that userspace
+		 * is not aware of are preserved so that no QEMU changes are
+		 * needed for pure kernel-internal optimisations.
+		 */
+		val = (val | kvm->arch.pv_auto_features) & valid;
+
 		/* All vCPUs need set the same PV features */
 		if ((kvm->arch.pv_features & LOONGARCH_PV_FEAT_UPDATED)
 				&& ((kvm->arch.pv_features & valid) != val))
arch/loongarch/kvm/vm.c +1 -0
index 1317c718f896..8af874e0b36b 100644
@@ -57,6 +57,7 @@ static void kvm_vm_init_features(struct kvm *kvm)
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_PREEMPT);
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_STEALTIME);
 	}
+	kvm->arch.pv_auto_features = kvm->arch.pv_features;
 }
 
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v3 2/4 LoongArch: KVM: Add PV TLB flush support via steal-time shared memory

In reply to Tao Cui

From: Tao Cui <[email protected]>

Implement paravirtualized TLB flush for LoongArch KVM guests using the existing steal-time shared memory page.

The mechanism uses the preempted byte in struct kvm_steal_time with an additional KVM_VCPU_FLUSH_TLB flag bit:

- When a guest vCPU needs remote TLB flush but the target vCPU is preempted (not running), it atomically sets KVM_VCPU_FLUSH_TLB in the target's steal-time preempted byte instead of sending an IPI.
- When the host re-enters the target vCPU (kvm_update_stolen_time()), it atomically reads and clears only the preempted byte via amand_db.w with mask ~0xff on the 32-bit aligned word. amand_db.w provides full barrier semantics and avoids the race with guest-side try_cmpxchg(). LoongArch is little-endian, so the preempted byte occupies bits [7:0]; this preserves pad[0..2] for future UAPI extension. If KVM_VCPU_FLUSH_TLB was set, the host drops the vCPU's VPID, which triggers a full TLB flush on the next VM entry via kvm_check_vpid().
- For non-preempted vCPUs, the guest falls back to normal IPI-based flush, avoiding unnecessary VM exits.

Issue a normal load (unsafe_get_user) before the atomic amand_db.w to avoid operating on stale cache data when the cache line was last modified by a different core.

This significantly reduces TLB flush overhead in multi-vCPU workloads where target vCPUs are often idle/preempted.

---
 arch/loongarch/include/asm/kvm_host.h      |  1 +
 arch/loongarch/include/asm/kvm_para.h      |  1 +
 arch/loongarch/include/uapi/asm/kvm.h      |  1 +
 arch/loongarch/include/uapi/asm/kvm_para.h |  1 +
 arch/loongarch/kvm/trace.h                 | 15 +++++++++++
 arch/loongarch/kvm/vcpu.c                  | 30 +++++++++++++++++++++-
 arch/loongarch/kvm/vm.c                    |  3 +++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +1 -0
index 903cb67ca817..a379868fb147 100644
@@ -170,6 +170,7 @@ enum emulation_result {
 #define LOONGARCH_PV_FEAT_MASK		(BIT(KVM_FEATURE_IPI) |		\
 					 BIT(KVM_FEATURE_PREEMPT) |	\
 					 BIT(KVM_FEATURE_STEAL_TIME) |	\
+					 BIT(KVM_FEATURE_PV_TLB_FLUSH) |\
 					 BIT(KVM_FEATURE_USER_HCALL) |	\
 					 BIT(KVM_FEATURE_VIRT_EXTIOI))
 
arch/loongarch/include/asm/kvm_para.h +1 -0
index fb17ba0fa101..28e3fa3b4c0e 100644
@@ -41,6 +41,7 @@ struct kvm_steal_time {
 	__u8  pad[47];
 };
 #define KVM_VCPU_PREEMPTED		(1 << 0)
+#define KVM_VCPU_FLUSH_TLB		(1 << 1)
 
 /*
  * Hypercall interface for KVM hypervisor
arch/loongarch/include/uapi/asm/kvm.h +1 -0
index cd0b5c11ca9c..e4cd4bbf8914 100644
@@ -106,6 +106,7 @@ struct kvm_fpu {
 #define  KVM_LOONGARCH_VM_FEAT_PTW		8
 #define  KVM_LOONGARCH_VM_FEAT_MSGINT		9
 #define  KVM_LOONGARCH_VM_FEAT_PV_PREEMPT	10
+#define  KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH	11
 
 /* Device Control API on vcpu fd */
 #define KVM_LOONGARCH_VCPU_CPUCFG	0
arch/loongarch/include/uapi/asm/kvm_para.h +1 -0
index d28cbcadd276..8872839251cc 100644
@@ -16,6 +16,7 @@
 #define  KVM_FEATURE_IPI		1
 #define  KVM_FEATURE_STEAL_TIME		2
 #define  KVM_FEATURE_PREEMPT		3
+#define  KVM_FEATURE_PV_TLB_FLUSH	4
 /* BIT 24 - 31 are features configurable by user space vmm */
 #define  KVM_FEATURE_VIRT_EXTIOI	24
 #define  KVM_FEATURE_USER_HCALL		25
arch/loongarch/kvm/trace.h +15 -0
index 3467ee22b704..8556954fa196 100644
@@ -210,6 +210,21 @@ TRACE_EVENT(kvm_vpid_change,
 	    TP_printk("VPID: 0x%08lx", __entry->vpid)
 );
 
+TRACE_EVENT(kvm_pv_tlb_flush,
+	TP_PROTO(struct kvm_vcpu *vcpu, bool need_flush),
+	TP_ARGS(vcpu, need_flush),
+	TP_STRUCT__entry(
+		__field(unsigned int, vcpu_id)
+		__field(bool, need_flush)
+	),
+	TP_fast_assign(
+		__entry->vcpu_id = vcpu->vcpu_id;
+		__entry->need_flush = need_flush;
+	),
+	TP_printk("vcpu %u need_flush %u", __entry->vcpu_id,
+		  __entry->need_flush)
+);
+
 #endif /* _TRACE_KVM_H */
 
 #undef TRACE_INCLUDE_PATH
arch/loongarch/kvm/vcpu.c +29 -1
index 47a59ce9e561..e4c19d0b8710 100644
@@ -173,7 +173,35 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu)
 	}
 
 	st = (struct kvm_steal_time __user *)ghc->hva;
-	if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PREEMPT)) {
+	if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) {
+		u32 old = 0;
+		int err = 0;
+
+		/* Issue a normal load to avoid stale cache data for amand_db.w */
+		unsafe_get_user(old, (u32 __user *)&st->preempted, out);
+
+		/* Atomically read and clear the preempted byte via amand_db.w. */
+		asm volatile(
+		"1: amand_db.w %1, %3, %2	\n"
+		"2:				\n"
+		_ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1)
+		: "+r" (err), "+&r" (old),
+		  "+ZB" (*(u32 *)&st->preempted)
+		: "r" ((u32)~0xffu)
+		: "memory");
+
+		if (err)
+			goto out;
+
+		vcpu->arch.st.preempted = 0;
+
+		if ((u8)old & KVM_VCPU_FLUSH_TLB) {
+			vcpu->arch.vpid = 0;	/* Drop vpid to flush TLB */
+			trace_kvm_pv_tlb_flush(vcpu, true);
+		} else {
+			trace_kvm_pv_tlb_flush(vcpu, false);
+		}
+	} else if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PREEMPT)) {
 		unsafe_put_user(0, &st->preempted, out);
 		vcpu->arch.st.preempted = 0;
 	}
arch/loongarch/kvm/vm.c +3 -0
index 8af874e0b36b..4be70e4f1279 100644
@@ -54,8 +54,10 @@ static void kvm_vm_init_features(struct kvm *kvm)
 	if (kvm_pvtime_supported()) {
 		kvm->arch.pv_features |= BIT(KVM_FEATURE_PREEMPT);
 		kvm->arch.pv_features |= BIT(KVM_FEATURE_STEAL_TIME);
+		kvm->arch.pv_features |= BIT(KVM_FEATURE_PV_TLB_FLUSH);
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_PREEMPT);
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_STEALTIME);
+		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH);
 	}
 	kvm->arch.pv_auto_features = kvm->arch.pv_features;
 }
@@ -159,6 +161,7 @@ static int kvm_vm_feature_has_attr(struct kvm *kvm, struct kvm_device_attr *attr
 	case KVM_LOONGARCH_VM_FEAT_PV_IPI:
 	case KVM_LOONGARCH_VM_FEAT_PV_PREEMPT:
 	case KVM_LOONGARCH_VM_FEAT_PV_STEALTIME:
+	case KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH:
 		if (kvm_vm_support(&kvm->arch, attr->attr))
 			return 0;
 		return -ENXIO;
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v3 3/4 LoongArch: KVM: Implement guest-side PV TLB flush

In reply to Tao Cui

From: Tao Cui <[email protected]>

Add the guest-side implementation of PV TLB flush for LoongArch KVM guests, complementing the host-side support added in the previous commit.

When running as a KVM guest, remote TLB flushes are optimized by avoiding IPIs to preempted vCPUs:

- kvm_flush_tlb_mask() checks each target vCPU's steal-time preempted flag. If a vCPU is preempted, it atomically sets KVM_VCPU_FLUSH_TLB in the shared preempted byte via cmpxchg and removes that CPU from the IPI mask.
- Only non-preempted vCPUs receive IPIs via on_each_cpu_mask().
- When the host schedules a deferred-flush vCPU back in, it invalidates the VPID and flushes the TLB automatically.

All six SMP TLB flush functions (flush_tlb_all, flush_tlb_mm, flush_tlb_range, flush_tlb_kernel_range, flush_tlb_page, flush_tlb_one) are updated to use the PV path when the static key pv_tlb_flush_key is enabled.

The feature is gated by KVM_FEATURE_PV_TLB_FLUSH and requires KVM_FEATURE_STEAL_TIME (for the shared memory page). Per-CPU cpumask buffers are allocated via arch_initcall.

---
 arch/loongarch/include/asm/kvm_para.h |  8 ++++
 arch/loongarch/include/asm/paravirt.h | 21 +++++++++++
 arch/loongarch/kernel/paravirt.c      | 54 +++++++++++++++++++++++++++
 arch/loongarch/kernel/smp.c           | 30 ++++++++++++---
 4 files changed, 107 insertions(+), 6 deletions(-)
arch/loongarch/include/asm/kvm_para.h +8 -0
index 28e3fa3b4c0e..7956aeff2436 100644
@@ -187,4 +187,12 @@ static inline bool kvm_check_and_clear_guest_paused(void)
 	return false;
 }
 
+static inline bool kvm_pv_tlb_flush_supported(void)
+{
+	unsigned int feat = kvm_arch_para_features();
+
+	return (feat & (1U << KVM_FEATURE_PV_TLB_FLUSH)) &&
+	       (feat & (1U << KVM_FEATURE_STEAL_TIME));
+}
+
 #endif /* _ASM_LOONGARCH_KVM_PARA_H */
arch/loongarch/include/asm/paravirt.h +21 -0
index acae1c5e5f88..6ce62b555a5d 100644
@@ -5,15 +5,26 @@
 #ifdef CONFIG_PARAVIRT
 
 #include <linux/jump_label.h>
+#include <linux/cpumask.h>
+#include <linux/smp.h>
 
 DECLARE_STATIC_KEY_FALSE(virt_preempt_key);
 DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);
+DECLARE_STATIC_KEY_FALSE(pv_tlb_flush_key);
 DECLARE_PER_CPU(struct kvm_steal_time, steal_time);
 
 int __init pv_ipi_init(void);
 int __init pv_time_init(void);
 int __init pv_spinlock_init(void);
 
+void kvm_flush_tlb_mask(const struct cpumask *cpumask,
+			smp_call_func_t func, void *info);
+
+static inline bool pv_tlb_flush_enabled(void)
+{
+	return static_branch_unlikely(&pv_tlb_flush_key);
+}
+
 #else
 
 static inline int pv_ipi_init(void)
@@ -31,5 +42,15 @@ static inline int pv_spinlock_init(void)
 	return 0;
 }
 
+static inline bool pv_tlb_flush_enabled(void)
+{
+	return false;
+}
+
+static inline void kvm_flush_tlb_mask(const struct cpumask *cpumask,
+				       smp_call_func_t func, void *info)
+{
+}
+
 #endif // CONFIG_PARAVIRT
 #endif
arch/loongarch/kernel/paravirt.c +54 -0

67 lines not on this page · raw message

arch/loongarch/kernel/smp.c +24 -6
index 50922610758b..bb3451b057ed 100644
@@ -727,7 +727,10 @@ static void flush_tlb_all_ipi(void *info)
 
 void flush_tlb_all(void)
 {
-	on_each_cpu(flush_tlb_all_ipi, NULL, 1);
+	if (pv_tlb_flush_enabled())
+		kvm_flush_tlb_mask(cpu_online_mask, flush_tlb_all_ipi, NULL);
+	else
+		on_each_cpu(flush_tlb_all_ipi, NULL, 1);
 }
 
 static void flush_tlb_mm_ipi(void *mm)
@@ -743,7 +746,10 @@ void flush_tlb_mm(struct mm_struct *mm)
 	preempt_disable();
 
 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
-		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
+		if (pv_tlb_flush_enabled())
+			kvm_flush_tlb_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm);
+		else
+			on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
 	} else {
 		unsigned int cpu;
 
@@ -782,7 +788,10 @@ void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned l
 			.addr2 = end,
 		};
 
-		on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
+		if (pv_tlb_flush_enabled())
+			kvm_flush_tlb_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd);
+		else
+			on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
 	} else {
 		unsigned int cpu;
 
@@ -809,7 +818,10 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
 		.addr2 = end,
 	};
 
-	on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
+	if (pv_tlb_flush_enabled())
+		kvm_flush_tlb_mask(cpu_online_mask, flush_tlb_kernel_range_ipi, &fd);
+	else
+		on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
 }
 
 static void flush_tlb_page_ipi(void *info)
@@ -828,7 +840,10 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
 			.addr1 = page,
 		};
 
-		on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
+		if (pv_tlb_flush_enabled())
+			kvm_flush_tlb_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd);
+		else
+			on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
 	} else {
 		unsigned int cpu;
 
@@ -851,6 +866,9 @@ static void flush_tlb_one_ipi(void *info)
 
 void flush_tlb_one(unsigned long vaddr)
 {
-	on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
+	if (pv_tlb_flush_enabled())
+		kvm_flush_tlb_mask(cpu_online_mask, flush_tlb_one_ipi, (void *)vaddr);
+	else
+		on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
 }
 EXPORT_SYMBOL(flush_tlb_one);
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH v3 4/4 KVM: selftests: loongarch: Add PV TLB flush performance test

In reply to Tao Cui

From: Tao Cui <[email protected]>

Add a multi-threaded benchmark to measure PV TLB flush performance inside LoongArch KVM guests.

The test spawns flusher threads that repeatedly mmap/munmap to trigger TLB shootdown IPIs, alongside idle threads that either sleep or busy-spin. With PV TLB flush enabled, IPIs to preempted vCPUs are replaced by deferred flags in the steal-time shared page.

Usage (inside guest):
./pv_tlb_flush_test <flushers> <idle> <iterations> <busy_idle> busy_idle=0: idle threads sleep (PV can skip IPIs to preempted vCPUs) busy_idle=1: idle threads spin (all vCPUs active, PV cannot optimize)

---
 .../kvm/loongarch/pv_tlb_flush_test.c         | 194 ++++++++++++++++++
 1 file changed, 194 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c
tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c new file +194 -0
new file mode 100644
index 000000000000..63efaf9ef1cd
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * LoongArch PV TLB Flush Performance Test
+ *
+ * Measure the overhead of remote TLB flushes in a KVM guest by spawning
+ * flusher threads that repeatedly mmap/munmap (triggering TLB shootdown
+ * IPIs) alongside idle threads that either sleep or busy-spin.
+ *
+ * With PV TLB flush enabled, IPIs to preempted vCPUs are replaced by
+ * deferred flags in the steal-time shared page, reducing flush latency.
+ *
+ * Usage:
+ *   Compile on LoongArch guest:
+ *     gcc -O2 -static -pthread -o pv_tlb_flush_test pv_tlb_flush_test.c
+ *   Run (inside KVM guest):
+ *     ./pv_tlb_flush_test <flushers> <idle> <iterations> <busy_idle>
+ *   Examples:
+ *     ./pv_tlb_flush_test 1 31 50000 0   # 1 flusher, 31 sleep, PV helps
+ *     ./pv_tlb_flush_test 1 31 50000 1   # 1 flusher, 31 busy-spin, no PV
+ *
+ *   busy_idle=0: idle threads sleep, vCPUs get preempted, PV TLB flush
+ *                can skip IPIs to them
+ *   busy_idle=1: idle threads spin, all vCPUs stay active, PV TLB flush
+ *                cannot optimize (baseline for comparison)
+ */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sched.h>
+#include <pthread.h>
+
+#define MEM_SIZE (2*1024*1024)
+#define DEFAULT_ITERS 50000
+#define MAX_THREADS 64
+
+static int nr_iters = DEFAULT_ITERS;
+static volatile int start_barrier;
+static volatile int stop_flag;
+static int busy_idle = 0;
+
+struct thread_args {
+	int cpu;
+	unsigned long *result;
+	int *completed;
+};
+
+static inline unsigned long clock_ns(void) {
+    struct timespec ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return (unsigned long)ts.tv_sec * 1000000000UL + ts.tv_nsec;
+}
+
+static void pin_cpu(int cpu) {
+    cpu_set_t set;
+    if (cpu < 0)
+        return;
+    CPU_ZERO(&set);
+    CPU_SET(cpu, &set);
+    sched_setaffinity(0, sizeof(set), &set);
+}
+
+static void *idle_thread(void *arg) {
+    struct thread_args *ta = arg;
+    pin_cpu(ta->cpu);
+    while (!__atomic_load_n(&start_barrier, __ATOMIC_ACQUIRE));
+    if (busy_idle) {
+        volatile long sink = 0;
+        while (!__atomic_load_n(&stop_flag, __ATOMIC_ACQUIRE))
+            sink++;
+    } else {
+        while (!__atomic_load_n(&stop_flag, __ATOMIC_ACQUIRE))
+            usleep(1000);
+    }
+    return NULL;
+}
+
+static void *flush_thread(void *arg) {
+    struct thread_args *ta = arg;
+    unsigned long start, end;
+    int i;
+    size_t mem_size = MEM_SIZE;
+    pin_cpu(ta->cpu);
+    while (!__atomic_load_n(&start_barrier, __ATOMIC_ACQUIRE));
+    start = clock_ns();
+    for (i = 0; i < nr_iters && !__atomic_load_n(&stop_flag, __ATOMIC_ACQUIRE); i++) {
+        void *p = mmap(NULL, mem_size, PROT_READ|PROT_WRITE,
+                       MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+        if (p == MAP_FAILED) break;
+        for (size_t off = 0; off < mem_size; off += 65536)
+            ((volatile char*)p)[off] = 0;
+        munmap(p, mem_size);
+    }
+    end = clock_ns();
+    *ta->result = end - start;
+    *ta->completed = i;
+    return NULL;
+}
+
+int main(int argc, char **argv) {
+    int nr_flush = 1, nr_idle = 3, i, run;
+    int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+    if (argc > 1) nr_flush = atoi(argv[1]);
+    if (argc > 2) nr_idle = atoi(argv[2]);
+    if (argc > 3) nr_iters = atoi(argv[3]);
+    if (argc > 4) busy_idle = atoi(argv[4]);
+
+    if (nr_flush < 1 || nr_idle < 0 || nr_flush + nr_idle > MAX_THREADS) {
+        fprintf(stderr, "Usage: %s [flushers(1-%d)] [idle(0-%d)] [iters] [busy_idle]\n",
+                argv[0], MAX_THREADS, MAX_THREADS);
+        return 1;
+    }
+    if (nr_iters <= 0) {
+        fprintf(stderr, "Error: iterations must be positive\n");
+        return 1;
+    }
+
+    printf("=== TLB Flush Benchmark ===\n");
+    printf("CPUs: %d  Flushers: %d  Idle: %d  Iters: %d  Mode: %s\n",
+           ncpus, nr_flush, nr_idle, nr_iters,
+           busy_idle ? "busy-spin" : "sleep");
+
+    for (run = 0; run < 3; run++) {
+        int total = nr_flush + nr_idle;
+        int do_pin = (total <= ncpus);
+        int created = 0;
+        pthread_t threads[MAX_THREADS];
+        unsigned long results[MAX_THREADS];
+        int completed[MAX_THREADS];
+        struct thread_args args[MAX_THREADS];
+        start_barrier = 0; stop_flag = 0;
+
+        for (i = 0; i < nr_idle; i++) {
+            args[i].cpu = do_pin ? nr_flush + i : -1;
+            args[i].result = NULL;
+            args[i].completed = NULL;
+            if (pthread_create(&threads[i], NULL, idle_thread, &args[i])) {
+                perror("pthread_create idle");
+                goto cleanup;
+            }
+            created++;
+        }
+        for (i = 0; i < nr_flush; i++) {
+            int idx = nr_idle + i;
+            results[idx] = 0;
+            completed[idx] = 0;
+            args[idx].cpu = do_pin ? i : -1;
+            args[idx].result = &results[idx];
+            args[idx].completed = &completed[idx];
+            if (pthread_create(&threads[idx], NULL, flush_thread, &args[idx])) {
+                perror("pthread_create flush");
+                goto cleanup;
+            }
+            created++;
+        }
+
+        usleep(10000);
+        __atomic_store_n(&start_barrier, 1, __ATOMIC_RELEASE);
+        for (i = 0; i < nr_flush; i++)
+            pthread_join(threads[nr_idle + i], NULL);
+        __atomic_store_n(&stop_flag, 1, __ATOMIC_RELEASE);
+        for (i = 0; i < nr_idle; i++)
+            pthread_join(threads[i], NULL);
+
+        unsigned long total_ns = 0;
+        unsigned long total_done = 0;
+        for (i = 0; i < nr_flush; i++) {
+            int idx = nr_idle + i;
+            unsigned long done = completed[idx];
+            if (done == 0) {
+                printf("  Run %d flusher %d: no iterations completed\n", run, i);
+                continue;
+            }
+            printf("  Run %d flusher %d: %lu ns/flush (%lu iters)\n",
+                   run, i, results[idx] / done, done);
+            total_ns += results[idx];
+            total_done += done;
+        }
+        if (total_done > 0)
+            printf("  Run %d Avg: %lu ns/flush\n", run, total_ns / total_done);
+        continue;
+
+cleanup:
+        __atomic_store_n(&start_barrier, 1, __ATOMIC_RELEASE);
+        __atomic_store_n(&stop_flag, 1, __ATOMIC_RELEASE);
+        for (i = 0; i < created; i++)
+            pthread_join(threads[i], NULL);
+        return 1;
+    }
+    return 0;
+}
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/4] LoongArch: KVM: Preserve auto-enabled PV features on userspace override

In reply to Tao Cui
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch unconditionally forces auto-enabled PV features ON, preventing userspace from disabling…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch unconditionally forces auto-enabled PV features ON, preventing userspace from disabling them, which breaks VM capability negotiation and live migration. --

commit 2e3dfd10f9606ff82713655e6b87be567a31ec59
Author: Tao Cui <[email protected]>

LoongArch: KVM: Preserve auto-enabled PV features on userspace override

This commit adds a pv_auto_features field to struct kvm_arch to save the kernel-default PV features. It then alters kvm_loongarch_cpucfg_set_attr() to automatically OR these saved features back into the user-provided feature mask during KVM_SET_DEVICE_ATTR.

Quoted 5 lines
arch/loongarch/kvm/vcpu.c +0 -0
index e28084c49e682..47a59ce9e5616 100644

arch/loongarch/kvm/vcpu.c static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu, · line 1173

Quoted 9 lines
arch/loongarch/kvm/vcpu.c +6 -0
@@ -1165,6 +1165,13 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,
 		if (val & ~valid)
 			return -EINVAL;
 
+		/*
+		 * Auto-enabled features (e.g. PV TLB flush) that userspace
+		 * is not aware of are preserved so that no QEMU changes are
+		 * needed for pure kernel-internal optimisations.
+		 */
+		val = (val | kvm->arch.pv_auto_features) & valid;

[Severity: High]
Does unconditionally forcing auto-enabled PV features on prevent userspace from disabling them?

If a VMM configures a VM for migration to an older host that lacks features like KVM_FEATURE_STEAL_TIME, it will clear the corresponding bits in the feature mask.

By forcing these features back on even when userspace explicitly omitted them, could this cause the guest to observe a feature set unsupported on the migration destination, resulting in migration failures?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH v3 2/4] LoongArch: KVM: Add PV TLB flush support via steal-time shared memory

In reply to Tao Cui
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The KVM_FEATURE_PV_TLB_FLUSH feature is forcefully enabled by default via `pv_auto_features`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The KVM_FEATURE_PV_TLB_FLUSH feature is forcefully enabled by default via pv_auto_features, bypassing capability negotiation and breaking backward live migration. --

commit 36054232bc9801e8c8c49df0b88a0802cdab7a98
Author: Tao Cui <[email protected]>

LoongArch: KVM: Add PV TLB flush support via steal-time shared memory

This commit implements a paravirtualized TLB flush for LoongArch KVM guests using the steal-time shared memory page. It uses the preempted byte to allow guests to request a remote TLB flush without sending an IPI when the target vCPU is preempted.

Quoted 4 lines
arch/loongarch/kvm/vm.c +0 -0
index 8af874e0b36b2..4be70e4f1279f 100644

[ ... ]

arch/loongarch/kvm/vm.c static void kvm_vm_init_features(struct kvm *kvm) · line 63

Quoted 11 lines
arch/loongarch/kvm/vm.c +2 -0
@@ -54,8 +54,10 @@ static void kvm_vm_init_features(struct kvm *kvm)
 	if (kvm_pvtime_supported()) {
 		kvm->arch.pv_features |= BIT(KVM_FEATURE_PREEMPT);
 		kvm->arch.pv_features |= BIT(KVM_FEATURE_STEAL_TIME);
+		kvm->arch.pv_features |= BIT(KVM_FEATURE_PV_TLB_FLUSH);
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_PREEMPT);
 		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_STEALTIME);
+		kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH);
 	}
 	kvm->arch.pv_auto_features = kvm->arch.pv_features;
 }

[Severity: Critical]
Does unconditionally adding KVM_FEATURE_PV_TLB_FLUSH to pv_features here force the feature on by default, breaking capability negotiation?

Because kvm_vm_init_features() assigns pv_features to pv_auto_features, the new PV TLB flush feature is included in the auto-enabled set. Looking at kvm_loongarch_cpucfg_set_attr() in arch/loongarch/kvm/vcpu.c, it forces auto-enabled features on:

    val = (val | kvm->arch.pv_auto_features) & valid;

Will this cause the source kernel to override userspace if the vmm explicitly requests the feature to be disabled to maintain live-migration compatibility with older hosts?

If the guest discovers and uses the feature, but migrates to an older host that either ignores the requests or rejects the unknown feature flag, could this result in silent memory corruption or migration failure?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH v3 3/4] LoongArch: KVM: Implement guest-side PV TLB flush

In reply to Tao Cui
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm_flush_tlb_mask() accesses a per-CPU cpumask without disabling preemption, creating a severe…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm_flush_tlb_mask() accesses a per-CPU cpumask without disabling preemption, creating a severe data race. --

commit d7f218c526b255a23f8247a1f67b9091d6410ff4
Author: Tao Cui <[email protected]>

LoongArch: KVM: Implement guest-side PV TLB flush

Add the guest-side implementation of PV TLB flush for LoongArch KVM guests, complementing the host-side support added in the previous commit.

Quoted 4 lines
arch/loongarch/kernel/paravirt.c +0 -0
index 10821cce554c0..34e3ae2d25016 100644

[ ... ]

arch/loongarch/kernel/paravirt.c int __init pv_ipi_init(void) · line 221

Quoted 13 lines
arch/loongarch/kernel/paravirt.c +9 -0
@@ -208,6 +210,58 @@ int __init pv_ipi_init(void)
 	return 0;
 }
 
+#ifdef CONFIG_SMP
+void kvm_flush_tlb_mask(const struct cpumask *cpumask,
+			       smp_call_func_t func, void *info)
+{
+	int cpu;
+	struct kvm_steal_time *src;
+	struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
+
+	cpumask_copy(flushmask, cpumask);

[Severity: High]
Does this per-CPU mask access require preemption to be disabled?

Looking at callers like flush_tlb_all(), flush_tlb_kernel_range(), and flush_tlb_one(), it appears they call kvm_flush_tlb_mask() directly without first disabling preemption. The fallback on_each_cpu() macro disables preemption internally, but this new paravirt path does not.

If the task is preempted and migrated to another CPU after fetching the local CPU's flushmask pointer, could it concurrently modify the original CPU's mask alongside another task scheduled on that CPU?

Also, would this trigger a warning with CONFIG_DEBUG_PREEMPT since this_cpu_cpumask_var_ptr() uses this_cpu_ptr() under the hood?

[ ... ]

Quoted 5 lines
arch/loongarch/kernel/smp.c +0 -0
index 50922610758ba..bb3451b057ed2 100644

arch/loongarch/kernel/smp.c static void flush_tlb_all_ipi(void *info) · line 734

Quoted 9 lines
arch/loongarch/kernel/smp.c +4 -1
@@ -727,7 +727,10 @@ static void flush_tlb_all_ipi(void *info)
 
 void flush_tlb_all(void)
 {
-	on_each_cpu(flush_tlb_all_ipi, NULL, 1);
+	if (pv_tlb_flush_enabled())
+		kvm_flush_tlb_mask(cpu_online_mask, flush_tlb_all_ipi, NULL);
+	else
+		on_each_cpu(flush_tlb_all_ipi, NULL, 1);
 }

[Severity: High]
Are we missing a preempt_disable() around this call to kvm_flush_tlb_mask()?

Because kvm_flush_tlb_mask() accesses a per-CPU cpumask immediately upon entry, calling it with preemption enabled could cause data races if the task migrates to another CPU.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

Re: [PATCH v3 4/4] KVM: selftests: loongarch: Add PV TLB flush performance test

In reply to Tao Cui
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Omission of new selftest file from the KVM selftests Makefile. - [Medium] The test bypasses the…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Omission of new selftest file from the KVM selftests Makefile.
- [Medium] The test bypasses the automated KVM selftests framework and requires manual execution inside a guest OS.
- [Medium] Integer overflow during argument parsing leads to stack buffer overflow.
--

commit 3eef713b92a18797245bf05fb771c17910daf380
Author: Tao Cui <[email protected]>

KVM: selftests: loongarch: Add PV TLB flush performance test

Add a multi-threaded benchmark to measure PV TLB flush performance inside LoongArch KVM guests.

tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c

Quoted 5 lines
tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c new file +0 -0
new file mode 100644
index 0000000000000..63efaf9ef1cd0

[Severity: Medium]
Is this test intentionally omitted from the build system Makefile?

According to the KVM selftests subsystem guidelines, any newly created test file must be integrated into the test Makefile. Because this file is not added to TEST_PROGS, TEST_GEN_PROGS, or TEST_FILES, the automated build system completely ignores it and the test won't be compiled or installed when make or make install is run.

Quoted 7 lines
tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c new file +6 -0
@@ -0,0 +1,194 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * LoongArch PV TLB Flush Performance Test
+ *
+ * Measure the overhead of remote TLB flushes in a KVM guest by spawning
+ * flusher threads that repeatedly mmap/munmap (triggering TLB shootdown

tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c line 16

Quoted 10 lines
tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c new file +10 -0
+ * IPIs) alongside idle threads that either sleep or busy-spin.
+ *
+ * With PV TLB flush enabled, IPIs to preempted vCPUs are replaced by
+ * deferred flags in the steal-time shared page, reducing flush latency.
+ *
+ * Usage:
+ *   Compile on LoongArch guest:
+ *     gcc -O2 -static -pthread -o pv_tlb_flush_test pv_tlb_flush_test.c
+ *   Run (inside KVM guest):
+ *     ./pv_tlb_flush_test <flushers> <idle> <iterations> <busy_idle>

[Severity: Medium]
Does this bypass the automated KVM selftests framework by requiring manual execution inside a guest OS?

KVM selftests in tools/testing/selftests/kvm/ are designed as host-side executables that programmatically create bare-metal micro-VMs to test KVM ioctls.

This patch introduces a standalone POSIX user-space program (using pthreads and the standard C library) that is explicitly designed to be manually compiled and executed from within a full Linux guest OS. This fundamentally violates the architectural model of the KVM selftests framework.

[ ... ]

tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c line 111

Quoted 9 lines
tools/testing/selftests/kvm/loongarch/pv_tlb_flush_test.c new file +9 -0
+int main(int argc, char **argv) {
+    int nr_flush = 1, nr_idle = 3, i, run;
+    int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+    if (argc > 1) nr_flush = atoi(argv[1]);
+    if (argc > 2) nr_idle = atoi(argv[2]);
+    if (argc > 3) nr_iters = atoi(argv[3]);
+    if (argc > 4) busy_idle = atoi(argv[4]);
+
+    if (nr_flush < 1 || nr_idle < 0 || nr_flush + nr_idle > MAX_THREADS) {

[Severity: Medium]
Can this bounds check suffer from integer overflow?

If a user supplies large positive numbers for nr_flush and nr_idle (e.g., 2,000,000,000) parsed via atoi(), their sum could overflow into a negative 32-bit signed integer. This would bypass the MAX_THREADS check.

The subsequent for loops would then use the original large positive values to iterate millions of times, writing far out of bounds on the 64-element stack allocated arrays (threads, results, completed, args), corrupting the stack.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4