Skip to contentSkip to conversation

dpaa2-eth: free the DPNI interrupt before releasing its resources

PATCH New Myeonghun Pak 2 participants 2 messages Nov 12 – Nov 16

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

PATCH 1/1 dpaa2-eth: free the DPNI interrupt before releasing its resources

dpaa2_eth_remove() and the probe error path call fsl_mc_free_irqs(), which returns MC interrupt resources without removing the devm-managed handler. Until devres runs, a link or endpoint change can race with MAC disconnection or access the freed MC portal and net device.

Add dpaa2_eth_free_irqs() to release and synchronize the handler before returning its MC resource. Use it before disconnecting the MAC and freeing the portal and net device in both paths.

Found by a static analysis tool under development. Not tested on hardware.

Fixes 6e2387e8f19e ("staging: fsl-dpaa2/eth: Add Freescale DPAA2 Ethernet driver")

---

 drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +10 -2
index 7227a35dfaf2..91462b88b34a 100644
@@ -4824,6 +4824,14 @@ static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
 	return err;
 }
 
+static void dpaa2_eth_free_irqs(struct fsl_mc_device *ls_dev)
+{
+	struct fsl_mc_device_irq *irq = ls_dev->irqs[0];
+
+	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
+	fsl_mc_free_irqs(ls_dev);
+}
+
 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
 {
 	int i;
@@ -5044,7 +5052,7 @@ static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
 	if (priv->do_link_poll)
 		kthread_stop(priv->poll_thread);
 	else
-		fsl_mc_free_irqs(dpni_dev);
+		dpaa2_eth_free_irqs(dpni_dev);
 err_poll_thread:
 	dpaa2_eth_disconnect_mac(priv);
 err_connect_mac:
@@ -5104,7 +5112,7 @@ static void dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
 	if (priv->do_link_poll)
 		kthread_stop(priv->poll_thread);
 	else
-		fsl_mc_free_irqs(ls_dev);
+		dpaa2_eth_free_irqs(ls_dev);
 
 	dpaa2_eth_disconnect_mac(priv);
 	dpaa2_eth_free_rings(priv);

base-commit: 0edbc945eb9edd0086f25b151a79cfe4adc0dfa8

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The patch's new synchronization point, dpaa2_eth_free_irqs() (or kthread_stop() for the poll thread), still sits after…

Pre-existing issues:
- [High] The dpaa2-switch driver in the same directory still has the defect this patch fixes in dpaa2-eth.
- [Medium] dpaa2_eth_free_irqs() releases the Linux handler (devm_free_irq) and returns the MSI vector to the parent DPRC pool…

--

dpaa2-eth: free the DPNI interrupt before releasing its resources

This commit adds dpaa2_eth_free_irqs(). It calls devm_free_irq() on the DPNI interrupt before fsl_mc_free_irqs(). The helper is used in dpaa2_eth_remove() and in the dpaa2_eth_probe() error path, so the handler is released and synchronized before the MAC is disconnected and the portal and net device are freed.

Quoted 5 lines
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +0 -0
index 7227a35dfaf2f..91462b88b34ad 100644

drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev) · line 4832

Quoted 9 lines
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +6 -0
@@ -4824,6 +4824,14 @@ static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
 	return err;
 }
 
+static void dpaa2_eth_free_irqs(struct fsl_mc_device *ls_dev)
+{
+	struct fsl_mc_device_irq *irq = ls_dev->irqs[0];
+
+	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
+	fsl_mc_free_irqs(ls_dev);

[Severity: Medium]
This isn't a bug introduced by this patch, but should this helper also disable the DPNI interrupt in the MC before the vector is given back?

dpaa2_eth_setup_irqs() arms it:

	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
				  DPNI_IRQ_INDEX, 1);

Nothing in dpaa2-eth ever calls dpni_set_irq_enable(..., 0). On the free path, __fsl_mc_msi_write_msg() also returns early without unprogramming the MSI:

	if (msi_desc->msg.address_lo == 0x0 && msi_desc->msg.address_hi == 0x0)
		return;

Once fsl_mc_free_irqs() returns the vector to the DPRC pool, another object in the same container could be given that vector. Suppose a DPNI link or endpoint change happens before dpaa2_eth_free_dpni()->dpni_reset(), or at any later time if the reset doesn't clear the enable. The DPNI could then send a stale MSI to the new owner's handler, and nothing would clear the DPNI IRQ status.

This depends on MC firmware behavior that can't be confirmed from the kernel source.

The peer teardown paths disable the interrupt at the object first. dprc_teardown_irq() does:

	(void)disable_dprc_irq(mc_dev);

	devm_free_irq(&mc_dev->dev, irq->virq, &mc_dev->dev);

	fsl_mc_free_irqs(mc_dev);

dpaa2_switch_teardown_irqs() also calls dpsw_set_irq_enable(..., 0) before fsl_mc_free_irqs().

Would a dpni_set_irq_enable(..., 0) call ahead of devm_free_irq() fit in this helper?

[Severity: High]
This isn't a bug introduced by this patch, but dpaa2-switch.c in the same directory seems to have the same problem that this patch fixes in dpaa2-eth.

dpaa2_switch_setup_irqs() registers dpaa2_switch_irq0_handler_thread() with devm_request_threaded_irq(). The teardown helper does only this:

drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.c:dpaa2_switch_teardown_irqs() {
	err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
				  DPSW_IRQ_INDEX_IF, 0);
	...
	fsl_mc_free_irqs(sw_dev);
}

There is no devm_free_irq() call. dpaa2_switch_remove() then frees the port netdevs and port_priv, ethsw->ports, ethsw->lags, the MC portal and ethsw. The handler stays registered until devres runs, after remove() returns.

Disabling the interrupt in the MC stops new interrupts. It doesn't wait for a handler thread that is already running, and that thread does:

	port_priv = ethsw->ports[if_id];

	if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
		dpaa2_switch_port_link_state_update(port_priv->netdev);

It later calls dpsw_clear_irq_status(ethsw->mc_io, ...).

Can these accesses hit freed memory? The dpaa2_switch_probe() error path at err_unregister_ports uses the same helper.

The error path inside dpaa2_switch_setup_irqs() already pairs devm_free_irq() with fsl_mc_free_irqs(). Should dpaa2-switch get the same fix, perhaps as a follow-up patch?

dpaa2-ptp doesn't have this problem. It uses request_threaded_irq(), and dpaa2_ptp_remove() calls ptp_qoriq_free(), which reaches free_irq(), before fsl_mc_free_irqs().

Quoted 5 lines
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +2 -0
+}
+
 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
 {
 	int i;

[ ... ]

drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c static void dpaa2_eth_remove(struct fsl_mc_device *ls_dev) · line 5115

Quoted 6 lines
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +1 -1
@@ -5104,7 +5112,7 @@ static void dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
 	if (priv->do_link_poll)
 		kthread_stop(priv->poll_thread);
 	else
-		fsl_mc_free_irqs(ls_dev);
+		dpaa2_eth_free_irqs(ls_dev);

[Severity: High]
Does this synchronization happen early enough? In dpaa2_eth_remove() it still runs after unregister_netdev():

	unregister_netdev(net_dev);

	dpaa2_eth_dl_port_del(priv);
	dpaa2_eth_dl_traps_unregister(priv);
	dpaa2_eth_dl_free(priv);

	if (priv->do_link_poll)
		kthread_stop(priv->poll_thread);
	else
		dpaa2_eth_free_irqs(ls_dev);

The commit message says a link or endpoint change can "access the freed MC portal and net device". A link-up handler that overlaps with unregister_netdev() still seems able to do that.

One possible sequence:

dpni_irq0_handler_thread()
dpaa2_eth_link_state_update()

    dpni_get_link_state() -> state.up = 1
    dpaa2_eth_set_rx_taildrop() (MC commands, may sleep)

                              dpaa2_eth_remove()
                                unregister_netdev()
                                  dpaa2_eth_stop()
                                    netif_carrier_off()
                                  linkwatch_forget_dev()
                                  reg_state = NETREG_UNREGISTERED

    netif_carrier_on(priv->net_dev)
      linkwatch_fire_event()
        linkwatch_add_event()
          netdev_hold(), dev added to lweventlist

                                dpaa2_eth_free_irqs() (thread already done)
                                ...
                                free_netdev(net_dev)

netif_carrier_on() returns early only for NETREG_UNINITIALIZED:

	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
		if (dev->reg_state == NETREG_UNINITIALIZED)
			return;
		atomic_inc(&dev->carrier_up_count);
		linkwatch_fire_event(dev);

So the unregistered device still gets queued on lweventlist. Could the linkwatch work then run linkwatch_do_dev() and netdev_put() on the net_device after free_netdev()?

dpaa2_eth_link_state_update() and the unregister/stop path don't share a lock. dpaa2_eth_poll_link_state() is also stopped only at this point, so the poll thread has the same window.

Would it be better to quiesce the DPNI interrupt, or stop the poll thread, before unregister_netdev()?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112065616.34209-1-mhun512%40gmail.com