arm64: vdso: replace gettimeofday.S with global vgettimeofday.C
PATCH v3 10/12 arm64: vdso: replace gettimeofday.S with global vgettimeofday.C
Take an effort represented in the previous 8 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.
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 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.
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.
--- arch/arm64/include/asm/vdso_datapage.h | 2 +- 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 | 71 +++++++ arch/arm64/kernel/vdso/datapage.h | 58 ++++++ arch/arm64/kernel/vdso/gettimeofday.S | 328 --------------------------------- arch/arm64/kernel/vdso/vgettimeofday.c | 3 + 8 files changed, 145 insertions(+), 337 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 +1 -1
@@ -25,7 +25,7 @@ struct vdso_data {__u64 raw_time_sec;/* Raw time */ __u64 raw_time_nsec;__u64 xtime_clock_sec;/* Kernel time */ -__u64 xtime_clock_nsec; +__u64 xtime_clock_snsec;__u64 xtime_coarse_sec;/* Coarse time */ __u64 xtime_coarse_nsec;__u64 wtm_clock_sec;/* Wall to monotonic time */
arch/arm64/kernel/asm-offsets.c +1 -1
@@ -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
@@ -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
@@ -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 +71 -0
71 lines not on this page · raw message
arch/arm64/kernel/vdso/datapage.h new file +58 -0
58 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
@@ -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
Mark Rutland
reviewerHi,
arch/arm64/kernel/vdso/compiler.h line 55
Quoted 4 lines · Mark Salyzyn
arch/arm64/kernel/vdso/compiler.h new file +4
-0+/* +* AArch64 implementation of arch_counter_get_cntvct() suitable for vdso +*/ +static __always_inline notrace u64 __arch_counter_get(void)
Can we give the VDSO helpers some common prefix, e.g. use arch_vdso_* and call this arch_vdo_read_counter()?
That would make it clear that this isn't a pointless copy of existing helpers, and having a consistent naming prefix would be nicer as more stuff gets added to the generic VDSO.
arch/arm64/kernel/vdso/compiler.h line 64
Quoted 9 lines
arch/arm64/kernel/vdso/compiler.h new file +9
-0+{ +u64 res; + +/* Read the virtual counter. */ +isb (); + asmvolatile ("mrs %0, cntvct_el0" :"=r" (res) ::"memory" ); + +return res; +}
This can be simplified to:
isb(); return read_sysreg(cntvct_el0);
[...]
Quoted 18 lines
arch/arm64/kernel/vdso/datapage.h new file +18
-0+/* +* We use the hidden visibility to prevent the compiler from generating a GOT +* relocation. Not only is going through a GOT useless (the entry couldn't and +* mustn't be overridden by another library), it does not even work: the linker +* cannot generate an absolute address to the data page. +* +* With the hidden visibility, the compiler simply generates a PC-relative +* relocation (R_ARM_REL32), and this is what we need. +*/ +extern const struct vdso_data _vdso_data __attribute__((visibility("hidden"))); + +static inline const struct vdso_data *__get_datapage (void ) +{ +const struct vdso_data *ret; +/* +* This simply puts &_vdso_data into ret. The reason why we don't use +* `ret = &_vdso_data` is that the compiler tends to optimise this in a +* very suboptimal way: instead of keeping &_vdso_data in a register,
arch/arm64/kernel/vdso/datapage.h line 50
Quoted 10 lines
arch/arm64/kernel/vdso/datapage.h new file +10
-0+* it goes through a relocation almost every time _vdso_data must be +* accessed (even in subfunctions). This is both time and space +* consuming: each relocation uses a word in the code section, and it +* has to be loaded at runtime. +* +* This trick hides the assignment from the compiler. Since it cannot +* track where the pointer comes from, it will only use one relocation +* where __get_datapage() is called, and then keep the result in a +* register. +*/
Has anyone reported this to upstream GCC?
It would be nice to minimize the set of bodges we have to carry forever. They tend to bit us in the end.
arch/arm64/kernel/vdso/datapage.h line 56
Quoted 6 lines
arch/arm64/kernel/vdso/datapage.h new file +6
-0+ asm("" :"=r" (ret) :"0" (&_vdso_data)); +return ret; +} + +/* We can only guarantee 56 bits of precision. */ +#define ARCH_CLOCK_FIXED_MASK (~(0xff00ull << 48))
Can't we use:
BITMASK_ULL(55, 0);
... ?
Thanks,
Mark.