Netdev List
 help / color / mirror / Atom feed
From: Ahmad Byagowi <ahmadexp@gmail.com>
To: netdev@vger.kernel.org, vadim.fedorenko@linux.dev,
	richardcochran@gmail.com
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com
Subject: [PATCH net v2 1/4] ptp: ocp: prevent timestamp rearming during detach
Date: Thu, 19 Nov 2026 13:19:48 -0800	[thread overview]
Message-ID: <20261119-timecard-common-v2-078fe69f33-1-ahmadexp@gmail.com> (raw)
In-Reply-To: <20261119-timecard-common-v2-078fe69f33-0-ahmadexp@gmail.com>

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.

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
Link: https://lore.kernel.org/r/ad6883e5-c103-4f82-8040-e5463477ca10@linux.dev
Signed-off-by: Ahmad Byagowi <ahmadexp@gmail.com>
---
 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. */
+		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, &reg->intr_mask);
 		iowrite32(1, &reg->intr);
 	} else {
-		int irq_vec = pci_irq_vector(bp->pdev, ext->irq_vec);
-
 		iowrite32(0, &reg->intr_mask);
 		iowrite32(0, &reg->enable);
 		ioread32(&reg->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);

  reply	other threads:[~2026-11-19 21:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 21:19 [PATCH net v2 0/4] ptp: ocp: fix timestamp teardown and DPLL routing Ahmad Byagowi
2026-11-19 21:19 ` Ahmad Byagowi [this message]
2026-11-19 21:19 ` [PATCH net v2 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations Ahmad Byagowi
2026-11-19 21:19 ` [PATCH net v2 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL Ahmad Byagowi
2026-11-19 21:19 ` [PATCH net v2 4/4] ptp: ocp: correct the DCF output frequency Ahmad Byagowi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-timecard-common-v2-078fe69f33-1-ahmadexp@gmail.com \
    --to=ahmadexp@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=vadim.fedorenko@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox