[PATCH 4/4] KVM: x86: open pv lazy tscdeadline forcily w/o modification in qemu

From: Wang Jianchao
Date: Thu Jul 06 2023 - 22:06:58 EST


This is not part of the patchset, but just a debug patch to make
the test convenient. It can open the pv lazy tscdeadline w/o
involving the qemu.

echo 1 > /proc/sys/kernel/apic_lazy_tsc_deadline

Since it is just for testing, the serializing is not so exact.
Please use it w/o any running guests ;)

Signed-off-by: Wang Jianchao <jianchwa@xxxxxxxxxxx>
---
arch/x86/kernel/apic/apic.c | 1 +
arch/x86/kvm/cpuid.c | 13 +++++++++++++
kernel/sysctl.c | 12 ++++++++++++
3 files changed, 26 insertions(+)

diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c
index 0fe1215..e60aaa3 100644
--- a/arch/x86/kernel/apic/apic.c
+++ b/arch/x86/kernel/apic/apic.c
@@ -679,6 +679,7 @@ static void setup_APIC_timer(void)
if (kvm_para_available() &&
kvm_para_has_feature(KVM_FEATURE_LAZY_TSCDEADLINE)) {
levt->set_next_event = kvm_lapic_next_deadline;
+ pr_info("%s: switch set_next_event to lazy tscdeadline version\n", __func__);
} else {
levt->set_next_event = lapic_next_deadline;
}
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 5a12601..ee5a828 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -380,9 +380,12 @@ u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
}

+extern int sysctl_apic_lazy_tsc_deadline;
+
static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
int nent)
{
+ struct kvm_cpuid_entry2 *pve2;
int r;

__kvm_update_cpuid_runtime(vcpu, e2, nent);
@@ -423,6 +426,16 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,

vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
+
+ if (sysctl_apic_lazy_tsc_deadline) {
+ pve2 = kvm_find_kvm_cpuid_features(vcpu);
+ if (pve2) {
+ pr_info("set lazy tscdeadline forcily\n");
+ pve2->eax |= 1 << KVM_FEATURE_LAZY_TSCDEADLINE;
+ } else {
+ pr_err("cannot open lazy tscdeadline forcily\n");
+ }
+ }
kvm_vcpu_after_set_cpuid(vcpu);

return 0;
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index bfe53e8..f5f94dd 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -137,6 +137,9 @@ int sysctl_legacy_va_layout;

#endif /* CONFIG_SYSCTL */

+int sysctl_apic_lazy_tsc_deadline;
+EXPORT_SYMBOL_GPL(sysctl_apic_lazy_tsc_deadline);
+
/*
* /proc/sys support
*/
@@ -2055,6 +2058,15 @@ static struct ctl_table kern_table[] = {
.extra2 = SYSCTL_INT_MAX,
},
#endif
+ {
+ .procname = "apic_lazy_tsc_deadline",
+ .data = &sysctl_apic_lazy_tsc_deadline,
+ .maxlen = sizeof(sysctl_apic_lazy_tsc_deadline),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
{ }
};

--
2.7.4


--------------55CAD26FA83D0286C7C60A7F--