Skip to contentSkip to conversation

s390: convert to GENERIC_VDSO

RFC PATCH Superseded Sven Schnelle 3 participants 12 messages Aug 3, 2020 – Aug 4, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Sven Schnelle

author reviewer
#0 lore raw

RFC PATCH 1/1 s390: convert to GENERIC_VDSO

these two patches convert the s390 architecture to generic VDSO. The first patch adds an option to add architecture specific information to struct vdso_data. We need that information because the old s390 assembly code had a steering capability, which steered the clock slowly. To emulate that behaviour we need to add the steering offset to struct vdso_data.

This requirements results in the need for a seqlock kind of lock, which is implemented open-coded in __arch_get_hw_counter(). open-coded because we cannot include seqlock.h in userspace code (and using the normal seqlock interface on kernel side might result in people changing struct seqlock, but not changing the vdso userspace part), therefore both sides are open-coded. I think in theory we could also call vdso_write_begin()/ vdso_write_end(). What do you think?

If there are no objections we would carry both patches through the s390 tree.

Thanks
Sven

author reviewer
#1 lore raw

PATCH 1/2 vdso: allow to add architecture-specific vdso data

Add the possibility to add architecture specific vDSO
data to struct vdso_data. This is useful if the arch specific user space VDSO code needs additional data during execution. If CONFIG_ARCH_HAS_VDSO_DATA is defined, the generic code will include asm/vdso/data.h which should contain 'struct arch_vdso_data'. This structure will be embedded in the generic vDSO data.

---
 arch/Kconfig            | 3 +++
 include/vdso/datapage.h | 7 +++++++
 2 files changed, 10 insertions(+)
arch/Kconfig +3 -0
index 8cc35dc556c7..e1017ce979e2 100644
@@ -979,6 +979,9 @@ config HAVE_SPARSE_SYSCALL_NR
 	  entries at 4000, 5000 and 6000 locations. This option turns on syscall
 	  related optimizations for a given architecture.
 
+config ARCH_HAS_VDSO_DATA
+	bool
+
 source "kernel/gcov/Kconfig"
 
 source "scripts/gcc-plugins/Kconfig"
include/vdso/datapage.h +7 -0
index 7955c56d6b3c..74e730238ce6 100644
@@ -19,6 +19,10 @@
 #include <vdso/time32.h>
 #include <vdso/time64.h>
 
+#ifdef CONFIG_ARCH_HAS_VDSO_DATA
+#include <asm/vdso/data.h>
+#endif
+
 #define VDSO_BASES	(CLOCK_TAI + 1)
 #define VDSO_HRES	(BIT(CLOCK_REALTIME)		| \
 			 BIT(CLOCK_MONOTONIC)		| \
@@ -97,6 +101,9 @@ struct vdso_data {
 	s32			tz_dsttime;
 	u32			hrtimer_res;
 	u32			__unused;
+#ifdef CONFIG_ARCH_HAS_VDSO_DATA
+	struct arch_vdso_data arch;
+#endif
 };
 
 /*
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/2] vdso: allow to add architecture-specific vdso data

Sven,

Quoted 17 lines · Sven Schnelle

source "scripts/gcc-plugins/Kconfig"

include/vdso/datapage.h +4 -0
index 7955c56d6b3c..74e730238ce6 100644
@@ -19,6 +19,10 @@
 #include <vdso/time32.h>
 #include <vdso/time64.h>
 
+#ifdef CONFIG_ARCH_HAS_VDSO_DATA
+#include <asm/vdso/data.h>
+#endif
+
 #define VDSO_BASES	(CLOCK_TAI + 1)
 #define VDSO_HRES	(BIT(CLOCK_REALTIME)		| \
 			 BIT(CLOCK_MONOTONIC)		| \

include/vdso/datapage.h struct vdso_data { · line 106

Quoted 6 lines
include/vdso/datapage.h +3 -0
@@ -97,6 +101,9 @@ struct vdso_data {
 	s32			tz_dsttime;
 	u32			hrtimer_res;
 	u32			__unused;
+#ifdef CONFIG_ARCH_HAS_VDSO_DATA
+	struct arch_vdso_data arch;
+#endif

just a few nits.

Can you please spare that #ifdef and do:

#ifdef CONFIG_ARCH_HAS_VDSO_DATA
#include <asm/vdso/data.h>
#else
struct arch_vdso_data {};
#endif

Please keep the tabular alignment of the struct members and add kernel doc in the comment above the struct.

Aside of that 'arch' is not really a intuitive name. 'arch_data' or something like that. Hmm?

Thanks,

tglx

author reviewer
#3 lore raw

Re: [PATCH 1/2] vdso: allow to add architecture-specific vdso data

Quoted 9 lines · Thomas Gleixner

just a few nits.

Can you please spare that #ifdef and do:

#ifdef CONFIG_ARCH_HAS_VDSO_DATA
#include <asm/vdso/data.h>
#else
struct arch_vdso_data {};
#endif

Ok.

Quoted 2 lines

Please keep the tabular alignment of the struct members and add kernel doc in the comment above the struct.

Ok.

Quoted 2 lines

Aside of that 'arch' is not really a intuitive name. 'arch_data' or something like that. Hmm?

arch_data is fine, thank you for your comments.

Regards
Sven

author reviewer
#4 lore raw

PATCH 2/2 s390: convert to GENERIC_VDSO

In reply to Sven Schnelle

This patch converts s390 to the generic vDSO. There are a few special things on s390:

- vDSO can be called without a stack frame - glibc did this in the past. So we need to allocate a stackframe on our own.

- The former assembly code used stcke to get the TOD clock and applied time steering to it. We need to do the same in the new code. This is done in the architecture specific __arch_get_hw_counter function. The steering information is stored in an architecure specific area in the vDSO data.

- CPUCLOCK_VIRT is now handled with a syscall fallback, which might be slower/less accurate than the old implementation.

The getcpu() function stays as an assembly function because there is no generic implementation and the code is just a few lines.

Performance number from my system do 100 mio gettimeofday() calls:

Plain syscall: 8.6s
Generic VDSO:  1.3s
old ASM VDSO:  1s

So it's a bit slower but still much faster than syscalls.

---
 arch/s390/Kconfig                           |   3 +
 arch/s390/include/asm/clocksource.h         |   7 +
 arch/s390/include/asm/vdso.h                |  25 +--
 arch/s390/include/asm/vdso/clocksource.h    |   8 +
 arch/s390/include/asm/vdso/data.h           |  14 ++
 arch/s390/include/asm/vdso/gettimeofday.h   |  78 ++++++++++
 arch/s390/include/asm/vdso/processor.h      |   5 +
 arch/s390/include/asm/vdso/vdso.h           |   0
 arch/s390/include/asm/vdso/vsyscall.h       |  26 ++++
 arch/s390/kernel/asm-offsets.c              |  20 ---
 arch/s390/kernel/entry.S                    |   6 -
 arch/s390/kernel/setup.c                    |   1 -
 arch/s390/kernel/time.c                     |  70 ++-------
 arch/s390/kernel/vdso.c                     |  29 +---
 arch/s390/kernel/vdso64/Makefile            |  19 ++-
 arch/s390/kernel/vdso64/clock_getres.S      |  50 ------
 arch/s390/kernel/vdso64/clock_gettime.S     | 163 --------------------
 arch/s390/kernel/vdso64/gettimeofday.S      |  71 ---------
 arch/s390/kernel/vdso64/vdso64_generic.c    |  18 +++
 arch/s390/kernel/vdso64/vdso_user_wrapper.S |  38 +++++
 20 files changed, 232 insertions(+), 419 deletions(-)
 create mode 100644 arch/s390/include/asm/clocksource.h
 create mode 100644 arch/s390/include/asm/vdso/clocksource.h
 create mode 100644 arch/s390/include/asm/vdso/data.h
 create mode 100644 arch/s390/include/asm/vdso/gettimeofday.h
 create mode 100644 arch/s390/include/asm/vdso/processor.h
 create mode 100644 arch/s390/include/asm/vdso/vdso.h
 create mode 100644 arch/s390/include/asm/vdso/vsyscall.h
 delete mode 100644 arch/s390/kernel/vdso64/clock_getres.S
 delete mode 100644 arch/s390/kernel/vdso64/clock_gettime.S
 delete mode 100644 arch/s390/kernel/vdso64/gettimeofday.S
 create mode 100644 arch/s390/kernel/vdso64/vdso64_generic.c
 create mode 100644 arch/s390/kernel/vdso64/vdso_user_wrapper.S
arch/s390/Kconfig +3 -0
index c7d7ede6300c..eb50f748e151 100644
@@ -73,6 +73,7 @@ config S390
 	select ARCH_HAS_STRICT_MODULE_RWX
 	select ARCH_HAS_SYSCALL_WRAPPER
 	select ARCH_HAS_UBSAN_SANITIZE_ALL
+	select ARCH_HAS_VDSO_DATA
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 	select ARCH_INLINE_READ_LOCK
 	select ARCH_INLINE_READ_LOCK_BH
@@ -118,6 +119,7 @@ config S390
 	select GENERIC_CPU_AUTOPROBE
 	select GENERIC_CPU_VULNERABILITIES
 	select GENERIC_FIND_FIRST_BIT
+	select GENERIC_GETTIMEOFDAY
 	select GENERIC_SMP_IDLE_THREAD
 	select GENERIC_TIME_VSYSCALL
 	select HAVE_ALIGNED_STRUCT_PAGE if SLUB
@@ -149,6 +151,7 @@ config S390
 	select HAVE_FUNCTION_TRACER
 	select HAVE_FUTEX_CMPXCHG if FUTEX
 	select HAVE_GCC_PLUGINS
+	select HAVE_GENERIC_VDSO
 	select HAVE_KERNEL_BZIP2
 	select HAVE_KERNEL_GZIP
 	select HAVE_KERNEL_LZ4
arch/s390/include/asm/clocksource.h new file +7 -0
new file mode 100644
index 000000000000..03434369fce4
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* s390-specific clocksource additions */
+
+#ifndef _ASM_S390_CLOCKSOURCE_H
+#define _ASM_S390_CLOCKSOURCE_H
+
+#endif /* _ASM_S390_CLOCKSOURCE_H */
arch/s390/include/asm/vdso.h +2 -23
index 0cd085cdeb4f..82f86b3c394b 100644
@@ -2,6 +2,8 @@
 #ifndef __S390_VDSO_H__
 #define __S390_VDSO_H__
 
+#include <vdso/datapage.h>
+
 /* Default link addresses for the vDSOs */
 #define VDSO32_LBASE	0
 #define VDSO64_LBASE	0
@@ -18,30 +20,7 @@
  * itself and may change without notice.
  */
 
-struct vdso_data {
-	__u64 tb_update_count;		/* Timebase atomicity ctr	0x00 */
-	__u64 xtime_tod_stamp;		/* TOD clock for xtime		0x08 */
-	__u64 xtime_clock_sec;		/* Kernel time			0x10 */
-	__u64 xtime_clock_nsec;		/*				0x18 */
-	__u64 xtime_coarse_sec;		/* Coarse kernel time		0x20 */
-	__u64 xtime_coarse_nsec;	/*				0x28 */
-	__u64 wtom_clock_sec;		/* Wall to monotonic clock	0x30 */
-	__u64 wtom_clock_nsec;		/*				0x38 */
-	__u64 wtom_coarse_sec;		/* Coarse wall to monotonic	0x40 */
-	__u64 wtom_coarse_nsec;		/*				0x48 */
-	__u32 tz_minuteswest;		/* Minutes west of Greenwich	0x50 */
-	__u32 tz_dsttime;		/* Type of dst correction	0x54 */
-	__u32 ectg_available;		/* ECTG instruction present	0x58 */
-	__u32 tk_mult;			/* Mult. used for xtime_nsec	0x5c */
-	__u32 tk_shift;			/* Shift used for xtime_nsec	0x60 */
-	__u32 ts_dir;			/* TOD steering direction	0x64 */
-	__u64 ts_end;			/* TOD steering end		0x68 */
-	__u32 hrtimer_res;		/* hrtimer resolution		0x70 */
-};
-
 struct vdso_per_cpu_data {
-	__u64 ectg_timer_base;
-	__u64 ectg_user_time;
 	/*
 	 * Note: node_id and cpu_nr must be at adjacent memory locations.
 	 * VDSO userspace must read both values with a single instruction.
arch/s390/include/asm/vdso/clocksource.h new file +8 -0
new file mode 100644
index 000000000000..a93eda0ce7bb
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_VDSO_CLOCKSOURCE_H
+#define __ASM_VDSO_CLOCKSOURCE_H
+
+#define VDSO_ARCH_CLOCKMODES	\
+	VDSO_CLOCKMODE_TOD
+
+#endif /* __ASM_VDSO_CLOCKSOURCE_H */
arch/s390/include/asm/vdso/data.h new file +14 -0
new file mode 100644
index 000000000000..c0af12ed891c
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __S390_ASM_VDSO_DATA_H
+#define __S390_ASM_VDSO_DATA_H
+
+#include <linux/types.h>
+#include <vdso/datapage.h>
+
+struct arch_vdso_data {
+	__u64 tod_steering_delta;
+	__u64 tod_steering_end;
+	__u32 tb_update_cnt;
+};
+
+#endif /* __S390_ASM_VDSO_DATA_H */
arch/s390/include/asm/vdso/gettimeofday.h new file +78 -0

78 lines not on this page · raw message

arch/s390/include/asm/vdso/processor.h new file +5 -0
new file mode 100644
index 000000000000..c47deb472e2a
@@ -0,0 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ASM_VDSO_PROCESSOR_H
+#define __ASM_VDSO_PROCESSOR_H
+
+#endif /* __ASM_VDSO_PROCESSOR_H */
arch/s390/include/asm/vdso/vdso.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
arch/s390/include/asm/vdso/vsyscall.h new file +26 -0
new file mode 100644
index 000000000000..6c67c08cefdd
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_VDSO_VSYSCALL_H
+#define __ASM_VDSO_VSYSCALL_H
+
+#ifndef __ASSEMBLY__
+
+#include <linux/hrtimer.h>
+#include <linux/timekeeper_internal.h>
+#include <vdso/datapage.h>
+#include <asm/vdso.h>
+/*
+ * Update the vDSO data page to keep in sync with kernel timekeeping.
+ */
+
+static __always_inline struct vdso_data *__s390_get_k_vdso_data(void)
+{
+	return vdso_data;
+}
+#define __arch_get_k_vdso_data __s390_get_k_vdso_data
+
+/* The asm-generic header needs to be included after the definitions above */
+#include <asm-generic/vdso/vsyscall.h>
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* __ASM_VDSO_VSYSCALL_H */
arch/s390/kernel/asm-offsets.c +0 -20
index 5d8cc1864566..ece58f2217cb 100644
@@ -59,26 +59,6 @@ int main(void)
 	OFFSET(__SF_SIE_REASON, stack_frame, empty1[2]);
 	OFFSET(__SF_SIE_FLAGS, stack_frame, empty1[3]);
 	BLANK();
-	/* timeval/timezone offsets for use by vdso */
-	OFFSET(__VDSO_UPD_COUNT, vdso_data, tb_update_count);
-	OFFSET(__VDSO_XTIME_STAMP, vdso_data, xtime_tod_stamp);
-	OFFSET(__VDSO_XTIME_SEC, vdso_data, xtime_clock_sec);
-	OFFSET(__VDSO_XTIME_NSEC, vdso_data, xtime_clock_nsec);
-	OFFSET(__VDSO_XTIME_CRS_SEC, vdso_data, xtime_coarse_sec);
-	OFFSET(__VDSO_XTIME_CRS_NSEC, vdso_data, xtime_coarse_nsec);
-	OFFSET(__VDSO_WTOM_SEC, vdso_data, wtom_clock_sec);
-	OFFSET(__VDSO_WTOM_NSEC, vdso_data, wtom_clock_nsec);
-	OFFSET(__VDSO_WTOM_CRS_SEC, vdso_data, wtom_coarse_sec);
-	OFFSET(__VDSO_WTOM_CRS_NSEC, vdso_data, wtom_coarse_nsec);
-	OFFSET(__VDSO_TIMEZONE, vdso_data, tz_minuteswest);
-	OFFSET(__VDSO_ECTG_OK, vdso_data, ectg_available);
-	OFFSET(__VDSO_TK_MULT, vdso_data, tk_mult);
-	OFFSET(__VDSO_TK_SHIFT, vdso_data, tk_shift);
-	OFFSET(__VDSO_TS_DIR, vdso_data, ts_dir);
-	OFFSET(__VDSO_TS_END, vdso_data, ts_end);
-	OFFSET(__VDSO_CLOCK_REALTIME_RES, vdso_data, hrtimer_res);
-	OFFSET(__VDSO_ECTG_BASE, vdso_per_cpu_data, ectg_timer_base);
-	OFFSET(__VDSO_ECTG_USER, vdso_per_cpu_data, ectg_user_time);
 	OFFSET(__VDSO_GETCPU_VAL, vdso_per_cpu_data, getcpu_val);
 	BLANK();
 	/* constants used by the vdso */
arch/s390/kernel/entry.S +0 -6
index 969b35b177dd..5de00e1f8830 100644
@@ -435,10 +435,8 @@ ENTRY(system_call)
 	jz	.Lsysc_skip_fpu
 	brasl	%r14,load_fpu_regs
 .Lsysc_skip_fpu:
-	lg	%r14,__LC_VDSO_PER_CPU
 	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r11)
 	stpt	__LC_EXIT_TIMER
-	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
 	lmg	%r0,%r15,__PT_R0(%r11)
 	b	__LC_RETURN_LPSWE
 
@@ -797,13 +795,11 @@ ENTRY(io_int_handler)
 	TRACE_IRQS_ON
 0:
 #endif
-	lg	%r14,__LC_VDSO_PER_CPU
 	mvc	__LC_RETURN_PSW(16),__PT_PSW(%r11)
 	tm	__PT_PSW+1(%r11),0x01	# returning to user ?
 	jno	.Lio_exit_kernel
 	BPEXIT	__TI_flags(%r12),_TIF_ISOLATE_BP
 	stpt	__LC_EXIT_TIMER
-	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
 .Lio_exit_kernel:
 	lmg	%r0,%r15,__PT_R0(%r11)
 	b	__LC_RETURN_LPSWE
@@ -1213,14 +1209,12 @@ ENTRY(mcck_int_handler)
 	brasl	%r14,s390_handle_mcck
 	TRACE_IRQS_ON
 .Lmcck_return:
-	lg	%r14,__LC_VDSO_PER_CPU
 	lmg	%r0,%r10,__PT_R0(%r11)
 	mvc	__LC_RETURN_MCCK_PSW(16),__PT_PSW(%r11) # move return PSW
 	tm	__LC_RETURN_MCCK_PSW+1,0x01 # returning to user ?
 	jno	0f
 	BPEXIT	__TI_flags(%r12),_TIF_ISOLATE_BP
 	stpt	__LC_EXIT_TIMER
-	mvc	__VDSO_ECTG_BASE(16,%r14),__LC_EXIT_TIMER
 0:	lmg	%r11,%r15,__PT_R11(%r11)
 	b	__LC_RETURN_MCCK_LPSWE
 
arch/s390/kernel/setup.c +0 -1
index 07aa15ba43b3..0a5cb423aae2 100644
@@ -402,7 +402,6 @@ static void __init setup_lowcore_dat_off(void)
 	memcpy(lc->alt_stfle_fac_list, S390_lowcore.alt_stfle_fac_list,
 	       sizeof(lc->alt_stfle_fac_list));
 	nmi_alloc_boot_cpu(lc);
-	vdso_alloc_boot_cpu(lc);
 	lc->sync_enter_timer = S390_lowcore.sync_enter_timer;
 	lc->async_enter_timer = S390_lowcore.async_enter_timer;
 	lc->exit_timer = S390_lowcore.exit_timer;
arch/s390/kernel/time.c +15 -55

118 lines not on this page · raw message

arch/s390/kernel/vdso.c +3 -26
index c4baefaa6e34..f9da5b149141 100644
@@ -20,6 +20,8 @@
 #include <linux/security.h>
 #include <linux/memblock.h>
 #include <linux/compat.h>
+#include <linux/binfmts.h>
+#include <vdso/datapage.h>
 #include <asm/asm-offsets.h>
 #include <asm/processor.h>
 #include <asm/mmu.h>
@@ -96,35 +98,12 @@ static union {
 	struct vdso_data	data;
 	u8			page[PAGE_SIZE];
 } vdso_data_store __page_aligned_data;
-struct vdso_data *vdso_data = &vdso_data_store.data;
-
-/*
- * Setup vdso data page.
- */
-static void __init vdso_init_data(struct vdso_data *vd)
-{
-	vd->ectg_available = test_facility(31);
-}
-
+struct vdso_data *vdso_data = (struct vdso_data *)&vdso_data_store.data;
 /*
  * Allocate/free per cpu vdso data.
  */
 #define SEGMENT_ORDER	2
 
-/*
- * The initial vdso_data structure for the boot CPU. Eventually
- * it is replaced with a properly allocated structure in vdso_init.
- * This is necessary because a valid S390_lowcore.vdso_per_cpu_data
- * pointer is required to be able to return from an interrupt or
- * program check. See the exit paths in entry.S.
- */
-struct vdso_data boot_vdso_data __initdata;
-
-void __init vdso_alloc_boot_cpu(struct lowcore *lowcore)
-{
-	lowcore->vdso_per_cpu_data = (unsigned long) &boot_vdso_data;
-}
-
 int vdso_alloc_per_cpu(struct lowcore *lowcore)
 {
 	unsigned long segment_table, page_table, page_frame;
@@ -246,8 +225,6 @@ static int __init vdso_init(void)
 {
 	int i;
 
-	vdso_init_data(vdso_data);
-
 	/* Calculate the size of the 64 bit vDSO */
 	vdso64_pages = ((&vdso64_end - &vdso64_start
 			 + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
arch/s390/kernel/vdso64/Makefile +15 -4
index 4a66a1cb919b..f1c0570780d1 100644
@@ -1,14 +1,20 @@
 # SPDX-License-Identifier: GPL-2.0
-# List of files in the vdso, has to be asm only for now
+# List of files in the vdso
 
 KCOV_INSTRUMENT := n
+ARCH_REL_TYPE_ABS := R_390_COPY|R_390_GLOB_DAT|R_390_JMP_SLOT|R_390_RELATIVE
+ARCH_REL_TYPE_ABS += R_390_GOT|R_390_PLT
 
-obj-vdso64 = gettimeofday.o clock_getres.o clock_gettime.o note.o getcpu.o
+include $(srctree)/lib/vdso/Makefile
+obj-vdso64 = vdso_user_wrapper.o note.o getcpu.o
+obj-cvdso64 = vdso64_generic.o
+CFLAGS_REMOVE_vdso64_generic.o = -pg $(CC_FLAGS_FTRACE) $(CC_FLAGS_EXPOLINE)
 
 # Build rules
 
-targets := $(obj-vdso64) vdso64.so vdso64.so.dbg
+targets := $(obj-vdso64) $(obj-cvdso64) vdso64.so vdso64.so.dbg
 obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64))
+obj-cvdso64 := $(addprefix $(obj)/, $(obj-cvdso64))
 
 KBUILD_AFLAGS += -DBUILD_VDSO
 KBUILD_CFLAGS += -DBUILD_VDSO
@@ -37,7 +43,7 @@ KASAN_SANITIZE := n
 $(obj)/vdso64_wrapper.o : $(obj)/vdso64.so
 
 # link rule for the .so file, .lds has to be first
-$(obj)/vdso64.so.dbg: $(obj)/vdso64.lds $(obj-vdso64) FORCE
+$(obj)/vdso64.so.dbg: $(src)/vdso64.lds $(obj-vdso64) $(obj-cvdso64) FORCE
 	$(call if_changed,ld)
 
 # strip rule for the .so file
@@ -49,9 +55,14 @@ $(obj)/%.so: $(obj)/%.so.dbg FORCE
 $(obj-vdso64): %.o: %.S FORCE
 	$(call if_changed_dep,vdso64as)
 
+$(obj-cvdso64): %.o: %.c FORCE
+	$(call if_changed_dep,vdso64cc)
+
 # actual build commands
 quiet_cmd_vdso64as = VDSO64A $@
       cmd_vdso64as = $(CC) $(a_flags) -c -o $@ $<
+quiet_cmd_vdso64cc = VDSO64C $@
+      cmd_vdso64cc = $(CC) $(c_flags) -c -o $@ $<
 
 # install commands for the unstripped file
 quiet_cmd_vdso_install = INSTALL $@
arch/s390/kernel/vdso64/clock_getres.S deleted +0 -50

50 lines not on this page · raw message

arch/s390/kernel/vdso64/clock_gettime.S deleted +0 -163

163 lines not on this page · raw message

arch/s390/kernel/vdso64/gettimeofday.S deleted +0 -71

71 lines not on this page · raw message

arch/s390/kernel/vdso64/vdso64_generic.c new file +18 -0
new file mode 100644
index 000000000000..a8cef7e4d137
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../../../lib/vdso/gettimeofday.c"
+
+int __s390_vdso_gettimeofday(struct __kernel_old_timeval *tv,
+			     struct timezone *tz)
+{
+	return __cvdso_gettimeofday(tv, tz);
+}
+
+int __s390_vdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
+{
+	return __cvdso_clock_gettime(clock, ts);
+}
+
+int __s390_vdso_clock_getres(clockid_t clock, struct __kernel_timespec *ts)
+{
+	return __cvdso_clock_getres(clock, ts);
+}
arch/s390/kernel/vdso64/vdso_user_wrapper.S new file +38 -0
new file mode 100644
index 000000000000..a775d7e52872
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <asm/vdso.h>
+#include <asm/unistd.h>
+#include <asm/asm-offsets.h>
+#include <asm/dwarf.h>
+#include <asm/ptrace.h>
+
+#define WRAPPER_FRAME_SIZE (STACK_FRAME_OVERHEAD+8)
+
+/*
+ * Older glibc version called vdso without allocating a stackframe. This wrapper
+ * is just used to allocate a stackframe. See
+ * https://sourceware.org/git/?p=glibc.git;a=commit;h=478593e6374f3818da39332260dc453cb19cfa1e
+ * for details.
+ */
+.macro vdso_func func
+	.globl __kernel_\func
+	.type  __kernel_\func,@function
+	.align 8
+__kernel_\func:
+	CFI_STARTPROC
+	aghi	%r15,-WRAPPER_FRAME_SIZE
+	CFI_DEF_CFA_OFFSET (STACK_FRAME_OVERHEAD + WRAPPER_FRAME_SIZE)
+	CFI_VAL_OFFSET 15, -STACK_FRAME_OVERHEAD
+	stg	%r14,STACK_FRAME_OVERHEAD(%r15)
+	brasl	%r14,__s390_vdso_\func
+	lg	%r14,STACK_FRAME_OVERHEAD(%r15)
+	aghi	%r15,WRAPPER_FRAME_SIZE
+	CFI_DEF_CFA_OFFSET STACK_FRAME_OVERHEAD
+	CFI_RESTORE 15
+	br	%r14
+	CFI_ENDPROC
+	.size	__kernel_\func,.-__kernel_\func
+.endm
+
+vdso_func gettimeofday
+vdso_func clock_getres
+vdso_func clock_gettime
Signature · 1 line
2.17.1
#5 lore raw
Quoted 2 lines · Sven Schnelle

- CPUCLOCK_VIRT is now handled with a syscall fallback, which might be slower/less accurate than the old implementation.

I can understand the slower, but why does it become less accurate?

Quoted 7 lines

Performance number from my system do 100 mio gettimeofday() calls:

Plain syscall: 8.6s
Generic VDSO:  1.3s
old ASM VDSO:  1s

So it's a bit slower but still much faster than syscalls.

Where is the overhead coming from?

arch/s390/include/asm/vdso/gettimeofday.h line 34

Quoted 10 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +10 -0
+static inline u64 __arch_get_hw_counter(s32 clock_mode)
+{
+	const struct vdso_data *vdso = __arch_get_vdso_data();
+	u64 adj, now;
+	int cnt;
+
+	do {
+		do {
+			cnt = READ_ONCE(vdso->arch.tb_update_cnt);
+		} while (cnt & 1);
                smp_rmb() ?

arch/s390/include/asm/vdso/gettimeofday.h line 39

Quoted 4 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +4 -0
+		now = get_tod_clock();
+		adj = vdso->arch.tod_steering_end - now;
+		if (unlikely((s64) adj > 0))
+			now += (vdso->arch.tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
                smp_rmb() ?
Quoted 5 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +2 -0
+	} while (cnt != READ_ONCE(vdso->arch.tb_update_cnt));
+	return now;
 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
 		lpar_offset = qto.tod_epoch_difference;

arch/s390/include/asm/vdso/gettimeofday.h static int stp_sync_clock(void *data) · line 559

Quoted 10 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +7 -0
@@ -599,6 +550,13 @@ static int stp_sync_clock(void *data)
 		if (stp_info.todoff[0] || stp_info.todoff[1] ||
 		    stp_info.todoff[2] || stp_info.todoff[3] ||
 		    stp_info.tmd != 2) {
+			vdso_data->arch.tb_update_cnt++;
+			/*
+			 * This barrier isn't really needed as we're called
+			 * from stop_machine_cpuslocked(). However it doesn't
+			 * hurt in case the code gets changed.
+			 */
+			smp_wmb();

WMB without a corresponding RMB and an explanation what's ordered against what is voodoo at best.

arch/s390/kernel/time.c static int stp_sync_clock(void *data) · line 570

Quoted 8 lines
arch/s390/kernel/time.c +1 -0
 			rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
 					&clock_delta);
 			if (rc == 0) {
@@ -609,6 +567,8 @@ static int stp_sync_clock(void *data)
 				if (rc == 0 && stp_info.tmd != 2)
 					rc = -EAGAIN;
 			}
+			smp_wmb(); /* see comment above */

See my comments above :)

Thanks,

tglx

author reviewer
#6 lore raw
Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Sven Schnelle

- CPUCLOCK_VIRT is now handled with a syscall fallback, which might be slower/less accurate than the old implementation.

I can understand the slower, but why does it become less accurate?

Because we saved the system/user times as almost the last instruction when leaving the kernel to userspace. Now it's a bit earlier, because it is done in the C code. So it's not really related to the syscall fallback, but the switch from assembly to C.

Quoted 9 lines
Quoted 7 lines

Performance number from my system do 100 mio gettimeofday() calls:

Plain syscall: 8.6s
Generic VDSO:  1.3s
old ASM VDSO:  1s

So it's a bit slower but still much faster than syscalls.

Where is the overhead coming from?

It's because we have to allocate a stackframe which we didn't do before, and the compiler generated code is less optimized than the hand-crafted assembly code we had before.

Quoted 39 lines

arch/s390/include/asm/vdso/gettimeofday.h line 34

Quoted 10 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +10 -0
+static inline u64 __arch_get_hw_counter(s32 clock_mode)
+{
+	const struct vdso_data *vdso = __arch_get_vdso_data();
+	u64 adj, now;
+	int cnt;
+
+	do {
+		do {
+			cnt = READ_ONCE(vdso->arch.tb_update_cnt);
+		} while (cnt & 1);
                smp_rmb() ?

arch/s390/include/asm/vdso/gettimeofday.h line 39

Quoted 4 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +4 -0
+		now = get_tod_clock();
+		adj = vdso->arch.tod_steering_end - now;
+		if (unlikely((s64) adj > 0))
+			now += (vdso->arch.tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
                smp_rmb() ?
Quoted 5 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +2 -0
+	} while (cnt != READ_ONCE(vdso->arch.tb_update_cnt));
+	return now;
 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
 		lpar_offset = qto.tod_epoch_difference;

arch/s390/include/asm/vdso/gettimeofday.h static int stp_sync_clock(void *data) · line 559

Quoted 10 lines
arch/s390/include/asm/vdso/gettimeofday.h new file +7 -0
@@ -599,6 +550,13 @@ static int stp_sync_clock(void *data)
 		if (stp_info.todoff[0] || stp_info.todoff[1] ||
 		    stp_info.todoff[2] || stp_info.todoff[3] ||
 		    stp_info.tmd != 2) {
+			vdso_data->arch.tb_update_cnt++;
+			/*
+			 * This barrier isn't really needed as we're called
+			 * from stop_machine_cpuslocked(). However it doesn't
+			 * hurt in case the code gets changed.
+			 */
+			smp_wmb();

WMB without a corresponding RMB and an explanation what's ordered against what is voodoo at best.

Quoted 10 lines

arch/s390/kernel/time.c static int stp_sync_clock(void *data) · line 570

Quoted 8 lines
arch/s390/kernel/time.c +1 -0
 			rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
 					&clock_delta);
 			if (rc == 0) {
@@ -609,6 +567,8 @@ static int stp_sync_clock(void *data)
 				if (rc == 0 && stp_info.tmd != 2)
 					rc = -EAGAIN;
 			}
+			smp_wmb(); /* see comment above */

See my comments above :)

:-)

What do you think about my question on using vdso_write_begin/end()? __arch_get_hw_counter() is called inside a vdso_read_retry() loop, so i would think that just enclosing this update with vdso_write_begin/end() should sufficient. But i'm not sure whether arch/ should call these functions.

Thanks
Sven

#7 lore raw

Sven,

Quoted 12 lines · Sven Schnelle
Quoted 10 lines · Thomas Gleixner

Review static int stp_sync_clock(void *data) · line 570

Quoted 8 lines
(quoted diff) +1 -0
 			rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
 					&clock_delta);
 			if (rc == 0) {
@@ -609,6 +567,8 @@ static int stp_sync_clock(void *data)
 				if (rc == 0 && stp_info.tmd != 2)
 					rc = -EAGAIN;
 			}
+			smp_wmb(); /* see comment above */

See my comments above :)

Quoted 7 lines

:-)

What do you think about my question on using vdso_write_begin/end()? __arch_get_hw_counter() is called inside a vdso_read_retry() loop, so i would think that just enclosing this update with vdso_write_begin/end() should sufficient. But i'm not sure whether arch/ should call these functions.

My knee jerk reaction is obviously NO, but OTOH it makes sense to utilize the existing sequence count for that.

Though that want's a bit more than just fiddling with the sequence counter to be future proof and not restricted to the horrors of stomp machine context or some other orchestration mechanism. Something like the below.

Thanks,

tglx

----
Subject: timekeeping/vsyscall: Provide vdso_update_begin/end()
From: Thomas Gleixner <[email protected]>
Date: Mon, 03 Aug 2020 17:25:31 +0200

Architectures can have the requirement to add additional architecture specific data to the VDSO data page which needs to be updated independent of the timekeeper updates.

To protect these updates vs. concurrent readers and a conflicting update through timekeeping, provide helper functions to make such updates safe.

vdso_update_begin() takes the timekeeper_lock to protect against a potential update from timekeeper code and increments the VDSO sequence count to signal data inconsistency to concurrent readers. vdso_update_end() makes the sequence count even again to signal data consistency and drops the timekeeper lock.

---
 include/vdso/vsyscall.h            |    3 +++
 kernel/time/timekeeping.c          |    2 +-
 kernel/time/timekeeping_internal.h |   11 ++++++++---
 kernel/time/vsyscall.c             |   30 ++++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 4 deletions(-)
include/vdso/vsyscall.h +3 -0
@@ -6,6 +6,9 @@
 
 #include <asm/vdso/vsyscall.h>
 
+void vdso_update_begin(void);
+void vdso_update_end(void);
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __VDSO_VSYSCALL_H */
kernel/time/timekeeping.c +1 -1
@@ -50,7 +50,7 @@ static struct {
 	.seq = SEQCNT_ZERO(tk_core.seq),
 };
 
-static DEFINE_RAW_SPINLOCK(timekeeper_lock);
+DEFINE_RAW_SPINLOCK(timekeeper_lock);
 static struct timekeeper shadow_timekeeper;
 
 /**
kernel/time/timekeeping_internal.h +8 -3
@@ -1,12 +1,14 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _TIMEKEEPING_INTERNAL_H
 #define _TIMEKEEPING_INTERNAL_H
-/*
- * timekeeping debug functions
- */
+
 #include <linux/clocksource.h>
+#include <linux/spinlock.h>
 #include <linux/time.h>
 
+/*
+ * timekeeping debug functions
+ */
 #ifdef CONFIG_DEBUG_FS
 extern void tk_debug_account_sleep_time(const struct timespec64 *t);
 #else
@@ -31,4 +33,7 @@ static inline u64 clocksource_delta(u64
 }
 #endif
 
+/* Semi public for serialization of non timekeeper VDSO updates. */
+extern raw_spinlock_t timekeeper_lock;
+
 #endif /* _TIMEKEEPING_INTERNAL_H */
kernel/time/vsyscall.c +30 -0
@@ -13,6 +13,8 @@
 #include <vdso/helpers.h>
 #include <vdso/vsyscall.h>
 
+#include "timekeeping_internal.h"
+
 static inline void update_vdso_data(struct vdso_data *vdata,
 				    struct timekeeper *tk)
 {
@@ -127,3 +129,31 @@ void update_vsyscall_tz(void)
 
 	__arch_sync_vdso_data(vdata);
 }
+
+/**
+ * vdso_update_begin - Start of a VDSO update section
+ *
+ * Allows architecture code to safely update the architecture specific VDSO
+ * data.
+ */
+void vdso_update_begin(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	raw_spin_lock(&timekeeper_lock);
+	vdso_write_begin(vdata);
+}
+
+/**
+ * vdso_update_end - End of a VDSO update section
+ *
+ * Pairs with vdso_update_begin().
+ */
+void vdso_update_end(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	vdso_write_end(vdata);
+	__arch_sync_vdso_data(vdata);
+	raw_spin_unlock(&timekeeper_lock);
+}
maintainer
#8 lore raw

kernel/time/vsyscall.c void update_vsyscall_tz(void) · line 145

Quoted 13 lines · Thomas Gleixner
kernel/time/vsyscall.c +13 -0
+/**
+ * vdso_update_begin - Start of a VDSO update section
+ *
+ * Allows architecture code to safely update the architecture specific VDSO
+ * data.
+ */
+void vdso_update_begin(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	raw_spin_lock(&timekeeper_lock);
+	vdso_write_begin(vdata);
+}

I would assume that this only works if vdso_update_begin() is called with irqs disabled, otherwise it could deadlock, no?

Maybe something like:

void vdso_update_begin(unsigned long *flags)

{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	raw_spin_lock_irqsave(&timekeeper_lock, *flags);
	vdso_write_begin(vdata);
}

void vdso_update_end(unsigned long *flags)

{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	vdso_write_end(vdata);
	__arch_sync_vdso_data(vdata);
	raw_spin_unlock_irqrestore(&timekeeper_lock, *flags);
}

? Just wondering.

#9 lore raw
Quoted 17 lines · Heiko Carstens

kernel/time/vsyscall.c void update_vsyscall_tz(void) · line 145

Quoted 13 lines · Thomas Gleixner
kernel/time/vsyscall.c +13 -0
+/**
+ * vdso_update_begin - Start of a VDSO update section
+ *
+ * Allows architecture code to safely update the architecture specific VDSO
+ * data.
+ */
+void vdso_update_begin(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	raw_spin_lock(&timekeeper_lock);
+	vdso_write_begin(vdata);
+}

I would assume that this only works if vdso_update_begin() is called with irqs disabled, otherwise it could deadlock, no?

Yes.

Quoted 8 lines

Maybe something like:

void vdso_update_begin(unsigned long *flags)

{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	raw_spin_lock_irqsave(&timekeeper_lock, *flags);
	vdso_write_begin(vdata);

Shudder. Why not returning flags?

Quoted 3 lines

}

void vdso_update_end(unsigned long *flags)

Ditto, why pointer and not value?

Quoted 9 lines
{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	vdso_write_end(vdata);
	__arch_sync_vdso_data(vdata);
	raw_spin_unlock_irqrestore(&timekeeper_lock, *flags);
}

? Just wondering.

Thought about that briefly, but then hated the flags thing and delegated it to the caller. Lockdep will yell if that lock is taken with interrupts enabled :)

But aside of the pointer vs. value thing, I'm fine with doing it in the functions.

Thanks,

tglx

maintainer
#10 lore raw
Quoted 24 lines · Thomas Gleixner
Quoted 17 lines · Heiko Carstens

kernel/time/vsyscall.c void update_vsyscall_tz(void) · line 145

Quoted 13 lines · Thomas Gleixner
kernel/time/vsyscall.c +13 -0
+/**
+ * vdso_update_begin - Start of a VDSO update section
+ *
+ * Allows architecture code to safely update the architecture specific VDSO
+ * data.
+ */
+void vdso_update_begin(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	raw_spin_lock(&timekeeper_lock);
+	vdso_write_begin(vdata);
+}

I would assume that this only works if vdso_update_begin() is called with irqs disabled, otherwise it could deadlock, no?

Yes.

Quoted 2 lines

Maybe something like:

Quoted 8 lines
Quoted 6 lines

void vdso_update_begin(unsigned long *flags)

{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	raw_spin_lock_irqsave(&timekeeper_lock, *flags);
	vdso_write_begin(vdata);

Shudder. Why not returning flags?

That was what I had initially but then looked at lock_timer_base(), and tried to be consistent. Ok, bad example, since lock_timer_base() cannot return flags.

Quoted 6 lines

Thought about that briefly, but then hated the flags thing and delegated it to the caller. Lockdep will yell if that lock is taken with interrupts enabled :)

But aside of the pointer vs. value thing, I'm fine with doing it in the functions.

FWIW, my preference would also to use values instead of pointers.

author reviewer
#11 lore raw
In reply to Thomas Gleixner

Hi,

Quoted 47 lines · Thomas Gleixner
Quoted 17 lines · Heiko Carstens

kernel/time/vsyscall.c void update_vsyscall_tz(void) · line 145

Quoted 13 lines · Thomas Gleixner
kernel/time/vsyscall.c +13 -0
+/**
+ * vdso_update_begin - Start of a VDSO update section
+ *
+ * Allows architecture code to safely update the architecture specific VDSO
+ * data.
+ */
+void vdso_update_begin(void)
+{
+	struct vdso_data *vdata = __arch_get_k_vdso_data();
+
+	raw_spin_lock(&timekeeper_lock);
+	vdso_write_begin(vdata);
+}

I would assume that this only works if vdso_update_begin() is called with irqs disabled, otherwise it could deadlock, no?

Yes.

Quoted 8 lines

Maybe something like:

void vdso_update_begin(unsigned long *flags)

{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	raw_spin_lock_irqsave(&timekeeper_lock, *flags);
	vdso_write_begin(vdata);

Shudder. Why not returning flags?

Quoted 3 lines

}

void vdso_update_end(unsigned long *flags)

Ditto, why pointer and not value?

Quoted 8 lines
{
	struct vdso_data *vdata = __arch_get_k_vdso_data();

	vdso_write_end(vdata);
	__arch_sync_vdso_data(vdata);
	raw_spin_unlock_irqrestore(&timekeeper_lock, *flags);
}
Quoted 8 lines
Quoted 1 line

? Just wondering.

Thought about that briefly, but then hated the flags thing and delegated it to the caller. Lockdep will yell if that lock is taken with interrupts enabled :)

But aside of the pointer vs. value thing, I'm fine with doing it in the functions.

Thanks Thomas & Heiko. I'll incorporate the changes into my patchset and send an updated version. Thomas, i think it's fine if i update your patch and we take it through the s390 tree?

Regards
Sven