Add NULL pointer checks to regulator_enable() et. al?

From: Bill Gatliff
Date: Wed Jan 25 2012 - 02:39:34 EST


Guys:


(Please CC me on replies).


I have this pattern coming up a LOT in my code:

struct regulator *reg;

/* probe() */
reg = regulator_get(dev, "VLOGIC");

/* elsewhere() */
if (!IS_ERR_OR_NULL(reg))
ret = regulator_enable(reg);

/* remove() */
if (!IS_ERR_OR_NULL(reg))
regulator_put(reg);

It comes up particularly often in drivers that are doing aggressive
runtime power management.

Unlike other kernel API, a NULL pointer from regulator_get() isn't a
true "error": it just means that there is no regulator associated with
the requested pin and device. It seems natural, therefore, for
regulator_enable() and friends to not consider a NULL regulator
pointer to be an error--- and definitely not something OOPS-worthy.
But if I forget even ONE check in the above code, that's exactly what
I get: an OOPS. But all those checks seem to be just tedium and noise
in this case.

Is there something truly objectionable in the following patch? If
not, then I'm happy to post a more complete one.

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 5b2328d..b6e303a 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1482,9 +1482,16 @@ static int _regulator_enable(struct regulator_dev *rdev)
*/
int regulator_enable(struct regulator *regulator)
{
- struct regulator_dev *rdev = regulator->rdev;
+ struct regulator_dev *rdev;
int ret = 0;

+ if (unlikely(!regulator))
+ return 0;
+ if (unlikely(IS_ERR(regulator)))
+ return PTR_ERR(regulator);
+
+ rdev = regulator->rdev;
+
mutex_lock(&rdev->mutex);

if (!regulator_check_voltage_update(rdev)) {


Note that I'm not arguing for such treatment all over the kernel---
just certain places in the regulator framework!


Kindest regards,


b.g.
--
Bill Gatliff
bgat@xxxxxxxxxxxxxxx
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/