From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30E2B482F2; Tue, 13 May 2025 15:12:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149180; cv=none; b=q4aFJqbKQO2on36QGDHs5Iq8TkeYFjXumyFtOGkRz7CyJh8ULrdSle/eY64R8Cq07JhdLkpX1Jvi6aQa9MnfpX5SxsAMU0AKBupuaLNEqbDK6HX//F8dhRgwyQZLNqUze838XQSYmZlgGrCQplei2m89tvpNAgoAHxVUfxxrlfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149180; c=relaxed/simple; bh=H5LZ7r0BO9UJwvNwXDzJW1WeWZKHWrNuAEaUA+3FKfU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=MZxWzsEcayEGoGw129/uzgYICCzmQh9N/AMftx8tnWwr34sZCjcXZKiLmwY07oHcOlmfQ4bP7cH1TymD5ORp5m9TVpB9hmtsDIzVncrXrMVxfUcnm3HakTxsdTpWuC6gWRNmCtqwlzovjb/ZHbLihVdUNBjm70bU4T39hAjAGv8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4sRbRDjw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7xJuSwhI; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4sRbRDjw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7xJuSwhI" Message-ID: <20250513145136.727753386@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L4D9JRJvf/qDRslz7xOrYCaTiKUlQohZZE5tQ+PqCuE=; b=4sRbRDjwLBjc1LgNoaU+koDAO+LMPs+JW3T8zfOQU+TdG7MB+nORvZqRIIaz22YdE/Csbw GSsGoaFtD6XTFd3/R9624Fm4ot92aSLejXTDEKwZjE2m1bfPl6A7VWbPJuc+U+h1C0S454 dnqBvcsds45v7G4F78W2MoA6LYww97y9Kc9tpYdkJ3FCXTPmgStIHxMt3Ov4xpsPt/yvUW N3F9ry4OG0DWmS+vxxxcYhnfDTn9GlK/MNzcCcL1S/SZT/ZmcvxTX78srFtPnNwDzyrQVX v7OqS5tqk9R7AijIrIgLJ8d8AuEWkQKqVfgpFZteLTWmsYU6XFbu0jLDZHIFfA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L4D9JRJvf/qDRslz7xOrYCaTiKUlQohZZE5tQ+PqCuE=; b=7xJuSwhImgbI69OMvECaVten+kRz29lrT3K6fzgNrJfL5/RbZBm04LoKsvMDJtUs3u+jdd k2pCkbNcvN+gyZDw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 01/26] timekeeping: Remove hardcoded access to tk_core References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:12:56 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This was overlooked in the initial conversion. Use the provided pointer to access the shadow timekeeper. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -663,7 +663,7 @@ static void timekeeping_restore_shadow(s =20 static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned i= nt action) { - struct timekeeper *tk =3D &tk_core.shadow_timekeeper; + struct timekeeper *tk =3D &tkd->shadow_timekeeper; =20 lockdep_assert_held(&tkd->lock); From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5B3F2BDC21; Tue, 13 May 2025 15:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149182; cv=none; b=NrtlKwWyZ9mwVtEuFUNnf6hz08s/eKU0EH3Mym2tmXzD8ThuxHdXSHTQ/iEZ70qoszdzR8V6NN8bO9XbxRk4Qial0u6CXrQYwSXZcluyS59NBB8IqA7udgOj4EBDSAHACIN5SRJ8tDGofC5eqTFZ1kJxqXgHJMMd3hy8FK5q7Eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149182; c=relaxed/simple; bh=wlpwNyknMfR1RoDXQOsQOLe9nGuz+tJKa17ta22S/mA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=C6iwNG3+9uSNHqnV6QZYUw6ns4H0eXzjWmOAcV1ZkMUtRdAs7v1lxwdPgRZvQTVR5SjPL/Aem1Qqi0eSSNq7QuJY3AV2q3zZmBzvFiAGaZ2D/QgXeLpb6R16sTwqeUgPxRoE0+jKlv0GTQnX7SmmNa6xoJv85l2rtdvyvPDLjd0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0PTuw/Lp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=yv8qNF4p; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0PTuw/Lp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="yv8qNF4p" Message-ID: <20250513145136.789188877@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149179; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PLK3LOofu2itK9FE/VIdqa3Xxrw13vea1Zi9cuZZRM0=; b=0PTuw/Lp3X4Op/SjEz/31xA7jO0ZZnLGQzp9AsG0QUOfbxE8m44p9j1sKBJvi0TpiUqFbW BCBapiFNf0Hf60Smuf6MXEmr2NvCq189syMstHx1R8afPyvtriY+wXa0wtG+OyzNQcW2HF iXSuhckWF3JzRQ/iTOuwRX9+0IWf403v11hXQ93TM6BHS2Xb9j2jifP2tmx6mg0ct3m+r0 h/VMGcJTCscd7Hw6vYNFxUcTE4ywcsHkwAie7up22LrdNhawNRJfnDIIjJNKMgyEO6pnt7 2gbe2wLBPEeOz1cK5FteKo+0yN1mp23viS4n1fg6X8bVXRWnCpA7OmDchlnrCA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149179; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PLK3LOofu2itK9FE/VIdqa3Xxrw13vea1Zi9cuZZRM0=; b=yv8qNF4pQFWbgKJeOVR5qe1q8cwIBsUHw10zzloRFjAIehDLAp1IShRMnRJ2kanIhMAdly p6Fcfgl7zrYYy+DA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 02/26] timekeeping: Cleanup kernel doc of __ktime_get_real_seconds() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:12:58 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -975,9 +975,14 @@ time64_t ktime_get_real_seconds(void) EXPORT_SYMBOL_GPL(ktime_get_real_seconds); =20 /** - * __ktime_get_real_seconds - The same as ktime_get_real_seconds - * but without the sequence counter protect. This internal function - * is called just when timekeeping lock is already held. + * __ktime_get_real_seconds - Unprotected access to CLOCK_REALTIME seconds + * + * The same as ktime_get_real_seconds() but without the sequence counter + * protection. This function is used in restricted contexts like the x86 M= CE + * handler and in KGDB. It's unprotected on 32-bit vs. concurrent half + * completed modification and only to be used for such critical contexts. + * + * Returns: Racy snapshot of the CLOCK_REALTIME seconds value */ noinstr time64_t __ktime_get_real_seconds(void) { From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBDD92BE7A5; Tue, 13 May 2025 15:13:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149184; cv=none; b=T9AL2n/b1ALvNj8y0vpJvaV7luvTKxmHJDA0zWHZJ9imFLVzIKDv0upVFrFQW0Ncq7ZpRBdml83zm0FwFGhGGPfWgxtszezS59O45D+4xjd2qqU6M5sw7ek1a5MOD+KhaMBSTubuVTIcw2DtaNbf1+Cy78ueUGSnJeujK/0Fy8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149184; c=relaxed/simple; bh=Lk+Ge/n0L9b7paxIP1W1xbsV3HcqlVTBNKwQhmg6QUw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OqQ8SHNCj6vapgr9x+JaqD7VbQxnbhWAhXozACRPelVVfsWBREKXimZYo9eF1WwCWE+ODcLpIefCZUlwcOLEpWJDKMMI+b/JtOfSg8YFPsNsEQ1gykn2ikrnXmdOHlJsWaTyZeUZwFmIpmx4T2Yr8oWOiM6LHpPiBgwE1iz5B2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=XW8QfH2g; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nWGMOUcJ; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="XW8QfH2g"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nWGMOUcJ" Message-ID: <20250513145136.848612979@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=psJzr+vgRpQMoFewg475KsVH7KC/S/u7MOLDzDXbvoE=; b=XW8QfH2gwzL+/PWPxGP6YGJvHIUSBiHFeMImR+ZhU8jB30nEoaG8v+1nyDF3wUOKlv7IGG j98DfjawdUcgyE3AYS3/zllQCwUHcmyIpAUO+DK0PP15OALTZs02JV6OKD4jsaKtcL+ETI fvcnVqKnd7OroGL2c7wxVHGno0osudKBdSlHO/RmUQfOCVnwJXZ70fjGzgYILLHOJnBVEg BvZcznr/2JiiRz1tPCpxmR27pjkHKiZdozRy4THaiYsbjPgndEfM+Kc/MIZ8dNghZ6PoMa oXloxgfpEPIgPTA1DPDE6YxoS82uvbCoX4JMD9ivaqzKJWpSMtICL0PmYqlQqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=psJzr+vgRpQMoFewg475KsVH7KC/S/u7MOLDzDXbvoE=; b=nWGMOUcJOhW+UwLd/BlsS8wxlYOrsNVsCKLV/UETSZDAxbHsK4Fe7BmBypgOeeEwu+wyKL VbJC90ABaLK9jBDw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 03/26] timekeeping: Avoid double notification in do_adjtimex() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:00 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Consolidate do_adjtimex() so that it does not notify about clock changes twice. Signed-off-by: Thomas Gleixner --- 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); =20 /** - * 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 =3D &tk_core.shadow_timekeeper; + struct timespec64 tmp; + if (ts->tv_nsec < 0 || ts->tv_nsec >=3D NSEC_PER_SEC) return -EINVAL; =20 - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; - struct timespec64 tmp; - - timekeeping_forward_now(tks); =20 - /* Make sure the proposed value is valid */ - tmp =3D 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); =20 - 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 =3D 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; } =20 - /* 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; } =20 +static int timekeeping_inject_offset(const struct timespec64 *ts) +{ + int ret; + + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) + ret =3D __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 hardwa= re * 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 =3D &tk_core.shadow_timekeeper; struct timekeeper *real_tk =3D &tk_core.timekeeper; @@ -2189,8 +2198,6 @@ static bool timekeeping_advance(enum tim int shift =3D 0, maxshift; u64 offset, orig_offset; =20 - 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; } =20 +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 t= ime * @@ -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 =3D false; bool clock_set =3D false; - struct timespec64 ts; int ret; =20 /* 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)); =20 - if (txc->modes & ADJ_SETOFFSET) { - struct timespec64 delta; - - delta.tv_sec =3D txc->time.tv_sec; - delta.tv_nsec =3D txc->time.tv_usec; - if (!(txc->modes & ADJ_NANO)) - delta.tv_nsec *=3D 1000; - ret =3D timekeeping_inject_offset(&delta); - if (ret) - return ret; - - offset_set =3D delta.tv_sec !=3D 0; - audit_tk_injoffset(delta); - } - audit_ntp_init(&ad); =20 ktime_get_real_ts64(&ts); @@ -2568,6 +2566,19 @@ int do_adjtimex(struct __kernel_timex *t struct timekeeper *tks =3D &tk_core.shadow_timekeeper; s32 orig_tai, tai; =20 + if (txc->modes & ADJ_SETOFFSET) { + delta.tv_sec =3D txc->time.tv_sec; + delta.tv_nsec =3D txc->time.tv_usec; + if (!(txc->modes & ADJ_NANO)) + delta.tv_nsec *=3D 1000; + ret =3D __timekeeping_inject_offset(&delta); + if (ret) + return ret; + + offset_set =3D delta.tv_sec !=3D 0; + clock_set =3D true; + } + orig_tai =3D tai =3D tks->tai_offset; ret =3D __do_adjtimex(txc, &ts, &tai, &ad); =20 @@ -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 |=3D __timekeeping_advance(TK_ADV_FREQ); } =20 - audit_ntp_log(&ad); + if (txc->modes & ADJ_SETOFFSET) + audit_tk_injoffset(delta); =20 - /* Update the multiplier immediately if frequency was set directly */ - if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D timekeeping_advance(TK_ADV_FREQ); + audit_ntp_log(&ad); =20 if (clock_set) clock_was_set(CLOCK_SET_WALL); From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A17D52BE7BF; Tue, 13 May 2025 15:13:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149186; cv=none; b=Bj+yt3TTJ1ktujlwkD1/z5JUuGRjZsgYXgPsxaVjdc85s+6J8Uuft9YJXltLfzkn+EmGgNLGAXk/6+J/bDH+MRWVRJzb2eNk4EUz3sEBGzR3VL2gknvc8o8hayemGmW4BE541x/AQZXQ8mATykS0wD0wL9I3C+gUIY+m2TBvQgk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149186; c=relaxed/simple; bh=tO94s04cwNa0K8JKWvxpUkZ4hglOVVlX8obWhhM6690=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=cXWcXsyFGRcPeAciBtA5lSVhy0CIrQAWB0cftck1Lof7TuGCzzO47jJJyRvlPZ9afi7PSgTfHChdjs+dDJ5wcv12PjOaSnyS5UfthJYnJHHgSMzMyVl7gNJss+XrNwiqHTCdvflcLtPKQcf7IL+DYxdom0v9RIca7VQwLPX2ujo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FQ8qF6Vp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=VlobM1Iq; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FQ8qF6Vp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="VlobM1Iq" Message-ID: <20250513145136.908036915@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ITg02Kv4JkJ6MviaQE7ufCrtvZ0fwMzABnGHEcpC8Ak=; b=FQ8qF6VpnFWdXYYX1Jj/rCC/PAZVUJD1XU1oz4Mz0l4VNfWrUjiAhLYlM6ygU3KhTsEKfN U4wByozt5uN8wjk2ijN9ahMUsWYsZiah0HWUSSexlTr/BVVgbgdmv3JSEoyF/T/ZYGJKtu dm+UCompcuR6uRZC/oEKg7jbSF06B5ZJ4Dqo2ss0DG2CWwAXldIurOOUwgfKB37Tv+0eLJ fb7Bt0P7SzKEn/8DQlZ2ZhiCg1JdPCyXzQMTFbDHhxbzeAzYIU2k/wOLWerzjmObhU6Y/H 9y4YngcGVPYzP9eiiiVztUkDlFjf9kMDk2LVZh/vpGKg4lZT+j+dI7NBkCtIAA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ITg02Kv4JkJ6MviaQE7ufCrtvZ0fwMzABnGHEcpC8Ak=; b=VlobM1IqkzUtzAI11rX4KS4DSt5vomcHunf9MjVq+8JyDP0hIu7aQe8IKa/Cx9025EjohV bd/TsNvdU9vRiGAQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 04/26] timekeeping: Introduce timekeeper ID References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:02 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen As long as there is only a single timekeeper, there is no need to clarify which timekeeper is used. But with the upcoming reusage of the timekeeper infrastructure for per PTP clock timekeepers, an ID is required to differentiate. Introduce an enum for timekeeper IDs, introduce a field in struct tk_data to store this timekeeper id and add also initialization. The id struct field is added at the end of the second cachline, as there is a 4 byte hole anyway. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner --- include/linux/timekeeper_internal.h | 14 +++++++++++++- kernel/time/timekeeping.c | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -12,6 +12,16 @@ #include =20 /** + * timekeeper_ids - IDs for various time keepers in the kernel + * @TIMEKEEPER_CORE: The central core timekeeper managing system time + * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed + */ +enum timekeeper_ids { + TIMEKEEPER_CORE, + TIMEKEEPERS_MAX, +}; + +/** * struct tk_read_base - base structure for timekeeping readout * @clock: Current clocksource used for timekeeping. * @mask: Bitmask for two's complement subtraction of non 64bit clocks @@ -52,6 +62,7 @@ struct tk_read_base { * @offs_boot: Offset clock monotonic -> clock boottime * @offs_tai: Offset clock monotonic -> clock tai * @coarse_nsec: The nanoseconds part for coarse time getters + * @id: The timekeeper ID * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds * @clock_was_set_seq: The sequence number of clock was set events @@ -101,7 +112,7 @@ struct tk_read_base { * which results in the following cacheline layout: * * 0: seqcount, tkr_mono - * 1: xtime_sec ... coarse_nsec + * 1: xtime_sec ... id * 2: tkr_raw, raw_sec * 3,4: Internal variables * @@ -123,6 +134,7 @@ struct timekeeper { ktime_t offs_boot; ktime_t offs_tai; u32 coarse_nsec; + enum timekeeper_ids id; =20 /* Cacheline 2: */ struct tk_read_base tkr_raw; --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1658,10 +1658,11 @@ read_persistent_wall_and_boot_offset(str *boot_offset =3D ns_to_timespec64(local_clock()); } =20 -static __init void tkd_basic_setup(struct tk_data *tkd) +static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id) { raw_spin_lock_init(&tkd->lock); seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock); + tkd->timekeeper.id =3D tkd->shadow_timekeeper.id =3D tk_id; } =20 /* @@ -1691,7 +1692,7 @@ void __init timekeeping_init(void) struct timekeeper *tks =3D &tk_core.shadow_timekeeper; struct clocksource *clock; =20 - tkd_basic_setup(&tk_core); + tkd_basic_setup(&tk_core, TIMEKEEPER_CORE); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 809F22BEC39; Tue, 13 May 2025 15:13:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149199; cv=none; b=YDvVuxW9mpSkp5ETosFHv1vYjcmmulJ/81qZ6a2eEq5PHtuowkJbL5iB51YFa9itTzQ/x4OF4QwSWl4qgZBS90Q+rbKdNNnZhsWWy3uSffzeMxna6aMlENbwn6GUiuWDRudzfLypae6WeCZP7NeH/ILNOEfBKhwpwHvg20ZbHdk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149199; c=relaxed/simple; bh=VlpD/aEqQPqlO9v6Lb2z2stYvqmnHu9mvPbwUMFbbhs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=lcsLrunBwBipap07H4k7sNvjfScBXFQHD/iAnGg4/fMH803NhIyGUzFAwGdHuSsSb9f37ufj/aU2I5IMfUbMng5Cx9kXPf5kJ3G+aqKKaCYwvOmzd4EU2r2oRv2YiGBpzgQ4m320iKwAdiPdSdJTcLBk7+gYjwAFW9Iz09gs+Tc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=erpG3n5K; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pS1ZUyeC; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="erpG3n5K"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pS1ZUyeC" Message-ID: <20250513145136.968343429@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149184; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=XPHajFVCZoXQl/Q8ekCokP2ZDch4oKNZfnysGAyLhao=; b=erpG3n5K2Z3RhUIhzFJ8R3wNIpHAxnzp7ooKt3R/NTiey3iPrW6Wf8LMLrAJPglS+JfxEK kWuzuLaXHr4OWCnzeyCkq1FFVbIAcUKMUwK2LMrLlCqcAVI2kejmhggQRZoiXGnUFiPpXq Pi1BmtO19Q3ZxqxW6wLIZ8RO1c0OPDitRMMbLRmWfl9uYnxwzwt7De5PUCE79Iujis7sAk 11JznXNGW9muXuM0CNk9CLaXRy3JH21vahSsUwEh49tBHn2t6HqMo1r3X5Qskj3DmrJPPF lZqoQZ2BcifdrNgNwK3AaB9n1tGwuMH/17ifsK3XykcQb/pIxsjm/aI2wezwTw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149184; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=XPHajFVCZoXQl/Q8ekCokP2ZDch4oKNZfnysGAyLhao=; b=pS1ZUyeCa0qJwlATcj2MM9izfMFJO5sakqrVo9qZUHjZF4dfrk/KnOOW/5nzv5dIuypny4 67gmrxXBMN4Io9Bg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 05/26] time: Introduce PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:04 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen To support per PTP clock timekeeping and the related user space interfaces, it's required to define a clock ID range for them. Reserve 8 PTP clock IDs after the regular timekeeping clock ID space. This is the maximum number of PTP clocks the kernel can support. The actual number of supported clocks depends obviously on the presence of PTP devices and might be constraint by the available VDSO space. Add the corresponding timekeeper IDs as well. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner --- include/linux/timekeeper_internal.h | 6 ++++++ include/uapi/linux/time.h | 10 ++++++++++ kernel/time/Kconfig | 3 +++ 3 files changed, 19 insertions(+) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -14,10 +14,16 @@ /** * timekeeper_ids - IDs for various time keepers in the kernel * @TIMEKEEPER_CORE: The central core timekeeper managing system time + * @TIMEKEEPER_PTP: The first PTP timekeeper + * @TIMEKEEPER_PTP_LAST:The last PTP timekeeper * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed */ enum timekeeper_ids { TIMEKEEPER_CORE, +#ifdef CONFIG_POSIX_PTP_CLOCKS + TIMEKEEPER_PTP, + TIMEKEEPER_PTP_LAST =3D TIMEKEEPER_PTP + MAX_PTP_CLOCKS - 1, +#endif TIMEKEEPERS_MAX, }; =20 --- a/include/uapi/linux/time.h +++ b/include/uapi/linux/time.h @@ -64,6 +64,16 @@ struct timezone { #define CLOCK_TAI 11 =20 #define MAX_CLOCKS 16 + +/* + * PTP clock support. PTP clocks are dynamically configured by associating + * a clock ID to a PTP device. The kernel can support up to 16 PTP clocks, + * but the actual limit depends on architecture constraints vs. VDSO. + */ +#define CLOCK_PTP MAX_CLOCKS +#define MAX_PTP_CLOCKS 8 +#define CLOCK_PTP_LAST (CLOCK_PTP + MAX_PTP_CLOCKS - 1) + #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC) #define CLOCKS_MONO CLOCK_MONOTONIC =20 --- a/kernel/time/Kconfig +++ b/kernel/time/Kconfig @@ -57,6 +57,9 @@ config POSIX_CPU_TIMERS_TASK_WORK bool default y if POSIX_TIMERS && HAVE_POSIX_CPU_TIMERS_TASK_WORK =20 +config POSIX_PTP_CLOCKS + def_bool POSIX_TIMERS && PTP_1588_CLOCK + config LEGACY_TIMER_TICK bool help From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 606E32BEC4A; Tue, 13 May 2025 15:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149190; cv=none; b=qAEy5sW6OOt5HrgMg+RetMZwzS0P2SZAYXEQ4cB5msMaECmhdhSPJfdo1DEXbKtQBLfpJZCcIOrmq0HJ3M6q+P0MY7Hj31c7PtL/J0NxCS8p3lteNIV/DMj27pYV4w05J4jRQNkyVEwBMQxGeFvZEkGNRQu0VyT/cpGlzteHsWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149190; c=relaxed/simple; bh=DwwKEIuus+UdOr5RIQ8TMS6/CPXHoaDA2h/kN43G7Ms=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=YJFb4gs/OIEw31EN/PWtoU4VpwkJm3ReCNpo/iKCOm4Lfc+DTujdXZOKQIhyglY359GJiptAL8SUoBYiYNAe/A4a9WxqRcE0PXnIJ0WlDuEuwkkIPaNquH1L8L8i6kl74+QFrzClfOuJZ+t5bKF7MToemWASJ4BK4LIUmngyqWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qQzYXnP6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Cve6WldI; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qQzYXnP6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Cve6WldI" Message-ID: <20250513145137.028405706@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149186; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Lnxo/7P6rkACSSzndKZtUnq8tJQo2w4xcXNLeCMUnV4=; b=qQzYXnP6asRHxkwKy2GJe25QDCWky7oP20bfra8XqZh6Cz6yiV2trxwm/XzwFjDuxf0B2F JPg0QVTL+SK7ZOVwP3iTyQWxKI3NsdqBaCmVxFWKJj2Es30G1WTAqXu2O3TjAw38CSScyP EkynQ/xiKZTNLpFZdVcr7IK3dzghzZcVB0YuVOGNpAXs6Qz8NT3xwjS7FIGAnqqn7jjQ/K HnsqViysSkaDijQ7j76VtgUKzU7WzvLbWW3V/KTGb7fRuJFjRp8st1vpHfZgHWEVUZZMq0 wChlSxINsLKRtL2U77Kz3pSHZ+hoFjcGqeROR3ck2aLhrTv9UhaKjY6fWBEFPg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149186; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Lnxo/7P6rkACSSzndKZtUnq8tJQo2w4xcXNLeCMUnV4=; b=Cve6WldIVPzJkHqLlQDetVQlalP53aIeqgWIn3SZYNbpZdPFGOcMna+ajoxX6XAug8xliD /+DGlosGWbMQI6Cw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 06/26] ntp: Add support for PTP timekeepers References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:05 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If PTP clocks are enabled, provide an array of NTP data so that independent PTP clock timekeepers can be steered accordingly. Signed-off-by: Thomas Gleixner --- kernel/time/ntp.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 #include "ntp_internal.h" #include "timekeeping_internal.h" @@ -86,14 +87,16 @@ struct ntp_data { #endif }; =20 -static struct ntp_data tk_ntp_data =3D { - .tick_usec =3D USER_TICK_USEC, - .time_state =3D TIME_OK, - .time_status =3D STA_UNSYNC, - .time_constant =3D 2, - .time_maxerror =3D NTP_PHASE_LIMIT, - .time_esterror =3D NTP_PHASE_LIMIT, - .ntp_next_leap_sec =3D TIME64_MAX, +static struct ntp_data tk_ntp_data[TIMEKEEPERS_MAX] =3D { + [ 0 ... TIMEKEEPERS_MAX - 1 ] =3D { + .tick_usec =3D USER_TICK_USEC, + .time_state =3D TIME_OK, + .time_status =3D STA_UNSYNC, + .time_constant =3D 2, + .time_maxerror =3D NTP_PHASE_LIMIT, + .time_esterror =3D NTP_PHASE_LIMIT, + .ntp_next_leap_sec =3D TIME64_MAX, + }, }; =20 #define SECS_PER_DAY 86400 @@ -351,13 +354,13 @@ static void __ntp_clear(struct ntp_data */ void ntp_clear(void) { - __ntp_clear(&tk_ntp_data); + __ntp_clear(&tk_ntp_data[TIMEKEEPER_CORE]); } =20 =20 u64 ntp_tick_length(void) { - return tk_ntp_data.tick_length; + return tk_ntp_data[TIMEKEEPER_CORE].tick_length; } =20 /** @@ -368,7 +371,7 @@ u64 ntp_tick_length(void) */ ktime_t ntp_get_next_leap(void) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; ktime_t ret; =20 if ((ntpdata->time_state =3D=3D TIME_INS) && (ntpdata->time_status & STA_= INS)) @@ -389,7 +392,7 @@ ktime_t ntp_get_next_leap(void) */ int second_overflow(time64_t secs) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; s64 delta; int leap =3D 0; s32 rem; @@ -605,7 +608,7 @@ static inline int update_rtc(struct time */ static inline bool ntp_synced(void) { - return !(tk_ntp_data.time_status & STA_UNSYNC); + return !(tk_ntp_data[TIMEKEEPER_CORE].time_status & STA_UNSYNC); } =20 /* @@ -762,7 +765,7 @@ static inline void process_adjtimex_mode int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; int result; =20 if (txc->modes & ADJ_ADJTIME) { @@ -1031,8 +1034,8 @@ static void hardpps_update_phase(struct */ void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 = *raw_ts) { + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; struct pps_normtime pts_norm, freq_norm; - struct ntp_data *ntpdata =3D &tk_ntp_data; =20 pts_norm =3D pps_normalize_ts(*phase_ts); =20 @@ -1083,18 +1086,18 @@ void __hardpps(const struct timespec64 * =20 static int __init ntp_tick_adj_setup(char *str) { - int rc =3D kstrtos64(str, 0, &tk_ntp_data.ntp_tick_adj); + int rc =3D kstrtos64(str, 0, &tk_ntp_data[TIMEKEEPER_CORE].ntp_tick_adj); if (rc) return rc; =20 - tk_ntp_data.ntp_tick_adj <<=3D NTP_SCALE_SHIFT; + tk_ntp_data[TIMEKEEPER_CORE].ntp_tick_adj <<=3D NTP_SCALE_SHIFT; return 1; } - __setup("ntp_tick_adj=3D", ntp_tick_adj_setup); =20 void __init ntp_init(void) { - ntp_clear(); + for (int id =3D 0; id < TIMEKEEPERS_MAX; id++) + __ntp_clear(tk_ntp_data + id); ntp_init_cmos_sync(); } From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 483F42BEC24; Tue, 13 May 2025 15:13:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149192; cv=none; b=cUqRGPOHcjbY7fh0MuuHvFOECsP0YrHiTTKc+7xK0E4kE1Ct06t5w18ykeyPgw4xNcMBh7OXygegehBF/GLDDVC1GwoJwhyzrsmMnMQjHj4lKG/6QeHKcnXOkJ9Q0Bqk5MjtFUfa5luv+AzNwk0/7H44myfeiaGWIpyOyMTky/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149192; c=relaxed/simple; bh=nbID1QPp+mJm1zriYwER3a76w48ZJBK56tf88xhinGY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=BuBQ1yOYSSr0nLGtrQP5Zx+xZbD+i2XuwqCE+rLpd2rSZ77HUl3aPiVoSuhmnp8c2HlKGFPG+bmJMk5tcv19CeZY5z/JY1bKb2EDp74ufIf7qZEODfZoPAGLIFcgmfSVJ4UX+OPKeuw7zfIc6ya/i21TVfzGWsVaZ2vqCMTA96Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=NmA+sGlc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Xb/nKw17; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="NmA+sGlc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Xb/nKw17" Message-ID: <20250513145137.088657076@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=A5j3o52zdWGqvEZkfz36bRjQ7CKt2Dcq+E6I8Zmxcqc=; b=NmA+sGlcpad4cm4nbTh1FZj7xkxB+fqIEa5G3kvP/T2fzzcwWmo592XYoU70Tt6ZjyEzq5 dRbCJ8et07LMHZmq+cpIQl7b6PbGfT/Na8K14oHxMJeIbiBjUObZ4Ym5/n7VLL9jAjomto 7WZf0FPDKicaYtZw9KIe9HogvKqFwJeIrwbf7qzIssYT4whUYzMX5u/G+DCQXCxZ4Uq+R+ nYA21nDW0IhagTQxiQNZeSkxB/vH9IbnRaCgcFD6wIBHwuVbpBCpVueEYATY3qy7V0iMPR tbtpSaj0tsTV86YkYXHkawA5q0Rt880o9ckDbPQxkUBASndeRW++NBKobDSvcA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=A5j3o52zdWGqvEZkfz36bRjQ7CKt2Dcq+E6I8Zmxcqc=; b=Xb/nKw17PUSV668JRiYAHZWvuPjcUsEh9GwXd+5qo/DPTPgaTAQ7HHxz7kolKzBrCd5A9d 3DJ3T9GDcfqn92Cw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 07/26] ntp: Add timekeeper ID arguments to public functions References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:07 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for supporting independent PTP clocks, add a time keeper ID to the relevant functions. Signed-off-by: Thomas Gleixner --- kernel/time/ntp.c | 33 +++++++++++++++++++-------------- kernel/time/ntp_internal.h | 11 +++++------ kernel/time/timekeeping.c | 12 ++++++------ 3 files changed, 30 insertions(+), 26 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -351,33 +351,38 @@ static void __ntp_clear(struct ntp_data =20 /** * ntp_clear - Clears the NTP state variables + * @tkid: Timekeeper ID to be able to select proper ntp data array member */ -void ntp_clear(void) +void ntp_clear(unsigned int tkid) { - __ntp_clear(&tk_ntp_data[TIMEKEEPER_CORE]); + __ntp_clear(&tk_ntp_data[tkid]); } =20 =20 -u64 ntp_tick_length(void) +u64 ntp_tick_length(unsigned int tkid) { - return tk_ntp_data[TIMEKEEPER_CORE].tick_length; + return tk_ntp_data[tkid].tick_length; } =20 /** * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime= _t + * @tkid: Timekeeper ID * - * Provides the time of the next leapsecond against CLOCK_REALTIME in - * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending. + * Returns: For @tkid =3D=3D TIMEKEEPER_CORE this provides the time of the= next + * leap second against CLOCK_REALTIME in a ktime_t format if a + * leap second is pending. KTIME_MAX otherwise. */ -ktime_t ntp_get_next_leap(void) +ktime_t ntp_get_next_leap(unsigned int tkid) { struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; - ktime_t ret; + + if (tkid !=3D TIMEKEEPER_CORE) + return KTIME_MAX; =20 if ((ntpdata->time_state =3D=3D TIME_INS) && (ntpdata->time_status & STA_= INS)) return ktime_set(ntpdata->ntp_next_leap_sec, 0); - ret =3D KTIME_MAX; - return ret; + + return KTIME_MAX; } =20 /* @@ -390,9 +395,9 @@ ktime_t ntp_get_next_leap(void) * * Also handles leap second processing, and returns leap offset */ -int second_overflow(time64_t secs) +int second_overflow(unsigned int tkid, time64_t secs) { - struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; + struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; s64 delta; int leap =3D 0; s32 rem; @@ -762,10 +767,10 @@ static inline void process_adjtimex_mode * adjtimex() mainly allows reading (and writing, if superuser) of * kernel time-keeping variables. used by xntpd. */ -int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, +int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const str= uct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad) { - struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; + struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; int result; =20 if (txc->modes & ADJ_ADJTIME) { --- a/kernel/time/ntp_internal.h +++ b/kernel/time/ntp_internal.h @@ -3,13 +3,12 @@ #define _LINUX_NTP_INTERNAL_H =20 extern void ntp_init(void); -extern void ntp_clear(void); +extern void ntp_clear(unsigned int tkid); /* Returns how long ticks are at present, in ns / 2^NTP_SCALE_SHIFT. */ -extern u64 ntp_tick_length(void); -extern ktime_t ntp_get_next_leap(void); -extern int second_overflow(time64_t secs); -extern int __do_adjtimex(struct __kernel_timex *txc, - const struct timespec64 *ts, +extern u64 ntp_tick_length(unsigned int tkid); +extern ktime_t ntp_get_next_leap(unsigned int tkid); +extern int second_overflow(unsigned int tkid, time64_t secs); +extern int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, co= nst struct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad); extern void __hardpps(const struct timespec64 *phase_ts, const struct time= spec64 *raw_ts); =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -601,7 +601,7 @@ EXPORT_SYMBOL_GPL(pvclock_gtod_unregiste */ static inline void tk_update_leap_state(struct timekeeper *tk) { - tk->next_leap_ktime =3D ntp_get_next_leap(); + tk->next_leap_ktime =3D ntp_get_next_leap(tk->id); if (tk->next_leap_ktime !=3D KTIME_MAX) /* Convert to monotonic time */ tk->next_leap_ktime =3D ktime_sub(tk->next_leap_ktime, tk->offs_real); @@ -678,7 +678,7 @@ static void timekeeping_update_from_shad =20 if (action & TK_CLEAR_NTP) { tk->ntp_error =3D 0; - ntp_clear(); + ntp_clear(tk->id); } =20 tk_update_leap_state(tk); @@ -2044,7 +2044,7 @@ static __always_inline void timekeeping_ */ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) { - u64 ntp_tl =3D ntp_tick_length(); + u64 ntp_tl =3D ntp_tick_length(tk->id); u32 mult; =20 /* @@ -2125,7 +2125,7 @@ static inline unsigned int accumulate_ns } =20 /* Figure out if its a leap sec and apply if needed */ - leap =3D second_overflow(tk->xtime_sec); + leap =3D second_overflow(tk->id, tk->xtime_sec); if (unlikely(leap)) { struct timespec64 ts; =20 @@ -2222,7 +2222,7 @@ static bool __timekeeping_advance(enum t shift =3D ilog2(offset) - ilog2(tk->cycle_interval); shift =3D max(0, shift); /* Bound shift to one less than what overflows tick_length */ - maxshift =3D (64 - (ilog2(ntp_tick_length())+1)) - 1; + maxshift =3D (64 - (ilog2(ntp_tick_length(tk->id)) + 1)) - 1; shift =3D min(shift, maxshift); while (offset >=3D tk->cycle_interval) { offset =3D logarithmic_accumulation(tk, offset, shift, &clock_set); @@ -2581,7 +2581,7 @@ int do_adjtimex(struct __kernel_timex *t } =20 orig_tai =3D tai =3D tks->tai_offset; - ret =3D __do_adjtimex(txc, &ts, &tai, &ad); + ret =3D __do_adjtimex(tks->id, txc, &ts, &tai, &ad); =20 if (tai !=3D orig_tai) { __timekeeping_set_tai_offset(tks, tai); From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECA4B2BF3CC; Tue, 13 May 2025 15:13:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149193; cv=none; b=ZQ2Eo+HLPzJW2cVItBwSXAlzZj5nZ4OmdfC0+WbpiWn+JVWcQDXoO9qjIxcq9sEcy01AIU+f0eh2LewabGA/LHxhoOL66gkMlUAOXZIx6N9ls6PFPXI879Q/GSadhOcYJJ6/mDBnSeZlQU63+d1ofLXzfOGYmPyXmzBJNGsEKpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149193; c=relaxed/simple; bh=nRDu08MROYo+GlEghQBNTn6DIxcSl1cp2QYWZDZ8ZgE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Xo+OEroEJ+kiGP8+qm39CQRwf11OGES3319gMoJnFvGpY1C3a/JlBKvfor+tQFOEiJemJxUl3+UH0XYVY4ZdNDfwqwoIfEUdlT/W3hxPD2P96Isy/Nk6L3MUiOd3k/CMlQ6owZ33s37Op3nD8aoxzvwWF7FzWGEEj3yACw1itio= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dpUZ+NEI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6JN7qSr0; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dpUZ+NEI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6JN7qSr0" Message-ID: <20250513145137.148177019@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=s7TqIjnwY+PkYxiPiQqY19Q4pa5q82PO4/E+388ZxbI=; b=dpUZ+NEIJ1Ejyt2bERI3z9R5xdDDMdefbjIb0kly4HRhvUHY0FRkFgET26Mnnldw5ZeMoh e8UCvqcIt/VnPype7jhKg8McGjDEYJoBp1HFqomwb6hFfRMMW6t9gMquwhil+BbzpXdxaZ Ws3WkygJNxT1GtYn36RBwpUHhL880n0bWl+XFE6o78QhLqPxOu/8sjmlSCL2Wi+BVRuH+I Xd15Vi5QcksD/PI+6gv6MBrN8qXkHGQPtqZVWFek6XxijY1AAcN1M0m0mBxuGdmDWEBxah 5Zlb7nA/fpchuXiHr/SSgOEqIg6Lbvv/a8aVYpilEfh2p6thD5zBaSvOTs21lA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=s7TqIjnwY+PkYxiPiQqY19Q4pa5q82PO4/E+388ZxbI=; b=6JN7qSr0fPBV4eo2Dsun+ZdCupZOj6S5WrAKZQKYM9P8VigweNxo6Hp0LcUWXXFfPjASkS kCtxJyR0+9Lm7LAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 08/26] ntp: Rename __do_adjtimex() to ntp_adjtimex() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:09 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clean up the name space. No functional change. Signed-off-by: Thomas Gleixner --- kernel/time/ntp.c | 4 ++-- kernel/time/ntp_internal.h | 4 ++-- kernel/time/timekeeping.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -767,8 +767,8 @@ static inline void process_adjtimex_mode * adjtimex() mainly allows reading (and writing, if superuser) of * kernel time-keeping variables. used by xntpd. */ -int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const str= uct timespec64 *ts, - s32 *time_tai, struct audit_ntp_data *ad) +int ntp_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const stru= ct timespec64 *ts, + s32 *time_tai, struct audit_ntp_data *ad) { struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; int result; --- a/kernel/time/ntp_internal.h +++ b/kernel/time/ntp_internal.h @@ -8,8 +8,8 @@ extern void ntp_clear(unsigned int tkid) extern u64 ntp_tick_length(unsigned int tkid); extern ktime_t ntp_get_next_leap(unsigned int tkid); extern int second_overflow(unsigned int tkid, time64_t secs); -extern int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, co= nst struct timespec64 *ts, - s32 *time_tai, struct audit_ntp_data *ad); +extern int ntp_adjtimex(unsigned int tkid, struct __kernel_timex *txc, con= st struct timespec64 *ts, + s32 *time_tai, struct audit_ntp_data *ad); extern void __hardpps(const struct timespec64 *phase_ts, const struct time= spec64 *raw_ts); =20 #if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2581,7 +2581,7 @@ int do_adjtimex(struct __kernel_timex *t } =20 orig_tai =3D tai =3D tks->tai_offset; - ret =3D __do_adjtimex(tks->id, txc, &ts, &tai, &ad); + ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &ad); =20 if (tai !=3D orig_tai) { __timekeeping_set_tai_offset(tks, tai); From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BB432BF3D6; Tue, 13 May 2025 15:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149195; cv=none; b=CiUNcCXwRym1KfNvUfo0tcbRiwNND0jHbO8kOuzDb5s9jJJ4XFW1uwVPUq9MwTHl9A8clVIVWAE/GyP53fJO74PmXiymxLVJ4EZzJKB/WW463gEP3IBPmLD24Q9hl8N2ANqh2pFkYLgNPeHOL5hgOdFEXQbB17b/1wQFz6VrP2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149195; c=relaxed/simple; bh=Ts5UXLeF9rqqvuYScy5iGUW+EHVIPfROcD6J6t48R4U=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=s/9s1DRPP4TeYSG27cPcCKcIN0xrU/p21oKKPoT9wbi2f2M6HibJOWjX1ZdFKyi0hGBKd7acP8ecTQAK0YQvdu17uYXP/ZecFbIrk3MJctuIzsN1nmQG5OuF1ac845g6L1h0kPhc1lFyiAsWgHhQoQQ1lsTAb/ilZWmo9wRrBlw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=y8024oJo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uB9K1t+d; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="y8024oJo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uB9K1t+d" Message-ID: <20250513145137.208548292@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=NEZ2sVMSeHxflpomI/Cs7zL+700sJ3oBWUqbPABYo8E=; b=y8024oJoOPyyQ1Rbi6hgNebHGpdy51GAdFikk7D2tkxY6ABldAy7ZrRmCAV69THfz+u3o0 P8LxW84enndY3Z3hw0JqHbMMVYGQNamRThbp8LHqmjLDLbJ5Vqns+kAFos9eAUUwINalr/ suOLAj+WC4Zpyl+l7X6DRyMKdK0nMFI5nbbqJYOHIZeoSa1skbO5AqVx2RZ/tTtf6nh4aF 6SOdh4mRR/AucgitAxEkes+CTONNUPuAe8ruxYHJZuuE0HZrlA6rrdK5N8iCxiOdRXxX9X zAIV0Ph9dKII0y3Kr/OGkjuWQ86hJynJicm+6iSX0zrbU2LPqjR84GM2CoybuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=NEZ2sVMSeHxflpomI/Cs7zL+700sJ3oBWUqbPABYo8E=; b=uB9K1t+dZ3V0MMTFz3HvFlas69Lch5C2TpanHy3CYA7sgUEnAA8d0zTw+SG+lYOYu2Wxyg tna/OhNR/+O7d1Dg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 09/26] timekeeping: Make __timekeeping_advance() reusable References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:11 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen In __timekeeping_advance() the pointer to struct tk_data is hardcoded by the use of &tk_core. As long as there is only a single timekeeper (tk_core), this is not a problem. But when __timekeeping_advance() will be reused for per ptp clock timekeepers, __timekeeping_advance() needs to be generalized. Add a pointer to struct tk_data as function argument of __timekeeping_advance() and adapt all call sites. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2191,10 +2191,10 @@ 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(struct tk_data *tkd, enum timekeeping_ad= v_mode mode) { - struct timekeeper *tk =3D &tk_core.shadow_timekeeper; - struct timekeeper *real_tk =3D &tk_core.timekeeper; + struct timekeeper *tk =3D &tkd->shadow_timekeeper; + struct timekeeper *real_tk =3D &tkd->timekeeper; unsigned int clock_set =3D 0; int shift =3D 0, maxshift; u64 offset, orig_offset; @@ -2247,7 +2247,7 @@ static bool __timekeeping_advance(enum t if (orig_offset !=3D offset) tk_update_coarse_nsecs(tk); =20 - timekeeping_update_from_shadow(&tk_core, clock_set); + timekeeping_update_from_shadow(tkd, clock_set); =20 return !!clock_set; } @@ -2255,7 +2255,7 @@ static bool __timekeeping_advance(enum t static bool timekeeping_advance(enum timekeeping_adv_mode mode) { guard(raw_spinlock_irqsave)(&tk_core.lock); - return __timekeeping_advance(mode); + return __timekeeping_advance(&tk_core, mode); } =20 /** @@ -2593,7 +2593,7 @@ int do_adjtimex(struct __kernel_timex *t =20 /* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D __timekeeping_advance(TK_ADV_FREQ); + clock_set |=3D __timekeeping_advance(&tk_core, TK_ADV_FREQ); } =20 if (txc->modes & ADJ_SETOFFSET) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A568C2BF97A; Tue, 13 May 2025 15:13:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149197; cv=none; b=q6gzkRFpFN12bkBwb248gK05DaYU5IWDi2Okvi6m/6ez6uMsttZ/ywXdNTXK1tOODnu9OdnzHB0+BuxTXGjTtw9DhUdsQNYhA7e1sxaxZeGaAgQIoyTjy1kl2z39/s8mhovhxJYUMEOgL13HiwwHIwH+ESZrorI7lf7+eK6vApg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149197; c=relaxed/simple; bh=cQGO/ovmi8i2iRU2Z3kxXxA6wSOAvbYSAmle/UPq1gg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=WIshCPiH4kc2nQuVgDg3P278/7XGSsl/8+VH89Q8wUwP0E3v06FLDf3VVkvF4MHrAY3xAbaGZ4mS/5Etmi0oGGQUklPxEN/olvXn3XJzplM/AXtoasOhljnY7ykdYzzKuvPr37w5e2u5juF1CtZkJO7fySzcvVNGSgb0RlVo0t4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JJXFwiG+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=G1SVjBGk; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JJXFwiG+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="G1SVjBGk" Message-ID: <20250513145137.269189575@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TEV58ThXX4TBUZI1Gy/a/wWIqOz+0plScOcb5MlAa40=; b=JJXFwiG++gZucr3fnGMd8TIVzC11VmBYMU1gcng2pmXLNe0VvbsLtDTiq87G/a61XXJig9 p94+WwFj+wlDALl31tqq03HPgvKitwt06k8vBNJ5AITWBZ3KE3pDIik2yFskV8bz8KHhfq xxT9w3PMljuxw+3mWIHTO0c4NigVxKl651J2rFEI2qhY3oeqfxoKV6Iy3+41WJhIddVSOt g5JHpPlaWVVliJ5HcA6lRIm3XUQoawp6UqYMwQtv1HnzUrTB3Gz9Yt8XYuIyz1u54MVuPv jT0pci9P6AUOqNrlPHV0PTz5KII+E3Xyl919ULbrEAHHaoz2nJDn+YUvSKxPoQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TEV58ThXX4TBUZI1Gy/a/wWIqOz+0plScOcb5MlAa40=; b=G1SVjBGkQM5qzWAvLjNqX+JHx6zSCi+j/vqjgoOxVSdFPQSPuTU6QFprenN3dO4D9RNYt/ ELrZ3Ja6guEAJQAg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 10/26] timekeeping: Prepare timekeeping_update_from_shadow() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:13 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Don't invoke the VDSO and paravirt updates when utilized for independent PTP clocks. This is a temporary workaround until the VDSO and paravirt interfaces have been worked out. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -683,13 +683,15 @@ static void timekeeping_update_from_shad =20 tk_update_leap_state(tk); tk_update_ktime_data(tk); + tk->tkr_mono.base_real =3D tk->tkr_mono.base + tk->offs_real; =20 - update_vsyscall(tk); - update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); + if (tk->id =3D=3D TIMEKEEPER_CORE) { + update_vsyscall(tk); + update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); =20 - tk->tkr_mono.base_real =3D tk->tkr_mono.base + tk->offs_real; - update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); - update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); + update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); + update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); + } =20 if (action & TK_CLOCK_WAS_SET) tk->clock_was_set_seq++; From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A38402BE0ED; Tue, 13 May 2025 15:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149201; cv=none; b=E5HmflG53fWS9S+jKBLvtl4W9joCwqvV4Tu0XnlPI2qWwykSpbd3O3QpbQ7e9NxwBCYlYPcSte3hVEIoTeehIiq280wf/QayLkL6W6/k+Q1AnTgLojlevlQAyMP1uydz1hJ11BNmYkTojX4LTEFVy45B9ZxMRxqnvnn68ghKCuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149201; c=relaxed/simple; bh=7rlVhwwEK7pkxbrm97gviPqsq5eXgHWjvV7zgZ/ryjU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=m9VgQLHUF98x6DEmXKDr0sNxh1uoNVgPe14dw2o5SRt/glJ2HC+0k0MFM0DnISm7SVLlcT1ZacDBH9HNRoBaG9PseAk9tk/buDQfFdk54TXIJR8xB9JmApVgZKgx9ZpcrVmJRSVb41E0ZUDw2qCBZlaCUetllSnltShD5C9Xm1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JnqjwGYs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qAJMli80; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JnqjwGYs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qAJMli80" Message-ID: <20250513145137.328165847@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=gX7iCiYfGsRnCTExZfiSGn2FqWTPgxe9dSFWPNxUFpM=; b=JnqjwGYsicjKTm11h6ZHQLG/XWQbUnX1ZGRNnWpHzc66X+rLNe0gH1ugL9iSMjhpdc6afO we55p6loqkNALj/P1dQsuiRI32WZ5RrgTVGuS0QRbT4MslSf9SQdhTgtfRgsC4hHSTrTvp GTtCm91rCw/ebH14iZAg7k4tX2nlSWK/KYo9IdCWleD0BS5g+dD1h3o8KZSCY692yEB/b8 u8OHhKBEmM7VG3WmS75C/MDawaQsXVMTPjKQx/DLalEOoeD9R++KpYVQa5aLxCO2RTW6uV 4CLHFul5bMOtrZbEjt5ytS2SQhmFmyt5umGEHrD+S/Bw7k/MJTSvzXew2HHDDQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=gX7iCiYfGsRnCTExZfiSGn2FqWTPgxe9dSFWPNxUFpM=; b=qAJMli802AqcheEh48q9iwOEaFdFiSHfEGKer7k4Zmxuoo+d6aZBfVObm4uIsgPTrN0uw4 FcocIFbCv3BBP/DQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 11/26] timekeeping: Add clock_valid flag to timekeeper References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:15 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner In preparation for supporting independent PTP clock timekeepers, add a clock valid field and set it to true for the system timekeeper. Signed-off-by: Thomas Gleixner --- include/linux/timekeeper_internal.h | 2 ++ kernel/time/timekeeping.c | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -73,6 +73,7 @@ struct tk_read_base { * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds * @clock_was_set_seq: The sequence number of clock was set events * @cs_was_changed_seq: The sequence number of clocksource change events + * @clock_valid: Indicator for valid clock * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset * @cycle_interval: Number of clock cycles in one NTP interval * @xtime_interval: Number of clock shifted nano seconds in one NTP @@ -149,6 +150,7 @@ struct timekeeper { /* Cachline 3 and 4 (timekeeping internal variables): */ unsigned int clock_was_set_seq; u8 cs_was_changed_seq; + u8 clock_valid; =20 struct timespec64 monotonic_to_boot; =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1660,11 +1660,12 @@ read_persistent_wall_and_boot_offset(str *boot_offset =3D ns_to_timespec64(local_clock()); } =20 -static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id) +static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id, bool valid) { raw_spin_lock_init(&tkd->lock); seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock); tkd->timekeeper.id =3D tkd->shadow_timekeeper.id =3D tk_id; + tkd->timekeeper.clock_valid =3D tkd->shadow_timekeeper.clock_valid =3D va= lid; } =20 /* @@ -1694,7 +1695,7 @@ void __init timekeeping_init(void) struct timekeeper *tks =3D &tk_core.shadow_timekeeper; struct clocksource *clock; =20 - tkd_basic_setup(&tk_core, TIMEKEEPER_CORE); + tkd_basic_setup(&tk_core, TIMEKEEPER_CORE, true); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 045C12BFC7E; Tue, 13 May 2025 15:13:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149200; cv=none; b=bO0i+LcHmMubzih78rstwJk77K21YOHs39UFOEpo1m6BAHm4D8xCZBkl083gekMiv7ZHSJkjh/i++I+MR56pKgRjUSsnPTsWfam3+wQNx804DDUR7jFEDGKmgj9IW0YKf7mxvLhnFUSBiSqWL0E4rL2tSTdbwyC7Z+PPSmJvQxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149200; c=relaxed/simple; bh=34XXiqAznQ3sVTAch8u/pFh87au6vUpyvR/l//LqkL8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=MXzRqBt4PC6rLva1vCIlcDajlhS/b/Q2mT/z94ACfdxNzeWaDiORsGNLmpnjGNYJUbK5P6+J6Y+kdd+FyM2Ej1J2ewh8Mbdr7Shh4wW/Je1ur4zzghBuzCl5dJX8/AWww0fMVJ/WuCVeqxPSVvyx/p8RMA4Zt8bqDeltze6OwN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=e0FKeFN1; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3g9MHmSq; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="e0FKeFN1"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3g9MHmSq" Message-ID: <20250513145137.387931544@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=tej1oBVu7uusO7XAHaHs2P/TIn7104f6OCHwxQdNvvU=; b=e0FKeFN1OMutE5OxmiLzIlUqhIh09qa7uJmBBOY/NBGCxbs2G+aTcGuwuBtt0tOeDwYA9W tI7UAHltpnduDpdRYUxmy+2TwSnVHVj/xWIL7FFOrEomnuxduZRiZ0F360Ir/eZjnXEV8P 22E25B3VOmvweYU/8Yy7q5rBO+cw0DlGLpk5svAh3IOg5wUWdhDaahPsvTjDrIdbm0JHvZ 0jzIKn6IupuE9tkfJy+/sTrAe2Qw1xlL7p8g5MgCr+BZBN//brj6R05Gg3SAU0/5fO2DPn j06+ft1nlSMRXeAb3oBr+m9TZgOBRmtKVKkS47f52AMZrsfoIWEOHzSOw9W1nA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=tej1oBVu7uusO7XAHaHs2P/TIn7104f6OCHwxQdNvvU=; b=3g9MHmSqbYwjPQYDmLN0DwrTFqdY78v1iUMiZR+Ru+A4Autr3ZLst6JiRJCNBpeZrx+LF1 J8W/NQF84YWBDXCA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 12/26] timekeeping: Introduce PTP time keepers References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:17 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen Provide time keepers for independent PTP clocks and initialize them during boot. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -53,7 +53,11 @@ struct tk_data { raw_spinlock_t lock; } ____cacheline_aligned; =20 -static struct tk_data tk_core; +static struct tk_data timekeeper_data[TIMEKEEPERS_MAX]; + +/* The core timekeeper */ +#define tk_core (timekeeper_data[TIMEKEEPER_CORE]) + =20 /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; @@ -113,6 +117,12 @@ static struct tk_fast tk_fast_raw ____c .base[1] =3D FAST_TK_INIT, }; =20 +#ifdef CONFIG_POSIX_PTP_CLOCKS +static __init void tk_ptp_setup(void); +#else +static inline void tk_ptp_setup(void) { } +#endif + unsigned long timekeeper_lock_irqsave(void) { unsigned long flags; @@ -1584,7 +1594,6 @@ void ktime_get_raw_ts64(struct timespec6 } EXPORT_SYMBOL(ktime_get_raw_ts64); =20 - /** * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres */ @@ -1696,6 +1705,7 @@ void __init timekeeping_init(void) struct clocksource *clock; =20 tkd_basic_setup(&tk_core, TIMEKEEPER_CORE, true); + tk_ptp_setup(); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && @@ -2625,3 +2635,11 @@ void hardpps(const struct timespec64 *ph } EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ + +#ifdef CONFIG_POSIX_PTP_CLOCKS +static __init void tk_ptp_setup(void) +{ + for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++) + tkd_basic_setup(&timekeeper_data[i], i, false); +} +#endif /* CONFIG_POSIX_PTP_CLOCKS */ From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D8062C030F; Tue, 13 May 2025 15:13:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149202; cv=none; b=B/cr74VcZtGz+zCYujvtSyYvDGH02/YKPNwRQFyKni4qICG7tbjCZjzRlAVaQlh0BLaO6BBv2SadOITXE5krRBxZbvGR0fibSAfMLQCESZfblGXXKZfZmZV7p+A8FCmykBGpAZwKiDSEAWgpvfvFyQBXEr5I3K/AFM+hZrs1PQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149202; c=relaxed/simple; bh=iD+Kcf0L16tFt0BYOWblZLMLXQ/SDxHWJWT4Krt2br0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=sja1eUjWFh8wDaPgFBeJCegB3REh9MIaSCQAaGLTxeA9XbfM/sx33kVXELf5LzeeZ9yyLVfzRcaAknU+f0UXEpNSHgYZdhzqSrY5rEo/UGPzUfTOinZQpE20KQ155M4LNyJ9Jgl1z09Vzz39PQssGtfBCnaNvycYixoI4S9YWFQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cYGZeexd; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Bcd/h5tn; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cYGZeexd"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Bcd/h5tn" Message-ID: <20250513145137.446283287@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=wK3kKijwAh9tE7s5X+OhXxFEfHd8a5JPnG7m6/+F1gk=; b=cYGZeexdo3ogM0CruXYBBARqi4Pk2m3aGVgcGbi1cUlnAqjNEueL8SmPeyCvIky6+ueB+K CS9bgJqdvALrYsaC76cof3Uuq/82NtexlybvITg8hzHGZTvIYr6dhqSCYzkKbocRZJpVob Jc5Um+Ans1/Y0EZKsBMydj5LSdjU5gbAN3GmtMcavRAi7DfUSZeHO871+MwDpb4mk0+qgV +pq6Rpn1IRR0ws/JLXpLDYr/41D1rzjNQMnzeBeUzay+2YJ18AmC1nPo9sZu/sdH9Gofx9 lpRcl4mwSlrd6G+gZ9DPl3da6Oi7clX1V3/h1RhwMzYzNEKszmVp7je0PDKf3w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=wK3kKijwAh9tE7s5X+OhXxFEfHd8a5JPnG7m6/+F1gk=; b=Bcd/h5tncOafB/p7gkWzOKCV7z5793M5+uIm3/t+V9Rxs1WA4dlR/oFew3cCKWpXUAHKGP LdtfL7q7e1dr78DQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 13/26] timekeeping: Provide ktime_get_ntp_seconds() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:18 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ntp_adjtimex() requires access to the actual time keeper per timekeeper ID. Provide an interface. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 9 +++++++++ kernel/time/timekeeping_internal.h | 3 +++ 2 files changed, 12 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2622,6 +2622,15 @@ int do_adjtimex(struct __kernel_timex *t return ret; } =20 +/* + * Invoked from NTP with the time keeper lock held, so lockless access is + * fine. + */ +long ktime_get_ntp_seconds(unsigned int id) +{ + return timekeeper_data[id].timekeeper.xtime_sec; +} + #ifdef CONFIG_NTP_PPS /** * hardpps() - Accessor function to NTP __hardpps function --- a/kernel/time/timekeeping_internal.h +++ b/kernel/time/timekeeping_internal.h @@ -45,4 +45,7 @@ static inline u64 clocksource_delta(u64 unsigned long timekeeper_lock_irqsave(void); void timekeeper_unlock_irqrestore(unsigned long flags); =20 +/* NTP specific interface to access the current seconds value */ +long ktime_get_ntp_seconds(unsigned int id); + #endif /* _TIMEKEEPING_INTERNAL_H */ From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E73E2C0853; Tue, 13 May 2025 15:13:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149204; cv=none; b=L4W6gar333iIW0/5StvHUQJ1VlN9+tiR1Cpy2+MCDI6f0BHBSeoghIXUxMY4iQ0yN2RZcIh9YRs0BG87LnElrsD3R1wCoXuN388q1Njghjq3ProQJlQmIdFoprsSlMrvJIBz1xj46q4ivWnejpkJZDTmqIcLQ3HVJbv6OrjezTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149204; c=relaxed/simple; bh=lB2z5dhrUGj7uJRON5wFn/Mo+auwXxtS7A3p44tVcvI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=YGaiFiI3EAC4JJju4L74hDF7Zuau24El01jxornwZYec62xZ/tVZGWfXi/a2OlFGWb4tO0kgIr7AKiTQzJk98yz/FDNpWMuTO/9EQgRcDSi7QeJEzx2psO8b5yZbopOqqh6V3w8kAVqzYmUoRpiLl4HLX/5axud22DOMeL9t8AQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2EFsIKMO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LDzjieyh; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2EFsIKMO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LDzjieyh" Message-ID: <20250513145137.505757500@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CMH1qXsCMxTYZXTLpTnot3lsaPnEiJx9oFdezOyQlEI=; b=2EFsIKMOilr8dRRjFfxmpyCXUKIjeND4Ov1g3T5gGU8zsaQtJZazTLSb2asmHTFC7IYuHT /rdEXSPaG0TogdEdL3YYd5ps+11Q8VHq9MWv6tRZhV4sP2h+0QnIEzDrPseN9Hd50021a2 qFVtliVvImm26iVZGhylW0GsMWSJGHRuyF7f5uB5814p2EBsIFnWpsVvVaRBXioE4pPjeo SvatDu/if4mpEwY6gRi37SSrf6I9ovU4RLISEqclnB6jmsmyy2Y0hO6I0z7KXCv0ia0WKI MlowdiIgrH0CqITJ5TBXD4LhahapNNomuBO5R6S3nuPVXy1OpwHSxMmSgTjbsQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CMH1qXsCMxTYZXTLpTnot3lsaPnEiJx9oFdezOyQlEI=; b=LDzjieyh1OgSfHM2nqPjRGDzplmHIuyRa6DLASA2ge/Dx7kOxSztLvJRpEYQLjx0XRAHGE ET/dulurQCJFfoAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 14/26] ntp: Use ktime_get_ntp_seconds() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:20 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use ktime_get_ntp_seconds() to prepare for independent PTP clocks so that the readout becomes per timekeeper. Signed-off-by: Thomas Gleixner --- kernel/time/ntp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -303,7 +303,7 @@ static void ntp_update_offset(struct ntp * Select how the frequency is to be controlled * and in which mode (PLL or FLL). */ - real_secs =3D __ktime_get_real_seconds(); + real_secs =3D ktime_get_ntp_seconds(ntpdata - tk_ntp_data); secs =3D (long)(real_secs - ntpdata->time_reftime); if (unlikely(ntpdata->time_status & STA_FREQHOLD)) secs =3D 0; @@ -710,7 +710,7 @@ static inline void process_adj_status(st * reference time to current time. */ if (!(ntpdata->time_status & STA_PLL) && (txc->status & STA_PLL)) - ntpdata->time_reftime =3D __ktime_get_real_seconds(); + ntpdata->time_reftime =3D ktime_get_ntp_seconds(ntpdata - tk_ntp_data); =20 /* only set allowed bits */ ntpdata->time_status &=3D STA_RONLY; From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 259F02C087C; Tue, 13 May 2025 15:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149206; cv=none; b=jlqw+dOnbuNDAJs1Ki4R3E13Ev/8UXAnMXnrNdCw2k0bc5Wr2dvXIEfPm4oiOKQ/wS5eyDFC6MuO0FD48QELC/u/ePjKibIzVmmHcMlJMDNjqsgAyVlJF7NZ2NE607SJ6XDWFZ0rSPOmIUqpSxcq01Bn7UfOHoEVArSZInXs848= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149206; c=relaxed/simple; bh=LhQREkONcb+tLt5GRK6qgTzA/6KmCZWf9yMBEfO/yeQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ty3hni48zZbEEGWLwRGdW/FiS4X1DKHLCKe7AOWcuaxOkMkPT/j+OcQdS6wjWNQ+LPfkgQJBkT5fW6WU64RZfQhE+I8Q7gGrp29w0/5HwUtfwD5KF6Xdr+5VrbfuXJTqch68gBhvVkyGxSn6Z3vF7KkWJUcjNdrp5iKUuv5g5xY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uqv9NzW5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=s6/VxClc; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uqv9NzW5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="s6/VxClc" Message-ID: <20250513145137.563966518@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=8XNmnyHFdARnwNRX2BFu6/xrrFBJ/FwOA2GC3ntaejs=; b=uqv9NzW5/9OF80V7xvFVFXBnft0/dhCLJUmXt4y9TqbnExOnjvyeuGVBMtRLWHVRKYhhiJ F+1YZUOeEz536uuDB/SDXGJfQr1g42fgHD1H0VaVixH20Um5yCKsmwsaSq1/MbgCGuaLB2 LOF7nCovIokYQMxveVZodWvM/wmJj6OD3xBw3Kn1pdQ0kKWSFau0HJdpZij5yDuEMRVLRv 8AMUphIqxj/+ZYuwleAdDiC9z1V86uXFetlH+MY74La1+PMfYXB/lpgx/I9upqQoeseIbP zaIRczVV181IxvBWsWagpX7oZvkgeCfV9YHSQgDzWU+nwQGGJXcGV2u6Z4MpZQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=8XNmnyHFdARnwNRX2BFu6/xrrFBJ/FwOA2GC3ntaejs=; b=s6/VxClc6B5EPIFL8IcDiID+cdJNrqsOvtjk+KyQswhlcz9uPeMIOmYk67SVV0CVYBAGvM GuikIQ806YCP14Bg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 15/26] timekeeping: Add PTP offset to timekeeper References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:22 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This offset will be used in the time getters of independent PTP clocks. It is added to the "monotonic" clock readout. As independent PTP clocks do not utilize the offset fields of the core time keeper, this is just an alias for offs_tai, so that the cache line layout stays the same. Signed-off-by: Thomas Gleixner --- include/linux/timekeeper_internal.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -67,6 +67,7 @@ struct tk_read_base { * @offs_real: Offset clock monotonic -> clock realtime * @offs_boot: Offset clock monotonic -> clock boottime * @offs_tai: Offset clock monotonic -> clock tai + * @offs_ptp: Offset clock monotonic -> clock PTP * @coarse_nsec: The nanoseconds part for coarse time getters * @id: The timekeeper ID * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW @@ -139,7 +140,10 @@ struct timekeeper { struct timespec64 wall_to_monotonic; ktime_t offs_real; ktime_t offs_boot; - ktime_t offs_tai; + union { + ktime_t offs_tai; + ktime_t offs_ptp; + }; u32 coarse_nsec; enum timekeeper_ids id; From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BADB2BE104; Tue, 13 May 2025 15:13:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149208; cv=none; b=UYL9TMZNDXZP8pGkFG+XGsHuQVCYVXh5R4ONwZuk7D3qHk1ZtXdGfDKqTsG/J21uEHpKVhfhocSbHkbzOpD0WRDXt3ZQIOtcjdcFonIepwwlXwqaVaIT9U9bSAiPQioIWY957x8EpssTwIWlcmoEYM+6RkAfsedVuIlew8dYzHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149208; c=relaxed/simple; bh=/2HR3MTj/BexmwQI5vGzCFXQo8FFBtvur8fQXTcZstc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=JalklNRYFhiaid+yCKa5cjxCs32Yv0G3dhm1yZUzaAKRxFl5ZiIq1ahMACWcUF06KdmMGaejTF4x7V9MuAFb35bx+U2yuyLTZe7og9h5RymAmEQLiv3wA0sZqvdxwPte88P1VfXwXEOHdnh/mn262WuIURRJLER6BPygZ4qHHQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=IlN4cJWZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=G5kqH16Q; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="IlN4cJWZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="G5kqH16Q" Message-ID: <20250513145137.623310147@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/NY1OD3UTfB7Efwoanw6NMCyC9WIGsZ7YVimvudzegs=; b=IlN4cJWZyngtmFmSCfybh/aNM5ShLaz2qawe49RxCujSqhPj9KRpBgaKEW6MQC/MMsZcYy yf72xgNgD/n9dXNlzjBfsKwNiPTwEks5lSfO1LTMXuTLwuUXDCfz/iTuU6eBMng5l5nlni JRUt3cHihplahPrkJHcpgKxxcLjAHOj2AmI7dtaAI95/AYLROz1v5+RG0V/SR5gKw+c1Lf vKknoGjM6sLApYOXaHQhTP5hXSIJkkmiKUXjK21gvOqDcdf6YNAkVwmpMHqk7A9tgskclQ ENwafnvXdk7O2KsTHQZL7RhY/eawUP6TYKrX4EdJNC3XaRxVSbXf9s5X/RhWlA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/NY1OD3UTfB7Efwoanw6NMCyC9WIGsZ7YVimvudzegs=; b=G5kqH16QNDzFaAwUDTtl5dwOOi6to9Nwi0gcmTGwY0iW1FRikAsw620q69aLklcDuloU4X u+EkQSaA+tLDzMCw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 16/26] timekeeping: Update PTP timekeepers on clocksource change References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:24 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Propagate a system clocksource change to the PTP timekeepers. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -119,8 +119,10 @@ static struct tk_fast tk_fast_raw ____c =20 #ifdef CONFIG_POSIX_PTP_CLOCKS static __init void tk_ptp_setup(void); +static void tk_ptp_update_clocksource(void); #else static inline void tk_ptp_setup(void) { } +static inline void tk_ptp_update_clocksource(void) { } #endif =20 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); } =20 + tk_ptp_update_clocksource(); + if (old) { if (old->disable) old->disable(old); @@ -2653,6 +2657,30 @@ EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ =20 #ifdef CONFIG_POSIX_PTP_CLOCKS + +/* Bitmap for the activated PTP timekeepers */ +static unsigned long ptp_timekeepers; + +/* Invoked from timekeeping after a clocksource change */ +static void tk_ptp_update_clocksource(void) +{ + unsigned long active =3D READ_ONCE(ptp_timekeepers); + unsigned int id; + + for_each_set_bit(id, &active, BITS_PER_LONG) { + struct tk_data *tkd =3D &timekeeper_data[id + TIMEKEEPER_PTP]; + struct timekeeper *tks =3D &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_ptp_setup(void) { for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61FE72C10BE; Tue, 13 May 2025 15:13:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149209; cv=none; b=ZH6T6jwXleptFZcypgcN3xzI00xQSQbKEePTDiOSY14lJ12Xf2n3YprSRvuKatDVrmxS8sojbpEo3Il57kuu0Ubwqr/BK5vecbJzdvN8v0P29qjxHCzveirJJshr/FQdIj4kaJ+81Y89sMjVFbdhMzZOgTI+ILQmU3CkXMWrtxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149209; c=relaxed/simple; bh=5lElRvz1sQzjfrIw2oIEVw2arW/2AmLQNPA1DgZzvlA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=QMw9b/Y3vU/3HOYoYbjFqpLZBMDNrQcmuUltPFMlTwva7vpS1zUMzU0lRKwbPda4zLWyzqy/Q8SasCutqAFdLNBs88bd03BLm2VhNKenVA+gaud1bCR5uuCmhMmBMnsWJ1r13UHMqePYgwCqB0Jnv8P/r1Z4j1iFL6h9zx5wH9k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=axBWjT/n; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ajPnB9Hv; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="axBWjT/n"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ajPnB9Hv" Message-ID: <20250513145137.681496427@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aWJhQt8zoJ/Z+lvXczMnNhXrfMyJvGibEQIfA9TRM7E=; b=axBWjT/n2v0gCIJ9KZQFiZTE9Ixnw0xx0+6wTyBPseD5m10h+Gk95dB8ZbCfM1jA78WJcH yZxTLHef2WcgcdiNrJNjfvOtfSN8Bokn8oM5yWFYu/JTOSu1afDoS5rWjq8JDyov9Es916 dbiDwSkqF74rFlRhWOa7HO2JTwFcFAW8i8PTzhC/uZ0ByFBbfAC+xzeBnUcEvHNX7llsrt Vv4a/MWhfZ8FIwUl1WU5yIYoaTYPYQVXkPynsJtvc4zQQQbbazgs0j0o2jnF7lWKz9Lh1n 0s8JRhmwPXKw8VI2MAbHrnxMNVxYw40p1vh1wIaos5V0hN/8wR4BNKv/Jhbjdw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aWJhQt8zoJ/Z+lvXczMnNhXrfMyJvGibEQIfA9TRM7E=; b=ajPnB9Hv2Qefn4FAQ+nniQHfyFKGjRwBqHa2YZZfmv8DyUloidbmr2G60uI/ogjRuEph9C dEG443pObGL4uyDA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 17/26] timekeeping: Provide time getters for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:26 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Provide interfaces similar to the ktime_get*() family which provide access to the independent PTP clocks. These interfaces have a boolean return value, which indicates whether the accessed clock is valid or not. Signed-off-by: Thomas Gleixner --- include/linux/timekeeping.h | 17 ++++++++++++ kernel/time/timekeeping.c | 62 +++++++++++++++++++++++++++++++++++++++= +++++ 2 files changed, 79 insertions(+) --- --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -60,6 +60,17 @@ extern time64_t __ktime_get_real_seconds extern time64_t ktime_get_real_seconds(void); =20 /* + * PTP clock interfaces + */ +#ifdef CONFIG_POSIX_PTP_CLOCKS +extern bool ktime_get_ptp(clockid_t id, ktime_t *kt); +extern bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *kt); +#else +static inline bool ktime_get_ptp(clockid_t id, ktime_t *kt) { return false= ; } +static inline bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *kt)= { return false; } +#endif + +/* * ktime_t based interfaces */ =20 @@ -263,6 +274,12 @@ extern bool timekeeping_rtc_skipresume(v =20 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta); =20 +/* + * PTP clocks + */ +bool ktime_get_ptp(clockid_t ptp_clock_id, ktime_t *ts); +bool ktime_get_ptp_ts64(clockid_t ptp_clock_id, struct timespec64 *ts); + /** * struct system_time_snapshot - simultaneous raw/real time capture with * counter value --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2661,6 +2661,23 @@ EXPORT_SYMBOL(hardpps); /* Bitmap for the activated PTP timekeepers */ static unsigned long ptp_timekeepers; =20 +static inline bool ptp_valid_clockid(clockid_t id) +{ + return id >=3D CLOCK_PTP && id <=3D CLOCK_PTP_LAST; +} + +static inline unsigned int clockid_to_tkid(unsigned int id) +{ + return TIMEKEEPER_PTP + id - CLOCK_PTP; +} + +static inline struct tk_data *ptp_get_tk_data(clockid_t id) +{ + if (!ptp_valid_clockid(id)) + return NULL; + return &timekeeper_data[clockid_to_tkid(id)]; +} + /* Invoked from timekeeping after a clocksource change */ static void tk_ptp_update_clocksource(void) { @@ -2681,6 +2698,51 @@ static void tk_ptp_update_clocksource(vo } } =20 +/** + * ktime_get_ptp - Get TAI time for a PTP clock + * @id: ID of the clock to read (CLOCK_PTP...) + * @kt: Pointer to ktime_t to store the time stamp + * + * Returns: True if the timestamp is valid, false otherwise + */ +bool ktime_get_ptp(clockid_t id, ktime_t *kt) +{ + struct tk_data *tkd =3D ptp_get_tk_data(id); + struct timekeeper *tk; + unsigned int seq; + ktime_t base; + u64 nsecs; + + WARN_ON(timekeeping_suspended); + + if (!tkd) + return false; + + tk =3D &tkd->timekeeper; + do { + seq =3D read_seqcount_begin(&tkd->seq); + if (!tk->clock_valid) + return false; + + base =3D ktime_add(tk->tkr_mono.base, tk->offs_ptp); + nsecs =3D timekeeping_get_ns(&tk->tkr_mono); + } while (read_seqcount_retry(&tkd->seq, seq)); + + *kt =3D ktime_add_ns(base, nsecs); + return true; +} +EXPORT_SYMBOL_GPL(ktime_get_ptp); + +bool ktime_get_ptp_ts64(clockid_t id, struct timespec64 *ts) +{ + ktime_t now; + + if (!ktime_get_ptp(id, &now)) + return false; + *ts =3D ktime_to_timespec64(now); + return true; +} + static __init void tk_ptp_setup(void) { for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCC102C17BA; Tue, 13 May 2025 15:13:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149213; cv=none; b=G6JAHb+kiURbDiCrLYnMppQ0FkjlWCDBaO8ah2yHeTWfx99TeZK8Tb+u6StUPeWLkqMu9axBRUinayEJg8utf8JSVaurPJsvbxR5CWEyOOLpgQgie1iIrkKC5Rpv2fNixuYIiL6lKL+Gl7CrzwE4Xgp1aHXZijXWGcyLzYG73ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149213; c=relaxed/simple; bh=hAz5q0VQ9Y6FyczM+6q/3+WlPu4wpj1SSncb9OxeL3o=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=nrNVugBh2Yx0G5lGUJ6JCi4gHftgujkkY/k65XcG7cVRAw1W/yZVVl0vK5b2oxEbrv6wY92YQARnc95NvU/yLnaWCFKka+ndBu3h8ZsYrc3AkrU5wFgmjLtptDYTHhfyXWDK1OS4KM+dJPd50P1ZE8PK0DYoRGTJn6xJti9u9+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OFS1kXCX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qWjUTurJ; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OFS1kXCX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qWjUTurJ" Message-ID: <20250513145137.740202418@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=xQLmb6LJzfcom1fG3G9nnmc6HzchRgiWvlWeZwisPhI=; b=OFS1kXCX//2vT6SrwQ7tErP3tug+zIFeInlbWFy/uK6odA2F1oGwSMRPfVMjYqnQmrkfQu 03/LOQ/77HRyTrH+wYWyo/hT/bJfHuK7bq+p0NqW6MQpHbbTq55NVi316rcx+WtVLUrPmF 3oTmPP6jhCjiAT2mAbwf3dD2TvFsqCAPJEdiOHqqtSUF/mQf9acXRRI1d16M9eRztV/aU4 I4h/BBq6epr9ssrfaliIc7BaEmgnpVXE2gFaDq1w/dY/vjsdBpRCzzJwZg1BGuj5VATB6v 0HyQlSMtZX6+cvOalYfQ7ETM9YDrKSCUyPyTH1W769zKYwmmNZqey7rDrRHjFQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=xQLmb6LJzfcom1fG3G9nnmc6HzchRgiWvlWeZwisPhI=; b=qWjUTurJtnv51mFjeAIvBJ9v/Lsa7yWrMPjLG5dhJxYbZVtMCNKGCheAslz9fBFWMtlLYW E4K2Je4pgbusuICw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 18/26] timekeeping: Add minimal posix-timers support for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:28 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Provide clock_getres(2) and clock_gettime(2) for PTP clocks. Signed-off-by: Thomas Gleixner --- kernel/time/posix-timers.c | 3 +++ kernel/time/posix-timers.h | 1 + kernel/time/timekeeping.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) --- --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1526,6 +1526,9 @@ static const struct k_clock * const posi [CLOCK_REALTIME_ALARM] =3D &alarm_clock, [CLOCK_BOOTTIME_ALARM] =3D &alarm_clock, [CLOCK_TAI] =3D &clock_tai, +#ifdef CONFIG_POSIX_PTP_CLOCKS + [CLOCK_PTP ... CLOCK_PTP_LAST] =3D &clock_ptp, +#endif }; =20 static const struct k_clock *clockid_to_kclock(const clockid_t id) --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -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_ptp; =20 void posix_timer_queue_signal(struct k_itimer *timr); =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2655,6 +2655,7 @@ EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ =20 #ifdef CONFIG_POSIX_PTP_CLOCKS +#include "posix-timers.h" =20 /* Bitmap for the activated PTP timekeepers */ static unsigned long ptp_timekeepers; @@ -2741,6 +2742,26 @@ bool ktime_get_ptp_ts64(clockid_t id, st return true; } =20 +static int ptp_get_res(clockid_t id, struct timespec64 *tp) +{ + if (!ptp_valid_clockid(id)) + return -ENODEV; + + tp->tv_sec =3D 0; + tp->tv_nsec =3D 1; + return 0; +} + +static int ptp_get_timespec(clockid_t id, struct timespec64 *tp) +{ + return ktime_get_ptp_ts64(id, tp) ? 0 : -ENODEV; +} + +const struct k_clock clock_ptp =3D { + .clock_getres =3D ptp_get_res, + .clock_get_timespec =3D ptp_get_timespec, +}; + static __init void tk_ptp_setup(void) { for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D3672C1E00; Tue, 13 May 2025 15:13:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149214; cv=none; b=koBUdH2ZtLBabN32pNGZuUlilAaEXYlNcVZh9MYq3TzAOG+k11bA8IVtg/25GkOZXkC5xMHRcN5o+p3htEV4D7jmGEDUus12oAyrfqVN0RvNZer60s5Q2r4dd6l6ZGxSAv7aGA9NsFQnzryPdeUvlhj0zidirmZ7w1/Z06hzMKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149214; c=relaxed/simple; bh=Z3OeIgNNvf+8U8VB8QsiVfDcY9uW7DIKl84gjExM2zY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OsiLTtFpZ5jKbQjGBPKKoIBMYbFjaciWCvb/doBNXDPYQ8hvAFptS/vyySc78rUsZGxIbP46Vq5v6lKpKmYeovtNr2ugbntRK3ByKGegiK4yq5Ogm1oHLanYlXQlDEJcaRYLiaUHOO/xk8NQwzdurCjIoLHmpkBn1s79pZEuvxY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=NMZXkrIO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=VGc7tcpW; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="NMZXkrIO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="VGc7tcpW" Message-ID: <20250513145137.799907954@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149210; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/YoB2Y/szX/3aLjPyHcg6yaHtAbNxEpl5yLobOxiZKI=; b=NMZXkrIO0c/JypbhWv9LrorjGB3VdMX1Q3X/aIFmrmiYhgPsBN2l/bCZChg43HmtHnSPXg casq8pmhPQaoDqjAAXYeayP99ILpHy6/VLrC/NPMWW3ZOK+OYaeieYHKqUEw8Zo3T9lBh4 /6DG9Svjq7GCWHJownNtEwOee1UgtwziRT2Niy5Ohrmpu8mc33ADcQmPxBFBtvx2IciIv/ nyMAP848DlkT1EjLIossoSOJ2w4olP/+sVon+2eWGfi2gxV2sg0GY/qMwJzWnMC7VhbnTj Rf14P+i/i5Mqmko5NpoUqYFzBJebOiKjb/OZerbuoT5OsYwS3M3Ga7c3/uETew== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149210; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/YoB2Y/szX/3aLjPyHcg6yaHtAbNxEpl5yLobOxiZKI=; b=VGc7tcpWniRriVVr83N+/ev9FzqjDYtEhbsqBMaRD8At6ikrxN4ecX3G2LZJJvPbFOqKBz J6ZPH1VohS/iUNBA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 19/26] timekeeping: Provide time setter for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:30 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add clocK_settime(2) support for PTP clocks. The function affects the PTP offset which is added to the "monotonic" clock readout. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2759,9 +2759,48 @@ static int ptp_get_timespec(clockid_t id return ktime_get_ptp_ts64(id, tp) ? 0 : -ENODEV; } =20 +static int ptp_clock_set(const clockid_t id, const struct timespec64 *tnew) +{ + struct tk_data *tkd =3D ptp_get_tk_data(id); + struct timekeeper *tks; + ktime_t tnow, nsecs; + + if (!timespec64_valid_settod(tnew)) + return -EINVAL; + if (!tkd) + return -ENODEV; + + tks =3D &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 =3D timekeeping_cycles_to_ns(&tks->tkr_mono, tks->tkr_mono.cycle_la= st); + tnow =3D ktime_add(tks->tkr_mono.base, nsecs); + + /* Calculate the new PTP offset */ + tks->offs_ptp =3D ktime_sub(timespec64_to_ktime(*tnew), tnow); + + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); + return 0; +} + const struct k_clock clock_ptp =3D { .clock_getres =3D ptp_get_res, .clock_get_timespec =3D ptp_get_timespec, + .clock_set =3D ptp_clock_set, }; =20 static __init void tk_ptp_setup(void) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DD892C1E15; Tue, 13 May 2025 15:13:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149215; cv=none; b=p1sorM56woU0nh7DJ3/x2zMNPoVXxZ35fx70HZOuBJIQXH5V8Oekj2ohgfpzpR/cYqPntkRnMziw3MZGG7uDABg4PIP2wjiF066g8fKrFeCAtoZiAhkit+AmMvBuyI6ajHjWxL7U7V/+srhUQPM9YWNxHrn9gFnnOC8TRSTK0kY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149215; c=relaxed/simple; bh=XNPSQ1Hy854FzcyRYag43LIzOLmKfi3VSE/hwFsgdag=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Dwq+qSUU6pUPoC/REfHXaR3V0DtQ+XkWr1eST/F1fVrXrWPDRUUs7FHXAWNiW/aigEoDY14ZK0D+1P9SsgbyzGSQETc8Ui8z2Sw5aXPHI+oyY36w8L7xuV39TB/buYUexVm6FPHIc6ZJIPVmJ4FoPTW+cbcWcD1b1WyLEr76zPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=iI1B4T7x; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tIe/ebI4; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="iI1B4T7x"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tIe/ebI4" Message-ID: <20250513145137.861047599@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149212; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=IKbUr1poQ8Wwcs8+3rNGvdlPp/sUv73Qu/xVzEO/Kaw=; b=iI1B4T7x0ukNkaVf0hVbUFuJEKVnjzeAfWBoouwsaOPEwWgXA9S2X6YeVbgGs5Xh1TTO/y ql0sg9p1ClgiyA2pSdGsrwdXkGl5kIRMKuqkC1MInJFp51IFY6nWsojnSwFkGJUylvJzTK MBIOCmgKTYmB4/vEzzHNPpI3CL+j0aVpYEEShsbF4nhoOPfwDTIbM1PoMMaGgt0HlkcNFW v6oNX04/7zdsnmwsShN+0vXumsXYFzSh3jxsuaaTDKjGWeEgev3KCJboedYbLkSYHtgQiA AQeH0YneyHOah/ctGIvC3RPIwXQpmltsOSIxRfJWPS0Oqna8FNyCnOfwkjb4DQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149212; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=IKbUr1poQ8Wwcs8+3rNGvdlPp/sUv73Qu/xVzEO/Kaw=; b=tIe/ebI4+XkvpNyXJDM6JsfP67IRTFcJmZWOxE7346ur12wDlggss87j8CLOTqyG09c6EO hhJgmr4D4o7/gPCw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 20/26] timekeeping: Make timekeeping_inject_offset() reusable References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:32 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split out the inner workings for PTP clock support and feed the core time keeper into it. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1433,32 +1433,32 @@ EXPORT_SYMBOL(do_settimeofday64); =20 /** * __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 t= imespec64 *ts) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; + struct timekeeper *tks =3D &tkd->shadow_timekeeper; struct timespec64 tmp; =20 if (ts->tv_nsec < 0 || ts->tv_nsec >=3D NSEC_PER_SEC) return -EINVAL; =20 - timekeeping_forward_now(tks); =20 /* Make sure the proposed value is valid */ tmp =3D 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; } =20 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; } =20 @@ -1467,7 +1467,7 @@ static int timekeeping_inject_offset(con int ret; =20 scoped_guard (raw_spinlock_irqsave, &tk_core.lock) - ret =3D __timekeeping_inject_offset(ts); + ret =3D __timekeeping_inject_offset(&tk_core, ts); =20 /* 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 =3D &tk_core; struct timespec64 delta, ts; struct audit_ntp_data ad; bool offset_set =3D false; @@ -2585,16 +2586,19 @@ int do_adjtimex(struct __kernel_timex *t ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); =20 - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; + scoped_guard (raw_spinlock_irqsave, &tkd->lock) { + struct timekeeper *tks =3D &tkd->shadow_timekeeper; s32 orig_tai, tai; =20 + if (!tks->clock_valid) + return -ENODEV; + if (txc->modes & ADJ_SETOFFSET) { delta.tv_sec =3D txc->time.tv_sec; delta.tv_nsec =3D txc->time.tv_usec; if (!(txc->modes & ADJ_NANO)) delta.tv_nsec *=3D 1000; - ret =3D __timekeeping_inject_offset(&delta); + ret =3D __timekeeping_inject_offset(tkd, &delta); if (ret) return ret; =20 @@ -2607,7 +2611,7 @@ int do_adjtimex(struct __kernel_timex *t =20 if (tai !=3D 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 =3D true; } else { tk_update_leap_state_all(&tk_core); @@ -2615,7 +2619,7 @@ int do_adjtimex(struct __kernel_timex *t =20 /* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D __timekeeping_advance(&tk_core, TK_ADV_FREQ); + clock_set |=3D __timekeeping_advance(tkd, TK_ADV_FREQ); } =20 if (txc->modes & ADJ_SETOFFSET) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2280D2C2AA8; Tue, 13 May 2025 15:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149217; cv=none; b=ltymLq5YQYFKt7Eo2D30vPiWyToLyJNVcvgtQmx6rTELTD9+oitOGojvUAvLAvOmmxsi4xBHeOvoXP1HUIN5kLuNP4awgg2Ao/OIUpZQ4ue0x3LnSVpRDdNugpNm6M+V+i/62bc33qhL75iIVS0wGAKrki8qGoay0q+w72xlpi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149217; c=relaxed/simple; bh=q0OLLkf2kUwS/yqPJjKV/EGMjakB97BItyqHxvQT36k=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=VehzDRxeP3Xed58cEtBoLBI1f1Lxou42ySPu1PZ6sKZOqDYsOxwkBTqBNx6tbtQ68CzTUVNUIMBtgDvfYEFAWJtz9oJ8WnSkgMTSa5DmWxLxNWpHycpaoPyq74HygP66b4Qj2ZBiYEyvE1bLK+OlmPai20Ro7uH2c7sBFKo0BrM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fheNSDQK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Y0ET2vDb; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fheNSDQK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Y0ET2vDb" Message-ID: <20250513145137.919542613@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=c3VDQSpxT2VVjCswcVkkZBuHllPOrYNSkdgO67m/MB8=; b=fheNSDQKMNENPum6m4F9BC4fjQceYCWPNb8dCdG+bPOPj9Ug2Lkxkycai66yVCi6e+kJjW QeBhk5A8GuwExw7wPOj/VwUmsdg1kRq732ObIS3SKcOjlCkng3WrkvwWeGMIGHnNwjxUZD Q3hE2CKkvRY6jU1ytmoLBO9LjWf9xLtBHdZb2ndiLIqwF6RaSOyUHeCXxywFDvWKavKr/D 3EFlqJ14goXPC2Tx1/T7/2Rc/wn3o/acJRs1NFkYPqsF7JuTFdcCEZQiCL6khg7HRGgs+r 04m3kOChV+WYGjRX/x16PHy6FzESDI7TCoQyfacbtjAT26THi6k7FCrtQsYNCw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=c3VDQSpxT2VVjCswcVkkZBuHllPOrYNSkdgO67m/MB8=; b=Y0ET2vDbQcr24pKUVwgttwD15oau71IAMWtEuLLmrTkI+VYLZe/9tuLQeioyakBAmWsB04 cwlwejXWAqXiWfDQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 21/26] timekeeping: Add PTP clock support to __timekeeping_inject_offset() References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Redirect the relative offset adjustment to the PTP clock offset instead of modifying CLOCK_REALTIME, which has no meaning in context of these clocks. Signed-off-by: Thomas Gleixner --- 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 =20 timekeeping_forward_now(tks); =20 - /* Make sure the proposed value is valid */ - tmp =3D 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_PTP_CLOCKS) || tks->id =3D=3D TIMEKEEPER_COR= E) { + /* Make sure the proposed value is valid */ + tmp =3D 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 =3D &tks->tkr_mono; + ktime_t now, offs; + + /* Get the current time */ + now =3D ktime_add_ns(tkr_mono->base, timekeeping_get_ns(tkr_mono)); + /* Add the relative offset change */ + offs =3D ktime_add(tks->offs_ptp, 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_ptp =3D offs; } =20 - 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; } From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F8842C2ACF; Tue, 13 May 2025 15:13:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149219; cv=none; b=SbUi3K78HLmZNrers8kl5rC9du4yyBNrsyjLjbxlyc/6nexGtdvK3TEH2IYRiqK9mMyrcKjbGNGtYgi2E7TjclrNX8LMwh4SSBWL+KlQUJR2F4WKedc+zDK5x/r8DsKBwOVJYxam4GjTmSUHvVZmuc0w+RjWlisr3pDJbib/tEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149219; c=relaxed/simple; bh=8amrdeLjP6iuVIcxRPgHu0Aegf/+POD14kN7I5mAUEE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=K8QzwRjN1YgTFS+gFWSk6sI//dkS10MfmzAUfEf9t2ItmFWZSF/fbtPCvWjX7gJ8/R5IuGsH04C3edZj1blykdwKZolm2Vvvl7hLYMc5TPiGMv86lVkTHMyw+QfK23xCrBJslrY8pmr18VEb+f2T8YrBG+ZljDUwtQD2QSdvx24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FLHkemHn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=l7it4b20; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FLHkemHn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="l7it4b20" Message-ID: <20250513145137.978734056@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Fh0otM2e5g5OQPHBjloAe8TGbXEVLZ/A+WiYmFpBvA4=; b=FLHkemHnwxgS1nYlj/mSpN94DjlF2lFdJ4q+AikqpUFCrtxAahLQBuZaCiJSThoEebr6Gz LY8WT1yOs0E9ghk4zXc++51ILH7qbwfmnPwk+/km9rORbbQ8aLL3GUffoIga8Ej1FRJGIP 0fxml6WmS68aOdQawIrdTgO8v2H17ixqDmqPnFspFYjnk13ZvB5d2d8myyaUxHMR8uxcQG 92kTBTv/dk+Qj8Qw02Xm65+/OLEUu86nSMJEHv6N7V4++17OEjml0gjP9WcQNWITqS5c4a ql7wLHcC+WCVqTV/6z7mKntQd/fKhfjTXwDRHM4SZKQ0Ts/yOTCZFctlTCwVVQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Fh0otM2e5g5OQPHBjloAe8TGbXEVLZ/A+WiYmFpBvA4=; b=l7it4b20CxiX7ogAsGnECBUuNWqUG4M1MJ0h+c7uSaU3hSATsU7b21wpdNkBHP5/VvFln6 wAy1UTXnCWm4dxAg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 22/26] timekeeping: Make do_adjtimex() reusable References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:35 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 independent PTP clocks. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 110 +++++++++++++++++++++++++----------------= ----- 1 file changed, 60 insertions(+), 50 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2580,17 +2580,18 @@ unsigned long random_get_entropy_fallbac } EXPORT_SYMBOL_GPL(random_get_entropy_fallback); =20 -/** - * 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 =3D &tk_core; - struct timespec64 delta, ts; - struct audit_ntp_data ad; - bool offset_set =3D false; - bool clock_set =3D false; + struct timekeeper *tks =3D &tkd->shadow_timekeeper; + struct timespec64 ts; + s32 orig_tai, tai; int ret; =20 /* Validate the data before disabling interrupts */ @@ -2599,56 +2600,65 @@ int do_adjtimex(struct __kernel_timex *t return ret; add_device_randomness(txc, sizeof(*txc)); =20 - audit_ntp_init(&ad); - ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); =20 - scoped_guard (raw_spinlock_irqsave, &tkd->lock) { - struct timekeeper *tks =3D &tkd->shadow_timekeeper; - s32 orig_tai, tai; - - if (!tks->clock_valid) - return -ENODEV; - - if (txc->modes & ADJ_SETOFFSET) { - delta.tv_sec =3D txc->time.tv_sec; - delta.tv_nsec =3D txc->time.tv_usec; - if (!(txc->modes & ADJ_NANO)) - delta.tv_nsec *=3D 1000; - ret =3D __timekeeping_inject_offset(tkd, &delta); - if (ret) - return ret; - - offset_set =3D delta.tv_sec !=3D 0; - clock_set =3D true; - } - - orig_tai =3D tai =3D tks->tai_offset; - ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &ad); - - if (tai !=3D orig_tai) { - __timekeeping_set_tai_offset(tks, tai); - timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET); - clock_set =3D 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 |=3D __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 =3D txc->time.tv_sec; + result->delta.tv_nsec =3D txc->time.tv_usec; + if (!(txc->modes & ADJ_NANO)) + result->delta.tv_nsec *=3D 1000; + ret =3D __timekeeping_inject_offset(tkd, &result->delta); + if (ret) + return ret; + result->clock_set =3D true; + } + + orig_tai =3D tai =3D tks->tai_offset; + ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &result->ad); + + if (tai !=3D orig_tai) { + __timekeeping_set_tai_offset(tks, tai); + timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET); + result->clock_set =3D true; + } else { + tk_update_leap_state_all(&tk_core); } =20 + /* Update the multiplier immediately if frequency was set directly */ + if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) + result->clock_set |=3D __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 =3D { }; + int ret; + + ret =3D __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); =20 - audit_ntp_log(&ad); + audit_ntp_log(&result.ad); =20 - if (clock_set) + if (result.clock_set) clock_was_set(CLOCK_SET_WALL); =20 - ntp_notify_cmos_timer(offset_set); + ntp_notify_cmos_timer(result.delta.tv_sec !=3D 0); =20 return ret; } From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4600B2C2FB9; Tue, 13 May 2025 15:13:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149221; cv=none; b=LiqbL2Bs1JPsRqXp/OoNWA0GVSej937aJ2j73lPGapQjE5sulqQmm2auFMIXKXN+pqTxZCLAGaphlcE2IiIq7N7m3LxpXNyWGFBDgRhBIP2fWgtR6zP4SXVEFCRJqVP+hLCe5doIqQn9EJqfO96hBYFKLbfB2Jx6uMZeodprIDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149221; c=relaxed/simple; bh=iJJ9ph2Xb8jo5inYrka+dOkNKYSGMVeqGO5dfJupNQc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=sdgeN+LnHNgIvIhIQx3phPj++1c5uekKsUtSAiqsxnPTiKHb8kDzS1eEATsmWgO3kfw4SuR7uXmnSjBRHrxp8Neh6rDbzINxI/tYb4L4JJBWJBYZX4alhtgCPdyLftEaBa9QLAj/G95bR/SMnRu52dUW2opBGAVBU+zkvZAP7hA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=iinsoVwS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=weCmPHnU; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="iinsoVwS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="weCmPHnU" Message-ID: <20250513145138.036707094@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=sshZ83NzzE8YwIcZ21KwrOb8k7lJw16Y76dziNXGJq4=; b=iinsoVwSSQ4rno6wgH/7CG/WCmCFxpqZZmIKf1CPSZOJgHPIPU59l7aK19zeTxRXQdTBDW Hp+JEG8zFoLX2Q7ty9lx5hjSRvQjr7pc0MH21OdBVHeDa1v/c588FtuEfY0Mt5Hbqv0hzY UXKRDluebUe4ojqFLUe5ZwBtPedG9tRVcH6lSQaKbrmyFAOVQ2DTBJE4tvKN+8GcHxa5lg 5A5eAH99OmjY0fGGGFfHei2Ff7VW+wWHBA9vX3/DocRLkx+1iHK3Yp97Q+uB+gLBZlJ1v2 okQBZo6o3DoCet1APS5tgew2p+9kiEXCCAyVuOFE3emPOK4MK98I77QGTXjnhA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=sshZ83NzzE8YwIcZ21KwrOb8k7lJw16Y76dziNXGJq4=; b=weCmPHnU4cCjXT4IfsvBFa8s1G9nBz9jDWQZJePA3J01e4Y7JWwktrD+G91tgGoXWJ/eEq 30GlzevJ35SOOxBw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 23/26] timekeeping: Prepare do_adtimex() for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:37 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exclude ADJ_TAI, leap seconds and PPS functionality and provide a time stamp based on the actual clock. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -58,6 +58,17 @@ static struct tk_data timekeeper_data[TI /* The core timekeeper */ #define tk_core (timekeeper_data[TIMEKEEPER_CORE]) =20 +#ifdef CONFIG_POSIX_PTP_CLOCKS +static inline bool tk_get_ptp_ts64(unsigned int tkid, struct timespec64 *t= s) +{ + return ktime_get_ptp_ts64(CLOCK_PTP + tkid - TIMEKEEPER_PTP, ts); +} +#else +static inline bool tk_get_ptp_ts64(unsigned int tkid, struct timespec64 *t= s) +{ + return false; +} +#endif =20 /* 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_adjt= imex */ -static int timekeeping_validate_timex(const struct __kernel_timex *txc) +static int timekeeping_validate_timex(const struct __kernel_timex *txc, bo= ol ptp_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(co return -EINVAL; } =20 + if (!ptp_clock) + return 0; + + /* PTP clocks are TAI based 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; } =20 @@ -2592,15 +2618,22 @@ static int __do_adjtimex(struct tk_data struct timekeeper *tks =3D &tkd->shadow_timekeeper; struct timespec64 ts; s32 orig_tai, tai; + bool ptp_clock; int ret; =20 + ptp_clock =3D IS_ENABLED(CONFIG_POSIX_PTP_CLOCKS) && tkd->timekeeper.id != =3D TIMEKEEPER_CORE; + /* Validate the data before disabling interrupts */ - ret =3D timekeeping_validate_timex(txc); + ret =3D timekeeping_validate_timex(txc, ptp_clock); if (ret) return ret; add_device_randomness(txc, sizeof(*txc)); =20 - ktime_get_real_ts64(&ts); + if (!ptp_clock) + ktime_get_real_ts64(&ts); + else + tk_get_ptp_ts64(tkd->timekeeper.id, &ts); + add_device_randomness(&ts, sizeof(ts)); =20 guard(raw_spinlock_irqsave)(&tkd->lock); From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C6F92C2FD3; Tue, 13 May 2025 15:13:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149223; cv=none; b=K8rBwKccfoFEQ9dEmizUYwmhkHlkwAReOFpRuZIAKaC7klEwb4gAx40c8m3mGHfQMZHeAtu5mwk3/3c8T6B7K0roDI1AlR54ddJs+m9kHElJKqDZfNY5FPWb4oLEL7wswvht35aRTcciPLZcPaMqJuAcsstMPDD8B4W+y8+QFlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149223; c=relaxed/simple; bh=BpeheMH6uIvVAFP8rYXlm1Dx+yNSML3xMiYiUukl6ZM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=QPOrlRNsViduaT6QpVAuVkfArUEiGxN5k9upadi4uWF59k7sPxnMoe8q8UUkpSXj0j4ZKC1AA0XTKyNXxX8nTV4IU6tnLiffebkDtw5kWzeAT/evGZ0WuF5Qxuywvt3g9BLQQnkgvI42OUeodPM9PiS+O84NzYjBNAr5Xh6TM3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pMeIoYv/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CkKs/Bhj; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pMeIoYv/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CkKs/Bhj" Message-ID: <20250513145138.095772163@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=JI4r1HUrTTSZl/aLlR5MvJoMLiHIykI2Fsz1lp+InB4=; b=pMeIoYv/PLQqAsIvYQR2pDin+lzW2HPz0z5SYGDY+VKWHu4rBSy8mXr8X5o/Zzr34FeA6H tTgAXii1TzYfaudK5GOMoGrEd0nYAouCx9sADCWt+XHMuz2r8AsFxKnldxpnJr5Q0wVu/q 1WETfzDywkALi8B6aeKz3DHkzRT5pNiyPHfJqGqbq+D5YLLEkE9hUzgXGPDbsGuxz45Zds wEYFS6rCzMwEdyeu5zkHUTdEZ6tO6/Lbiwpw6GYDkD1pm7ejGnG4h93ygryJAIwV6DYQ03 VayvT0jOvZOWAVUq58Z0F4YEVvrUD5NENfn8nNdGQ86QG58DWgjj/3Qz37JPGw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=JI4r1HUrTTSZl/aLlR5MvJoMLiHIykI2Fsz1lp+InB4=; b=CkKs/BhjsufBRs5g7yp0U/YqLp78MoOQBDwt4EoIWRrPgnPf+G4RgVLwwccU64t9GYoY9G FUg62wtZASActKAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 24/26] timekeeping: Provide adjtimex() for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:39 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The behaviour is close to clock_adtime(CLOCK_REALTIME) with the following differences: 1) ADJ_SETOFFSET adjusts the PTP clock offset =20 2) ADJ_TAI is not supported 3) Leap seconds are not supported 4) PPS is not supported Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2862,10 +2862,26 @@ static int ptp_clock_set(const clockid_t return 0; } =20 +static int ptp_clock_adj(const clockid_t id, struct __kernel_timex *txc) +{ + struct tk_data *tkd =3D ptp_get_tk_data(id); + struct adjtimex_result result =3D { }; + + if (!tkd) + return -ENODEV; + + /* + * @result is ignored for now as there are neither hrtimers nor a + * RTC related to these PTP clocks. + */ + return __do_adjtimex(tkd, txc, &result); +} + const struct k_clock clock_ptp =3D { .clock_getres =3D ptp_get_res, .clock_get_timespec =3D ptp_get_timespec, .clock_set =3D ptp_clock_set, + .clock_adj =3D ptp_clock_adj, }; =20 static __init void tk_ptp_setup(void) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 093D62C376A; Tue, 13 May 2025 15:13:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149226; cv=none; b=ZFzrvoXDKIOQApHuXnRR140nDe8rZ2M8Pw1z7eLsICT5QjlMZrvSFdDexYGtk25l0RJjTI/nTIsXN6U+k4M2uJz9PiFW8sbsOyLC1VPud+ATfisCEdsVNOaYQqU/Uj8yYyeOK435lYDlZOhjSF/snvQ1T9x1341g0MVBDRxjciE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149226; c=relaxed/simple; bh=T3gvaVQqrA1P0XTGSWSYE7BuwD8qt8nxw+8IRtG2EtE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=fSH9tq9ZNm6j0JY0F8Oq2xyeLI6X/6KOwlfAVesF+BH82SjMl7MnuZnaetdkuomovwXYuTHkj0vPA1JL4IkWNZIKkJFryFB6nM5w+Pbvk1WOVNZ4uQUHu1x03qFoXRm2c+7/1ahhellKUI8ZI2CVKPeZfYFjWOB3+fOQ/rJECFw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dCVmWtpz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mRHBgcED; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dCVmWtpz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mRHBgcED" Message-ID: <20250513145138.154416598@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=OSY0dlYZbI+6pUHvq6kewlJZ9+OAPtR4t6sW7tnac1k=; b=dCVmWtpzEVlegCuwfT9QIZIQOuBE/eGJDxuLDZpcRE3C9QUmektiyy1F9MoeIdlBzT6WkR +R9b+J/zxAwwGL7UWjlnvgqxS8b2FsIM4D3HPkBfHKWOuSLLygEREhfz95WgNzkqdnbha/ BSL7Y6nkxREPu1m8S0+bwsTTblfGABsVykk4LpWYPuahcYVmzijIce37NKi2twn7nN40/P yPdUCu0T4E7myiNtQC+uTAQ3mKTMpQlpI9AX0gjooTYtmFM3JbDfoBNUl6DHsydBgwKoXc /3qN/YVsuFuoSLN8bAT7FO1VXpGeQLrxxCsbWgw/PWSVIe6KKxK8mY4CxEG5ow== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=OSY0dlYZbI+6pUHvq6kewlJZ9+OAPtR4t6sW7tnac1k=; b=mRHBgcEDInI54BeIHUIJKO5jkoKVAy5ZKBfiqe7PshmSzU6/NzpJY1yuVAW/ZjA9yaQJ8l 3q60YyVJtKHrMbAQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 25/26] timekeeping: Provide update for PTP timekeepers References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:42 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update the PTP timekeepers periodically. For now this is tied to the system timekeeper update from the tick. This might be revisited. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -130,9 +130,11 @@ static struct tk_fast tk_fast_raw ____c =20 #ifdef CONFIG_POSIX_PTP_CLOCKS static __init void tk_ptp_setup(void); +static void tk_ptp_advance(void); static void tk_ptp_update_clocksource(void); #else static inline void tk_ptp_setup(void) { } +static inline void tk_ptp_advance(void) { } static inline void tk_ptp_update_clocksource(void) { } #endif =20 @@ -2312,11 +2314,13 @@ static bool timekeeping_advance(enum tim /** * update_wall_time - Uses the current clocksource to increment the wall t= ime * + * It also updates eventually installed PTP clock timekeepers */ void update_wall_time(void) { if (timekeeping_advance(TK_ADV_TICK)) clock_was_set_delayed(); + tk_ptp_advance(); } =20 /** @@ -2762,6 +2766,20 @@ static void tk_ptp_update_clocksource(vo } } =20 +static void tk_ptp_advance(void) +{ + unsigned long active =3D READ_ONCE(ptp_timekeepers); + unsigned int id; + + for_each_set_bit(id, &active, BITS_PER_LONG) { + struct tk_data *tkd =3D &timekeeper_data[id + TIMEKEEPER_PTP]; + + guard(raw_spinlock)(&tkd->lock); + if (tkd->shadow_timekeeper.clock_valid) + __timekeeping_advance(tkd, TK_ADV_TICK); + } +} + /** * ktime_get_ptp - Get TAI time for a PTP clock * @id: ID of the clock to read (CLOCK_PTP...) From nobody Sun Dec 14 02:06:04 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 241982BE7B8; Tue, 13 May 2025 15:13:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149228; cv=none; b=uqyCArUmZUgWeylJFasH1J1HHUgN/oU2rRp3eyuBB/k9WJIDEbeoHZtF6j77fQ0rXGyciJBp5yKmwIQXTCqn147fxpV3V2jjgaGvxAZDaRcOon9UjZ2k8YTFUiYPh8VJViNUe6lxn+SNt5DavYWHcajKQjBWQl4baYr7AeAj/+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149228; c=relaxed/simple; bh=41v7R615ZjWO7LUc2GvrJB6a77p2xlm3SbRtu9DjoEY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=hGKr7TGIp+Nr3Z5mtFmLcbbQ2HNTsK2/CP9f+LylTDHp35DNcJ+BR9csnaoJBvYbKcBPguRjSx/GFM8BeC6nt80hCGMaXPbWsB3C8DDpDDE16Zw44+wakte5zNqGkGdulS3R/xlEBnrGdq/JSUCj0aUea9PNSk6axe+qfFAoLYk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DMkNnoqY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wrsEkRGd; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DMkNnoqY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wrsEkRGd" Message-ID: <20250513145138.212062332@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149225; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=C/nM/U5FRwaKrJBXZ75yUzC1wtA3Or236TCJmRmP5DU=; b=DMkNnoqYgQEe1rVzPnNkHzukFoOaWhepgv0FcX74b4P36xUtV+Eb5v9vP0FoQRMP3fTWM4 mV19RLhrPko7tTfxeWlmWWLEGJu09p8dAujukhwBD6rwQHTENp2MtERfhqo5TIJKCUlxoS KPtS+9TzG9Ta6BTETVSDcMIxuNfa+1vdkcfddQBkhOXfC0qTUpWbKSXeZDiTMqeFq81Xkh N31QvJwh79fsLD88R68Aft3FDIlov7Djo0Tw8luodFdU2RBExF2RWNNYPNSfHWDZKA0SXa XSxFhYjn69vk+TVln+Mr1wGPTH7xiizuqpfGACaf7F/FnBwbEDSpd9PL8RQj1Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149225; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=C/nM/U5FRwaKrJBXZ75yUzC1wtA3Or236TCJmRmP5DU=; b=wrsEkRGdB5n8wpG+GCkszFKbZ6zRdPejuIf7pcn9CK8JiiB3kGQCvImxMAAmYONUvDeJVv m3qjrLzbgAdbvaCA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Alex Gieringer Subject: [patch 26/26] timekeeping: Provide interface to control independent PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:44 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Independent PTP 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 --- Documentation/ABI/stable/sysfs-kernel-time-ptp | 6 + kernel/time/timekeeping.c | 125 ++++++++++++++++++++= +++++ 2 files changed, 131 insertions(+) --- /dev/null +++ b/Documentation/ABI/stable/sysfs-kernel-time-ptp @@ -0,0 +1,6 @@ +What: /sys/kernel/time/ptp//enable +Date: May 2025 +Contact: Thomas Gleixner +Description: + Controls the enablement of independent PTP clock + timekeepers. --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -2900,6 +2901,130 @@ const struct k_clock clock_ptp =3D { .clock_adj =3D ptp_clock_adj, }; =20 +static void ptp_clock_enable(unsigned int id) +{ + struct tk_read_base *tkr_raw =3D &tk_core.timekeeper.tkr_raw; + struct tk_data *tkd =3D ptp_get_tk_data(id); + struct timekeeper *tks =3D &tkd->shadow_timekeeper; + + /* Prevent the core timekeeper from changing. */ + guard(raw_spinlock_irq)(&tk_core.lock); + + /* + * Setup the PTP clock assuming that the raw core timekeeper clock + * frequency conversion is close enough. PTP userspace has to + * adjust for the deviation via clock_adjtime(2). + */ + guard(raw_spinlock_nested)(&tkd->lock); + + /* Remove leftovers of a previous registration */ + memset(tks, 0, sizeof(*tks)); + /* Restore the timekeeper id */ + tks->id =3D tkd->timekeeper.id; + /* Setup the timekeeper based on the current system clocksource */ + tk_setup_internals(tks, tkr_raw->clock); + + /* Mark it valid and set it live */ + tks->clock_valid =3D true; + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); +} + +static void ptp_clock_disable(unsigned int id) +{ + struct tk_data *tkd =3D ptp_get_tk_data(id); + + guard(raw_spinlock_irq)(&tkd->lock); + tkd->shadow_timekeeper.clock_valid =3D false; + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); +} + +static DEFINE_MUTEX(ptp_clock_mutex); + +static ssize_t ptp_clock_enable_store(struct kobject *kobj, struct kobj_at= tribute *attr, + const char *buf, size_t count) +{ + /* Lazy atoi() as name is "0..7" */ + int id =3D kobj->name[0] & 0x7; + bool enable; + + if (!capable(CAP_SYS_TIME)) + return -EPERM; + + if (kstrtobool(buf, &enable) < 0) + return -EINVAL; + + guard(mutex)(&ptp_clock_mutex); + if (enable =3D=3D test_bit(id, &ptp_timekeepers)) + return count; + + if (enable) { + ptp_clock_enable(CLOCK_PTP + id); + set_bit(id, &ptp_timekeepers); + } else { + ptp_clock_disable(CLOCK_PTP + id); + clear_bit(id, &ptp_timekeepers); + } + return count; +} + +static ssize_t ptp_clock_enable_show(struct kobject *kobj, struct kobj_att= ribute *attr, char *buf) +{ + unsigned long active =3D READ_ONCE(ptp_timekeepers); + /* Lazy atoi() as name is "0..7" */ + int id =3D kobj->name[0] & 0x7; + + return sysfs_emit(buf, "%d\n", test_bit(id, &active)); +} + +static struct kobj_attribute ptp_clock_enable_attr =3D __ATTR_RW(ptp_clock= _enable); + +static struct attribute *ptp_clock_enable_attrs[] =3D { + &ptp_clock_enable_attr.attr, + NULL, +}; + +static const struct attribute_group ptp_clock_enable_attr_group =3D { + .attrs =3D ptp_clock_enable_attrs, +}; + +static int __init tk_ptp_sysfs_init(void) +{ + struct kobject *ptpo, *tko =3D kobject_create_and_add("time", kernel_kobj= ); + + if (!tko) { + pr_warn("Unable to create /sys/kernel/time/. POSIX PTP clocks disabled.\= n"); + return -ENOMEM; + } + + ptpo =3D kobject_create_and_add("ptp_clocks", tko); + if (!ptpo) { + pr_warn("Unable to create /sys/kernel/time/ptp_clocks. POSIX PTP clocks = disabled.\n"); + kobject_put(tko); + return -ENOMEM; + } + + for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++) { + char id[2] =3D { [0] =3D '0' + (i - TIMEKEEPER_PTP), }; + struct kobject *clk =3D kobject_create_and_add(id, ptpo); + + if (!clk) { + pr_warn("Unable to create /sys/kernel/time/ptp_clocks/%d\n", + i - TIMEKEEPER_PTP); + return -ENOMEM; + } + + int ret =3D sysfs_create_group(clk, &ptp_clock_enable_attr_group); + + if (ret) { + pr_warn("Unable to create /sys/kernel/time/ptp_clocks/%d/enable\n", + i - TIMEKEEPER_PTP); + return ret; + } + } + return 0; +} +late_initcall(tk_ptp_sysfs_init); + static __init void tk_ptp_setup(void) { for (int i =3D TIMEKEEPER_PTP; i <=3D TIMEKEEPER_PTP_LAST; i++)