Skip to contentSkip to conversation

vdso fixups

PATCH In review Arnd Bergmann 2 participants 6 messages Mar 7, 2019 – Mar 8, 2019

Subsystems (7)
lore
Share & tools
lore

Arnd Bergmann

author maintainer
#0 lore raw

cover vdso fixups

Hi Vincento,

Here are a couple more patches that I had to apply on top of your series to get the combined vdso to build cleanly in all randconfig configurations.

Unfortunately, I haven't managed to build the arm64 compat vdso at all here, so there may be additional patches needed for that.

Arnd

Arnd Bergmann (4):
vdso: ARM: fix building without ARM_ARCH_TIMER
x86: vdso: fix compat_vdso build
vdso: arm: use __iter_div_u64_rem() for 64-bit division
arm64: vdso: turn off profiling etc

 arch/arm/include/asm/vdso/gettimeofday.h |  4 ++
 arch/arm/kernel/vdso.c                   |  7 +--
 arch/arm64/kernel/vdso/Makefile          |  6 ++
 arch/x86/include/asm/mshyperv-tsc.h      | 76 ++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h          | 70 +---------------------
 arch/x86/include/asm/pvclock.h           |  2 +-
 arch/x86/include/asm/vdso/gettimeofday.h |  2 +-
 arch/x86/include/asm/vgtod.h             |  2 +-
 arch/x86/kernel/pvclock.c                |  1 +
 include/linux/hrtimer.h                  | 15 +----
 include/linux/hrtimer_defs.h             | 25 ++++++++
 lib/vdso/gettimeofday.c                  |  3 +-
 12 files changed, 121 insertions(+), 92 deletions(-)
 create mode 100644 arch/x86/include/asm/mshyperv-tsc.h
 create mode 100644 include/linux/hrtimer_defs.h
Signature · 1 line
2.20.0
author maintainer
#1 lore raw

PATCH 1/4 vdso: ARM: fix building without ARM_ARCH_TIMER

When the arch timer is disabled, the vdso fails to build:

In file included from lib/vdso/gettimeofday.c:26,
from <command-line>:
arch/arm/include/asm/vdso/gettimeofday.h: In function '__arch_get_hw_counter':
arch/arm/include/asm/vdso/gettimeofday.h:72:9: error: implicit declaration of function 'arch_counter_get_cntvct'; did you mean 'arch_timer_get_rate'? [-Werror=implicit-function-declaration]

  return arch_counter_get_cntvct();
         ^~~~~~~~~~~~~~~~~~~~~~~

Make it use the fallback path instead by returning a negative error code here.

---
 arch/arm/include/asm/vdso/gettimeofday.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/include/asm/vdso/gettimeofday.h +4 -0
index 112df50920b0..77ace4a757c1 100644
@@ -69,7 +69,11 @@ static __always_inline notrace int clock_getres_fallback(
 
 static __always_inline notrace u64 __arch_get_hw_counter(int clock_mode)
 {
+#ifdef CONFIG_ARM_ARCH_TIMER
 	return arch_counter_get_cntvct();
+#else
+	return -EINVAL; /* use fallback */
+#endif
 }
 
 static __always_inline notrace const struct vdso_data *__arch_get_vdso_data(void)
Signature · 1 line
2.20.0
author maintainer
#2 lore raw

PATCH 2/4 x86: vdso: fix compat_vdso build

In reply to Arnd Bergmann

When we build the compat vdso, the kernel headers will be used with a 32-bit compiler combined with configuration options for the 64-bit kernel, which leads to many undesired effects from header files that make assumptions about the configuration options, such as:

In file included from include/linux/mm_types.h:15,

                 from include/asm-generic/fixmap.h:19,
                 from arch/x86/include/asm/fixmap.h:193,
                 from arch/x86/include/asm/apic.h:10,
                 from arch/x86/include/asm/smp.h:13,
                 from include/linux/smp.h:68,
                 from include/linux/percpu.h:7,
                 from include/linux/hrtimer.h:19,
                 from arch/x86/entry/vdso/vdso32/../../../../../lib/vdso/gettimeofday.c:9,
                 from arch/x86/entry/vdso/vdso32/../vclock_gettime.c:13,
                 from arch/x86/entry/vdso/vdso32/vclock_gettime.c:31:

include/linux/page-flags-layout.h:95:2: error: #warning "KASAN: not enough bits in page flags for tag" [-Werror=cpp]
#warning "KASAN: not enough bits in page flags for tag"
^~~~~~~

Limit the scope of the headers much more, by removing linux/hrtimer.h and asm/mshyperv.h from the list of included files. Instead, the parts we actually need are moved into separate headers that are included by the normal ones.

---
 arch/x86/include/asm/mshyperv-tsc.h      | 76 ++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h          | 70 +---------------------
 arch/x86/include/asm/pvclock.h           |  2 +-
 arch/x86/include/asm/vdso/gettimeofday.h |  2 +-
 arch/x86/include/asm/vgtod.h             |  2 +-
 arch/x86/kernel/pvclock.c                |  1 +
 include/linux/hrtimer.h                  | 15 +----
 include/linux/hrtimer_defs.h             | 25 ++++++++
 lib/vdso/gettimeofday.c                  |  3 +-
 9 files changed, 109 insertions(+), 87 deletions(-)
 create mode 100644 arch/x86/include/asm/mshyperv-tsc.h
 create mode 100644 include/linux/hrtimer_defs.h
arch/x86/include/asm/mshyperv-tsc.h new file +76 -0
new file mode 100644
index 000000000000..99c98ccea0bf
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_MSHYPER_TSCPAGE_H
+#define _ASM_X86_MSHYPER_TSCPAGE_H
+
+#include <asm/hyperv-tlfs.h>
+
+#ifdef CONFIG_HYPERV_TSCPAGE
+struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
+static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
+				       u64 *cur_tsc)
+{
+	u64 scale, offset;
+	u32 sequence;
+
+	/*
+	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
+	 * Top-Level Functional Specification ver. 3.0 and above. To get the
+	 * reference time we must do the following:
+	 * - READ ReferenceTscSequence
+	 *   A special '0' value indicates the time source is unreliable and we
+	 *   need to use something else. The currently published specification
+	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
+	 *   instead of '0' as the special value, see commit c35b82ef0294.
+	 * - ReferenceTime =
+	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
+	 * - READ ReferenceTscSequence again. In case its value has changed
+	 *   since our first reading we need to discard ReferenceTime and repeat
+	 *   the whole sequence as the hypervisor was updating the page in
+	 *   between.
+	 */
+	do {
+		sequence = READ_ONCE(tsc_pg->tsc_sequence);
+		if (!sequence)
+			return U64_MAX;
+		/*
+		 * Make sure we read sequence before we read other values from
+		 * TSC page.
+		 */
+		smp_rmb();
+
+		scale = READ_ONCE(tsc_pg->tsc_scale);
+		offset = READ_ONCE(tsc_pg->tsc_offset);
+		*cur_tsc = rdtsc_ordered();
+
+		/*
+		 * Make sure we read sequence after we read all other values
+		 * from TSC page.
+		 */
+		smp_rmb();
+
+	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
+
+	return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
+}
+
+static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
+{
+	u64 cur_tsc;
+
+	return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc);
+}
+
+#else
+static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
+{
+	return NULL;
+}
+
+static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
+				       u64 *cur_tsc)
+{
+	BUG();
+	return U64_MAX;
+}
+#endif
+#endif
arch/x86/include/asm/mshyperv.h +1 -69

80 lines not on this page · raw message

arch/x86/include/asm/pvclock.h +1 -1
index b6033680d458..19b695ff2c68 100644
@@ -2,7 +2,7 @@
 #ifndef _ASM_X86_PVCLOCK_H
 #define _ASM_X86_PVCLOCK_H
 
-#include <linux/clocksource.h>
+#include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
 
 /* some helper functions for xen and kvm pv clock sources */
arch/x86/include/asm/vdso/gettimeofday.h +1 -1
index 510a0ebd5748..6b2d6ce26bcc 100644
@@ -18,7 +18,7 @@
 #include <asm/unistd.h>
 #include <asm/msr.h>
 #include <asm/pvclock.h>
-#include <asm/mshyperv.h>
+#include <asm/mshyperv-tsc.h>
 
 #define _vdso_data (&VVAR(vdso_data))
 
arch/x86/include/asm/vgtod.h +1 -1
index 0b4e343c62be..cd60f292c553 100644
@@ -3,7 +3,7 @@
 #define _ASM_X86_VGTOD_H
 
 #include <linux/compiler.h>
-#include <linux/clocksource.h>
+#include <asm/clocksource.h>
 #include <vdso/datapage.h>
 #include <vdso/helpers.h>
 
arch/x86/kernel/pvclock.c +1 -0
index 9b158b4716d2..747264182df7 100644
@@ -15,6 +15,7 @@
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
+#include <linux/clocksource.h>
 #include <linux/kernel.h>
 #include <linux/percpu.h>
 #include <linux/notifier.h>
include/linux/hrtimer.h +1 -14
index 2e8957eac4d4..c922ce02e2e6 100644
@@ -12,6 +12,7 @@
 #ifndef _LINUX_HRTIMER_H
 #define _LINUX_HRTIMER_H
 
+#include <linux/hrtimer_defs.h>
 #include <linux/rbtree.h>
 #include <linux/ktime.h>
 #include <linux/init.h>
@@ -298,26 +299,12 @@ struct clock_event_device;
 
 extern void hrtimer_interrupt(struct clock_event_device *dev);
 
-/*
- * The resolution of the clocks. The resolution value is returned in
- * the clock_getres() system call to give application programmers an
- * idea of the (in)accuracy of timers. Timer values are rounded up to
- * this resolution values.
- */
-# define HIGH_RES_NSEC		1
-# define KTIME_HIGH_RES		(HIGH_RES_NSEC)
-# define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
-# define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
-
 extern void clock_was_set_delayed(void);
 
 extern unsigned int hrtimer_resolution;
 
 #else
 
-# define MONOTONIC_RES_NSEC	LOW_RES_NSEC
-# define KTIME_MONOTONIC_RES	KTIME_LOW_RES
-
 #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
 
 static inline void clock_was_set_delayed(void) { }
include/linux/hrtimer_defs.h new file +25 -0
new file mode 100644
index 000000000000..1f181d31e29c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifndef _LINUX_HRTIMER_DEFS_H
+#define _LINUX_HRTIMER_DEFS_H
+
+#ifdef CONFIG_HIGH_RES_TIMERS
+
+/*
+ * The resolution of the clocks. The resolution value is returned in
+ * the clock_getres() system call to give application programmers an
+ * idea of the (in)accuracy of timers. Timer values are rounded up to
+ * this resolution values.
+ */
+# define HIGH_RES_NSEC		1
+# define KTIME_HIGH_RES		(HIGH_RES_NSEC)
+# define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
+# define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
+
+#else
+
+# define MONOTONIC_RES_NSEC	LOW_RES_NSEC
+# define KTIME_MONOTONIC_RES	KTIME_LOW_RES
+
+#endif
+
+#endif
lib/vdso/gettimeofday.c +2 -1
index f10e3703abaa..9b4944d2c2ed 100644
@@ -6,7 +6,8 @@
 #include <linux/math64.h>
 #include <linux/time.h>
 #include <linux/kernel.h>
-#include <linux/hrtimer.h>
+#include <linux/ktime.h>
+#include <linux/hrtimer_defs.h>
 #include <vdso/datapage.h>
 #include <vdso/helpers.h>
 
Signature · 1 line
2.20.0
author maintainer
#3 lore raw

PATCH 3/4 vdso: arm: use __iter_div_u64_rem() for 64-bit division

In reply to Arnd Bergmann

Some compilers, e.g. clang-8, can turn a loop back into a division operation, which here results in a link failure:

arch/arm/kernel/vdso.o: In function `update_vsyscall':
vdso.c:(.text+0x1c4): undefined reference to `__aeabi_uldivmod'

The problem is already solved in the generic __iter_div_u64_rem() function, so use that here.

---
 arch/arm/kernel/vdso.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/arm/kernel/vdso.c +2 -5
index 68930c90c504..32ad392e403b 100644
@@ -328,11 +328,8 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec		= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec			= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
 	nsec			= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec		= nsec;
+
+	vdso_ts->sec += __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__tk_is_cntvct) {
 		vdso_data->clock_mode	= 0;
Signature · 1 line
2.20.0
author maintainer
#4 lore raw

PATCH 4/4 arm64: vdso: turn off profiling etc

In reply to Arnd Bergmann

I took this from the x86 version, without it, all
kinds of things fail to build in the vdso.

---
 arch/arm64/kernel/vdso/Makefile | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/kernel/vdso/Makefile +6 -0
index c904b2a9b255..3768600219c0 100644
@@ -25,6 +25,12 @@ ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \
 VDSO_LDFLAGS := -Bsymbolic
 
 CFLAGS_REMOVE_vgettimeofday.o = -pg -Os
+KBUILD_CFLAGS			+= $(DISABLE_LTO)
+KASAN_SANITIZE                  := n
+UBSAN_SANITIZE                  := n
+OBJECT_FILES_NON_STANDARD       := y
+KCOV_INSTRUMENT			:= n
+
 ifeq ($(c-gettimeofday-y),)
 CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny
 else
Signature · 1 line
2.20.0
#5 lore raw
In reply to Arnd Bergmann

Hi Arnd,

Quoted 8 lines · Arnd Bergmann

Hi Vincento,

Here are a couple more patches that I had to apply on top of your series to get the combined vdso to build cleanly in all randconfig configurations.

Unfortunately, I haven't managed to build the arm64 compat vdso at all here, so there may be additional patches needed for that.

Thank you for this. Going forward, I will start using randconfig as well to make sure that I catch as many issues as I can.

For what concerns arm64 compat vdso, the compilation is currently disabled by default when the compiler is clang, because there is a problem with the include paths that requires further investigation. I was planning to provide a separate patchset to address the problem.

Quoted 24 lines

Arnd

Arnd Bergmann (4):
vdso: ARM: fix building without ARM_ARCH_TIMER
x86: vdso: fix compat_vdso build
vdso: arm: use __iter_div_u64_rem() for 64-bit division
arm64: vdso: turn off profiling etc

 arch/arm/include/asm/vdso/gettimeofday.h |  4 ++
 arch/arm/kernel/vdso.c                   |  7 +--
 arch/arm64/kernel/vdso/Makefile          |  6 ++
 arch/x86/include/asm/mshyperv-tsc.h      | 76 ++++++++++++++++++++++++
 arch/x86/include/asm/mshyperv.h          | 70 +---------------------
 arch/x86/include/asm/pvclock.h           |  2 +-
 arch/x86/include/asm/vdso/gettimeofday.h |  2 +-
 arch/x86/include/asm/vgtod.h             |  2 +-
 arch/x86/kernel/pvclock.c                |  1 +
 include/linux/hrtimer.h                  | 15 +----
 include/linux/hrtimer_defs.h             | 25 ++++++++
 lib/vdso/gettimeofday.c                  |  3 +-
 12 files changed, 121 insertions(+), 92 deletions(-)
 create mode 100644 arch/x86/include/asm/mshyperv-tsc.h
 create mode 100644 include/linux/hrtimer_defs.h
Signature · 2 lines
Regards,
Vincenzo