2021-06-25 12:55:53

by Alexandru Ardelean

[permalink] [raw]
Subject: [RFC PATCH] regulator: devres: disable regulator on release if refcount is 1

Evidently, this came about after doing a few of these types of constructs:

static void reg_disable(void *reg)
{
regulator_disable(reg)
}

...

ret = regulator_enable(reg);
if (ret)
return ret;

ret = devm_add_action_or_reset(dev, reg_disable, reg);

...

Naturally, the first thought was to try to move this construct into
regulator core, but I remembered that a devm_regulator_enable() function
isn't all that loved.
The construct above looks like it could become a short-hand (in the form of
devm_regulator_enable()), somewhere in the regulator framework.

After going back through the previous discussions [referenced below, sorry
if I missed any], it looks like maybe an idea would be to call
regulator_disable() right before regulator_put() inside
devm_regulator_release(). But we need to call it only if the 'enable_count'
is 1.

This means that the last 'regulator_disable()' (on driver remove) becomes
optional.
If there are any unbalanced regulator_enable()/regulator_disable() calls,
the 'enable_count' won't be touched and 'regulator_put()' will print a
warning.
The condition could be made to check if 'enable_count >= 1', and the
behavior would be the same, but it's probably a good idea not to touch this
refcount if isn't 1.

The only disadvantage to this approach, is that it changes the order in the
drivers in which the register_disable() gets called, with respect to other
steps in the probe/remove order.
With this, the register_disable() will be called right before the consumer
reference is free'd.

But the other advantage is that regulator_disable() calls can be removed
in simple drivers, where the consumer reference has been requested
with devm_regulator_get().

References:
https://lore.kernel.org/lkml/20170213023249.GA27688@dtor-ws/
https://lkml.org/lkml/2014/2/4/940

Cc: Dmitry Torokhov <[email protected]>
Cc: Guenter Roeck <[email protected]>
Signed-off-by: Alexandru Ardelean <[email protected]>
---

Note: this patch applies indepently of this series:
https://lore.kernel.org/lkml/[email protected]/

drivers/regulator/devres.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 826c29499d69..1852afc02990 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -16,7 +16,12 @@

static void devm_regulator_release(struct device *dev, void *res)
{
- regulator_put(*(struct regulator **)res);
+ struct regulator *regulator = *(struct regulator **)res;
+
+ if (regulator->enable_count == 1)
+ regulator_disable(regulator);
+
+ regulator_put(regulator);
}

static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
--
2.31.1


2021-06-28 23:30:51

by Mark Brown

[permalink] [raw]
Subject: Re: [RFC PATCH] regulator: devres: disable regulator on release if refcount is 1

On Fri, Jun 25, 2021 at 03:53:07PM +0300, Alexandru Ardelean wrote:

> This means that the last 'regulator_disable()' (on driver remove) becomes
> optional.
> If there are any unbalanced regulator_enable()/regulator_disable() calls,
> the 'enable_count' won't be touched and 'regulator_put()' will print a
> warning.

This doesn't seem like it's going to make reviewing and debugging
reference counting issues any easier, it seems even more of a concern
than a devm version TBH. It's also not clear why if we were doing this
we'd restrict it to a single reference.


Attachments:
(No filename) (578.00 B)
signature.asc (499.00 B)
Download all attachments

2021-06-29 07:57:04

by Alexandru Ardelean

[permalink] [raw]
Subject: Re: [RFC PATCH] regulator: devres: disable regulator on release if refcount is 1

On Mon, 28 Jun 2021 at 17:53, Mark Brown <[email protected]> wrote:
>
> On Fri, Jun 25, 2021 at 03:53:07PM +0300, Alexandru Ardelean wrote:
>
> > This means that the last 'regulator_disable()' (on driver remove) becomes
> > optional.
> > If there are any unbalanced regulator_enable()/regulator_disable() calls,
> > the 'enable_count' won't be touched and 'regulator_put()' will print a
> > warning.
>
> This doesn't seem like it's going to make reviewing and debugging
> reference counting issues any easier, it seems even more of a concern
> than a devm version TBH. It's also not clear why if we were doing this
> we'd restrict it to a single reference.

Yeah, it doesn't make much difference if the refcount is 1 or higher.
For any refcount higher than 1, it's a serious unbalance of reg enable
+ disable.
And I agree that this may complicate reviews, as this would be one
extra subtlety [in the regulator framework] to account for.

Will send an RFC v2 with the short-hand.
I'm not trying to force anything with this, but looking at all the
repetitiveness of the devm_add_action_or_reset() hook to disable
simple regulators, I thought I'd [also] propose a variant or another.

2021-06-30 12:50:29

by Alexandru Ardelean

[permalink] [raw]
Subject: [RFC PATCH v2] regulator: devres: add devm_regulator_enable() as short-hand

Evidently, this came about after doing a few of these types of constructs:

static void reg_disable(void *reg)
{
regulator_disable(reg)
}
...
ret = regulator_enable(reg);
if (ret)
return ret;

ret = devm_add_action_or_reset(dev, reg_disable, reg);
...

A previous proposal was done via [1], to automatically decrease the enable
refcount in devm_regulator_release() if it's 1.
A point was made that this [indeed] can cause issues with
reviewing/tracking enable refcount.

I'm not sure [at this point in time] whether a devm_regulator_enable()
function is still something that would be a good idea, given that this can
cause issues with consumers potentially mixing regulator_enable() and
devm_regulator_enable() calls. This concern was mentioned in [2].

But, there are now a number of simple drivers that are implementing the above
construct already, so it may be an idea to propose this short-hand in the
regulator framework.

References:
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/20170213023249.GA27688@dtor-ws/
[3] https://lkml.org/lkml/2014/2/4/940

Cc: Dmitry Torokhov <[email protected]>
Cc: Guenter Roeck <[email protected]>
Signed-off-by: Alexandru Ardelean <[email protected]>
---
.../driver-api/driver-model/devres.rst | 1 +
drivers/regulator/devres.c | 20 +++++++++++++++++++
include/linux/regulator/consumer.h | 8 ++++++++
3 files changed, 29 insertions(+)

diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index e0814d214048..8a92e0888cb8 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -408,6 +408,7 @@ REGULATOR
devm_regulator_get()
devm_regulator_put()
devm_regulator_register()
+ devm_regulator_enable()

RESET
devm_reset_control_get()
diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index a8de0aa88bad..dfb23ac178a0 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -533,3 +533,23 @@ void *devm_regulator_irq_helper(struct device *dev,
return ptr;
}
EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);
+
+static void devm_regulator_disable(void *regulator)
+{
+ regulator_disable(regulator);
+}
+
+/**
+ * devm_regulator_enable - device-managed regulator_enable
+ * @dev: device for regulator "consumer"
+ * @regulator: regulator source
+ *
+ * This will register an action handler for disabling the regulator
+ * by reistering a regulator_disable() call via devm_add_action_or_reset().
+ * It's meant to be a short-hand for all the drivers using this construct.
+ */
+int devm_regulator_enable(struct device *dev, struct regulator *regulator)
+{
+ return devm_add_action_or_reset(dev, devm_regulator_disable, regulator);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_enable);
diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h
index f72ca73631be..01f17205c567 100644
--- a/include/linux/regulator/consumer.h
+++ b/include/linux/regulator/consumer.h
@@ -236,6 +236,8 @@ void devm_regulator_bulk_unregister_supply_alias(struct device *dev,

/* regulator output control and status */
int __must_check regulator_enable(struct regulator *regulator);
+int __must_check devm_regulator_enable(struct device *dev,
+ struct regulator *regulator);
int regulator_disable(struct regulator *regulator);
int regulator_force_disable(struct regulator *regulator);
int regulator_is_enabled(struct regulator *regulator);
@@ -432,6 +434,12 @@ static inline int regulator_enable(struct regulator *regulator)
return 0;
}

+static inline int devm_regulator_enable(struct device *dev,
+ struct regulator *regulator)
+{
+ return 0;
+}
+
static inline int regulator_disable(struct regulator *regulator)
{
return 0;
--
2.31.1