Skip to contentSkip to conversation

r8169: fix RTNL deadlock in runtime suspend

PATCH New Stefano Munari 2 participants 2 messages Nov 18 – Nov 21

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

PATCH 1/1 r8169: fix RTNL deadlock in runtime suspend

rtl8169_runtime_suspend() and the paths that resume the device under RTNL wait for each other in opposite order:

- The runtime suspend callback runs with the device in RPM_SUSPENDING and then calls rtnl_lock(): it owns the suspend in progress and waits for RTNL.

- rtl8169_close() and the ethtool ioctl/netlink paths run with RTNL held and then call pm_runtime_get_sync(), which sleeps uninterruptibly in rpm_resume() until the device leaves
RPM_SUSPENDING: they own RTNL and wait for the suspend to finish.

If the suspend starts after the other task has taken RTNL but before it reaches pm_runtime_get_sync(), neither can make progress:

ip link set down / ethtool pm workqueue
-------------------------- ------------
rtnl_lock()
rpm_suspend()
status = RPM_SUSPENDING rtl8169_runtime_suspend()
pm_runtime_get_sync()
rpm_resume()
wait for !RPM_SUSPENDING
rtnl_lock()

RTNL is never released, so every later RTNL user blocks behind it.

The suspend is the delayed one scheduled by rtl8169_runtime_idle():
the interface is up w/out link and runtime PM is enabled (power/control = auto).

Blocked tasks, "ip link set down":

task:kworker/u66:4 state:D

  Workqueue: pm pm_runtime_work
   __mutex_lock
   rtl8169_runtime_suspend+0x1a/0x70 [r8169]
   pci_pm_runtime_suspend+0x78/0x230
   __rpm_callback+0x56/0x1e0
   rpm_callback+0x30/0x80
   rpm_suspend+0xe7/0x630
   pm_runtime_work+0x57/0xb0

task:python3 state:D

   rpm_resume+0x1d0/0x750
   __pm_runtime_resume+0x43/0x60
   rtl8169_close+0x32/0x100 [r8169]
   __dev_close_many+0xaf/0x1b0
   __dev_change_flags+0xe2/0x1f0
   netif_change_flags+0x27/0x70
   do_setlink.isra.0+0x367/0xe70
   rtnl_newlink+0x4c6/0xab0

Use rtnl_trylock() and return -EBUSY if RTNL is contended. The PM core then sets the device back to RPM_ACTIVE and wakes up the waiters in rpm_resume(), without recording a runtime PM error. In the deadlock scenario the RTNL holder is the task holding runtime PM reference, so the suspend is rescheduled by rtl8169_runtime_idle() when it drops its usage count. If RTNL is held by an unrelated task, the device stays active until the next idle check (usage count drop or link down).

This issue was found using an AI code review agent.

Tested on RTL8168gu/8111gu (XID 509), net tree 7.4.0-rc2.
Setup: interface up, cable unplugged, power/control = auto.

It was reproduced with the unpatched driver by timing
"ip link set down" to the expiry of the 10s runtime suspend timer. It was also reproduced deterministically with a debug-only msleep() before rtnl_lock() in rtl8169_runtime_suspend() to widen the race window and "ethtool -i" issued during the sleep.

With the patch the timed reproducer completed 100 attempts, hitting the race window in 12 of them, without deadlock.
The deterministic msleep() variant no longer hangs. Kernel selftests were not run.

Fixes abe5fc42f9ce ("r8169: use RTNL to protect critical sections")

---
Race window (RTNL held before rtl8169_close() reaches pm_runtime_get_sync()) on the unpatched driver, measured with kprobes over 200 x "ip link set down":

idle: median ~175 us, 95% below 250 us, max 3.1 ms
moderate load: median ~165 us, 95% below 2.1 ms, max 6.0 ms

---
 drivers/net/ethernet/realtek/r8169_main.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/net/ethernet/realtek/r8169_main.c +8 -1
index f151ab681393..c60d87015eb9 100644
@@ -5975,7 +5975,14 @@ static int rtl8169_runtime_suspend(struct device *device)
 		return 0;
 	}
 
-	rtnl_lock();
+	/*
+	 * Do not block on RTNL.
+	 * Other callers holding RTNL may wait for suspend to complete
+	 * in pm_runtime_get_sync(), which would deadlock.
+	 */
+	if (!rtnl_trylock())
+		return -EBUSY;
+
 	__rtl8169_set_wol(tp, WAKE_PHY);
 	rtl8169_net_suspend(tp);
 	rtnl_unlock();
Signature · 1 line
2.55.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: 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] The patch makes rtl8169_runtime_suspend() (drivers/net/ethernet/realtek/r8169_main.c) return -EBUSY whenever…

--

r8169: fix RTNL deadlock in runtime suspend

This patch replaces rtnl_lock() in rtl8169_runtime_suspend() with rtnl_trylock(), and returns -EBUSY when RTNL is contended. The goal is to avoid an ABBA deadlock. The runtime suspend callback keeps the device in RPM_SUSPENDING while it waits for RTNL. Meanwhile, RTNL holders such as rtl8169_close() and the ethtool paths wait in pm_runtime_get_sync().

Quoted 6 lines
drivers/net/ethernet/realtek/r8169_main.c +0 -0
index f151ab6813932..c60d87015eb94 100644
@@ -5975,7 +5975,14 @@ static int rtl8169_runtime_suspend(struct device *device)
 		return 0;

drivers/net/ethernet/realtek/r8169_main.c static int rtl8169_runtime_suspend(struct device *device) · line 5984

Quoted 10 lines
drivers/net/ethernet/realtek/r8169_main.c +7 -1
 	}
 
-	rtnl_lock();
+	/*
+	 * Do not block on RTNL.
+	 * Other callers holding RTNL may wait for suspend to complete
+	 * in pm_runtime_get_sync(), which would deadlock.
+	 */
+	if (!rtnl_trylock())
+		return -EBUSY;

[Severity: Medium]
Once this returns -EBUSY, does anything schedule another runtime suspend attempt?

r8169 arms the suspend from rtl8169_runtime_idle() with a plain timer request, not with autosuspend:

drivers/net/ethernet/realtek/r8169_main.c:rtl8169_runtime_idle() {
    ...
	if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
		pm_schedule_suspend(device, 10000);
    ...
}

pm_schedule_suspend() leaves timer_autosuspends at 0. As a result, pm_suspend_timer_fn() calls rpm_suspend() with RPM_ASYNC but without RPM_AUTO. The driver also never calls pm_runtime_use_autosuspend() or pm_runtime_mark_last_busy().

On -EBUSY, the fail path of rpm_suspend() only retries for autosuspend:

drivers/base/power/runtime.c:rpm_suspend() {
    ...
	if (!dev->power.runtime_error && (rpmflags & RPM_AUTO) &&
	    pm_runtime_autosuspend_expiration(dev) != 0)
		goto repeat;

	pm_runtime_cancel_pending(dev);
    ...
}

So the pending request and the suspend timer are both cancelled.

The comment in pci_pm_runtime_suspend() says "-EBUSY and -EAGAIN is used to request the runtime PM core to schedule a new suspend". The core doesn't do that for non-autosuspend requests, though.

After that, the suspend is only re-armed if rtl8169_runtime_idle() runs again. That happens on:

- open/close
- the LED helpers, which use pm_runtime_put_sync()
- the ethtool core's pm_runtime_put() in net/ethtool/ioctl.c and net/ethtool/netlink.c
- rtl_mac_link_down(), which calls pm_runtime_idle()

rtl8169_get_stats64() uses pm_runtime_put_noidle(), so stats reads don't re-arm it.

The patch targets a setup where the interface is up with no carrier and power/control is set to auto. In that setup the link-down event has already happened.

Suppose an unrelated task holds RTNL when the 10s timer fires. Examples are a netlink or ethtool request for another NIC, a netdev notifier, or an ip -s link dump. Can the NIC then stay in D0 indefinitely, without WAKE_PHY armed? Before this patch, rtnl_lock() waited, so the suspend always completed.

The commit message says:

If RTNL is held by an unrelated task, the device stays active until the next idle check (usage count drop or link down).

Is a next idle check actually guaranteed in this setup?

This also looks like the "trylock with fallback to skip the work entirely" pattern. That pattern is only acceptable when the failure path reschedules the work.

Would one of these approaches work instead?

- Convert the driver to autosuspend, using pm_runtime_use_autosuspend() plus pm_runtime_mark_last_busy() before returning -EBUSY, so the PM core retries.

- Re-arm from the driver after the callback returns, for example with a work item that calls pm_runtime_idle() or pm_schedule_suspend(). Re-arming from inside the callback won't work, because pm_runtime_cancel_pending() in the fail path cancels it.

- Restructure the suspend path so it doesn't need RTNL.

Quoted 3 lines
drivers/net/ethernet/realtek/r8169_main.c +0 -0
 	__rtl8169_set_wol(tp, WAKE_PHY);
 	rtl8169_net_suspend(tp);
 	rtnl_unlock();
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118000631.17771-1-stefanomunari.sm%40gmail.com