Thread (1 message) 1 message, 1 author, 2016-03-12

Re: [PATCH v6 1/1] iio:pressure:ms5611: fix crash when probing regulator

From: Jonathan Cameron <hidden>
Date: 2016-03-12 11:04:32
Also in: linux-iio

On 09/03/16 11:34, Gregor Boirie wrote:
When not compiled-in, regulator layer will return a NULL pointer when
trying to get a reference to any regulator using devm_regulator_get().  As
IS_ERR() does not consider this an error, the ms5611 probing operation will
try to enable a NULL regulator, which will invariably cause a kernel
crash.
This patch fixes this situation by using devm_regulator_get_optional()
instead of devm_regulator_get().

Fixes: 3145229f9191 ("iio:pressure:ms5611: power regulator support")
Signed-off-by: Gregor Boirie <gregor.boirie-ITF29qwbsa/QT0dZR+AlfA@public.gmane.org>
I've been thinking a bit more about these cases and think we are missusing
the optional regulators.  If regulator support is present and a non optional
register is requested then it will feed a dummy regulator back. 
The intent of this is to handle the case where an non optional regulator is
needed, but not specified (typically because it is always on).

So the question is how should we handle the regulators not built in case.
If you do call devm_regulator_get you'll get a null pointer.
Without regulators built in regulator_enable will be fine (returns 0)
as does regulator disable.

So I'm unclear on where the crash is coming from.

Also interesting is this driver enables the regulator but never disables
it (which it should).   I was clearly half asleep when reviewing this.
Sorry!

Jonathan

quoted hunk
---
 drivers/iio/pressure/ms5611_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c
index 37dbc04..a2a871b 100644
--- a/drivers/iio/pressure/ms5611_core.c
+++ b/drivers/iio/pressure/ms5611_core.c
@@ -387,8 +387,8 @@ static const struct iio_info ms5611_info = {
 static int ms5611_init(struct iio_dev *indio_dev)
 {
 	int ret;
-	struct regulator *vdd = devm_regulator_get(indio_dev->dev.parent,
-	                                           "vdd");
+	struct regulator *vdd =
+		devm_regulator_get_optional(indio_dev->dev.parent, "vdd");
 
 	/* Enable attached regulator if any. */
 	if (!IS_ERR(vdd)) {
@@ -398,6 +398,10 @@ static int ms5611_init(struct iio_dev *indio_dev)
 			        "failed to enable Vdd supply: %d\n", ret);
 			return ret;
 		}
+	} else {
+		ret = PTR_ERR(vdd);
+		if (ret != -ENODEV)
+			return ret;
 	}
 
 	ret = ms5611_reset(indio_dev);
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help