[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <htnwor46q3435pddkafm7flmx4m2bs4553gq3mx4jzevtfgg2l@h4abniqo4dzf>
Date: Wed, 14 May 2025 10:07:11 +0200
From: Antoine Tenart <atenart@...nel.org>
To: Thomas Gleixner <tglx@...utronix.de>
Cc: LKML <linux-kernel@...r.kernel.org>, 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: Re: [patch 26/26] timekeeping: Provide interface to control
independent PTP clocks
On Tue, May 13, 2025 at 05:13:44PM +0200, Thomas Gleixner wrote:
> 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 <tglx@...utronix.de>
> ---
> 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/<ID>/enable
The path added below is /sys/kernel/time/ptp_clocks/<ID>/enable.
> +Date: May 2025
> +Contact: Thomas Gleixner <tglx@...utronix.de>
> +Description:
> + Controls the enablement of independent PTP clock
> + timekeepers.
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -14,6 +14,7 @@
> #include <linux/sched/loadavg.h>
> #include <linux/sched/clock.h>
> #include <linux/syscore_ops.h>
> +#include <linux/sysfs.h>
> #include <linux/clocksource.h>
> #include <linux/jiffies.h>
> #include <linux/time.h>
> @@ -2900,6 +2901,130 @@ const struct k_clock clock_ptp = {
> .clock_adj = ptp_clock_adj,
> };
>
> +static void ptp_clock_enable(unsigned int id)
> +{
> + struct tk_read_base *tkr_raw = &tk_core.timekeeper.tkr_raw;
> + struct tk_data *tkd = ptp_get_tk_data(id);
> + struct timekeeper *tks = &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 = 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 = true;
> + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL);
> +}
> +
> +static void ptp_clock_disable(unsigned int id)
> +{
> + struct tk_data *tkd = ptp_get_tk_data(id);
> +
> + guard(raw_spinlock_irq)(&tkd->lock);
> + tkd->shadow_timekeeper.clock_valid = 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_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)(&ptp_clock_mutex);
> + if (enable == 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_attribute *attr, char *buf)
> +{
> + unsigned long active = READ_ONCE(ptp_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 ptp_clock_enable_attr = __ATTR_RW(ptp_clock_enable);
> +
> +static struct attribute *ptp_clock_enable_attrs[] = {
> + &ptp_clock_enable_attr.attr,
> + NULL,
> +};
> +
> +static const struct attribute_group ptp_clock_enable_attr_group = {
> + .attrs = ptp_clock_enable_attrs,
> +};
> +
> +static int __init tk_ptp_sysfs_init(void)
> +{
> + struct kobject *ptpo, *tko = 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 = 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 = TIMEKEEPER_PTP; i <= TIMEKEEPER_PTP_LAST; i++) {
> + char id[2] = { [0] = '0' + (i - TIMEKEEPER_PTP), };
> + struct kobject *clk = 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 = 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 = TIMEKEEPER_PTP; i <= TIMEKEEPER_PTP_LAST; i++)
>
>
Powered by blists - more mailing lists