[PATCH 02/10] selftests: vDSO: Introduce vdso_types.h

Thomas Weißschuh posted 10 patches 3 months ago
There is a newer version of this series
[PATCH 02/10] selftests: vDSO: Introduce vdso_types.h
Posted by Thomas Weißschuh 3 months ago
Currently the vDSO selftests use the time-related types from libc.
This works on glibc by chance today but will break with other libc
implementations or on distributions which switch to 64-bit times
everywhere.

The kernel's UAPI headers provide the proper types to use with the vDSO
(and raw syscalls) but are not necessarily compatible with libc types.
Introduce a new header which makes the UAPI headers compatible with the
libc.

Tested with glibc, musl and nolibc.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 tools/testing/selftests/vDSO/vdso_types.h | 70 +++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/tools/testing/selftests/vDSO/vdso_types.h b/tools/testing/selftests/vDSO/vdso_types.h
new file mode 100644
index 0000000000000000000000000000000000000000..a1f2f5412e813d5ba6e57a87e28b9eacc68cccac
--- /dev/null
+++ b/tools/testing/selftests/vDSO/vdso_types.h
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2025 Thomas Weißschuh <thomas.weissschuh@linutronix.de>, Linutronix GmbH
+ *
+ * Types to use with vDSO functions.
+ *
+ * The types used by the vDSO functions do not necessarily match the ones used
+ * by libc. The kernel's UAPI headers do provide definitions for those types
+ * but are often not compatible with libc which applications need to use.
+ * As the types differ between platforms listing them manually is a lot of work
+ * and error prone. Instead hack around the incompatibilities of the libc and
+ * UAPI headers and use the UAPI types.
+ */
+#ifndef __VDSO_TYPES_H__
+#define __VDSO_TYPES_H__
+
+/*
+ * Include the headers from libc first to not override any of its types later.
+ */
+#include <time.h>
+#include <sys/time.h>
+
+/*
+ * Avoid collisions.
+ */
+#define timeval kernel_timeval_moved
+#define itimerspec kernel_itimerspec_moved
+#define itimerval kernel_itimerval_moved
+#define timezone kernel_timezone_moved
+
+/*
+ * Get the UAPI types.
+ */
+#include <linux/time.h>
+
+#undef timeval
+#undef itimerspec
+#undef itimerval
+#undef timezone
+
+/*
+ * The UAPI headers do not provide their own 'struct __kernel_timezone'.
+ * 'struct timezone' is the one from libc.
+ */
+struct kernel_timezone {
+	int	tz_minuteswest;
+	int	tz_dsttime;
+};
+
+#include <linux/version.h>
+
+/*
+ * UAPI headers from the libc may be older and not provide these.
+ */
+#if KERNEL_VERSION(5, 5, 0) > LINUX_VERSION_CODE
+typedef __kernel_long_t		__kernel_old_time_t;
+
+struct __kernel_old_timespec {
+	__kernel_old_time_t	tv_sec;
+	long			tv_nsec;
+};
+#endif
+
+typedef long (*vdso_gettimeofday_t)(struct __kernel_old_timeval *tv, struct kernel_timezone *tz);
+typedef long (*vdso_clock_gettime_t)(__kernel_clockid_t clk_id, struct __kernel_old_timespec *ts);
+typedef long (*vdso_clock_gettime64_t)(__kernel_clockid_t clk_id, struct __kernel_timespec *ts);
+typedef long (*vdso_clock_getres_t)(__kernel_clockid_t clk_id, struct __kernel_old_timespec *ts);
+typedef __kernel_time_t (*vdso_time_t)(__kernel_time_t *t);
+
+#endif /* __VDSO_TYPES_H__ */

-- 
2.51.0

Re: [PATCH 02/10] selftests: vDSO: Introduce vdso_types.h
Posted by Arnd Bergmann 3 months ago
On Tue, Nov 11, 2025, at 11:49, Thomas Weißschuh wrote:
> +/*
> + * UAPI headers from the libc may be older and not provide these.
> + */
> +#if KERNEL_VERSION(5, 5, 0) > LINUX_VERSION_CODE
> +typedef __kernel_long_t		__kernel_old_time_t;
> +
> +struct __kernel_old_timespec {
> +	__kernel_old_time_t	tv_sec;
> +	long			tv_nsec;
> +};
> +#endif

Doesn't this also need to define __kernel_old_timeval, which you
refer to below?

> +typedef __kernel_time_t (*vdso_time_t)(__kernel_time_t *t);

This I think needs to be __kernel_old_time_t instead of __kernel_time_t.

       Arnd
Re: [PATCH 02/10] selftests: vDSO: Introduce vdso_types.h
Posted by Thomas Weißschuh 3 months ago
On Tue, Nov 11, 2025 at 01:45:56PM +0100, Arnd Bergmann wrote:
> On Tue, Nov 11, 2025, at 11:49, Thomas Weißschuh wrote:
> > +/*
> > + * UAPI headers from the libc may be older and not provide these.
> > + */
> > +#if KERNEL_VERSION(5, 5, 0) > LINUX_VERSION_CODE
> > +typedef __kernel_long_t		__kernel_old_time_t;
> > +
> > +struct __kernel_old_timespec {
> > +	__kernel_old_time_t	tv_sec;
> > +	long			tv_nsec;
> > +};
> > +#endif
> 
> Doesn't this also need to define __kernel_old_timeval, which you
> refer to below?

It was added in 4.17, so it is present in the 4.19.88 headers my musl
ships with. I can add it for completeness.

> > +typedef __kernel_time_t (*vdso_time_t)(__kernel_time_t *t);
> 
> This I think needs to be __kernel_old_time_t instead of __kernel_time_t.

Indeed, thanks. The only user assigns the return value to a 'long' and
doesn't use the pointer argument. So for now it doesn't matter.
I'll add a proper test for it.


Thomas
Re: [PATCH 02/10] selftests: vDSO: Introduce vdso_types.h
Posted by Thomas Weißschuh 3 months ago
On Tue, Nov 11, 2025 at 02:47:34PM +0100, Thomas Weißschuh wrote:
> On Tue, Nov 11, 2025 at 01:45:56PM +0100, Arnd Bergmann wrote:
> > On Tue, Nov 11, 2025, at 11:49, Thomas Weißschuh wrote:
> > > +/*
> > > + * UAPI headers from the libc may be older and not provide these.
> > > + */
> > > +#if KERNEL_VERSION(5, 5, 0) > LINUX_VERSION_CODE
> > > +typedef __kernel_long_t		__kernel_old_time_t;
> > > +
> > > +struct __kernel_old_timespec {
> > > +	__kernel_old_time_t	tv_sec;
> > > +	long			tv_nsec;
> > > +};
> > > +#endif
> > 
> > Doesn't this also need to define __kernel_old_timeval, which you
> > refer to below?
> 
> It was added in 4.17, so it is present in the 4.19.88 headers my musl
> ships with. I can add it for completeness.

Given that __kernel_old_timeval has an architecture-specific override on SPARC,
I'd rather leave out a custom fallback until we really need one.

(...)


Thomas