Skip to contentSkip to conversation

KVM: kvmclock: Fix vCPUs > 64 can't be online/hotpluged

PATCH Superseded Wanpeng Li 3 participants 5 messages Jan 14, 2021 – Jan 19, 2021

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wanpeng Li

author
#0 lore raw

PATCH 1/1 KVM: kvmclock: Fix vCPUs > 64 can't be online/hotpluged

From: Wanpeng Li <[email protected]>

The per-cpu vsyscall pvclock data pointer assigns either an element of the static array hv_clock_boot (#vCPU <= 64) or dynamically allocated memory hvclock_mem (vCPU > 64), the dynamically memory will not be allocated if kvmclock vsyscall is disabled, this can result in cpu hotpluged fails in kvmclock_setup_percpu() which returns -ENOMEM. This patch fixes it by not assigning vsyscall pvclock data pointer if kvmclock vdso_clock_mode is not VDSO_CLOCKMODE_PVCLOCK.

Fixes 6a1cac56f4 ("x86/kvm: Use __bss_decrypted attribute in shared variables")
Cc Brijesh Singh <[email protected]>
Cc [email protected]#v4.19-rc5+
---
 arch/x86/kernel/kvmclock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/kvmclock.c +2 -1
index aa59374..0624290 100644
@@ -296,7 +296,8 @@ static int kvmclock_setup_percpu(unsigned int cpu)
 	 * pointers. So carefully check. CPU0 has been set up in init
 	 * already.
 	 */
-	if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+	if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)) ||
+	    (kvm_clock.vdso_clock_mode != VDSO_CLOCKMODE_PVCLOCK))
 		return 0;
 
 	/* Use the static page for the first CPUs, allocate otherwise */
Signature · 1 line
2.7.4
#1 lore raw
Quoted 25 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

The per-cpu vsyscall pvclock data pointer assigns either an element of the static array hv_clock_boot (#vCPU <= 64) or dynamically allocated memory hvclock_mem (vCPU > 64), the dynamically memory will not be allocated if kvmclock vsyscall is disabled, this can result in cpu hotpluged fails in kvmclock_setup_percpu() which returns -ENOMEM. This patch fixes it by not assigning vsyscall pvclock data pointer if kvmclock vdso_clock_mode is not VDSO_CLOCKMODE_PVCLOCK.

Fixes 6a1cac56f4 ("x86/kvm: Use __bss_decrypted attribute in shared variables")
Cc Brijesh Singh <[email protected]>
Cc [email protected]#v4.19-rc5+
---
 arch/x86/kernel/kvmclock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/kvmclock.c +0 -0
index aa59374..0624290 100644

arch/x86/kernel/kvmclock.c static int kvmclock_setup_percpu(unsigned int cpu) · line 301

Quoted 7 lines
arch/x86/kernel/kvmclock.c +2 -1
@@ -296,7 +296,8 @@ static int kvmclock_setup_percpu(unsigned int cpu)
 	 * pointers. So carefully check. CPU0 has been set up in init
 	 * already.
 	 */
-	if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
+	if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)) ||
+	    (kvm_clock.vdso_clock_mode != VDSO_CLOCKMODE_PVCLOCK))
 		return 0;

The comment above should probably be updated as it is not clear why we check kvm_clock.vdso_clock_mode here. Actually, I would even suggest we introduce a 'kvmclock_tsc_stable' global instead to avoid this indirect check.

arch/x86/kernel/kvmclock.c static int kvmclock_setup_percpu(unsigned int cpu) · line 302

Quoted 2 lines
arch/x86/kernel/kvmclock.c +0 -0
 	/* Use the static page for the first CPUs, allocate otherwise */

Also, would it be better if we just avoid cpuhp_setup_state() call in this case? E.g. both these ideas combined (completely untested):

arch/x86/kernel/kvmclock.c +6 -2
index aa593743acf6..0827aef3ccb8 100644
@@ -25,6 +25,7 @@
 
 static int kvmclock __initdata = 1;
 static int kvmclock_vsyscall __initdata = 1;
+static bool kvmclock_tsc_stable __ro_after_init = true;
 static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
 static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
 static u64 kvm_sched_clock_offset __ro_after_init;
@@ -275,8 +276,10 @@ static int __init kvm_setup_vsyscall_timeinfo(void)
                return 0;
 
        flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
-       if (!(flags & PVCLOCK_TSC_STABLE_BIT))
+       if (!(flags & PVCLOCK_TSC_STABLE_BIT)) {
+               kvmclock_tsc_stable = false;
                return 0;
+       }
 
        kvm_clock.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
 #endif
@@ -325,7 +328,8 @@ void __init kvmclock_init(void)
                return;
        }
 
-       if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
+       if (kvmclock_tsc_stable &&
+           cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
                              kvmclock_setup_percpu, NULL) < 0) {
                return;
        }
Signature · 1 line
Vitaly
author
#2 lore raw
Quoted 32 lines · Vitaly Kuznetsov
Quoted 29 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

The per-cpu vsyscall pvclock data pointer assigns either an element of the static array hv_clock_boot (#vCPU <= 64) or dynamically allocated memory hvclock_mem (vCPU > 64), the dynamically memory will not be allocated if kvmclock vsyscall is disabled, this can result in cpu hotpluged fails in kvmclock_setup_percpu() which returns -ENOMEM. This patch fixes it by not assigning vsyscall pvclock data pointer if kvmclock vdso_clock_mode is not VDSO_CLOCKMODE_PVCLOCK.

Fixes 6a1cac56f4 ("x86/kvm: Use __bss_decrypted attribute in shared variables")
Cc Brijesh Singh <[email protected]>
Cc [email protected]#v4.19-rc5+
---
 arch/x86/kernel/kvmclock.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/kvmclock.c +0 -1
index aa59374..0624290 100644
@@ -296,7 +296,8 @@ static int kvmclock_setup_percpu(unsigned int cpu)
       * pointers. So carefully check. CPU0 has been set up in init
       * already.
       */
-     if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
Quoted 8 lines

arch/x86/kernel/kvmclock.c static int kvmclock_setup_percpu(unsigned int cpu) · line 301

Quoted 3 lines
arch/x86/kernel/kvmclock.c +2 -0
+     if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)) ||
+         (kvm_clock.vdso_clock_mode != VDSO_CLOCKMODE_PVCLOCK))
              return 0;

The comment above should probably be updated as it is not clear why we check kvm_clock.vdso_clock_mode here. Actually, I would even suggest we introduce a 'kvmclock_tsc_stable' global instead to avoid this indirect check.

I prefer to update the comment above, assign vsyscall pvclock data pointer iff kvmclock vsyscall is enabled.

Wanpeng

maintainer
#3 lore raw
Quoted 6 lines · Wanpeng Li
Quoted 4 lines

The comment above should probably be updated as it is not clear why we check kvm_clock.vdso_clock_mode here. Actually, I would even suggest we introduce a 'kvmclock_tsc_stable' global instead to avoid this indirect check.

I prefer to update the comment above, assign vsyscall pvclock data pointer iff kvmclock vsyscall is enabled.

Are you going to send v2?

Paolo

author
#4 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Wanpeng Li
Quoted 4 lines

The comment above should probably be updated as it is not clear why we check kvm_clock.vdso_clock_mode here. Actually, I would even suggest we introduce a 'kvmclock_tsc_stable' global instead to avoid this indirect check.

I prefer to update the comment above, assign vsyscall pvclock data pointer iff kvmclock vsyscall is enabled.

Are you going to send v2?

Yes. :) https://lore.kernel.org/kvm/[email protected]/

Wanpeng