FAILED: patch "[PATCH] wifi: brcmfmac: drain bus_reset work on device removal" failed to apply to 6.18-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x 43b25879f004c98defa2776bedc6ca4763c51945
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026072951-caring-silicon-be2d@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 43b25879f004c98defa2776bedc6ca4763c51945 Mon Sep 17 00:00:00 2001
From: Fan Wu <[email protected]>
Date: Sat, 18 Jul 2026 02:43:53 +0000
Subject: [PATCH] wifi: brcmfmac: drain bus_reset work on device removal
brcmf_fw_crashed() and the debugfs "reset" entry both schedule drvr->bus_reset, whose callback recovers drvr through container_of() and dereferences it. The removal path frees drvr (brcmf_free -> wiphy_free) without draining the work, so a bus_reset callback pending or running during removal can outlive drvr.
Cancellation cannot live in brcmf_detach() or brcmf_free(): the work callback reaches teardown through the bus .reset op (PCIe brcmf_pcie_reset -> brcmf_detach; SDIO brcmf_sdio_bus_reset -> brcmf_sdiod_remove -> brcmf_free), so cancelling there would wait for the running work and deadlock.
Add a per-bus mutex (bus_reset_lock) and route all arming through brcmf_bus_schedule_reset(), which under the lock skips when the bus is marked removing. Each bus remove entry calls brcmf_bus_cancel_reset_work(), which under the same lock sets removing and cancels the work. Holding the mutex across cancel_work_sync() makes the set-removing + drain step atomic. Every producer reaches the arming path from process context -- the PCIe firmware-halt notification runs in the threaded IRQ handler (brcmf_pcie_isr_thread) and the SDIO hostmail path runs from the data workqueue -- so the mutex is taken only in sleepable contexts. Where applicable the remove entry first stops the firmware-crash producer: on PCIe mask the mailbox and synchronize_irq; on SDIO unregister the bus interrupt and cancel the data worker, which also reports firmware halts through brcmf_fw_crashed(). The mutex is initialized at bus allocation. The SDIO suspend power-off path frees drvr through the same brcmf_sdiod_remove() and takes the same lock; resume re-allows the work only on a successful re-probe.
Also guard brcmf_fw_crashed() against a NULL bus_if/drvr: it can fire before brcmf_attach() wires up drvr, and it dereferences drvr (bphy_err/brcmf_dev_coredump) before reaching the arming gate.
The bus_reset work is shared across buses, so the drain is applied to every remove path: PCIe (the .reset op introduced by the Fixes commit), SDIO (arms the same work through brcmf_fw_crashed()), and USB (via the debugfs "reset" entry). cancel_work_sync() drains a running or pending bus_reset work item before removal frees drvr, and patch 1/2 makes the scratch-buffer release safe when reset teardown has already released those DMA buffers.
This patch fixes the lifetime of the bus_reset work item itself. It does not attempt to address the separate, pre-existing lifetime of the asynchronous firmware completion started by the PCIe reset path. That callback needs its own lifetime/ownership protocol and is being tracked separately.
This issue was found by an in-house static analysis tool.
drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c +13 -0
@@ -1069,6 +1069,7 @@ static int brcmf_ops_sdio_probe(struct sdio_func *func, bus_if =kzalloc_obj (*bus_if);if (!bus_if)return -ENOMEM ; +mutex_init (&bus_if->bus_reset_lock); sdiodev =kzalloc_obj (*sdiodev);if (!sdiodev) {kfree (bus_if);
@@ -1130,6 +1131,14 @@ static void brcmf_ops_sdio_remove(struct sdio_func *func)if (func->num !=1 )return ; +/* Drain bus_reset before the shared brcmf_sdiod_remove() +* teardown, which the SDIO reset callback also reaches. The +* data worker can arm bus_reset via brcmf_fw_crashed(); cancel +* it first. +*/ +brcmf_sdio_cancel_datawork (sdiodev->bus); +brcmf_bus_cancel_reset_work (bus_if); +/* only proceed with rest of cleanup if func 1 */ brcmf_sdiod_remove (sdiodev);
@@ -1204,6 +1213,8 @@ static int brcmf_ops_sdio_suspend(struct device *dev) }else {/* power will be cut so remove device, probe again in resume */ brcmf_sdiod_intr_unregister (sdiodev); +brcmf_sdio_cancel_datawork (sdiodev->bus); +brcmf_bus_cancel_reset_work (bus_if); ret =brcmf_sdiod_remove (sdiodev);if (ret)brcmf_err ("Failed to remove device on suspend\n" );
@@ -1229,6 +1240,8 @@ static int brcmf_ops_sdio_resume(struct device *dev) ret =brcmf_sdiod_probe (sdiodev);if (ret)brcmf_err ("Failed to probe device on resume\n" ); +else +brcmf_bus_allow_reset_work (bus_if); }else {if (sdiodev->wowl_enabled && sdiodev->settings->bus.sdio.oob_irq_supported)disable_irq_wake (sdiodev->settings->bus.sdio.oob_irq_nr);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/bus.h +6 -0
@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/firmware.h> #include <linux/device.h> +#include <linux/mutex.h> #include "debug.h" /* IDs of the 6 default common rings of msgbuf protocol */
@@ -179,6 +180,8 @@ struct brcmf_bus {enum brcmf_fwvendor fwvid;bool always_use_fws_queue;bool wowl_supported; +bool removing;/* device removal in progress; quiesce async work */ +struct mutex bus_reset_lock;const struct brcmf_bus_ops *ops;struct brcmf_bus_msgbuf *msgbuf;
@@ -186,6 +189,9 @@ struct brcmf_bus {struct list_head list; }; +void brcmf_bus_cancel_reset_work (struct brcmf_bus *bus_if); +void brcmf_bus_allow_reset_work (struct brcmf_bus *bus_if); +/* * callback wrappers */
drivers/net/wireless/broadcom/brcm80211/brcmfmac/core.c +42 -4
69 lines not on this page · raw message
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +6 -0
@@ -2503,6 +2503,7 @@ brcmf_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id) ret = -ENOMEM ; goto fail; } +mutex_init (&bus->bus_reset_lock); bus->msgbuf =kzalloc_obj (*bus->msgbuf);if (!bus->msgbuf) { ret = -ENOMEM ;
@@ -2598,6 +2599,11 @@ brcmf_pcie_remove(struct pci_dev *pdev)if (devinfo->ci)brcmf_pcie_intr_disable (devinfo); +if (devinfo->irq_allocated) +synchronize_irq (pdev->irq); + +brcmf_bus_cancel_reset_work (bus); +brcmf_detach (&pdev->dev);brcmf_free (&pdev->dev);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c +6 -0
@@ -4560,6 +4560,12 @@ int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)return ret; } +void brcmf_sdio_cancel_datawork (struct brcmf_sdio *bus) +{ +if (bus) +cancel_work_sync (&bus->datawork); +} +/* Detach and free everything */ void brcmf_sdio_remove (struct brcmf_sdio *bus) {
drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h +1 -0
@@ -361,6 +361,7 @@ int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev);int brcmf_sdio_probe (struct brcmf_sdio_dev *sdiodev);void brcmf_sdio_remove (struct brcmf_sdio *bus);void brcmf_sdio_isr (struct brcmf_sdio *bus,bool in_isr); +void brcmf_sdio_cancel_datawork (struct brcmf_sdio *bus);void brcmf_sdio_wd_timer (struct brcmf_sdio *bus,bool active);void brcmf_sdio_wowl_config (struct device *dev,bool enabled);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c +3 -0
@@ -1260,6 +1260,7 @@ static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo, ret = -ENOMEM ; goto fail; } +mutex_init (&bus->bus_reset_lock); bus->dev = dev; bus_pub->bus = bus;
@@ -1329,6 +1330,8 @@ brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)return ;brcmf_dbg (USB ,"Enter, bus_pub %p\n" , devinfo); +brcmf_bus_cancel_reset_work (devinfo->bus_pub.bus); +brcmf_detach (devinfo->dev);brcmf_free (devinfo->dev);kfree (devinfo->bus_pub.bus);
Sasha Levin
maintainerPATCH 1/1 wifi: brcmfmac: drain bus_reset work on device removal
From: Fan Wu <[email protected]>
[ Upstream commit 43b25879f004c98defa2776bedc6ca4763c51945 ]
brcmf_fw_crashed() and the debugfs "reset" entry both schedule drvr->bus_reset, whose callback recovers drvr through container_of() and dereferences it. The removal path frees drvr (brcmf_free -> wiphy_free) without draining the work, so a bus_reset callback pending or running during removal can outlive drvr.
Cancellation cannot live in brcmf_detach() or brcmf_free(): the work callback reaches teardown through the bus .reset op (PCIe brcmf_pcie_reset -> brcmf_detach; SDIO brcmf_sdio_bus_reset -> brcmf_sdiod_remove -> brcmf_free), so cancelling there would wait for the running work and deadlock.
Add a per-bus mutex (bus_reset_lock) and route all arming through brcmf_bus_schedule_reset(), which under the lock skips when the bus is marked removing. Each bus remove entry calls brcmf_bus_cancel_reset_work(), which under the same lock sets removing and cancels the work. Holding the mutex across cancel_work_sync() makes the set-removing + drain step atomic. Every producer reaches the arming path from process context -- the PCIe firmware-halt notification runs in the threaded IRQ handler (brcmf_pcie_isr_thread) and the SDIO hostmail path runs from the data workqueue -- so the mutex is taken only in sleepable contexts. Where applicable the remove entry first stops the firmware-crash producer: on PCIe mask the mailbox and synchronize_irq; on SDIO unregister the bus interrupt and cancel the data worker, which also reports firmware halts through brcmf_fw_crashed(). The mutex is initialized at bus allocation. The SDIO suspend power-off path frees drvr through the same brcmf_sdiod_remove() and takes the same lock; resume re-allows the work only on a successful re-probe.
Also guard brcmf_fw_crashed() against a NULL bus_if/drvr: it can fire before brcmf_attach() wires up drvr, and it dereferences drvr (bphy_err/brcmf_dev_coredump) before reaching the arming gate.
The bus_reset work is shared across buses, so the drain is applied to every remove path: PCIe (the .reset op introduced by the Fixes commit), SDIO (arms the same work through brcmf_fw_crashed()), and USB (via the debugfs "reset" entry). cancel_work_sync() drains a running or pending bus_reset work item before removal frees drvr, and patch 1/2 makes the scratch-buffer release safe when reset teardown has already released those DMA buffers.
This patch fixes the lifetime of the bus_reset work item itself. It does not attempt to address the separate, pre-existing lifetime of the asynchronous firmware completion started by the PCIe reset path. That callback needs its own lifetime/ownership protocol and is being tracked separately.
This issue was found by an in-house static analysis tool.
--- .../broadcom/brcm80211/brcmfmac/bcmsdh.c | 13 ++++++ .../broadcom/brcm80211/brcmfmac/bus.h | 6 +++ .../broadcom/brcm80211/brcmfmac/core.c | 46 +++++++++++++++++-- .../broadcom/brcm80211/brcmfmac/pcie.c | 6 +++ .../broadcom/brcm80211/brcmfmac/sdio.c | 6 +++ .../broadcom/brcm80211/brcmfmac/sdio.h | 1 + .../broadcom/brcm80211/brcmfmac/usb.c | 3 ++ 7 files changed, 77 insertions(+), 4 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c +13 -0
@@ -1070,6 +1070,7 @@ static int brcmf_ops_sdio_probe(struct sdio_func *func, bus_if =kzalloc (sizeof (*bus_if),GFP_KERNEL );if (!bus_if)return -ENOMEM ; +mutex_init (&bus_if->bus_reset_lock); sdiodev =kzalloc (sizeof (*sdiodev),GFP_KERNEL );if (!sdiodev) {kfree (bus_if);
@@ -1131,6 +1132,14 @@ static void brcmf_ops_sdio_remove(struct sdio_func *func)if (func->num !=1 )return ; +/* Drain bus_reset before the shared brcmf_sdiod_remove() +* teardown, which the SDIO reset callback also reaches. The +* data worker can arm bus_reset via brcmf_fw_crashed(); cancel +* it first. +*/ +brcmf_sdio_cancel_datawork (sdiodev->bus); +brcmf_bus_cancel_reset_work (bus_if); +/* only proceed with rest of cleanup if func 1 */ brcmf_sdiod_remove (sdiodev);
@@ -1205,6 +1214,8 @@ static int brcmf_ops_sdio_suspend(struct device *dev) }else {/* power will be cut so remove device, probe again in resume */ brcmf_sdiod_intr_unregister (sdiodev); +brcmf_sdio_cancel_datawork (sdiodev->bus); +brcmf_bus_cancel_reset_work (bus_if); ret =brcmf_sdiod_remove (sdiodev);if (ret)brcmf_err ("Failed to remove device on suspend\n" );
@@ -1230,6 +1241,8 @@ static int brcmf_ops_sdio_resume(struct device *dev) ret =brcmf_sdiod_probe (sdiodev);if (ret)brcmf_err ("Failed to probe device on resume\n" ); +else +brcmf_bus_allow_reset_work (bus_if); }else {if (sdiodev->wowl_enabled && sdiodev->settings->bus.sdio.oob_irq_supported)disable_irq_wake (sdiodev->settings->bus.sdio.oob_irq_nr);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/bus.h +6 -0
@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/firmware.h> #include <linux/device.h> +#include <linux/mutex.h> #include "debug.h" /* IDs of the 6 default common rings of msgbuf protocol */
@@ -179,6 +180,8 @@ struct brcmf_bus {enum brcmf_fwvendor fwvid;bool always_use_fws_queue;bool wowl_supported; +bool removing;/* device removal in progress; quiesce async work */ +struct mutex bus_reset_lock;const struct brcmf_bus_ops *ops;struct brcmf_bus_msgbuf *msgbuf;
@@ -186,6 +189,9 @@ struct brcmf_bus {struct list_head list; }; +void brcmf_bus_cancel_reset_work (struct brcmf_bus *bus_if); +void brcmf_bus_allow_reset_work (struct brcmf_bus *bus_if); +/* * callback wrappers */
drivers/net/wireless/broadcom/brcm80211/brcmfmac/core.c +42 -4
69 lines not on this page · raw message
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +6 -0
@@ -2504,6 +2504,7 @@ brcmf_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id) ret = -ENOMEM ; goto fail; } +mutex_init (&bus->bus_reset_lock); bus->msgbuf =kzalloc (sizeof (*bus->msgbuf),GFP_KERNEL );if (!bus->msgbuf) { ret = -ENOMEM ;
@@ -2599,6 +2600,11 @@ brcmf_pcie_remove(struct pci_dev *pdev)if (devinfo->ci)brcmf_pcie_intr_disable (devinfo); +if (devinfo->irq_allocated) +synchronize_irq (pdev->irq); + +brcmf_bus_cancel_reset_work (bus); +brcmf_detach (&pdev->dev);brcmf_free (&pdev->dev);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c +6 -0
@@ -4560,6 +4560,12 @@ int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)return ret; } +void brcmf_sdio_cancel_datawork (struct brcmf_sdio *bus) +{ +if (bus) +cancel_work_sync (&bus->datawork); +} +/* Detach and free everything */ void brcmf_sdio_remove (struct brcmf_sdio *bus) {
drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h +1 -0
@@ -361,6 +361,7 @@ int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev);int brcmf_sdio_probe (struct brcmf_sdio_dev *sdiodev);void brcmf_sdio_remove (struct brcmf_sdio *bus);void brcmf_sdio_isr (struct brcmf_sdio *bus,bool in_isr); +void brcmf_sdio_cancel_datawork (struct brcmf_sdio *bus);void brcmf_sdio_wd_timer (struct brcmf_sdio *bus,bool active);void brcmf_sdio_wowl_config (struct device *dev,bool enabled);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c +3 -0
@@ -1260,6 +1260,7 @@ static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo, ret = -ENOMEM ; goto fail; } +mutex_init (&bus->bus_reset_lock); bus->dev = dev; bus_pub->bus = bus;
@@ -1329,6 +1330,8 @@ brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)return ;brcmf_dbg (USB ,"Enter, bus_pub %p\n" , devinfo); +brcmf_bus_cancel_reset_work (devinfo->bus_pub.bus); +brcmf_detach (devinfo->dev);brcmf_free (devinfo->dev);kfree (devinfo->bus_pub.bus);
Signature · 1 line
2.53.0