* [PATCH wireless 0/2] wifi: mwifiex: pcie: fix NULL dereferences in the card work at teardown
@ 2026-11-24 0:59 Antoine Pichot
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
0 siblings, 2 replies; 3+ messages in thread
From: Antoine Pichot @ 2026-11-24 0:59 UTC (permalink / raw)
To: Brian Norris, Francesco Dolcini
Cc: Antoine Pichot, linux-wireless, linux-kernel, Andreas Kemnade
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
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH wireless 1/2] wifi: mwifiex: pcie: stop the card work before tearing down the adapter
2026-11-24 0:59 [PATCH wireless 0/2] wifi: mwifiex: pcie: fix NULL dereferences in the card work at teardown Antoine Pichot
@ 2026-11-24 0:59 ` 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
1 sibling, 0 replies; 3+ messages in thread
From: Antoine Pichot @ 2026-11-24 0:59 UTC (permalink / raw)
To: Brian Norris, Francesco Dolcini
Cc: Antoine Pichot, linux-wireless, linux-kernel, Andreas Kemnade,
stable
If the firmware does not answer the shutdown command sent by
mwifiex_pcie_remove(), mwifiex_cmd_timeout_func() queues a firmware dump
and a card reset on card->work. remove() continues into
mwifiex_remove_card(), which clears card->adapter and frees the
interfaces while the dump runs. When the dump ends, the work reads
card->adapter for the reset and dereferences NULL:
mwifiex_pcie 0000:01:00.0: mwifiex_cmd_timeout_func: Timeout cmd id = 0xaa, act = 0x0
mwifiex_pcie 0000:01:00.0: info: shutdown mwifiex...
mwifiex_pcie 0000:01:00.0: == mwifiex firmware dump start ==
BUG: kernel NULL pointer dereference, address: 0000000000000088
Workqueue: events mwifiex_pcie_work [mwifiex_pcie]
RIP: 0010:mwifiex_pcie_work+0x87/0xc0 [mwifiex_pcie]
This crashed a Surface Book 2 (88W8897) at shutdown four times in about
400 reboots; the three crashes with a full log went through
mwifiex_pcie_shutdown().
The teardown waits for the work in mwifiex_cleanup_pcie(), but only
after card->adapter is cleared. Commit b713bbf1471b ("mwifiex: cancel
pcie/sdio work in remove/shutdown handler") cancelled the work earlier
in remove() for this crash. Commit 7e34c0d2f631 ("Revert "mwifiex:
cancel pcie/sdio work in remove/shutdown handler"") reverted it, saying
that since commit b014e96d1abb ("PCI: Protect
pci_error_handlers->reset_notify() usage with device_lock()") a reset
cannot run during remove(), and that the work then waited in
pci_reset_function() for the device lock that remove() holds. The device
lock does not cover the dump, nor the read of card->adapter before the
reset. Since commit a64e7a79dd60 ("mwifiex: resolve reset vs.
remove()/shutdown() deadlocks"), the work only tries to take that lock,
so remove() can wait for it before the teardown again.
Use disable_work_sync() rather than cancel_work_sync(), so that a
timeout or a debugfs reset request during the teardown cannot queue the
work again. The card is allocated at probe, so a later bind starts with
a new work. When the firmware does not answer the shutdown command,
remove() already waited for the dump that the timeout starts (about 46 s
on the 88W8897), in mwifiex_cleanup_pcie(); it now waits before the
teardown instead. A dump still waiting in the queue is cancelled.
remove() also returns early when a failed reset or a failed probe left
no adapter. The work can still be queued then: a failed reset queues it
again from inside it, and the teardown of a failed probe stops the
command timer only after waiting for the work. Wait for it there too,
before the card is freed.
Tested on the Surface Book 2 with a debug change that queues the dump
and the reset in remove() right after the shutdown command. Without this
patch (kernel 7.2.15) the work found card->adapter NULL in 3 runs out of
3. With it (kernel 7.3.4, before the wait on the early return was added;
the main path is unchanged), and a 200 ms pause so that the dump was
already running when remove() reached the wait, remove() waited for the
dump (about 46 s) and the teardown completed cleanly in 5 runs out of 5.
With patch 2 also applied, a debug change that makes a reset fail (it
drops one command during firmware initialisation, so that the command
timeout queues the work again and the initialisation fails), and a 5 s
pause in the work when it finds no adapter, remove() took the early
return while that work was running, and returned only after it.
Found, written and tested with an AI coding assistant (Claude Code with
Claude Opus 5.5) under my direction; I reviewed the patch and the test
results.
Fixes: 7e34c0d2f631 ("Revert "mwifiex: cancel pcie/sdio work in remove/shutdown handler"")
Cc: stable@vger.kernel.org # 6.10+
Signed-off-by: Antoine Pichot <antoine.pichot@gmail.com>
---
drivers/net/wireless/marvell/mwifiex/pcie.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c
index ecbe864b0797..b63e6ac6e582 100644
--- a/drivers/net/wireless/marvell/mwifiex/pcie.c
+++ b/drivers/net/wireless/marvell/mwifiex/pcie.c
@@ -437,8 +437,14 @@ static void mwifiex_pcie_remove(struct pci_dev *pdev)
wait_for_completion(&card->fw_done);
adapter = card->adapter;
- if (!adapter || !adapter->priv_num)
+ if (!adapter || !adapter->priv_num) {
+ /* The adapter is gone, but a failed reset or probe can have
+ * queued the work again, and the card it runs on is freed
+ * when remove() returns.
+ */
+ disable_work_sync(&card->work);
return;
+ }
reg = card->pcie.reg;
if (reg)
@@ -456,6 +462,13 @@ static void mwifiex_pcie_remove(struct pci_dev *pdev)
mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
}
+ /* A timeout of the commands above queues a firmware dump and a card
+ * reset on card->work. Wait for it and keep it from being queued
+ * again before the teardown clears card->adapter and frees the
+ * interfaces the dump reads.
+ */
+ disable_work_sync(&card->work);
+
mwifiex_remove_card(adapter);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH wireless 2/2] wifi: mwifiex: pcie: skip the card work once the adapter is gone
2026-11-24 0:59 [PATCH wireless 0/2] wifi: mwifiex: pcie: fix NULL dereferences in the card work at teardown Antoine Pichot
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 ` Antoine Pichot
1 sibling, 0 replies; 3+ messages in thread
From: Antoine Pichot @ 2026-11-24 0:59 UTC (permalink / raw)
To: Brian Norris, Francesco Dolcini
Cc: Antoine Pichot, linux-wireless, linux-kernel, Andreas Kemnade,
stable
The work can also find card->adapter NULL without remove(), when
firmware initialisation fails at probe or after a reset. Both paths end
in the failure path of _mwifiex_fw_dpc(), which clears card->adapter and
frees the adapter:
- At probe, a command timeout during the initialisation queues a dump
and a reset. The failure path clears card->adapter during the dump and
waits for the work in mwifiex_cleanup_pcie(). The work then reads NULL
for the reset.
- After a reset started by the work, the failure path runs inside the
work, and it skips cancel_work_sync() while a PCI reset is ongoing
since commit 4add4d988f95 ("mwifiex: pcie: skip cancel_work_sync() on
reset failure path"). A timeout during the failed initialisation
queues the work again, and it runs after the adapter is freed, with
card->adapter NULL.
[1] reports four crashes in the card work on a Surface Pro 3 after the
driver's recovery from a firmware failure. In the reporter's kernel
build, all four are the work dereferencing a NULL card->adapter: three
in the reset branch and one at the start of the dump. This patch was not
tested on that machine. On a Surface Book 2 the unpatched driver crashed
on the probe path.
Read card->adapter when the work starts and return if it is NULL.
When remove() or the probe failure path tears the driver down, the
pointer stays valid while the work runs, since both wait for the work
before freeing the adapter. Skip the reset if card->adapter was cleared
during the dump: a teardown is then in progress. Pair it with
WRITE_ONCE() where card->adapter is set and cleared.
Tested on the Surface Book 2 (kernel 7.3.4) with a debug change that
drops one command during firmware initialisation, so that the command
timeout queues a dump and a reset and the initialisation fails. After a
failed runtime reset, the work queued again by the timeout found
card->adapter NULL and returned. At probe, the failure path cleared
card->adapter during the dump and the work skipped the reset. No crash
or warning in either case.
Found, written and tested with an AI coding assistant (Claude Code with
Claude Opus 5.5) under my direction; I reviewed the patch and the test
results.
Link: https://github.com/linux-surface/linux-surface/issues/2350 [1]
Fixes: 3860e5e39532 ("mwifiex: move pcie_work and related variables inside card")
Cc: stable@vger.kernel.org
Signed-off-by: Antoine Pichot <antoine.pichot@gmail.com>
---
drivers/net/wireless/marvell/mwifiex/pcie.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/drivers/net/wireless/marvell/mwifiex/pcie.c b/drivers/net/wireless/marvell/mwifiex/pcie.c
index b63e6ac6e582..c4837102f0cd 100644
--- a/drivers/net/wireless/marvell/mwifiex/pcie.c
+++ b/drivers/net/wireless/marvell/mwifiex/pcie.c
@@ -2842,13 +2842,24 @@ static void mwifiex_pcie_work(struct work_struct *work)
{
struct pcie_service_card *card =
container_of(work, struct pcie_service_card, work);
+ struct mwifiex_adapter *adapter = READ_ONCE(card->adapter);
+
+ /* The adapter is gone if a failed reset or probe has torn it
+ * down.
+ */
+ if (!adapter)
+ return;
if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
&card->work_flags))
- mwifiex_pcie_device_dump_work(card->adapter);
+ mwifiex_pcie_device_dump_work(adapter);
+ /* A teardown that started during the dump has cleared
+ * card->adapter. Skip the reset, which would run into that
+ * teardown.
+ */
if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
- &card->work_flags))
- mwifiex_pcie_card_reset_work(card->adapter);
+ &card->work_flags) && READ_ONCE(card->adapter))
+ mwifiex_pcie_card_reset_work(adapter);
}
/* This function dumps FW information */
@@ -3171,7 +3182,7 @@ static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
struct pcie_service_card *card = adapter->card;
/* save adapter pointer in card */
- card->adapter = adapter;
+ WRITE_ONCE(card->adapter, adapter);
if (mwifiex_pcie_request_irq(adapter))
return -1;
@@ -3215,7 +3226,7 @@ static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
if (card->msi_enable)
pci_disable_msi(pdev);
}
- card->adapter = NULL;
+ WRITE_ONCE(card->adapter, NULL);
}
/*
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 0:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 0:59 [PATCH wireless 0/2] wifi: mwifiex: pcie: fix NULL dereferences in the card work at teardown Antoine Pichot
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox