Skip to contentSkip to conversation

mfd: cs42l43: Move cancel_work_sync() into remove

PATCH Superseded Charles Keepax 2 participants 3 messages Sep 4

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: cs42l43: Move cancel_work_sync() into remove

The cs42l43 driver kicks off a work item waiting for the device to appear on the SoundWire bus, once the device appears the MFD children are added and the clean up is then done through devres. As the work is synchronised in from the devres clean up this could lead to new devres items being added after the clean up has started, if the driver is removed before the work has fully completed. The work should be synchronised before any devres clean up is started.

Fixes 0f35dc4bd50d ("mfd: cs42l43: Use devres for remove as well")
---
 drivers/mfd/cs42l43-i2c.c |  8 ++++++++
 drivers/mfd/cs42l43-sdw.c |  8 ++++++++
 drivers/mfd/cs42l43.c     | 14 +++++++++++---
 drivers/mfd/cs42l43.h     |  1 +
 4 files changed, 28 insertions(+), 3 deletions(-)
drivers/mfd/cs42l43-i2c.c +8 -0
index cbe05c3ea9100..36c5b03bae693 100644
@@ -54,6 +54,13 @@ static int cs42l43_i2c_probe(struct i2c_client *i2c)
 	return cs42l43_dev_probe(cs42l43);
 }
 
+static void cs42l43_i2c_remove(struct i2c_client *i2c)
+{
+	struct cs42l43 *cs42l43 = dev_get_drvdata(&i2c->dev);
+
+	cs42l43_dev_remove(cs42l43);
+}
+
 #if IS_ENABLED(CONFIG_OF)
 static const struct of_device_id cs42l43_of_match[] = {
 	{ .compatible = "cirrus,cs42l43", .data = (void *)CS42L43_DEVID_VAL },
@@ -81,6 +88,7 @@ static struct i2c_driver cs42l43_i2c_driver = {
 	},
 
 	.probe		= cs42l43_i2c_probe,
+	.remove		= cs42l43_i2c_remove,
 };
 module_i2c_driver(cs42l43_i2c_driver);
 
drivers/mfd/cs42l43-sdw.c +8 -0
index 0a6999453f325..afed34d23af80 100644
@@ -180,6 +180,13 @@ static int cs42l43_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *
 	return cs42l43_dev_probe(cs42l43);
 }
 
+static void cs42l43_sdw_remove(struct sdw_slave *sdw)
+{
+	struct cs42l43 *cs42l43 = dev_get_drvdata(&sdw->dev);
+
+	cs42l43_dev_remove(cs42l43);
+}
+
 static const struct sdw_device_id cs42l43_sdw_id[] = {
 	SDW_SLAVE_ENTRY(0x01FA, 0x4243, (void *)CS42L43_DEVID_VAL),
 	SDW_SLAVE_ENTRY(0x01FA, 0x2A3B, (void *)CS42L43B_DEVID_VAL),
@@ -194,6 +201,7 @@ static struct sdw_driver cs42l43_sdw_driver = {
 	},
 
 	.probe		= cs42l43_sdw_probe,
+	.remove		= cs42l43_sdw_remove,
 	.id_table	= cs42l43_sdw_id,
 	.ops		= &cs42l43_sdw_ops,
 };
drivers/mfd/cs42l43.c +11 -3
index 4212ebcca60b5..ccb68f7ebb5a4 100644
@@ -1099,14 +1099,22 @@ static int cs42l43_power_down(struct cs42l43 *cs42l43)
 	return 0;
 }
 
-static void cs42l43_dev_remove(void *data)
+static void cs42l43_dev_power_down(void *data)
 {
 	struct cs42l43 *cs42l43 = data;
 
+	cs42l43_power_down(cs42l43);
+}
+
+void cs42l43_dev_remove(struct cs42l43 *cs42l43)
+{
 	cancel_work_sync(&cs42l43->boot_work);
 
-	cs42l43_power_down(cs42l43);
+	/* If the work never ran drop the pm_runtime reference from probe. */
+	if (!cs42l43->irq_chip.irq_drv_data)
+		pm_runtime_put_sync(cs42l43->dev);
 }
+EXPORT_SYMBOL_NS_GPL(cs42l43_dev_remove, "MFD_CS42L43");
 
 int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 {
@@ -1153,7 +1161,7 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	if (ret)
 		return ret;
 
-	ret = devm_add_action_or_reset(cs42l43->dev, cs42l43_dev_remove, cs42l43);
+	ret = devm_add_action_or_reset(cs42l43->dev, cs42l43_dev_power_down, cs42l43);
 	if (ret)
 		return ret;
 
drivers/mfd/cs42l43.h +1 -0
index a0068f6572e2c..862bc6fb68cb7 100644
@@ -25,5 +25,6 @@ bool cs42l43_precious_register(struct device *dev, unsigned int reg);
 bool cs42l43_volatile_register(struct device *dev, unsigned int reg);
 
 int cs42l43_dev_probe(struct cs42l43 *cs42l43);
+void cs42l43_dev_remove(struct cs42l43 *cs42l43);
 
 #endif /* CS42L43_CORE_INT_H */
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Double pm_runtime_put_sync() call leading to usage counter underflow when boot_work fails early. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Double pm_runtime_put_sync() call leading to usage counter underflow when boot_work fails early. --

commit 2dc71c160f47bf4867fee4856b9a979eea19562c
Author: Charles Keepax <[email protected]>

mfd: cs42l43: Move cancel_work_sync() into remove

This commit moves the synchronization of the boot_work item out of devres cleanup and into a dedicated remove function. It also adds a heuristic check to drop the runtime PM reference taken during probe if the work never ran.

Quoted 4 lines
drivers/mfd/cs42l43.c +0 -0
index 039754e6eaa09..480447bc80ed9 100644

[ ... ]

Quoted 1 line
drivers/mfd/cs42l43.c +0 -0
@@ -1100,14 +1100,22 @@ static int cs42l43_power_down(struct cs42l43 *cs42l43)

[ ... ]

Quoted 6 lines
drivers/mfd/cs42l43.c +2 -1
-static void cs42l43_dev_remove(void *data)
+static void cs42l43_dev_power_down(void *data)
 {
 	struct cs42l43 *cs42l43 = data;
 
+	cs42l43_power_down(cs42l43);

drivers/mfd/cs42l43.c static int cs42l43_power_down(struct cs42l43 *cs42l43) · line 1115

Quoted 10 lines
drivers/mfd/cs42l43.c +7 -1
+}
+
+void cs42l43_dev_remove(struct cs42l43 *cs42l43)
+{
 	cancel_work_sync(&cs42l43->boot_work);
 
-	cs42l43_power_down(cs42l43);
+	/* If the work never ran drop the pm_runtime reference from probe. */
+	if (!cs42l43->irq_chip.irq_drv_data)
+		pm_runtime_put_sync(cs42l43->dev);

[Severity: High]
Can this heuristic cause a double pm_runtime_put_sync() call if the work executes but fails early?

If cs42l43_boot_work() runs but hits an early failure (for instance, a timeout or a device ID mismatch), the error path in the work item already handles dropping the PM reference and exits:

drivers/mfd/cs42l43.c:cs42l43_boot_work() {
    ...
err:
    pm_runtime_put_sync(cs42l43->dev);
}

Because the work failed early, cs42l43->irq_chip.irq_drv_data remains NULL.

If the driver is later unbound, cs42l43_dev_remove() will run. cancel_work_sync() will complete successfully since the work already finished, and this heuristic (!cs42l43->irq_chip.irq_drv_data) will evaluate to true.

Will this incorrectly drop the PM runtime reference a second time, causing a usage counter underflow?

Quoted 2 lines

}
+EXPORT_SYMBOL_NS_GPL(cs42l43_dev_remove, "MFD_CS42L43");

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 23 lines · On Fri, Sep 04, 2026 at 10:49:03AM +0000, [email protected] wrote:

drivers/mfd/cs42l43.c static int cs42l43_power_down(struct cs42l43 *cs42l43) · line 1115

Quoted 8 lines
drivers/mfd/cs42l43.c +5 -1
+void cs42l43_dev_remove(struct cs42l43 *cs42l43)
+{
 	cancel_work_sync(&cs42l43->boot_work);
 
-	cs42l43_power_down(cs42l43);
+	/* If the work never ran drop the pm_runtime reference from probe. */
+	if (!cs42l43->irq_chip.irq_drv_data)
+		pm_runtime_put_sync(cs42l43->dev);

[Severity: High]
Can this heuristic cause a double pm_runtime_put_sync() call if the work executes but fails early?

If cs42l43_boot_work() runs but hits an early failure (for instance, a timeout or a device ID mismatch), the error path in the work item already handles dropping the PM reference and exits:

drivers/mfd/cs42l43.c:cs42l43_boot_work() {
    ...
err:
    pm_runtime_put_sync(cs42l43->dev);
}
Quoted 8 lines

Because the work failed early, cs42l43->irq_chip.irq_drv_data remains NULL.

If the driver is later unbound, cs42l43_dev_remove() will run. cancel_work_sync() will complete successfully since the work already finished, and this heuristic (!cs42l43->irq_chip.irq_drv_data) will evaluate to true.

Will this incorrectly drop the PM runtime reference a second time, causing a usage counter underflow?

Yeah that is fair, I will think this through a little more.

Thanks,
Charles