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
  2026-11-24 12:30 ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ 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] 4+ 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-24  3:28   ` Hsin-Te Yuan
  2026-11-24 12:30 ` netdev-bot+sashiko
  1 sibling, 1 reply; 4+ 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] 4+ messages in thread

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

On Mon, Nov 23, 2026 at 8:32 PM <netdev-bot+sinfo@kernel.org> wrote:
>
> 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.

The deadlock was hit during suspend testing on real hardware, on arm64
devices running a 6.12-based kernel. A device was put to sleep
(suspend-to-idle) and a USB-C monitor was then unplugged. The r8152
adapter is on the USB hub that disconnects along with the monitor.
The device could not be woken up in 2 of 10 attempts, and both devices
that were tried showed it.
With the patch applied to that 6.12-based kernel, 50 cycles of the same
sequence on a device that had reproduced the deadlock showed no failures.

Regards,
Hsin-Te

^ permalink raw reply	[flat|nested] 4+ 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-24 12:30 ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 12:30 UTC (permalink / raw)
  To: yuanhsinte
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, hayeswang,
	linux-usb, netdev, yuanhsinte

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

Critical: 0 · High: 1 · Medium: 0 · Low: 1

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] Moving unregister_pm_notifier() out of rtl8152_close() removes
  the only thing that kept rtl_notifier() from racing ndo_stop.
- [Low] The commit author address does not match the Signed-off-by
  address.

--

r8152: do not (un)register the PM notifier under RTNL

This patch moves register_pm_notifier() and unregister_pm_notifier() out
of the ndo_open and ndo_stop handlers. They now run in
rtl8152_probe_once(), its error path and rtl8152_disconnect(), which
avoids a deadlock between RTNL and the PM notifier chain rwsem. It also
adds a PM_NOTIFIER_AUTOPM_HELD flag, so the runtime PM reference taken
by rtl_notifier() is only dropped if it was actually taken.

> Fixes: 5ee3c60c8d3b ("r8152: fix the wake event")
> Signed-off-by: Hsin-Te Yuan <yuanhsinte@google.com>

[Severity: Low]
The Author line uses yuanhsinte@chromium.org, but the only Signed-off-by
uses yuanhsinte@google.com. Should these match?

checkpatch reports this as FROM_SIGN_OFF_MTCH ("From:/Signed-off-by:
email address mismatch").

> diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
> index d3bb7ee9c9bf3..fea47a8924149 100644
> --- a/drivers/net/usb/r8152.c
> +++ b/drivers/net/usb/r8152.c

[ ... ]

> @@ -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);

[Severity: High]
Can rtl_notifier() now race with rtl8152_close()?

Before this patch, rtl8152_close() started with unregister_pm_notifier(),
which does down_write(&pm_chain_head.rwsem). That call waited for the
whole PM_SUSPEND_PREPARE chain to finish, including the synchronous
runtime resume started from rtl_notifier():

rtl_notifier()
  usb_autopm_get_interface()
    rtl8152_resume()            /* takes tp->control */
      rtl8152_runtime_resume()

The notifier now stays registered for the whole lifetime of the device.
Nothing seems to stop this path from overlapping ndo_stop.
rtl8152_runtime_resume() checks netif_running() once and then does:

	tp->rtl_ops.autosuspend_en(tp, false);
	napi_disable(napi);
	set_bit(WORK_ENABLE, &tp->flags);
	...
	napi_enable(napi);
	...
	usb_submit_urb(tp->intr_urb, GFP_NOIO);

At the same time, __dev_close_many() holds RTNL, clears
__LINK_STATE_START and calls rtl8152_close(). That function does the
following without tp->control or an autopm reference:

	tasklet_disable(&tp->tx_tl);
	clear_bit(WORK_ENABLE, &tp->flags);
	usb_kill_urb(tp->intr_urb);
	cancel_delayed_work_sync(&tp->schedule);
	napi_disable(&tp->napi);

If the napi_disable() in close runs first, would the napi_disable() in
rtl8152_runtime_resume() loop forever? NAPI stays disabled until the
next open. The resuming task holds tp->control and holds
pm_chain_head.rwsem for read.

rtl8152_close() would then block in usb_autopm_get_interface(), waiting
for that resume while still holding RTNL. That looks like the same
suspend/RTNL hang the commit message describes.

If the napi_disable() in the resume path runs first, close waits for its
napi_enable(). The resume path then sets WORK_ENABLE and resubmits
tp->intr_urb, which undoes the teardown. Close then continues into
rtl_ops.down() and free_all_mem():

free_all_mem()
	usb_free_urb(tp->intr_urb);
	tp->intr_urb = NULL;

	kfree(tp->intr_buff);
	tp->intr_buff = NULL;

Is the interrupt URB still queued at the HCD at that point? If so,
intr_callback() would read the freed tp->intr_buff and resubmit the URB.
That would be a use-after-free, with DMA into freed memory.

Other autoresume sources, such as remote wakeup or hw_phy_work, can
already race close this way. The PM notifier, though, is a new trigger
added by this change.

This needs an interface that is up but runtime-suspended, and an ifdown
that runs at the same time as the start of system suspend or
hibernation. PREPARE notifiers run before tasks are frozen, so these two
can overlap.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-r8152-pm-notifier-rtnl-v1-1-31e011a2ee51%40google.com

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

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

Thread overview: 4+ 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-24  3:28   ` Hsin-Te Yuan
2026-11-24 12:30 ` netdev-bot+sashiko

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