Linux USB
 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
  2026-11-23 12:38 ` sashiko-bot
  0 siblings, 2 replies; 3+ 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] 3+ messages in thread

* Re: [PATCH net] r8152: do not (un)register the PM notifier under RTNL
  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
  2026-11-23 12:38 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-11-23 12:32 UTC (permalink / raw)
  To: Hsin-Te Yuan
  Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, hayeswang, linux-usb, netdev, Hsin-Te Yuan

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

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

* Re: [PATCH net] r8152: do not (un)register the PM notifier under RTNL
  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
@ 2026-11-23 12:38 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-23 12:38 UTC (permalink / raw)
  To: Hsin-Te Yuan; +Cc: linux-usb

> 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)
> [ ... ]
> 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-r8152-pm-notifier-rtnl-v1-1-31e011a2ee51@google.com?part=1

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

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

Thread overview: 3+ 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
2026-11-23 12:38 ` sashiko-bot

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