Skip to contentSkip to conversation

Enable PV qspinlock for Hyper-V

PATCH In review Yi Sun 4 participants 10 messages Oct 8, 2018 – Oct 10, 2018

Subsystems (3)
lore
Share & tools
lore

Yi Sun

author
#0 lore raw

v4 cover Enable PV qspinlock for Hyper-V

v3->v4:
- To avoid possible hang issue (if hv_qlock_wait can be interrupted, kick may happen before reading HV_X64_MSR_GUEST_IDLE so that hang happens), disable interrupt before READ_ONCE, then restore it after reading HV_X64_MSR_GUEST_IDLE.
v2->v3:
- use "Hyper-V: " as the message prefix
- remove unnecessary header files
- remove unnecessary check in 'hv_qlock_wait'
- fix compilation error on different platforms

v1->v2:
- compile hv_spinlock.c only when CONFIG_PARAVIRT_SPINLOCKS enabled
- merge v1 patch 2/3 to single patch
- remove part of the boilerplate in hv_spinlock.c
- declare hv_pvspin as __initdata
- remove spin_wait_info and hv_notify_long_spin_wait because SpinWaitInfo is a standalone feature.
- add comments for reading HV_X64_MSR_GUEST_IDLE
- replace pr_warn to pr_info
- use pr_fmt instead of the 'hv:' prefix
- register callback function for smp_ops.smp_prepare_boot_cpu to initialize hyper-v spinlock

This patch adds the necessary Hyper-V specific code to allow PV qspinlock work on Hyper-V.

In wait callback function, read HV_X64_MSR_GUEST_IDLE MSR to trigger the guest's transition to the idle power state which can be exited by an IPI even if IF flag is disabled.

In kick callback function, just send platform IPI to make waiting vcpu exit idle state.

In vcpu_is_preempted callback function, return false directly because Hyper-V does not provide such interface so far.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Michael Kelley (EOSG) <[email protected]>
Cc Juergen Gross <[email protected]>

Yi Sun (2):
X86/Hyper-V: Add Guest IDLE MSR support locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

 Documentation/admin-guide/kernel-parameters.txt |  5 ++
 arch/x86/hyperv/Makefile                        |  4 ++
 arch/x86/hyperv/hv_spinlock.c                   | 85 +++++++++++++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h              |  5 ++
 arch/x86/include/asm/mshyperv.h                 |  1 +
 arch/x86/kernel/cpu/mshyperv.c                  | 14 ++++
 6 files changed, 114 insertions(+)
 create mode 100644 arch/x86/hyperv/hv_spinlock.c
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v4 1/2 X86/Hyper-V: Add Guest IDLE MSR support

Hyper-V may expose a HV_X64_MSR_GUEST_IDLE MSR. We can read HYPERV_CPUID_FEATURES eax to check it. Read this MSR can trigger the guest's transition to the idle power state which can be exited by an IPI even if IF flag is disabled.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
---
 arch/x86/include/asm/hyperv-tlfs.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/include/asm/hyperv-tlfs.h +5 -0
index 00e01d2..4139f76 100644
@@ -38,6 +38,8 @@
 #define HV_MSR_TIME_REF_COUNT_AVAILABLE		(1 << 1)
 /* Partition reference TSC MSR is available */
 #define HV_MSR_REFERENCE_TSC_AVAILABLE		(1 << 9)
+/* Partition Guest IDLE MSR is available */
+#define HV_X64_MSR_GUEST_IDLE_AVAILABLE		(1 << 10)
 
 /* A partition's reference time stamp counter (TSC) page */
 #define HV_X64_MSR_REFERENCE_TSC		0x40000021
@@ -246,6 +248,9 @@
 #define HV_X64_MSR_STIMER3_CONFIG		0x400000B6
 #define HV_X64_MSR_STIMER3_COUNT		0x400000B7
 
+/* Hyper-V guest idle MSR */
+#define HV_X64_MSR_GUEST_IDLE			0x400000F0
+
 /* Hyper-V guest crash notification MSR's */
 #define HV_X64_MSR_CRASH_P0			0x40000100
 #define HV_X64_MSR_CRASH_P1			0x40000101
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v4 2/2 locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

In reply to Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Michael Kelley (EOSG) <[email protected]>
Cc Juergen Gross <[email protected]>

---
v3->v4:
- To avoid possible hang issue (if hv_qlock_wait can be interrupted, kick may happen before reading HV_X64_MSR_GUEST_IDLE so that hang happens), disable interrupt before READ_ONCE, then restore it after reading HV_X64_MSR_GUEST_IDLE. (suggested by Juergen Gross)

---
 Documentation/admin-guide/kernel-parameters.txt |  5 ++
 arch/x86/hyperv/Makefile                        |  4 ++
 arch/x86/hyperv/hv_spinlock.c                   | 85 +++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h                 |  1 +
 arch/x86/kernel/cpu/mshyperv.c                  | 14 ++++
 5 files changed, 109 insertions(+)
 create mode 100644 arch/x86/hyperv/hv_spinlock.c
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 92eb1f4..0fc8448 100644
@@ -1385,6 +1385,11 @@
 	hvc_iucv_allow=	[S390]	Comma-separated list of z/VM user IDs.
 				If specified, z/VM IUCV HVC accepts connections
 				from listed z/VM user IDs only.
+
+	hv_nopvspin	[X86,HYPER_V]
+			Disables the ticketlock slowpath using HYPER-V PV
+			optimizations.
+
 	keep_bootcon	[KNL]
 			Do not unregister boot console at start. This is only
 			useful for debugging when something happens in the window
arch/x86/hyperv/Makefile +4 -0
index b21ee65..1c11f94 100644
@@ -1,2 +1,6 @@
 obj-y			:= hv_init.o mmu.o nested.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
+
+ifdef CONFIG_X86_64
+obj-$(CONFIG_PARAVIRT_SPINLOCKS)	+= hv_spinlock.o
+endif
arch/x86/hyperv/hv_spinlock.c new file +85 -0

85 lines not on this page · raw message

arch/x86/include/asm/mshyperv.h +1 -0
index f377044..759cfd2 100644
@@ -351,6 +351,7 @@ static inline int cpumask_to_vpset(struct hv_vpset *vpset,
 
 #ifdef CONFIG_X86_64
 void hv_apic_init(void);
+void __init hv_init_spinlocks(void);
 #else
 static inline void hv_apic_init(void) {}
 #endif
arch/x86/kernel/cpu/mshyperv.c +14 -0
index ad12733..a5cc219 100644
@@ -199,6 +199,16 @@ static unsigned long hv_get_tsc_khz(void)
 	return freq / 1000;
 }
 
+#if defined(CONFIG_SMP) && IS_ENABLED(CONFIG_HYPERV)
+static void __init hv_smp_prepare_boot_cpu(void)
+{
+	native_smp_prepare_boot_cpu();
+#if defined(CONFIG_X86_64) && defined(CONFIG_PARAVIRT_SPINLOCKS)
+	hv_init_spinlocks();
+#endif
+}
+#endif
+
 static void __init ms_hyperv_init_platform(void)
 {
 	int hv_host_info_eax;
@@ -303,6 +313,10 @@ static void __init ms_hyperv_init_platform(void)
 	if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE)
 		alloc_intr_gate(HYPERV_STIMER0_VECTOR,
 				hv_stimer0_callback_vector);
+
+#if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+#endif
 #endif
 }
 
Signature · 1 line
1.9.1
Reviewed-by on v4 2/2
#3 lore raw

Re: [PATCH v4 2/2] locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

Quoted 10 lines · Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Michael Kelley (EOSG) <[email protected]>
Cc Juergen Gross <[email protected]>

Juergen

#4 lore raw

[tip:x86/hyperv] x86/hyperv: Enable PV qspinlock for Hyper-V

In reply to Yi Sun
x86/hyperv: Enable PV qspinlock for Hyper-V
Commit-ID:  b36bcb7ce0b353bd4889e06b61e036744b02ca51
Gitweb:     https://git.kernel.org/tip/b36bcb7ce0b353bd4889e06b61e036744b02ca51
Author:     Yi Sun <[email protected]>
AuthorDate: Mon, 8 Oct 2018 16:29:34 +0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 9 Oct 2018 11:26:31 +0200

x86/hyperv: Enable PV qspinlock for Hyper-V

Implement the required wait and kick callbacks to support PV spinlocks in Hyper-V guests.

[ tglx: Document the requirement for disabling interrupts in the wait()
callback. Remove goto and unnecessary includes. Add prototype for hv_vcpu_is_preempted(). ]

---
 Documentation/admin-guide/kernel-parameters.txt |  5 ++
 arch/x86/hyperv/Makefile                        |  4 ++
 arch/x86/hyperv/hv_spinlock.c                   | 88 +++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h                 |  2 +
 arch/x86/kernel/cpu/mshyperv.c                  | 14 ++++
 5 files changed, 113 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 92eb1f42240d..ca6ca93e8281 100644
@@ -1385,6 +1385,11 @@
 	hvc_iucv_allow=	[S390]	Comma-separated list of z/VM user IDs.
 				If specified, z/VM IUCV HVC accepts connections
 				from listed z/VM user IDs only.
+
+	hv_nopvspin	[X86,HYPER_V] Disables the paravirt spinlock optimizations
+				      which allow the hypervisor to 'idle' the
+				      guest on lock contention.
+
 	keep_bootcon	[KNL]
 			Do not unregister boot console at start. This is only
 			useful for debugging when something happens in the window
arch/x86/hyperv/Makefile +4 -0
index b21ee65c4101..1c11f9420a82 100644
@@ -1,2 +1,6 @@
 obj-y			:= hv_init.o mmu.o nested.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
+
+ifdef CONFIG_X86_64
+obj-$(CONFIG_PARAVIRT_SPINLOCKS)	+= hv_spinlock.o
+endif
arch/x86/hyperv/hv_spinlock.c new file +88 -0

88 lines not on this page · raw message

arch/x86/include/asm/mshyperv.h +2 -0
index f37704497d8f..0d6271cce198 100644
@@ -351,6 +351,8 @@ int hyperv_flush_guest_mapping(u64 as);
 
 #ifdef CONFIG_X86_64
 void hv_apic_init(void);
+void __init hv_init_spinlocks(void);
+bool hv_vcpu_is_preempted(int vcpu);
 #else
 static inline void hv_apic_init(void) {}
 #endif
arch/x86/kernel/cpu/mshyperv.c +14 -0
index ad12733f6058..2179c7efc1f4 100644
@@ -199,6 +199,16 @@ static unsigned long hv_get_tsc_khz(void)
 	return freq / 1000;
 }
 
+#if defined(CONFIG_SMP) && IS_ENABLED(CONFIG_HYPERV)
+static void __init hv_smp_prepare_boot_cpu(void)
+{
+	native_smp_prepare_boot_cpu();
+#if defined(CONFIG_X86_64) && defined(CONFIG_PARAVIRT_SPINLOCKS)
+	hv_init_spinlocks();
+#endif
+}
+#endif
+
 static void __init ms_hyperv_init_platform(void)
 {
 	int hv_host_info_eax;
@@ -303,6 +313,10 @@ static void __init ms_hyperv_init_platform(void)
 	if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE)
 		alloc_intr_gate(HYPERV_STIMER0_VECTOR,
 				hv_stimer0_callback_vector);
+
+# if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+# endif
 #endif
 }
 
#5 lore raw

Re: [PATCH v4 2/2] locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

In reply to Yi Sun

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 319

Quoted 6 lines · Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

arch/x86/kernel/cpu/mshyperv.c +3 -0
+#if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+#endif

What's wrong with using the common pattern of:

	#ifdef CONFIG_SMP

?

Thanks,

Ingo

author
#6 lore raw

Re: [PATCH v4 2/2] locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

Quoted 12 lines · Ingo Molnar

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 319

Quoted 6 lines · Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

arch/x86/kernel/cpu/mshyperv.c +3 -0
+#if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+#endif

What's wrong with using the common pattern of:

Quoted 3 lines
	#ifdef CONFIG_SMP

?

There is no difference between "#ifdef" and "#if defined". I just copied it from "hv_smp_prepare_boot_cpu()". Do you need me submit a fix patch after this set is merged?

Quoted 4 lines

Thanks,

Ingo

#7 lore raw

Re: [PATCH v4 2/2] locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

Quoted 13 lines · Yi Sun
Quoted 12 lines · Ingo Molnar

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 319

Quoted 6 lines · Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

arch/x86/kernel/cpu/mshyperv.c +3 -0
+#if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+#endif

What's wrong with using the common pattern of:

Quoted 7 lines
Quoted 3 lines
	#ifdef CONFIG_SMP

?

There is no difference between "#ifdef" and "#if defined". I just copied it from "hv_smp_prepare_boot_cpu()". Do you need me submit a fix patch after this set is merged?

It's equivalent code, my point was that '#ifdef CONFIG_XYZ' is the shorter, more canonical pattern we use in the kernel most of the time, it's shorter, easier to read. We only use defined() for longer preprocessor directive conditions, and it's a pattern for "there's something more complex here than a simple CONFIG_XYZ dependency".

Anyway, Thomas fixed it up in the latest iteration.

Thanks,

Ingo

author
#8 lore raw

Re: [PATCH v4 2/2] locking/pvqspinlock, hv: Enable PV qspinlock for Hyper-V

Quoted 29 lines · Ingo Molnar
Quoted 13 lines · Yi Sun
Quoted 12 lines · Ingo Molnar

arch/x86/kernel/cpu/mshyperv.c static void __init ms_hyperv_init_platform(void) · line 319

Quoted 6 lines · Yi Sun

Follow PV spinlock mechanism to implement the callback functions to allow the CPU idling and kicking operations on Hyper-V.

arch/x86/kernel/cpu/mshyperv.c +3 -0
+#if defined(CONFIG_SMP)
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+#endif

What's wrong with using the common pattern of:

Quoted 7 lines
Quoted 3 lines
	#ifdef CONFIG_SMP

?

There is no difference between "#ifdef" and "#if defined". I just copied it from "hv_smp_prepare_boot_cpu()". Do you need me submit a fix patch after this set is merged?

It's equivalent code, my point was that '#ifdef CONFIG_XYZ' is the shorter, more canonical pattern we use in the kernel most of the time, it's shorter, easier to read. We only use defined() for longer preprocessor directive conditions, and it's a pattern for "there's something more complex here than a simple CONFIG_XYZ dependency".

Quoted 5 lines

Anyway, Thomas fixed it up in the latest iteration.

Thanks,

Ingo

Thank you! I will notice it in the future.

BRs,
Yi Sun

#9 lore raw

[tip:x86/paravirt] x86/hyperv: Enable PV qspinlock for Hyper-V

In reply to Yi Sun
x86/hyperv: Enable PV qspinlock for Hyper-V
Commit-ID:  3a025de64bf89c84a79909069e3c24ad9e710d27
Gitweb:     https://git.kernel.org/tip/3a025de64bf89c84a79909069e3c24ad9e710d27
Author:     Yi Sun <[email protected]>
AuthorDate: Mon, 8 Oct 2018 16:29:34 +0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 9 Oct 2018 14:21:39 +0200

x86/hyperv: Enable PV qspinlock for Hyper-V

Implement the required wait and kick callbacks to support PV spinlocks in Hyper-V guests.

[ tglx: Document the requirement for disabling interrupts in the wait()
callback. Remove goto and unnecessary includes. Add prototype for hv_vcpu_is_preempted(). Adapted to pending paravirt changes. ]

---
 Documentation/admin-guide/kernel-parameters.txt |  5 ++
 arch/x86/hyperv/Makefile                        |  4 ++
 arch/x86/hyperv/hv_spinlock.c                   | 88 +++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h                 |  2 +
 arch/x86/kernel/cpu/mshyperv.c                  | 14 ++++
 5 files changed, 113 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +5 -0
index 9871e649ffef..4cdf6a673592 100644
@@ -1385,6 +1385,11 @@
 	hvc_iucv_allow=	[S390]	Comma-separated list of z/VM user IDs.
 				If specified, z/VM IUCV HVC accepts connections
 				from listed z/VM user IDs only.
+
+	hv_nopvspin	[X86,HYPER_V] Disables the paravirt spinlock optimizations
+				      which allow the hypervisor to 'idle' the
+				      guest on lock contention.
+
 	keep_bootcon	[KNL]
 			Do not unregister boot console at start. This is only
 			useful for debugging when something happens in the window
arch/x86/hyperv/Makefile +4 -0
index b21ee65c4101..1c11f9420a82 100644
@@ -1,2 +1,6 @@
 obj-y			:= hv_init.o mmu.o nested.o
 obj-$(CONFIG_X86_64)	+= hv_apic.o
+
+ifdef CONFIG_X86_64
+obj-$(CONFIG_PARAVIRT_SPINLOCKS)	+= hv_spinlock.o
+endif
arch/x86/hyperv/hv_spinlock.c new file +88 -0

88 lines not on this page · raw message

arch/x86/include/asm/mshyperv.h +2 -0
index f37704497d8f..0d6271cce198 100644
@@ -351,6 +351,8 @@ int hyperv_flush_guest_mapping(u64 as);
 
 #ifdef CONFIG_X86_64
 void hv_apic_init(void);
+void __init hv_init_spinlocks(void);
+bool hv_vcpu_is_preempted(int vcpu);
 #else
 static inline void hv_apic_init(void) {}
 #endif
arch/x86/kernel/cpu/mshyperv.c +14 -0
index ad12733f6058..1c72f3819eb1 100644
@@ -199,6 +199,16 @@ static unsigned long hv_get_tsc_khz(void)
 	return freq / 1000;
 }
 
+#if defined(CONFIG_SMP) && IS_ENABLED(CONFIG_HYPERV)
+static void __init hv_smp_prepare_boot_cpu(void)
+{
+	native_smp_prepare_boot_cpu();
+#if defined(CONFIG_X86_64) && defined(CONFIG_PARAVIRT_SPINLOCKS)
+	hv_init_spinlocks();
+#endif
+}
+#endif
+
 static void __init ms_hyperv_init_platform(void)
 {
 	int hv_host_info_eax;
@@ -303,6 +313,10 @@ static void __init ms_hyperv_init_platform(void)
 	if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE)
 		alloc_intr_gate(HYPERV_STIMER0_VECTOR,
 				hv_stimer0_callback_vector);
+
+# ifdef CONFIG_SMP
+	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
+# endif
 #endif
 }