Skip to contentSkip to conversation

igb: fix runtime PM resume issues

PATCH Superseded Antonio Quartulli 3 participants 16 messages Nov 30 – Dec 2

Subsystems (5)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover igb: fix runtime PM resume issues

igb can be runtime suspended when the link goes down (with power/control set to auto). The suspend can deadlock with an RTNL holder resuming the device, e.g. ethtool (patch 1). Resuming calls functions that need RTNL without holding it (patches 3 and 5), XDP and XSK changes race with a resume or follow a failed one (patch 6), and devices that can't signal a wake-up are suspended anyway, so their port stays down when the link comes back (patch 7).

Patch 2 fixes the XSK pool disable path when the device has fewer rings than the stack has queues (a single Tx queue with VFs on 82576, or none while runtime suspended); patch 4 keeps such queues while they still have a zero-copy pool, so that it can still be disabled.

Tested with the 82576 emulated by QEMU, with test-only hooks to delay a runtime suspend, to make the open fail on resume, and to force MSI with a single queue on resume. Before/after this version: ethtool during a runtime suspend hangs with RTNL held / completes, and the suspend is retried; closing a zero-copy AF_XDP socket after a failed resume hangs in napi_disable() with RTNL held / completes, then removing the XDP program works and adding one fails with -ENODEV. The runtime suspend tests ran without patch 7, as QEMU's 82576 can't signal PME. Not
tested: real hardware and PME wake-up, a real system sleep, PCI error recovery. Not run under KASAN.

Pre-existing issues left for a follow-up: in the XSK queue disable and enable paths the Tx NAPI isn't stopped when Rx and Tx don't share a vector, a failed buffer reallocation leaves the queue half torn down, Rx wakeups fail on a queue without a Tx ring, and disabling a pool during PCI error recovery still hangs. igc has the same issues (see the notes) and also takes RTNL when runtime suspending; its fixes will be sent separately.

Changes in v6:
- new patch 1: don't wait for RTNL when runtime suspending (Sashiko)
- patch 2: the message and comment name the runtime suspended case (Sashiko)
- patch 3: the work item resumes the device first (Sashiko)
- patch 4: only zero-copy pools keep a queue (Sashiko)
- patch 5: don't cancel the work item in __igb_shutdown() anymore
- patch 6: handle a failed resume (Sashiko)
- added Janis's Reviewed-by to the unchanged patches 2 and 7

Changes in v5:
- new patch 3: keep the stack's Rx queues that still have an XSK pool (Janis)
- patch 4: cancel the work item before a suspend (Sashiko)
- new patch 5: resume the device before changing XDP or XSK settings (Sashiko)
- patch 6: check pci_dev_run_wake() when scheduling the suspend (Sashiko)

Changes in v4:
- patch 1: also handle queues with an Rx but no Tx ring, as with VFs:
tear down the Rx ring and skip only the Tx part (Sashiko); subject changed from "don't disable rings ..." to "don't touch rings ..."

Changes in v3:
- new patch 1
- patch 2: set the real queue counts on system sleep resume and from a work item on runtime resume, and drop packets for queues the device doesn't have until then (Janis, Sashiko)
- patch 3 (now 4): link the NAPIs from the same work item

Changes in v2:
- patch 2 (v2 patch 1): keep setting the queue counts in __igb_open(), only skipping them on resume (Sashiko)
- mention the tested hardware in the commit messages

v5: https://lore.kernel.org/netdev/[email protected]/
v4: https://lore.kernel.org/netdev/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

Antonio Quartulli (7):
igb: don't wait for RTNL when runtime suspending
igb: don't touch rings that don't exist when disabling an XSK pool
igb: fix RTNL assertion when resuming from runtime suspend
igb: keep the stack's Rx queues that still have an XSK pool
igb: link queues to NAPIs under RTNL after runtime resume
igb: resume the device before changing XDP or XSK settings
igb: only allow runtime PM if the device can wake up

 drivers/net/ethernet/intel/igb/igb.h      |   2 +
 drivers/net/ethernet/intel/igb/igb_main.c | 182 ++++++++++++++++++----
 drivers/net/ethernet/intel/igb/igb_xsk.c  |  42 +++--
 3 files changed, 186 insertions(+), 40 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH v6 1/7 igb: don't wait for RTNL when runtime suspending

The PM core marks the device as suspending before calling igb_runtime_suspend(), which takes RTNL. An RTNL holder that resumes the device meanwhile, e.g. ethtool, igb_open() or igb_close(), waits for the suspend to end, and the suspend waits for RTNL: both hang, with RTNL taken.

The suspend still needs RTNL, as it closes the interface and frees the queues. When runtime suspending, only try to take it, and return -EBUSY if it's taken, which keeps the device active. Retry from a work item:
resuming and releasing the device schedules a new suspend if the link is still down.

Fixes 9474933caf21 ("igb: close/suspend race in netif_device_detach")

---
v6: new patch (Sashiko)

 drivers/net/ethernet/intel/igb/igb.h      |  1 +
 drivers/net/ethernet/intel/igb/igb_main.c | 28 ++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
drivers/net/ethernet/intel/igb/igb.h +1 -0
index 8c9b020..4250b4d 100644
@@ -586,6 +586,7 @@ struct igb_adapter {
 
 	struct work_struct reset_task;
 	struct work_struct watchdog_task;
+	struct work_struct rpm_retry_task;
 	bool fc_autoneg;
 	u8  tx_timeout_factor;
 	struct timer_list blink_timer;
drivers/net/ethernet/intel/igb/igb_main.c +27 -1
index d4a897a..fffcb31 100644
@@ -119,6 +119,7 @@ static void igb_set_rx_mode(struct net_device *);
 static void igb_update_phy_info(struct timer_list *);
 static void igb_watchdog(struct timer_list *);
 static void igb_watchdog_task(struct work_struct *);
+static void igb_rpm_retry_task(struct work_struct *);
 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
 static void igb_get_stats64(struct net_device *dev,
 			    struct rtnl_link_stats64 *stats);
@@ -3426,6 +3427,7 @@ static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	INIT_WORK(&adapter->reset_task, igb_reset_task);
 	INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
+	INIT_WORK(&adapter->rpm_retry_task, igb_rpm_retry_task);
 
 	/* Initialize link properties that are user-changeable */
 	adapter->fc_autoneg = true;
@@ -3889,6 +3891,7 @@ static void igb_remove(struct pci_dev *pdev)
 
 	cancel_work_sync(&adapter->reset_task);
 	cancel_work_sync(&adapter->watchdog_task);
+	cancel_work_sync(&adapter->rpm_retry_task);
 
 #ifdef CONFIG_IGB_DCA
 	if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
@@ -9501,7 +9504,18 @@ static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
 	u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
 	bool wake;
 
-	rtnl_lock();
+	/* The PM core marks the device as suspending before a runtime
+	 * suspend, and an RTNL holder resuming it meanwhile (e.g. ethtool)
+	 * waits for the suspend to end: waiting for RTNL here would deadlock.
+	 * Only try to take it: -EBUSY keeps the device active, and
+	 * igb_rpm_retry_task() schedules a new suspend.
+	 */
+	if (!runtime) {
+		rtnl_lock();
+	} else if (!rtnl_trylock()) {
+		schedule_work(&adapter->rpm_retry_task);
+		return -EBUSY;
+	}
 	netif_device_detach(netdev);
 
 	if (netif_running(netdev))
@@ -9666,6 +9680,18 @@ static int igb_runtime_idle(struct device *dev)
 	return -EBUSY;
 }
 
+/* Resuming and releasing the device runs igb_runtime_idle(), which schedules
+ * a new suspend if the link is still down
+ */
+static void igb_rpm_retry_task(struct work_struct *work)
+{
+	struct igb_adapter *adapter = container_of(work, struct igb_adapter,
+						   rpm_retry_task);
+
+	pm_runtime_get_sync(&adapter->pdev->dev);
+	pm_runtime_put(&adapter->pdev->dev);
+}
+
 static int igb_runtime_suspend(struct device *dev)
 {
 	return __igb_shutdown(to_pci_dev(dev), NULL, 1);
Signature · 1 line
2.56.1
#2 lore raw

PATCH v6 2/7 igb: don't touch rings that don't exist when disabling an XSK pool

In reply to Antonio Quartulli

xsk_get_pool_from_qid() checks the queue id against the stack's real queue counts, which can be higher than the number of rings the device
has: on 82576, enabling VFs leaves a single Tx queue while keeping two Rx queues, and a runtime suspend frees all rings. Disabling the pool of such a queue, e.g. one bound to queue 1 before enabling VFs, then passes a NULL Tx ring to igb_txrx_ring_disable(), and a NULL Rx ring too if the socket is closed while the device is runtime suspended.

Only disable and re-enable the rings the device has: skip the queue if it has no Rx ring, and only the Tx part if it has no Tx ring, so that an Rx ring using the pool still stops using it.

Tested with the 82576 emulated by QEMU and a zero-copy AF_XDP socket on queue 1: closing it after enabling a VF no longer oopses and the Rx ring no longer references the pool afterwards (checked with a debug print, as QEMU doesn't spread traffic over the PF queues with VFs), and without VFs queue 1 keeps receiving after the close. Not run under KASAN.

Fixes 80f6ccf9f116 ("igb: Introduce XSK data structures and helpers")

---
v6: the message and comment name the runtime suspended case instead of the MSI fallback, which only reaches this check with patch 4 (Sashiko); no code changes
v5: the message also mentions the runtime suspended case (no code changes)
v4: also handle queues with an Rx but no Tx ring, as with VFs (Sashiko):
tear down the Rx ring and skip only the Tx part; tested with a zero-copy AF_XDP socket; subject was "don't disable rings that don't exist ..."
v3: new patch.

 drivers/net/ethernet/intel/igb/igb_xsk.c | 38 +++++++++++++++++-------
 1 file changed, 27 insertions(+), 11 deletions(-)
drivers/net/ethernet/intel/igb/igb_xsk.c +27 -11
index ce4a7b5..b6234e3 100644
@@ -31,15 +31,22 @@ static int igb_realloc_rx_buffer_info(struct igb_ring *ring, bool pool_present)
 	return 0;
 }
 
+/* With VFs, the device has a single Tx queue but can have more Rx queues */
+static struct igb_ring *igb_xsk_tx_ring(struct igb_adapter *adapter, u16 qid)
+{
+	return qid < adapter->num_tx_queues ? adapter->tx_ring[qid] : NULL;
+}
+
 static void igb_txrx_ring_disable(struct igb_adapter *adapter, u16 qid)
 {
-	struct igb_ring *tx_ring = adapter->tx_ring[qid];
+	struct igb_ring *tx_ring = igb_xsk_tx_ring(adapter, qid);
 	struct igb_ring *rx_ring = adapter->rx_ring[qid];
 	struct e1000_hw *hw = &adapter->hw;
 
-	set_bit(IGB_RING_FLAG_TX_DISABLED, &tx_ring->flags);
-
-	wr32(E1000_TXDCTL(tx_ring->reg_idx), 0);
+	if (tx_ring) {
+		set_bit(IGB_RING_FLAG_TX_DISABLED, &tx_ring->flags);
+		wr32(E1000_TXDCTL(tx_ring->reg_idx), 0);
+	}
 	wr32(E1000_RXDCTL(rx_ring->reg_idx), 0);
 
 	synchronize_net();
@@ -47,24 +54,27 @@ static void igb_txrx_ring_disable(struct igb_adapter *adapter, u16 qid)
 	/* Rx/Tx share the same napi context. */
 	napi_disable(&rx_ring->q_vector->napi);
 
-	igb_clean_tx_ring(tx_ring);
+	if (tx_ring) {
+		igb_clean_tx_ring(tx_ring);
+		memset(&tx_ring->tx_stats, 0, sizeof(tx_ring->tx_stats));
+	}
 	igb_clean_rx_ring(rx_ring);
-
 	memset(&rx_ring->rx_stats, 0, sizeof(rx_ring->rx_stats));
-	memset(&tx_ring->tx_stats, 0, sizeof(tx_ring->tx_stats));
 }
 
 static void igb_txrx_ring_enable(struct igb_adapter *adapter, u16 qid)
 {
-	struct igb_ring *tx_ring = adapter->tx_ring[qid];
+	struct igb_ring *tx_ring = igb_xsk_tx_ring(adapter, qid);
 	struct igb_ring *rx_ring = adapter->rx_ring[qid];
 
-	igb_configure_tx_ring(adapter, tx_ring);
+	if (tx_ring)
+		igb_configure_tx_ring(adapter, tx_ring);
 	igb_configure_rx_ring(adapter, rx_ring);
 
 	synchronize_net();
 
-	clear_bit(IGB_RING_FLAG_TX_DISABLED, &tx_ring->flags);
+	if (tx_ring)
+		clear_bit(IGB_RING_FLAG_TX_DISABLED, &tx_ring->flags);
 
 	/* call igb_desc_unused which always leaves
 	 * at least 1 descriptor unused to make sure
@@ -148,7 +158,13 @@ static int igb_xsk_pool_disable(struct igb_adapter *adapter, u16 qid)
 		return -EINVAL;
 
 	rx_ring = adapter->rx_ring[qid];
-	if_running = netif_running(adapter->netdev) && igb_xdp_is_enabled(adapter);
+	/* The stack can know more queues than the device has, e.g. while
+	 * runtime suspended, which frees the rings: skip the rings it
+	 * doesn't have.
+	 */
+	if_running = netif_running(adapter->netdev) &&
+		     igb_xdp_is_enabled(adapter) &&
+		     qid < adapter->num_rx_queues;
 	if (if_running)
 		igb_txrx_ring_disable(adapter, qid);
 
Signature · 1 line
2.56.1
#3 lore raw

PATCH v6 3/7 igb: fix RTNL assertion when resuming from runtime suspend

In reply to Antonio Quartulli

Since commit ac8c58f5b535 ("igb: fix deadlock caused by taking RTNL in RPM resume path") __igb_resume() doesn't take RTNL for runtime resume, but __igb_open() still calls netif_set_real_num_{tx,rx}_queues(), which require it. An asynchronous runtime resume (e.g. after the link comes back) then triggers "RTNL: assertion failed".

Only set the real queue counts in __igb_open() when RTNL is held (open and system sleep resume), and set them from a work item taking RTNL on runtime resume. The work item resumes the device first, so that it doesn't run in the middle of a later suspend or resume. Until it runs, after a fallback to fewer queues on resume (MSI-X to MSI), the stack can use queues the device doesn't have: drop those packets in igb_xmit_frame() instead of mapping several stack queues, each with its own xmit lock, onto the same ring. The stack can't be kept off those queues instead, as __igb_open() and netif_device_attach() start all of them and changing the count needs RTNL.

Tested with the 82576 emulated by QEMU.

Fixes ac8c58f5b535 ("igb: fix deadlock caused by taking RTNL in RPM resume path")

---
v6: the work item resumes the device first, so that it can't run during a suspend or resume (Sashiko); dropped Janis's Reviewed-by, as the patch changed
v5: no changes
v4: no changes
v3:
- set the real queue counts when RTNL is held (open and system sleep resume), and from a work item on runtime resume, instead of not at all on resume (Janis, Sashiko)
- until then, drop packets for queues the device doesn't have
- stable: netif_set_real_num_queues() needs 5.15+ and dev_core_stats_tx_dropped_inc() 5.18+; older backports can use the separate tx/rx calls and netdev->stats.tx_dropped
v2:
- keep setting the queue counts in __igb_open() after igb_request_irq(), only skipping them on resume (Sashiko)
- drop the reference to igc

Note: igc has the same issue: it sets the real queue counts only in igc_open(), before requesting the IRQs, and never on resume. A fix will follow separately once it has been tested on igc hardware (Joe offered to test).

 drivers/net/ethernet/intel/igb/igb.h      |  1 +
 drivers/net/ethernet/intel/igb/igb_main.c | 76 +++++++++++++++++------
 2 files changed, 57 insertions(+), 20 deletions(-)
drivers/net/ethernet/intel/igb/igb.h +1 -0
index 4250b4d..0639b00 100644
@@ -587,6 +587,7 @@ struct igb_adapter {
 	struct work_struct reset_task;
 	struct work_struct watchdog_task;
 	struct work_struct rpm_retry_task;
+	struct work_struct rtnl_resume_task;
 	bool fc_autoneg;
 	u8  tx_timeout_factor;
 	struct timer_list blink_timer;
drivers/net/ethernet/intel/igb/igb_main.c +56 -20

140 lines not on this page · raw message

Signature · 1 line
2.56.1
#4 lore raw

Re: [PATCH net v6 3/7] igb: fix RTNL assertion when resuming from runtime suspend

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: 0 · Medium: 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).

- [Medium] At this commit, the runtime-resume path still hits the RTNL assertion the patch says it fixes.

--

igb: fix RTNL assertion when resuming from runtime suspend

On runtime resume, __igb_open() no longer calls netif_set_real_num_queues(), since RTNL is not held there. A new rtnl_resume_task work item does it later under RTNL. Until that work runs, igb_xmit_frame() drops packets on stack queues beyond adapter->num_tx_queues.

Quoted 4 lines
drivers/net/ethernet/intel/igb/igb_main.c +0 -0
index 2fb9615d3bab3..5774a4d3c9810 100644

[ ... ]

Quoted 16 lines
drivers/net/ethernet/intel/igb/igb_main.c +2 -10
@@ -4181,16 +4209,15 @@ static int __igb_open(struct net_device *netdev, bool resuming)
 	if (err)
 		goto err_req_irq;
 
-	/* Notify the stack of the actual queue counts. */
-	err = netif_set_real_num_tx_queues(adapter->netdev,
-					   adapter->num_tx_queues);
-	if (err)
-		goto err_set_queues;
-
-	err = netif_set_real_num_rx_queues(adapter->netdev,
-					   adapter->num_rx_queues);
-	if (err)
-		goto err_set_queues;
+	/* Notify the stack of the actual queue counts. This needs RTNL: on
+	 * runtime resume igb_rtnl_resume_task() does it.

drivers/net/ethernet/intel/igb/igb_main.c static int __igb_open(struct net_device *netdev, bool resuming) · line 4223

Quoted 10 lines
drivers/net/ethernet/intel/igb/igb_main.c +7 -0
+	 */
+	if (rtnl_held) {
+		err = netif_set_real_num_queues(netdev, adapter->num_tx_queues,
+						adapter->num_rx_queues);
+		if (err)
+			goto err_set_queues;
+	}
 
 	/* From here on the code is the same as igb_up() */
 	clear_bit(__IGB_DOWN, &adapter->state);

[Severity: Medium]
Does runtime resume still hit an RTNL assertion a few lines below this? Only netif_set_real_num_queues() checks rtnl_held. The loop right after clear_bit() still runs on every path:

__igb_open() {
    ...
	for (i = 0; i < adapter->num_q_vectors; i++) {
		napi = &adapter->q_vector[i]->napi;
		napi_enable(napi);
		igb_set_queue_napi(adapter, i, napi);
	}
    ...
}

igb_set_queue_napi() calls netif_queue_set_napi() for the rx and tx ring of each vector. netif_queue_set_napi() in net/core/dev.c does:

	netdev_assert_locked_ops_compat_or_invisible(dev);

For a registered igb netdev that doesn't use the instance ops lock, this becomes ASSERT_RTNL(). The resulting path would be:

igb_runtime_resume()
__igb_resume(dev, true)
__igb_open(netdev, true, false)
igb_set_queue_napi()
netif_queue_set_napi()
ASSERT_RTNL()

If so, every async runtime resume of a running interface would still print "RTNL: assertion failed" with a WARN. The rxq->napi and txq->napi pointers would also still be written without RTNL.

Also, igb_rtnl_resume_task() only restores the real queue counts. __igb_shutdown() calls igb_clear_interrupt_scheme(), which frees the q_vectors. Should the work item also link the queues to the new NAPIs under RTNL?

A later patch in the series seems to fix this: "igb: link queues to NAPIs under RTNL after runtime resume". It wraps igb_set_queue_napi() in __igb_open() in an if (rtnl_held) check. It also calls a new igb_link_queue_napis() from igb_rtnl_resume_task().

Could that change be folded into this commit? As it stands, this commit is a bisect point that still triggers the assertion its subject says it fixes.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130213740.3323814-1-antonio%40mandelbit.com
#5 lore raw

Re: [PATCH net v6 3/7] igb: fix RTNL assertion when resuming from runtime suspend

Quoted 1 line

Does runtime resume still hit an RTNL assertion a few lines below this?

Yes, until patch 5. They are two bugs from different commits with different stable ranges (ac8c58f5b535 and b75a1dea500f, 6.16+), so they are fixed in separate patches; each one fixes the assertion its Fixes tag introduced.

#6 lore raw

PATCH v6 4/7 igb: keep the stack's Rx queues that still have an XSK pool

In reply to Antonio Quartulli

When igb lowers its queue counts by itself, after a fallback from MSI-X to MSI on resume or when enabling VFs, xsk_get_pool_from_qid() doesn't find the pool of a socket bound to a removed queue anymore. Disabling it then fails ("Failed to disable zero-copy!") and the pool's DMA mappings are never unmapped.

ethtool refuses to remove a queue that still has an XSK pool, but igb can't refuse a VF enable or a resume fallback. Keep the real Rx queue count up to the highest queue that still has a zero-copy XSK pool instead, so that the pool can still be disabled, and lower it at the next open. Copy mode pools don't need the driver to be disabled. The Tx count follows the device: an XSK pool is always bound to both an Rx and a Tx queue, and the lookup finds it through the Rx one.

Tested with the 82576 emulated by QEMU and a zero-copy AF_XDP socket, bound to queue 2 when enabling a VF, and to queue 1 across a runtime resume forced to use MSI with a single queue (with a test hook): closing it warned and leaked before, not anymore.

---
v6: only zero-copy pools keep a queue: copy mode pools don't need the driver to be disabled (Sashiko); dropped Janis's Reviewed-by
v5: new patch (Janis)

 drivers/net/ethernet/intel/igb/igb_main.c | 30 +++++++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)
drivers/net/ethernet/intel/igb/igb_main.c +25 -5
index bf24768..8b8e8c3 100644
@@ -4129,6 +4129,29 @@ static int igb_sw_init(struct igb_adapter *adapter)
 	return 0;
 }
 
+/* ethtool refuses to remove a queue that still has an XSK pool. igb can't
+ * refuse when it lowers its queue counts by itself, so keep the stack's Rx
+ * queue instead, or a zero-copy pool couldn't be disabled anymore: lower the
+ * count once it's gone, at the next open.
+ */
+static int igb_set_real_num_queues(struct igb_adapter *adapter)
+{
+	struct net_device *netdev = adapter->netdev;
+	unsigned int rxq = adapter->num_rx_queues;
+	struct xsk_buff_pool *pool;
+	unsigned int i;
+
+	for (i = netdev->real_num_rx_queues; i > rxq; i--) {
+		pool = xsk_get_pool_from_qid(netdev, i - 1);
+		if (pool && pool->umem->zc) {
+			rxq = i;
+			break;
+		}
+	}
+
+	return netif_set_real_num_queues(netdev, adapter->num_tx_queues, rxq);
+}
+
 /* Some settings need RTNL, which isn't held when resuming from runtime
  * suspend: apply them from here instead, if the device is still up.
  * Resuming the device first waits for a resume or suspend in progress.
@@ -4142,9 +4165,7 @@ static void igb_rtnl_resume_task(struct work_struct *work)
 	pm_runtime_get_sync(&adapter->pdev->dev);
 	if (!test_bit(__IGB_DOWN, &adapter->state) &&
 	    netif_running(adapter->netdev)) {
-		if (netif_set_real_num_queues(adapter->netdev,
-					      adapter->num_tx_queues,
-					      adapter->num_rx_queues))
+		if (igb_set_real_num_queues(adapter))
 			netdev_err(adapter->netdev,
 				   "error setting real queue count\n");
 	}
@@ -4214,8 +4235,7 @@ static int __igb_open(struct net_device *netdev, bool resuming,
 	 * runtime resume igb_rtnl_resume_task() does it.
 	 */
 	if (rtnl_held) {
-		err = netif_set_real_num_queues(netdev, adapter->num_tx_queues,
-						adapter->num_rx_queues);
+		err = igb_set_real_num_queues(adapter);
 		if (err)
 			goto err_set_queues;
 	}
Signature · 1 line
2.56.1
#7 lore raw

Re: [PATCH net v6 4/7] igb: keep the stack's Rx queues that still have an XSK pool

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] After this patch, the stack's Rx queue count and igb's ring count can disagree until the next open.

--

igb: keep the stack's Rx queues that still have an XSK pool

This patch adds igb_set_real_num_queues(). When igb lowers its own queue counts after a VF enable or an MSI-X to MSI fallback on resume, the helper keeps the stack's real Rx queue count up to the highest queue that still has a zero-copy XSK pool. That way the pool can still be found and disabled, and the count is lowered at the next open.

Quoted 19 lines
drivers/net/ethernet/intel/igb/igb_main.c +11 -0
index 5774a4d3c9810..9752350ffc68b 100644
@@ -4128,6 +4128,29 @@ static int igb_sw_init(struct igb_adapter *adapter)
 	return 0;
 }
 
+/* ethtool refuses to remove a queue that still has an XSK pool. igb can't
+ * refuse when it lowers its queue counts by itself, so keep the stack's Rx
+ * queue instead, or a zero-copy pool couldn't be disabled anymore: lower the
+ * count once it's gone, at the next open.
+ */
+static int igb_set_real_num_queues(struct igb_adapter *adapter)
+{
+	struct net_device *netdev = adapter->netdev;
+	unsigned int rxq = adapter->num_rx_queues;
+	struct xsk_buff_pool *pool;
+	unsigned int i;

drivers/net/ethernet/intel/igb/igb_main.c static int igb_sw_init(struct igb_adapter *adapter) · line 4151

Quoted 10 lines
drivers/net/ethernet/intel/igb/igb_main.c +10 -0
+
+	for (i = netdev->real_num_rx_queues; i > rxq; i--) {
+		pool = xsk_get_pool_from_qid(netdev, i - 1);
+		if (pool && pool->umem->zc) {
+			rxq = i;
+			break;
+		}
+	}
+
+	return netif_set_real_num_queues(netdev, adapter->num_tx_queues, rxq);

[Severity: Low]
Until the next open, can this leave the stack advertising Rx queues that have no ring behind them?

For example, say a zero-copy pool is bound to queue 2. A VF enable or an MSI-X to MSI fallback then lowers adapter->num_rx_queues to 1, but real_num_rx_queues stays at 3. Queue 1 has no ring, yet the stack still counts it as real.

In that window, a new AF_XDP bind to queue 1 passes the check in xsk_reg_pool_at_qid():

net/xdp/xsk.c:xsk_reg_pool_at_qid() {
    ...
	if (queue_id >= max(dev->real_num_rx_queues,
			    dev->real_num_tx_queues))
		return -EINVAL;
    ...
}

With XDP_COPY, xp_assign_dev() returns 0 straight away. With default flags, ndo_bpf(XDP_SETUP_XSK_POOL) reaches igb_xsk_pool_enable(), which rejects the queue:

drivers/net/ethernet/intel/igb/igb_xsk.c:igb_xsk_pool_enable() {
    ...
	if (qid >= adapter->num_rx_queues)
		return -EINVAL;
    ...
}

xp_assign_dev() then falls back to copy mode and returns 0.

So the bind succeeds, but the socket never gets any Rx traffic, because no ring has queue_index 1. On Tx, real_num_tx_queues follows the device, so __xsk_generic_xmit() skips the socket and returns no error:

net/xdp/xsk.c:__xsk_generic_xmit() {
    ...
	if (xs->queue_id >= xs->dev->real_num_tx_queues)
		goto out;
    ...
}

Before this patch, igb would have lowered real_num_rx_queues to 1 and the same bind would have failed with -EINVAL.

Is this window intended? Or should binds to queues at or above adapter->num_rx_queues still be refused while the count is held up?

Quoted 2 lines
drivers/net/ethernet/intel/igb/igb_main.c +2 -0
+}
+

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130213740.3323814-1-antonio%40mandelbit.com
#8 lore raw

Re: [PATCH net v6 4/7] igb: keep the stack's Rx queues that still have an XSK pool

Quoted 2 lines

Until the next open, can this leave the stack advertising Rx queues that have no ring behind them?

Yes, that's the trade-off: the alternative is a pool that can't be disabled anymore. A copy mode bind to such a queue in that window gets no traffic, as you say; I've left it like that.

#9 lore raw

PATCH v6 5/7 igb: link queues to NAPIs under RTNL after runtime resume

In reply to Antonio Quartulli

__igb_open() links queues to NAPIs with netif_queue_set_napi(), which requires RTNL, but RTNL isn't held when resuming from runtime suspend:
this triggers "RTNL: assertion failed".

Link them in __igb_open() only when RTNL is held, and from the work item that sets the real queue counts on runtime resume. The links can't be kept across a suspend, because __igb_shutdown() frees the q_vectors. The work item resumes the device and checks __IGB_DOWN under RTNL first, so the q_vectors exist when it links them.

Tested with the 82576 emulated by QEMU.

Fixes b75a1dea500f ("igb: Link queues to NAPI instances")

---
v6: don't cancel the work item in __igb_shutdown() anymore: it resumes the device first (patch 3), and a runtime suspend no longer waits for RTNL (patch 1), so cancelling it there could deadlock (Sashiko); dropped Janis's Reviewed-by
v5: cancel the work item before a suspend (Sashiko)
v4: no changes
v3: link the NAPIs from the work item added by the previous patch, and in __igb_open() whenever RTNL is held (also on system sleep resume); dropped Joe's Reviewed-by, as the patch changed
v2: no code changes; mention the tested hardware

Note: igc has the same issue: __igc_open() also links queues to NAPIs (igc_set_queue_napi()) on the RTNL-less runtime resume path. A fix will follow separately once it has been tested on igc hardware.

 drivers/net/ethernet/intel/igb/igb_main.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/igb/igb_main.c +13 -2
index 8b8e8c3..55352df 100644
@@ -4152,9 +4152,18 @@ static int igb_set_real_num_queues(struct igb_adapter *adapter)
 	return netif_set_real_num_queues(netdev, adapter->num_tx_queues, rxq);
 }
 
+static void igb_link_queue_napis(struct igb_adapter *adapter)
+{
+	int i;
+
+	for (i = 0; i < adapter->num_q_vectors; i++)
+		igb_set_queue_napi(adapter, i, &adapter->q_vector[i]->napi);
+}
+
 /* Some settings need RTNL, which isn't held when resuming from runtime
  * suspend: apply them from here instead, if the device is still up.
- * Resuming the device first waits for a resume or suspend in progress.
+ * Resuming the device first waits for a resume or suspend in progress, so
+ * that the q_vectors aren't being freed or allocated meanwhile.
  */
 static void igb_rtnl_resume_task(struct work_struct *work)
 {
@@ -4168,6 +4177,7 @@ static void igb_rtnl_resume_task(struct work_struct *work)
 		if (igb_set_real_num_queues(adapter))
 			netdev_err(adapter->netdev,
 				   "error setting real queue count\n");
+		igb_link_queue_napis(adapter);
 	}
 	pm_runtime_put(&adapter->pdev->dev);
 	rtnl_unlock();
@@ -4246,7 +4256,8 @@ static int __igb_open(struct net_device *netdev, bool resuming,
 	for (i = 0; i < adapter->num_q_vectors; i++) {
 		napi = &adapter->q_vector[i]->napi;
 		napi_enable(napi);
-		igb_set_queue_napi(adapter, i, napi);
+		if (rtnl_held)
+			igb_set_queue_napi(adapter, i, napi);
 	}
 
 	/* Clear any pending interrupts. */
Signature · 1 line
2.56.1
#10 lore raw

PATCH v6 6/7 igb: resume the device before changing XDP or XSK settings

In reply to Antonio Quartulli

igb_xdp() can run while the device is runtime suspended or resuming, and __igb_resume() rebuilds the queue counts and the rings without RTNL. igb_xdp_setup() and igb_xsk_pool_setup() can then see a queue count whose rings don't exist yet (NULL dereference), or a NAPI that isn't enabled yet (napi_disable() never returns). Changing the XDP program of a runtime suspended device also opened it twice.

Resume the device for the duration of igb_xdp(), as ethtool does. If resuming fails, the rings don't exist, or have no resources and their NAPIs were never enabled: don't touch them when disabling an XSK pool, refuse to enable one or to add an XDP program, and only update the software state when removing or replacing one, as failing that would leak the program.

Tested with the 82576 emulated by QEMU by closing a zero-copy AF_XDP socket and attaching an XDP program while the device is runtime suspended; the latter no longer warns in __netif_napi_del_locked() because of the double open. The races themselves weren't reproduced.

Fixes 9cbc948b5a20 ("igb: add XDP support")
Fixes 80f6ccf9f116 ("igb: Introduce XSK data structures and helpers")

---
v6: handle a failed resume: skip rings without resources, refuse to enable a pool or add an XDP program, and only update the software state when removing or replacing one (Sashiko); dropped Janis's Reviewed-by
v5: new patch (Sashiko)

 drivers/net/ethernet/intel/igb/igb_main.c | 31 ++++++++++++++++++++---
 drivers/net/ethernet/intel/igb/igb_xsk.c  |  8 ++++--
 2 files changed, 33 insertions(+), 6 deletions(-)
drivers/net/ethernet/intel/igb/igb_main.c +27 -4
index 55352df..e5a2870 100644
@@ -2909,6 +2909,16 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
 		}
 	}
 
+	/* After a failed resume the device is detached and its rings have no
+	 * resources: only update the software state, and don't add a program
+	 */
+	if (running && !netif_device_present(dev) &&
+	    (!adapter->num_rx_queues || !adapter->rx_ring[0]->desc)) {
+		if (prog && !adapter->xdp_prog)
+			return -ENODEV;
+		running = false;
+	}
+
 	old_prog = xchg(&adapter->xdp_prog, prog);
 	need_reset = (!!prog != !!old_prog);
 
@@ -2943,16 +2953,29 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
 static int igb_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 {
 	struct igb_adapter *adapter = netdev_priv(dev);
+	int err;
+
+	/* The rings only exist while the device is resumed, and resuming
+	 * rebuilds them without RTNL. If resuming fails, igb_xdp_setup() and
+	 * igb_xsk_pool_setup() find the device down.
+	 */
+	pm_runtime_get_sync(&adapter->pdev->dev);
 
 	switch (xdp->command) {
 	case XDP_SETUP_PROG:
-		return igb_xdp_setup(dev, xdp);
+		err = igb_xdp_setup(dev, xdp);
+		break;
 	case XDP_SETUP_XSK_POOL:
-		return igb_xsk_pool_setup(adapter, xdp->xsk.pool,
-					  xdp->xsk.queue_id);
+		err = igb_xsk_pool_setup(adapter, xdp->xsk.pool,
+					 xdp->xsk.queue_id);
+		break;
 	default:
-		return -EINVAL;
+		err = -EINVAL;
 	}
+
+	pm_runtime_put(&adapter->pdev->dev);
+
+	return err;
 }
 
 int igb_xdp_xmit_back(struct igb_adapter *adapter, struct xdp_buff *xdp)
drivers/net/ethernet/intel/igb/igb_xsk.c +6 -2
index b6234e3..e70f818 100644
@@ -120,6 +120,10 @@ static int igb_xsk_pool_enable(struct igb_adapter *adapter,
 	    qid >= netdev->real_num_tx_queues)
 		return -EINVAL;
 
+	/* After a failed resume the rings have no resources */
+	if (netif_running(netdev) && !adapter->rx_ring[qid]->desc)
+		return -ENODEV;
+
 	err = xsk_pool_dma_map(pool, &adapter->pdev->dev, IGB_RX_DMA_ATTR);
 	if (err)
 		return err;
@@ -160,11 +164,11 @@ static int igb_xsk_pool_disable(struct igb_adapter *adapter, u16 qid)
 	rx_ring = adapter->rx_ring[qid];
 	/* The stack can know more queues than the device has, e.g. while
 	 * runtime suspended, which frees the rings: skip the rings it
-	 * doesn't have.
+	 * doesn't have, or that have no resources after a failed resume.
 	 */
 	if_running = netif_running(adapter->netdev) &&
 		     igb_xdp_is_enabled(adapter) &&
-		     qid < adapter->num_rx_queues;
+		     qid < adapter->num_rx_queues && rx_ring->desc;
 	if (if_running)
 		igb_txrx_ring_disable(adapter, qid);
 
Signature · 1 line
2.56.1
#11 lore raw

Re: [PATCH net v6 6/7] igb: resume the device before changing XDP or XSK settings

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] igb: broken failed-resume and 3K-buffer checks in igb_xdp_setup() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] igb: broken failed-resume and 3K-buffer checks in igb_xdp_setup()
--

commit 7c39c9b9bbe9f71a6d6b0fe773ef6680942f2a1f
Author: Antonio Quartulli <[email protected]>

igb: resume the device before changing XDP or XSK settings

This commit resumes the device across igb_xdp() via pm_runtime_get_sync() and handles failed resumes in igb_xdp_setup() and the XSK pool setup paths.

Quoted 10 lines
drivers/net/ethernet/intel/igb/igb_main.c +2 -0
index 11c136d7bea75..79652ccbfdf00 100644
@@ -2909,6 +2909,16 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
 		}
 	}
 
+	/* After a failed resume the device is detached and its rings have no
+	 * resources: only update the software state, and don't add a program

drivers/net/ethernet/intel/igb/igb_main.c static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) · line 2923

Quoted 10 lines
drivers/net/ethernet/intel/igb/igb_main.c +8 -0
+	 */
+	if (running && !netif_device_present(dev) &&
+	    (!adapter->num_rx_queues || !adapter->rx_ring[0]->desc)) {
+		if (prog && !adapter->xdp_prog)
+			return -ENODEV;
+		running = false;
+	}
+
 	old_prog = xchg(&adapter->xdp_prog, prog);
 	need_reset = (!!prog != !!old_prog);

[Severity: Medium]
Can this failed-resume check miss detached devices when !running, or be blocked from removing an XDP program by the earlier igb_rx_bufsz() check?

__igb_shutdown() detaches the netdev and calls igb_clear_interrupt_scheme(), which frees all rings and zeroes adapter->num_rx_queues even when the interface is down. If resuming fails while !running (or the interface is brought down after a failed resume), gating this check on running lets a new XDP program be attached on a detached device and skips the frame_size > igb_rx_bufsz(ring) loop completely when adapter->num_rx_queues is 0. igb_xsk_pool_enable() similarly gates its !adapter->rx_ring[qid]->desc check on netif_running(netdev) instead of !netif_device_present(netdev).

Conversely, IGB_RING_FLAG_RX_3K_BUFFER is only set on rx_ring during igb_configure_rx() in __igb_open(), not when igb_init_interrupt_scheme() allocates the rings:

drivers/net/ethernet/intel/igb/igb_main.c:igb_set_rx_buffer_len() {
    ...
	if (adapter->max_frame_size > IGB_MAX_FRAME_BUILD_SKB ||
	    IGB_2K_TOO_SMALL_WITH_PADDING ||
	    rd32(E1000_RCTL) & E1000_RCTL_SBP)
		set_ring_uses_large_buffer(rx_ring);
    ...
}

If an XDP program was attached with a frame_size requiring 3K buffers (2048 < frame_size <= 3072) and __igb_resume() later allocates fresh rings in igb_init_interrupt_scheme() without reaching igb_configure_rx() (because __igb_open() fails when allocating ring resources, or the interface is down), igb_rx_bufsz(ring) returns 2048 and the loop above returns -EINVAL before reaching the failed-resume check, preventing removal or replacement of the program and leaking old_prog.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#12 lore raw

Re: [PATCH net v6 6/7] igb: resume the device before changing XDP or XSK settings

In reply to Antonio Quartulli
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

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

Critical: 0 · High: 1 (2) · Medium: 3 · 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 uses desc != NULL (plus !netif_device_present() in igb_xdp_setup) to decide that the queues are live and the NAPIs…
- [Medium] The commit message names the root cause as '__igb_resume() rebuilds the queue counts and the rings without RTNL', but the fix…
- [Medium] The patch deliberately ignores the return value of pm_runtime_get_sync() in igb_xdp().
- [Medium] igb: broken failed-resume and 3K-buffer checks in igb_xdp_setup()

Pre-existing issues:
- [High] This bug predates the patch, but the patch fixes the same bug class for igb_xdp() only.
- [High] This bug predates the patch, but the new guard does not cover it.

--

igb: resume the device before changing XDP or XSK settings

This commit holds a runtime PM reference for the duration of igb_xdp(), so the rings exist while XDP and XSK settings are changed. It also adds checks to igb_xdp_setup(), igb_xsk_pool_enable() and igb_xsk_pool_disable(). These checks leave alone rings that have no resources after a failed resume.

Quoted 7 lines
drivers/net/ethernet/intel/igb/igb_main.c +0 -0
index 11c136d7bea75..79652ccbfdf00 100644
@@ -2909,6 +2909,16 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
 		}
 	}

drivers/net/ethernet/intel/igb/igb_main.c static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) · line 2920

Quoted 10 lines
drivers/net/ethernet/intel/igb/igb_main.c +9 -0
 
+	/* After a failed resume the device is detached and its rings have no
+	 * resources: only update the software state, and don't add a program
+	 */
+	if (running && !netif_device_present(dev) &&
+	    (!adapter->num_rx_queues || !adapter->rx_ring[0]->desc)) {
+		if (prog && !adapter->xdp_prog)
+			return -ENODEV;
+		running = false;
+	}

[Severity: Medium]
Is this check in the right place, and is running the right condition to gate it on?

It only runs when netif_running(dev) is true. __igb_shutdown() calls igb_clear_interrupt_scheme() even when the interface is down, so if the interface is down when __igb_resume() fails, adapter->num_rx_queues is still 0. The frame size loop above then runs zero times and this check is skipped. A new program therefore gets attached to a detached device without any MTU validation.

The opposite case is also possible. IGB_RING_FLAG_RX_3K_BUFFER is only set by igb_set_rx_buffer_len() from igb_configure_rx(). It is not set when igb_init_interrupt_scheme() allocates the rings. __igb_resume() reallocates the rings, but it doesn't reach igb_configure() when the interface is down. It also doesn't get there when __igb_open() fails in igb_setup_all_tx_resources() or igb_setup_all_rx_resources(). On PAGE_SIZE < 8192, igb_rx_bufsz() then returns 2048 instead of 3072.

With a program attached and an MTU that needs 3K buffers, the frame size loop then returns -EINVAL before this check is reached, even when prog is NULL. Removing or replacing the program fails. dev_xdp_uninstall() ignores that error at unregister time, so the reference held in adapter->xdp_prog is never dropped. Isn't that the leak this check is meant to avoid?

The down case can now be hit after a successful resume too. igb_xdp() resumes the device, so the loop no longer sees num_rx_queues == 0.

igb_xsk_pool_enable() has a similar gap. Its new check only refuses when netif_running(netdev) is true. If the interface is closed after __igb_open() failed during resume, a pool can still be enabled on a detached device whose rings have no resources.

Would it work to do the failed-resume check before the frame size loop, base it on netif_device_present() rather than running, and only validate the ring buffer sizes when a program is being added? Or should the loop compute the buffer size the way igb_set_rx_buffer_len() does, instead of reading ring flags that only igb_configure_rx() sets?

[Severity: High]

Does this guard cover a device that PCI error recovery has detached? igb_io_error_detected() detaches the device and calls igb_down(), but it leaves the rings and IRQs allocated:

igb_io_error_detected() {
	...
	netif_device_detach(netdev);

	if (state == pci_channel_io_perm_failure)
		return PCI_ERS_RESULT_DISCONNECT;

	rtnl_lock();
	if (netif_running(netdev))
		igb_down(adapter);
	rtnl_unlock();

	pci_disable_device(pdev);
	...
}

In that state the device is RPM_ACTIVE, so pm_runtime_get_sync() returns immediately. rx_ring[0]->desc is non-NULL, so running stays true. When a program is added or removed, igb_close() does nothing because the device isn't present:

int igb_close(struct net_device *netdev)

{
	if (netif_device_present(netdev) || netdev->dismantle)
		return __igb_close(netdev, false);
	return 0;
}

igb_open() then runs __igb_open() a second time on live rings. Isn't this the same double open the commit message describes?

The second open overwrites and leaks the descriptor DMA memory and buffer_info. With MSI-X, igb_request_msix() fails because the vectors are still requested. The fallback in igb_request_irq() then calls igb_clear_interrupt_scheme(). That frees q_vectors that the registered ring IRQ handlers still use as dev_id.

The error path leaves tx_buffer_info and desc NULL and ring->dma stale, and igb_xdp_setup() still returns 0. Later, igb_io_resume()->igb_up()->igb_configure_tx_ring() writes the stale dma into TDBAL/TDBAH and calls memset() on the NULL tx_buffer_info with RTNL held.

The new XSK checks have the same gap. In igb_xsk_pool_disable(), and in igb_xsk_pool_enable() when XDP is enabled, the desc test passes:

	if_running = netif_running(adapter->netdev) &&
		     igb_xdp_is_enabled(adapter) &&
		     qid < adapter->num_rx_queues && rx_ring->desc;

igb_txrx_ring_disable() then calls napi_disable() on a NAPI that igb_down() already disabled. Wouldn't that wait forever with RTNL held?

igb_down() sets __IGB_DOWN on both the failed-resume path and the AER path. Would testing __IGB_DOWN cover this case? Always skipping the close/open pair on a detached device might also work.

[Severity: High]

This is a pre-existing issue, but this guard also requires !netif_device_present(). Because of that, it doesn't catch the state left when the igb_open() call at the end of igb_xdp_setup() fails:

	if (running)
		igb_open(dev);

	return 0;

__igb_open() can fail in igb_setup_all_tx_resources(), igb_setup_all_rx_resources() or igb_request_irq(). Its error path frees the rings, so desc becomes NULL, and it returns before napi_enable(). The interface stays IFF_UP and present, and the NAPIs stay disabled from the earlier igb_down().

On the next program add or remove, running and need_reset are both true, and igb_close()->__igb_close()->igb_down() calls napi_disable() on the already disabled NAPIs. Can napi_disable_locked() then loop forever on NAPIF_STATE_SCHED with RTNL held? "ip link set down" looks like it would hang the same way.

The new XSK checks treat netif_running() && !desc as "no resources" no matter whether the device is present. Should igb_xdp_setup() match them? Should the igb_open() error also be returned to the caller?

Quoted 3 lines
drivers/net/ethernet/intel/igb/igb_main.c +1 -0
+
 	old_prog = xchg(&adapter->xdp_prog, prog);
 	need_reset = (!!prog != !!old_prog);

[ ... ]

drivers/net/ethernet/intel/igb/igb_main.c static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) · line 2962

Quoted 11 lines
drivers/net/ethernet/intel/igb/igb_main.c +7 -0
@@ -2943,16 +2953,29 @@ static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
 static int igb_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 {
 	struct igb_adapter *adapter = netdev_priv(dev);
+	int err;
+
+	/* The rings only exist while the device is resumed, and resuming
+	 * rebuilds them without RTNL. If resuming fails, igb_xdp_setup() and
+	 * igb_xsk_pool_setup() find the device down.
+	 */
+	pm_runtime_get_sync(&adapter->pdev->dev);

[Severity: Medium]

The commit message gives the cause as "__igb_resume() rebuilds the queue counts and the rings without RTNL". Does pm_runtime_get_sync() also serialise against system resume?

On system resume, igb_resume()->__igb_resume(dev, false) still rebuilds the rings before it takes RTNL:

	if (igb_init_interrupt_scheme(adapter, true)) {
	...
	if (!rpm)
		rtnl_lock();

igb_set_interrupt_capability() sets adapter->num_rx_queues before igb_alloc_q_vectors() fills adapter->rx_ring[]. The device is RPM_ACTIVE at that point, so pm_runtime_get_sync() returns 1 immediately.

Kernel contexts that aren't frozen can still call ndo_bpf under RTNL in that window. One example is xp_release_deferred() on system_wq, which does rtnl_lock() and then xp_clear_dev().

Could such a caller see num_rx_queues > 0 while rx_ring[qid] is still NULL? It would then dereference NULL through rx_ring->desc in igb_xsk_pool_disable(), or through igb_rx_bufsz(ring) and adapter->rx_ring[0]->desc in igb_xdp_setup().

[Severity: Medium]

The return value of pm_runtime_get_sync() is ignored because, after a failed resume, the callees "find the device down". Does that state stay the same for the rest of igb_xdp()?

__igb_resume() can fail with -EBUSY or -EAGAIN, for example from the shared legacy request_irq() fallback in igb_request_irq(). rpm_callback() treats those errors as transient, and the device goes back to RPM_SUSPENDED.

The usage count held by igb_xdp() doesn't stop another rpm_resume() from a context that doesn't take RTNL, such as writing "on" to power/control or igb_rpm_retry_task().

That resume runs igb_runtime_resume()->__igb_resume(dev, true) without RTNL. It re-runs igb_init_interrupt_scheme(), allocates desc and enables NAPI. Meanwhile igb_xdp_setup() or igb_xsk_pool_*() read num_rx_queues, rx_ring[] and desc without a lock.

Could the "no resources, don't touch NAPI" decision go stale here? Ring memory could also be reinitialised under the reader, or program updates could land on rings that are being replaced.

[Severity: High]

This isn't a bug introduced by this patch, but igb_xdp() is the only ndo that this patch resumes. Do igb_change_mtu() and igb_set_features()->igb_reinit_locked()/igb_reset() have the same problem on a runtime suspended device?

While the device is runtime suspended, igb_free_q_vectors() has set num_rx_queues, num_tx_queues and num_q_vectors to 0 and cleared adapter->q_vector[]. netif_running() is still true. In igb_change_mtu():

	if (igb_xdp_is_enabled(adapter)) {
		int i;

		for (i = 0; i < adapter->num_rx_queues; i++) {
	...
	if (netif_running(netdev))
		igb_down(adapter);
	...
	if (netif_running(netdev))
		igb_up(adapter);

The XDP frame size loop runs zero times, so igb_change_mtu() accepts an MTU that igb_xdp_setup() would reject.

Without MSI-X, igb_up() calls igb_assign_vector(adapter->q_vector[0], 0) with a NULL q_vector. Is that a NULL dereference with RTNL held?

With MSI-X, igb_up() clears __IGB_DOWN while num_tx_queues is 0. igb_xdp_xmit() only checks __IGB_DOWN before it calls igb_xdp_tx_queue_mapping():

	if (r_idx >= adapter->num_tx_queues)
		r_idx = r_idx % adapter->num_tx_queues;

Can an XDP_REDIRECT into igb then divide by zero?

igb_down() also reads the registers of a device in D3. If those reads return all-ones, igb_rd32() sets hw->hw_addr to NULL and warns. __igb_resume() never restores hw_addr, so the NIC looks like it stays unusable until the driver is reloaded.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130213740.3323814-1-antonio%40mandelbit.com
#13 lore raw

Re: [PATCH net v6 6/7] igb: resume the device before changing XDP or XSK settings

Quoted 2 lines

Is this check in the right place, and is running the right condition to gate it on?

Right, reproduced: removing a program that needs 3K buffers fails after a resume with the interface down. In v7 the buffer size is only checked when adding or replacing a program, and a detached device only gets the software state updated. Adding a program is still checked against rings that a resume has just allocated, i.e. against 2K buffers, like on a device that has never been opened; computing the size from the MTU as igb_set_rx_buffer_len() does can follow separately.

Quoted 1 line

Does this guard cover a device that PCI error recovery has detached?

The double open is fixed in v7 by the same change. The XSK state during PCI error recovery is pre-existing and will be fixed separately, with the other XSK queue issues.

Quoted 3 lines

This is a pre-existing issue, but this guard also requires !netif_device_present(). Because of that, it doesn't catch the state left when the igb_open() call at the end of igb_xdp_setup() fails:

Pre-existing and not specific to resume; it needs the program change to be undone as well, so it will be fixed separately.

Quoted 3 lines

The commit message gives the cause as "__igb_resume() rebuilds the queue counts and the rings without RTNL". Does pm_runtime_get_sync() also serialise against system resume?

No. The caller you name runs on system_freezable_wq since fece8420e76f ("xsk: freeze deferred pool teardown without blocking unregister"); the system resume path rebuilding the rings without RTNL is pre-existing and not addressed by this series.

Quoted 3 lines

The return value of pm_runtime_get_sync() is ignored because, after a failed resume, the callees "find the device down". Does that state stay the same for the rest of igb_xdp()?

It needs a resume that fails with a transient error, followed by another resume that doesn't hold RTNL while igb_xdp() runs. I've left it, as closing it would mean taking a lock in the resume path, which is what ac8c58f5b535 removed.

Quoted 4 lines

This isn't a bug introduced by this patch, but igb_xdp() is the only ndo that this patch resumes. Do igb_change_mtu() and igb_set_features()->igb_reinit_locked()/igb_reset() have the same problem on a runtime suspended device?

igb detaches the netdev while runtime suspended, and dev_set_mtu() refuses a detached device with -ENODEV before calling ndo_change_mtu (checked: "ip link set mtu" fails while suspended). igb_set_features() only acts on changes of NTUPLE, RXALL and VLAN Rx stripping, which only come through ethtool, and ethtool resumes the device first.

pw-bot: cr

#14 lore raw

PATCH v6 7/7 igb: only allow runtime PM if the device can wake up

In reply to Antonio Quartulli

igb lets the device be runtime suspended even if it can't signal a wake-up from D3. Such a device doesn't notice the link coming back, so the port stays down until it is resumed from user space.

Only schedule a runtime suspend if pci_dev_run_wake() is true. Devices that can't wake up no longer runtime suspend, even with power/control set to auto. Checking it when scheduling the suspend, rather than at probe and remove as e1000e and r8169 do, keeps the runtime PM usage count balanced if the result changes while the driver is bound.

Tested with the 82576 emulated by QEMU.

---
v6: no changes
v5: check pci_dev_run_wake() when scheduling the suspend rather than at probe and remove, which keeps the usage count balanced (Sashiko)
v4: no changes
v3: no changes
v2: no code changes; mention the tested hardware

Note: igc has the same issue: igc_probe() also allows runtime PM regardless of pci_dev_run_wake().

 drivers/net/ethernet/intel/igb/igb_main.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/ethernet/intel/igb/igb_main.c +11 -3
index e5a2870..d8f8df8 100644
@@ -5643,6 +5643,15 @@ static void igb_watchdog(struct timer_list *t)
 	schedule_work(&adapter->watchdog_task);
 }
 
+/* A device that can't wake up wouldn't notice the link coming back while
+ * runtime suspended
+ */
+static void igb_schedule_runtime_suspend(struct igb_adapter *adapter)
+{
+	if (pci_dev_run_wake(adapter->pdev))
+		pm_schedule_suspend(&adapter->pdev->dev, MSEC_PER_SEC * 5);
+}
+
 static void igb_watchdog_task(struct work_struct *work)
 {
 	struct igb_adapter *adapter = container_of(work,
@@ -5795,8 +5804,7 @@ no_wait:
 					return;
 				}
 			}
-			pm_schedule_suspend(netdev->dev.parent,
-					    MSEC_PER_SEC * 5);
+			igb_schedule_runtime_suspend(adapter);
 
 		/* also check for alternate media here */
 		} else if (!netif_carrier_ok(netdev) &&
@@ -9765,7 +9773,7 @@ static int igb_runtime_idle(struct device *dev)
 	struct igb_adapter *adapter = netdev_priv(netdev);
 
 	if (!igb_has_link(adapter))
-		pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
+		igb_schedule_runtime_suspend(adapter);
 
 	return -EBUSY;
 }
Signature · 1 line
2.56.1
#15 lore raw

Re: [PATCH net v6 7/7] igb: only allow runtime PM if the device can wake up

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] Two existing comments now leave out the wake-capability condition this patch added.

--

igb: only allow runtime PM if the device can wake up

This patch adds igb_schedule_runtime_suspend(), which only calls pm_schedule_suspend() when pci_dev_run_wake() is true. The watchdog task and igb_runtime_idle() now use it. A device that can't signal a wake-up from D3 is no longer runtime suspended.

Quoted 17 lines
drivers/net/ethernet/intel/igb/igb_main.c +9 -0
index 79652ccbfdf00..bb1ef4320fb69 100644
@@ -5642,6 +5642,15 @@ static void igb_watchdog(struct timer_list *t)
 	schedule_work(&adapter->watchdog_task);
 }
 
+/* A device that can't wake up wouldn't notice the link coming back while
+ * runtime suspended
+ */
+static void igb_schedule_runtime_suspend(struct igb_adapter *adapter)
+{
+	if (pci_dev_run_wake(adapter->pdev))
+		pm_schedule_suspend(&adapter->pdev->dev, MSEC_PER_SEC * 5);
+}
+

[ ... ]

drivers/net/ethernet/intel/igb/igb_main.c static int igb_runtime_idle(struct device *dev) · line 9778

Quoted 9 lines
drivers/net/ethernet/intel/igb/igb_main.c +1 -1
@@ -9764,7 +9772,7 @@ static int igb_runtime_idle(struct device *dev)
 	struct igb_adapter *adapter = netdev_priv(netdev);
 
 	if (!igb_has_link(adapter))
-		pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
+		igb_schedule_runtime_suspend(adapter);
 
 	return -EBUSY;
 }

[Severity: Low]
This isn't a bug, but two existing comments that describe this path are now out of date. They don't mention the new pci_dev_run_wake() check.

The comment above igb_rpm_retry_task() says:

    /* Resuming and releasing the device runs igb_runtime_idle(), which schedules
     * a new suspend if the link is still down
     */

And the comment in __igb_shutdown() says:

* Only try to take it: -EBUSY keeps the device active, and
* igb_rpm_retry_task() schedules a new suspend. */

With this patch, the retry path goes through igb_rpm_retry_task()->pm_runtime_put()->igb_runtime_idle()-> igb_schedule_runtime_suspend(). That path no longer schedules a suspend when pci_dev_run_wake() has become false since the original suspend was scheduled. The commit message explicitly considers that case.

Could these two comments be updated to say the new suspend is only scheduled if the device can still wake up?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130213740.3323814-1-antonio%40mandelbit.com