From: ahmadexp EXTTS, PPS and period sysfs stores invoke the clock driver callback without the pincfg_mux mutex. The ioctl paths and pin configuration changes use that mutex, so sysfs can race with those operations and with other sysfs requests. Take pincfg_mux around each enable callback. Keep validation and permission checks outside the critical section, propagate callback failures, and return -ERESTARTSYS when mutex acquisition is interrupted. The mutex is required because disable callbacks may sleep while draining an IRQ. A coding assistant helped identify the missing serialization, develop the fix and regression tests, and prepare this changelog. The source-derived tests exercise the actual callbacks with controlled scheduling; KUnit coverage uses the actual stores under lock contention. Fixes: d94ba80ebbea ("ptp: Added a brand new class driver for ptp clocks.") Link: https://lore.kernel.org/r/179548551283.3135.17206659598806657887@kernel.org Signed-off-by: Ahmad Byagowi --- drivers/ptp/ptp_sysfs.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c index 8e0e0aa09981..90fdddc1e6a7 100644 --- a/drivers/ptp/ptp_sysfs.c +++ b/drivers/ptp/ptp_sysfs.c @@ -60,7 +60,10 @@ static ssize_t extts_enable_store(struct device *dev, if (req.extts.index >= ops->n_ext_ts) goto out; + if (mutex_lock_interruptible(&ptp->pincfg_mux)) + return -ERESTARTSYS; err = ops->enable(ops, &req, enable ? 1 : 0); + mutex_unlock(&ptp->pincfg_mux); if (err) goto out; @@ -130,7 +133,10 @@ static ssize_t period_store(struct device *dev, goto out; enable = req.perout.period.sec || req.perout.period.nsec; + if (mutex_lock_interruptible(&ptp->pincfg_mux)) + return -ERESTARTSYS; err = ops->enable(ops, &req, enable); + mutex_unlock(&ptp->pincfg_mux); if (err) goto out; @@ -157,7 +163,10 @@ static ssize_t pps_enable_store(struct device *dev, if (cnt != 1) goto out; + if (mutex_lock_interruptible(&ptp->pincfg_mux)) + return -ERESTARTSYS; err = ops->enable(ops, &req, enable ? 1 : 0); + mutex_unlock(&ptp->pincfg_mux); if (err) goto out; -- 2.54.0 (Apple Git-157) From: ahmadexp PTP_EXTTS_REQUEST can enable a channel without assigning PTP_PF_EXTTS to a pin. ptp_disable_all_events() currently walks only configured pins, so unassigned channels can remain active after the clock is unregistered. After removing the user interfaces and draining active operations, issue a direct PTP_CLK_REQ_EXTTS disable for every advertised external timestamp channel. Keep pin assignments unchanged and retain the existing PPS shutdown. This is a PTP core change and does not modify any clock driver. A coding assistant helped identify the unassigned-channel teardown gap, develop the core fix and regression tests, and prepare this changelog. The regression harness exercises the actual cleanup path with controlled clock callbacks; no physical card was tested. Fixes: a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events") Fixes: 627366c51145 ("ptp: ocp: fix resource freeing order") Link: https://lore.kernel.org/r/179548551283.3135.17206659598806657887@kernel.org Signed-off-by: Ahmad Byagowi --- drivers/ptp/ptp_chardev.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/ptp/ptp_chardev.c b/drivers/ptp/ptp_chardev.c index f63b45f5ee6a..5835c01cddb4 100644 --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -53,11 +53,15 @@ void ptp_disable_all_events(struct ptp_clock *ptp) unsigned int i; mutex_lock(&ptp->pincfg_mux); - /* Disable any pins that may raise EXTTS events */ - for (i = 0; i < info->n_pins; i++) - if (info->pin_config[i].func == PTP_PF_EXTTS) - ptp_disable_pinfunc(info, info->pin_config[i].func, - info->pin_config[i].chan); + /* EXTTS requests do not require a pin assignment. */ + for (i = 0; i < info->n_ext_ts; i++) { + struct ptp_clock_request req = { + .type = PTP_CLK_REQ_EXTTS, + .extts.index = i, + }; + + info->enable(info, &req, 0); + } /* Disable the PPS event if the driver has PPS support */ if (info->pps) { -- 2.54.0 (Apple Git-157)