Skip to contentSkip to conversation

Add SoundWire deatach helper

PATCH Superseded Charles Keepax 2 participants 7 messages Aug 31

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add SoundWire deatach helper

Currently, if a SoundWire driver takes an action that causes the device to detach from the bus it needs to be careful. There will be a period of time until the detach is recognised by the core, if the driver immediately calls sdw_slave_wait_for_init() it is possible for that to complete before the detach has been recognised. Leaving the driver with a device that is potentially not ready for what follows.

To avoid this race add a new helper that drivers can use to inform the core the device will detach. This clears the enumeration completions ensuring that sdw_slave_wait_for_init() can't complete until the device re-attaches.

This is the third part of a longer series of moving these slave attach/deatach operations into the core:

https://lore.kernel.org/linux-sound/[email protected]/
https://lore.kernel.org/linux-sound/[email protected]/

I have based this series off Vinod's soundwire tree as that probably makes sense for which tree to take it through.

Thanks,
Charles

Charles Keepax (2):
soundwire: Add a helper function to indicate a device will detach
mfd: cs42l43: Move to core detach helper

 drivers/mfd/cs42l43-sdw.c     |  2 --
 drivers/mfd/cs42l43.c         | 15 +--------------
 drivers/soundwire/bus.h       |  6 ------
 include/linux/mfd/cs42l43.h   |  1 -
 include/linux/soundwire/sdw.h | 29 +++++++++++++++++++++++++++++
 5 files changed, 30 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 soundwire: Add a helper function to indicate a device will detach

SoundWire devices will sometimes need to reset themselves, typical times might include when taking over the device to ensure a consistent state or after downloading a firmware update to boot into the new version. Often such a reset will also result in the device detaching from the SoundWire bus, in this case the driver has to be careful. Naively calling sdw_slave_wait_for_init() after doing the reset could immediately succeed if the bus hasn't seen the detach yet, causing the driver to proceed attempting to communicate with the device whilst it is detached.

Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus. This will reinit the completions in the peripheral for device attach, ensuring that the next sdw_slave_wait_for_init() will not complete before the detach has been seen by the core.

---
 drivers/soundwire/bus.h       |  6 ------
 include/linux/soundwire/sdw.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 6 deletions(-)
drivers/soundwire/bus.h +0 -6
index 44e4f51939176..59b6aee0ea6eb 100644
@@ -231,12 +231,6 @@ static inline void sdw_fill_port_params(struct sdw_port_params *params,
 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr);
 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value);
 
-/*
- * At the moment we only track Master-initiated hw_reset.
- * Additional fields can be added as needed
- */
-#define SDW_UNATTACH_REQUEST_MASTER_RESET	BIT(0)
-
 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request);
 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size);
 void sdw_compute_slave_ports(struct sdw_master_runtime *m_rt,
include/linux/soundwire/sdw.h +29 -0
index 707ca6bd1e23d..969604755ce41 100644
@@ -25,6 +25,13 @@ struct device_node;
 struct sdw_bus;
 struct sdw_slave;
 
+/*
+ * At the moment we only track Master-initiated/Slave-initiated hw_reset.
+ * Additional fields can be added as needed
+ */
+#define SDW_UNATTACH_REQUEST_MASTER_RESET	BIT(0)
+#define SDW_UNATTACH_REQUEST_SLAVE_RESET	BIT(1)
+
 /* SDW spec defines and enums, as defined by MIPI 1.1. Spec */
 
 /* SDW Broadcast Device Number */
@@ -1214,6 +1221,28 @@ static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u
 
 #endif /* CONFIG_SOUNDWIRE */
 
+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ * @timeout_us: Timeout in microseconds.
+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to
+ * return.
+ *
+ * Return: Zero on success, and a negative error code on failure.
+ */
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);
+}
+
 /**
  * sdw_slave_wait_for_init - Wait for device initialisation
  * @slave: Pointer to the SoundWire peripheral.
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/2] soundwire: Add a helper function to indicate a device will detach

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] sdw_slave_signal_unattach() fails to update slave->status to SDW_SLAVE_UNATTACHED, which can…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] sdw_slave_signal_unattach() fails to update slave->status to SDW_SLAVE_UNATTACHED, which can deadlock the SoundWire enumeration state machine if the transient hardware detach is missed.
- [Low] The kernel-doc comment for sdw_slave_signal_unattach() describes a non-existent parameter and an incorrect return value.
--

commit b3922d4aabf3e57f2fd4348625745b06244e4fe8
Author: Charles Keepax <[email protected]>

soundwire: Add a helper function to indicate a device will detach

This commit adds a helper function, sdw_slave_signal_unattach(), to indicate when a SoundWire device is about to detach due to a reset. This ensures the core reinitializes the completions so that sdw_slave_wait_for_init() correctly waits for the device to reattach.

Quoted 4 lines
include/linux/soundwire/sdw.h +0 -0
index 707ca6bd1e23d..969604755ce41 100644

[ ... ]

Quoted 4 lines

+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ * @timeout_us: Timeout in microseconds.

Quoted 8 lines

+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to
+ * return.
+ *
+ * Return: Zero on success, and a negative error code on failure.
+ */

[Severity: Low]
This isn't a bug, but the kernel-doc comment describes a @timeout_us parameter and a return value that do not exist in the inline function signature. Could these be removed to prevent kernel-doc warnings during W=1 builds?

include/linux/soundwire/sdw.h static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u · line 1243

Quoted 8 lines
include/linux/soundwire/sdw.h +8 -0
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);

[Severity: High]
Does this function also need to update slave->status to SDW_SLAVE_UNATTACHED?

If a transient hardware unattach is missed (e.g., due to interrupt coalescing or scheduling delays), the core might see the device reattach as device 0. When sdw_handle_slave_status() processes this, it calls sdw_program_device_num() which checks if the status is SDW_SLAVE_UNATTACHED.

If slave->status is left as SDW_SLAVE_ATTACHED, sdw_program_device_num() aborts without reprogramming the device ID. This leaves the device stuck on device 0, failing enumeration and causing sdw_slave_wait_for_init() to time out.

Quoted 5 lines
include/linux/soundwire/sdw.h +2 -0
+}
+
 /**
  * sdw_slave_wait_for_init - Wait for device initialisation
  * @slave: Pointer to the SoundWire peripheral.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/2] soundwire: Add a helper function to indicate a device will detach

Quoted 13 lines · On Mon, Aug 31, 2026 at 12:03:47PM +0000, [email protected] wrote:
Quoted 4 lines

+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ * @timeout_us: Timeout in microseconds.

Quoted 8 lines

+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to
+ * return.
+ *
+ * Return: Zero on success, and a negative error code on failure.
+ */

Quoted 4 lines

[Severity: Low]
This isn't a bug, but the kernel-doc comment describes a @timeout_us parameter and a return value that do not exist in the inline function signature. Could these be removed to prevent kernel-doc warnings during W=1 builds?

Oops, sorry must have been from an earlier version of the patch. Will get that fixed up for a v2.

Quoted 17 lines

include/linux/soundwire/sdw.h static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u · line 1243

Quoted 8 lines
include/linux/soundwire/sdw.h +8 -0
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);

[Severity: High]
Does this function also need to update slave->status to SDW_SLAVE_UNATTACHED?

If a transient hardware unattach is missed (e.g., due to interrupt coalescing or scheduling delays), the core might see the device reattach as device 0. When sdw_handle_slave_status() processes this, it calls sdw_program_device_num() which checks if the status is SDW_SLAVE_UNATTACHED.

Quoted 5 lines

If slave->status is left as SDW_SLAVE_ATTACHED, sdw_program_device_num() aborts without reprogramming the device ID. This leaves the device stuck on device 0, failing enumeration and causing sdw_slave_wait_for_init() to time out.

Potentially, however I would argue this is rather a separate issue. If we missed an unattach and don't reprogram the slave ID that is a much bigger problem.

I am less certain how possible the missing a detach is, that likely depends on the host hardware but if we do the problems run far deeper than just the wait for init function timing out. I am also a little unkeen on setting SDW_SLAVE_UNATTACHED, as the device hasn't actually unattached yet. It feels better to me if that is still linked to the hosts view of the state, the aim here is just to ensure we can't skip the wait.

Thanks,
Charles

#4 lore raw

PATCH 2/2 mfd: cs42l43: Move to core detach helper

In reply to Charles Keepax

Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.

---
 drivers/mfd/cs42l43-sdw.c   |  2 --
 drivers/mfd/cs42l43.c       | 15 +--------------
 include/linux/mfd/cs42l43.h |  1 -
 3 files changed, 1 insertion(+), 17 deletions(-)
drivers/mfd/cs42l43-sdw.c +0 -2
index 0a6999453f325..f0825d6bf7b38 100644
@@ -102,8 +102,6 @@ static int cs42l43_sdw_update_status(struct sdw_slave *sdw, enum sdw_slave_statu
 		break;
 	case SDW_SLAVE_UNATTACHED:
 		dev_dbg(cs42l43->dev, "Device detach\n");
-
-		complete(&cs42l43->device_detach);
 		break;
 	default:
 		break;
drivers/mfd/cs42l43.c +1 -14
index 4212ebcca60b5..f08ee598366d9 100644
@@ -30,7 +30,6 @@
 #define CS42L43_RESET_DELAY_MS			20
 
 #define CS42L43_SDW_ATTACH_TIMEOUT_MS		5000
-#define CS42L43_SDW_DETACH_TIMEOUT_MS		100
 
 #define CS42L43_MCU_BOOT_STAGE1			1
 #define CS42L43_MCU_BOOT_STAGE2			2
@@ -555,7 +554,7 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 		{ CS42L43_SFT_RESET, CS42L43_SFT_RESET_VAL },
 	};
 
-	reinit_completion(&cs42l43->device_detach);
+	sdw_slave_signal_unattach(cs42l43->sdw);
 
 	/*
 	 * Apply cache only because the soft reset will cause the device to
@@ -566,17 +565,6 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 
 	msleep(CS42L43_RESET_DELAY_MS);
 
-	if (cs42l43->sdw) {
-		unsigned long timeout = msecs_to_jiffies(CS42L43_SDW_DETACH_TIMEOUT_MS);
-		unsigned long time;
-
-		time = wait_for_completion_timeout(&cs42l43->device_detach, timeout);
-		if (!time) {
-			dev_err(cs42l43->dev, "Timed out waiting for device detach\n");
-			return -ETIMEDOUT;
-		}
-	}
-
 	return -EAGAIN;
 }
 
@@ -1115,7 +1103,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	dev_set_drvdata(cs42l43->dev, cs42l43);
 
 	mutex_init(&cs42l43->pll_lock);
-	init_completion(&cs42l43->device_detach);
 	init_completion(&cs42l43->firmware_download);
 	INIT_WORK(&cs42l43->boot_work, cs42l43_boot_work);
 
include/linux/mfd/cs42l43.h +0 -1
index 8e993fb535e68..798dc55149662 100644
@@ -86,7 +86,6 @@ struct cs42l43 {
 	struct regmap_irq_chip_data *irq_data;
 
 	struct work_struct boot_work;
-	struct completion device_detach;
 	struct completion firmware_download;
 	int firmware_error;
 
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH 2/2] mfd: cs42l43: Move to core detach helper

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Preexisting lifecycle bug: Devres items added by an asynchronous workqueue after devm teardown has started are permanently leaked. --

commit 28cea6eee264ba4b58e5dad35cea9b520bc94140
Author: Charles Keepax <[email protected]>

mfd: cs42l43: Move to core detach helper

This commit switches the cs42l43 driver to use a new SoundWire core helper for tracking device detachments instead of tracking it internally. It removes the custom device_detach completion.

Quoted 4 lines
drivers/mfd/cs42l43.c +0 -0
index 4212ebcca60b5..f08ee598366d9 100644

[ ... ]

drivers/mfd/cs42l43.c int cs42l43_dev_probe(struct cs42l43 *cs42l43) · line 1107

Quoted 7 lines
drivers/mfd/cs42l43.c +0 -1
@@ -1115,7 +1103,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	dev_set_drvdata(cs42l43->dev, cs42l43);
 
 	mutex_init(&cs42l43->pll_lock);
-	init_completion(&cs42l43->device_detach);
 	init_completion(&cs42l43->firmware_download);
 	INIT_WORK(&cs42l43->boot_work, cs42l43_boot_work);

[Severity: High]
This is a pre-existing issue, but is there a potential lifecycle memory leak and use-after-free here regarding the asynchronous boot_work?

Later in cs42l43_dev_probe(), cs42l43_dev_remove() is registered as a devm action via devm_add_action_or_reset(). If the driver is unbound shortly after probe (e.g., via sysfs unbind or device hot-unplug), the devm teardown will run and execute cs42l43_dev_remove(), which calls cancel_work_sync(&cs42l43->boot_work).

If cs42l43_boot_work() is concurrently running, cancel_work_sync() will block. Inside the work, devm_mfd_add_devices() is called, which adds new devres items to the device. Because devres_release_all() is already iterating over its captured list of items during the unbind, these newly added devm actions are skipped and permanently leaked.

This leaves the MFD child devices registered with pointers to a freed parent cs42l43 structure, which could result in a use-after-free if those children access their parent data.

Should the boot_work synchronization be moved out of the devres teardown path so it executes before devres_release_all() captures the release list?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH 2/2] mfd: cs42l43: Move to core detach helper

Quoted 27 lines · On Mon, Aug 31, 2026 at 12:03:59PM +0000, [email protected] wrote:
Quoted 2 lines
drivers/mfd/cs42l43.c +0 -0

[ ... ]

drivers/mfd/cs42l43.c int cs42l43_dev_probe(struct cs42l43 *cs42l43) · line 1107

Quoted 7 lines
drivers/mfd/cs42l43.c +0 -1
@@ -1115,7 +1103,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	dev_set_drvdata(cs42l43->dev, cs42l43);
 
 	mutex_init(&cs42l43->pll_lock);
-	init_completion(&cs42l43->device_detach);
 	init_completion(&cs42l43->firmware_download);
 	INIT_WORK(&cs42l43->boot_work, cs42l43_boot_work);

[Severity: High]
This is a pre-existing issue, but is there a potential lifecycle memory leak and use-after-free here regarding the asynchronous boot_work?

Later in cs42l43_dev_probe(), cs42l43_dev_remove() is registered as a devm action via devm_add_action_or_reset(). If the driver is unbound shortly after probe (e.g., via sysfs unbind or device hot-unplug), the devm teardown will run and execute cs42l43_dev_remove(), which calls cancel_work_sync(&cs42l43->boot_work).

If cs42l43_boot_work() is concurrently running, cancel_work_sync() will block. Inside the work, devm_mfd_add_devices() is called, which adds new devres items to the device. Because devres_release_all() is already iterating over its captured list of items during the unbind, these newly added devm actions are skipped and permanently leaked.

Quoted 6 lines

This leaves the MFD child devices registered with pointers to a freed parent cs42l43 structure, which could result in a use-after-free if those children access their parent data.

Should the boot_work synchronization be moved out of the devres teardown path so it executes before devres_release_all() captures the release list?

This does sound like a legit issue, although probably pretty hard to hit in practice. I will have a think about a patch for this, although clearly separate from this chain.

Thanks,
Charles