regulator: debugging and fixing supply deps
cover regulator: debugging and fixing supply deps
It turns out that commit aea6cb99703e ("regulator: resolve supply after creating regulator") exposed a number of issues in regulator initialization and introduced a memory leak of its own. One uncovered problem was already fixed by cf1ad559a20d ("regulator: defer probe when trying to get voltage from unresolved supply"). This series fixes the remaining ones and adds a two debugging aids to help in the future.
The final patch adds a workaround to preexisting problem occurring with regulators that have the same name as its supply_name. This worked before by accident, so might be worth backporting. The error message is left on purpose so that these configurations can be detected and fixed.
(The first two patches are resends from Nov 5).
Michał Mirosław (4):
regulator: fix memory leak with repeated set_machine_constraints()
regulator: debug early supply resolving
regulator: avoid resolve_supply() infinite recursion
regulator: workaround self-referent regulators
drivers/regulator/core.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/4 regulator: fix memory leak with repeated set_machine_constraints()
Fixed commit introduced a possible second call to set_machine_constraints() and that allocates memory for rdev->constraints. Move the allocation to the caller so it's easier to manage and done once.
--- drivers/regulator/core.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/regulator/core.c +13 -16
@@ -1315,7 +1315,6 @@ static int _regulator_do_enable(struct regulator_dev *rdev);/** * set_machine_constraints - sets regulator constraints * @rdev: regulator source -* @constraints: constraints to apply * * Allows platform initialisation code to define and constrain * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
@@ -1323,21 +1322,11 @@ static int _regulator_do_enable(struct regulator_dev *rdev);* regulator operations to proceed i.e. set_voltage, set_current_limit, * set_mode. */ -static int set_machine_constraints (struct regulator_dev *rdev, -const struct regulation_constraints *constraints) +static int set_machine_constraints (struct regulator_dev *rdev) {int ret =0 ;const struct regulator_ops *ops = rdev->desc->ops; -if (constraints) - rdev->constraints =kmemdup (constraints,sizeof (*constraints), -GFP_KERNEL ); -else - rdev->constraints =kzalloc (sizeof (*constraints), -GFP_KERNEL ); -if (!rdev->constraints) -return -ENOMEM ; - ret =machine_constraints_voltage (rdev, rdev->constraints);if (ret !=0 )return ret;
@@ -5146,7 +5135,6 @@ struct regulator_dev * regulator_register(const struct regulator_desc *regulator_desc, const struct regulator_config *cfg) { - const struct regulation_constraints *constraints = NULL; const struct regulator_init_data *init_data;struct regulator_config *config =NULL ;static atomic_t regulator_no =ATOMIC_INIT (-1 );
@@ -5285,14 +5273,23 @@ regulator_register(const struct regulator_desc *regulator_desc,/* set regulator constraints */ if (init_data) - constraints = &init_data->constraints; + rdev->constraints =kmemdup (&init_data->constraints, +sizeof (*rdev->constraints), +GFP_KERNEL ); +else + rdev->constraints =kzalloc (sizeof (*rdev->constraints), +GFP_KERNEL ); +if (!rdev->constraints) { + ret = -ENOMEM ; + goto wash; + }if (init_data && init_data->supply_regulator) rdev->supply_name = init_data->supply_regulator;else if (regulator_desc->supply_name) rdev->supply_name = regulator_desc->supply_name; - ret =set_machine_constraints (rdev, constraints); + ret =set_machine_constraints (rdev);if (ret == -EPROBE_DEFER ) {/* Regulator might be in bypass mode and so needs its supply * to set the constraints */
@@ -5301,7 +5298,7 @@ regulator_register(const struct regulator_desc *regulator_desc,* that is just being created */ ret =regulator_resolve_supply (rdev);if (!ret) - ret =set_machine_constraints (rdev, constraints); + ret =set_machine_constraints (rdev);else rdev_dbg (rdev,"unable to resolve supply early: %pe\n" ,ERR_PTR (ret));
Signature · 1 line
2.20.1
PATCH 2/4 regulator: debug early supply resolving
In reply to Michał MirosławHelp debugging the case when set_machine_constraints() needs to be repeated.
--- drivers/regulator/core.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/regulator/core.c +2 -0
@@ -5296,6 +5296,8 @@ regulator_register(const struct regulator_desc *regulator_desc,/* FIXME: this currently triggers a chicken-and-egg problem * when creating -SUPPLY symlink in sysfs to a regulator * that is just being created */ +rdev_dbg (rdev,"will resolve supply early: %s\n" , + rdev->supply_name); ret =regulator_resolve_supply (rdev);if (!ret) ret =set_machine_constraints (rdev);
Signature · 1 line
2.20.1
PATCH 3/4 regulator: avoid resolve_supply() infinite recursion
In reply to Michał MirosławWhen a regulator's name equals its supply's name the regulator_resolve_supply() recurses indefinitely. Add a check so that debugging the problem is easier. The "fixed" commit just exposed the problem.
--- drivers/regulator/core.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/regulator/core.c +6 -0
@@ -1841,6 +1841,12 @@ static int regulator_resolve_supply(struct regulator_dev *rdev) } } +if (r == rdev) { +dev_err (dev,"Supply for %s (%s) resolved to itself\n" , + rdev->desc->name, rdev->supply_name); +return -EINVAL ; + } +/* * If the supply's parent device is not the same as the * regulator's parent device, then ensure the parent device
Signature · 1 line
2.20.1
PATCH 4/4 regulator: workaround self-referent regulators
In reply to Michał MirosławWorkaround regulators whose supply name happens to be the same as its own name. This fixes boards that used to work before the early supply resolving was removed. The error message is left in place so that offending drivers can be detected.
--- drivers/regulator/core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/regulator/core.c +4 -1
@@ -1844,7 +1844,10 @@ static int regulator_resolve_supply(struct regulator_dev *rdev)if (r == rdev) {dev_err (dev,"Supply for %s (%s) resolved to itself\n" , rdev->desc->name, rdev->supply_name); -return -EINVAL ; +if (!have_full_constraints ()) +return -EINVAL ; + r = dummy_regulator_rdev; +get_device (&r->dev); }/*
Signature · 1 line
2.20.1