Linux wireless drivers development
 help / color / mirror / Atom feed
From: Antoine Pichot <antoine.pichot@gmail.com>
To: Brian Norris <briannorris@chromium.org>,
	Francesco Dolcini <francesco@dolcini.it>
Cc: Antoine Pichot <antoine.pichot@gmail.com>,
	linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	Andreas Kemnade <andreas@kemnade.info>
Subject: [PATCH wireless 0/2] wifi: mwifiex: pcie: fix NULL dereferences in the card work at teardown
Date: Tue, 24 Nov 2026 09:59:30 +0900	[thread overview]
Message-ID: <20261124005935.290010-1-antoine.pichot@gmail.com> (raw)

A command timeout makes mwifiex queue a firmware dump and a card reset
on card->work. If the driver is torn down while that work runs, the
teardown clears card->adapter and the work dereferences NULL.

On a Surface Book 2 (88W8897 over PCIe), this crashed the machine at
shutdown four times in about 400 reboots, run back to back for an
unrelated test. In each crash with a full log, the firmware did not
answer the shutdown command, and mwifiex_pcie_shutdown() tore the driver
down during the dump. NULL dereferences of card->adapter in the card
work are reported on a Surface Pro 3, after the driver's recovery from a
firmware failure, in [2], and on SDIO in [1].

Patch 1 makes remove() wait for the work, and keep it from being queued
again, before the teardown. This brings back the wait that commit
7e34c0d2f631 ("Revert "mwifiex: cancel pcie/sdio work in remove/shutdown
handler"") reverted [3]. It is safe since commit a64e7a79dd60 ("mwifiex:
resolve reset vs. remove()/shutdown() deadlocks"), from the same series,
made the work only try to take the device lock.

Patch 2 makes the work return when card->adapter is already NULL and
skip the reset when a teardown started during the dump. This covers
firmware initialisation failures that clear card->adapter before or
during the dump; they tear the driver down without remove().

The RFC in [1] adds NULL checks only, and a NULL check cannot help when
the memory is freed after the check. For example,
mwifiex_drv_info_dump() checks priv->netdev, then reads its name and
transmit queues. During removal, mwifiex_remove_card() can unregister
and free that netdev in between: it deletes the interfaces before it
clears card->adapter and waits for the work. With patch 1, remove()
waits for the work before the teardown starts, so the two no longer
overlap.

mwifiex_sdio has the same race, and [1] is that crash, but the same
change is not safe there. The SDIO card reset rebuilds the adapter
inside the work, so with the wait in remove(), a reset queued by the
shutdown command's timeout would run in full during the removal. If its
reinitialisation fails, mwifiex_cleanup_sdio() calls cancel_work_sync()
on the work it runs in, and remove() would never return. An SDIO fix
would first have to keep the reset from running during removal. I have
no SDIO hardware to test one. Andreas, if you can run your
rmmod/modprobe loop with such a patch, I can send one.

On the Surface Book 2, running v7.3.4 with the linux-surface patches,
patch 1 was tested with a debug change that queues the dump and the
reset in remove(), and both patches together through repeated load and
removal, a runtime reset, and failed resets and a failed probe with a
second debug change that drops one command during firmware
initialisation. On v7.3.5 with the linux-surface patches, lockdep,
KASAN, debug objects and the hung-task detector, both patches went
through the same load, removal, reset and failure tests, and through the
dump-and-reset change with the dump already running; the kernel printed
no warnings during them.

With W=1, the patches add no warning on x86_64, i386, arm64, and
big-endian ppc64 and ppc32 (the last without SMP). They add none with
sparse on x86_64, and allmodconfig with CONFIG_WERROR builds on
wireless/main and next-20261123. Not tested at runtime: other
architectures, a kernel without SMP or without preemption, and
linux-next. No fault injection.

I made this series with an AI coding assistant, Claude Code with
Anthropic's Claude Opus 5.5 model. Over several sessions I asked it to
find the cause of the shutdown crashes, check whether a fix already
existed upstream, then write and test one. It decoded the crash records
and the traces in [2], wrote the patches, the debug changes and these
messages, and ran the tests on the Surface Book 2. I reviewed the
patches and the test results.

For the stable kernels: patch 1 uses disable_work_sync(), added in Linux
6.10, so it can only be backported to 6.10 and later. Patch 2 does not
depend on it and can be backported further on its own; it then fixes the
NULL dereferences, but the dump can still run alongside the teardown.

Not addressed here, and older than these patches:

- A PCI reset started outside the work (sysfs reset, bus reset) whose
  reinitialisation fails frees the adapter without waiting for the work:
  mwifiex_cleanup_pcie() skips cancel_work_sync() while
  pci_reset_ongoing is set.
- At probe, if the dump ends before the failure path clears
  card->adapter, the reset still runs. mwifiex_shutdown_sw() then waits
  for fw_done, which _mwifiex_fw_dpc() completes only after waiting for
  this work. Both wait forever, and the work holds the device lock, so a
  later unbind or power-off hangs too.
- The crash in mwifiex_hist_data_add() that [2] also shows, which is not
  in the card work.

[1] https://lore.kernel.org/all/20251118072527.174375-1-andreas@kemnade.info/
[2] https://github.com/linux-surface/linux-surface/issues/2350
[3] https://lore.kernel.org/all/20180112210837.112628-1-briannorris@chromium.org/

Antoine Pichot (2):
  wifi: mwifiex: pcie: stop the card work before tearing down the
    adapter
  wifi: mwifiex: pcie: skip the card work once the adapter is gone

 drivers/net/wireless/marvell/mwifiex/pcie.c | 36 +++++++++++++++++----
 1 file changed, 30 insertions(+), 6 deletions(-)


base-commit: dcf495789037ab78d8ddf5844239cf85d92ac5f1
-- 
2.56.1


             reply	other threads:[~2026-11-24  0:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  0:59 Antoine Pichot [this message]
2026-11-24  0:59 ` [PATCH wireless 1/2] wifi: mwifiex: pcie: stop the card work before tearing down the adapter Antoine Pichot
2026-11-24  0:59 ` [PATCH wireless 2/2] wifi: mwifiex: pcie: skip the card work once the adapter is gone Antoine Pichot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124005935.290010-1-antoine.pichot@gmail.com \
    --to=antoine.pichot@gmail.com \
    --cc=andreas@kemnade.info \
    --cc=briannorris@chromium.org \
    --cc=francesco@dolcini.it \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox