Skip to contentSkip to conversation

ptp: serialize requests and stop active EXTTS

PATCH New Ahmad Byagowi 2 participants 4 messages Nov 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v7 cover ptp: serialize requests and stop active EXTTS

This series is limited to the PTP/PHC core. No driver change is needed to apply it.

The sysfs enable callbacks take pincfg_mux, as the ioctl callbacks already do. The core tracks successfully enabled timestamp channels under that mutex, including requests without a pin assignment. On unregister, after the user interfaces have been removed, it disables tracked channels and preserves existing cleanup for assigned pins. The tracked channel indices go directly to EXTTS requests, without changing pin assignments. PPS is disabled last, before source release.

Changes since v6:
- Use the commit establishing the enable callback locking contract for the sysfs Fixes tag. Clarify unchanged error handling.
- Replace the blanket disable loop with tracked channel cleanup. Do not call into drivers for idle, unassigned channels.
- Keep the existing OCP detach and shared-PPS logic untouched.
- Align the author name and coalesce the commit trailers.

Validation (local fixtures, not included in the patches): strict PTP object builds on x86_64 and i386, allmodconfig and allyesconfig. Source-derived tests model teardown, last-subscriber IRQ draining, idle, failed and already-disabled requests, and unassigned channels. The host fixture uses stubbed register and interrupt operations. Nine local KUnit cases pass in QEMU with KASAN, lockdep and DEBUG_ATOMIC_SLEEP, using real PTP unregister and sysfs callbacks with a test clock.
No physical card or switch test was performed.

Coding-assistant support was used to review callback and teardown paths, develop fixes and local test cases, and prepare changelogs.

v6: https://lore.kernel.org/r/[email protected]/
Review: https://lore.kernel.org/r/[email protected]

Ahmad Byagowi (2):
ptp: serialize sysfs enable requests with ioctl paths
ptp: shut down enabled unassigned EXTTS channels on unregister

#1 lore raw

PATCH v7 1/2 ptp: serialize sysfs enable requests with ioctl paths

Drivers may call ptp_find_pin() from their enable callback; that helper requires the caller to hold pincfg_mux. The ioctl paths hold the lock, but the EXTTS, PPS and periodic-output sysfs stores do not.

Take pincfg_mux around the three sysfs callbacks. Leave existing validation, permission checks and callback error handling unchanged. Return -ERESTARTSYS if lock acquisition is interrupted.

Developed with coding-assistant support during review and validation.

Fixes 62582a7ee783 ("ptp: Avoid deadlocks in the programmable pin code.")
---
 drivers/ptp/ptp_sysfs.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/ptp/ptp_sysfs.c +9 -0
index 8e0e0aa09981..90fdddc1e6a7 100644
@@ -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;
 

base-commit: 4392bb809812ac79449cd0cf1f5e00469ee102d1

Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

PATCH v7 2/2 ptp: shut down enabled unassigned EXTTS channels on unregister

In reply to Ahmad Byagowi

EXTTS requests can enable a channel without an assigned pin. The unregister path only disables assigned EXTTS pins, so an enabled unassigned channel can remain armed when resources are released.

Track successful ioctl and sysfs enables under pincfg_mux. Clear the bit on successful disable, including when changing a pin assignment. On unregister keep the existing assigned-pin cleanup and send one disable for each tracked channel, before disabling PPS. Do not send requests to unassigned channels that were never enabled. This avoids calling drivers that can change the pin mux on an EXTTS disable.

The calls still follow POSIX clock unregister and precede PPS source release. This patch does not change any driver or any pin assignment.

Developed with coding-assistant support using the v5 and v6 review.

Fixes a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events")
---
 drivers/ptp/ptp_chardev.c | 53 +++++++++++++++++++++++++++++++--------
 drivers/ptp/ptp_clock.c   | 11 ++++++++
 drivers/ptp/ptp_private.h |  7 +++++-
 drivers/ptp/ptp_sysfs.c   |  2 +-
 4 files changed, 61 insertions(+), 12 deletions(-)
drivers/ptp/ptp_chardev.c +43 -10
index f63b45f5ee6a..b2e198c65e7e 100644
@@ -17,9 +17,28 @@
 
 #include "ptp_private.h"
 
-static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
+int ptp_extts_enable(struct ptp_clock *ptp, struct ptp_clock_request *req,
+		     bool enable)
+{
+	struct ptp_clock_info *info = ptp->info;
+	unsigned int chan = req->extts.index;
+	int err;
+
+	lockdep_assert_held(&ptp->pincfg_mux);
+	if (info->n_ext_ts <= 0 || chan >= info->n_ext_ts)
+		return -EINVAL;
+
+	err = info->enable(info, req, enable);
+	if (!err)
+		__assign_bit(chan, ptp->extts_enabled, enable);
+
+	return err;
+}
+
+static int ptp_disable_pinfunc(struct ptp_clock *ptp,
 			       enum ptp_pin_function func, unsigned int chan)
 {
+	struct ptp_clock_info *ops = ptp->info;
 	struct ptp_clock_request rq;
 	int err = 0;
 
@@ -31,7 +50,7 @@ static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
 	case PTP_PF_EXTTS:
 		rq.type = PTP_CLK_REQ_EXTTS;
 		rq.extts.index = chan;
-		err = ops->enable(ops, &rq, 0);
+		err = ptp_extts_enable(ptp, &rq, false);
 		break;
 	case PTP_PF_PEROUT:
 		rq.type = PTP_CLK_REQ_PEROUT;
@@ -50,14 +69,28 @@ static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
 void ptp_disable_all_events(struct ptp_clock *ptp)
 {
 	struct ptp_clock_info *info = ptp->info;
+	struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
+	unsigned long chan;
 	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);
+	/* Retain existing cleanup for assigned pins not tracked below. */
+	for (i = 0; i < info->n_pins; i++) {
+		const struct ptp_pin_desc *pin = &info->pin_config[i];
+
+		if (pin->func == PTP_PF_EXTTS && info->n_ext_ts > 0 &&
+		    pin->chan < info->n_ext_ts &&
+		    !test_bit(pin->chan, ptp->extts_enabled))
+			ptp_disable_pinfunc(ptp, pin->func, pin->chan);
+	}
+
+	/* A successful EXTTS request need not have an assigned pin. */
+	if (info->n_ext_ts > 0) {
+		for_each_set_bit(chan, ptp->extts_enabled, info->n_ext_ts) {
+			req.extts.index = chan;
+			info->enable(info, &req, 0);
+		}
+	}
 
 	/* Disable the PPS event if the driver has PPS support */
 	if (info->pps) {
@@ -115,7 +148,7 @@ int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
 	 * channel.
 	 */
 	if (pin1) {
-		ptp_disable_pinfunc(info, func, chan);
+		ptp_disable_pinfunc(ptp, func, chan);
 		pin1->func = PTP_PF_NONE;
 		pin1->chan = 0;
 	}
@@ -123,7 +156,7 @@ int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
 	/* Disable whatever function was previously assigned to the requested
 	 * pin.
 	 */
-	ptp_disable_pinfunc(info, pin2->func, pin2->chan);
+	ptp_disable_pinfunc(ptp, pin2->func, pin2->chan);
 	pin2->func = func;
 	pin2->chan = chan;
 
@@ -241,7 +274,7 @@ static long ptp_extts_request(struct ptp_clock *ptp, unsigned int cmd, void __us
 		return -EOPNOTSUPP;
 
 	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
-		return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0);
+		return ptp_extts_enable(ptp, &req, req.extts.flags & PTP_ENABLE_FEATURE);
 }
 
 static long ptp_perout_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
drivers/ptp/ptp_clock.c +11 -0
index 47ffc773065a..10f8dccdea4f 100644
@@ -219,6 +219,7 @@ static void ptp_clock_release(struct device *dev)
 
 	ptp_cleanup_pin_groups(ptp);
 	kfree(ptp->vclock_index);
+	bitmap_free(ptp->extts_enabled);
 	mutex_destroy(&ptp->pincfg_mux);
 	mutex_destroy(&ptp->n_vclocks_mux);
 	/* Delete first entry */
@@ -346,6 +347,14 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 		goto no_memory;
 	}
 
+	if (info->n_ext_ts > 0) {
+		ptp->extts_enabled = bitmap_zalloc(info->n_ext_ts, GFP_KERNEL);
+		if (!ptp->extts_enabled) {
+			err = -ENOMEM;
+			goto no_extts_bitmap;
+		}
+	}
+
 	err = xa_alloc(&ptp_clocks_map, &index, ptp, xa_limit_31b,
 		       GFP_KERNEL);
 	if (err)
@@ -492,6 +501,8 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 no_memory_queue:
 	xa_erase(&ptp_clocks_map, index);
 no_slot:
+	bitmap_free(ptp->extts_enabled);
+no_extts_bitmap:
 	kfree(ptp);
 no_memory:
 	return ERR_PTR(err);
drivers/ptp/ptp_private.h +6 -1
index 99e7eb241336..0ad8b53405af 100644
@@ -51,7 +51,8 @@ struct ptp_clock {
 	long dialed_frequency; /* remembers the frequency adjustment */
 	struct list_head tsevqs; /* timestamp fifo list */
 	spinlock_t tsevqs_lock; /* protects tsevqs from concurrent access */
-	struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
+	struct mutex pincfg_mux; /* protects pin configuration and enable state */
+	unsigned long *extts_enabled;
 	wait_queue_head_t tsev_wq;
 	int defunct; /* tells readers to go away when clock is being removed */
 	struct device_attribute *pin_dev_attr;
@@ -148,6 +149,10 @@ extern const struct class ptp_class;
 
 void ptp_disable_all_events(struct ptp_clock *ptp);
 
+/* Caller must hold pincfg_mux. */
+int ptp_extts_enable(struct ptp_clock *ptp, struct ptp_clock_request *req,
+		     bool enable);
+
 /* caller must hold pincfg_mux */
 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
 		    enum ptp_pin_function func, unsigned int chan);
drivers/ptp/ptp_sysfs.c +1 -1
index 90fdddc1e6a7..1583f8d75907 100644
@@ -62,7 +62,7 @@ static ssize_t extts_enable_store(struct device *dev,
 
 	if (mutex_lock_interruptible(&ptp->pincfg_mux))
 		return -ERESTARTSYS;
-	err = ops->enable(ops, &req, enable ? 1 : 0);
+	err = ptp_extts_enable(ptp, &req, enable);
 	mutex_unlock(&ptp->pincfg_mux);
 	if (err)
 		goto out;
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw
In reply to Ahmad Byagowi
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.