Skip to contentSkip to conversation

arm64: vdso: replace gettimeofday.S with global vgettimeofday.C

PATCH Superseded Mark Salyzyn 3 participants 6 messages Oct 31, 2017 – Dec 15, 2017 1 message not in the archive arm64

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 10/12 arm64: vdso: replace gettimeofday.S with global vgettimeofday.C

Take an effort from the previous 9 patches to recode the arm64 vdso code from assembler to C previously submitted by
Andrew Pinski <[email protected]>, rework it for use in both arm and arm64, overlapping any optimizations for each architecture. But instead of landing it in arm64, land the result into lib/vdso and unify both implementations to simplify future maintenance.

apinski at cavium.com makes the following claims in the original patch:

This allows the compiler to optimize the divide by 1000 and remove the other divides.

On ThunderX, gettimeofday improves by 32%. On ThunderX 2, gettimeofday improves by 18%.

Note I noticed a bug in the old (arm64) implementation of __kernel_clock_getres; it was checking only the lower 32bits of the pointer; this would work for most cases but could fail in a few.

<end of claim>

Cc James Morse <[email protected]>
Cc Russell King <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Dmitry Safonov <[email protected]>
Cc John Stultz <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Kees Cook <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Andy Gross <[email protected]>
Cc Kevin Brodsky <[email protected]>
Cc Andrew Pinski <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc linux-kernel at vger.kernel.org
Cc linux-arm-kernel at lists.infradead.org

v2:
- turned off profiling, restored quiet_cmd_vdsoas.

v3:
- move arch/arm/vdso/vgettimeofday.c to lib/vdso/vgettimeofday.c.
- adjust vgettimeofday.c to be a better global candidate, switch to using ARCH_PROVIDES_TIMER and __arch_counter_get() as more generic.

v4:
- simplify arch_vdso_read_counter, use read_sysreg.
- Use GENMASK_ULL macro for ARCH_CLOCK_FIXED_MASK.
- update commit message.
- add vdso_wtm_clock_nsec_t, vdso_xtime_clock_sec_t and vdso_raw_time_nsec_t.

---
 arch/arm64/include/asm/vdso_datapage.h |  23 ++-
 arch/arm64/kernel/asm-offsets.c        |   2 +-
 arch/arm64/kernel/vdso.c               |   2 +-
 arch/arm64/kernel/vdso/Makefile        |  16 +-
 arch/arm64/kernel/vdso/compiler.h      |  67 +++++++
 arch/arm64/kernel/vdso/datapage.h      |  59 ++++++
 arch/arm64/kernel/vdso/gettimeofday.S  | 328 ---------------------------------
 arch/arm64/kernel/vdso/vgettimeofday.c |   3 +
 8 files changed, 160 insertions(+), 340 deletions(-)
 create mode 100644 arch/arm64/kernel/vdso/compiler.h
 create mode 100644 arch/arm64/kernel/vdso/datapage.h
 delete mode 100644 arch/arm64/kernel/vdso/gettimeofday.S
 create mode 100644 arch/arm64/kernel/vdso/vgettimeofday.c
arch/arm64/include/asm/vdso_datapage.h +19 -4
index 2b9a63771eda..95f4a7abab80 100644
@@ -20,16 +20,31 @@
 
 #ifndef __ASSEMBLY__
 
+#ifndef _VDSO_WTM_CLOCK_SEC_T
+#define _VDSO_WTM_CLOCK_SEC_T
+typedef __u64 vdso_wtm_clock_nsec_t;
+#endif
+
+#ifndef _VDSO_XTIME_CLOCK_SEC_T
+#define _VDSO_XTIME_CLOCK_SEC_T
+typedef __u64 vdso_xtime_clock_sec_t;
+#endif
+
+#ifndef _VDSO_RAW_TIME_SEC_T
+#define _VDSO_RAW_TIME_SEC_T
+typedef __u64 vdso_raw_time_sec_t;
+#endif
+
 struct vdso_data {
 	__u64 cs_cycle_last;	/* Timebase at clocksource init */
-	__u64 raw_time_sec;	/* Raw time */
+	vdso_raw_time_sec_t raw_time_sec;	/* Raw time */
 	__u64 raw_time_nsec;
-	__u64 xtime_clock_sec;	/* Kernel time */
-	__u64 xtime_clock_nsec;
+	vdso_xtime_clock_sec_t xtime_clock_sec;	/* Kernel time */
+	__u64 xtime_clock_snsec;
 	__u64 xtime_coarse_sec;	/* Coarse time */
 	__u64 xtime_coarse_nsec;
 	__u64 wtm_clock_sec;	/* Wall to monotonic time */
-	__u64 wtm_clock_nsec;
+	vdso_wtm_clock_nsec_t wtm_clock_nsec;
 	__u32 tb_seq_count;	/* Timebase sequence counter */
 	/* cs_* members must be adjacent and in this order (ldp accesses) */
 	__u32 cs_mono_mult;	/* NTP-adjusted clocksource multiplier */
arch/arm64/kernel/asm-offsets.c +1 -1
index 71bf088f1e4b..db5b305806b7 100644
@@ -104,7 +104,7 @@ int main(void)
   DEFINE(VDSO_RAW_TIME_SEC,	offsetof(struct vdso_data, raw_time_sec));
   DEFINE(VDSO_RAW_TIME_NSEC,	offsetof(struct vdso_data, raw_time_nsec));
   DEFINE(VDSO_XTIME_CLK_SEC,	offsetof(struct vdso_data, xtime_clock_sec));
-  DEFINE(VDSO_XTIME_CLK_NSEC,	offsetof(struct vdso_data, xtime_clock_nsec));
+  DEFINE(VDSO_XTIME_CLK_NSEC,	offsetof(struct vdso_data, xtime_clock_snsec));
   DEFINE(VDSO_XTIME_CRS_SEC,	offsetof(struct vdso_data, xtime_coarse_sec));
   DEFINE(VDSO_XTIME_CRS_NSEC,	offsetof(struct vdso_data, xtime_coarse_nsec));
   DEFINE(VDSO_WTM_CLK_SEC,	offsetof(struct vdso_data, wtm_clock_sec));
arch/arm64/kernel/vdso.c +1 -1
index 2d419006ad43..59f150c25889 100644
@@ -238,7 +238,7 @@ void update_vsyscall(struct timekeeper *tk)
 		vdso_data->raw_time_sec         = tk->raw_sec;
 		vdso_data->raw_time_nsec        = tk->tkr_raw.xtime_nsec;
 		vdso_data->xtime_clock_sec	= tk->xtime_sec;
-		vdso_data->xtime_clock_nsec	= tk->tkr_mono.xtime_nsec;
+		vdso_data->xtime_clock_snsec	= tk->tkr_mono.xtime_nsec;
 		vdso_data->cs_mono_mult		= tk->tkr_mono.mult;
 		vdso_data->cs_raw_mult		= tk->tkr_raw.mult;
 		/* tkr_mono.shift == tkr_raw.shift */
arch/arm64/kernel/vdso/Makefile +10 -6
index 62c84f7cb01b..0925f8908dcb 100644
@@ -5,18 +5,26 @@
 # Heavily based on the vDSO Makefiles for other archs.
 #
 
-obj-vdso := gettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o
 
 # Build rules
 targets := $(obj-vdso) vdso.so vdso.so.dbg
 obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
 
-ccflags-y := -shared -fno-common -fno-builtin
+ccflags-y := -shared -fno-common -fno-builtin -fno-stack-protector
+ccflags-y += -DDISABLE_BRANCH_PROFILING
 ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \
 		$(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
 
+# Force -O2 to avoid libgcc dependencies
+CFLAGS_REMOVE_vgettimeofday.o = -pg -Os
+CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny
+
 # Disable gcov profiling for VDSO code
 GCOV_PROFILE := n
+KASAN_SANITIZE := n
+UBSAN_SANITIZE := n
+KCOV_INSTRUMENT := n
 
 # Workaround for bare-metal (ELF) toolchains that neglect to pass -shared
 # down to collect2, resulting in silent corruption of the vDSO image.
@@ -48,10 +56,6 @@ endef
 include/generated/vdso-offsets.h: $(obj)/vdso.so.dbg FORCE
 	$(call if_changed,vdsosym)
 
-# Assembly rules for the .S files
-$(obj-vdso): %.o: %.S FORCE
-	$(call if_changed_dep,vdsoas)
-
 # Actual build commands
 quiet_cmd_vdsold = VDSOL   $@
       cmd_vdsold = $(CC) $(c_flags) -Wl,-n -Wl,-T $^ -o $@
arch/arm64/kernel/vdso/compiler.h new file +67 -0

67 lines not on this page · raw message

arch/arm64/kernel/vdso/datapage.h new file +59 -0

59 lines not on this page · raw message

arch/arm64/kernel/vdso/gettimeofday.S deleted +0 -328

328 lines not on this page · raw message

arch/arm64/kernel/vdso/vgettimeofday.c new file +3 -0
new file mode 100644
index 000000000000..b73d4011993d
@@ -0,0 +1,3 @@
+#include "compiler.h"
+#include "datapage.h"
+#include "../../../../lib/vdso/vgettimeofday.c"
Signature · 1 line
2.15.0.rc2.357.g7e34df9404-goog
#1 lore raw
Quoted 16 lines · Mark Salyzyn

. . .

arch/arm64/kernel/vdso/Makefile +1 -1
index 62c84f7cb01b..0925f8908dcb 100644
@@ -5,18 +5,26 @@
  # Heavily based on the vDSO Makefiles for other archs.
  #
 
-obj-vdso := gettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o
 
  # Build rules
  targets := $(obj-vdso) vdso.so vdso.so.dbg
  obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
 

arch/arm64/kernel/vdso/Makefile line 22

Quoted 10 lines
arch/arm64/kernel/vdso/Makefile +6 -1
-ccflags-y := -shared -fno-common -fno-builtin
+ccflags-y := -shared -fno-common -fno-builtin -fno-stack-protector
+ccflags-y += -DDISABLE_BRANCH_PROFILING
  ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \
  		$(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
 
+# Force -O2 to avoid libgcc dependencies
+CFLAGS_REMOVE_vgettimeofday.o = -pg -Os
+CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny
+

This last stanza breaks clang builds. Suggested fix is:

@@ -18,7 +18,11 @@ ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \

?# Force -O2 to avoid libgcc dependencies
?CFLAGS_REMOVE_vgettimeofday.o = -pg -Os
+ifeq ($(cc-name),clang)
+CFLAGS_vgettimeofday.o = -O2
+else
?CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny
+endif

?# Disable gcov profiling for VDSO code
?GCOV_PROFILE := n

Holding off respin or followup patch until after we get a response from Jon Masters <[email protected]> on their QE tests on this patch series.

-- Mark

#3 lore raw
In reply to a message not in the archive
Quoted 7 lines · Jon Masters
Quoted 3 lines · Mark Salyzyn

Holding off respin or followup patch until after we get a response from Jon Masters <[email protected]> on their QE tests on this patch series.

Copying Jeff Bastian who owns QE. Jeff, can you recall what test was failing when we tried the gettimeofday C rewrite VDSO patches?

The LTP clock_getres01 test was failing when we tried the initial implementation of gettimeofday-in-C. This would be a good place to start when testing the new version of the patches.

https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/syscalls/clock_getres/clock_getres01.c

Signature · 3 lines
Jeff Bastian
Kernel QE - Hardware Enablement
Red Hat
#4 lore raw
Quoted 6 lines · Jeffrey Bastian
Quoted 5 lines · Jon Masters
Quoted 3 lines · Mark Salyzyn

Holding off respin or followup patch until after we get a response from Jon Masters <[email protected]> on their QE tests on this patch series.

Quoted 10 lines
Quoted 2 lines

Copying Jeff Bastian who owns QE. Jeff, can you recall what test was failing when we tried the gettimeofday C rewrite VDSO patches?

The LTP clock_getres01 test was failing when we tried the initial implementation of gettimeofday-in-C. This would be a good place to start when testing the new version of the patches.

https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/syscalls/clock_getres/clock_getres01.c

FWIW, I just tried this on my Mustang (4.14 + these patches) with both DT and acpi=force and it passed.

Thanks,
Laura

#5 lore raw
In reply to Jeffrey Bastian
Quoted 5 lines · Jeffrey Bastian
Quoted 4 lines · Jon Masters
Quoted 3 lines · Mark Salyzyn

Holding off respin or followup patch until after we get a response from Jon Masters <[email protected]> on their QE tests on this patch series.

Quoted 9 lines
Quoted 2 lines

Copying Jeff Bastian who owns QE. Jeff, can you recall what test was failing when we tried the gettimeofday C rewrite VDSO patches?

The LTP clock_getres01 test was failing when we tried the initial implementation of gettimeofday-in-C. This would be a good place to start when testing the new version of the patches.

https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/syscalls/clock_getres/clock_getres01.c

Thanks.

I ran the test (32 and 64 bit) on my latest work (but back-ported to 4.9) and it passes. I did not change __vdso_clock_getres or the fallback (syscall) handler in the patch series though, so the failure has me wondering. Could you re-run the test with latest to be sure, and I will look into setting up to test with a ToT kernel (after the thanksgiving break).

Using bionic libc, with advance modifications to utilize __vdso_clock_getres; required in order to support the test.

NB: in AOSP _today_ bionic does _not_ propagate the errno correctly on the __vdso_clock_gettime vdso path and will understandably fail the similar tests. The public rubber stamp of __vdso_clock_gettime code to support __vdso_clock_getres will not work. errno propagation issue will be fixed in bionic.

-- Mark

#6 lore raw
In reply to Jeffrey Bastian
Quoted 6 lines · Mark Salyzyn

I ran the test (32 and 64 bit) on my latest work (but back-ported to 4.9) and it passes. I did not change __vdso_clock_getres or the fallback (syscall) handler in the patch series though, so the failure has me wondering. Could you re-run the test with latest to be sure, and I will look into setting up to test with a ToT kernel (after the thanksgiving break).

I applied your patch set to a 4.14 kernel and ran LTP clock_getres01 (64-bit) and it still passed:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root at localhost ~]# uname -r
4.14.0-vdsogtod.aarch64

[root at localhost ~]# ./ltp-full-20170929/testcases/kernel/syscalls/clock_getres/clock_getres01 tst_test.c:934: INFO: Timeout per run is 0h 05m 00s
clock_getres01.c:79: PASS: clock_getres(REALTIME, ...) succeeded clock_getres01.c:79: PASS: clock_getres(MONOTONIC, ...) succeeded clock_getres01.c:79: PASS: clock_getres(PROCESS_CPUTIME_ID, ...) succeeded clock_getres01.c:79: PASS: clock_getres(THREAD_CPUTIME_ID, ...) succeeded clock_getres01.c:79: PASS: clock_getres(REALTIME, ...) succeeded clock_getres01.c:79: PASS: clock_getres(CLOCK_MONOTONIC_RAW, ...) succeeded clock_getres01.c:79: PASS: clock_getres(CLOCK_REALTIME_COARSE, ...) succeeded clock_getres01.c:79: PASS: clock_getres(CLOCK_MONOTONIC_COARSE, ...) succeeded clock_getres01.c:79: PASS: clock_getres(CLOCK_BOOTTIME, ...) succeeded clock_getres01.c:64: CONF: clock_getres(CLOCK_REALTIME_ALARM, ...) NO SUPPORTED clock_getres01.c:64: CONF: clock_getres(CLOCK_BOOTTIME_ALARM, ...) NO SUPPORTED clock_getres01.c:79: PASS: clock_getres(-1, ...) succeeded

Summary:

passed   10
failed   0
skipped  2

warnings 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I also tried another test (see source below) which sleeps for 0.5 seconds and then verifies that approximately 0.5 seconds passed. With the original gettimeofday-in-C patches, it would often fail with weird elapsed times like 0.1 seconds or 1.1 seconds:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 1207, tv_nsec = 6403566 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 1207, tv_nsec = 16512476
Elapsed: 0.10108910
FAIL: elapsed time is not approximately 0.5 seconds 255

[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 1215, tv_nsec = 9784545 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 1216, tv_nsec = 9898595
Elapsed: 1.114050
FAIL: elapsed time is not approximately 0.5 seconds 255

[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 1225, tv_nsec = 13710370 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 1225, tv_nsec = 13819740
Elapsed: 0.109370
FAIL: elapsed time is not approximately 0.5 seconds 255
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The elapsed time matches the expected 0.5 seconds with the latest patch version:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 2077, tv_nsec = 989623095 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 2078, tv_nsec = 489728070
Elapsed: 0.500104975
PASS
0
[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 2079, tv_nsec = 519625035 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 2080, tv_nsec = 19726365
Elapsed: 0.500101330
PASS
0
[root at localhost ~]# ./a.out ; echo $?
1: clock_gettime: tv_sec = 2080, tv_nsec = 684894725 Sleeping 0.5 seconds
2: clock_gettime: tv_sec = 2081, tv_nsec = 184994605
Elapsed: 0.500099880
PASS
0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Source for the elapsed time test:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <time.h>

int main(void)

{
    int ret;
    clockid_t clk_id = CLOCK_MONOTONIC_RAW;
    struct timespec tp1, tp2, elapsed;
    struct timespec req, rem;

    ret = clock_gettime(clk_id, &tp1);
    if (ret) {
        perror("ERROR: clock_gettime:");
    }
    printf("1: clock_gettime: tv_sec = %ld, tv_nsec = %lld\n",
           tp1.tv_sec, tp1.tv_nsec);

    printf("Sleeping 0.5 seconds\n");
    req.tv_sec = 0;
    req.tv_nsec = 500000000;
    ret = nanosleep(&req, &rem);
    if (ret) {
        perror("ERROR: nanosleep:");
        printf("       Remaining sleep: tv_sec = %ld, tv_nsec = %lld\n",
               rem.tv_sec, rem.tv_nsec);
    }

    ret = clock_gettime(clk_id, &tp2);
    if (ret) {
        perror("ERROR: clock_gettime:");
    }
    printf("2: clock_gettime: tv_sec = %ld, tv_nsec = %lld\n",
           tp2.tv_sec, tp2.tv_nsec);

    if ((tp2.tv_nsec - tp1.tv_nsec) < 0) {
        elapsed.tv_sec = tp2.tv_sec - tp1.tv_sec - 1;
        elapsed.tv_nsec = 1000000000 + tp2.tv_nsec - tp1.tv_nsec;
    } else {
        elapsed.tv_sec = tp2.tv_sec - tp1.tv_sec;
        elapsed.tv_nsec = tp2.tv_nsec - tp1.tv_nsec;
    }

    printf("Elapsed: %ld.%lld\n", elapsed.tv_sec, elapsed.tv_nsec);

    if (elapsed.tv_sec != 0 ||
        elapsed.tv_nsec < 490000000 ||
        elapsed.tv_nsec > 510000000) {
        printf("FAIL: elapsed time is not approximately 0.5 seconds\n");
        return -1;
    }

    printf("PASS\n");
    return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signature · 3 lines
Jeff Bastian
Kernel QE - Hardware Enablement
Red Hat