[tip: timers/vdso] vdso: Add alarmclock support infrastructure

From: tip-bot2 for Thomas Weißschuh

Date: Mon Nov 30 2026 - 15:28:17 EST


The following commit has been merged into the timers/vdso branch of tip:

Commit-ID: 308b373c295853b367a42b6f62df0ab2007065e2
Gitweb: https://git.kernel.org/tip/308b373c295853b367a42b6f62df0ab2007065e2
Author: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
AuthorDate: Tue, 10 Nov 2026 13:19:33
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Mon, 30 Nov 2026 18:05:18 +01:00

vdso: Add alarmclock support infrastructure

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.

Subsequent changes will extend VDSO_ALARM with the relevant clocks.

[ tglx: Make it work in namespaces on a best effort basis ]

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Link: https://patch.msgid.link/20261110-vdso-alarm-clocks-v3-2-8f3c0c5b5a11@xxxxxxxxxxxxxx
---
include/vdso/datapage.h | 5 +++--
kernel/time/alarmtimer.c | 2 ++
kernel/time/namespace_vdso.c | 15 +++++++++++++++
kernel/time/timekeeping_internal.h | 4 ++++
kernel/time/vsyscall.c | 11 +++++++++++
lib/vdso/gettimeofday.c | 12 ++++++++++++
6 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/include/vdso/datapage.h b/include/vdso/datapage.h
index 49d5081..ac4172a 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
@@ -113,7 +114,7 @@ struct vdso_clock {
* @tz_minuteswest: minutes west of Greenwich
* @tz_dsttime: type of DST correction
* @hrtimer_res: hrtimer resolution
- * @__unused: unused
+ * @alarmclock_mask: Mask to enable alarmclock readout at runtime
*
* vdso_time_data will be accessed by 64 bit and compat code at the same time
* so we should be careful before modifying this structure.
@@ -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 ea5be58..161a4b4 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/namespace_vdso.c b/kernel/time/namespace_vdso.c
index 42faeac..4880bfd 100644
--- a/kernel/time/namespace_vdso.c
+++ b/kernel/time/namespace_vdso.c
@@ -68,6 +68,20 @@ static void timens_setup_vdso_clock_data(struct vdso_clock *vc,
offset[CLOCK_BOOTTIME_ALARM] = boottime;
}

+/*
+ * Best effort attempt to provide VDSO support for CLOCK_ALARM_* for
+ * namespaces. This can only be initialized when the namespace is set up. If
+ * that happens before a RTC is registered with alarm timers, then this
+ * namespace will fall back to the syscall even when a RTC becomes available
+ * later.
+ */
+static void timens_setup_vdso_alarmmask(struct vdso_time_data *ns_vdata)
+{
+ struct vdso_time_data *vdata = vdso_k_time_data;
+
+ ns_vdata->alarmclock_mask = READ_ONCE(vdata->alarmclock_mask);
+}
+
struct page *find_timens_vvar_page(struct vm_area_struct *vma)
{
if (likely(vma->vm_mm == current->mm))
@@ -104,6 +118,7 @@ static void timens_set_vvar_page(struct task_struct *task,
ns->frozen_offsets = true;
vdata = page_address(ns->vvar_page);

+ timens_setup_vdso_alarmmask(vdata);
timens_setup_vdso_clock_data(&vdata->hres_coarse, ns);
timens_setup_vdso_clock_data(&vdata->raw, ns);

diff --git a/kernel/time/timekeeping_internal.h b/kernel/time/timekeeping_internal.h
index 6d719b8..3ffda8a 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 12d3518..fc3313d 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 4d1d503..9fd9ac2 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;
}