[patch V3 06/11] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()

Thomas Gleixner posted 11 patches 3 months, 2 weeks ago
[patch V3 06/11] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
Posted by Thomas Gleixner 3 months, 2 weeks ago
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 <tglx@linutronix.de>
---
 kernel/time/timekeeping.c |   34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
---

--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1448,16 +1448,34 @@ static int __timekeeping_inject_offset(s
 
 	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(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()
Posted by John Stultz 3 months, 1 week ago
On Wed, Jun 25, 2025 at 11:38 AM Thomas Gleixner <tglx@linutronix.de> wrote:
>
> 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 <tglx@linutronix.de>
> ---
>  kernel/time/timekeeping.c |   34 ++++++++++++++++++++++++++--------
>  1 file changed, 26 insertions(+), 8 deletions(-)
> ---
>
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1448,16 +1448,34 @@ static int __timekeeping_inject_offset(s
>
>         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(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)


> +               /* 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;

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:
 Acked-by: John Stultz <jstultz@google.com>

thanks
-john
[tip: timers/ptp] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset()
Posted by tip-bot2 for Thomas Gleixner 3 months, 1 week ago
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 <tglx@linutronix.de>
AuthorDate:    Wed, 25 Jun 2025 20:38:42 +02:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
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.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: John Stultz <jstultz@google.com>
Link: https://lore.kernel.org/all/20250625183758.124057787@linutronix.de

---
 kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 2d294cf..e893557 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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 timespe
 
 	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(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;
 }