If an r8152 Ethernet adapter is plugged or unplugged as a device goes to sleep, the suspend attempt and every task waiting on RTNL can deadlock, leaving the device unable to wake up. In commit 5ee3c60c8d3b ("r8152: fix the wake event"), register_pm_notifier() and unregister_pm_notifier() were added to rtl8152_open() and rtl8152_close(), the .ndo_open and .ndo_stop handlers, which run under RTNL and take the PM notifier chain rwsem (pm_chain_head.rwsem) for writing. During suspend or hibernation, the PM core holds that rwsem for reading while calling wg_pm_notification() (registered in wg_device_init()), which takes rtnl_lock() unless CONFIG_PM_AUTOSLEEP or CONFIG_PM_USERSPACE_AUTOSLEEP is enabled: Task A (open or close) Task B (suspend or hibernate) rtnl_lock() down_read(&pm_chain_head.rwsem) rtl8152_open() / rtl8152_close() wg_pm_notification() (un)register_pm_notifier() rtnl_lock() down_write(&pm_chain_head.rwsem) Register the notifier in rtl8152_probe_once(), and unregister it in rtl8152_disconnect() and in the probe error path, outside RTNL. rtl8152_close() previously relied on unregister_pm_notifier() to wait for a runtime resume started by rtl_notifier(). Without that barrier, a concurrent runtime resume can race with the teardown in rtl8152_close() once it passes the netif_running() check in rtl8152_runtime_resume(): Task A (close) Task B (runtime resume) __dev_close_many() rtl8152_runtime_resume() netif_running() == true clear __LINK_STATE_START rtl8152_close() autosuspend_en(tp, false) clear WORK_ENABLE / kill URB napi_disable(&tp->napi) napi_disable(&tp->napi) /* spins */ usb_autopm_get_interface() /* waits for Task B */ If Task A disables NAPI first, Task B spins forever in napi_disable() while Task A blocks in usb_autopm_get_interface() waiting for the resume to finish under RTNL. Conversely, if Task B runs set_bit(WORK_ENABLE) and usb_submit_urb(tp->intr_urb) after Task A's usb_kill_urb(), Task A later frees tp->intr_buff in free_all_mem() while the interrupt URB is still active. Call usb_autopm_get_interface() at the start of rtl8152_close() instead of in the middle of it, as usbnet_stop() does, so that any runtime resume or suspend in progress completes before teardown begins and none starts until the reference is dropped. Count the runtime PM references that rtl_notifier() takes, drop one at a post event only if one is held, and drop what is left right after unregister_pm_notifier(). Otherwise an adapter probed or removed between the prepare and post events would drop a reference it never took or never drop one it took. It is a counter rather than a flag because PM_SUSPEND_PREPARE can arrive between PM_HIBERNATION_PREPARE and PM_POST_HIBERNATION while /dev/snapshot is open. System suspend now also resumes an autosuspended adapter whose interface is down, which rtl8152_runtime_resume() handles. Fixes: 5ee3c60c8d3b ("r8152: fix the wake event") Signed-off-by: Hsin-Te Yuan --- The deadlock was hit during suspend-to-idle testing on arm64 devices running a 6.12-based kernel, when unplugging a USB-C monitor whose hub carries an r8152 Ethernet adapter (RTL8153) left the device unable to wake up. v2 was tested on the same 6.12-based arm64 hardware with an RTL8153 without the patch and passed with v2, along with driver unbind/bind, closing an autosuspended interface during suspend, nested suspend with /dev/snapshot open, and suspend-to-idle with USB hub disconnect. adapter: racing pm_test=freezer suspend against eth0 down/up deadlocked --- Changes in v2: - Call usb_autopm_get_interface() before the teardown in rtl8152_close(), so that the runtime resume started by rtl_notifier() cannot race with it (Sashiko). - Count the references held by rtl_notifier() instead of using a flag, which lost one if a suspend started while /dev/snapshot was open. - Say how the issue was found and tested (netdev-bot). - Use the chromium.org address for both the author and the Signed-off-by so that they match. - Rebase on net/main. - Link to v1: https://lore.kernel.org/r/20261123-r8152-pm-notifier-rtnl-v1-1-31e011a2ee51@google.com --- drivers/net/usb/r8152.c | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c index d3bb7ee9c9bf..d34afb5a85ce 100644 --- a/drivers/net/usb/r8152.c +++ b/drivers/net/usb/r8152.c @@ -932,6 +932,7 @@ struct r8152 { struct mutex control; /* use for hw setting */ #ifdef CONFIG_PM_SLEEP struct notifier_block pm_notifier; + atomic_t pm_notifier_refs; #endif struct tasklet_struct tx_tl; @@ -7077,12 +7078,19 @@ static int rtl_notifier(struct notifier_block *nb, unsigned long action, switch (action) { case PM_HIBERNATION_PREPARE: case PM_SUSPEND_PREPARE: - usb_autopm_get_interface(tp->intf); + /* + * Count the references taken. The device may be probed or + * removed before the matching PM_POST_* event, and a suspend + * may start while /dev/snapshot is open for hibernation. + */ + if (!usb_autopm_get_interface(tp->intf)) + atomic_inc(&tp->pm_notifier_refs); break; case PM_POST_HIBERNATION: case PM_POST_SUSPEND: - usb_autopm_put_interface(tp->intf); + if (atomic_add_unless(&tp->pm_notifier_refs, -1, 0)) + usb_autopm_put_interface(tp->intf); break; case PM_POST_RESTORE: @@ -7135,10 +7143,6 @@ static int rtl8152_open(struct net_device *netdev) mutex_unlock(&tp->control); usb_autopm_put_interface(tp->intf); -#ifdef CONFIG_PM_SLEEP - tp->pm_notifier.notifier_call = rtl_notifier; - register_pm_notifier(&tp->pm_notifier); -#endif return 0; out_unlock: @@ -7153,11 +7157,11 @@ static int rtl8152_open(struct net_device *netdev) static int rtl8152_close(struct net_device *netdev) { struct r8152 *tp = netdev_priv(netdev); - int res = 0; + int res; + + /* Keep a runtime resume or suspend from racing with the teardown */ + res = usb_autopm_get_interface(tp->intf); -#ifdef CONFIG_PM_SLEEP - unregister_pm_notifier(&tp->pm_notifier); -#endif tasklet_disable(&tp->tx_tl); clear_bit(WORK_ENABLE, &tp->flags); usb_kill_urb(tp->intr_urb); @@ -7165,7 +7169,6 @@ static int rtl8152_close(struct net_device *netdev) napi_disable(&tp->napi); netif_stop_queue(netdev); - res = usb_autopm_get_interface(tp->intf); if (res < 0 || test_bit(RTL8152_INACCESSIBLE, &tp->flags)) { rtl_drop_queued_tx(tp); rtl_stop_rx(tp); @@ -10283,6 +10286,15 @@ static int rtl8152_probe_once(struct usb_interface *intf, netif_napi_add(netdev, &tp->napi, r8152_poll); +#ifdef CONFIG_PM_SLEEP + /* + * Not in ndo_open(): this takes the PM notifier chain lock, which + * must not be taken under RTNL. + */ + tp->pm_notifier.notifier_call = rtl_notifier; + register_pm_notifier(&tp->pm_notifier); +#endif + ret = register_netdev(netdev); if (ret != 0) { dev_err(&intf->dev, "couldn't register the device\n"); @@ -10308,6 +10320,12 @@ static int rtl8152_probe_once(struct usb_interface *intf, unregister_netdev(netdev); out1: +#ifdef CONFIG_PM_SLEEP + unregister_pm_notifier(&tp->pm_notifier); + /* No PM_POST_* event is going to be delivered anymore. */ + while (atomic_add_unless(&tp->pm_notifier_refs, -1, 0)) + usb_autopm_put_interface(tp->intf); +#endif tasklet_kill(&tp->tx_tl); cancel_delayed_work_sync(&tp->hw_phy_work); if (tp->rtl_ops.unload) @@ -10364,6 +10382,12 @@ static void rtl8152_disconnect(struct usb_interface *intf) rtl_set_unplug(tp); unregister_netdev(tp->netdev); +#ifdef CONFIG_PM_SLEEP + unregister_pm_notifier(&tp->pm_notifier); + /* No PM_POST_* event is going to be delivered anymore. */ + while (atomic_add_unless(&tp->pm_notifier_refs, -1, 0)) + usb_autopm_put_interface(tp->intf); +#endif tasklet_kill(&tp->tx_tl); cancel_delayed_work_sync(&tp->hw_phy_work); if (tp->rtl_ops.unload) --- base-commit: e2fb9368a41662bc54624f9be87ea8989b109306 change-id: 20261123-r8152-pm-notifier-rtnl-76975ae76f7d Best regards, -- Hsin-Te Yuan