regulator: core: Clean up on enable failure
PATCH 1/1 regulator: core: Clean up on enable failure
If regulator_enable() fails, enable_count is incremented still. A consumer, assuming no matching regulator_disable() is necessary on failure, will then get this error message upon regulator_put() since enable_count is non-zero:
[ 1.277418] WARNING: CPU: 3 PID: 1 at drivers/regulator/core.c:2304 _regulator_put.part.0+0x168/0x170
The consumer could try to fix this in their driver by cleaning up on error from regulator_enable() (i.e. call regulator_disable()), but that results in the following since regulator_enable() failed and didn't increment user_count:
[ 1.258112] unbalanced disables for vreg_l17c
[ 1.262606] WARNING: CPU: 4 PID: 1 at drivers/regulator/core.c:2899 _regulator_disable+0xd4/0x190Fix this by decrementing enable_count upon failure to enable.
With this in place, just the reason for failure to enable is printed as expected and developers can focus on the root cause of their issue instead of thinking their usage of the regulator consumer api is incorrect. For example, in my case:
[ 1.240426] vreg_l17c: invalid input voltage found
---
I'm new to using the regulator framework, but I _believe_ this is a cosmetic bug that's fixed by this patch. I went down a bit of a rabbit hole because of the original WARN() message, so I'm trying to prevent others from doing the same :)
Please let me know what you think, I tested on the misconfigured system and on a working system and things seemed to work as expected.
Thanks,
Andrew
drivers/regulator/core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/regulator/core.c +7 -2
@@ -2733,13 +2733,18 @@ static int _regulator_do_enable(struct regulator_dev *rdev)*/ static int _regulator_handle_consumer_enable (struct regulator *regulator) { +int ret;struct regulator_dev *rdev = regulator->rdev;lockdep_assert_held_once (&rdev->mutex.base); regulator->enable_count++; -if (regulator->uA_load && regulator->enable_count ==1 ) -return drms_uA_update (rdev); +if (regulator->uA_load && regulator->enable_count ==1 ) { + ret =drms_uA_update (rdev); +if (ret) + regulator->enable_count--; +return ret; + }return 0 ; }
Signature · 1 line
2.37.1
Hi,
Quoted 34 lines · Andrew Halaney
If
regulator_enable()fails,enable_countis incremented still. A consumer, assuming no matchingregulator_disable()is necessary on failure, will then get this error message uponregulator_put()sinceenable_countis non-zero:[ 1.277418] WARNING: CPU: 3 PID: 1 at drivers/regulator/core.c:2304 _regulator_put.part.0+0x168/0x170The consumer could try to fix this in their driver by cleaning up on error from
regulator_enable()(i.e. callregulator_disable()), but that results in the following sinceregulator_enable()failed and didn't incrementuser_count:[ 1.258112] unbalanced disables for vreg_l17c [ 1.262606] WARNING: CPU: 4 PID: 1 at drivers/regulator/core.c:2899 _regulator_disable+0xd4/0x190Fix this by decrementing
enable_countupon failure to enable.With this in place, just the reason for failure to enable is printed as expected and developers can focus on the root cause of their issue instead of thinking their usage of the regulator consumer api is incorrect. For example, in my case:
[ 1.240426] vreg_l17c: invalid input voltage foundFixes 5451781dadf8 ("regulator: core: Only count load for enabled consumers")Signed-off-by Andrew Halaney <[email protected]>---
I'm new to using the regulator framework, but I _believe_ this is a cosmetic bug that's fixed by this patch. I went down a bit of a rabbit hole because of the original
WARN()message, so I'm trying to prevent others from doing the same :)
Quoted 8 lines
Please let me know what you think, I tested on the misconfigured system and on a working system and things seemed to work as expected.
Thanks,
Andrewdrivers/regulator/core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
This looks right to me. Thanks!
Quoted 22 lines · Andrew Halaney
If
regulator_enable()fails,enable_countis incremented still. A consumer, assuming no matchingregulator_disable()is necessary on failure, will then get this error message uponregulator_put()sinceenable_countis non-zero:[ 1.277418] WARNING: CPU: 3 PID: 1 at drivers/regulator/core.c:2304 _regulator_put.part.0+0x168/0x170The consumer could try to fix this in their driver by cleaning up on error from
regulator_enable()(i.e. callregulator_disable()), but that results in the following sinceregulator_enable()failed and didn't incrementuser_count:[ 1.258112] unbalanced disables for vreg_l17c [ 1.262606] WARNING: CPU: 4 PID: 1 at drivers/regulator/core.c:2899 _regulator_disable+0xd4/0x190Fix this by decrementing
enable_countupon failure to enable.With this in place, just the reason for failure to enable is printed as expected and developers can focus on the root cause of their issue instead of thinking their usage of the regulator consumer api is incorrect. For example, in my case:
Quoted 4 lines
[ 1.240426] vreg_l17c: invalid input voltage foundFixes 5451781dadf8 ("regulator: core: Only count load for enabled consumers")Signed-off-by Andrew Halaney <[email protected]>
Mark Brown
maintainerdrivers/regulator/core.c static int _regulator_do_enable(struct regulator_dev *rdev) · line 2743
Quoted 4 lines · Andrew Halaney
drivers/regulator/core.c +2
-2-if (regulator->uA_load && regulator->enable_count ==1 ) -return drms_uA_update (rdev); +if (regulator->uA_load && regulator->enable_count ==1 ) { + ret =drms_uA_update (rdev);
I am vaugely surprised and a bit concerned that drms_uA_update() can fail...
Hi,
Quoted 10 lines · Mark Brown
drivers/regulator/core.c static int _regulator_do_enable(struct regulator_dev *rdev) · line 2743
Quoted 4 lines · Andrew Halaney
drivers/regulator/core.c +2
-2-if (regulator->uA_load && regulator->enable_count ==1 ) -return drms_uA_update (rdev); +if (regulator->uA_load && regulator->enable_count ==1 ) { + ret =drms_uA_update (rdev);I am vaugely surprised and a bit concerned that
drms_uA_update()can fail...
In his original email Andrew implied that his system was misconfigured when he was seeing this error. I presume this isn't common which is why nobody had noticed this before now, but given that the function drms_uA_update() does return an error code it seems like we should handle it properly in the caller.
-Doug
Quoted 11 lines · Doug Anderson
Hi,
Quoted 8 lines · Mark Brown
Quoted 4 lines · Andrew Halaney
drivers/regulator/core.c +2
-2-if (regulator->uA_load && regulator->enable_count ==1 ) -return drms_uA_update (rdev); +if (regulator->uA_load && regulator->enable_count ==1 ) { + ret =drms_uA_update (rdev);
Quoted 8 lines
Quoted 2 lines
I am vaugely surprised and a bit concerned that
drms_uA_update()can fail...In his original email Andrew implied that his system was misconfigured when he was seeing this error. I presume this isn't common which is why nobody had noticed this before now, but given that the function
drms_uA_update()does return an error code it seems like we should handle it properly in the caller.
Doug was right, I thought my devicetree was bad. But after spending the morning trying to write a reply about why exactly it was bad, I realized that it might not be that. I agree with him that this patch makes sense (to me at least) regardless.
Here I am reporting on how a recent change in qcom-rpmh-regulator.c made this appear (no idea yet if the change is at fault, or if I've got some other misconfiguration though in my devicetree!):
https://lore.kernel.org/all/20220822193153.zn2oxljmd76awqot@halaneylaptop/
Thanks,
Andrew
Mark Brown
maintainerQuoted 8 lines · Andrew Halaney
If
regulator_enable()fails,enable_countis incremented still. A consumer, assuming no matchingregulator_disable()is necessary on failure, will then get this error message uponregulator_put()sinceenable_countis non-zero:[ 1.277418] WARNING: CPU: 3 PID: 1 at drivers/regulator/core.c:2304 _regulator_put.part.0+0x168/0x170[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
Thanks!
[1/1] regulator: core: Clean up on enable failure
commit: c32f1ebfd26bece77141257864ed7b4720da1557
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark