On 19/11/2026 21:19, Ahmad Byagowi wrote:
Detach disables timestamp interrupts before unregistering the PTP clock.
Until the POSIX clock is unregistered, an ioctl can rearm a channel
after that disable. The clock can then be released while the timestamp
IRQ is still registered. The core cleanup does not necessarily disable
the channel: EXTTS requests do not require a PTP_PF_EXTTS pin assignment.
I think we have to fix it other way. Basically, there are several ways
to enable EXTTS in ptp_ocp:
1. Via sma* attributes in ptp_ocp sysfs entry
2. Via pin name sysfs entry under ptp device
3. Via PTP_PIN_SETFUNC ioctl
4. Via PTP_EXTTS_REQUEST ioctl
On teardown, sma* attributes (1) are removed before disabling
interrupts. Options 2 and 3 set PTP_PF_EXTTS function and reverted
during ptp_clock_unregister(). Only option 4 does not set PTP_PF_EXTTS
function to pin, because it calls ops->enable() callback directly.
To fix it properly enable() callback has to set PTP_PF_EXTTS function
if it's requested. That will make ptp_disable_all_events() do exactly
what is has to do - clear all events generators of ptp device after
removing ioctl interface.
quoted hunk ↗ jump to hunk
Use the existing device spinlock to serialize the stopping check, PPS
subscription updates and timestamp MMIO. Set ts_stopping under that lock
before detach disables the channels, so an enable already in progress
finishes first and later enables fail with -ENODEV. Keep disables
available for PTP core cleanup.
Flush the interrupt mask writes under the lock, then drop the lock and
restore IRQ flags before pci_irq_vector(), which takes the MSI descriptor
mutex, and synchronize_irq(), which may sleep. During teardown, bypass
the PPS no-state-change shortcut: a concurrent disable
may have cleared pps_req_map and dropped the lock without yet draining
its IRQ. Detach must still disable and drain that channel before
ptp_clock_unregister().
Reported by the netdev Sashiko AI review, then checked against the PTP
and POSIX-clock paths. The source-derived userspace harness reproduces
rearming with the old source. With this fix it rejects late enables,
serializes in-flight enables, drains a mocked active IRQ and covers a
disable paused after unlock but before IRQ synchronization. It checks
that MMIO is locked and vector lookup and IRQ synchronization run unlocked
with IRQ flags restored. Shared PPS/EXTTS subscriptions and all six
channels pass with ASan/UBSan. This is not an in-kernel UAF, lockdep or
physical-card test.
Codex revised the code, tests and changelog from Vadim's review, at the
request to check the spinlock approach and rerun the removal-race tests.
Fixes: 627366c51145 ("ptp: ocp: fix resource freeing order")
Suggested-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org (local)
Link: https://lore.kernel.org/r/ad6883e5-c103-4f82-8040-e5463477ca10@linux.dev (local)
Signed-off-by: Ahmad Byagowi <redacted>
---
drivers/ptp/ptp_ocp.c | 35 ++++++++++++++++++++++++++++-------
1 file changed, 28 insertions(+), 7 deletions(-)
diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index 625a85b2dc..4c98558e96 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -367,6 +367,8 @@ struct ptp_ocp {
struct pci_dev *pdev;
struct device dev;
spinlock_t lock;
+ /* Protected by lock; blocks timestamp arming during teardown. */
+ bool ts_stopping;
struct ocp_reg __iomem *reg;
struct tod_reg __iomem *tod;
struct pps_reg __iomem *pps_to_ext;@@ -2550,6 +2552,15 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
struct ptp_ocp_ext_src *ext = priv;
struct ts_reg __iomem *reg = ext->mem;
struct ptp_ocp *bp = ext->bp;
+ unsigned long flags;
+ bool need_sync = false;
+ int err = 0;
+
+ spin_lock_irqsave(&bp->lock, flags);
+ if (enable && bp->ts_stopping) {
+ err = -ENODEV;
+ goto out;
+ }
if (ext == bp->pps) {
u32 old_map = bp->pps_req_map;@@ -2559,9 +2570,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
else
bp->pps_req_map &= ~req;
- /* if no state change, just return */
- if ((!!old_map ^ !!bp->pps_req_map) == 0)
- return 0;
+ /* Teardown must drain IRQs even if another disable cleared the map. */
there was no need of this in the previous patch, why do we need it now?
quoted hunk ↗ jump to hunk
+ if ((!!old_map ^ !!bp->pps_req_map) == 0 && !bp->ts_stopping)
+ goto out;
}
if (enable) {@@ -2569,16 +2580,21 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
iowrite32(1, ®->intr_mask);
iowrite32(1, ®->intr);
} else {
- int irq_vec = pci_irq_vector(bp->pdev, ext->irq_vec);
-
iowrite32(0, ®->intr_mask);
iowrite32(0, ®->enable);
ioread32(®->intr_mask);
+ need_sync = true;
+ }
+
+out:
+ spin_unlock_irqrestore(&bp->lock, flags);
+ if (need_sync) {
+ int irq_vec = pci_irq_vector(bp->pdev, ext->irq_vec);
+
if (irq_vec > 0)
synchronize_irq(irq_vec);
}
-
- return 0;
+ return err;
}
static void@@ -5749,12 +5765,17 @@ ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
static void
ptp_ocp_detach(struct ptp_ocp *bp)
{
+ unsigned long flags;
int i;
ptp_ocp_debugfs_remove_device(bp);
ptp_ocp_detach_sysfs(bp);
ptp_ocp_attr_group_del(bp);
timer_delete_sync(&bp->watchdog);
+ /* Drain in-flight enables and prevent rearming before unregister. */
+ spin_lock_irqsave(&bp->lock, flags);
+ bp->ts_stopping = true;
+ spin_unlock_irqrestore(&bp->lock, flags);
/* Disable interrupts on all timestampers */
if (bp->ts0)
ptp_ocp_ts_enable(bp->ts0, 0, false);