[PATCH v6] linux: Add support for getrandom vDSO

Stefan Liebler stli@linux.ibm.com
Tue Sep 17 08:07:52 GMT 2024


On 13.09.24 18:27, Adhemerval Zanella wrote:
> Linux 6.11 will support calling getrandom() from the vDSO (commit
> 4ad10a5f5f78a5b3e525a63bd075a4eb1139dde1). It operates on a thread-local
> opaque state allocated with mmap using flags specified by the vDSO.
> 
> Multiple states are allocated at once, as many as fit into a page, and
> these are held in an array of available states to be doled out to each
> thread upon first use, and recycled when a thread terminates.  As these
> states run low, more are allocated.
> 
> To make this procedure async-signal-safe, a simple guard is used in the
> LSB of the opaque state address, falling back to the syscall if there's
> reentrancy contention.
> 
> Also, _Fork() is handled by blocking signals on opaque state allocation
> (so _Fork() always sees a consistent state even if it interrupts a
> getrandom() call) and by iterating over the thread stack cache on
> reclaim_stack.  Each opaque state will be in the free states list
> (grnd_alloc.states) or allocated to a running thread.
> 
> The cancellation is handled by always using GRND_NONBLOCK flags while
> calling the vDSO, and falling back to the cancellable syscall if the
> kernel returns EAGAIN (would block).   Since getrandom is not defined
> by POSIX and cancellation is supported as an extension, the cancellation
> is handled as 'may occur' instead of 'shall occur' [1], meaning that
> if vDSO does not block (the expected behavior) getrandom will not
> act as a cancellation entrypoint.  It avoids a pthread_testcancel call
> on the fast path (different than 'shall occur' functions, like sem_wait()).
> 
> It is currently enabled for x86_64. And the kernel's random.git tree has
> support pending for arm64, powerpc32, powerpc64, loongarch64, and s390x.
> 
> Checked on x86_64-linux-gnu.
> 
> Co-authored-by: Jason A. Donenfeld <Jason@zx2c4.com>
> 
> [1] https://pubs.opengroup.org/onlinepubs/9799919799/nframe.html
> 
> --
...
> diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> index a2b021bd86..7dc072ae2d 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
> @@ -376,6 +376,7 @@
>  # define HAVE_TIME_VSYSCALL             "__vdso_time"
>  # define HAVE_GETCPU_VSYSCALL		"__vdso_getcpu"
>  # define HAVE_CLOCK_GETRES64_VSYSCALL   "__vdso_clock_getres"
> +# define HAVE_GETRANDOM_VSYSCALL        "__vdso_getrandom"
>  
>  # define HAVE_CLONE3_WRAPPER			1
>  

Hi,
FYI, I've tested a kernel from random.git with s390x 64bit vdso support
(NOTE: s390 31bit does not have getrandom VDSO support):
5ad9d91baacc selftests: vDSO: check cpu caps before running chacha test
b920aa77be6d s390/vdso: Wire up getrandom() vdso implementation
c1ae1b4ef5bf s390/vdso: Move vdso symbol handling to separate header file
e10863fffe3a s390/vdso: Allow alternatives in vdso code
a919390e9194 s390/module: Provide find_section() helper
94c7755b1e8f s390/facility: Let test_facility() generate static branch
if possible
013e984397a2 s390/alternatives: Remove ALT_FACILITY_EARLY
26d4959681e3 s390/facility: Disable compile time optimization for
decompressor code

And adjusted s390x sysdep.h:
diff --git a/sysdeps/unix/sysv/linux/s390/sysdep.h
b/sysdeps/unix/sysv/linux/s390/sysdep.h
index 9b3000ca62..a5823d519f 100644
--- a/sysdeps/unix/sysv/linux/s390/sysdep.h
+++ b/sysdeps/unix/sysv/linux/s390/sysdep.h
@@ -78,6 +78,9 @@
 #endif
 #define HAVE_GETTIMEOFDAY_VSYSCALL     "__kernel_gettimeofday"
 #define HAVE_GETCPU_VSYSCALL           "__kernel_getcpu"
+#ifdef __s390x__
+# define HAVE_GETRANDOM_VSYSCALL       "__kernel_getrandom"
+#endif

 #define HAVE_CLONE3_WRAPPER            1
 #endif


The glibc testsuite runs fine and I've used gdb to step into the new
__arch_chacha20_blocks_nostack function in vdso.

According to the discussion, the s390x vdso support will be in kernel 6.12:
https://lore.kernel.org/all/ZuRS6wEa6lKOyuo9@zx2c4.com/

@Adhemerval: Do you want to include HAVE_GETRANDOM_VSYSCALL for s390x
now or later?

Bye,
Stefan


More information about the Libc-alpha mailing list