aarch64: vdso: Wire up getrandom() vDSO implementation
Subsystems (6)
PATCH v3 1/1 aarch64: vdso: Wire up getrandom() vDSO implementation
Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data.
The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided uses NEON on the permute operation, with a fallback to the syscall for chips that do not support AdvSIMD.
This also passes the vdso_test_chacha test along with vdso_test_getrandom. The vdso_test_getrandom bench-single result on Neoverse-N1 shows:
vdso: 25000000 times in 0.783884250 seconds
libc: 25000000 times in 8.780275399 seconds
syscall: 25000000 times in 8.786581518 seconds
--
Changes from v2:
- Refactor Makefile to use same flags for vgettimeofday and vgetrandom.
- Removed rodata usage and fixed BE on vgetrandom-chacha.S.
Changes from v1:
- Fixed style issues and typos.
- Added fallback for systems without NEON support.
- Avoid use of non-volatile vector registers in neon chacha20.
- Use c-getrandom-y for vgetrandom.c.
- Fixed TIMENS vdso_rnd_data access.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/vdso.h | 6 + arch/arm64/include/asm/vdso/getrandom.h | 49 ++++++ arch/arm64/include/asm/vdso/vsyscall.h | 10 ++ arch/arm64/kernel/vdso.c | 6 - arch/arm64/kernel/vdso/Makefile | 25 ++- arch/arm64/kernel/vdso/vdso | 1 + arch/arm64/kernel/vdso/vdso.lds.S | 4 + arch/arm64/kernel/vdso/vgetrandom-chacha.S | 178 +++++++++++++++++++++ arch/arm64/kernel/vdso/vgetrandom.c | 15 ++ lib/vdso/getrandom.c | 1 + tools/arch/arm64/vdso | 1 + tools/include/linux/compiler.h | 4 + tools/testing/selftests/vDSO/Makefile | 3 +- 14 files changed, 289 insertions(+), 15 deletions(-) create mode 100644 arch/arm64/include/asm/vdso/getrandom.h create mode 120000 arch/arm64/kernel/vdso/vdso create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c create mode 120000 tools/arch/arm64/vdso
arch/arm64/Kconfig +1 -0
@@ -262,6 +262,7 @@ config ARM64 select TRACE_IRQFLAGS_NMI_SUPPORT select HAVE_SOFTIRQ_ON_OWN_STACK select USER_STACKTRACE_SUPPORT + select VDSO_GETRANDOM help ARM 64-bit (AArch64) Linux support.
arch/arm64/include/asm/vdso.h +6 -0
@@ -16,6 +16,12 @@#ifndef __ASSEMBLY__ +enum vvar_pages { +VVAR_DATA_PAGE_OFFSET , +VVAR_TIMENS_PAGE_OFFSET , +VVAR_NR_PAGES , +}; +#include <generated/vdso-offsets.h> #define VDSO_SYMBOL (base, name) \
arch/arm64/include/asm/vdso/getrandom.h new file +49 -0
49 lines not on this page · raw message
arch/arm64/include/asm/vdso/vsyscall.h +10 -0
@@ -2,8 +2,11 @@#ifndef __ASM_VDSO_VSYSCALL_H#define __ASM_VDSO_VSYSCALL_H +#define __VDSO_RND_DATA_OFFSET 480 +#ifndef __ASSEMBLY__ +#include <asm/vdso.h> #include <linux/timekeeper_internal.h> #include <vdso/datapage.h>
@@ -21,6 +24,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void) }#define __arch_get_k_vdso_data __arm64_get_k_vdso_data +static __always_inline +struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void) +{ +return (void *)vdso_data + __VDSO_RND_DATA_OFFSET; +} +#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data +static __always_inline void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk) {
arch/arm64/kernel/vdso.c +0 -6
@@ -34,12 +34,6 @@ enum vdso_abi { VDSO_ABI_AA32, }; -enum vvar_pages { -VVAR_DATA_PAGE_OFFSET , -VVAR_TIMENS_PAGE_OFFSET , -VVAR_NR_PAGES , -}; -struct vdso_abi_info {const char *name;const char *vdso_code_start;
arch/arm64/kernel/vdso/Makefile +17 -8
@@ -9,7 +9,7 @@ # Include the generic Makefile to check the built vdso. include $(srctree)/lib/vdso/Makefile -obj-vdso := vgettimeofday.o note.o sigreturn.o +obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o # Build rules targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -34,19 +34,28 @@ ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18 ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO # -Wmissing-prototypes and -Wmissing-declarations are removed from -# the CFLAGS of vgettimeofday.c to make possible to build the -# kernel with CONFIG_WERROR enabled. -CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \ - $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \ - $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \ - -Wmissing-prototypes -Wmissing-declarations +# the CFLAGS to make possible to build the kernel with CONFIG_WERROR enabled. +CC_FLAGS_REMOVE_VDSO := $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \ + $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \ + $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \ + -Wmissing-prototypes -Wmissing-declarations -CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables +CC_FLAGS_ADD_VDSO := -O2 -mcmodel=tiny -fasynchronous-unwind-tables + +CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_REMOVE_VDSO) +CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_REMOVE_VDSO) + +CFLAGS_vgettimeofday.o = $(CC_FLAGS_ADD_VDSO) +CFLAGS_vgetrandom.o = $(CC_FLAGS_ADD_VDSO) ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y) endif +ifneq ($(c-getrandom-y),) + CFLAGS_vgetrandom.o += -include $(c-getrandom-y) +endif + targets += vdso.lds CPPFLAGS_vdso.lds += -P -C -U$(ARCH)
arch/arm64/kernel/vdso/vdso new file +1 -0
@@ -0,0 +1 @@ +../../../arch/arm64/kernel/vdso
arch/arm64/kernel/vdso/vdso.lds.S +4 -0
@@ -11,7 +11,9 @@ #include <linux/const.h> #include <asm/page.h> #include <asm/vdso.h> +#include <asm/vdso/vsyscall.h> #include <asm-generic/vmlinux.lds.h> +#include <vdso/datapage.h> OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64") OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64) SECTIONS { PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE); + PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET); #ifdef CONFIG_TIME_NS PROVIDE(_timens_data = _vdso_data + PAGE_SIZE); #endif
@@ -102,6 +105,7 @@ VERSION __kernel_gettimeofday; __kernel_clock_gettime; __kernel_clock_getres; + __kernel_getrandom; local: *; }; }
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +178 -0
178 lines not on this page · raw message
arch/arm64/kernel/vdso/vgetrandom.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +typeof (__cvdso_getrandom) __kernel_getrandom; + +ssize_t __kernel_getrandom (void *buffer,size_t len,unsigned int flags,void *opaque_state,size_t opaque_len) +{ + asm goto ( + ALTERNATIVE("b %[fallback]", "nop", ARM64_HAS_FPSIMD) : : : : fallback); +return __cvdso_getrandom (buffer, len, flags, opaque_state, opaque_len); + +fallback: +if (unlikely (opaque_len == ~0UL && !buffer && !len && !flags)) +return -ENOSYS ; +return getrandom_syscall (buffer, len, flags); +}
lib/vdso/getrandom.c +1 -0
@@ -5,6 +5,7 @@#include <linux/array_size.h> #include <linux/minmax.h> +#include <linux/mm.h> #include <vdso/datapage.h> #include <vdso/getrandom.h> #include <vdso/unaligned.h>
tools/arch/arm64/vdso new file +1 -0
@@ -0,0 +1 @@ +../../../arch/arm64/kernel/vdso
tools/include/linux/compiler.h +4 -0
@@ -2,6 +2,8 @@#ifndef _TOOLS_LINUX_COMPILER_H_#define _TOOLS_LINUX_COMPILER_H_ +#ifndef __ASSEMBLY__ +#include <linux/compiler_types.h> #ifndef __compiletime_error
@@ -224,4 +226,6 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s __asm__ ("" : "=r" (var) : "0" (var))#endif +#endif /* __ASSEMBLY__ */ +#endif /* _TOOLS_LINUX_COMPILER_H */
tools/testing/selftests/vDSO/Makefile +2 -1
@@ -9,7 +9,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) TEST_GEN_PROGS += vdso_standalone_test_x86 endif TEST_GEN_PROGS += vdso_test_correctness -ifeq ($(ARCH)$(CONFIG_X86_32),$(filter $(ARCH)$(CONFIG_X86_32),x86 x86_64 loongarch)) +ifeq ($(ARCH)$(CONFIG_X86_32),$(filter $(ARCH)$(CONFIG_X86_32),x86 x86_64 loongarch arm64)) TEST_GEN_PROGS += vdso_test_getrandom TEST_GEN_PROGS += vdso_test_chacha endif
@@ -40,5 +40,6 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(SRCARCH)/vdso/vgetrandom-chacha.S $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \ -idirafter $(top_srcdir)/arch/$(SRCARCH)/include \ + -idirafter $(top_srcdir)/arch/$(SRCARCH)/include/generated \ -idirafter $(top_srcdir)/include \ -D__ASSEMBLY__ -Wa,--noexecstack
Signature · 1 line
2.43.0
Mark Rutland
reviewerReview
Quoted 6 lines · Adhemerval Zanella
(quoted diff) +6
-0+static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void) +{ + /* + * If a task belongs to a time namespace then the real VVAR page is mapped + * with the VVAR_TIMENS_PAGE_OFFSET offset. + */
This confused me, and I see that it is truncated from the existing commit in arch/arm64/kerne/vdso.c:
If a task belongs to a time namespace then a namespace specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET offset.
... and IIUC the "namespace specific VVAR" page doesn't have the RNG data, right? It'd be good to spell that out, e.g.
/*
* The RNG data is in the real VVAR data page, but if a task
* belongs to a time namespsace then VVAR_DATA_PAGE_OFFSET
* points to the namespace-specific VVAR page and
* VVAR_TIMENS_PAGE_OFFSET points to the real VVAR page. */
It does feel weird that everything else has to work around timer namespaces rather than that being limited to the timer code, so we'll probably want to flip that if we add anything else to the VDSO, or have a separate VVAR_RNG page.
Quoted 4 lines
(quoted diff) +4
-0+ if (IS_ENABLED(CONFIG_TIME_NS) && _vdso_data->clock_mode == VDSO_CLOCKMODE_TIMENS) + return (void*)&_vdso_rng_data + VVAR_TIMENS_PAGE_OFFSET * PAGE_SIZE; + return &_vdso_rng_data; +}
[...]
Quoted 11 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +5
-0@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +typeof (__cvdso_getrandom) __kernel_getrandom; + +ssize_t __kernel_getrandom (void *buffer,size_t len,unsigned int flags,void *opaque_state,size_t opaque_len)
arch/arm64/kernel/vdso/vgetrandom.c line 15
Quoted 10 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +10
-0+{ + asm goto ( + ALTERNATIVE("b %[fallback]", "nop", ARM64_HAS_FPSIMD) : : : : fallback); +return __cvdso_getrandom (buffer, len, flags, opaque_state, opaque_len); + +fallback: +if (unlikely (opaque_len == ~0UL && !buffer && !len && !flags)) +return -ENOSYS ; +return getrandom_syscall (buffer, len, flags); +}
The asm it pretty painful to read, and AFAICT what you actually want here is alternative_has_cap_likely(), which we could use were that not using alt_cb_patch_nops behind the scenes.
I reckon it's worth making the work for the VDSO first (patch below); that way you can make this much nicer:
ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags,
void *opaque_state, size_t opaque_len)
{
if (alternative_has_cap_likely(ARM64_HAS_FPSIMD)) {
return __cvdso_getrandom(buffer, len, flags,
opaque_state, opaque_len);
}
if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags))
return -ENOSYS;
return getrandom_syscall(buffer, len, flags);
}... though the conditions for returning -ENOSYS look very odd to me; why do we care about fast-pathing that specific case rather than forwarding that to the kernel, and does __cvdso_getrandom() handle that correctly?
Mark
---->8----
From b7ee23e4ec47805527c9d7c2ee6b02328fe8437a Mon Sep 17 00:00:00 2001
From: Mark Rutland <[email protected]>
Date: Mon, 2 Sep 2024 15:08:12 +0100
Subject: [PATCH] arm64: alternative: make alternative_has_cap_likely() VDSO
compatible
Currently alternative_has_cap_unlikely() can be used in VDSO code, but alternative_has_cap_likely() cannot as it references alt_cb_patch_nops, which is not available when linking the VDSO. This is unfortunate as it would be useful to have alternative_has_cap_likely() available in VDSO code.
The use of alt_cb_patch_nops was added in commit:
d926079f17bf8aa4 ("arm64: alternatives: add shared NOP callback")
... as removing duplicate NOPs within the kernel Image saved areasonable amount of space.
Given the VDSO code will have nowhere near as many alternative branches as the main kernel image, this isn't much of a concern, and a few extra nops isn't a massive problem.
Change alternative_has_cap_likely() to only use alt_cb_patch_nops for the main kernel image, and allow duplicate NOPs in VDSO code.
--- arch/arm64/include/asm/alternative-macros.h | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/include/asm/alternative-macros.h +4 -0
@@ -230,7 +230,11 @@ alternative_has_cap_likely(const unsigned long cpucap)return false; asm goto( +#ifdef BUILD_VDSO + ALTERNATIVE("b %l[l_no]", "nop", %[cpucap]) +#else ALTERNATIVE_CB("b %l[l_no]", %[cpucap], alt_cb_patch_nops) +#endif : : [cpucap] "i" (cpucap) :
Signature · 1 line
2.30.2
Jason A. Donenfeld
maintainerQuoted 14 lines · Mark Rutland
ssize_t__kernel_getrandom(void *buffer,size_tlen, unsigned int flags,
void*opaque_state,size_topaque_len){ if (alternative_has_cap_likely(ARM64_HAS_FPSIMD)) { return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len); } if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) return -ENOSYS; return getrandom_syscall(buffer, len, flags); }
Quoted 3 lines
... though the conditions for returning
-ENOSYSlook very odd to me; why do we care about fast-pathing that specific case rather than forwarding that to the kernel, and does__cvdso_getrandom()handle that correctly?
Adhemerval's code here is fine and correct. The opaque_len==~0UL thing is a special vDSO case for getting the param struct back, not something related to the kernel. See __cvdso_getrandom_data() for details.
Mark Rutland
reviewerQuoted 15 lines · Jason A. Donenfeld
Quoted 14 lines · Mark Rutland
ssize_t__kernel_getrandom(void *buffer,size_tlen, unsigned int flags,
void*opaque_state,size_topaque_len){ if (alternative_has_cap_likely(ARM64_HAS_FPSIMD)) { return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len); } if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) return -ENOSYS; return getrandom_syscall(buffer, len, flags); }
Quoted 7 lines
Quoted 3 lines
... though the conditions for returning
-ENOSYSlook very odd to me; why do we care about fast-pathing that specific case rather than forwarding that to the kernel, and does__cvdso_getrandom()handle that correctly?Adhemerval's code here is fine and correct. The opaque_len==~0UL thing is a special vDSO case for getting the param struct back, not something related to the kernel. See
__cvdso_getrandom_data()for details.
Ok, so this is to say "we cannot provide a vgetrandom_opaque_params".
Is the syscall fallback just for the CRIU case mentioned in __cvdso_getrandom_data()? The comment above __cvdso_getrandom_data() says:
If @buffer, @len, and @flags are 0, and @opaque_len is ~0UL, then @opaque_state is populated with a struct vgetrandom_opaque_params and the function returns 0; if it does not return 0, this function should not be used.
... so presumably the caller shouldn't bother to call again if it got -ENOSYS above.
Mark.