timekeeping: Provide support for auxiliary clocks - Remaining series
Subsystems (4)
v3 cover timekeeping: Provide support for auxiliary clocks - Remaining series
This is the remaining series of V2, which can be found here:
https://lore.kernel.org/all/[email protected]
The first 15 patches of V2 have been merged into the tip tree and this is the update of the remaining 11, which addresses the review feedback.
Changes vs. V2:
- Use kobject.h, clockid_t and cleanup the sysfs init - Thomas W.
- Use aux_tkd, aux_tks instead of tkd, tks in aux clock specific functions for clarity - John
- Remove misleading clock TAI comment - John
The series applies on top of:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git timers/ptp
and is available from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git timers/ptp/clocks
A small update for PTP to support auxiliary clocks in PTP_SYS_OFFSET_EXTENDED is going to be sent seperately as it has a dependency on pending PTP changes.
Thanks,
tglx
--- Documentation/ABI/stable/sysfs-kernel-time-aux-clocks | 5 include/linux/posix-timers.h | 5 include/linux/timekeeping.h | 11 kernel/time/posix-timers.c | 3 kernel/time/posix-timers.h | 1 kernel/time/timekeeping.c | 484 +++++++++++++++--- 6 files changed, 451 insertions(+), 58 deletions(-)
PATCH v3 1/11 timekeeping: Update auxiliary timekeepers on clocksource change
Propagate a system clocksource change to the auxiliary timekeepers so that they can pick up the new clocksource.
--- kernel/time/timekeeping.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
---
kernel/time/timekeeping.c +28 -0
@@ -119,8 +119,10 @@ static struct tk_fast tk_fast_raw ____c#ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup (void ); +static void tk_aux_update_clocksource (void );#else static inline void tk_aux_setup (void ) { } +static inline void tk_aux_update_clocksource (void ) { }#endif unsigned long timekeeper_lock_irqsave (void )
@@ -1548,6 +1550,8 @@ static int change_clocksource(void *datatimekeeping_update_from_shadow (&tk_core,TK_UPDATE_ALL ); } +tk_aux_update_clocksource (); +if (old) {if (old->disable) old->disable (old);
@@ -2651,6 +2655,30 @@ EXPORT_SYMBOL(hardpps);#endif /* CONFIG_NTP_PPS */ #ifdef CONFIG_POSIX_AUX_CLOCKS + +/* Bitmap for the activated auxiliary timekeepers */ +static unsigned long aux_timekeepers; + +/* Invoked from timekeeping after a clocksource change */ +static void tk_aux_update_clocksource (void ) +{ +unsigned long active =READ_ONCE (aux_timekeepers); +unsigned int id; + +for_each_set_bit (id, &active,BITS_PER_LONG ) { +struct tk_data *tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ]; +struct timekeeper *tks = &tkd->shadow_timekeeper; + +guard (raw_spinlock_irqsave)(&tkd->lock); +if (!tks->clock_valid) +continue ; + +timekeeping_forward_now (tks); +tk_setup_internals (tks, tk_core.timekeeper.tkr_mono.clock); +timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL ); + } +} +static __init void tk_aux_setup (void ) {for (int i =TIMEKEEPER_AUX_FIRST ; i <=TIMEKEEPER_AUX_LAST ; i++)
Re: [patch V3 01/11] timekeeping: Update auxiliary timekeepers on clocksource change
Quoted 33 lines · Thomas Gleixner
Propagate a system clocksource change to the auxiliary timekeepers so that they can pick up the new clocksource.
Signed-off-by Thomas Gleixner <[email protected]>--- kernel/time/timekeeping.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)---
kernel/time/timekeeping.c +4
-0@@ -119,8 +119,10 @@ static struct tk_fast tk_fast_raw ____c#ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup (void ); +static void tk_aux_update_clocksource (void );#else static inline void tk_aux_setup (void ) { } +static inline void tk_aux_update_clocksource (void ) { }#endif unsigned long timekeeper_lock_irqsave (void )@@ -1548,6 +1550,8 @@ static int change_clocksource(void *datatimekeeping_update_from_shadow (&tk_core,TK_UPDATE_ALL ); } +tk_aux_update_clocksource (); +if (old) {if (old->disable) old->disable (old);
kernel/time/timekeeping.c EXPORT_SYMBOL(hardpps); · line 2661
Quoted 7 lines
kernel/time/timekeeping.c +4
-0@@ -2651,6 +2655,30 @@ EXPORT_SYMBOL(hardpps);#endif /* CONFIG_NTP_PPS */ #ifdef CONFIG_POSIX_AUX_CLOCKS + +/* Bitmap for the activated auxiliary timekeepers */ +static unsigned long aux_timekeepers; +
Nit: Would it be useful to clarify this is accessed without locks, and the related tks->clock_valid *must* be checked while holding the lock before using a timekeeper that is considered activated in the aux_timekeepers bitmap?
Otherwise,
Re: [patch V3 01/11] timekeeping: Update auxiliary timekeepers on clocksource change
Quoted 8 lines · John Stultz
kernel/time/timekeeping.c EXPORT_SYMBOL(hardpps); · line 2661
Quoted 3 lines
kernel/time/timekeeping.c +3
-0+/* Bitmap for the activated auxiliary timekeepers */ +static unsigned long aux_timekeepers; +Nit: Would it be useful to clarify this is accessed without locks, and the related
tks->clock_valid*must* be checked while holding the lock before using a timekeeper that is considered activated in theaux_timekeepersbitmap?
I'll amend all your nits when applying the pile.
Thanks,
tglx
[tip: timers/ptp] timekeeping: Update auxiliary timekeepers on clocksource change
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 9f7729480a2c771bbe49b7eab034a8eaa5e27bfb Gitweb: https://git.kernel.org/tip/9f7729480a2c771bbe49b7eab034a8eaa5e27bfb Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:29 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:12 +02:00
timekeeping: Update auxiliary timekeepers on clocksource change
Propagate a system clocksource change to the auxiliary timekeepers so that they can pick up the new clocksource.
--- kernel/time/timekeeping.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
kernel/time/timekeeping.c +33 -0
@@ -119,8 +119,10 @@ static struct tk_fast tk_fast_raw ____cacheline_aligned = {#ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup (void ); +static void tk_aux_update_clocksource (void );#else static inline void tk_aux_setup (void ) { } +static inline void tk_aux_update_clocksource (void ) { }#endif unsigned long timekeeper_lock_irqsave (void )
@@ -1548,6 +1550,8 @@ static int change_clocksource(void *data)timekeeping_update_from_shadow (&tk_core,TK_UPDATE_ALL ); } +tk_aux_update_clocksource (); +if (old) {if (old->disable) old->disable (old);
@@ -2651,6 +2655,35 @@ EXPORT_SYMBOL(hardpps);#endif /* CONFIG_NTP_PPS */ #ifdef CONFIG_POSIX_AUX_CLOCKS + +/* +* Bitmap for the activated auxiliary timekeepers to allow lockless quick +* checks in the hot paths without touching extra cache lines. If set, then +* the state of the corresponding timekeeper has to be re-checked under +* timekeeper::lock. +*/ +static unsigned long aux_timekeepers; + +/* Invoked from timekeeping after a clocksource change */ +static void tk_aux_update_clocksource (void ) +{ +unsigned long active =READ_ONCE (aux_timekeepers); +unsigned int id; + +for_each_set_bit (id, &active,BITS_PER_LONG ) { +struct tk_data *tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ]; +struct timekeeper *tks = &tkd->shadow_timekeeper; + +guard (raw_spinlock_irqsave)(&tkd->lock); +if (!tks->clock_valid) +continue ; + +timekeeping_forward_now (tks); +tk_setup_internals (tks, tk_core.timekeeper.tkr_mono.clock); +timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL ); + } +} +static __init void tk_aux_setup (void ) {for (int i =TIMEKEEPER_AUX_FIRST ; i <=TIMEKEEPER_AUX_LAST ; i++)
PATCH v3 2/11 timekeeping: Provide time getters for auxiliary clocks
In reply to Thomas GleixnerProvide interfaces similar to the ktime_get*() family which provide access to the auxiliary clocks.
These interfaces have a boolean return value, which indicates whether the accessed clock is valid or not.
---
V3: Remove misleading TAI comment and use aux_tkd* for clarity - John
--- include/linux/posix-timers.h | 5 +++ include/linux/timekeeping.h | 11 +++++++ kernel/time/timekeeping.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+)
---
include/linux/posix-timers.h +5 -0
@@ -37,6 +37,11 @@ static inline int clockid_to_fd(const clreturn ~(clk >>3 ); } +static inline bool clockid_aux_valid (clockid_t id) +{ +return IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) && id >=CLOCK_AUX && id <=CLOCK_AUX_LAST ; +} +#ifdef CONFIG_POSIX_TIMERS #include <linux/signal_types.h>
include/linux/timekeeping.h +11 -0
@@ -263,6 +263,17 @@ extern bool timekeeping_rtc_skipresume(vextern void timekeeping_inject_sleeptime64 (const struct timespec64 *delta); +/* +* Auxiliary clock interfaces +*/ +#ifdef CONFIG_POSIX_AUX_CLOCKS +extern bool ktime_get_aux (clockid_t id,ktime_t *kt); +extern bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt); +#else +static inline bool ktime_get_aux (clockid_t id,ktime_t *kt) {return false; } +static inline bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt) {return false; } +#endif +/** * struct system_time_snapshot - simultaneous raw/real time capture with * counter value
kernel/time/timekeeping.c +65 -0
77 lines not on this page · raw message
Re: [patch V3 02/11] timekeeping: Provide time getters for auxiliary clocks
Quoted 8 lines · Thomas Gleixner
Provide interfaces similar to the ktime_get*() family which provide access to the auxiliary clocks.
These interfaces have a boolean return value, which indicates whether the accessed clock is valid or not.
Signed-off-by Thomas Gleixner <[email protected]>
Quoted 8 lines
---
V3: Remove misleading TAI comment and use aux_tkd* for clarity - John--- include/linux/posix-timers.h | 5 +++ include/linux/timekeeping.h | 11 +++++++ kernel/time/timekeeping.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+)---
[tip: timers/ptp] timekeeping: Provide time getters for auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 05bc6e6290f91d2d40086ab4ef52da21c14ec4b6 Gitweb: https://git.kernel.org/tip/05bc6e6290f91d2d40086ab4ef52da21c14ec4b6 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:31 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:12 +02:00
timekeeping: Provide time getters for auxiliary clocks
Provide interfaces similar to the ktime_get*() family which provide access to the auxiliary clocks.
These interfaces have a boolean return value, which indicates whether the accessed clock is valid or not.
--- include/linux/posix-timers.h | 5 +++- include/linux/timekeeping.h | 11 ++++++- kernel/time/timekeeping.c | 65 +++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+)
include/linux/posix-timers.h +5 -0
@@ -37,6 +37,11 @@ static inline int clockid_to_fd(const clockid_t clk)return ~(clk >>3 ); } +static inline bool clockid_aux_valid (clockid_t id) +{ +return IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) && id >=CLOCK_AUX && id <=CLOCK_AUX_LAST ; +} +#ifdef CONFIG_POSIX_TIMERS #include <linux/signal_types.h>
include/linux/timekeeping.h +11 -0
@@ -263,6 +263,17 @@ extern bool timekeeping_rtc_skipresume(void);extern void timekeeping_inject_sleeptime64 (const struct timespec64 *delta); +/* +* Auxiliary clock interfaces +*/ +#ifdef CONFIG_POSIX_AUX_CLOCKS +extern bool ktime_get_aux (clockid_t id,ktime_t *kt); +extern bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt); +#else +static inline bool ktime_get_aux (clockid_t id,ktime_t *kt) {return false; } +static inline bool ktime_get_aux_ts64 (clockid_t id,struct timespec64 *kt) {return false; } +#endif +/** * struct system_time_snapshot - simultaneous raw/real time capture with * counter value
kernel/time/timekeeping.c +65 -0
77 lines not on this page · raw message
PATCH v3 3/11 timekeeping: Add minimal posix-timers support for auxiliary clocks
In reply to Thomas GleixnerProvide clock_getres(2) and clock_gettime(2) for auxiliary clocks.
--- kernel/time/posix-timers.c | 3 +++ kernel/time/posix-timers.h | 1 + kernel/time/timekeeping.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+)
---
kernel/time/posix-timers.c +3 -0
@@ -1526,6 +1526,9 @@ static const struct k_clock * const posi [CLOCK_REALTIME_ALARM] = &alarm_clock, [CLOCK_BOOTTIME_ALARM] = &alarm_clock, [CLOCK_TAI ] = &clock_tai, +#ifdef CONFIG_POSIX_AUX_CLOCKS + [CLOCK_AUX ... CLOCK_AUX_LAST] = &clock_aux, +#endif };static const struct k_clock *clockid_to_kclock (const clockid_t id)
kernel/time/posix-timers.h +1 -0
@@ -41,6 +41,7 @@ extern const struct k_clock clock_posix_extern const struct k_clock clock_process;extern const struct k_clock clock_thread;extern const struct k_clock alarm_clock; +extern const struct k_clock clock_aux;void posix_timer_queue_signal (struct k_itimer *timr);
kernel/time/timekeeping.c +21 -0
@@ -2655,6 +2655,7 @@ EXPORT_SYMBOL(hardpps);#endif /* CONFIG_NTP_PPS */ #ifdef CONFIG_POSIX_AUX_CLOCKS +#include "posix-timers.h" /* Bitmap for the activated auxiliary timekeepers */ static unsigned long aux_timekeepers;
@@ -2744,6 +2745,26 @@ bool ktime_get_aux_ts64(clockid_t id, st }EXPORT_SYMBOL_GPL (ktime_get_aux_ts64); +static int aux_get_res (clockid_t id,struct timespec64 *tp) +{ +if (!clockid_aux_valid (id)) +return -ENODEV ; + + tp->tv_sec =0 ; + tp->tv_nsec =1 ; +return 0 ; +} + +static int aux_get_timespec (clockid_t id,struct timespec64 *tp) +{ +return ktime_get_aux_ts64 (id, tp) ?0 : -ENODEV ; +} + +const struct k_clock clock_aux = { + .clock_getres = aux_get_res, + .clock_get_timespec = aux_get_timespec, +}; +static __init void tk_aux_setup (void ) {for (int i =TIMEKEEPER_AUX_FIRST ; i <=TIMEKEEPER_AUX_LAST ; i++)
Re: [patch V3 03/11] timekeeping: Add minimal posix-timers support for auxiliary clocks
Quoted 5 lines · Thomas Gleixner
Provide
clock_getres(2)andclock_gettime(2)for auxiliary clocks.Signed-off-by Thomas Gleixner <[email protected]>---
[tip: timers/ptp] timekeeping: Add minimal posix-timers support for auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 606424bf4ffd9d27865c45b5707c1edac6b187ed Gitweb: https://git.kernel.org/tip/606424bf4ffd9d27865c45b5707c1edac6b187ed Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:32 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:12 +02:00
timekeeping: Add minimal posix-timers support for auxiliary clocks
Provide clock_getres(2) and clock_gettime(2) for auxiliary clocks.
--- kernel/time/posix-timers.c | 3 +++ kernel/time/posix-timers.h | 1 + kernel/time/timekeeping.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+)
kernel/time/posix-timers.c +3 -0
@@ -1526,6 +1526,9 @@ static const struct k_clock * const posix_clocks[] = { [CLOCK_REALTIME_ALARM] = &alarm_clock, [CLOCK_BOOTTIME_ALARM] = &alarm_clock, [CLOCK_TAI ] = &clock_tai, +#ifdef CONFIG_POSIX_AUX_CLOCKS + [CLOCK_AUX ... CLOCK_AUX_LAST] = &clock_aux, +#endif };static const struct k_clock *clockid_to_kclock (const clockid_t id)
kernel/time/posix-timers.h +1 -0
@@ -41,6 +41,7 @@ extern const struct k_clock clock_posix_dynamic;extern const struct k_clock clock_process;extern const struct k_clock clock_thread;extern const struct k_clock alarm_clock; +extern const struct k_clock clock_aux;void posix_timer_queue_signal (struct k_itimer *timr);
kernel/time/timekeeping.c +21 -0
@@ -2655,6 +2655,7 @@ EXPORT_SYMBOL(hardpps);#endif /* CONFIG_NTP_PPS */ #ifdef CONFIG_POSIX_AUX_CLOCKS +#include "posix-timers.h" /* * Bitmap for the activated auxiliary timekeepers to allow lockless quick
@@ -2749,6 +2750,26 @@ bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *ts) }EXPORT_SYMBOL_GPL (ktime_get_aux_ts64); +static int aux_get_res (clockid_t id,struct timespec64 *tp) +{ +if (!clockid_aux_valid (id)) +return -ENODEV ; + + tp->tv_sec =0 ; + tp->tv_nsec =1 ; +return 0 ; +} + +static int aux_get_timespec (clockid_t id,struct timespec64 *tp) +{ +return ktime_get_aux_ts64 (id, tp) ?0 : -ENODEV ; +} + +const struct k_clock clock_aux = { + .clock_getres = aux_get_res, + .clock_get_timespec = aux_get_timespec, +}; +static __init void tk_aux_setup (void ) {for (int i =TIMEKEEPER_AUX_FIRST ; i <=TIMEKEEPER_AUX_LAST ; i++)
PATCH v3 4/11 timekeeping: Provide time setter for auxiliary clocks
In reply to Thomas GleixnerAdd clock_settime(2) support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks.
--- kernel/time/timekeeping.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
---
kernel/time/timekeeping.c +39 -0
@@ -2757,9 +2757,48 @@ static int aux_get_timespec(clockid_t idreturn ktime_get_aux_ts64 (id, tp) ?0 : -ENODEV ; } +static int aux_clock_set (const clockid_t id,const struct timespec64 *tnew) +{ +struct tk_data *tkd =aux_get_tk_data (id); +struct timekeeper *tks; +ktime_t tnow, nsecs; + +if (!timespec64_valid_settod (tnew)) +return -EINVAL ; +if (!tkd) +return -ENODEV ; + + tks = &tkd->shadow_timekeeper; + +guard (raw_spinlock_irq)(&tkd->lock); +if (!tks->clock_valid) +return -ENODEV ; + +/* Forward the timekeeper base time */ +timekeeping_forward_now (tks); +/* +* Get the updated base time. tkr_mono.base has not been +* updated yet, so do that first. That makes the update +* in timekeeping_update_from_shadow() redundant, but +* that's harmless. After that @tnow can be calculated +* by using tkr_mono::cycle_last, which has been set +* by timekeeping_forward_now(). +*/ +tk_update_ktime_data (tks); + nsecs =timekeeping_cycles_to_ns (&tks->tkr_mono, tks->tkr_mono.cycle_last); + tnow =ktime_add (tks->tkr_mono.base, nsecs); + +/* Calculate the new AUX offset */ + tks->offs_aux =ktime_sub (timespec64_to_ktime (*tnew), tnow); + +timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL ); +return 0 ; +} +const struct k_clock clock_aux = { .clock_getres = aux_get_res, .clock_get_timespec = aux_get_timespec, + .clock_set = aux_clock_set, };static __init void tk_aux_setup (void )
Re: [patch V3 04/11] timekeeping: Provide time setter for auxiliary clocks
Quoted 6 lines · Thomas Gleixner
Add
clock_settime(2)support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks.Signed-off-by Thomas Gleixner <[email protected]>---
Minor fretting: I worry a little that the difference here between the default timekeeper where set adjusts the REALTIME offset from MONOTONIC, and here where it directly adjusts "mono" might confuse later readers?
Would something about that be useful to have in a comment?
Either way,
Re: [patch V3 04/11] timekeeping: Provide time setter for auxiliary clocks
Quoted 5 lines · John Stultz
Quoted 4 lines · Thomas Gleixner
Add
clock_settime(2)support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Thomas Gleixner <[email protected]>---
Minor fretting: I worry a little that the difference here between the default timekeeper where set adjusts the REALTIME offset from MONOTONIC, and here where it directly adjusts "mono" might confuse later readers?
Actually it's not really that different.
In both cases the new offset to the monotonic clock is calculated and stored in the relevant tk::offs_* member.
The difference is that the core timekeeper operates on xtime, but for simplicity I chose to calculate the resulting tk::offs_aux directly from the monotonic base. That's valid with the aux clocks as they don't need any of the xtime parts.
I added some blurb to it.
Re: [patch V3 04/11] timekeeping: Provide time setter for auxiliary clocks
Quoted 16 lines · Thomas Gleixner
Quoted 5 lines · John Stultz
Quoted 4 lines · Thomas Gleixner
Add
clock_settime(2)support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks.Quoted 7 lines
Quoted 2 lines
Signed-off-by Thomas Gleixner <[email protected]>---
Minor fretting: I worry a little that the difference here between the default timekeeper where set adjusts the REALTIME offset from MONOTONIC, and here where it directly adjusts "mono" might confuse later readers?
Actually it's not really that different.
Quoted 7 lines
In both cases the new offset to the monotonic clock is calculated and stored in the relevant tk::offs_* member.
The difference is that the core timekeeper operates on xtime, but for simplicity I chose to calculate the resulting
tk::offs_auxdirectly from the monotonic base. That's valid with the aux clocks as they don't need any of the xtime parts.
Actually using xtime and adjusting it and xtime_to_mono does not work for those auxiliary clocks because the offset to clock "monotonic" is allowed to be negative, unless it would result in a overall negative time readout.
This is required for those clocks because the TSN/PTP zoo out there especially in automation/automotive uses clockmasters starting at the epoch for their specialized networks. So if the clockmaster starts up _after_ the client, then the pile of xtime sanity checks would prevent setting the clock back to the epoch.
I tried to work around that, but the result was more horrible and fragile than the current approach with the aux specific offset.
Don't ask :)
Thanks,
tglx
[tip: timers/ptp] timekeeping: Provide time setter for auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 60ecc26ec5af567a55f362ad92c0cac8b894541c Gitweb: https://git.kernel.org/tip/60ecc26ec5af567a55f362ad92c0cac8b894541c Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:34 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:12 +02:00
timekeeping: Provide time setter for auxiliary clocks
Add clock_settime(2) support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks.
--- kernel/time/timekeeping.c | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+)
kernel/time/timekeeping.c +44 -0
@@ -2765,9 +2765,53 @@ static int aux_get_timespec(clockid_t id, struct timespec64 *tp)return ktime_get_aux_ts64 (id, tp) ?0 : -ENODEV ; } +static int aux_clock_set (const clockid_t id,const struct timespec64 *tnew) +{ +struct tk_data *aux_tkd =aux_get_tk_data (id); +struct timekeeper *aux_tks; +ktime_t tnow, nsecs; + +if (!timespec64_valid_settod (tnew)) +return -EINVAL ; +if (!aux_tkd) +return -ENODEV ; + + aux_tks = &aux_tkd->shadow_timekeeper; + +guard (raw_spinlock_irq)(&aux_tkd->lock); +if (!aux_tks->clock_valid) +return -ENODEV ; + +/* Forward the timekeeper base time */ +timekeeping_forward_now (aux_tks); +/* +* Get the updated base time. tkr_mono.base has not been +* updated yet, so do that first. That makes the update +* in timekeeping_update_from_shadow() redundant, but +* that's harmless. After that @tnow can be calculated +* by using tkr_mono::cycle_last, which has been set +* by timekeeping_forward_now(). +*/ +tk_update_ktime_data (aux_tks); + nsecs =timekeeping_cycles_to_ns (&aux_tks->tkr_mono, aux_tks->tkr_mono.cycle_last); + tnow =ktime_add (aux_tks->tkr_mono.base, nsecs); + +/* +* Calculate the new AUX offset as delta to @tnow ("monotonic"). +* That avoids all the tk::xtime back and forth conversions as +* xtime ("realtime") is not applicable for auxiliary clocks and +* kept in sync with "monotonic". +*/ + aux_tks->offs_aux =ktime_sub (timespec64_to_ktime (*tnew), tnow); + +timekeeping_update_from_shadow (aux_tkd,TK_UPDATE_ALL ); +return 0 ; +} +const struct k_clock clock_aux = { .clock_getres = aux_get_res, .clock_get_timespec = aux_get_timespec, + .clock_set = aux_clock_set, };static __init void tk_aux_setup (void )
PATCH v3 5/11 timekeeping: Make timekeeping_inject_offset() reusable
In reply to Thomas GleixnerSplit out the inner workings for auxiliary clock support and feed the core time keeper into it.
--- kernel/time/timekeeping.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
---
kernel/time/timekeeping.c +15 -11
@@ -1433,32 +1433,32 @@ EXPORT_SYMBOL(do_settimeofday64);/** * __timekeeping_inject_offset - Adds or subtracts from the current time. +* @tkd: Pointer to the timekeeper to modify * @ts: Pointer to the timespec variable containing the offset * * Adds or subtracts an offset value from the current time. */ -static int __timekeeping_inject_offset (const struct timespec64 *ts) +static int __timekeeping_inject_offset (struct tk_data *tkd,const struct timespec64 *ts) { -struct timekeeper *tks = &tk_core.shadow_timekeeper; +struct timekeeper *tks = &tkd->shadow_timekeeper;struct timespec64 tmp;if (ts->tv_nsec <0 || ts->tv_nsec >=NSEC_PER_SEC )return -EINVAL ; -timekeeping_forward_now (tks);/* Make sure the proposed value is valid */ tmp =timespec64_add (tk_xtime (tks), *ts);if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || !timespec64_valid_settod (&tmp)) { -timekeeping_restore_shadow (&tk_core); +timekeeping_restore_shadow (tkd);return -EINVAL ; }tk_xtime_add (tks, ts);tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts)); -timekeeping_update_from_shadow (&tk_core,TK_UPDATE_ALL ); +timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL );return 0 ; }
@@ -1467,7 +1467,7 @@ static int timekeeping_inject_offset(conint ret;scoped_guard (raw_spinlock_irqsave, &tk_core.lock) - ret = __timekeeping_inject_offset(ts); + ret = __timekeeping_inject_offset(&tk_core, ts);/* Signal hrtimers about time change */ if (!ret)
@@ -2568,6 +2568,7 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fal*/ int do_adjtimex (struct __kernel_timex *txc) { +struct tk_data *tkd = &tk_core;struct timespec64 delta, ts;struct audit_ntp_data ad;bool offset_set = false;
@@ -2585,16 +2586,19 @@ int do_adjtimex(struct __kernel_timex *tktime_get_real_ts64 (&ts);add_device_randomness (&ts,sizeof (ts)); - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { -struct timekeeper *tks = &tk_core.shadow_timekeeper; + scoped_guard (raw_spinlock_irqsave, &tkd->lock) { +struct timekeeper *tks = &tkd->shadow_timekeeper;s32 orig_tai, tai; +if (!tks->clock_valid) +return -ENODEV ; +if (txc->modes &ADJ_SETOFFSET ) { delta.tv_sec = txc->time.tv_sec; delta.tv_nsec = txc->time.tv_usec;if (!(txc->modes &ADJ_NANO )) delta.tv_nsec *=1000 ; - ret =__timekeeping_inject_offset (&delta); + ret =__timekeeping_inject_offset (tkd, &delta);if (ret)return ret;
@@ -2607,7 +2611,7 @@ int do_adjtimex(struct __kernel_timex *tif (tai != orig_tai) {__timekeeping_set_tai_offset (tks, tai); -timekeeping_update_from_shadow (&tk_core,TK_CLOCK_WAS_SET ); +timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); clock_set = true; }else {tk_update_leap_state_all (&tk_core);
@@ -2615,7 +2619,7 @@ int do_adjtimex(struct __kernel_timex *t/* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) - clock_set |=__timekeeping_advance (&tk_core,TK_ADV_FREQ ); + clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); }if (txc->modes &ADJ_SETOFFSET )
Re: [patch V3 05/11] timekeeping: Make timekeeping_inject_offset() reusable
Quoted 5 lines · Thomas Gleixner
Split out the inner workings for auxiliary clock support and feed the core time keeper into it.
Signed-off-by Thomas Gleixner <[email protected]>
Looks ok to me.
[tip: timers/ptp] timekeeping: Make timekeeping_inject_offset() reusable
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: e8db3a55798d70f2c222c6103990776fca6a6ebc Gitweb: https://git.kernel.org/tip/e8db3a55798d70f2c222c6103990776fca6a6ebc Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:40 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Make timekeeping_inject_offset() reusable
Split out the inner workings for auxiliary clock support and feed the core time keeper into it.
--- kernel/time/timekeeping.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
kernel/time/timekeeping.c +15 -11
@@ -1433,32 +1433,32 @@ EXPORT_SYMBOL(do_settimeofday64);/** * __timekeeping_inject_offset - Adds or subtracts from the current time. +* @tkd: Pointer to the timekeeper to modify * @ts: Pointer to the timespec variable containing the offset * * Adds or subtracts an offset value from the current time. */ -static int __timekeeping_inject_offset (const struct timespec64 *ts) +static int __timekeeping_inject_offset (struct tk_data *tkd,const struct timespec64 *ts) { -struct timekeeper *tks = &tk_core.shadow_timekeeper; +struct timekeeper *tks = &tkd->shadow_timekeeper;struct timespec64 tmp;if (ts->tv_nsec <0 || ts->tv_nsec >=NSEC_PER_SEC )return -EINVAL ; -timekeeping_forward_now (tks);/* Make sure the proposed value is valid */ tmp =timespec64_add (tk_xtime (tks), *ts);if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || !timespec64_valid_settod (&tmp)) { -timekeeping_restore_shadow (&tk_core); +timekeeping_restore_shadow (tkd);return -EINVAL ; }tk_xtime_add (tks, ts);tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts)); -timekeeping_update_from_shadow (&tk_core,TK_UPDATE_ALL ); +timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL );return 0 ; }
@@ -1467,7 +1467,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts)int ret;scoped_guard (raw_spinlock_irqsave, &tk_core.lock) - ret = __timekeeping_inject_offset(ts); + ret = __timekeeping_inject_offset(&tk_core, ts);/* Signal hrtimers about time change */ if (!ret)
@@ -2568,6 +2568,7 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fallback);*/ int do_adjtimex (struct __kernel_timex *txc) { +struct tk_data *tkd = &tk_core;struct timespec64 delta, ts;struct audit_ntp_data ad;bool offset_set = false;
@@ -2585,16 +2586,19 @@ int do_adjtimex(struct __kernel_timex *txc)ktime_get_real_ts64 (&ts);add_device_randomness (&ts,sizeof (ts)); - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { -struct timekeeper *tks = &tk_core.shadow_timekeeper; + scoped_guard (raw_spinlock_irqsave, &tkd->lock) { +struct timekeeper *tks = &tkd->shadow_timekeeper;s32 orig_tai, tai; +if (!tks->clock_valid) +return -ENODEV ; +if (txc->modes &ADJ_SETOFFSET ) { delta.tv_sec = txc->time.tv_sec; delta.tv_nsec = txc->time.tv_usec;if (!(txc->modes &ADJ_NANO )) delta.tv_nsec *=1000 ; - ret =__timekeeping_inject_offset (&delta); + ret =__timekeeping_inject_offset (tkd, &delta);if (ret)return ret;
@@ -2607,7 +2611,7 @@ int do_adjtimex(struct __kernel_timex *txc)if (tai != orig_tai) {__timekeeping_set_tai_offset (tks, tai); -timekeeping_update_from_shadow (&tk_core,TK_CLOCK_WAS_SET ); +timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); clock_set = true; }else {tk_update_leap_state_all (&tk_core);
@@ -2615,7 +2619,7 @@ int do_adjtimex(struct __kernel_timex *txc)/* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) - clock_set |=__timekeeping_advance (&tk_core,TK_ADV_FREQ ); + clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); }if (txc->modes &ADJ_SETOFFSET )
PATCH v3 6/11 timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
In reply to Thomas GleixnerRedirect the relative offset adjustment to the auxiliary clock offset instead of modifying CLOCK_REALTIME, which has no meaning in context of these clocks.
--- kernel/time/timekeeping.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
---
kernel/time/timekeeping.c +26 -8
@@ -1448,16 +1448,34 @@ static int __timekeeping_inject_offset(stimekeeping_forward_now (tks); -/* Make sure the proposed value is valid */ - tmp =timespec64_add (tk_xtime (tks), *ts); -if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || - !timespec64_valid_settod (&tmp)) { -timekeeping_restore_shadow (tkd); -return -EINVAL ; +if (!IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tks->id ==TIMEKEEPER_CORE ) { +/* Make sure the proposed value is valid */ + tmp =timespec64_add (tk_xtime (tks), *ts); +if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || + !timespec64_valid_settod (&tmp)) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + +tk_xtime_add (tks, ts); +tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts)); + }else { +struct tk_read_base *tkr_mono = &tks->tkr_mono; +ktime_t now, offs; + +/* Get the current time */ + now =ktime_add_ns (tkr_mono->base,timekeeping_get_ns (tkr_mono)); +/* Add the relative offset change */ + offs =ktime_add (tks->offs_aux,timespec64_to_ktime (*ts)); + +/* Prevent that the resulting time becomes negative */ +if (ktime_add (now, offs) <0 ) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + tks->offs_aux = offs; } -tk_xtime_add (tks, ts); -tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts));timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL );return 0 ; }
Re: [patch V3 06/11] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
Quoted 14 lines · Thomas Gleixner
Redirect the relative offset adjustment to the auxiliary clock offset instead of modifying
CLOCK_REALTIME, which has no meaning in context of these clocks.Signed-off-by Thomas Gleixner <[email protected]>--- kernel/time/timekeeping.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)---
kernel/time/timekeeping.c static int __timekeeping_inject_offset(s · line 1451
Quoted 10 lines
kernel/time/timekeeping.c +1
-6@@ -1448,16 +1448,34 @@ static int __timekeeping_inject_offset(stimekeeping_forward_now (tks); -/* Make sure the proposed value is valid */ - tmp =timespec64_add (tk_xtime (tks), *ts); -if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || - !timespec64_valid_settod (&tmp)) { -timekeeping_restore_shadow (tkd); -return -EINVAL ; +if (!IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tks->id ==TIMEKEEPER_CORE ) {
I feel like this should be in something like:
static inline bool is_core_timekeeper(tsk)
Quoted 15 lines
kernel/time/timekeeping.c +15
-0+/* Make sure the proposed value is valid */ + tmp =timespec64_add (tk_xtime (tks), *ts); +if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || + !timespec64_valid_settod (&tmp)) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + +tk_xtime_add (tks, ts); +tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts)); + }else { +struct tk_read_base *tkr_mono = &tks->tkr_mono; +ktime_t now, offs; + +/* Get the current time */
kernel/time/timekeeping.c static int __timekeeping_inject_offset(s · line 1476
Quoted 10 lines
kernel/time/timekeeping.c +10
-0+ now =ktime_add_ns (tkr_mono->base,timekeeping_get_ns (tkr_mono)); +/* Add the relative offset change */ + offs =ktime_add (tks->offs_aux,timespec64_to_ktime (*ts)); + +/* Prevent that the resulting time becomes negative */ +if (ktime_add (now, offs) <0 ) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + tks->offs_aux = offs;
I'd also consider moving this else into a aux helper function as well, but I'm not 100% on that.
Other than the is_core_timekeeper (or timekeeper_is_core() maybe?) suggestion above:
thanks
-john
[tip: timers/ptp] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 2c8aea59c206b12b436373861590baeda728be12 Gitweb: https://git.kernel.org/tip/2c8aea59c206b12b436373861590baeda728be12 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:42 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
Redirect the relative offset adjustment to the auxiliary clock offset instead of modifying CLOCK_REALTIME, which has no meaning in context of these clocks.
--- kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-)
kernel/time/timekeeping.c +31 -8
@@ -1431,6 +1431,11 @@ int do_settimeofday64(const struct timespec64 *ts) }EXPORT_SYMBOL (do_settimeofday64); +static inline bool timekeeper_is_core_tk (struct timekeeper *tk) +{ +return !IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tk->id ==TIMEKEEPER_CORE ; +} +/** * __timekeeping_inject_offset - Adds or subtracts from the current time. * @tkd: Pointer to the timekeeper to modify
@@ -1448,16 +1453,34 @@ static int __timekeeping_inject_offset(struct tk_data *tkd, const struct timespetimekeeping_forward_now (tks); -/* Make sure the proposed value is valid */ - tmp =timespec64_add (tk_xtime (tks), *ts); -if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || - !timespec64_valid_settod (&tmp)) { -timekeeping_restore_shadow (tkd); -return -EINVAL ; +if (timekeeper_is_core_tk (tks)) { +/* Make sure the proposed value is valid */ + tmp =timespec64_add (tk_xtime (tks), *ts); +if (timespec64_compare (&tks->wall_to_monotonic, ts) >0 || + !timespec64_valid_settod (&tmp)) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + +tk_xtime_add (tks, ts); +tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts)); + }else { +struct tk_read_base *tkr_mono = &tks->tkr_mono; +ktime_t now, offs; + +/* Get the current time */ + now =ktime_add_ns (tkr_mono->base,timekeeping_get_ns (tkr_mono)); +/* Add the relative offset change */ + offs =ktime_add (tks->offs_aux,timespec64_to_ktime (*ts)); + +/* Prevent that the resulting time becomes negative */ +if (ktime_add (now, offs) <0 ) { +timekeeping_restore_shadow (tkd); +return -EINVAL ; + } + tks->offs_aux = offs; } -tk_xtime_add (tks, ts); -tk_set_wall_to_mono (tks,timespec64_sub (tks->wall_to_monotonic, *ts));timekeeping_update_from_shadow (tkd,TK_UPDATE_ALL );return 0 ; }
PATCH v3 7/11 timekeeping: Make do_adjtimex() reusable
In reply to Thomas GleixnerSplit out the actual functionality of adjtimex() and make do_adjtimex() a wrapper which feeds the core timekeeper into it and handles the result including audit at the call site.
This allows to reuse the actual functionality for auxiliary clocks.
--- kernel/time/timekeeping.c | 110 +++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 50 deletions(-)
---
kernel/time/timekeeping.c +60 -50
@@ -2580,17 +2580,18 @@ unsigned long random_get_entropy_fallbac }EXPORT_SYMBOL_GPL (random_get_entropy_fallback); -/** -* do_adjtimex() - Accessor function to NTP __do_adjtimex function -* @txc: Pointer to kernel_timex structure containing NTP parameters -*/ -int do_adjtimex (struct __kernel_timex *txc) +struct adjtimex_result { +struct audit_ntp_data ad; +struct timespec64 delta; +bool clock_set; +}; + +static int __do_adjtimex (struct tk_data *tkd,struct __kernel_timex *txc, +struct adjtimex_result *result) { -struct tk_data *tkd = &tk_core; -struct timespec64 delta, ts; -struct audit_ntp_data ad; -bool offset_set = false; -bool clock_set = false; +struct timekeeper *tks = &tkd->shadow_timekeeper; +struct timespec64 ts; +s32 orig_tai, tai;int ret;/* Validate the data before disabling interrupts */
@@ -2599,56 +2600,65 @@ int do_adjtimex(struct __kernel_timex *treturn ret;add_device_randomness (txc,sizeof (*txc)); -audit_ntp_init (&ad); -ktime_get_real_ts64 (&ts);add_device_randomness (&ts,sizeof (ts)); - scoped_guard (raw_spinlock_irqsave, &tkd->lock) { -struct timekeeper *tks = &tkd->shadow_timekeeper; -s32 orig_tai, tai; - -if (!tks->clock_valid) -return -ENODEV ; - -if (txc->modes &ADJ_SETOFFSET ) { - delta.tv_sec = txc->time.tv_sec; - delta.tv_nsec = txc->time.tv_usec; -if (!(txc->modes &ADJ_NANO )) - delta.tv_nsec *=1000 ; - ret =__timekeeping_inject_offset (tkd, &delta); -if (ret) -return ret; - - offset_set = delta.tv_sec !=0 ; - clock_set = true; - } - - orig_tai = tai = tks->tai_offset; - ret =ntp_adjtimex (tks->id, txc, &ts, &tai, &ad); - -if (tai != orig_tai) { -__timekeeping_set_tai_offset (tks, tai); -timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); - clock_set = true; - }else { -tk_update_leap_state_all (&tk_core); - } - -/* Update the multiplier immediately if frequency was set directly */ -if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) - clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); +guard (raw_spinlock_irqsave)(&tkd->lock); + +if (!tks->clock_valid) +return -ENODEV ; + +if (txc->modes &ADJ_SETOFFSET ) { + result->delta.tv_sec = txc->time.tv_sec; + result->delta.tv_nsec = txc->time.tv_usec; +if (!(txc->modes &ADJ_NANO )) + result->delta.tv_nsec *=1000 ; + ret =__timekeeping_inject_offset (tkd, &result->delta); +if (ret) +return ret; + result->clock_set = true; + } + + orig_tai = tai = tks->tai_offset; + ret =ntp_adjtimex (tks->id, txc, &ts, &tai, &result->ad); + +if (tai != orig_tai) { +__timekeeping_set_tai_offset (tks, tai); +timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); + result->clock_set = true; + }else { +tk_update_leap_state_all (&tk_core); } +/* Update the multiplier immediately if frequency was set directly */ +if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) + result->clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); + +return ret; +} + +/** +* do_adjtimex() - Accessor function to NTP __do_adjtimex function +* @txc: Pointer to kernel_timex structure containing NTP parameters +*/ +int do_adjtimex (struct __kernel_timex *txc) +{ +struct adjtimex_result result = { }; +int ret; + + ret =__do_adjtimex (&tk_core, txc, &result); +if (ret <0 ) +return ret; +if (txc->modes &ADJ_SETOFFSET ) -audit_tk_injoffset (delta); +audit_tk_injoffset (result.delta); -audit_ntp_log (&ad); +audit_ntp_log (&result.ad); -if (clock_set) +if (result.clock_set)clock_was_set (CLOCK_SET_WALL ); -ntp_notify_cmos_timer (offset_set); +ntp_notify_cmos_timer (result.delta.tv_sec !=0 );return ret; }
Re: [patch V3 07/11] timekeeping: Make do_adjtimex() reusable
Quoted 8 lines · Thomas Gleixner
Split out the actual functionality of
adjtimex()and makedo_adjtimex()a wrapper which feeds the core timekeeper into it and handles the result including audit at the call site.This allows to reuse the actual functionality for auxiliary clocks.
Signed-off-by Thomas Gleixner <[email protected]>
[tip: timers/ptp] timekeeping: Make do_adjtimex() reusable
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 775f71ebedd382da390dc16a4c28cffa5b937f79 Gitweb: https://git.kernel.org/tip/775f71ebedd382da390dc16a4c28cffa5b937f79 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:43 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Make do_adjtimex() reusable
Split out the actual functionality of adjtimex() and make do_adjtimex() a wrapper which feeds the core timekeeper into it and handles the result including audit at the call site.
This allows to reuse the actual functionality for auxiliary clocks.
--- kernel/time/timekeeping.c | 102 ++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 46 deletions(-)
kernel/time/timekeeping.c +56 -46
@@ -2585,17 +2585,18 @@ unsigned long random_get_entropy_fallback(void) }EXPORT_SYMBOL_GPL (random_get_entropy_fallback); -/** -* do_adjtimex() - Accessor function to NTP __do_adjtimex function -* @txc: Pointer to kernel_timex structure containing NTP parameters -*/ -int do_adjtimex (struct __kernel_timex *txc) +struct adjtimex_result { +struct audit_ntp_data ad; +struct timespec64 delta; +bool clock_set; +}; + +static int __do_adjtimex (struct tk_data *tkd,struct __kernel_timex *txc, +struct adjtimex_result *result) { -struct tk_data *tkd = &tk_core; -struct timespec64 delta, ts; -struct audit_ntp_data ad; -bool offset_set = false; -bool clock_set = false; +struct timekeeper *tks = &tkd->shadow_timekeeper; +struct timespec64 ts; +s32 orig_tai, tai;int ret;/* Validate the data before disabling interrupts */
@@ -2604,56 +2605,65 @@ int do_adjtimex(struct __kernel_timex *txc)return ret;add_device_randomness (txc,sizeof (*txc)); -audit_ntp_init (&ad); -ktime_get_real_ts64 (&ts);add_device_randomness (&ts,sizeof (ts)); - scoped_guard (raw_spinlock_irqsave, &tkd->lock) { -struct timekeeper *tks = &tkd->shadow_timekeeper; -s32 orig_tai, tai; +guard (raw_spinlock_irqsave)(&tkd->lock); -if (!tks->clock_valid) -return -ENODEV ; - -if (txc->modes &ADJ_SETOFFSET ) { - delta.tv_sec = txc->time.tv_sec; - delta.tv_nsec = txc->time.tv_usec; -if (!(txc->modes &ADJ_NANO )) - delta.tv_nsec *=1000 ; - ret =__timekeeping_inject_offset (tkd, &delta); -if (ret) -return ret; - - offset_set = delta.tv_sec !=0 ; - clock_set = true; - } +if (!tks->clock_valid) +return -ENODEV ; - orig_tai = tai = tks->tai_offset; - ret =ntp_adjtimex (tks->id, txc, &ts, &tai, &ad); +if (txc->modes &ADJ_SETOFFSET ) { + result->delta.tv_sec = txc->time.tv_sec; + result->delta.tv_nsec = txc->time.tv_usec; +if (!(txc->modes &ADJ_NANO )) + result->delta.tv_nsec *=1000 ; + ret =__timekeeping_inject_offset (tkd, &result->delta); +if (ret) +return ret; + result->clock_set = true; + } -if (tai != orig_tai) { -__timekeeping_set_tai_offset (tks, tai); -timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); - clock_set = true; - }else { -tk_update_leap_state_all (&tk_core); - } + orig_tai = tai = tks->tai_offset; + ret =ntp_adjtimex (tks->id, txc, &ts, &tai, &result->ad); -/* Update the multiplier immediately if frequency was set directly */ -if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) - clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); +if (tai != orig_tai) { +__timekeeping_set_tai_offset (tks, tai); +timekeeping_update_from_shadow (tkd,TK_CLOCK_WAS_SET ); + result->clock_set = true; + }else { +tk_update_leap_state_all (&tk_core); } +/* Update the multiplier immediately if frequency was set directly */ +if (txc->modes & (ADJ_FREQUENCY |ADJ_TICK )) + result->clock_set |=__timekeeping_advance (tkd,TK_ADV_FREQ ); + +return ret; +} + +/** +* do_adjtimex() - Accessor function to NTP __do_adjtimex function +* @txc: Pointer to kernel_timex structure containing NTP parameters +*/ +int do_adjtimex (struct __kernel_timex *txc) +{ +struct adjtimex_result result = { }; +int ret; + + ret =__do_adjtimex (&tk_core, txc, &result); +if (ret <0 ) +return ret; +if (txc->modes &ADJ_SETOFFSET ) -audit_tk_injoffset (delta); +audit_tk_injoffset (result.delta); -audit_ntp_log (&ad); +audit_ntp_log (&result.ad); -if (clock_set) +if (result.clock_set)clock_was_set (CLOCK_SET_WALL ); -ntp_notify_cmos_timer (offset_set); +ntp_notify_cmos_timer (result.delta.tv_sec !=0 );return ret; }
PATCH v3 8/11 timekeeping: Prepare do_adtimex() for auxiliary clocks
In reply to Thomas GleixnerExclude ADJ_TAI, leap seconds and PPS functionality as they make no sense in the context of auxiliary clocks and provide a time stamp based on the actual clock.
--- kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-)
---
kernel/time/timekeeping.c +36 -3
@@ -58,6 +58,17 @@ static struct tk_data timekeeper_data[TI/* The core timekeeper */ #define tk_core (timekeeper_data[TIMEKEEPER_CORE]) +#ifdef CONFIG_POSIX_AUX_CLOCKS +static inline bool tk_get_aux_ts64 (unsigned int tkid,struct timespec64 *ts) +{ +return ktime_get_aux_ts64 (CLOCK_AUX + tkid -TIMEKEEPER_AUX_FIRST , ts); +} +#else +static inline bool tk_get_aux_ts64 (unsigned int tkid,struct timespec64 *ts) +{ +return false; +} +#endif /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended;
@@ -2503,7 +2514,7 @@ ktime_t ktime_get_update_offsets_now(uns/* * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex */ -static int timekeeping_validate_timex (const struct __kernel_timex *txc) +static int timekeeping_validate_timex (const struct __kernel_timex *txc,bool aux_clock) {if (txc->modes &ADJ_ADJTIME ) {/* singleshot must not be used with any other mode bits */
@@ -2562,6 +2573,21 @@ static int timekeeping_validate_timex(coreturn -EINVAL ; } +if (!aux_clock) +return 0 ; + +/* Auxiliary clocks are similar to TAI and do not have leap seconds */ +if (txc->status & (STA_INS |STA_DEL )) +return -EINVAL ; + +/* No TAI offset setting */ +if (txc->modes &ADJ_TAI ) +return -EINVAL ; + +/* No PPS support either */ +if (txc->status & (STA_PPSFREQ |STA_PPSTIME )) +return -EINVAL ; +return 0 ; }
@@ -2592,15 +2618,22 @@ static int __do_adjtimex(struct tk_datastruct timekeeper *tks = &tkd->shadow_timekeeper;struct timespec64 ts;s32 orig_tai, tai; +bool aux_clock;int ret; + aux_clock =IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) && tkd->timekeeper.id !=TIMEKEEPER_CORE ; +/* Validate the data before disabling interrupts */ - ret =timekeeping_validate_timex (txc); + ret =timekeeping_validate_timex (txc, aux_clock);if (ret)return ret;add_device_randomness (txc,sizeof (*txc)); -ktime_get_real_ts64 (&ts); +if (!aux_clock) +ktime_get_real_ts64 (&ts); +else +tk_get_aux_ts64 (tkd->timekeeper.id, &ts); +add_device_randomness (&ts,sizeof (ts));guard (raw_spinlock_irqsave)(&tkd->lock);
Re: [patch V3 08/11] timekeeping: Prepare do_adtimex() for auxiliary clocks
Quoted 22 lines · Thomas Gleixner
Exclude
ADJ_TAI, leap seconds and PPS functionality as they make no sense in the context of auxiliary clocks and provide a time stamp based on the actual clock.Signed-off-by Thomas Gleixner <[email protected]>--- kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-)---
kernel/time/timekeeping.c +5
-0@@ -2562,6 +2573,21 @@ static int timekeeping_validate_timex(coreturn -EINVAL ; } +if (!aux_clock) +return 0 ; + +/* Auxiliary clocks are similar to TAI and do not have leap seconds */ +if (txc->status & (STA_INS |STA_DEL ))
kernel/time/timekeeping.c static int timekeeping_validate_timex(co · line 2590
Quoted 10 lines
kernel/time/timekeeping.c +10
-0+return -EINVAL ; + +/* No TAI offset setting */ +if (txc->modes &ADJ_TAI ) +return -EINVAL ; + +/* No PPS support either */ +if (txc->status & (STA_PPSFREQ |STA_PPSTIME )) +return -EINVAL ; +
Just a taste issue, but I think it would be more clear if these checks were nested under the
if (aux_clock) {
...}
As otherwise if you read-over and miss the !aux_clock early return it seems like you're erroring out on normally valid cases.
But it's a minor thing.
kernel/time/timekeeping.c static int __do_adjtimex(struct tk_data · line 2625
Quoted 9 lines
kernel/time/timekeeping.c +3
-0@@ -2592,15 +2618,22 @@ static int __do_adjtimex(struct tk_datastruct timekeeper *tks = &tkd->shadow_timekeeper;struct timespec64 ts;s32 orig_tai, tai; +bool aux_clock;int ret; + aux_clock =IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) && tkd->timekeeper.id !=TIMEKEEPER_CORE ; +
Again, the is_core_timekeeper() check would be helpful here (or alternatively is_aux_timekeeper())
Otherwise:
thanks
-john
[tip: timers/ptp] timekeeping: Prepare do_adtimex() for auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 4eca49d0b621b314ac7c80f363932ec6f6c8abc8 Gitweb: https://git.kernel.org/tip/4eca49d0b621b314ac7c80f363932ec6f6c8abc8 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:45 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Prepare do_adtimex() for auxiliary clocks
Exclude ADJ_TAI, leap seconds and PPS functionality as they make no sense in the context of auxiliary clocks and provide a time stamp based on the actual clock.
--- kernel/time/timekeeping.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-)
kernel/time/timekeeping.c +33 -3
@@ -58,6 +58,17 @@ static struct tk_data timekeeper_data[TIMEKEEPERS_MAX];/* The core timekeeper */ #define tk_core (timekeeper_data[TIMEKEEPER_CORE]) +#ifdef CONFIG_POSIX_AUX_CLOCKS +static inline bool tk_get_aux_ts64 (unsigned int tkid,struct timespec64 *ts) +{ +return ktime_get_aux_ts64 (CLOCK_AUX + tkid -TIMEKEEPER_AUX_FIRST , ts); +} +#else +static inline bool tk_get_aux_ts64 (unsigned int tkid,struct timespec64 *ts) +{ +return false; +} +#endif /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended;
@@ -2508,7 +2519,7 @@ ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,/* * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex */ -static int timekeeping_validate_timex (const struct __kernel_timex *txc) +static int timekeeping_validate_timex (const struct __kernel_timex *txc,bool aux_clock) {if (txc->modes &ADJ_ADJTIME ) {/* singleshot must not be used with any other mode bits */
@@ -2567,6 +2578,20 @@ static int timekeeping_validate_timex(const struct __kernel_timex *txc)return -EINVAL ; } +if (aux_clock) { +/* Auxiliary clocks are similar to TAI and do not have leap seconds */ +if (txc->status & (STA_INS |STA_DEL )) +return -EINVAL ; + +/* No TAI offset setting */ +if (txc->modes &ADJ_TAI ) +return -EINVAL ; + +/* No PPS support either */ +if (txc->status & (STA_PPSFREQ |STA_PPSTIME )) +return -EINVAL ; + } +return 0 ; }
@@ -2595,17 +2620,22 @@ static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc, struct adjtimex_result *result) {struct timekeeper *tks = &tkd->shadow_timekeeper; +bool aux_clock = !timekeeper_is_core_tk (tks);struct timespec64 ts;s32 orig_tai, tai;int ret;/* Validate the data before disabling interrupts */ - ret =timekeeping_validate_timex (txc); + ret =timekeeping_validate_timex (txc, aux_clock);if (ret)return ret;add_device_randomness (txc,sizeof (*txc)); -ktime_get_real_ts64 (&ts); +if (!aux_clock) +ktime_get_real_ts64 (&ts); +else +tk_get_aux_ts64 (tkd->timekeeper.id, &ts); +add_device_randomness (&ts,sizeof (ts));guard (raw_spinlock_irqsave)(&tkd->lock);
PATCH v3 9/11 timekeeping: Provide adjtimex() for auxiliary clocks
In reply to Thomas GleixnerThe behaviour is close to clock_adtime(CLOCK_REALTIME) with the following differences:
1) ADJ_SETOFFSET adjusts the auxiliary clock offset
2) ADJ_TAI is not supported
3) Leap seconds are not supported
4) PPS is not supported
--- kernel/time/timekeeping.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
---
kernel/time/timekeeping.c +16 -0
@@ -2860,10 +2860,26 @@ static int aux_clock_set(const clockid_treturn 0 ; } +static int aux_clock_adj (const clockid_t id,struct __kernel_timex *txc) +{ +struct tk_data *tkd =aux_get_tk_data (id); +struct adjtimex_result result = { }; + +if (!tkd) +return -ENODEV ; + +/* +* @result is ignored for now as there are neither hrtimers nor a +* RTC related to auxiliary clocks for now. +*/ +return __do_adjtimex (tkd, txc, &result); +} +const struct k_clock clock_aux = { .clock_getres = aux_get_res, .clock_get_timespec = aux_get_timespec, .clock_set = aux_clock_set, + .clock_adj = aux_clock_adj, };static __init void tk_aux_setup (void )
Re: [patch V3 09/11] timekeeping: Provide adjtimex() for auxiliary clocks
Quoted 6 lines · Thomas Gleixner
The behaviour is close to
clock_adtime(CLOCK_REALTIME)with the following differences:1)
ADJ_SETOFFSETadjusts the auxiliary clock offset
Quoted 7 lines
2)
ADJ_TAIis not supported3) Leap seconds are not supported
4) PPS is not supported
Signed-off-by Thomas Gleixner <[email protected]>
[tip: timers/ptp] timekeeping: Provide adjtimex() for auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: ecf3e70304911be1c14cd21baa0bc611a53ec50b Gitweb: https://git.kernel.org/tip/ecf3e70304911be1c14cd21baa0bc611a53ec50b Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:46 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Provide adjtimex() for auxiliary clocks
The behaviour is close to clock_adtime(CLOCK_REALTIME) with the following differences:
1) ADJ_SETOFFSET adjusts the auxiliary clock offset
2) ADJ_TAI is not supported
3) Leap seconds are not supported
4) PPS is not supported
--- kernel/time/timekeeping.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
kernel/time/timekeeping.c +16 -0
@@ -2875,10 +2875,26 @@ static int aux_clock_set(const clockid_t id, const struct timespec64 *tnew)return 0 ; } +static int aux_clock_adj (const clockid_t id,struct __kernel_timex *txc) +{ +struct tk_data *aux_tkd =aux_get_tk_data (id); +struct adjtimex_result result = { }; + +if (!aux_tkd) +return -ENODEV ; + +/* +* @result is ignored for now as there are neither hrtimers nor a +* RTC related to auxiliary clocks for now. +*/ +return __do_adjtimex (aux_tkd, txc, &result); +} +const struct k_clock clock_aux = { .clock_getres = aux_get_res, .clock_get_timespec = aux_get_timespec, .clock_set = aux_clock_set, + .clock_adj = aux_clock_adj, };static __init void tk_aux_setup (void )
PATCH v3 10/11 timekeeping: Provide update for auxiliary timekeepers
In reply to Thomas GleixnerUpdate the auxiliary timekeepers periodically. For now this is tied to the system timekeeper update from the tick. This might be revisited and moved out of the tick.
--- kernel/time/timekeeping.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
---
kernel/time/timekeeping.c +18 -0
@@ -131,9 +131,11 @@ static struct tk_fast tk_fast_raw ____c#ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup (void );static void tk_aux_update_clocksource (void ); +static void tk_aux_advance (void );#else static inline void tk_aux_setup (void ) { }static inline void tk_aux_update_clocksource (void ) { } +static inline void tk_aux_advance (void ) { }#endif unsigned long timekeeper_lock_irqsave (void )
@@ -2312,11 +2314,13 @@ static bool timekeeping_advance(enum tim/** * update_wall_time - Uses the current clocksource to increment the wall time * +* It also updates the enabled auxiliary clock timekeepers */ void update_wall_time (void ) {if (timekeeping_advance (TK_ADV_TICK ))clock_was_set_delayed (); +tk_aux_advance (); }/**
@@ -2762,6 +2766,20 @@ static void tk_aux_update_clocksource(vo } } +static void tk_aux_advance (void ) +{ +unsigned long active =READ_ONCE (aux_timekeepers); +unsigned int id; + +for_each_set_bit (id, &active,BITS_PER_LONG ) { +struct tk_data *tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ]; + +guard (raw_spinlock)(&tkd->lock); +if (tkd->shadow_timekeeper.clock_valid) +__timekeeping_advance (tkd,TK_ADV_TICK ); + } +} +/** * ktime_get_aux - Get time for a AUX clock * @id: ID of the clock to read (CLOCK_AUX...)
Re: [patch V3 10/11] timekeeping: Provide update for auxiliary timekeepers
Quoted 13 lines · Thomas Gleixner
Update the auxiliary timekeepers periodically. For now this is tied to the system timekeeper update from the tick. This might be revisited and moved out of the tick.
Signed-off-by Thomas Gleixner <[email protected]>--- kernel/time/timekeeping.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)---
kernel/time/timekeeping.c static void tk_aux_update_clocksource(vo · line 2775
Quoted 10 lines
kernel/time/timekeeping.c +7
-0@@ -2762,6 +2766,20 @@ static void tk_aux_update_clocksource(vo } } +static void tk_aux_advance (void ) +{ +unsigned long active =READ_ONCE (aux_timekeepers); +unsigned int id; + +for_each_set_bit (id, &active,BITS_PER_LONG ) { +struct tk_data *tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ];
Again, a nit, but I'd use aux_tkd or something just to be super clear we're using aux ones here.
kernel/time/timekeeping.c static void tk_aux_update_clocksource(vo · line 2781
Quoted 6 lines
kernel/time/timekeeping.c +6
-0+ +guard (raw_spinlock)(&tkd->lock); +if (tkd->shadow_timekeeper.clock_valid) +__timekeeping_advance (tkd,TK_ADV_TICK ); + } +}
Otherwise,
[tip: timers/ptp] timekeeping: Provide update for auxiliary timekeepers
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: e6d4c00719a6b1dda3fb358b4c973595f9dfd455 Gitweb: https://git.kernel.org/tip/e6d4c00719a6b1dda3fb358b4c973595f9dfd455 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:47 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Provide update for auxiliary timekeepers
Update the auxiliary timekeepers periodically. For now this is tied to the system timekeeper update from the tick. This might be revisited and moved out of the tick.
--- kernel/time/timekeeping.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
kernel/time/timekeeping.c +19 -0
@@ -131,9 +131,11 @@ static struct tk_fast tk_fast_raw ____cacheline_aligned = {#ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup (void );static void tk_aux_update_clocksource (void ); +static void tk_aux_advance (void );#else static inline void tk_aux_setup (void ) { }static inline void tk_aux_update_clocksource (void ) { } +static inline void tk_aux_advance (void ) { }#endif unsigned long timekeeper_lock_irqsave (void )
@@ -2317,11 +2319,13 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode)/** * update_wall_time - Uses the current clocksource to increment the wall time * +* It also updates the enabled auxiliary clock timekeepers */ void update_wall_time (void ) {if (timekeeping_advance (TK_ADV_TICK ))clock_was_set_delayed (); +tk_aux_advance (); }/**
@@ -2764,6 +2768,21 @@ static void tk_aux_update_clocksource(void) } } +static void tk_aux_advance (void ) +{ +unsigned long active =READ_ONCE (aux_timekeepers); +unsigned int id; + +/* Lockless quick check to avoid extra cache lines */ +for_each_set_bit (id, &active,BITS_PER_LONG ) { +struct tk_data *aux_tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ]; + +guard (raw_spinlock)(&aux_tkd->lock); +if (aux_tkd->shadow_timekeeper.clock_valid) +__timekeeping_advance (aux_tkd,TK_ADV_TICK ); + } +} +/** * ktime_get_aux - Get time for a AUX clock * @id: ID of the clock to read (CLOCK_AUX...)
PATCH v3 11/11 timekeeping: Provide interface to control auxiliary clocks
In reply to Thomas GleixnerAuxiliary clocks are disabled by default and attempts to access them fail.
Provide an interface to enable/disable them at run-time.
---
V3: Use kobject.h, clockid_t and cleanup the sysfs init - Thomas W.
Use aux_tkd, aux_tks for clarity - John
--- Documentation/ABI/stable/sysfs-kernel-time-aux-clocks | 5 kernel/time/timekeeping.c | 116 ++++++++++++++++++ 2 files changed, 121 insertions(+)
Documentation/ABI/stable/sysfs-kernel-time-aux-clocks new file +5 -0
@@ -0,0 +1,5 @@ +What: /sys/kernel/time/aux_clocks/<ID>/enable +Date: May 2025 +Contact: Thomas Gleixner <[email protected]> +Description: + Controls the enablement of auxiliary clock timekeepers.
kernel/time/timekeeping.c +116 -0
128 lines not on this page · raw message
Re: [patch V3 11/11] timekeeping: Provide interface to control auxiliary clocks
Quoted 11 lines · Thomas Gleixner
Auxiliary clocks are disabled by default and attempts to access them fail.
Provide an interface to enable/disable them at run-time.
Signed-off-by Thomas Gleixner <[email protected]>---
V3: Usekobject.h,clockid_tand cleanup the sysfs init - Thomas W.
Useaux_tkd,aux_tksfor clarity - John
---
[tip: timers/ptp] timekeeping: Provide interface to control auxiliary clocks
In reply to Thomas GleixnerThe following commit has been merged into the timers/ptp branch of tip:
The following commit has been merged into the timers/ptp branch of tip:
Commit-ID: 7b95663a3d96b39b40f169dba5faef3e20163c5c Gitweb: https://git.kernel.org/tip/7b95663a3d96b39b40f169dba5faef3e20163c5c Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 25 Jun 2025 20:38:49 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 27 Jun 2025 20:13:13 +02:00
timekeeping: Provide interface to control auxiliary clocks
Auxiliary clocks are disabled by default and attempts to access them fail.
Provide an interface to enable/disable them at run-time.
--- Documentation/ABI/stable/sysfs-kernel-time-aux-clocks | 5 +- kernel/time/timekeeping.c | 116 +++++++++- 2 files changed, 121 insertions(+) create mode 100644 Documentation/ABI/stable/sysfs-kernel-time-aux-clocks
Documentation/ABI/stable/sysfs-kernel-time-aux-clocks new file +5 -0
@@ -0,0 +1,5 @@ +What: /sys/kernel/time/aux_clocks/<ID>/enable +Date: May 2025 +Contact: Thomas Gleixner <[email protected]> +Description: + Controls the enablement of auxiliary clock timekeepers.
kernel/time/timekeeping.c +116 -0
128 lines not on this page · raw message