Skip to contentSkip to conversation

ptp: ocp: fix resource freeing order

PATCH Superseded Vadim Fedorenko 2 participants 2 messages May 22 – May 25

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Vadim Fedorenko

author maintainer
#0 lore raw

PATCH v4 1/1 ptp: ocp: fix resource freeing order

Commit a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events") added a call to ptp_disable_all_events() which changes the configuration of pins if they support EXTTS events. In ptp_ocp_detach() pins resources are freed before ptp_clock_unregister() and it leads to use-after-free during driver removal. Fix it by changing the order of free/unregister calls. To avoid irq handler running on the other core while ptp device unregistering, call synchronize_irq() after HW is configured to stop producing irqs and no irqs are in-flight.

Fixes a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events")

---
v3 -> v4:
* address issues flagged by Sashiko by adding synchronize_irq() after
* disabling HW irq
v2 -> v3:
* fix code issue in v2
v1 -> v2:
* on v1 sashiko mentioned that some IRQs may fire after bp->ptp freed. it looks like a false positive, because ptp_clock_unregister() will explicitly disable all EXTTS channels before destroying ptp_clock structure. But there is a possibility of timestampers being active without PTP_PF_EXTTS function set. It can be done through sysfs files and it will change pins configuration directly, but can only be done for TS1..TS4. Disable them explicitly after detaching sysfs files.

---
 drivers/ptp/ptp_ocp.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/ptp/ptp_ocp.c +17 -3
index d88ab2f86b1b..12e1642c677a 100644
@@ -2216,8 +2216,13 @@ 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);
+		if (irq_vec > 0)
+			synchronize_irq(irq_vec);
 	}
 
 	return 0;
@@ -4558,6 +4563,18 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 	ptp_ocp_detach_sysfs(bp);
 	ptp_ocp_attr_group_del(bp);
 	timer_delete_sync(&bp->watchdog);
+	/* Disable interrupts on all timestampers */
+	if (bp->ts1)
+		ptp_ocp_ts_enable(bp->ts1, 0, false);
+	if (bp->ts2)
+		ptp_ocp_ts_enable(bp->ts2, 0, false);
+	if (bp->ts3)
+		ptp_ocp_ts_enable(bp->ts3, 0, false);
+	if (bp->ts4)
+		ptp_ocp_ts_enable(bp->ts4, 0, false);
+	if (bp->ptp)
+		ptp_clock_unregister(bp->ptp);
+	kfree(bp->ptp_info.pin_config);
 	ptp_ocp_unregister_ext(bp->ts0);
 	ptp_ocp_unregister_ext(bp->ts1);
 	ptp_ocp_unregister_ext(bp->ts2);
@@ -4575,9 +4592,6 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
 	if (bp->n_irqs)
 		pci_free_irq_vectors(bp->pdev);
-	if (bp->ptp)
-		ptp_clock_unregister(bp->ptp);
-	kfree(bp->ptp_info.pin_config);
 	device_unregister(&bp->dev);
 }
 
Signature · 1 line
2.47.3
maintainer
#1 lore raw

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
ptp: ocp: fix resource freeing order

This change reorders ptp_ocp_detach() so that ptp_clock_unregister() runs before the external timestamp sources are torn down, and adds a synchronize_irq() inside ptp_ocp_ts_enable() when disabling, to address a use-after-free introduced by commit a60fc3294a37 ("ptp: rework ptp_clock_unregister() to disable events").

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
index beacc2ffb166..1ac58951188a 100644

[ ... ]

Quoted 9 lines
drivers/ptp/ptp_ocp.c +5 -0
@@ -4867,6 +4872,18 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 	ptp_ocp_detach_sysfs(bp);
 	ptp_ocp_attr_group_del(bp);
 	timer_delete_sync(&bp->watchdog);
+	/* Disable interrupts on all timestampers */
+	if (bp->ts1)
+		ptp_ocp_ts_enable(bp->ts1, 0, false);
+	if (bp->ts2)
+		ptp_ocp_ts_enable(bp->ts2, 0, false);

drivers/ptp/ptp_ocp.c ptp_ocp_detach(struct ptp_ocp *bp) · line 4889

Quoted 10 lines
drivers/ptp/ptp_ocp.c +7 -0
+	if (bp->ts3)
+		ptp_ocp_ts_enable(bp->ts3, 0, false);
+	if (bp->ts4)
+		ptp_ocp_ts_enable(bp->ts4, 0, false);
+	if (bp->ptp)
+		ptp_clock_unregister(bp->ptp);
+	kfree(bp->ptp_info.pin_config);
 	ptp_ocp_unregister_ext(bp->ts0);
 	ptp_ocp_unregister_ext(bp->ts1);
 	ptp_ocp_unregister_ext(bp->ts2);

[High]
The commit message says "To avoid irq handler running on the other core while ptp device unregistering, call synchronize_irq() after HW is configured to stop producing irqs and no irqs are in-flight." Does this pre-disable loop cover all of the timestampers that share the ptp_ocp_ts_irq path?

The loop disables ts1..ts4 but not bp->ts0 or bp->pps, yet both of those sources go through ptp_ocp_ts_irq() which calls ptp_clock_event(ext->bp->ptp, &ev).

After ptp_clock_unregister(bp->ptp) returns, the matching put_device() inside it can drop the final reference and free bp->ptp via ptp_clock_release(). Until ptp_ocp_unregister_ext(bp->ts0) and ptp_ocp_unregister_ext(bp->pps) run later in the function (which call ext->info->enable(ext, ~0, false) and the internal synchronize_irq inside pci_free_irq), can an in-flight ts0 or pps interrupt dereference the freed bp->ptp?

ts0 is the GNSS1 PPS timestamper and bp->pps is the internal 1 Hz PPS, both of which userspace can enable via PTP_EXTTS_REQUEST without ever populating pin_config (they have no SMA pin to map), so ptp_disable_all_events() walking pin_config does not necessarily turn them off. For bp->pps in particular, when only OCP_REQ_TIMESTAMP is set in pps_req_map, ptp_ocp_ts_enable() takes the early-return path:

	if (ext == bp->pps) {
		u32 old_map = bp->pps_req_map;

		if (enable)
			bp->pps_req_map |= req;
		else
			bp->pps_req_map &= ~req;

		/* if no state change, just return */
		if ((!!old_map ^ !!bp->pps_req_map) == 0)
			return 0;
	}

so even a disable from ptp_disable_all_events() can leave the HW enabled.

Would extending the pre-disable loop to also cover bp->ts0 and bp->pps (using ~0 as req, like ptp_ocp_unregister_ext() does, so the pps early return is not hit) close the same race for those sources?

Signature · 1 line
pw-bot: cr