iio: light: gp2ap002: runtime PM and error path fixes
Subsystems (2)
cover iio: light: gp2ap002: runtime PM and error path fixes
Hi,
This series fixes three issues in the gp2ap002 driver, all present since the driver was introduced.
Patch 1 fixes an unbalanced runtime PM reference count reachable from userspace by writing the event enable attribute twice with the same value, as the IIO core does not deduplicate such writes.
Patch 2 fixes the runtime suspend error path leaving the IRQ disable depth unbalanced, permanently disabling proximity events after a failed suspend.
Patch 3 fixes regulator enable leaks in the runtime resume error paths, which matters as the supplies may be shared with other devices.
Regards,
Nikhil Gautam
Nikhil Gautam (3):
iio: light: gp2ap002: Fix unbalanced runtime PM on repeated event
writes
iio: light: gp2ap002: re-enable irq if runtime suspend fails
iio: light: gp2ap002: Fix regulator leaks in runtime resume error path
drivers/iio/light/gp2ap002.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/3 iio: light: gp2ap002: Fix unbalanced runtime PM on repeated event writes
The IIO core does not filter duplicate writes to the event enable attribute, so writing the same value twice invokes write_event_config() twice. Enabling twice leaks a runtime PM reference, preventing the device from ever suspending again; disabling twice underflows the usage count and triggers a "Runtime PM usage count underflow" warning.
Bail out early when the requested state matches the current state. While at it, switch to pm_runtime_resume_and_get() so a failed resume is propagated to userspace instead of silently marking the event enabled.
--- drivers/iio/light/gp2ap002.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/iio/light/gp2ap002.c +9 -3
@@ -343,6 +343,10 @@ static int gp2ap002_write_event_config(struct iio_dev *indio_dev, bool state) {struct gp2ap002 *gp2ap002 =iio_priv (indio_dev); +int ret; + +if (state == gp2ap002->enabled) +return 0 ;if (state) {/*
@@ -350,14 +354,16 @@ static int gp2ap002_write_event_config(struct iio_dev *indio_dev,* already) and reintialize the sensor by using runtime_pm * callbacks. */ -pm_runtime_get_sync (gp2ap002->dev); - gp2ap002->enabled = true; + + ret =pm_runtime_resume_and_get (gp2ap002->dev); +if (ret) +return ret; }else {pm_runtime_mark_last_busy (gp2ap002->dev);pm_runtime_put_autosuspend (gp2ap002->dev); - gp2ap002->enabled = false; } + gp2ap002->enabled = state;return 0 ; }
Signature · 1 line
2.39.5
Re: [PATCH v1 1/3] iio: light: gp2ap002: Fix unbalanced runtime PM on repeated event writes
Quoted 7 lines · Nikhil Gautam
The IIO core does not filter duplicate writes to the event enable attribute, so writing the same value twice invokes
write_event_config()twice. Enabling twice leaks a runtime PM reference, preventing the device from ever suspending again; disabling twice underflows the usage count and triggers a "Runtime PM usage count underflow" warning.
Quoted 8 lines
Bail out early when the requested state matches the current state. While at it, switch to
pm_runtime_resume_and_get()so a failed resume is propagated to userspace instead of silently marking the event enabled.Fixes 97d642e23037c ("iio: light: Add a driver for Sharp GP2AP002x00F")Signed-off-by Nikhil Gautam <[email protected]>
No blank lines in the tag block.
...
drivers/iio/light/gp2ap002.c static int gp2ap002_write_event_config(struct iio_dev *indio_dev, · line 362
Quoted 2 lines
drivers/iio/light/gp2ap002.c +0
-0}else {pm_runtime_mark_last_busy (gp2ap002->dev);
At some point consider dropping this dup call.
Quoted 3 lines
drivers/iio/light/gp2ap002.c +0
-1pm_runtime_put_autosuspend (gp2ap002->dev); - gp2ap002->enabled = false; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 1/3] iio: light: gp2ap002: Fix unbalanced runtime PM on repeated event writes
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Nikhil Gautam
The IIO core does not filter duplicate writes to the event enable attribute, so writing the same value twice invokes
write_event_config()twice. Enabling twice leaks a runtime PM reference, preventing the device from ever suspending again; disabling twice underflows the usage count and triggers a "Runtime PM usage count underflow" warning.
Quoted 8 lines
Quoted 7 lines
Bail out early when the requested state matches the current state. While at it, switch to
pm_runtime_resume_and_get()so a failed resume is propagated to userspace instead of silently marking the event enabled.Fixes 97d642e23037c ("iio: light: Add a driver for Sharp GP2AP002x00F")Signed-off-by Nikhil Gautam <[email protected]>No blank lines in the tag block.
ACK, Will update in v2
Quoted 5 lines
...
Quoted 2 lines
} else { pm_runtime_mark_last_busy(gp2ap002->dev);At some point consider dropping this dup call.
ACK, Will update in v2
Quoted 3 lines
Quoted 3 lines
pm_runtime_put_autosuspend(gp2ap002->dev);drivers/iio/light/gp2ap002.c +0
-1- gp2ap002->enabled = false; }
PATCH 2/3 iio: light: gp2ap002: re-enable irq if runtime suspend fails
In reply to Nikhil Gautamgp2ap002_runtime_suspend() disables the irq before writing OPMOD. If the write fails, the callback returns an error with the irq still disabled while the PM core marks the device active again.
re-enable the irq before returning the error so the irq state matches the active state the PM core restores.
--- drivers/iio/light/gp2ap002.c | 1 + 1 file changed, 1 insertion(+)
drivers/iio/light/gp2ap002.c +1 -0
@@ -650,6 +650,7 @@ static int gp2ap002_runtime_suspend(struct device *dev)/* Disable chip and IRQ, everything off */ ret =regmap_write (gp2ap002->map,GP2AP002_OPMOD ,0x00 );if (ret) { +enable_irq (gp2ap002->irq);dev_err (gp2ap002->dev,"error setting up operation mode\n" );return ret; }
Signature · 1 line
2.39.5
PATCH 3/3 iio: light: gp2ap002: Fix regulator leaks in runtime resume error path
In reply to Nikhil GautamIf enabling vio fails, vdd is left enabled; if re-initializing the sensor fails, both regulators are left enabled.
unwind previously enabled regulators on failure, mirroring the error handling already used in probe.
--- drivers/iio/light/gp2ap002.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/light/gp2ap002.c +8 -2
@@ -678,7 +678,7 @@ static int gp2ap002_runtime_resume(struct device *dev) ret =regulator_enable (gp2ap002->vio);if (ret) {dev_err (dev,"failed to enable VIO regulator in resume path\n" ); -return ret; + goto out_disable_vdd; }msleep (20 );
@@ -686,13 +686,19 @@ static int gp2ap002_runtime_resume(struct device *dev) ret =gp2ap002_init (gp2ap002);if (ret) {dev_err (dev,"re-initialization failed\n" ); -return ret; + goto out_disable_vio; }/* Re-activate the IRQ */ enable_irq (gp2ap002->irq);return 0 ; + +out_disable_vio: +regulator_disable (gp2ap002->vio); +out_disable_vdd: +regulator_disable (gp2ap002->vdd); +return ret; } static DEFINE_RUNTIME_DEV_PM_OPS(gp2ap002_dev_pm_ops, gp2ap002_runtime_suspend,
Signature · 1 line
2.39.5