mfd: da903x: cancel IRQ work during teardown
PATCH 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.
Add a devm action after requesting the IRQ. The action disables the IRQ and cancels the work before automatic IRQ release. Run the same action before removing child devices on normal detach.
This issue was found by a static analysis tool.
--- drivers/mfd/da903x.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/mfd/da903x.c +14 -0
@@ -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,
@@ -529,6 +537,11 @@ static int da903x_probe(struct i2c_client *client)return ret; } + ret =devm_add_action_or_reset (&client->dev, da903x_cancel_irq_work, + chip); +if (ret) +return ret; +return da903x_add_subdevs (chip, pdata); }
@@ -536,6 +549,7 @@ static void da903x_remove(struct i2c_client *client) {struct da903x_chip *chip =i2c_get_clientdata (client); +devm_release_action (&client->dev, da903x_cancel_irq_work, chip);da903x_remove_subdevs (chip); }
Signature · 1 line
2.50.1.windows.1
Lee Jones
maintainer/* Sashiko Automation: Issues Found (2 Findings) */
Please review, explain or fix the Sashiko review(s).
If this is a set, this request includes the other patches too.
Quoted 35 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.Add a devm action after requesting the IRQ. The action disables the IRQ and cancels the work before automatic IRQ release. Run the same action before removing child devices on normal detach.
This issue was found by a static analysis tool.
Signed-off-by Hongyan Xu <[email protected]>--- drivers/mfd/da903x.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/mfd/da903x.c +8
-0@@ -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,
drivers/mfd/da903x.c static int da903x_probe(struct i2c_client *client) · line 546
Quoted 10 lines
drivers/mfd/da903x.c +5
-0@@ -529,6 +537,11 @@ static int da903x_probe(struct i2c_client *client)return ret; } + ret =devm_add_action_or_reset (&client->dev, da903x_cancel_irq_work, + chip); +if (ret) +return ret; +return da903x_add_subdevs (chip, pdata); }
[Severity: High]
If da903x_add_subdevs() fails, it cleans up the subdevices internally before returning an error, leaving a window where the IRQ and workqueue are still active.
da903x_add_subdevs() handles errors like this:
failed:
da903x_remove_subdevs(chip);
return ret;When the error is returned to da903x_probe(), the devres unwinding will eventually cancel the IRQ work, but the subdevices have already been removed.
Since the teardown logic in da903x_remove() explicitly cancels the IRQ work before removing subdevices, is there a similar race condition during a probe failure?
Quoted 11 lines
drivers/mfd/da903x.c +1
-0@@ -536,6 +549,7 @@ static void da903x_remove(struct i2c_client *client) {struct da903x_chip *chip =i2c_get_clientdata (client); +devm_release_action (&client->dev, da903x_cancel_irq_work, chip);da903x_remove_subdevs (chip); }Signature · 1 line
2.50.1.windows.1
Signature · 1 line
Lee Jones
PATCH v2 1/1 mfd: da903x: cancel IRQ work during teardown
In reply to Hongyan XuThe 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.
Add a devm action after requesting the IRQ. The action disables the IRQ and cancels the work before automatic IRQ release. Run the same action before removing child devices on normal detach.
If da903x_add_subdevs() fails, it removes already-created child devices before returning from probe. Release the action explicitly before that cleanup so the work is stopped before child-device teardown.
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.
--- drivers/mfd/da903x.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/mfd/da903x.c +15 -0
@@ -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,
@@ -484,6 +492,7 @@ static int da903x_add_subdevs(struct da903x_chip *chip,return 0 ; failed: +devm_release_action (chip->dev, da903x_cancel_irq_work, chip);da903x_remove_subdevs (chip);return ret; }
@@ -529,6 +538,11 @@ static int da903x_probe(struct i2c_client *client)return ret; } + ret =devm_add_action_or_reset (&client->dev, da903x_cancel_irq_work, + chip); +if (ret) +return ret; +return da903x_add_subdevs (chip, pdata); }
@@ -536,6 +550,7 @@ static void da903x_remove(struct i2c_client *client) {struct da903x_chip *chip =i2c_get_clientdata (client); +devm_release_action (&client->dev, da903x_cancel_irq_work, chip);da903x_remove_subdevs (chip); }
Signature · 1 line
2.50.1.windows.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Low] The commit message description starts with a lowercase letter, violating MFD subsystem guidelines.
Pre-existing issues:
- [High] Missing NULL check for platform data (pdata) leads to a kernel oops if the device is instantiated without it. --
--- Patch [1]: [PATCH v2] mfd: da903x: cancel IRQ work during teardown --- commit 2a17d672041aff8f9dbc3cb91964d70e165a4c40
Author: Hongyan Xu <[email protected]>
mfd: da903x: cancel IRQ work during teardown
This commit adds a devm action to disable the IRQ and cancel the scheduled IRQ work during teardown. It also ensures the action is properly released if child device creation fails during probe.
Quoted 1 line
mfd: da903x: cancel IRQ work during teardown
[Severity: Low]
This isn't a bug, but MFD subsystem guidelines require the description after the subsystem prefix to be capitalized. Should "cancel" be capitalized here?
Quoted 4 lines
drivers/mfd/da903x.c +0
-0
[ ... ]
drivers/mfd/da903x.c static int da903x_probe(struct i2c_client *client) · line 546
Quoted 10 lines
drivers/mfd/da903x.c +5
-0@@ -529,6 +538,11 @@ static int da903x_probe(struct i2c_client *client)return ret; } + ret =devm_add_action_or_reset (&client->dev, da903x_cancel_irq_work, + chip); +if (ret) +return ret; +return da903x_add_subdevs (chip, pdata);
[Severity: High]
This is a pre-existing issue, but can passing pdata here cause a NULL pointer dereference if the device is instantiated without platform data?
If the driver is instantiated via the i2c sysfs interface without platform data, dev_get_platdata() returns NULL. This is then passed directly into da903x_add_subdevs(), which blindly dereferences it:
da903x_probe()->da903x_add_subdevs()
for (i = 0; i < pdata->num_subdevs; i++) {
subdev = &pdata->subdevs[i];Could we add a NULL check for pdata in da903x_probe() to prevent a kernel oops?
Quoted 3 lines
drivers/mfd/da903x.c +0
-0}@@ -536,6 +550,7 @@ static void da903x_remove(struct i2c_client *client)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Lee Jones
maintainerQuoted 43 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.Add a devm action after requesting the IRQ. The action disables the IRQ and cancels the work before automatic IRQ release. Run the same action before removing child devices on normal detach.
If
da903x_add_subdevs()fails, it removes already-created child devices before returning from probe. Release the action explicitly before that cleanup so the work is stopped before child-device teardown.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-5Signed-off-by Hongyan Xu <[email protected]>--- drivers/mfd/da903x.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/mfd/da903x.c +8
-0@@ -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,
drivers/mfd/da903x.c static int da903x_add_subdevs(struct da903x_chip *chip, · line 495
Quoted 4 lines
drivers/mfd/da903x.c +1
-0@@ -484,6 +492,7 @@ static int da903x_add_subdevs(struct da903x_chip *chip,return 0 ; failed: +devm_release_action (chip->dev, da903x_cancel_irq_work, chip);
Manually calling 'devm_release_action' here and in 'da903x_remove' somewhat defeats the purpose of using managed resources. Should we instead register the subdevice removal as a devm action as well?
If we register the subdevices' cleanup as a devm action before the IRQ cancel action, devres will execute them in reverse order during cleanup. This would allow us to eliminate the '.remove' callback entirely.
drivers/mfd/da903x.c static int da903x_probe(struct i2c_client *client) · line 545
Quoted 12 lines
drivers/mfd/da903x.c +5
-0da903x_remove_subdevs (chip);return ret; }@@ -529,6 +538,11 @@ static int da903x_probe(struct i2c_client *client)return ret; } + ret =devm_add_action_or_reset (&client->dev, da903x_cancel_irq_work, + chip); +if (ret) +return ret; +
If we transition to using a devm action for the subdevice removal, we could call 'da903x_add_subdevs' first, register its cleanup action, and then register 'da903x_cancel_irq_work'. This would guarantee that the IRQ work is cancelled before the subdevices are removed during any unwinding or driver removal.
drivers/mfd/da903x.c static void da903x_remove(struct i2c_client *client) · line 553
Quoted 8 lines
drivers/mfd/da903x.c +1
-0return da903x_add_subdevs (chip, pdata); }@@ -536,6 +550,7 @@ static void da903x_remove(struct i2c_client *client) {struct da903x_chip *chip =i2c_get_clientdata (client); +devm_release_action (&client->dev, da903x_cancel_irq_work, chip);
With both cleanup steps handled by devres in the correct order, we should be able to remove 'da903x_remove' entirely, simplifying the driver.
Quoted 6 lines
drivers/mfd/da903x.c +0
-0da903x_remove_subdevs (chip); }Signature · 1 line
2.50.1.windows.1
Signature · 1 line
Lee Jones