From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: netdev@vger.kernel.org,
"Richard Cochran" <richardcochran@gmail.com>,
"Christopher Hall" <christopher.s.hall@intel.com>,
"David Zage" <david.zage@intel.com>,
"John Stultz" <jstultz@google.com>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
"Miroslav Lichvar" <mlichvar@redhat.com>,
"Werner Abt" <werner.abt@meinberg-usa.com>,
"David Woodhouse" <dwmw2@infradead.org>,
"Stephen Boyd" <sboyd@kernel.org>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"Kurt Kanzenbach" <kurt@linutronix.de>,
"Nam Cao" <namcao@linutronix.de>,
"Alex Gieringer" <gieri@linutronix.de>
Subject: [patch 03/26] timekeeping: Avoid double notification in do_adjtimex()
Date: Tue, 13 May 2025 17:13:00 +0200 (CEST) [thread overview]
Message-ID: <20250513145136.848612979@linutronix.de> (raw)
In-Reply-To: 20250513144615.252881431@linutronix.de
Consolidate do_adjtimex() so that it does not notify about clock changes
twice.
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
kernel/time/timekeeping.c | 98 ++++++++++++++++++++++++++--------------------
1 file changed, 56 insertions(+), 42 deletions(-)
---
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1413,40 +1413,49 @@ int do_settimeofday64(const struct times
EXPORT_SYMBOL(do_settimeofday64);
/**
- * timekeeping_inject_offset - Adds or subtracts from the current time.
+ * __timekeeping_inject_offset - Adds or subtracts from the current time.
* @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(const struct timespec64 *ts)
{
+ struct timekeeper *tks = &tk_core.shadow_timekeeper;
+ struct timespec64 tmp;
+
if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
return -EINVAL;
- scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
- struct timekeeper *tks = &tk_core.shadow_timekeeper;
- struct timespec64 tmp;
-
- 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);
- return -EINVAL;
- }
+ timekeeping_forward_now(tks);
- 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);
+ /* 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);
+ return -EINVAL;
}
- /* Signal hrtimers about time change */
- clock_was_set(CLOCK_SET_WALL);
+ 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);
return 0;
}
+static int timekeeping_inject_offset(const struct timespec64 *ts)
+{
+ int ret;
+
+ scoped_guard (raw_spinlock_irqsave, &tk_core.lock)
+ ret = __timekeeping_inject_offset(ts);
+
+ /* Signal hrtimers about time change */
+ if (!ret)
+ clock_was_set(CLOCK_SET_WALL);
+ return ret;
+}
+
/*
* Indicates if there is an offset between the system clock and the hardware
* clock/persistent clock/rtc.
@@ -2181,7 +2190,7 @@ static u64 logarithmic_accumulation(stru
* timekeeping_advance - Updates the timekeeper to the current time and
* current NTP tick length
*/
-static bool timekeeping_advance(enum timekeeping_adv_mode mode)
+static bool __timekeeping_advance(enum timekeeping_adv_mode mode)
{
struct timekeeper *tk = &tk_core.shadow_timekeeper;
struct timekeeper *real_tk = &tk_core.timekeeper;
@@ -2189,8 +2198,6 @@ static bool timekeeping_advance(enum tim
int shift = 0, maxshift;
u64 offset, orig_offset;
- guard(raw_spinlock_irqsave)(&tk_core.lock);
-
/* Make sure we're fully resumed: */
if (unlikely(timekeeping_suspended))
return false;
@@ -2244,6 +2251,12 @@ static bool timekeeping_advance(enum tim
return !!clock_set;
}
+static bool timekeeping_advance(enum timekeeping_adv_mode mode)
+{
+ guard(raw_spinlock_irqsave)(&tk_core.lock);
+ return __timekeeping_advance(mode);
+}
+
/**
* update_wall_time - Uses the current clocksource to increment the wall time
*
@@ -2532,10 +2545,10 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fal
*/
int do_adjtimex(struct __kernel_timex *txc)
{
+ struct timespec64 delta, ts;
struct audit_ntp_data ad;
bool offset_set = false;
bool clock_set = false;
- struct timespec64 ts;
int ret;
/* Validate the data before disabling interrupts */
@@ -2544,21 +2557,6 @@ int do_adjtimex(struct __kernel_timex *t
return ret;
add_device_randomness(txc, sizeof(*txc));
- if (txc->modes & ADJ_SETOFFSET) {
- struct timespec64 delta;
-
- 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);
- if (ret)
- return ret;
-
- offset_set = delta.tv_sec != 0;
- audit_tk_injoffset(delta);
- }
-
audit_ntp_init(&ad);
ktime_get_real_ts64(&ts);
@@ -2568,6 +2566,19 @@ int do_adjtimex(struct __kernel_timex *t
struct timekeeper *tks = &tk_core.shadow_timekeeper;
s32 orig_tai, tai;
+ 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);
+ if (ret)
+ return ret;
+
+ offset_set = delta.tv_sec != 0;
+ clock_set = true;
+ }
+
orig_tai = tai = tks->tai_offset;
ret = __do_adjtimex(txc, &ts, &tai, &ad);
@@ -2578,13 +2589,16 @@ int do_adjtimex(struct __kernel_timex *t
} 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(TK_ADV_FREQ);
}
- audit_ntp_log(&ad);
+ if (txc->modes & ADJ_SETOFFSET)
+ audit_tk_injoffset(delta);
- /* Update the multiplier immediately if frequency was set directly */
- if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
- clock_set |= timekeeping_advance(TK_ADV_FREQ);
+ audit_ntp_log(&ad);
if (clock_set)
clock_was_set(CLOCK_SET_WALL);
next prev parent reply other threads:[~2025-05-13 15:13 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-13 15:12 [patch 00/26] timekeeping: Provide support for independent PTP timekeepers Thomas Gleixner
2025-05-13 15:12 ` [patch 01/26] timekeeping: Remove hardcoded access to tk_core Thomas Gleixner
2025-05-13 15:12 ` [patch 02/26] timekeeping: Cleanup kernel doc of __ktime_get_real_seconds() Thomas Gleixner
2025-05-13 15:13 ` Thomas Gleixner [this message]
2025-05-13 15:13 ` [patch 04/26] timekeeping: Introduce timekeeper ID Thomas Gleixner
2025-05-13 15:13 ` [patch 05/26] time: Introduce PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 06/26] ntp: Add support for PTP timekeepers Thomas Gleixner
2025-05-13 15:13 ` [patch 07/26] ntp: Add timekeeper ID arguments to public functions Thomas Gleixner
2025-05-13 15:13 ` [patch 08/26] ntp: Rename __do_adjtimex() to ntp_adjtimex() Thomas Gleixner
2025-05-13 15:13 ` [patch 09/26] timekeeping: Make __timekeeping_advance() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 10/26] timekeeping: Prepare timekeeping_update_from_shadow() Thomas Gleixner
2025-05-13 15:13 ` [patch 11/26] timekeeping: Add clock_valid flag to timekeeper Thomas Gleixner
2025-05-13 15:13 ` [patch 12/26] timekeeping: Introduce PTP time keepers Thomas Gleixner
2025-05-13 15:13 ` [patch 13/26] timekeeping: Provide ktime_get_ntp_seconds() Thomas Gleixner
2025-05-13 15:13 ` [patch 14/26] ntp: Use ktime_get_ntp_seconds() Thomas Gleixner
2025-05-13 15:13 ` [patch 15/26] timekeeping: Add PTP offset to timekeeper Thomas Gleixner
2025-05-13 15:13 ` [patch 16/26] timekeeping: Update PTP timekeepers on clocksource change Thomas Gleixner
2025-05-13 15:13 ` [patch 17/26] timekeeping: Provide time getters for PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 18/26] timekeeping: Add minimal posix-timers support " Thomas Gleixner
2025-05-13 15:13 ` [patch 19/26] timekeeping: Provide time setter " Thomas Gleixner
2025-05-13 15:13 ` [patch 20/26] timekeeping: Make timekeeping_inject_offset() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 21/26] timekeeping: Add PTP clock support to __timekeeping_inject_offset() Thomas Gleixner
2025-05-13 15:13 ` [patch 22/26] timekeeping: Make do_adjtimex() reusable Thomas Gleixner
2025-05-13 15:13 ` [patch 23/26] timekeeping: Prepare do_adtimex() for PTP clocks Thomas Gleixner
2025-05-13 15:13 ` [patch 24/26] timekeeping: Provide adjtimex() " Thomas Gleixner
2025-05-13 15:13 ` [patch 25/26] timekeeping: Provide update for PTP timekeepers Thomas Gleixner
2025-05-13 15:13 ` [patch 26/26] timekeeping: Provide interface to control independent PTP clocks Thomas Gleixner
2025-05-14 8:07 ` Antoine Tenart
2025-05-14 8:37 ` Thomas Gleixner
2025-05-14 7:12 ` [patch 00/26] timekeeping: Provide support for independent PTP timekeepers Miroslav Lichvar
2025-05-14 8:54 ` Thomas Gleixner
2025-05-14 15:58 ` Richard Cochran
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250513145136.848612979@linutronix.de \
--to=tglx@linutronix.de \
--cc=anna-maria@linutronix.de \
--cc=christopher.s.hall@intel.com \
--cc=david.zage@intel.com \
--cc=dwmw2@infradead.org \
--cc=frederic@kernel.org \
--cc=gieri@linutronix.de \
--cc=jstultz@google.com \
--cc=kurt@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=namcao@linutronix.de \
--cc=netdev@vger.kernel.org \
--cc=richardcochran@gmail.com \
--cc=sboyd@kernel.org \
--cc=thomas.weissschuh@linutronix.de \
--cc=werner.abt@meinberg-usa.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).