lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250513145137.799907954@linutronix.de>
Date: Tue, 13 May 2025 17:13:30 +0200 (CEST)
From: Thomas Gleixner <tglx@...utronix.de>
To: LKML <linux-kernel@...r.kernel.org>
Cc: netdev@...r.kernel.org,
 Richard Cochran <richardcochran@...il.com>,
 Christopher Hall <christopher.s.hall@...el.com>,
 David Zage <david.zage@...el.com>,
 John Stultz <jstultz@...gle.com>,
 Frederic Weisbecker <frederic@...nel.org>,
 Anna-Maria Behnsen <anna-maria@...utronix.de>,
 Miroslav Lichvar <mlichvar@...hat.com>,
 Werner Abt <werner.abt@...nberg-usa.com>,
 David Woodhouse <dwmw2@...radead.org>,
 Stephen Boyd <sboyd@...nel.org>,
 Thomas Weißschuh <thomas.weissschuh@...utronix.de>,
 Kurt Kanzenbach <kurt@...utronix.de>,
 Nam Cao <namcao@...utronix.de>,
 Alex Gieringer <gieri@...utronix.de>
Subject: [patch 19/26] timekeeping: Provide time setter for PTP clocks

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 <tglx@...utronix.de>

---
 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;
 }
 
+static int ptp_clock_set(const clockid_t id, const struct timespec64 *tnew)
+{
+	struct tk_data *tkd = ptp_get_tk_data(id);
+	struct timekeeper *tks;
+	ktime_t tnow, nsecs;
+
+	if (!timespec64_valid_settod(tnew))
+		return -EINVAL;
+	if (!tkd)
+		return -ENODEV;
+
+	tks = &tkd->shadow_timekeeper;
+
+	guard(raw_spinlock_irq)(&tkd->lock);
+	if (!tks->clock_valid)
+		return -ENODEV;
+
+	/* Forward the timekeeper base time */
+	timekeeping_forward_now(tks);
+	/*
+	 * Get the updated base time. tkr_mono.base has not been
+	 * updated yet, so do that first. That makes the update
+	 * in timekeeping_update_from_shadow() redundant, but
+	 * that's harmless. After that @tnow can be calculated
+	 * by using tkr_mono::cycle_last, which has been set
+	 * by timekeeping_forward_now().
+	 */
+	tk_update_ktime_data(tks);
+	nsecs = timekeeping_cycles_to_ns(&tks->tkr_mono, tks->tkr_mono.cycle_last);
+	tnow = ktime_add(tks->tkr_mono.base, nsecs);
+
+	/* Calculate the new PTP offset */
+	tks->offs_ptp = ktime_sub(timespec64_to_ktime(*tnew), tnow);
+
+	timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL);
+	return 0;
+}
+
 const struct k_clock clock_ptp = {
 	.clock_getres		= ptp_get_res,
 	.clock_get_timespec	= ptp_get_timespec,
+	.clock_set		= ptp_clock_set,
 };
 
 static __init void tk_ptp_setup(void)


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ