* [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach
2026-11-18 20:05 [PATCH net 0/4] ptp: ocp: fix timestamp teardown and DPLL routing Ahmad Byagowi
@ 2026-11-18 20:05 ` Ahmad Byagowi
2026-11-19 18:24 ` Vadim Fedorenko
2026-11-18 20:05 ` [PATCH net 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations Ahmad Byagowi
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Ahmad Byagowi @ 2026-11-18 20:05 UTC (permalink / raw)
To: netdev, vadim.fedorenko, richardcochran
Cc: andrew+netdev, davem, edumazet, kuba, linux-kernel, pabeni
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.
Serialize timestamp enables and disables with a mutex. Mark timestampers
as stopping under that mutex before the detach disable loop, so an
enable already in progress finishes first and later enables fail with
-ENODEV. Keep disables available for the PTP core cleanup. The existing
MMIO readback and synchronize_irq() then drain events before
ptp_clock_unregister() can release the clock.
Reported by the netdev Sashiko AI review, then checked against the
current PTP and POSIX-clock paths. A source-derived userspace harness
forces an enable after detach's disable and reproduces the rearm with
the old source. With this fix it rejects that enable, waits for an
enable already in progress, and waits for a mocked active IRQ. Both
timestamp and combined PPS/EXTTS paths pass with ASan/UBSan. This
validates ordering with stubbed MMIO/IRQ operations, not an in-kernel
use-after-free reproduction. No physical card was tested.
Codex prepared the fix, tests and changelog from the review and upstream
source, at the request to separate common-driver fixes from WiWi support
and validate removal ordering.
Fixes: 627366c51145 ("ptp: ocp: fix resource freeing order")
Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
drivers/ptp/ptp_ocp.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index 625a85b2dc..478f7012b2 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -367,6 +367,9 @@ struct ptp_ocp {
struct pci_dev *pdev;
struct device dev;
spinlock_t lock;
+ /* Serializes timestamp arming and teardown. */
+ struct mutex ts_lock;
+ bool ts_stopping;
struct ocp_reg __iomem *reg;
struct tod_reg __iomem *tod;
struct pps_reg __iomem *pps_to_ext;
@@ -2550,6 +2553,13 @@ 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;
+ int err = 0;
+
+ mutex_lock(&bp->ts_lock);
+ if (enable && bp->ts_stopping) {
+ err = -ENODEV;
+ goto out;
+ }
if (ext == bp->pps) {
u32 old_map = bp->pps_req_map;
@@ -2561,7 +2571,7 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
/* if no state change, just return */
if ((!!old_map ^ !!bp->pps_req_map) == 0)
- return 0;
+ goto out;
}
if (enable) {
@@ -2578,7 +2588,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
synchronize_irq(irq_vec);
}
- return 0;
+out:
+ mutex_unlock(&bp->ts_lock);
+ return err;
}
static void
@@ -5613,6 +5625,7 @@ ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
bp->ptp_info = ptp_ocp_clock_info;
spin_lock_init(&bp->lock);
+ mutex_init(&bp->ts_lock);
for (i = 0; i < __PORT_COUNT; i++)
bp->port[i].uport = NULL;
@@ -5755,6 +5768,10 @@ ptp_ocp_detach(struct ptp_ocp *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. */
+ mutex_lock(&bp->ts_lock);
+ bp->ts_stopping = true;
+ mutex_unlock(&bp->ts_lock);
/* Disable interrupts on all timestampers */
if (bp->ts0)
ptp_ocp_ts_enable(bp->ts0, 0, false);
@@ -5789,6 +5806,7 @@ ptp_ocp_detach(struct ptp_ocp *bp)
if (bp->n_irqs)
pci_free_irq_vectors(bp->pdev);
mutex_destroy(&bp->cpld_lock);
+ mutex_destroy(&bp->ts_lock);
device_unregister(&bp->dev);
}
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach
2026-11-18 20:05 ` [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach Ahmad Byagowi
@ 2026-11-19 18:24 ` Vadim Fedorenko
2026-11-19 21:19 ` Ahmad Byagowi
0 siblings, 1 reply; 7+ messages in thread
From: Vadim Fedorenko @ 2026-11-19 18:24 UTC (permalink / raw)
To: Ahmad Byagowi, netdev, richardcochran
Cc: andrew+netdev, davem, edumazet, kuba, linux-kernel, pabeni
On 18/11/2026 20:05, 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.
>
> Serialize timestamp enables and disables with a mutex. Mark timestampers
> as stopping under that mutex before the detach disable loop, so an
> enable already in progress finishes first and later enables fail with
> -ENODEV. Keep disables available for the PTP core cleanup. The existing
> MMIO readback and synchronize_irq() then drain events before
> ptp_clock_unregister() can release the clock.
>
> Reported by the netdev Sashiko AI review, then checked against the
> current PTP and POSIX-clock paths. A source-derived userspace harness
> forces an enable after detach's disable and reproduces the rearm with
> the old source. With this fix it rejects that enable, waits for an
> enable already in progress, and waits for a mocked active IRQ. Both
> timestamp and combined PPS/EXTTS paths pass with ASan/UBSan. This
> validates ordering with stubbed MMIO/IRQ operations, not an in-kernel
> use-after-free reproduction. No physical card was tested.
>
> Codex prepared the fix, tests and changelog from the review and upstream
> source, at the request to separate common-driver fixes from WiWi support
> and validate removal ordering.
>
> Fixes: 627366c51145 ("ptp: ocp: fix resource freeing order")
>
> Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
>
> Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
> ---
> drivers/ptp/ptp_ocp.c | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
> index 625a85b2dc..478f7012b2 100644
> --- a/drivers/ptp/ptp_ocp.c
> +++ b/drivers/ptp/ptp_ocp.c
> @@ -367,6 +367,9 @@ struct ptp_ocp {
> struct pci_dev *pdev;
> struct device dev;
> spinlock_t lock;
> + /* Serializes timestamp arming and teardown. */
> + struct mutex ts_lock;
> + bool ts_stopping;
> struct ocp_reg __iomem *reg;
> struct tod_reg __iomem *tod;
> struct pps_reg __iomem *pps_to_ext;
> @@ -2550,6 +2553,13 @@ 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;
> + int err = 0;
> +
> + mutex_lock(&bp->ts_lock);
> + if (enable && bp->ts_stopping) {
> + err = -ENODEV;
> + goto out;
> + }
ts_stopping is changed only in device detach callback, there is actually
no need to cover whole ptp_ocp_ts_enable() with mutex, I believe
existing spinlock can be used to serialize this access, no?
>
> if (ext == bp->pps) {
> u32 old_map = bp->pps_req_map;
> @@ -2561,7 +2571,7 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
>
> /* if no state change, just return */
> if ((!!old_map ^ !!bp->pps_req_map) == 0)
> - return 0;
> + goto out;
> }
>
> if (enable) {
> @@ -2578,7 +2588,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
> synchronize_irq(irq_vec);
> }
>
> - return 0;
> +out:
> + mutex_unlock(&bp->ts_lock);
> + return err;
> }
>
> static void
> @@ -5613,6 +5625,7 @@ ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
>
> bp->ptp_info = ptp_ocp_clock_info;
> spin_lock_init(&bp->lock);
> + mutex_init(&bp->ts_lock);
>
> for (i = 0; i < __PORT_COUNT; i++)
> bp->port[i].uport = NULL;
> @@ -5755,6 +5768,10 @@ ptp_ocp_detach(struct ptp_ocp *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. */
> + mutex_lock(&bp->ts_lock);
> + bp->ts_stopping = true;
> + mutex_unlock(&bp->ts_lock);
> /* Disable interrupts on all timestampers */
> if (bp->ts0)
> ptp_ocp_ts_enable(bp->ts0, 0, false);
> @@ -5789,6 +5806,7 @@ ptp_ocp_detach(struct ptp_ocp *bp)
> if (bp->n_irqs)
> pci_free_irq_vectors(bp->pdev);
> mutex_destroy(&bp->cpld_lock);
> + mutex_destroy(&bp->ts_lock);
> device_unregister(&bp->dev);
> }
>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach
2026-11-19 18:24 ` Vadim Fedorenko
@ 2026-11-19 21:19 ` Ahmad Byagowi
0 siblings, 0 replies; 7+ messages in thread
From: Ahmad Byagowi @ 2026-11-19 21:19 UTC (permalink / raw)
To: netdev, richardcochran, vadim.fedorenko
Cc: andrew+netdev, davem, edumazet, kuba, linux-kernel, pabeni
On Thu, 19 Nov 2026 at 18:24:24 +0000, Vadim Fedorenko wrote:
> On 18/11/2026 20:05, Ahmad Byagowi wrote:
>> + mutex_lock(&bp->ts_lock);
>> + if (enable && bp->ts_stopping) {
>> + err = -ENODEV;
>> + goto out;
>> + }
>
> ts_stopping is changed only in device detach callback, there is actually
> no need to cover whole ptp_ocp_ts_enable() with mutex, I believe
> existing spinlock can be used to serialize this access, no?
Yes, the existing bp->lock is sufficient. I have sent the four-patch
v2 common-fix series to net:
https://lore.kernel.org/r/20261119-timecard-common-v2-078fe69f33-0-ahmadexp@gmail.com
The revised timestamp fix uses spin_lock_irqsave() for the stopping
check, PPS subscription updates and MMIO. Detach sets ts_stopping under the same lock. Keeping
the register writes in that critical section prevents an enable which
passed the check from writing after detach has disabled the channel.
The disable path still flushes the interrupt-mask write, but drops the
spinlock and restores IRQ flags before both pci_irq_vector() and
synchronize_irq(). The former takes the MSI descriptor mutex on this
kernel; the latter may sleep. There is no new driver mutex or associated
initialization/destruction.
One additional detail is the PPS no-state-change shortcut. Once the
IRQ wait is outside the lock, another disable can clear pps_req_map and
unlock before draining its IRQ. While stopping, the candidate bypasses
that shortcut so detach still disables and drains the channel itself.
Outside teardown, removing one of the PPS/EXTTS subscriptions still
leaves the other subscription armed without unnecessary MMIO.
The source-derived ASan/UBSan harness passes late-enable, in-flight-enable,
active-IRQ and in-flight-disable cases, shared PPS/EXTTS handling, and all
six channels. It asserts that MMIO is locked and vector lookup and IRQ
waits are unlocked; six deliberately broken variants are rejected.
Both common and WiWi integration PTP objects compile for x86-64 with W=1
and KCFLAGS=-Werror.
This is software evidence, not kernel lockdep or physical unbind testing.
Thanks for the suggestion.
Ahmad
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations
2026-11-18 20:05 [PATCH net 0/4] ptp: ocp: fix timestamp teardown and DPLL routing Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach Ahmad Byagowi
@ 2026-11-18 20:05 ` Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 4/4] ptp: ocp: correct the DCF output frequency Ahmad Byagowi
3 siblings, 0 replies; 7+ messages in thread
From: Ahmad Byagowi @ 2026-11-18 20:05 UTC (permalink / raw)
To: netdev, vadim.fedorenko, richardcochran
Cc: andrew+netdev, arkadiusz.kubalewski, davem, edumazet, jiri, kuba,
linux-kernel, pabeni
The selector table index is not the hardware selector value. Passing the
index to the SMA setter can select unrelated signals, or fail on ART.
Use the table value, as the sysfs path already does.
Mask the direction enable bit before the frequency lookup. Otherwise an
enabled, direction-configurable pin has no matching table entry and its
DPLL get or dump fails. Apply both corrections to all board variants.
Reported by the netdev Sashiko AI review and confirmed by code
inspection. Source-derived C regression tests pass with ASan/UBSan;
restoring the faulty selector, mask, disabled-state or DCF value makes
the corresponding regression fail. Tests use stubbed MMIO, not hardware.
The driver object builds on current net with W=1 and KCFLAGS=-Werror. No
physical card was tested.
Codex prepared the fix, tests and changelog from the review and upstream
source, at the request to separate common-driver fixes from WiWi support
and validate removal ordering.
Fixes: 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
drivers/ptp/ptp_ocp.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index 478f7012b2..d449ed8ca2 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -5900,7 +5900,8 @@ static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
tbl = bp->sma_op->tbl[sma->mode];
for (i = 0; tbl[i].name; i++)
if (tbl[i].frequency == frequency)
- return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr + 1);
+ return ptp_ocp_sma_store_val(bp, tbl[i].value,
+ sma->mode, sma_nr + 1);
return -EINVAL;
}
@@ -5918,6 +5919,7 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
int i;
val = bp->sma_op->get(bp, sma_nr + 1);
+ val &= SMA_SELECT_MASK;
tbl = bp->sma_op->tbl[sma->mode];
for (i = 0; tbl[i].name; i++)
if (val == tbl[i].value) {
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL
2026-11-18 20:05 [PATCH net 0/4] ptp: ocp: fix timestamp teardown and DPLL routing Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations Ahmad Byagowi
@ 2026-11-18 20:05 ` Ahmad Byagowi
2026-11-18 20:05 ` [PATCH net 4/4] ptp: ocp: correct the DCF output frequency Ahmad Byagowi
3 siblings, 0 replies; 7+ messages in thread
From: Ahmad Byagowi @ 2026-11-18 20:05 UTC (permalink / raw)
To: netdev, vadim.fedorenko, richardcochran
Cc: andrew+netdev, arkadiusz.kubalewski, davem, edumazet, jiri, kuba,
linux-kernel, pabeni
Disabling an SMA input clears its mux field. Zero is also the selector
for 10 MHz, so a DPLL read reports 10 MHz while sysfs reports None. The
DPLL core then treats a request to enable 10 MHz as unchanged and skips
the setter, leaving the input disabled.
Substitute SMA_DISABLE for disabled inputs before looking up the
frequency, as the sysfs path already does. The None entry reports zero
Hz and a subsequent 10 MHz request reaches the setter.
Reported by the netdev Sashiko AI review and confirmed by code
inspection. Source-derived C regression tests pass with ASan/UBSan;
restoring the faulty selector, mask, disabled-state or DCF value makes
the corresponding regression fail. Tests use stubbed MMIO, not hardware.
The driver object builds on current net with W=1 and KCFLAGS=-Werror. No
physical card was tested.
Codex prepared the fix, tests and changelog from the review and upstream
source, at the request to separate common-driver fixes from WiWi support
and validate removal ordering.
Fixes: 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
drivers/ptp/ptp_ocp.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index d449ed8ca2..650047581f 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -5920,6 +5920,8 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
val = bp->sma_op->get(bp, sma_nr + 1);
val &= SMA_SELECT_MASK;
+ if (sma->mode == SMA_MODE_IN && sma->disabled)
+ val = SMA_DISABLE;
tbl = bp->sma_op->tbl[sma->mode];
for (i = 0; tbl[i].name; i++)
if (val == tbl[i].value) {
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net 4/4] ptp: ocp: correct the DCF output frequency
2026-11-18 20:05 [PATCH net 0/4] ptp: ocp: fix timestamp teardown and DPLL routing Ahmad Byagowi
` (2 preceding siblings ...)
2026-11-18 20:05 ` [PATCH net 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL Ahmad Byagowi
@ 2026-11-18 20:05 ` Ahmad Byagowi
3 siblings, 0 replies; 7+ messages in thread
From: Ahmad Byagowi @ 2026-11-18 20:05 UTC (permalink / raw)
To: netdev, vadim.fedorenko, richardcochran
Cc: andrew+netdev, arkadiusz.kubalewski, davem, edumazet, jiri, kuba,
linux-kernel, pabeni
The output selector table uses 77000 Hz for DCF, whereas the input table
and the DPLL supported frequency list use 77500 Hz. A request for the
advertised DCF77 frequency consequently fails on an output, and reading
that output reports a frequency outside its advertised set.
Use 77500 Hz for the DCF output selector as well.
Reported by the netdev Sashiko AI review and confirmed by code
inspection. Source-derived C regression tests pass with ASan/UBSan;
restoring the faulty selector, mask, disabled-state or DCF value makes
the corresponding regression fail. Tests use stubbed MMIO, not hardware.
The driver object builds on current net with W=1 and KCFLAGS=-Werror. No
physical card was tested.
Codex prepared the fix, tests and changelog from the review and upstream
source, at the request to separate common-driver fixes from WiWi support
and validate removal ordering.
Fixes: 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
Link: https://lore.kernel.org/r/179502617840.3136.12704245716223038161@kernel.org
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
drivers/ptp/ptp_ocp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
index 650047581f..aa950cae68 100644
--- a/drivers/ptp/ptp_ocp.c
+++ b/drivers/ptp/ptp_ocp.c
@@ -1376,7 +1376,7 @@ static const struct ocp_selector ptp_ocp_sma_out[] = {
{ .name = "GNSS1", .value = 0x0004, .frequency = 1 },
{ .name = "GNSS2", .value = 0x0008, .frequency = 1 },
{ .name = "IRIG", .value = 0x0010, .frequency = 10000 },
- { .name = "DCF", .value = 0x0020, .frequency = 77000 },
+ { .name = "DCF", .value = 0x0020, .frequency = 77500 },
{ .name = "GEN1", .value = 0x0040 },
{ .name = "GEN2", .value = 0x0080 },
{ .name = "GEN3", .value = 0x0100 },
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 7+ messages in thread