Netdev List
 help / color / mirror / Atom feed
* [PATCH net v3 0/2] net: wwan: iosm: fix S3 resume by re-binding the driver after it
@ 2026-11-26 19:41 David Westen
  2026-11-26 19:41 ` [PATCH net v3 1/2] net: wwan: iosm: free the MUX after the netdevs that use it David Westen
  2026-11-26 19:41 ` [PATCH net v3 2/2] net: wwan: iosm: re-bind the driver when the modem rebooted during S3 David Westen
  0 siblings, 2 replies; 3+ messages in thread
From: David Westen @ 2026-11-26 19:41 UTC (permalink / raw)
  To: netdev
  Cc: Loic Poulain, Sergey Ryazanov, Johannes Berg, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	M Chetan Kumar, Maciej S. Szmigiero, David Westen

On platforms where S3 cuts power to the WWAN card, the modem boots again
on its own while the driver still treats it as running, and the modem
can't be used after resume until the driver is re-bound. Patch 2 detects
this on resume and re-binds the driver then. Patch 1 fixes a
use-after-free in the cleanup, which the re-bind would hit with wwan0 up.

v2 unbound the driver before every S3 suspend. That runs the cleanup
against a modem that is still running, and the AI review and my tests
showed how many ways that goes wrong: the modem kept writing into the
freed MUX buffers (DMA faults with a strict IOMMU), and timers, tasks
and MSIs could outlive what they use. Fixing that properly means
reworking the cleanup. v3 instead lets the modem lose power, as it did
before, and re-binds the driver after resume, when the modem no longer
holds any of the driver's memory.

Changes in v3:
- Re-bind after resume, and only if the modem rebooted, instead of
  unbinding before every S3 suspend. Nothing changes where the card
  keeps power in S3 (AI review), or for s2idle, opportunistic sleep and
  an explicit freeze or standby (Maciej).
- Dropped "cancel the ADB timer on cleanup". Closing that race for a
  normal unbind needs a larger rework of the cleanup; patch 2 keeps the
  re-bind out of it, and out of the MSI race the AI review found.
- New patch 1.

On the ThinkPad X1 Carbon 6th gen I tested on, S3 takes the same suspend
path as s2idle, and the modem crashes before it loses power. That is
where the CD_READY in patch 2's log comes from. The s2idle series [1]
removes that crash, but the modem still boots again after S3, so both
are needed there. They don't depend on each other.

Tested on an XMM7360 in that ThinkPad:
- With KASAN, lockdep and a strict IOMMU: re-bound and connected again
  after each of five S3 cycles, also with wwan0 up through the suspend;
  no re-bind after s2idle; three unbinds with wwan0 up and traffic. No
  reports.
- Together with the s2idle series, on a kernel without debug options:
  the same, and s2idle connected again without a re-bind.

Not tested: the XMM7560 (MUX aggregation), platforms that use the D3L2
path, and hibernation, which this series doesn't change. Known limits:
- On the D3L2 path, interrupts from the rebooted modem reach the driver
  until its resume callback runs, as without this series.
- A modem that stays in its boot stage after losing power delays the end
  of resume by up to 10 s before the re-bind.
- For a normal unbind, the cleanup races with the ADB timer and with
  MSIs are unchanged.
- Re-binding three times or more without a power cycle can leave the
  link without downlink until the bearer is reconnected. That happens
  without this series too, and doesn't affect S3, where the modem loses
  power.

Patch 2 builds on commit 0b6f6593aa8c ("net: wwan: iosm: Fix hibernation
by re-binding the driver around it").

[1] https://lore.kernel.org/r/20261124172505.41520-1-davidlwesten@gmail.com

v2: https://lore.kernel.org/r/20261119042714.505405-1-davidlwesten@gmail.com
v1: https://lore.kernel.org/r/20261116031148.333990-1-davidlwesten@gmail.com

The investigation and the patches were done with an AI coding assistant
(Claude Opus 5.5). We wrote the patches together, and I ran the tests.

David Westen (2):
  net: wwan: iosm: free the MUX after the netdevs that use it
  net: wwan: iosm: re-bind the driver when the modem rebooted during S3

 drivers/net/wwan/iosm/iosm_ipc_imem.c      | 54 ++++++++++++++++++++--
 drivers/net/wwan/iosm/iosm_ipc_imem.h      | 18 ++++++++
 drivers/net/wwan/iosm/iosm_ipc_imem_ops.c  |  4 ++
 drivers/net/wwan/iosm/iosm_ipc_irq.c       |  3 ++
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c |  6 +++
 drivers/net/wwan/iosm/iosm_ipc_pcie.c      | 37 +++++++++++++++
 drivers/net/wwan/iosm/iosm_ipc_protocol.c  |  3 ++
 7 files changed, 121 insertions(+), 4 deletions(-)

-- 
2.56.0


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

* [PATCH net v3 1/2] net: wwan: iosm: free the MUX after the netdevs that use it
  2026-11-26 19:41 [PATCH net v3 0/2] net: wwan: iosm: fix S3 resume by re-binding the driver after it David Westen
@ 2026-11-26 19:41 ` David Westen
  2026-11-26 19:41 ` [PATCH net v3 2/2] net: wwan: iosm: re-bind the driver when the modem rebooted during S3 David Westen
  1 sibling, 0 replies; 3+ messages in thread
From: David Westen @ 2026-11-26 19:41 UTC (permalink / raw)
  To: netdev
  Cc: Loic Poulain, Sergey Ryazanov, Johannes Berg, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	M Chetan Kumar, Maciej S. Szmigiero, David Westen

ipc_imem_cleanup() frees the MUX before it removes the netdevs, so
closing a netdev that is up uses the freed MUX:

  BUG: KASAN: slab-use-after-free in ipc_mux_close_session
  Call Trace:
   ipc_mux_close_session
   ipc_wwan_link_stop
   __dev_close_many
   wwan_unregister_ops
   ipc_wwan_deinit
   ipc_imem_cleanup
   ipc_pcie_remove

Free the MUX last, after debugfs, the ports and the netdevs. Closing
the netdev then tells the modem to close the session, so that it stops
writing into the MUX buffers before they are freed. The reply is
ignored, as for the port closes. Ring the doorbell for the command at
once, as the cleanup has already cancelled the TD update timer.

Reproduced with KASAN on an XMM7360 by unbinding the driver while
wwan0 is up. With this change, three such unbinds show no report.

Fixes: 3670970dd8c6 ("net: iosm: shared memory IPC interface")
Signed-off-by: David Westen <davidlwesten@gmail.com>
---
 drivers/net/wwan/iosm/iosm_ipc_imem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wwan/iosm/iosm_ipc_imem.c b/drivers/net/wwan/iosm/iosm_ipc_imem.c
index 4405c8531888..3ab409166c50 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_imem.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_imem.c
@@ -178,7 +178,8 @@ void ipc_imem_msg_send_feature_set(struct iosm_imem *ipc_imem,
 void ipc_imem_td_update_timer_start(struct iosm_imem *ipc_imem)
 {
 	/* Use the TD update timer only in the runtime phase */
-	if (!ipc_imem->enter_runtime || ipc_imem->td_update_timer_suspended) {
+	if (!ipc_imem->enter_runtime || ipc_imem->td_update_timer_suspended ||
+	    ipc_imem->phase == IPC_P_OFF_REQ) {
 		/* trigger the doorbell irq on CP directly. */
 		ipc_protocol_doorbell_trigger(ipc_imem->ipc_protocol,
 					      IPC_HP_TD_UPD_TMR_START);
@@ -1244,10 +1245,10 @@ void ipc_imem_cleanup(struct iosm_imem *ipc_imem)
 	cancel_work_sync(&ipc_imem->run_state_worker);
 
 	if (test_and_clear_bit(FULLY_FUNCTIONAL, &ipc_imem->flag)) {
-		ipc_mux_deinit(ipc_imem->mux);
 		ipc_debugfs_deinit(ipc_imem);
-		ipc_wwan_deinit(ipc_imem->wwan);
 		ipc_port_deinit(ipc_imem->ipc_port);
+		ipc_wwan_deinit(ipc_imem->wwan);
+		ipc_mux_deinit(ipc_imem->mux);
 	}
 
 	if (test_and_clear_bit(IOSM_DEVLINK_INIT, &ipc_imem->flag))
-- 
2.56.0


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

* [PATCH net v3 2/2] net: wwan: iosm: re-bind the driver when the modem rebooted during S3
  2026-11-26 19:41 [PATCH net v3 0/2] net: wwan: iosm: fix S3 resume by re-binding the driver after it David Westen
  2026-11-26 19:41 ` [PATCH net v3 1/2] net: wwan: iosm: free the MUX after the netdevs that use it David Westen
@ 2026-11-26 19:41 ` David Westen
  1 sibling, 0 replies; 3+ messages in thread
From: David Westen @ 2026-11-26 19:41 UTC (permalink / raw)
  To: netdev
  Cc: Loic Poulain, Sergey Ryazanov, Johannes Berg, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	M Chetan Kumar, Maciej S. Szmigiero, David Westen

On platforms where S3 cuts power to the WWAN card, the modem boots
again while the driver still treats it as running, and the first port
open after resume fails:

  iosm 0000:03:00.0: PORT open refused, phase A-CD_READY

The driver takes this from a stale stage in the shared memory: on an
XMM7360, the modem writes CD_READY there before it loses power. The
modem itself is fine.

On resume, read the stage from the MMIO area. If the modem booted
again, mark it as gone: the driver then neither talks to it nor waits
for it, and the cleanup only releases memory. Once the modem has
reached the run stage, re-bind the driver, so that probe finds it as
after a cold boot. Nothing changes where the card keeps power, and a
crashed modem is left alone.

Tested with S3 on an XMM7360 in a ThinkPad X1 Carbon 6th gen, with
KASAN, lockdep and a strict IOMMU: five cycles, also with wwan0 up
through the suspend.

Fixes: 7e98d785ae61 ("net: iosm: entry point")
Signed-off-by: David Westen <davidlwesten@gmail.com>
---
 drivers/net/wwan/iosm/iosm_ipc_imem.c      | 47 +++++++++++++++++++++-
 drivers/net/wwan/iosm/iosm_ipc_imem.h      | 18 +++++++++
 drivers/net/wwan/iosm/iosm_ipc_imem_ops.c  |  4 ++
 drivers/net/wwan/iosm/iosm_ipc_irq.c       |  3 ++
 drivers/net/wwan/iosm/iosm_ipc_mux_codec.c |  6 +++
 drivers/net/wwan/iosm/iosm_ipc_pcie.c      | 37 +++++++++++++++++
 drivers/net/wwan/iosm/iosm_ipc_protocol.c  |  3 ++
 7 files changed, 117 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wwan/iosm/iosm_ipc_imem.c b/drivers/net/wwan/iosm/iosm_ipc_imem.c
index 3ab409166c50..673c5ad403e7 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_imem.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_imem.c
@@ -208,6 +208,9 @@ void ipc_imem_hrtimer_stop(struct hrtimer *hr_timer)
  */
 void ipc_imem_adb_timer_start(struct iosm_imem *ipc_imem)
 {
+	if (test_bit(IOSM_MODEM_GONE, &ipc_imem->flag))
+		return;
+
 	if (!hrtimer_active(&ipc_imem->adb_timer)) {
 		ipc_imem->hrtimer_period =
 			ktime_set(0, IOSM_AGGR_MUX_ADB_FINISH_TIMEOUT_NSEC);
@@ -1042,6 +1045,44 @@ void ipc_imem_pm_s2idle_sleep(struct iosm_imem *ipc_imem, bool sleep)
 	ipc_protocol_s2idle_sleep(ipc_imem->ipc_protocol, sleep);
 }
 
+bool ipc_imem_modem_rebooted(struct iosm_imem *ipc_imem)
+{
+	if (!test_bit(FULLY_FUNCTIONAL, &ipc_imem->flag))
+		return false;
+
+	switch (ipc_mmio_get_exec_stage(ipc_imem->mmio)) {
+	case IPC_MEM_EXEC_STAGE_RUN:
+		return ipc_mmio_get_ipc_state(ipc_imem->mmio) ==
+		       IPC_MEM_DEVICE_IPC_UNINIT;
+	case IPC_MEM_EXEC_STAGE_CRASH:
+	case IPC_MEM_EXEC_STAGE_CD_READY:
+	case IPC_MEM_EXEC_STAGE_INVALID:
+		return false;
+	default:
+		return true;
+	}
+}
+
+bool ipc_imem_wait_for_run(struct iosm_imem *ipc_imem)
+{
+	int timeout = IPC_MODEM_BOOT_TIMEOUT;
+
+	for (;;) {
+		switch (ipc_mmio_get_exec_stage(ipc_imem->mmio)) {
+		case IPC_MEM_EXEC_STAGE_RUN:
+			return true;
+		case IPC_MEM_EXEC_STAGE_CRASH:
+		case IPC_MEM_EXEC_STAGE_CD_READY:
+			return false;
+		default:
+			break;
+		}
+		if (!--timeout)
+			return false;
+		msleep(20);
+	}
+}
+
 void ipc_imem_pm_resume(struct iosm_imem *ipc_imem)
 {
 	enum ipc_mem_exec_stage stage;
@@ -1204,6 +1245,9 @@ static void ipc_imem_device_ipc_uninit(struct iosm_imem *ipc_imem)
 	int timeout = IPC_MODEM_UNINIT_TIMEOUT_MS;
 	enum ipc_mem_device_ipc_state ipc_state;
 
+	if (test_bit(IOSM_MODEM_GONE, &ipc_imem->flag))
+		return;
+
 	/* When PCIe link is up set IPC_UNINIT
 	 * of the modem otherwise ignore it when PCIe link down happens.
 	 */
@@ -1440,7 +1484,8 @@ struct iosm_imem *ipc_imem_init(struct iosm_pcie *pcie, unsigned int device_id,
 void ipc_imem_irq_process(struct iosm_imem *ipc_imem, int irq)
 {
 	/* Debounce IPC_EV_IRQ. */
-	if (ipc_imem && !ipc_imem->ev_irq_pending[irq]) {
+	if (ipc_imem && !ipc_imem->ev_irq_pending[irq] &&
+	    !test_bit(IOSM_MODEM_GONE, &ipc_imem->flag)) {
 		ipc_imem->ev_irq_pending[irq] = true;
 		ipc_task_queue_send_task(ipc_imem, ipc_imem_tq_irq_cb, irq,
 					 NULL, 0, false);
diff --git a/drivers/net/wwan/iosm/iosm_ipc_imem.h b/drivers/net/wwan/iosm/iosm_ipc_imem.h
index 5664ac507c90..3f6d1ccf2a7c 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_imem.h
+++ b/drivers/net/wwan/iosm/iosm_ipc_imem.h
@@ -102,6 +102,8 @@ struct ipc_chnl_cfg;
 
 #define FULLY_FUNCTIONAL 0
 #define IOSM_DEVLINK_INIT 1
+/* The modem rebooted: don't talk to it or wait for it until the re-bind */
+#define IOSM_MODEM_GONE 2
 
 /* List of the supported UL/DL pipes. */
 enum ipc_mem_pipes {
@@ -404,6 +406,22 @@ void ipc_imem_pm_s2idle_sleep(struct iosm_imem *ipc_imem, bool sleep);
  */
 void ipc_imem_pm_suspend(struct iosm_imem *ipc_imem);
 
+/**
+ * ipc_imem_modem_rebooted - Check whether the modem booted again
+ * @ipc_imem:	Pointer to imem data-struct
+ *
+ * Returns: true if it did after the driver had brought it up
+ */
+bool ipc_imem_modem_rebooted(struct iosm_imem *ipc_imem);
+
+/**
+ * ipc_imem_wait_for_run - Wait up to about 10 s for the run stage
+ * @ipc_imem:	Pointer to imem data-struct
+ *
+ * Returns: true if the modem reached it
+ */
+bool ipc_imem_wait_for_run(struct iosm_imem *ipc_imem);
+
 /**
  * ipc_imem_pm_resume - The HAL shall inform the shared memory layer
  *			that the device is active.
diff --git a/drivers/net/wwan/iosm/iosm_ipc_imem_ops.c b/drivers/net/wwan/iosm/iosm_ipc_imem_ops.c
index 109cf8930488..8cff81919480 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_imem_ops.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_imem_ops.c
@@ -218,6 +218,9 @@ void ipc_imem_sys_port_close(struct iosm_imem *ipc_imem,
 		return;
 	}
 
+	if (test_bit(IOSM_MODEM_GONE, &ipc_imem->flag))
+		goto close;
+
 	/* If there are any pending TDs then wait for Timeout/Completion before
 	 * closing pipe.
 	 */
@@ -267,6 +270,7 @@ void ipc_imem_sys_port_close(struct iosm_imem *ipc_imem,
 		ipc_imem->app_notify_dl_pend = 0;
 	}
 
+close:
 	/* Due to wait for completion in messages, there is a small window
 	 * between closing the pipe and updating the channel is closed. In this
 	 * small window there could be HP update from Host Driver. Hence update
diff --git a/drivers/net/wwan/iosm/iosm_ipc_irq.c b/drivers/net/wwan/iosm/iosm_ipc_irq.c
index 702f50a48151..d9d0e28fafae 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_irq.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_irq.c
@@ -25,6 +25,9 @@ static void ipc_write_dbell_reg(struct iosm_pcie *ipc_pcie, int irq_n, u32 data)
 
 void ipc_doorbell_fire(struct iosm_pcie *ipc_pcie, int irq_n, u32 data)
 {
+	if (ipc_pcie->imem && test_bit(IOSM_MODEM_GONE, &ipc_pcie->imem->flag))
+		return;
+
 	ipc_write_dbell_reg(ipc_pcie, irq_n, data);
 }
 
diff --git a/drivers/net/wwan/iosm/iosm_ipc_mux_codec.c b/drivers/net/wwan/iosm/iosm_ipc_mux_codec.c
index 0bbd41263cc2..a31f6fa7ea55 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_mux_codec.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_mux_codec.c
@@ -150,6 +150,9 @@ int ipc_mux_dl_acb_send_cmds(struct iosm_mux *ipc_mux, u32 cmd_type, u8 if_id,
 	union mux_type_cmdh cmdh;
 	int ret = 0;
 
+	if (test_bit(IOSM_MODEM_GONE, &ipc_mux->imem->flag))
+		return -EIO;
+
 	acb->if_id = if_id;
 	ret = ipc_mux_acb_alloc(ipc_mux);
 	if (ret)
@@ -1525,6 +1528,9 @@ int ipc_mux_ul_trigger_encode(struct iosm_mux *ipc_mux, int if_id,
 	struct mux_session *session = &ipc_mux->session[if_id];
 	int ret = -EINVAL;
 
+	if (test_bit(IOSM_MODEM_GONE, &ipc_mux->imem->flag))
+		return -EIO;
+
 	if (ipc_mux->channel &&
 	    ipc_mux->channel->state != IMEM_CHANNEL_ACTIVE) {
 		dev_err(ipc_mux->dev,
diff --git a/drivers/net/wwan/iosm/iosm_ipc_pcie.c b/drivers/net/wwan/iosm/iosm_ipc_pcie.c
index 49052eb511ba..767a609462b4 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_pcie.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_pcie.c
@@ -427,6 +427,10 @@ static int __maybe_unused ipc_pcie_resume_cb(struct device *dev)
 
 	ipc_pcie = pci_get_drvdata(pdev);
 
+	/* pm_notify() re-binds the driver for a modem that booted again */
+	if (ipc_imem_modem_rebooted(ipc_pcie->imem))
+		set_bit(IOSM_MODEM_GONE, &ipc_pcie->imem->flag);
+
 	switch (ipc_pcie->d3l2_support) {
 	case IPC_PCIE_D0L12:
 		ipc_pcie_resume_s2idle(ipc_pcie);
@@ -532,6 +536,37 @@ void ipc_pcie_kfree_skb(struct iosm_pcie *ipc_pcie, struct sk_buff *skb)
 	dev_kfree_skb(skb);
 }
 
+static void ipc_pcie_rebind_if_rebooted(struct pci_dev *pdev)
+{
+	struct iosm_pcie *ipc_pcie;
+	bool rebooted = false;
+
+	device_lock(&pdev->dev);
+	if (pdev->dev.driver == &iosm_ipc_driver.driver) {
+		ipc_pcie = pci_get_drvdata(pdev);
+		rebooted = test_bit(IOSM_MODEM_GONE, &ipc_pcie->imem->flag);
+		/* Let probe find it in the run stage, as after a cold boot */
+		if (rebooted && !ipc_imem_wait_for_run(ipc_pcie->imem))
+			dev_warn(&pdev->dev, "modem not running after resume");
+	}
+	device_unlock(&pdev->dev);
+
+	if (rebooted && device_reprobe(&pdev->dev))
+		dev_err(&pdev->dev, "re-binding the driver failed");
+}
+
+static void ipc_pcie_rebind_rebooted(void)
+{
+	const struct pci_device_id *id;
+	struct pci_dev *pdev;
+
+	for (id = iosm_ipc_ids; id->vendor; id++) {
+		pdev = NULL;
+		while ((pdev = pci_get_device(id->vendor, id->device, pdev)))
+			ipc_pcie_rebind_if_rebooted(pdev);
+	}
+}
+
 static int pm_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
 {
 	if (mode == PM_HIBERNATION_PREPARE || mode == PM_RESTORE_PREPARE) {
@@ -551,6 +586,8 @@ static int pm_notify(struct notifier_block *nb, unsigned long mode, void *_unuse
 				pci_registered = true;
 			}
 		}
+	} else if (mode == PM_POST_SUSPEND) {
+		ipc_pcie_rebind_rebooted();
 	}
 
 	return 0;
diff --git a/drivers/net/wwan/iosm/iosm_ipc_protocol.c b/drivers/net/wwan/iosm/iosm_ipc_protocol.c
index 527355a671b6..a49a45ed0b0d 100644
--- a/drivers/net/wwan/iosm/iosm_ipc_protocol.c
+++ b/drivers/net/wwan/iosm/iosm_ipc_protocol.c
@@ -69,6 +69,9 @@ int ipc_protocol_msg_send(struct iosm_protocol *ipc_protocol,
 	/* Trap if called from non-preemptible context */
 	might_sleep();
 
+	if (test_bit(IOSM_MODEM_GONE, &ipc_protocol->imem->flag))
+		return -EIO;
+
 	response.status = IPC_MEM_MSG_CS_INVALID;
 	init_completion(&response.completion);
 
-- 
2.56.0


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

end of thread, other threads:[~2026-11-26 19:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 19:41 [PATCH net v3 0/2] net: wwan: iosm: fix S3 resume by re-binding the driver after it David Westen
2026-11-26 19:41 ` [PATCH net v3 1/2] net: wwan: iosm: free the MUX after the netdevs that use it David Westen
2026-11-26 19:41 ` [PATCH net v3 2/2] net: wwan: iosm: re-bind the driver when the modem rebooted during S3 David Westen

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