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.681496427@linutronix.de>
Date: Tue, 13 May 2025 17:13:26 +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 17/26] timekeeping: Provide time getters for PTP clocks

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

---
 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);
 
 /*
+ * 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
  */
 
@@ -263,6 +274,12 @@ extern bool timekeeping_rtc_skipresume(v
 
 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
 
+/*
+ * 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;
 
+static inline bool ptp_valid_clockid(clockid_t id)
+{
+	return id >= CLOCK_PTP && id <= 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
 	}
 }
 
+/**
+ * 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 = 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 = &tkd->timekeeper;
+	do {
+		seq = read_seqcount_begin(&tkd->seq);
+		if (!tk->clock_valid)
+			return false;
+
+		base = ktime_add(tk->tkr_mono.base, tk->offs_ptp);
+		nsecs = timekeeping_get_ns(&tk->tkr_mono);
+	} while (read_seqcount_retry(&tkd->seq, seq));
+
+	*kt = 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 = ktime_to_timespec64(now);
+	return true;
+}
+
 static __init void tk_ptp_setup(void)
 {
 	for (int i = TIMEKEEPER_PTP; i <= TIMEKEEPER_PTP_LAST; i++)


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ