[BUG] Deadlock in _cfg80211_unregister_wdev()

Started by Maximilian Luz on May 13, 2021 · 6 messages from 3 authors · Last reply May 15, 2021

open the first message on its own page

Message details and related content
Following commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"), the mwifiex_pcie module fails to unload. This also
prevents the device from rebooting / shutting down.

Attempting to unload the module produces the log pasted below. Upon
further investigation, this looks like a deadlock inside
_cfg80211_unregister_wdev():

- According to [1], this function expects the rdev->wiphy.mtx to be
  held.
- Down the line, this function (through some indirections, see third
  trace in log below) calls call_netdevice_notifiers(NETDEV_GOING_DOWN,
  ...) [2].
- One of the registered notifiers seems to be
  cfg80211_netdev_notifier_call(), which attempts to lock
  rdev->wiphy.mtx again [3], completing the deadlock.

Regards,
Max


[1]: https://elixir.bootlin.com/linux/v5.13-rc1/source/net/wireless/core.c#L1130
[2]: https://elixir.bootlin.com/linux/v5.13-rc1/source/net/core/dev.c#L1667
[3]: https://elixir.bootlin.com/linux/v5.13-rc1/source/net/wireless/core.c#L1428

[  245.504760] INFO: task kworker/u16:1:107 blocked for more than 122 seconds.
[  245.504764]       Tainted: G         C OE     5.11.0-1-surface-dev #2
[  245.504765] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
[  245.504766] task:kworker/u16:1   state:D stack:    0 pid:  107 ppid:     2 flags:0x00004000
[  245.504771] Workqueue: events_power_efficient reg_check_chans_work [cfg80211]
[  245.504817] Call Trace:
[  245.504820]  __schedule+0x2dd/0x8b0
[  245.504826]  schedule+0x5b/0xc0
[  245.504829]  schedule_preempt_disabled+0x11/0x20
[  245.504831]  __mutex_lock.constprop.0+0x317/0x500
[  245.504835]  reg_check_chans_work+0x2d/0x3c0 [cfg80211]
[  245.504867]  process_one_work+0x214/0x3e0
[  245.504870]  worker_thread+0x4d/0x3d0
[  245.504872]  ? rescuer_thread+0x410/0x410
[  245.504874]  kthread+0x133/0x150

70 more lines

Message details and related content
On Fri, 2021-05-14 at 01:07 +0200, Maximilian Luz wrote:
Following commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"), the mwifiex_pcie module fails to unload. This also
prevents the device from rebooting / shutting down.

Attempting to unload the module
I'm *guessing* that you're attempting to unload the module while the
interface is still up, i.e. you didn't "ip link set wlan0 down" first?

If so, that is likely fixed by this commit as well:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ea6b2098dd02789f68770fd3d5a373732207be2f

However, your log says:
[  245.504764]       Tainted: G         C OE     5.11.0-1-surface-dev #2
so I have no idea what kernel you're using, because 5.11 did *not*
contain commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"). If you backported the bug you get to be
responsible for backporting the fixes too?


If that's all not solving the issue then please try to resolve with gdb
what line of code "cfg80211_netdev_notifier_call+0x12a" is, and please
also clarify exactly what (upstream!) kernel you're using.

johannes

Message details and related content
On 5/14/21 10:26 AM, Johannes Berg wrote:
On Fri, 2021-05-14 at 01:07 +0200, Maximilian Luz wrote:
quoted
Following commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"), the mwifiex_pcie module fails to unload. This also
prevents the device from rebooting / shutting down.

Attempting to unload the module
I'm *guessing* that you're attempting to unload the module while the
interface is still up, i.e. you didn't "ip link set wlan0 down" first?
I did not. Doing so indeed allows unloading of the module.
If so, that is likely fixed by this commit as well:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ea6b2098dd02789f68770fd3d5a373732207be2f
Thanks for pointing this out. That's backported to 5.12.2, right?
Unfortunately the error still persists there (as on all other post 5.12
kernels, as mentioned below).
However, your log says:
quoted
[  245.504764]       Tainted: G         C OE     5.11.0-1-surface-dev #2
so I have no idea what kernel you're using, because 5.11 did *not*
contain commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"). If you backported the bug you get to be
responsible for backporting the fixes too?
This is a log from a05829a7222e checked out, which was pre 5.12-rc1, so the
last tag is still 5.11 which sets the package version in my packaging scripts
to that as well. It's not an actual 5.11 kernel, sorry for the confusion.
There is no backporting going on, just bisecting.
If that's all not solving the issue then please try to resolve with gdb
what line of code "cfg80211_netdev_notifier_call+0x12a" is, and please
also clarify exactly what (upstream!) kernel you're using.
The issue exists on 5.12 through 5.12.4, as well as 5.13-rc1 (which is
why I didn't bother specifying a version, sorry again).

If you really want me to, I'll try to find some time to learn GDB kernel
debugging (never done that before, so might take a bit) however, I think
it's fairly clear what's going wrong and why the fix you've linked below
doesn't apply in this case:

Your fix is fixing

    cfg80211_destroy_iface_wk() takes wiphy_lock
     -> cfg80211_destroy_ifaces()
      -> ieee80211_del_iface
       -> ieeee80211_if_remove
        -> cfg80211_unregister_wdev
         -> unregister_netdevice_queue
          -> dev_close_many
           -> __dev_close_many
            -> raw_notifier_call_chain
             -> cfg80211_netdev_notifier_call

by addressing this in cfg80211_destroy_iface{s,_wk}(). The trace from my
log shows

    mwifiex_uninit_sw() takes wiphy_lock
     -> mwifiex_del_virtual_intf
      -> cfg80211_unregister_netdevice()
       -> cfg80211_unregister_wdev()
        -> _cfg80211_unregister_wdev() has lockdep_assert_held(&rdev->wiphy.mtx)
         -> unregister_netdevice_queue
          -> dev_close_many
           -> __dev_close_many
            -> raw_notifier_call_chain
             -> cfg80211_netdev_notifier_call attempts to take wiphy_lock again

So your fix does not address this particular issue. It doesn't even
touch any of the affected code path. I believe it is instead fixing one
symptom of the same underlying problem.

19 more lines

Message details and related content
On 5/14/21 1:07 AM, Maximilian Luz wrote:
Following commit a05829a7222e ("cfg80211: avoid holding the RTNL when
calling the driver"), the mwifiex_pcie module fails to unload. This also
prevents the device from rebooting / shutting down.

Attempting to unload the module produces the log pasted below. Upon
further investigation, this looks like a deadlock inside
_cfg80211_unregister_wdev():

- According to [1], this function expects the rdev->wiphy.mtx to be
   held.
- Down the line, this function (through some indirections, see third
   trace in log below) calls call_netdevice_notifiers(NETDEV_GOING_DOWN,
   ...) [2].
- One of the registered notifiers seems to be
   cfg80211_netdev_notifier_call(), which attempts to lock
   rdev->wiphy.mtx again [3], completing the deadlock.
Looks like the underlying issue also leads to

  https://lore.kernel.org/linux-wireless/ab4d00ce52f32bd8e45ad0448a44737e@bewaar.me/

Regards,
Max
Message details and related content
On Fri, May 14, 2021 at 10:26:25AM +0200, Johannes Berg wrote:
If that's all not solving the issue then please try to resolve with gdb
what line of code "cfg80211_netdev_notifier_call+0x12a" is, and please
also clarify exactly what (upstream!) kernel you're using.
I can reproduce on v5.12 and v5.12.4 as well. With v5.12.4, I'm at:

net/wireless/core.c:
1428			wiphy_lock(&rdev->wiphy);
include/net/cfg80211.h:
5269		mutex_lock(&wiphy->mtx);


i.e.,

static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
					 unsigned long state, void *ptr)
{
...
	case NETDEV_GOING_DOWN:
		wiphy_lock(&rdev->wiphy);  <--- right here
		cfg80211_leave(rdev, wdev);
		wiphy_unlock(&rdev->wiphy);
...

It would seem like _anyone_ that calls cfg80211_unregister_wdev() with
an interface up will hit this -- not unique to mwifiex. In fact, apart
from the fact that all his line numbers are wrong, Maximilian's original
email points out exactly where the deadlock is.
cfg80211_unregister_wdev() holds the wiphy lock, and the GOING_DOWN
notification also tries to grab it.

It does happen that in many other paths, you've already ensured that you
bring the interface down, so e.g., mac80211 drivers don't tend to hit
this. But I wouldn't be surprised if a few other cfg80211 drivers hit
this too.

The best solution I could figure was to do a similar lock dance done in
nl80211_del_interface() -- close the netdev without holding the wiphy
lock. I'll send out a patch shortly.

Brian
Message details and related content
On 5/15/21 4:44 AM, Brian Norris wrote:
It would seem like _anyone_ that calls cfg80211_unregister_wdev() with
an interface up will hit this -- not unique to mwifiex. In fact, apart
from the fact that all his line numbers are wrong, Maximilian's original
email points out exactly where the deadlock is.
cfg80211_unregister_wdev() holds the wiphy lock, and the GOING_DOWN
notification also tries to grab it.

It does happen that in many other paths, you've already ensured that you
bring the interface down, so e.g., mac80211 drivers don't tend to hit
this. But I wouldn't be surprised if a few other cfg80211 drivers hit
this too.

The best solution I could figure was to do a similar lock dance done in
nl80211_del_interface() -- close the netdev without holding the wiphy
lock. I'll send out a patch shortly.
I believe that if we're going to fix that in the individual drivers,
there should be at least some sort of warning/documentation on
cfg80211_unregister_wdev().

Also someone might want to look at other WiFi drivers calling
cfg80211_unregister_wdev(). For example, I can see a locked call in the
brcm80211 driver, but no previous dev_close() call (see [1]). Haven't
looked in detail though, so I might just be wrong.

I can't help but think that this should maybe be addressed in that
common part instead. I know too little of that subsystem to tell if that
might be infeasible though.

Regards,
Max

[1]: https://elixir.bootlin.com/linux/v5.13-rc1/source/drivers/net/wireless/broadcom/brcm80211/brcmfmac/p2p.c#L2445
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help