public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: netdev@vger.kernel.org,
	"Richard Cochran" <richardcochran@gmail.com>,
	"Christopher Hall" <christopher.s.hall@intel.com>,
	"John Stultz" <jstultz@google.com>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	"Miroslav Lichvar" <mlichvar@redhat.com>,
	"Werner Abt" <werner.abt@meinberg-usa.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"Kurt Kanzenbach" <kurt@linutronix.de>,
	"Nam Cao" <namcao@linutronix.de>,
	"Antoine Tenart" <atenart@kernel.org>
Subject: [patch V3 11/11] timekeeping: Provide interface to control auxiliary clocks
Date: Wed, 25 Jun 2025 20:38:49 +0200 (CEST)	[thread overview]
Message-ID: <20250625183758.444626478@linutronix.de> (raw)
In-Reply-To: 20250625182951.587377878@linutronix.de

Auxiliary 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 <tglx@linutronix.de>
---
V3: Use kobject.h, clockid_t and cleanup the sysfs init - Thomas W.
    Use aux_tkd, aux_tks for clarity - John
---
 Documentation/ABI/stable/sysfs-kernel-time-aux-clocks |    5 
 kernel/time/timekeeping.c                             |  116 ++++++++++++++++++
 2 files changed, 121 insertions(+)

--- /dev/null
+++ b/Documentation/ABI/stable/sysfs-kernel-time-aux-clocks
@@ -0,0 +1,5 @@
+What:		/sys/kernel/time/aux_clocks/<ID>/enable
+Date:		May 2025
+Contact:	Thomas Gleixner <tglx@linutronix.de>
+Description:
+		Controls the enablement of auxiliary clock timekeepers.
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -6,6 +6,7 @@
 #include <linux/timekeeper_internal.h>
 #include <linux/module.h>
 #include <linux/interrupt.h>
+#include <linux/kobject.h>
 #include <linux/percpu.h>
 #include <linux/init.h>
 #include <linux/mm.h>
@@ -2903,6 +2904,121 @@ const struct k_clock clock_aux = {
 	.clock_adj		= aux_clock_adj,
 };
 
+static void aux_clock_enable(clockid_t id)
+{
+	struct tk_read_base *tkr_raw = &tk_core.timekeeper.tkr_raw;
+	struct tk_data *aux_tkd = aux_get_tk_data(id);
+	struct timekeeper *aux_tks = &aux_tkd->shadow_timekeeper;
+
+	/* Prevent the core timekeeper from changing. */
+	guard(raw_spinlock_irq)(&tk_core.lock);
+
+	/*
+	 * Setup the auxiliary clock assuming that the raw core timekeeper
+	 * clock frequency conversion is close enough. Userspace has to
+	 * adjust for the deviation via clock_adjtime(2).
+	 */
+	guard(raw_spinlock_nested)(&aux_tkd->lock);
+
+	/* Remove leftovers of a previous registration */
+	memset(aux_tks, 0, sizeof(*aux_tks));
+	/* Restore the timekeeper id */
+	aux_tks->id = aux_tkd->timekeeper.id;
+	/* Setup the timekeeper based on the current system clocksource */
+	tk_setup_internals(aux_tks, tkr_raw->clock);
+
+	/* Mark it valid and set it live */
+	aux_tks->clock_valid = true;
+	timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
+}
+
+static void aux_clock_disable(clockid_t id)
+{
+	struct tk_data *aux_tkd = aux_get_tk_data(id);
+
+	guard(raw_spinlock_irq)(&aux_tkd->lock);
+	aux_tkd->shadow_timekeeper.clock_valid = false;
+	timekeeping_update_from_shadow(aux_tkd, TK_UPDATE_ALL);
+}
+
+static DEFINE_MUTEX(aux_clock_mutex);
+
+static ssize_t aux_clock_enable_store(struct kobject *kobj, struct kobj_attribute *attr,
+				      const char *buf, size_t count)
+{
+	/* Lazy atoi() as name is "0..7" */
+	int id = kobj->name[0] & 0x7;
+	bool enable;
+
+	if (!capable(CAP_SYS_TIME))
+		return -EPERM;
+
+	if (kstrtobool(buf, &enable) < 0)
+		return -EINVAL;
+
+	guard(mutex)(&aux_clock_mutex);
+	if (enable == test_bit(id, &aux_timekeepers))
+		return count;
+
+	if (enable) {
+		aux_clock_enable(CLOCK_AUX + id);
+		set_bit(id, &aux_timekeepers);
+	} else {
+		aux_clock_disable(CLOCK_AUX + id);
+		clear_bit(id, &aux_timekeepers);
+	}
+	return count;
+}
+
+static ssize_t aux_clock_enable_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
+{
+	unsigned long active = READ_ONCE(aux_timekeepers);
+	/* Lazy atoi() as name is "0..7" */
+	int id = kobj->name[0] & 0x7;
+
+	return sysfs_emit(buf, "%d\n", test_bit(id, &active));
+}
+
+static struct kobj_attribute aux_clock_enable_attr = __ATTR_RW(aux_clock_enable);
+
+static struct attribute *aux_clock_enable_attrs[] = {
+	&aux_clock_enable_attr.attr,
+	NULL
+};
+
+static const struct attribute_group aux_clock_enable_attr_group = {
+	.attrs = aux_clock_enable_attrs,
+};
+
+static int __init tk_aux_sysfs_init(void)
+{
+	struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj);
+
+	if (!tko)
+		return -ENOMEM;
+
+	auxo = kobject_create_and_add("aux_clocks", tko);
+	if (!auxo) {
+		kobject_put(tko);
+		return -ENOMEM;
+	}
+
+	for (int i = 0; i <= MAX_AUX_CLOCKS; i++) {
+		char id[2] = { [0] = '0' + i, };
+		struct kobject *clk = kobject_create_and_add(id, auxo);
+
+		if (!clk)
+			return -ENOMEM;
+
+		int ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
+
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+late_initcall(tk_aux_sysfs_init);
+
 static __init void tk_aux_setup(void)
 {
 	for (int i = TIMEKEEPER_AUX_FIRST; i <= TIMEKEEPER_AUX_LAST; i++)


  parent reply	other threads:[~2025-06-25 18:38 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25 18:38 [patch V3 00/11] timekeeping: Provide support for auxiliary clocks - Remaining series Thomas Gleixner
2025-06-25 18:38 ` [patch V3 01/11] timekeeping: Update auxiliary timekeepers on clocksource change Thomas Gleixner
2025-06-27  4:43   ` John Stultz
2025-06-27 14:04     ` Thomas Gleixner
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 02/11] timekeeping: Provide time getters for auxiliary clocks Thomas Gleixner
2025-06-27  4:18   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 03/11] timekeeping: Add minimal posix-timers support " Thomas Gleixner
2025-06-27  4:19   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 04/11] timekeeping: Provide time setter " Thomas Gleixner
2025-06-27  4:23   ` John Stultz
2025-06-27 14:18     ` Thomas Gleixner
2025-06-27 14:57       ` Thomas Gleixner
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 05/11] timekeeping: Make timekeeping_inject_offset() reusable Thomas Gleixner
2025-06-27  4:26   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 06/11] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset() Thomas Gleixner
2025-06-27  4:54   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 07/11] timekeeping: Make do_adjtimex() reusable Thomas Gleixner
2025-06-27  4:56   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 08/11] timekeeping: Prepare do_adtimex() for auxiliary clocks Thomas Gleixner
2025-06-27  5:00   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 09/11] timekeeping: Provide adjtimex() " Thomas Gleixner
2025-06-27  5:01   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` [patch V3 10/11] timekeeping: Provide update for auxiliary timekeepers Thomas Gleixner
2025-06-27  5:05   ` John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner
2025-06-25 18:38 ` Thomas Gleixner [this message]
2025-06-27  5:07   ` [patch V3 11/11] timekeeping: Provide interface to control auxiliary clocks John Stultz
2025-06-30 15:05   ` [tip: timers/ptp] " tip-bot2 for Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250625183758.444626478@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=anna-maria@linutronix.de \
    --cc=atenart@kernel.org \
    --cc=christopher.s.hall@intel.com \
    --cc=dwmw2@infradead.org \
    --cc=frederic@kernel.org \
    --cc=jstultz@google.com \
    --cc=kurt@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=namcao@linutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=sboyd@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=werner.abt@meinberg-usa.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox