Netdev List
 help / color / mirror / Atom feed
* [PATCH net] r8152: do not (un)register the PM notifier under RTNL
@ 2026-11-23 12:29 Hsin-Te Yuan
  2026-11-23 12:32 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Hsin-Te Yuan @ 2026-11-23 12:29 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, hayeswang
  Cc: linux-usb, netdev, Hsin-Te Yuan

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.

Set PM_NOTIFIER_AUTOPM_HELD when rtl_notifier() acquires a runtime PM
reference, and drop the reference at the post event or right after
unregister_pm_notifier() only if that flag was set. 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. 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 <yuanhsinte@google.com>
---
 drivers/net/usb/r8152.c | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
index d3bb7ee9c9bf..fea47a892414 100644
--- a/drivers/net/usb/r8152.c
+++ b/drivers/net/usb/r8152.c
@@ -792,6 +792,7 @@ enum rtl8152_flags {
 	IN_PRE_RESET,
 	PROBED_WITH_NO_ERRORS,
 	PROBE_SHOULD_RETRY,
+	PM_NOTIFIER_AUTOPM_HELD,
 };
 
 #define DEVICE_ID_LENOVO_USB_C_TRAVEL_HUB		0x721e
@@ -7077,12 +7078,18 @@ 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);
+		/*
+		 * Remember that the reference was taken. The device may be
+		 * probed or removed before the matching PM_POST_* event.
+		 */
+		if (!usb_autopm_get_interface(tp->intf))
+			set_bit(PM_NOTIFIER_AUTOPM_HELD, &tp->flags);
 		break;
 
 	case PM_POST_HIBERNATION:
 	case PM_POST_SUSPEND:
-		usb_autopm_put_interface(tp->intf);
+		if (test_and_clear_bit(PM_NOTIFIER_AUTOPM_HELD, &tp->flags))
+			usb_autopm_put_interface(tp->intf);
 		break;
 
 	case PM_POST_RESTORE:
@@ -7135,10 +7142,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:
@@ -7155,9 +7158,6 @@ static int rtl8152_close(struct net_device *netdev)
 	struct r8152 *tp = netdev_priv(netdev);
 	int res = 0;
 
-#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);
@@ -10283,6 +10283,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 +10317,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. */
+	if (test_and_clear_bit(PM_NOTIFIER_AUTOPM_HELD, &tp->flags))
+		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 +10379,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. */
+		if (test_and_clear_bit(PM_NOTIFIER_AUTOPM_HELD, &tp->flags))
+			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: af6737b9066832a2017c281159950ba92385ce00
change-id: 20261123-r8152-pm-notifier-rtnl-76975ae76f7d

Best regards,
-- 
Hsin-Te Yuan <yuanhsinte@google.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 12:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 12:29 [PATCH net] r8152: do not (un)register the PM notifier under RTNL Hsin-Te Yuan
2026-11-23 12:32 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox