[PATCH v3 2/4] vdso: Add alarmclock support infrastructure

From: Thomas Weißschuh

Date: Tue Nov 10 2026 - 08:54:27 EST


Prepare the addition of alarmclock support to the vDSO.
These behave exactly as their non-alarm counterparts,
except that they can only be used when an RTC device is enabled.

A registered RTC device can never be unregistered, so this does not need
to be handled. The basetimes will be maintained through the regular
update_vsyscall() path. As the basetime will have been set by the time
vdso_time_enable_alarmclock() will be called no synchronization is
necessary here. An unavailable alarmclock will fall through to the
system call, similar to disabled auxiliary clocks.

The following patches will extend VDSO_ALARM with the relevant clocks.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
include/vdso/datapage.h | 3 ++-
kernel/time/alarmtimer.c | 2 ++
kernel/time/timekeeping_internal.h | 4 ++++
kernel/time/vsyscall.c | 11 +++++++++++
lib/vdso/gettimeofday.c | 12 ++++++++++++
5 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/include/vdso/datapage.h b/include/vdso/datapage.h
index 5c4dac5bdda9..89ecf9f9fce5 100644
--- a/include/vdso/datapage.h
+++ b/include/vdso/datapage.h
@@ -40,6 +40,7 @@ struct vdso_arch_data {
BIT(CLOCK_MONOTONIC_COARSE))
#define VDSO_RAW (BIT(CLOCK_MONOTONIC_RAW))
#define VDSO_AUX __GENMASK(CLOCK_AUX_LAST, CLOCK_AUX)
+#define VDSO_ALARM 0

/**
* struct vdso_timestamp - basetime per clock_id
@@ -132,7 +133,7 @@ struct vdso_time_data {
s32 tz_minuteswest;
s32 tz_dsttime;
u32 hrtimer_res;
- u32 __unused;
+ u32 alarmclock_mask;
} ____cacheline_aligned;

/**
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index ea5be5870e51..161a4b4127f8 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -29,6 +29,7 @@
#include <linux/time_namespace.h>

#include "posix-timers.h"
+#include "timekeeping_internal.h"

#define CREATE_TRACE_POINTS
#include <trace/events/alarmtimer.h>
@@ -101,6 +102,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
scoped_guard(spinlock_irqsave, &rtcdev_lock) {
if (!IS_ERR(pdev) && !rtcdev && try_module_get(rtc->owner)) {
rtcdev = rtc;
+ vdso_time_enable_alarmclock();
/* hold a reference so it doesn't go away */
get_device(dev);
pdev = NULL;
diff --git a/kernel/time/timekeeping_internal.h b/kernel/time/timekeeping_internal.h
index 6d719b8e5ea2..3ffda8af157f 100644
--- a/kernel/time/timekeeping_internal.h
+++ b/kernel/time/timekeeping_internal.h
@@ -55,6 +55,7 @@ long ktime_get_ntp_seconds(unsigned int id);
extern void update_vsyscall(struct timekeeper *tk);
extern void update_vsyscall_tz(void);
extern void vdso_time_update_aux(struct timekeeper *tk);
+extern void vdso_time_enable_alarmclock(void);

#else

@@ -67,6 +68,9 @@ static inline void update_vsyscall_tz(void)
static inline void vdso_time_update_aux(struct timekeeper *tk)
{
}
+static inline void vdso_time_enable_alarmclock(void)
+{
+}
#endif

#endif /* _TIMEKEEPING_INTERNAL_H */
diff --git a/kernel/time/vsyscall.c b/kernel/time/vsyscall.c
index 12d3518306e4..fc3313d6afa6 100644
--- a/kernel/time/vsyscall.c
+++ b/kernel/time/vsyscall.c
@@ -141,6 +141,17 @@ void update_vsyscall_tz(void)
__arch_sync_vdso_time_data(vdata);
}

+#ifdef CONFIG_RTC_CLASS
+void vdso_time_enable_alarmclock(void)
+{
+ struct vdso_time_data *vdata = vdso_k_time_data;
+
+ WRITE_ONCE(vdata->alarmclock_mask, VDSO_ALARM);
+
+ __arch_sync_vdso_time_data(vdata);
+}
+#endif
+
#ifdef CONFIG_POSIX_AUX_CLOCKS
void vdso_time_update_aux(struct timekeeper *tk)
{
diff --git a/lib/vdso/gettimeofday.c b/lib/vdso/gettimeofday.c
index 4d1d5031cf32..9fd9ac287309 100644
--- a/lib/vdso/gettimeofday.c
+++ b/lib/vdso/gettimeofday.c
@@ -109,6 +109,14 @@ static __always_inline bool vdso_clockid_valid(clockid_t clock)
return likely((u32) clock <= CLOCK_AUX_LAST);
}

+static __always_inline u32 vdso_alarmclock_mask(const struct vdso_time_data *vd)
+{
+ if (!IS_ENABLED(CONFIG_RTC_CLASS))
+ return 0;
+
+ return READ_ONCE(vd->alarmclock_mask);
+}
+
/*
* Must not be invoked within the sequence read section as a race inside
* that loop could result in __iter_div_u64_rem() being extremely slow.
@@ -305,6 +313,8 @@ __cvdso_clock_gettime_common(const struct vdso_time_data *vd, clockid_t clock,
vc = &vd->raw;
else if (msk & VDSO_AUX)
return do_aux(vd, clock, ts);
+ else if (msk & vdso_alarmclock_mask(vd))
+ vc = &vd->hres_coarse;
else
return false;

@@ -461,6 +471,8 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
ns = LOW_RES_NSEC;
} else if (msk & VDSO_AUX) {
ns = aux_clock_resolution_ns();
+ } else if (msk & vdso_alarmclock_mask(vd)) {
+ ns = READ_ONCE(vd->hrtimer_res);
} else {
return false;
}

--
2.56.0