Skip to contentSkip to conversation

x86: Enable a few new instructions

PATCH In review Fenghua Yu 3 participants 19 messages Jul 23, 2018 – Aug 1, 2018

Subsystems (6)
lore
Share & tools
lore

Fenghua Yu

author
#0 lore raw

cover x86: Enable a few new instructions

A few new instructions including direct stores (movdiri and movdir64b) and user wait (umwait, umonitor, and tpause) and IA32_MWAIT_CONTROL MSR to control umwait/umonitor/tpause behaviors will be available in Tremont and other future x86 processors.

This patch set enumerates the instructions, adds a sysfs interface for user to configure the umwait/umonitor/tpause instructions, and provides APIs for user to call the instructions.

The sysfs interface file are in /sys/devices/system/cpu/umwait_control/ umwait_enable_c0_2 because it's hard to find an existing place to host the files.

The user APIs for the instructions are implemented as vDSO functions.

Detailed information on the instructions and the MSR can be found in the latest Intel Architecture Instruction Set Extensions and Future Features Programming Reference at
https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf

Changelog:

Based on comments from Thomas:
- Change user APIs to vDSO functions
- Change sysfs to positive logic and enable file name
- Change patch descriptions etc

Fenghua Yu (7):
x86/cpufeatures: Enumerate MOVDIRI instruction x86/cpufeatures: Enumerate MOVDIR64B instruction x86/cpufeatures: Enumerate UMONITOR, UMWAIT, and TPAUSE instructions x86/umwait_contro: Set global umwait maximum time limit and umwait

    C0.2 state

x86/vdso: Add vDSO functions for direct store instructions x86/vdso: Add vDSO functions for user wait instructions selftests/vDSO: Add selftest to test vDSO functions for direct store
and user wait instructions

 arch/x86/entry/vdso/Makefile                      |   2 +-
 arch/x86/entry/vdso/vdirectstore.c                | 152 ++++++++
 arch/x86/entry/vdso/vdso.lds.S                    |  20 ++
 arch/x86/entry/vdso/vma.c                         |  21 ++
 arch/x86/entry/vdso/vuserwait.c                   | 233 +++++++++++++
 arch/x86/include/asm/cpufeatures.h                |   3 +
 arch/x86/include/asm/msr-index.h                  |   4 +
 arch/x86/include/asm/vdso_funcs_data.h            |  20 ++
 arch/x86/include/asm/vvar.h                       |   1 +
 arch/x86/power/Makefile                           |   1 +
 arch/x86/power/umwait.c                           | 116 +++++++
 tools/testing/selftests/vDSO/Makefile             |   4 +-
 tools/testing/selftests/vDSO/vdso_inst_test_x86.c | 405 ++++++++++++++++++++++
 13 files changed, 980 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/entry/vdso/vdirectstore.c
 create mode 100644 arch/x86/entry/vdso/vuserwait.c
 create mode 100644 arch/x86/include/asm/vdso_funcs_data.h
 create mode 100644 arch/x86/power/umwait.c
 create mode 100644 tools/testing/selftests/vDSO/vdso_inst_test_x86.c
Signature · 1 line
2.5.0
author
#1 lore raw

PATCH 1/7 x86/cpufeatures: Enumerate MOVDIRI instruction

MOVDIRI moves doubleword or quadword from register to memory through direct store which is implemented by using write combining (WC) for writing data directly into memory without caching the data.

Programmable agents can handle streaming offload (e.g. high speed packet processing in network). Hardware implements a doorbell (tail pointer) register that is updated by software when adding new work-elements to the streaming offload work-queue.

MOVDIRI can be used as the doorbell write which is a 4-byte or 8-byte uncachable write to MMIO. MOVDIRI has lower overhead than other ways to write the doorbell.

Availability of the MOVDIRI instruction is indicated by the presence of the CPUID feature flag MOVDIRI(CPUID.0x07.0x0:ECX[bit 27]).

Please check the latest Intel Architecture Instruction Set Extensions and Future Features Programming Reference for more details on the CPUID feature MOVDIRI flag.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 5701f5cecd31..92630c469675 100644
@@ -329,6 +329,7 @@
 #define X86_FEATURE_LA57		(16*32+16) /* 5-level page tables */
 #define X86_FEATURE_RDPID		(16*32+22) /* RDPID instruction */
 #define X86_FEATURE_CLDEMOTE		(16*32+25) /* CLDEMOTE instruction */
+#define X86_FEATURE_MOVDIRI		(16*32+27) /* MOVDIRI instruction */
 
 /* AMD-defined CPU features, CPUID level 0x80000007 (EBX), word 17 */
 #define X86_FEATURE_OVERFLOW_RECOV	(17*32+ 0) /* MCA overflow recovery support */
Signature · 1 line
2.5.0
author
#2 lore raw

PATCH 2/7 x86/cpufeatures: Enumerate MOVDIR64B instruction

In reply to Fenghua Yu

MOVDIR64B moves 64-bytes as direct-store with 64-bytes write atomicity. Direct store is implemented by using write combining (WC) for writing data directly into memory without caching the data.

In low latency offload (e.g. Non-Volatile Memory, etc), MOVDIR64B writes work descriptors (and data in some cases) to device-hosted work-queues atomically without cache pollution.

Availability of the MOVDIR64B instruction is indicated by the presence of the CPUID feature flag MOVDIR64B (CPUID.0x07.0x0:ECX[bit 28]).

Please check the latest Intel Architecture Instruction Set Extensions and Future Features Programming Reference for more details on the CPUID feature MOVDIR64B flag.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 92630c469675..69f1137877b6 100644
@@ -330,6 +330,7 @@
 #define X86_FEATURE_RDPID		(16*32+22) /* RDPID instruction */
 #define X86_FEATURE_CLDEMOTE		(16*32+25) /* CLDEMOTE instruction */
 #define X86_FEATURE_MOVDIRI		(16*32+27) /* MOVDIRI instruction */
+#define X86_FEATURE_MOVDIR64B		(16*32+28) /* MOVDIR64B instruction */
 
 /* AMD-defined CPU features, CPUID level 0x80000007 (EBX), word 17 */
 #define X86_FEATURE_OVERFLOW_RECOV	(17*32+ 0) /* MCA overflow recovery support */
Signature · 1 line
2.5.0
author
#3 lore raw

PATCH 3/7 x86/cpufeatures: Enumerate UMONITOR, UMWAIT, and TPAUSE instructions

In reply to Fenghua Yu

UMONITOR, UMWAIT, and TPAUSE are a set of user wait instructions.

UMONITOR arms address monitoring hardware using an address. A store to an address within the specified address range triggers the monitoring hardware to wake up the processor waiting in umwait.

UMWAIT instructs the processor to enter an implementation-dependent optimized state while monitoring a range of addresses. The optimized state may be either a light-weight power/performance optimized state (c0.1 state) or an improved power/performance optimized state (c0.2 state).

The UMONITOR and UMWAIT operate together to provide power saving in idle.

TPAUSE instructs the processor to enter an implementation-dependent optimized state c0.1 or c0.2 state and wake up when time-stamp counter reaches specified timeout.

The three instructions may be executed at any privilege level.

Availability of the user wait instructions is indicated by the presence of the CPUID feature flag WAITPKG CPUID.0x07.0x0:ECX[5].

Please check the latest Intel Architecture Instruction Set Extensions and Future Features Programming Reference for more details on the instructions and CPUID feature WAITPKG flag.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 69f1137877b6..70ed3087821d 100644
@@ -318,6 +318,7 @@
 #define X86_FEATURE_UMIP		(16*32+ 2) /* User Mode Instruction Protection */
 #define X86_FEATURE_PKU			(16*32+ 3) /* Protection Keys for Userspace */
 #define X86_FEATURE_OSPKE		(16*32+ 4) /* OS Protection Keys Enable */
+#define X86_FEATURE_WAITPKG		(16*32+ 5) /* UMONITOR/UMWAIT/TPAUSE Instructions */
 #define X86_FEATURE_AVX512_VBMI2	(16*32+ 6) /* Additional AVX512 Vector Bit Manipulation Instructions */
 #define X86_FEATURE_GFNI		(16*32+ 8) /* Galois Field New Instructions */
 #define X86_FEATURE_VAES		(16*32+ 9) /* Vector AES */
Signature · 1 line
2.5.0
author
#4 lore raw

PATCH 4/7 x86/umwait_contro: Set global umwait maximum time limit and umwait C0.2 state

In reply to Fenghua Yu

UMWAIT or TPAUSE called by user process makes processor to reside in a light-weight power/performance optimized state (C0.1 state) or an improved power/performance optimized state (C0.2 state).

IA32_UMWAIT_CONTROL MSR register allows OS to set global maximum umwait time and disable C0.2 on the processor.

The maximum time value in IA32_UMWAIT_CONTROL[31-2] is set as zero which means there is no global time limit for UMWAIT and TPAUSE instructions. Each process sets its own umwait maximum time as the instructions operand. We don't set a non-zero global umwait maximum time value to enforce user wait timeout because we couldn't find any usage for it.

By default C0.2 is enabled so user wait instructions can enter the state if user wants to save more power but wakeup time is slower. In some cases e.g. real time, user wants to disable C0.2 and all C0.2 requests revert to C0.1.

A new "/sys/devices/system/cpu/umwait_control/umwait_enable_c0_2" file is created to allow user to check if C0.2 is enabled or disabled and also allow user to enable or disable C0.2. Value "0" in the file means C0.2 is disabled. Value "1" means C0.2 is enabled.

---
 arch/x86/include/asm/msr-index.h |   4 ++
 arch/x86/power/Makefile          |   1 +
 arch/x86/power/umwait.c          | 116 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 arch/x86/power/umwait.c
arch/x86/include/asm/msr-index.h +4 -0
index 68b2c3150de1..92ef30f0f62d 100644
@@ -58,6 +58,10 @@
 #define MSR_PLATFORM_INFO_CPUID_FAULT_BIT	31
 #define MSR_PLATFORM_INFO_CPUID_FAULT		BIT_ULL(MSR_PLATFORM_INFO_CPUID_FAULT_BIT)
 
+#define MSR_IA32_UMWAIT_CONTROL		0xe1
+#define UMWAIT_CONTROL_C02_BIT		0x0
+#define UMWAIT_CONTROL_C02_MASK		0x00000001
+
 #define MSR_PKG_CST_CONFIG_CONTROL	0x000000e2
 #define NHM_C3_AUTO_DEMOTE		(1UL << 25)
 #define NHM_C1_AUTO_DEMOTE		(1UL << 26)
arch/x86/power/Makefile +1 -0
index a4701389562c..d3dfa8a47983 100644
@@ -8,3 +8,4 @@ CFLAGS_cpu.o	:= $(nostackp)
 
 obj-$(CONFIG_PM_SLEEP)		+= cpu.o
 obj-$(CONFIG_HIBERNATION)	+= hibernate_$(BITS).o hibernate_asm_$(BITS).o
+obj-y				+= umwait.o
arch/x86/power/umwait.c new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.5.0
maintainer
#5 lore raw

Re: [PATCH 4/7] x86/umwait_contro: Set global umwait maximum time limit and umwait C0.2 state

Quoted 4 lines · Fenghua Yu

UMWAIT or TPAUSE called by user process makes processor to reside in a light-weight power/performance optimized state (C0.1 state) or an improved power/performance optimized state (C0.2 state).

Quoted 8 lines

IA32_UMWAIT_CONTROL MSR register allows OS to set global maximum umwait time and disable C0.2 on the processor.

The maximum time value in IA32_UMWAIT_CONTROL[31-2] is set as zero which means there is no global time limit for UMWAIT and TPAUSE instructions. Each process sets its own umwait maximum time as the instructions operand. We don't set a non-zero global umwait maximum time value to enforce user wait timeout because we couldn't find any usage for it.

Do you know what the instruction designers had in mind? I assume they were thinking of *something*, but I'm seriously mystified by three things:

- Why does CF work the way it does? It seems like it would be
genuinely useful for CF to indicate whether the in-register timeout has expired, but that's not what CF does.

- Why does the global timeout apply even at CPL 0?

- Why does the C0.2 control apply at CPL 0?

And I'm also a bit surprised that the instruction can't be turned off entirely for CPL 3.

#6 lore raw

Re: [PATCH 4/7] x86/umwait_contro: Set global umwait maximum time limit and umwait C0.2 state

Quoted 14 lines · Andy Lutomirski
Quoted 4 lines · Fenghua Yu

UMWAIT or TPAUSE called by user process makes processor to reside in a light-weight power/performance optimized state (C0.1 state) or an improved power/performance optimized state (C0.2 state).

Quoted 8 lines

IA32_UMWAIT_CONTROL MSR register allows OS to set global maximum umwait time and disable C0.2 on the processor.

The maximum time value in IA32_UMWAIT_CONTROL[31-2] is set as zero which means there is no global time limit for UMWAIT and TPAUSE instructions. Each process sets its own umwait maximum time as the instructions operand. We don't set a non-zero global umwait maximum time value to enforce user wait timeout because we couldn't find any usage for it.

Quoted 6 lines

Do you know what the instruction designers had in mind? I assume they were thinking of *something*, but I'm seriously mystified by three things:

- Why does CF work the way it does? It seems like it would be genuinely
useful for CF to indicate whether the in-register timeout has expired, but that's not what CF does.

Right, it would be useful to see whether the timeout caused the exit or something else.

Thanks,

tglx

author
#7 lore raw

PATCH 5/7 x86/vdso: Add vDSO functions for direct store instructions

In reply to Fenghua Yu

User wants to query if direct store instructions are supported and use the instructions. The vDSO functions provides fast interface for user to query the support and use the instructions.

movdiri_supported and its alias __vdso_movdiri_supported check if movdiri instructions are supported.

movdir64b_supported and its alias __vdso_movdir64b_supported checks if movdir64b instruction is supported.

movdiri32 and its alias __vdso_movdiri32 provide user APIs for calling 32-bit movdiri instruction.

movdiri64 and its alias __vdso_movdiri64 provide user APIs for calling 64-bit movdiri instruction.

movdir64b and its alias __vdso_movdir64b provide user APIs to move 64-byte data through movdir64b instruction.

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the intrinsic functions support in GCC and the APIs movdiri_supported and movdir64b_supported cannot be implemented as GCC functions.

---
 arch/x86/entry/vdso/Makefile           |   2 +-
 arch/x86/entry/vdso/vdirectstore.c     | 152 +++++++++++++++++++++++++++++++++
 arch/x86/entry/vdso/vdso.lds.S         |  10 +++
 arch/x86/entry/vdso/vma.c              |  12 +++
 arch/x86/include/asm/vdso_funcs_data.h |  17 ++++
 arch/x86/include/asm/vvar.h            |   1 +
 6 files changed, 193 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/entry/vdso/vdirectstore.c
 create mode 100644 arch/x86/include/asm/vdso_funcs_data.h
arch/x86/entry/vdso/Makefile +1 -1
index b9ed1aa53a26..af4fcae5de83 100644
@@ -17,7 +17,7 @@ VDSO32-$(CONFIG_X86_32)		:= y
 VDSO32-$(CONFIG_IA32_EMULATION)	:= y
 
 # files to link into the vdso
-vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o
+vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o
 
 # files to link into kernel
 obj-y				+= vma.o
arch/x86/entry/vdso/vdirectstore.c new file +152 -0

152 lines not on this page · raw message

arch/x86/entry/vdso/vdso.lds.S +10 -0
index d3a2dce4cfa9..097cdcda43a5 100644
@@ -25,6 +25,16 @@ VERSION {
 		__vdso_getcpu;
 		time;
 		__vdso_time;
+		movdiri_supported;
+		__vdso_movdiri_supported;
+		movdiri32;
+		__vdso_movdiri32;
+		movdiri64;
+		__vdso_movdiri64;
+		movdir64b_supported;
+		__vdso_movdir64b_supported;
+		movdir64b;
+		__vdso_movdir64b;
 	local: *;
 	};
 }
arch/x86/entry/vdso/vma.c +12 -0
index 5b8b556dbb12..edbe5e63e5c2 100644
@@ -23,11 +23,14 @@
 #include <asm/desc.h>
 #include <asm/cpufeature.h>
 #include <asm/mshyperv.h>
+#include <asm/vdso_funcs_data.h>
 
 #if defined(CONFIG_X86_64)
 unsigned int __read_mostly vdso64_enabled = 1;
 #endif
 
+DEFINE_VVAR(struct vdso_funcs_data, vdso_funcs_data);
+
 void __init init_vdso_image(const struct vdso_image *image)
 {
 	BUG_ON(image->size % PAGE_SIZE != 0);
@@ -367,6 +370,14 @@ static int vgetcpu_online(unsigned int cpu)
 	return smp_call_function_single(cpu, vgetcpu_cpu_init, NULL, 1);
 }
 
+static void __init init_vdso_funcs_data(void)
+{
+	if (static_cpu_has(X86_FEATURE_MOVDIRI))
+		vdso_funcs_data.movdiri_supported = true;
+	if (static_cpu_has(X86_FEATURE_MOVDIR64B))
+		vdso_funcs_data.movdir64b_supported = true;
+}
+
 static int __init init_vdso(void)
 {
 	init_vdso_image(&vdso_image_64);
@@ -374,6 +385,7 @@ static int __init init_vdso(void)
 #ifdef CONFIG_X86_X32_ABI
 	init_vdso_image(&vdso_image_x32);
 #endif
+	init_vdso_funcs_data();
 
 	/* notifier priority > KVM */
 	return cpuhp_setup_state(CPUHP_AP_X86_VDSO_VMA_ONLINE,
arch/x86/include/asm/vdso_funcs_data.h new file +17 -0
new file mode 100644
index 000000000000..b99a5685029e
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_VDSO_FUNCS_DATA_H
+#define _ASM_X86_VDSO_FUNCS_DATA_H
+
+#include <linux/errno.h>
+#include <linux/types.h>
+#include <asm/vvar.h>
+
+/* This structure contains data used by vDSO functions. */
+struct vdso_funcs_data {
+	bool movdiri_supported;   /* if movdiri instruction is supported */
+	bool movdir64b_supported; /* if movdir64b instruction is supported */
+};
+
+#define _vdso_funcs_data (&VVAR(vdso_funcs_data))
+
+#endif /* _ASM_X86_VDSO_FUNCS_DATA_H */
arch/x86/include/asm/vvar.h +1 -0
index 3f32dfc2ab73..74047c0490fe 100644
@@ -45,6 +45,7 @@ extern char __vvar_page;
 /* DECLARE_VVAR(offset, type, name) */
 
 DECLARE_VVAR(128, struct vsyscall_gtod_data, vsyscall_gtod_data)
+DECLARE_VVAR(256, struct vdso_funcs_data, vdso_funcs_data)
 
 #undef DECLARE_VVAR
 
Signature · 1 line
2.5.0
maintainer
#8 lore raw

Re: [PATCH 5/7] x86/vdso: Add vDSO functions for direct store instructions

Quoted 16 lines · Fenghua Yu

User wants to query if direct store instructions are supported and use the instructions. The vDSO functions provides fast interface for user to query the support and use the instructions.

movdiri_supported and its alias __vdso_movdiri_supported check if movdiri instructions are supported.

movdir64b_supported and its alias __vdso_movdir64b_supported checks if movdir64b instruction is supported.

movdiri32 and its alias __vdso_movdiri32 provide user APIs for calling 32-bit movdiri instruction.

movdiri64 and its alias __vdso_movdiri64 provide user APIs for calling 64-bit movdiri instruction.

Quoted 7 lines

movdir64b and its alias __vdso_movdir64b provide user APIs to move 64-byte data through movdir64b instruction.

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the intrinsic functions support in GCC and the APIs movdiri_supported and movdir64b_supported cannot be implemented as GCC functions.

I'm not convinced that any of this belongs in the vDSO at all. You could just add AT_HWCAP (or AT_HWCAP2) flags for the new instructions. Or user code could use CPUID just like for any other new instruction. But, if there really is some compelling reason to add this to the vDSO, then see below:

+

arch/x86/entry/vdso/vdirectstore.c line 13

Quoted 3 lines
arch/x86/entry/vdso/vdirectstore.c new file +3 -0
+notrace bool __vdso_movdiri_supported(void)
+{
+	return _vdso_funcs_data->movdiri_supported;

return static_cpu_has(X86_FEATURE_MOVDIRI);

And all the VVAR stuff can be removed.

author
#9 lore raw

Re: [PATCH 5/7] x86/vdso: Add vDSO functions for direct store instructions

Quoted 5 lines · Andy Lutomirski
Quoted 2 lines · Fenghua Yu

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the

I'm not convinced that any of this belongs in the vDSO at all. You could just add AT_HWCAP (or AT_HWCAP2) flags for the new instructions. Or user

Thomas asked to use vDSO. Please see the discussion thread:
https://lkml.org/lkml/2018/6/19/316

Quoted 6 lines

code could use CPUID just like for any other new instruction. But, if there really is some compelling reason to add this to the vDSO, then see below:

arch/x86/entry/vdso/vdirectstore.c line 13

Quoted 3 lines
arch/x86/entry/vdso/vdirectstore.c new file +3 -0
+notrace bool __vdso_movdiri_supported(void)
+{
+	return _vdso_funcs_data->movdiri_supported;

return static_cpu_has(X86_FEATURE_MOVDIRI);

But boot_cpu_data (used in static_cpu_has) cannot be accessed by user unless mapped in VVAR. So this change cannot be compiled.

Quoted 1 line

And all the VVAR stuff can be removed.

The VVAR stuff needs to map the kernel data _vdso_funcs_data to user space before user can access it.

Thanks.

-Fenghua

maintainer
#10 lore raw

Re: [PATCH 5/7] x86/vdso: Add vDSO functions for direct store instructions

Quoted 9 lines · Fenghua Yu
Quoted 5 lines · Andy Lutomirski
Quoted 2 lines · Fenghua Yu

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the

I'm not convinced that any of this belongs in the vDSO at all. You could just add AT_HWCAP (or AT_HWCAP2) flags for the new instructions. Or user

Thomas asked to use vDSO. Please see the discussion thread:
https://lkml.org/lkml/2018/6/19/316

I think he meant that, if these helpers belong in the kernel at all, then they belong in the vDSO. But I think they mostly don't belong in the kernel.

Quoted 10 lines
Quoted 7 lines

code could use CPUID just like for any other new instruction. But, if there really is some compelling reason to add this to the vDSO, then see below:

arch/x86/entry/vdso/vdirectstore.c line 13

Quoted 3 lines
arch/x86/entry/vdso/vdirectstore.c new file +3 -0
+notrace bool __vdso_movdiri_supported(void)
+{
+    return _vdso_funcs_data->movdiri_supported;

return static_cpu_has(X86_FEATURE_MOVDIRI);

But boot_cpu_data (used in static_cpu_has) cannot be accessed by user unless mapped in VVAR. So this change cannot be compiled.

The underlying alternative infrastructure works in the vDSO. You'd need to introduce an alternate version of _static_cpu_has if BUILD_VDSO that skips the boot_cpu_has fallback.

author
#11 lore raw

Re: [PATCH 5/7] x86/vdso: Add vDSO functions for direct store instructions

Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Fenghua Yu
Quoted 5 lines · Andy Lutomirski
Quoted 2 lines · Fenghua Yu

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the

I'm not convinced that any of this belongs in the vDSO at all. You could just add AT_HWCAP (or AT_HWCAP2) flags for the new instructions. Or user

Quoted 6 lines
Quoted 2 lines

Thomas asked to use vDSO. Please see the discussion thread:
https://lkml.org/lkml/2018/6/19/316

I think he meant that, if these helpers belong in the kernel at all, then they belong in the vDSO. But I think they mostly don't belong in the kernel.

I think both you and Thomas are right. I misunderstood Thomas's comments.

I will remove the APIs/functions from next version of patch set. They don't belong to kernel and will be implemented in either GCC or glibc.

GCC8 already implemented movdiri and movdir64b instruction intrinsics. GCC9 will implement user wait instruction intrinsics.
If user wants to use those instinsics now, this GCC link has them:
https://gcc.gnu.org/git/?p=gcc.git;a=summary

If user wants to use the intrinsics in native code, the user needs to check availability of the features (i.e. movdiri, movdir64b, waitpkg) by running cpuid or a glibc interface that is going to be available in newer glibc.

Thank you for your comments!

-Fenghua

author
#12 lore raw

PATCH 6/7 x86/vdso: Add vDSO functions for user wait instructions

In reply to Fenghua Yu

User wants to query if user wait instructions (umonitor, umwait, and tpause) are supported and use the instructions. The vDSO functions provides fast interface for user to check the support and use the instructions.

waitpkg_supported and its alias __vdso_waitpkg_supported check if user wait instructions (a.k.a. wait package feature) are supported

umonitor and its alias __vdso_umonitor provide user APIs for calling umonitor instruction.

umwait and its alias __vdso_umwait provide user APIs for calling umwait instruction.

tpause and its alias __vdso_tpause provide user APIs for calling tpause instruction.

nsec_to_tsc and its alias __vdso_nsec_to_tsc converts nanoseconds to TSC counter if TSC frequency is known. It will fail if TSC frequency is unknown.

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the intrinsic functions support in GCC and the API waitpkg_supported and nsec_to_tsc cannot be implemented as GCC functions.

---
 arch/x86/entry/vdso/Makefile           |   2 +-
 arch/x86/entry/vdso/vdso.lds.S         |  10 ++
 arch/x86/entry/vdso/vma.c              |   9 ++
 arch/x86/entry/vdso/vuserwait.c        | 233 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/vdso_funcs_data.h |   3 +
 5 files changed, 256 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/entry/vdso/vuserwait.c
arch/x86/entry/vdso/Makefile +1 -1
index af4fcae5de83..fb0062b09b3c 100644
@@ -17,7 +17,7 @@ VDSO32-$(CONFIG_X86_32)		:= y
 VDSO32-$(CONFIG_IA32_EMULATION)	:= y
 
 # files to link into the vdso
-vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o
+vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o vuserwait.o
 
 # files to link into kernel
 obj-y				+= vma.o
arch/x86/entry/vdso/vdso.lds.S +10 -0
index 097cdcda43a5..0942710608bf 100644
@@ -35,6 +35,16 @@ VERSION {
 		__vdso_movdir64b_supported;
 		movdir64b;
 		__vdso_movdir64b;
+		waitpkg_supported;
+		__vdso_waitpkg_supported;
+		umonitor;
+		__vdso_umonitor;
+		umwait;
+		__vdso_umwait;
+		tpause;
+		__vdso_tpause;
+		nsec_to_tsc;
+		__vdso_nsec_to_tsc;
 	local: *;
 	};
 }
arch/x86/entry/vdso/vma.c +9 -0
index edbe5e63e5c2..006dfb5e5003 100644
@@ -372,10 +372,19 @@ static int vgetcpu_online(unsigned int cpu)
 
 static void __init init_vdso_funcs_data(void)
 {
+	struct system_counterval_t sys_counterval;
+
 	if (static_cpu_has(X86_FEATURE_MOVDIRI))
 		vdso_funcs_data.movdiri_supported = true;
 	if (static_cpu_has(X86_FEATURE_MOVDIR64B))
 		vdso_funcs_data.movdir64b_supported = true;
+	if (static_cpu_has(X86_FEATURE_WAITPKG))
+		vdso_funcs_data.waitpkg_supported = true;
+	if (static_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) {
+		vdso_funcs_data.tsc_known_freq = true;
+		sys_counterval = convert_art_ns_to_tsc(1);
+		vdso_funcs_data.tsc_per_nsec = sys_counterval.cycles;
+	}
 }
 
 static int __init init_vdso(void)
arch/x86/entry/vdso/vuserwait.c new file +233 -0

233 lines not on this page · raw message

arch/x86/include/asm/vdso_funcs_data.h +3 -0
index b99a5685029e..a4caa64bbe8d 100644
@@ -10,6 +10,9 @@
 struct vdso_funcs_data {
 	bool movdiri_supported;   /* if movdiri instruction is supported */
 	bool movdir64b_supported; /* if movdir64b instruction is supported */
+	bool waitpkg_supported;   /* if wait pkg instructions are supported */
+	bool tsc_known_freq;      /* if TSC has known freqency */
+	u64  tsc_per_nsec;        /* TSC counter per nanosecond */
 };
 
 #define _vdso_funcs_data (&VVAR(vdso_funcs_data))
Signature · 1 line
2.5.0
maintainer
#13 lore raw

Re: [PATCH 6/7] x86/vdso: Add vDSO functions for user wait instructions

Quoted 81 lines · Fenghua Yu

User wants to query if user wait instructions (umonitor, umwait, and tpause) are supported and use the instructions. The vDSO functions provides fast interface for user to check the support and use the instructions.

waitpkg_supported and its alias __vdso_waitpkg_supported check if user wait instructions (a.k.a. wait package feature) are supported

umonitor and its alias __vdso_umonitor provide user APIs for calling umonitor instruction.

umwait and its alias __vdso_umwait provide user APIs for calling umwait instruction.

tpause and its alias __vdso_tpause provide user APIs for calling tpause instruction.

nsec_to_tsc and its alias __vdso_nsec_to_tsc converts nanoseconds to TSC counter if TSC frequency is known. It will fail if TSC frequency is unknown.

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the intrinsic functions support in GCC and the API waitpkg_supported and nsec_to_tsc cannot be implemented as GCC functions.

---
  arch/x86/entry/vdso/Makefile           |   2 +-
  arch/x86/entry/vdso/vdso.lds.S         |  10 ++
  arch/x86/entry/vdso/vma.c              |   9 ++
  arch/x86/entry/vdso/vuserwait.c        | 233 +++++++++++++++++++++++++++++++++
  arch/x86/include/asm/vdso_funcs_data.h |   3 +
  5 files changed, 256 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/entry/vdso/vuserwait.c

arch/x86/entry/vdso/Makefile +1 -1
index af4fcae5de83..fb0062b09b3c 100644
@@ -17,7 +17,7 @@ VDSO32-$(CONFIG_X86_32)		:= y
  VDSO32-$(CONFIG_IA32_EMULATION)	:= y
 
  # files to link into the vdso
-vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o
+vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o vuserwait.o
 
  # files to link into kernel
  obj-y				+= vma.o
arch/x86/entry/vdso/vdso.lds.S +10 -0
index 097cdcda43a5..0942710608bf 100644
@@ -35,6 +35,16 @@ VERSION {
  		__vdso_movdir64b_supported;
  		movdir64b;
  		__vdso_movdir64b;
+		waitpkg_supported;
+		__vdso_waitpkg_supported;
+		umonitor;
+		__vdso_umonitor;
+		umwait;
+		__vdso_umwait;
+		tpause;
+		__vdso_tpause;
+		nsec_to_tsc;
+		__vdso_nsec_to_tsc;
  	local: *;
  	};
  }
arch/x86/entry/vdso/vma.c +2 -0
index edbe5e63e5c2..006dfb5e5003 100644
@@ -372,10 +372,19 @@ static int vgetcpu_online(unsigned int cpu)
 
  static void __init init_vdso_funcs_data(void)
  {
+	struct system_counterval_t sys_counterval;
+
  	if (static_cpu_has(X86_FEATURE_MOVDIRI))

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu) · line 387

Quoted 10 lines
arch/x86/entry/vdso/vma.c +7 -0
  		vdso_funcs_data.movdiri_supported = true;
  	if (static_cpu_has(X86_FEATURE_MOVDIR64B))
  		vdso_funcs_data.movdir64b_supported = true;
+	if (static_cpu_has(X86_FEATURE_WAITPKG))
+		vdso_funcs_data.waitpkg_supported = true;
+	if (static_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) {
+		vdso_funcs_data.tsc_known_freq = true;
+		sys_counterval = convert_art_ns_to_tsc(1);
+		vdso_funcs_data.tsc_per_nsec = sys_counterval.cycles;
+	}

You're losing a ton of precision here. You might even be losing *all* of the precision and malfunctioning rather badly.

The correct way to do this is:

tsc_counts = ns * mul >> shift;

and the vclock code illustrates it. convert_art_ns_to_tsc() is a bad example because it uses an expensive division operation for no good reason except that no one bothered to optimize it.

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu)

Quoted 9 lines
arch/x86/entry/vdso/vma.c +9 -0
+notrace int __vdso_nsec_to_tsc(unsigned long nsec, unsigned long *tsc)
+{
+	if (!_vdso_funcs_data->tsc_known_freq)
+		return -ENODEV;
+
+	*tsc = _vdso_funcs_data->tsc_per_nsec * nsec;
+
+	return 0;
+}

Please don't expose this one at all. It would be nice for programs that use waitpkg to be migratable using CRIU-like tools, and this export actively harms any such effort. If you omit this function, then the kernel could learn to abort an in-progress __vdso_umwait if preempted (rseq-style) and CRIU would just work. It would be a bit of a hack, but it solves a real problem.

Quoted 1 line

+notrace int __vdso_umwait(int state, unsigned long nsec)

__vdso_umwait_relative(), please. Because some day (possibly soon) someone will want __vdso_umwait_absolute() and its friend __vdso_read_art_ns() so they can do:

u64 start = __vdso_read_art_ns(); __vdso_umonitor(...);
... do something potentially slow or that might fault ... __vdso_umwait_absolute(start + timeout);

Also, this patch appears to have a subtle but show-stopping race. Consider:

1. Task A does UMONITOR on CPU 1
2. Task A is preempted.
3. Task B does UMONITOR on CPU 1 at a different address
4. Task A resumes
5. Task A does UMWAIT

Now task A hangs, at least until the next external wakeup happens.

It's not entirely clear to me how you're supposed to fix this without some abomination that's so bad that it torpedoes the entire feature. Except that there is no chicken bit to turn this thing off. Sigh.

maintainer
#14 lore raw

Re: [PATCH 6/7] x86/vdso: Add vDSO functions for user wait instructions

Quoted 144 lines · Andy Lutomirski
Quoted 82 lines · Fenghua Yu

User wants to query if user wait instructions (umonitor, umwait, and tpause) are supported and use the instructions. The vDSO functions provides fast interface for user to check the support and use the instructions.

waitpkg_supported and its alias __vdso_waitpkg_supported check if user wait instructions (a.k.a. wait package feature) are supported

umonitor and its alias __vdso_umonitor provide user APIs for calling umonitor instruction.

umwait and its alias __vdso_umwait provide user APIs for calling umwait instruction.

tpause and its alias __vdso_tpause provide user APIs for calling tpause instruction.

nsec_to_tsc and its alias __vdso_nsec_to_tsc converts nanoseconds to TSC counter if TSC frequency is known. It will fail if TSC frequency is unknown.

The instructions can be implemented in intrinsic functions in future GCC. But the vDSO interfaces are available to user without the intrinsic functions support in GCC and the API waitpkg_supported and nsec_to_tsc cannot be implemented as GCC functions.

---
  arch/x86/entry/vdso/Makefile           |   2 +-
  arch/x86/entry/vdso/vdso.lds.S         |  10 ++
  arch/x86/entry/vdso/vma.c              |   9 ++
  arch/x86/entry/vdso/vuserwait.c        | 233
+++++++++++++++++++++++++++++++++
  arch/x86/include/asm/vdso_funcs_data.h |   3 +
  5 files changed, 256 insertions(+), 1 deletion(-)

create mode 100644 arch/x86/entry/vdso/vuserwait.c

arch/x86/entry/vdso/Makefile +1 -1
index af4fcae5de83..fb0062b09b3c 100644
@@ -17,7 +17,7 @@ VDSO32-$(CONFIG_X86_32)               := y
  VDSO32-$(CONFIG_IA32_EMULATION)       := y
    # files to link into the vdso
-vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o
+vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vdirectstore.o

vuserwait.o

    # files to link into kernel
  obj-y                         += vma.o
arch/x86/entry/vdso/vdso.lds.S +10 -0
index 097cdcda43a5..0942710608bf 100644
@@ -35,6 +35,16 @@ VERSION {
                __vdso_movdir64b_supported;
                movdir64b;
                __vdso_movdir64b;
+               waitpkg_supported;
+               __vdso_waitpkg_supported;
+               umonitor;
+               __vdso_umonitor;
+               umwait;
+               __vdso_umwait;
+               tpause;
+               __vdso_tpause;
+               nsec_to_tsc;
+               __vdso_nsec_to_tsc;
        local: *;
        };
  }
arch/x86/entry/vdso/vma.c +2 -0
index edbe5e63e5c2..006dfb5e5003 100644
@@ -372,10 +372,19 @@ static int vgetcpu_online(unsigned int cpu)
    static void __init init_vdso_funcs_data(void)
  {
+       struct system_counterval_t sys_counterval;
+
        if (static_cpu_has(X86_FEATURE_MOVDIRI))

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu) · line 386

Quoted 10 lines
arch/x86/entry/vdso/vma.c +7 -0
                vdso_funcs_data.movdiri_supported = true;
        if (static_cpu_has(X86_FEATURE_MOVDIR64B))
                vdso_funcs_data.movdir64b_supported = true;
+       if (static_cpu_has(X86_FEATURE_WAITPKG))
+               vdso_funcs_data.waitpkg_supported = true;
+       if (static_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) {
+               vdso_funcs_data.tsc_known_freq = true;
+               sys_counterval = convert_art_ns_to_tsc(1);
+               vdso_funcs_data.tsc_per_nsec = sys_counterval.cycles;
+       }

You're losing a ton of precision here. You might even be losing *all* of the precision and malfunctioning rather badly.

The correct way to do this is:

tsc_counts = ns * mul >> shift;

and the vclock code illustrates it. convert_art_ns_to_tsc() is a bad example because it uses an expensive division operation for no good reason except that no one bothered to optimize it.

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu)

Quoted 9 lines
arch/x86/entry/vdso/vma.c +9 -0
+notrace int __vdso_nsec_to_tsc(unsigned long nsec, unsigned long *tsc)
+{
+       if (!_vdso_funcs_data->tsc_known_freq)
+               return -ENODEV;
+
+       *tsc = _vdso_funcs_data->tsc_per_nsec * nsec;
+
+       return 0;
+}

Please don't expose this one at all. It would be nice for programs that use waitpkg to be migratable using CRIU-like tools, and this export actively harms any such effort. If you omit this function, then the kernel could learn to abort an in-progress __vdso_umwait if preempted (rseq-style) and CRIU would just work. It would be a bit of a hack, but it solves a real problem.

Quoted 1 line

+notrace int __vdso_umwait(int state, unsigned long nsec)

__vdso_umwait_relative(), please. Because some day (possibly soon) someone will want __vdso_umwait_absolute() and its friend __vdso_read_art_ns() so they can do:

u64 start = __vdso_read_art_ns(); __vdso_umonitor(...);
... do something potentially slow or that might fault ... __vdso_umwait_absolute(start + timeout);

Also, this patch appears to have a subtle but show-stopping race. Consider:

1. Task A does UMONITOR on CPU 1
2. Task A is preempted.
3. Task B does UMONITOR on CPU 1 at a different address
4. Task A resumes
5. Task A does UMWAIT

Quoted 5 lines

Now task A hangs, at least until the next external wakeup happens.

It's not entirely clear to me how you're supposed to fix this without some abomination that's so bad that it torpedoes the entire feature. Except that there is no chicken bit to turn this thing off. Sigh.

The UMWAIT mechanism also looks like it will work incorrectly under a VM. How do you (or, more generally, Intel) plan to handle that?

#15 lore raw

Re: [PATCH 6/7] x86/vdso: Add vDSO functions for user wait instructions

In reply to Andy Lutomirski
Quoted 11 lines · Andy Lutomirski
Quoted 10 lines · Fenghua Yu

static void __init init_vdso_funcs_data(void)
{

arch/x86/entry/vdso/vma.c +4 -0
+	struct system_counterval_t sys_counterval;
+
  	if (static_cpu_has(X86_FEATURE_MOVDIRI))
  		vdso_funcs_data.movdiri_supported = true;
  	if (static_cpu_has(X86_FEATURE_MOVDIR64B))
  		vdso_funcs_data.movdir64b_supported = true;
+	if (static_cpu_has(X86_FEATURE_WAITPKG))
+		vdso_funcs_data.waitpkg_supported = true;
Quoted 8 lines

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu) · line 387

Quoted 5 lines
arch/x86/entry/vdso/vma.c +5 -0
+	if (static_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) {
+		vdso_funcs_data.tsc_known_freq = true;
+		sys_counterval = convert_art_ns_to_tsc(1);
+		vdso_funcs_data.tsc_per_nsec = sys_counterval.cycles;
+	}

You're losing a ton of precision here. You might even be losing *all* of the precision and malfunctioning rather badly.

Indeed.

Quoted 5 lines

The correct way to do this is:

tsc_counts = ns * mul >> shift;

and the vclock code illustrates it.

Albeit you cannot use the TSC mult/shift pair as that is for the TSC to nsec conversion.

To get the proper mult/shift pair use clocks_calc_mult_shift(). Note that the scaled math has an upper limit when using 64 bit variables. You might need 128bit scaled math to make it work correctly.

Quoted 3 lines

convert_art_ns_to_tsc() is a bad example because it uses an expensive division operation for no good reason except that no one bothered to optimize it.

Right. It's not a hot path function and it does the job and we would need 128bit scaled math to avoid mult overflows.

Aside of that I have no idea why anyone would use convert_art_ns_to_tsc() for anything else than converting art to nsecs.

Quoted 7 lines
Quoted 1 line

+notrace int __vdso_umwait(int state, unsigned long nsec)

__vdso_umwait_relative(), please. Because some day (possibly soon) someone will want __vdso_umwait_absolute() and its friend __vdso_read_art_ns() so they can do:

u64 start = __vdso_read_art_ns();

Errm. No. You can't read ART. ART is only used by decives to which it is distributed. You can only read TSC here and convert that to nsecs.

Quoted 3 lines

__vdso_umonitor(...);
... do something potentially slow or that might fault ... __vdso_umwait_absolute(start + timeout);

That definitely requires 128bit scaled math to work correctly, unless you make the timeout relative before conversion.

But I really think we should avoid creating yet another interface to retrieve TSC time in nsecs. We have enough of these things already.

Ideally we'd use CLOCK_MONOTONIC here, but that needs more thought as:

1) TSC might be disabled as the timekeeping clocksource

2) The mult/shift pair for converting to nanoseconds is affected by NTP/PTP so it can be different from the initial mult/shift pair for converting nanoseconds to TSC.

A possible solution would be to use CLOCK_MOTONIC_RAW which is not affected by NTP/PTP adjustments. But that still has the issue of TSC not being the timekeeping clocksource. Bah, the whole TSC deadline mode sucks. I have no idea what's wrong with simple down counters. They Just Work.

Quoted 1 line

Also, this patch appears to have a subtle but show-stopping race. Consider:

It's not the patch which has this issue. It's the hardware ....

Quoted 11 lines

1. Task A does UMONITOR on CPU 1
2. Task A is preempted.
3. Task B does UMONITOR on CPU 1 at a different address
4. Task A resumes
5. Task A does UMWAIT

Now task A hangs, at least until the next external wakeup happens.

It's not entirely clear to me how you're supposed to fix this without some abomination that's so bad that it torpedoes the entire feature. Except that there is no chicken bit to turn this thing off. Sigh.

That sounds similar to the ARM monitor which is used for implementing cmpxchg. They had to sprinkle monitor aborts all over the place....

So yes, unless there is undocumented magic which aborts the monitor under certain circumstances, this thing is broken beyond repair.

Thanks,

tglx

maintainer
#16 lore raw

Re: [PATCH 6/7] x86/vdso: Add vDSO functions for user wait instructions

Quoted 48 lines · Thomas Gleixner
Quoted 17 lines · Andy Lutomirski
Quoted 5 lines · Fenghua Yu

static void __init init_vdso_funcs_data(void)
{

arch/x86/entry/vdso/vma.c +2 -0
+   struct system_counterval_t sys_counterval;
+
    if (static_cpu_has(X86_FEATURE_MOVDIRI))

arch/x86/entry/vdso/vma.c static int vgetcpu_online(unsigned int cpu) · line 387

Quoted 10 lines
arch/x86/entry/vdso/vma.c +7 -0
            vdso_funcs_data.movdiri_supported = true;
    if (static_cpu_has(X86_FEATURE_MOVDIR64B))
            vdso_funcs_data.movdir64b_supported = true;
+   if (static_cpu_has(X86_FEATURE_WAITPKG))
+           vdso_funcs_data.waitpkg_supported = true;
+   if (static_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) {
+           vdso_funcs_data.tsc_known_freq = true;
+           sys_counterval = convert_art_ns_to_tsc(1);
+           vdso_funcs_data.tsc_per_nsec = sys_counterval.cycles;
+   }
Quoted 2 lines

You're losing a ton of precision here. You might even be losing *all* of the precision and malfunctioning rather badly.

Indeed.

Quoted 5 lines

The correct way to do this is:

tsc_counts = ns * mul >> shift;

and the vclock code illustrates it.

Albeit you cannot use the TSC mult/shift pair as that is for the TSC to nsec conversion.

To get the proper mult/shift pair use clocks_calc_mult_shift(). Note that the scaled math has an upper limit when using 64 bit variables. You might need 128bit scaled math to make it work correctly.

Quoted 3 lines

convert_art_ns_to_tsc() is a bad example because it uses an expensive division operation for no good reason except that no one bothered to optimize it.

Right. It's not a hot path function and it does the job and we would need 128bit scaled math to avoid mult overflows.

Aside of that I have no idea why anyone would use convert_art_ns_to_tsc() for anything else than converting art to nsecs.

Quoted 2 lines
Quoted 1 line

+notrace int __vdso_umwait(int state, unsigned long nsec)

Quoted 8 lines
Quoted 5 lines

__vdso_umwait_relative(), please. Because some day (possibly soon) someone will want __vdso_umwait_absolute() and its friend __vdso_read_art_ns() so they can do:

u64 start = __vdso_read_art_ns();

Errm. No. You can't read ART. ART is only used by decives to which it is distributed. You can only read TSC here and convert that to nsecs.

Bah.

But my point remains -- I think that the user (non-vDSO) code should think in nanoseconds, not TSC ticks. That we have have a much better chance of getting migration right.

Quoted 15 lines
Quoted 4 lines

__vdso_umonitor(...);
... do something potentially slow or that might fault ... __vdso_umwait_absolute(start + timeout);

That definitely requires 128bit scaled math to work correctly, unless you make the timeout relative before conversion.

But I really think we should avoid creating yet another interface to retrieve TSC time in nsecs. We have enough of these things already.

Ideally we'd use CLOCK_MONOTONIC here, but that needs more thought as:

1) TSC might be disabled as the timekeeping clocksource

Quoted 8 lines

2) The mult/shift pair for converting to nanoseconds is affected by NTP/PTP so it can be different from the initial mult/shift pair for converting nanoseconds to TSC.

A possible solution would be to use CLOCK_MOTONIC_RAW which is not affected by NTP/PTP adjustments. But that still has the issue of TSC not being the timekeeping clocksource. Bah, the whole TSC deadline mode sucks. I have no idea what's wrong with simple down counters. They Just Work.

I think it's not totally crazy to declare UMWAIT on a system with a non-TSC clocksource to be unsupported.

#17 lore raw

Re: [PATCH 6/7] x86/vdso: Add vDSO functions for user wait instructions

Quoted 11 lines · Andy Lutomirski
Quoted 4 lines · Thomas Gleixner
Quoted 1 line

u64 start = __vdso_read_art_ns();

Errm. No. You can't read ART. ART is only used by decives to which it is distributed. You can only read TSC here and convert that to nsecs.

Bah.

But my point remains -- I think that the user (non-vDSO) code should think in nanoseconds, not TSC ticks. That we have have a much better chance of getting migration right.

Agreed. And we should not create new interfaces for that. We already have clock_gettime() which is the right thing to use.

Quoted 7 lines
Quoted 4 lines

A possible solution would be to use CLOCK_MOTONIC_RAW which is not affected by NTP/PTP adjustments. But that still has the issue of TSC not being the timekeeping clocksource. Bah, the whole TSC deadline mode sucks. I have no idea what's wrong with simple down counters. They Just Work.

I think it's not totally crazy to declare UMWAIT on a system with a non-TSC clocksource to be unsupported.

Sure, the information is already available in the VDSO gtod data.

Thanks,

tglx

author
#18 lore raw

PATCH 7/7 selftests/vDSO: Add selftest to test vDSO functions for direct store and user wait instructions

In reply to Fenghua Yu

The selftest tool tests the vDSO functions for calling the instructions including movdiri32, movdiri64, movdir64b, umonitor, umwait, tpause, and their support checking.

Limited by testing environment, the selftest doesn't contain some complex tests e.g. wake up process by writing the monitor address. After testing environment is ready, the tests will be added.

---
 tools/testing/selftests/vDSO/Makefile             |   4 +-
 tools/testing/selftests/vDSO/vdso_inst_test_x86.c | 405 ++++++++++++++++++++++
 2 files changed, 408 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/vDSO/vdso_inst_test_x86.c
tools/testing/selftests/vDSO/Makefile +3 -1
index f5d7a7851e21..d83228714fbd 100644
@@ -8,7 +8,7 @@ ifeq ($(CONFIG_X86_32),y)
 LDLIBS += -lgcc_s
 endif
 
-TEST_PROGS := $(OUTPUT)/vdso_test $(OUTPUT)/vdso_standalone_test_x86
+TEST_PROGS := $(OUTPUT)/vdso_test $(OUTPUT)/vdso_standalone_test_x86 $(OUTPUT)/vdso_inst_test_x86
 
 all: $(TEST_PROGS)
 $(OUTPUT)/vdso_test: parse_vdso.c vdso_test.c
@@ -17,5 +17,7 @@ $(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
 		vdso_standalone_test_x86.c parse_vdso.c \
 		-o $@
 
+$(OUTPUT)/vdso_inst_test_x86: parse_vdso.c vdso_inst_test_x86.c
+
 EXTRA_CLEAN := $(TEST_PROGS)
 endif
tools/testing/selftests/vDSO/vdso_inst_test_x86.c new file +405 -0

405 lines not on this page · raw message

Signature · 1 line
2.5.0