Skip to contentSkip to conversation

mfd: da903x: cancel IRQ work during teardown

PATCH Applied Hongyan Xu 3 participants 3 messages Aug 14 – Sep 2

Subsystems (3)
lore
Share & tools
lore

Hongyan Xu

author
#0 lore raw

PATCH v3 1/1 mfd: da903x: cancel IRQ work during teardown

The IRQ handler disables the IRQ and schedules irq_work. Releasing the IRQ does not drain that work, which can continue to use the devm-allocated chip and notifier state.

Manage both IRQ-work cancellation and subdevice removal with devres. Register the subdevice-removal action before requesting the IRQ, then add the IRQ-work action after the request. This avoids leaving a live IRQ without work cancellation if action registration fails. Devres cleanup then disables the IRQ and drains the work before releasing the IRQ and removing child devices. Register both actions before creating the children so partial probe failures use the same ordering.

This issue was found by the author's in-house static analysis tool. The patch was reviewed by the author against the latest mainline tree.

Fixes 26b8f5e1e2d1 ("mfd: add base support for Dialog DA9030/DA9034 PMICs")
Assisted-by Codex:GPT-5

---
Changes in v3:
- Manage subdevice removal with devres and drop the remove callback.
- Register its action before IRQ acquisition, then register IRQ-work cancellation after it, covering action and partial-probe failures.

 drivers/mfd/da903x.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)
drivers/mfd/da903x.c +25 -9
index e86b39de3303..e4ad48e1f139 100644
@@ -421,6 +421,14 @@ static irqreturn_t da903x_irq_handler(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+static void da903x_cancel_irq_work(void *data)
+{
+	struct da903x_chip *chip = data;
+
+	disable_irq(chip->client->irq);
+	cancel_work_sync(&chip->irq_work);
+}
+
 static const struct da903x_chip_ops da903x_ops[] = {
 	[0] = {
 		.init_chip	= da9030_init_chip,
@@ -456,6 +464,13 @@ static int da903x_remove_subdevs(struct da903x_chip *chip)
 	return device_for_each_child(chip->dev, NULL, __remove_subdev);
 }
 
+static void da903x_remove_subdevs_action(void *data)
+{
+	struct da903x_chip *chip = data;
+
+	da903x_remove_subdevs(chip);
+}
+
 static int da903x_add_subdevs(struct da903x_chip *chip,
 					struct da903x_platform_data *pdata)
 {
@@ -484,7 +499,6 @@ static int da903x_add_subdevs(struct da903x_chip *chip,
 	return 0;
 
 failed:
-	da903x_remove_subdevs(chip);
 	return ret;
 }
 
@@ -520,6 +534,11 @@ static int da903x_probe(struct i2c_client *client)
 	chip->ops->mask_events(chip, chip->events_mask);
 	chip->ops->read_events(chip, &tmp);
 
+	ret = devm_add_action_or_reset(&client->dev,
+				       da903x_remove_subdevs_action, chip);
+	if (ret)
+		return ret;
+
 	ret = devm_request_irq(&client->dev, client->irq, da903x_irq_handler,
 			IRQF_TRIGGER_FALLING,
 			"da903x", chip);
@@ -529,14 +548,12 @@ static int da903x_probe(struct i2c_client *client)
 		return ret;
 	}
 
-	return da903x_add_subdevs(chip, pdata);
-}
-
-static void da903x_remove(struct i2c_client *client)
-{
-	struct da903x_chip *chip = i2c_get_clientdata(client);
+	ret = devm_add_action_or_reset(&client->dev, da903x_cancel_irq_work,
+				       chip);
+	if (ret)
+		return ret;
 
-	da903x_remove_subdevs(chip);
+	return da903x_add_subdevs(chip, pdata);
 }
 
 static struct i2c_driver da903x_driver = {
@@ -544,7 +561,6 @@ static struct i2c_driver da903x_driver = {
 		.name	= "da903x",
 	},
 	.probe		= da903x_probe,
-	.remove		= da903x_remove,
 	.id_table	= da903x_id_table,
 };
 
Signature · 1 line
2.50.1.windows.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

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

New issues:
- [Low] The commit message description string is not capitalized.

Pre-existing issues:
- [High] Missing NULL check for pdata in probe path leads to kernel Oops.
- [High] Data race on chip->events_mask leading to lost interrupt masks. --

--- Patch [1]: [PATCH v3] mfd: da903x: cancel IRQ work during teardown --- commit d86788370ab7658c6e3b5684327f0a0b0169e280
Author: Hongyan Xu <[email protected]>

mfd: da903x: cancel IRQ work during teardown

The IRQ handler disables the IRQ and schedules irq_work. Releasing the IRQ does not drain that work, which can continue to use the devm-allocated chip and notifier state. Manage both IRQ-work cancellation and subdevice removal with devres.

Quoted 1 line

mfd: da903x: cancel IRQ work during teardown

[Severity: Low]
This isn't a bug, but there's a minor formatting issue in the commit message. The MFD subsystem guidelines require capitalizing the description after the subsystem prefix.

Could the commit message be updated to use 'Cancel' instead of 'cancel' to match these conventions?

Quoted 4 lines
drivers/mfd/da903x.c +0 -0
index e86b39de3303a..e4ad48e1f139b 100644

[ ... ]

drivers/mfd/da903x.c static int da903x_probe(struct i2c_client *client) · line 535

Quoted 3 lines
drivers/mfd/da903x.c +0 -0
@@ -520,6 +534,11 @@ static int da903x_probe(struct i2c_client *client)
 	chip->ops->mask_events(chip, chip->events_mask);
 	chip->ops->read_events(chip, &tmp);

[Severity: High]
This is a pre-existing issue, but I noticed a potential data race involving updates to chip->events_mask.

The exported functions da903x_register_notifier() and da903x_unregister_notifier() call da9030_unmask_events() and da9030_mask_events().

These functions perform non-atomic read-modify-write operations without holding chip->lock:

drivers/mfd/da903x.c:da9030_unmask_events() {
    ...
    chip->events_mask &= ~events;
    ...
}

If multiple child drivers (like backlight or regulator) are probed or removed concurrently, could these bitwise updates race and overwrite each other's changes?

Would this lead to lost updates to the interrupt mask, causing some child devices to permanently miss interrupts?

Quoted 4 lines
drivers/mfd/da903x.c +4 -0
+	ret = devm_add_action_or_reset(&client->dev,
+				       da903x_remove_subdevs_action, chip);
+	if (ret)
+		return ret;

[ ... ]

Quoted 8 lines
drivers/mfd/da903x.c +0 -4
@@ -529,14 +548,12 @@ static int da903x_probe(struct i2c_client *client)
 		return ret;
 	}
 
-	return da903x_add_subdevs(chip, pdata);
-}
-
-static void da903x_remove(struct i2c_client *client)

drivers/mfd/da903x.c static int da903x_probe(struct i2c_client *client) · line 557

Quoted 10 lines
drivers/mfd/da903x.c +5 -3
-{
-	struct da903x_chip *chip = i2c_get_clientdata(client);
+	ret = devm_add_action_or_reset(&client->dev, da903x_cancel_irq_work,
+				       chip);
+	if (ret)
+		return ret;
 
-	da903x_remove_subdevs(chip);
+	return da903x_add_subdevs(chip, pdata);
 }

[Severity: High]
This is a pre-existing issue, but I noticed a potential null pointer dereference when passing pdata here.

If the I2C device is dynamically instantiated via the sysfs new_device interface or if platform code fails to attach platform data, dev_get_platdata(&client->dev) returns NULL.

This null pointer is then passed unconditionally to da903x_add_subdevs(), which dereferences it immediately:

drivers/mfd/da903x.c:da903x_add_subdevs() {
    ...
    for (i = 0; i < pdata->num_subdevs; i++) {
        subdev = &pdata->subdevs[i];
    ...
}

Could this lead to a kernel oops triggered by privileged userspace interacting with sysfs?

Should we add a null check for pdata before creating subdevices?

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

Re: (subset) [PATCH v3] mfd: da903x: cancel IRQ work during teardown

In reply to Hongyan Xu
Quoted 12 lines · Hongyan Xu

The IRQ handler disables the IRQ and schedules irq_work. Releasing the IRQ does not drain that work, which can continue to use the devm-allocated chip and notifier state.

Manage both IRQ-work cancellation and subdevice removal with devres. Register the subdevice-removal action before requesting the IRQ, then add the IRQ-work action after the request. This avoids leaving a live IRQ without work cancellation if action registration fails. Devres cleanup then disables the IRQ and drains the work before releasing the IRQ and removing child devices. Register both actions before creating the children so partial probe failures use the same ordering.

Quoted 1 line

[...]

Applied, thanks!

[1/1] mfd: da903x: cancel IRQ work during teardown

      commit: df41eb0718d7ec4609c98844aca3ea381bb569ef

--
Lee Jones [李琼斯]