Skip to contentSkip to conversation

regulator: core: Limit propagation of parent voltage count and list

PATCH Superseded Matthias Kaehlcke 4 participants 8 messages Mar 24, 2017 – Mar 27, 2017 Voltage and current regulator framework

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regulator: core: Limit propagation of parent voltage count and list

Commit 26988efe11b1 ("regulator: core: Allow to get voltage count and list from parent") introduces the propagation of the parent voltage count and list for regulators that don't provide this information themselves. The goal is to support simple switch regulators, however as a side effect normal continuous regulators can leak details of their supplies and provide consumers with inconsistent information.

Limit the propagation of the voltage count and list to regulators which don't have get_voltage(_sel) and list_voltage ops.

---
 drivers/regulator/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/regulator/core.c +9 -0
index 53d4fc70dbd0..121838e0125b 100644
@@ -2487,6 +2487,10 @@ static int _regulator_list_voltage(struct regulator *regulator,
 		if (lock)
 			mutex_unlock(&rdev->mutex);
 	} else if (rdev->supply) {
+		// Limit propagation of parent values to switch regulators
+		if (ops->get_voltage || ops->get_voltage_sel)
+			return -EINVAL;
+
 		ret = _regulator_list_voltage(rdev->supply, selector, lock);
 	} else {
 		return -EINVAL;
@@ -2540,6 +2544,7 @@ EXPORT_SYMBOL_GPL(regulator_is_enabled);
 int regulator_count_voltages(struct regulator *regulator)
 {
 	struct regulator_dev	*rdev = regulator->rdev;
+	const struct regulator_ops *ops = rdev->desc->ops;
 
 	if (rdev->desc->n_voltages)
 		return rdev->desc->n_voltages;
@@ -2547,6 +2552,10 @@ int regulator_count_voltages(struct regulator *regulator)
 	if (!rdev->supply)
 		return -EINVAL;
 
+	// Limit propagation of parent value to switch regulators
+	if (ops->get_voltage || ops->get_voltage_sel || ops->list_voltage)
+		return -EINVAL;
+
 	return regulator_count_voltages(rdev->supply);
 }
 EXPORT_SYMBOL_GPL(regulator_count_voltages);
Signature · 1 line
2.12.1.578.ge9c3154ca4-goog
Reviewed-by on v1 1/1
#1 lore raw

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2490

Quoted 9 lines · Matthias Kaehlcke
drivers/regulator/core.c +1 -0
index 53d4fc70dbd0..121838e0125b 100644
@@ -2487,6 +2487,10 @@ static int _regulator_list_voltage(struct regulator *regulator,
 		if (lock)
 			mutex_unlock(&rdev->mutex);
 	} else if (rdev->supply) {
+		// Limit propagation of parent values to switch regulators

The kernel doesn't use C99 comments. Oddly enough, this isn't actually in the coding style doc (Documentation/process/coding-style.rst), nor is it caught by scripts/checkpatch.pl (even though it clearly has a 'C99 comment' rule).

Quoted 15 lines
drivers/regulator/core.c +4 -0
+		if (ops->get_voltage || ops->get_voltage_sel)
+			return -EINVAL;
+
 		ret = _regulator_list_voltage(rdev->supply, selector, lock);
 	} else {
 		return -EINVAL;
@@ -2540,6 +2544,7 @@ EXPORT_SYMBOL_GPL(regulator_is_enabled);
 int regulator_count_voltages(struct regulator *regulator)
 {
 	struct regulator_dev	*rdev = regulator->rdev;
+	const struct regulator_ops *ops = rdev->desc->ops;
 
 	if (rdev->desc->n_voltages)
 		return rdev->desc->n_voltages;

drivers/regulator/core.c int regulator_count_voltages(struct regulator *regulator) · line 2555

Quoted 4 lines
drivers/regulator/core.c +1 -0
@@ -2547,6 +2552,10 @@ int regulator_count_voltages(struct regulator *regulator)
 	if (!rdev->supply)
 		return -EINVAL;
 
+	// Limit propagation of parent value to switch regulators

Same here.

drivers/regulator/core.c int regulator_count_voltages(struct regulator *regulator) · line 2561

Quoted 6 lines
drivers/regulator/core.c +3 -0
+	if (ops->get_voltage || ops->get_voltage_sel || ops->list_voltage)
+		return -EINVAL;
+
 	return regulator_count_voltages(rdev->supply);
 }
 EXPORT_SYMBOL_GPL(regulator_count_voltages);

I'm not very familiar with this code, but judging by your problem description in previous threads and by comparing with the logic in _regulator_get_voltage() (for when to reference the ->supply), this seems resonable. So:

It's probably worth verifying that this doesn't break whatever Javier was supporting in the first place, as a sanity check.

Brian

Reviewed-by Tested-by
#2 lore raw

Hello Matthias,

Quoted 12 lines · Brian Norris

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2490

Quoted 9 lines · Matthias Kaehlcke
drivers/regulator/core.c +1 -0
index 53d4fc70dbd0..121838e0125b 100644
@@ -2487,6 +2487,10 @@ static int _regulator_list_voltage(struct regulator *regulator,
 		if (lock)
 			mutex_unlock(&rdev->mutex);
 	} else if (rdev->supply) {
+		// Limit propagation of parent values to switch regulators

The kernel doesn't use C99 comments. Oddly enough, this isn't actually

+1

Quoted 5 lines

in the coding style doc (Documentation/process/coding-style.rst), nor is it caught by scripts/checkpatch.pl (even though it clearly has a 'C99 comment' rule).

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2491

Quoted 1 line
drivers/regulator/core.c +1 -0
+		if (ops->get_voltage || ops->get_voltage_sel)

It's valid to have a .get_voltage_sel callback without a .list_voltage?

At least it seems that _regulator_get_voltage() assumes that having a .get_voltage_sel implies that a .list_voltage will also be available.

static int _regulator_get_voltage(struct regulator_dev *rdev) {
...

	if (rdev->desc->ops->get_voltage_sel) {
		sel = rdev->desc->ops->get_voltage_sel(rdev);
		if (sel < 0)
			return sel;
		ret = rdev->desc->ops->list_voltage(rdev, sel);
	} else if (rdev->desc->ops->get_voltage) {

...
}

So I would only check for if (ops->get_voltage).

Quoted 17 lines
Quoted 17 lines
drivers/regulator/core.c +3 -0
+			return -EINVAL;
+
 		ret = _regulator_list_voltage(rdev->supply, selector, lock);
 	} else {
 		return -EINVAL;
@@ -2540,6 +2544,7 @@ EXPORT_SYMBOL_GPL(regulator_is_enabled);
 int regulator_count_voltages(struct regulator *regulator)
 {
 	struct regulator_dev	*rdev = regulator->rdev;
+	const struct regulator_ops *ops = rdev->desc->ops;
 
 	if (rdev->desc->n_voltages)
 		return rdev->desc->n_voltages;
@@ -2547,6 +2552,10 @@ int regulator_count_voltages(struct regulator *regulator)
 	if (!rdev->supply)
 		return -EINVAL;
 
Quoted 7 lines

drivers/regulator/core.c int regulator_count_voltages(struct regulator *regulator) · line 2555

Quoted 1 line
drivers/regulator/core.c +1 -0
+	// Limit propagation of parent value to switch regulators

Same here.

drivers/regulator/core.c int regulator_count_voltages(struct regulator *regulator) · line 2558

Quoted 3 lines
drivers/regulator/core.c +3 -0
+	if (ops->get_voltage || ops->get_voltage_sel || ops->list_voltage)
+		return -EINVAL;
+

I wonder if instead of always checking if the regulator lacks operations, it wouldn't be better to do it just once and store that the regulator is a switch so that state can be used as explicit check for switch instead.

Something like if (!rdev->supply || !rdev->switch) looks more clear to me.

Quoted 11 lines

drivers/regulator/core.c int regulator_count_voltages(struct regulator *regulator) · line 2561

Quoted 3 lines
drivers/regulator/core.c +0 -0
 	return regulator_count_voltages(rdev->supply);
 }
 EXPORT_SYMBOL_GPL(regulator_count_voltages);

I'm not very familiar with this code, but judging by your problem description in previous threads and by comparing with the logic in _regulator_get_voltage() (for when to reference the ->supply), this seems resonable. So:

Agreed, the logic sounds reasonable indeed and I didn't think of this case when writing the mentioned commit, so feel free to add:

Quoted 3 lines

It's probably worth verifying that this doesn't break whatever Javier was supporting in the first place, as a sanity check.

I've tested in the system that led to the mentioned commit and I did not find any issue with $SUBJECT.

Quoted 2 lines

Brian

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Open Source Group
Samsung Research America
maintainer
#3 lore raw
Quoted 6 lines · Javier Martinez Canillas
Quoted 2 lines · Brian Norris

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2491

Quoted 2 lines
drivers/regulator/core.c +1 -0
+		if (ops->get_voltage || ops->get_voltage_sel)

It's valid to have a .get_voltage_sel callback without a .list_voltage?

No.

#4 lore raw
In reply to Javier Martinez Canillas

Thanks for the reviews and testing!

El Sat, Mar 25, 2017 at 02:05:47AM -0300 Javier Martinez Canillas ha dit:

Quoted 13 lines · Brian Norris
Quoted 12 lines · Matthias Kaehlcke

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2490

Quoted 9 lines
drivers/regulator/core.c +1 -0
index 53d4fc70dbd0..121838e0125b 100644
@@ -2487,6 +2487,10 @@ static int _regulator_list_voltage(struct regulator *regulator,
 		if (lock)
 			mutex_unlock(&rdev->mutex);
 	} else if (rdev->supply) {
+		// Limit propagation of parent values to switch regulators

The kernel doesn't use C99 comments. Oddly enough, this isn't actually

Quoted 1 line

+1

Will fix

Quoted 14 lines
Quoted 5 lines

in the coding style doc (Documentation/process/coding-style.rst), nor is it caught by scripts/checkpatch.pl (even though it clearly has a 'C99 comment' rule).

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2491

Quoted 1 line
drivers/regulator/core.c +1 -0
+		if (ops->get_voltage || ops->get_voltage_sel)

It's valid to have a .get_voltage_sel callback without a .list_voltage?

At least it seems that _regulator_get_voltage() assumes that having a .get_voltage_sel implies that a .list_voltage will also be available.

static int _regulator_get_voltage(struct regulator_dev *rdev) {
...

Quoted 8 lines
	if (rdev->desc->ops->get_voltage_sel) {
		sel = rdev->desc->ops->get_voltage_sel(rdev);
		if (sel < 0)
			return sel;
		ret = rdev->desc->ops->list_voltage(rdev, sel);
	} else if (rdev->desc->ops->get_voltage) {

...
}

The same function (from which I derived the conditions) suggests that a regulator could have a .list_voltage op even if it doesn't have .get_voltage_sel:

Quoted 6 lines

...
if (rdev->desc->ops->get_voltage_sel) {
...
} else if (rdev->desc->ops->get_voltage) {
...
} else if (rdev->desc->ops->list_voltage) {

I don't know for sure if this condition is superfluous or if there are cases where it makes sense to have a .list_voltage but not .get_voltage_sel.

Quoted 6 lines

I wonder if instead of always checking if the regulator lacks operations, it wouldn't be better to do it just once and store that the regulator is a switch so that state can be used as explicit check for switch instead.

Something like if (!rdev->supply || !rdev->switch) looks more clear to me.

I agree and we can even reduce it to if (!rdev_switch) since a switch implicitly has a supply.

I'll send out a new version soon.

Matthias

#5 lore raw

Hello Matthias,

Quoted 2 lines · Matthias Kaehlcke

Thanks for the reviews and testing!

You are welcome.

[snip]

Quoted 30 lines
Quoted 10 lines
Quoted 1 line

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2491

Quoted 1 line
drivers/regulator/core.c +1 -0
+		if (ops->get_voltage || ops->get_voltage_sel)

It's valid to have a .get_voltage_sel callback without a .list_voltage?

At least it seems that _regulator_get_voltage() assumes that having a .get_voltage_sel implies that a .list_voltage will also be available.

static int _regulator_get_voltage(struct regulator_dev *rdev) {
...

Quoted 8 lines
	if (rdev->desc->ops->get_voltage_sel) {
		sel = rdev->desc->ops->get_voltage_sel(rdev);
		if (sel < 0)
			return sel;
		ret = rdev->desc->ops->list_voltage(rdev, sel);
	} else if (rdev->desc->ops->get_voltage) {

...
}

The same function (from which I derived the conditions) suggests that a regulator could have a .list_voltage op even if it doesn't have .get_voltage_sel:

Quoted 6 lines

...
if (rdev->desc->ops->get_voltage_sel) {
...
} else if (rdev->desc->ops->get_voltage) {
...
} else if (rdev->desc->ops->list_voltage) {

Quoted 4 lines

I don't know for sure if this condition is superfluous or if there are cases where it makes sense to have a .list_voltage but not .get_voltage_sel.

I don't think is the same condition. Unless I'm misreading the code what it's checking is if there's a .list_voltage even when there is no .get_voltage_sel.

IOW, it's valid to have a .list_voltage even when there's no callback for .get_voltage_sel, but the opposite isn't true.

Quoted 10 lines
Quoted 6 lines

I wonder if instead of always checking if the regulator lacks operations, it wouldn't be better to do it just once and store that the regulator is a switch so that state can be used as explicit check for switch instead.

Something like if (!rdev->supply || !rdev->switch) looks more clear to me.

I agree and we can even reduce it to if (!rdev_switch) since a switch implicitly has a supply.

I wonder if that's always true. What happens if you have a switch but its <name>-supply parent isn't defined in the Device Tree?

Quoted 4 lines

I'll send out a new version soon.

Matthias

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Open Source Group
Samsung Research America
maintainer
#6 lore raw
Quoted 9 lines · Javier Martinez Canillas
Quoted 3 lines · Matthias Kaehlcke

I don't know for sure if this condition is superfluous or if there are cases where it makes sense to have a .list_voltage but not .get_voltage_sel.

I don't think is the same condition. Unless I'm misreading the code what it's checking is if there's a .list_voltage even when there is no .get_voltage_sel.

Yes.

Quoted 2 lines

IOW, it's valid to have a .list_voltage even when there's no callback for .get_voltage_sel, but the opposite isn't true.

Yes.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Something like if (!rdev->supply || !rdev->switch) looks more clear to me.

I agree and we can even reduce it to if (!rdev_switch) since a switch implicitly has a supply.

I wonder if that's always true. What happens if you have a switch but its <name>-supply parent isn't defined in the Device Tree?

We may already be substituting in the dummy supply, I'd need to go check but if we're not that's a fairly straightforward fix for the immediate problem. You do then have to worry about the fact that the parent supply might not have voltage operations either, dummy supplies obviously won't and there are use cases where an actual supply might not either (things like unregulated wall supplies).

#7 lore raw
In reply to Javier Martinez Canillas

El Mon, Mar 27, 2017 at 01:54:50PM -0400 Javier Martinez Canillas ha dit:

Quoted 37 lines · Matthias Kaehlcke
Quoted 31 lines
Quoted 11 lines
Quoted 2 lines

drivers/regulator/core.c static int _regulator_list_voltage(struct regulator *regulator, · line 2491

Quoted 2 lines
drivers/regulator/core.c +1 -0
+		if (ops->get_voltage || ops->get_voltage_sel)

It's valid to have a .get_voltage_sel callback without a .list_voltage?

At least it seems that _regulator_get_voltage() assumes that having a .get_voltage_sel implies that a .list_voltage will also be available.

static int _regulator_get_voltage(struct regulator_dev *rdev) {
...

Quoted 8 lines
	if (rdev->desc->ops->get_voltage_sel) {
		sel = rdev->desc->ops->get_voltage_sel(rdev);
		if (sel < 0)
			return sel;
		ret = rdev->desc->ops->list_voltage(rdev, sel);
	} else if (rdev->desc->ops->get_voltage) {

...
}

The same function (from which I derived the conditions) suggests that a regulator could have a .list_voltage op even if it doesn't have .get_voltage_sel:

Quoted 6 lines

...
if (rdev->desc->ops->get_voltage_sel) {
...
} else if (rdev->desc->ops->get_voltage) {
...
} else if (rdev->desc->ops->list_voltage) {

Quoted 4 lines

I don't know for sure if this condition is superfluous or if there are cases where it makes sense to have a .list_voltage but not .get_voltage_sel.

Quoted 6 lines

I don't think is the same condition. Unless I'm misreading the code what it's checking is if there's a .list_voltage even when there is no .get_voltage_sel.

IOW, it's valid to have a .list_voltage even when there's no callback for .get_voltage_sel, but the opposite isn't true.

I see, thanks for the clarification.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

I wonder if instead of always checking if the regulator lacks operations, it wouldn't be better to do it just once and store that the regulator is a switch so that state can be used as explicit check for switch instead.

Something like if (!rdev->supply || !rdev->switch) looks more clear to me.

Quoted 6 lines
Quoted 3 lines

I agree and we can even reduce it to if (!rdev_switch) since a switch implicitly has a supply.

I wonder if that's always true. What happens if you have a switch but its <name>-supply parent isn't defined in the Device Tree?

My idea was to only set rdev->switch after having resolved the parent supply, though I concede this is not semantically. Maybe we still want this logic but give the flag a different name?

Matthias