Skip to content

Commit 04b44d2

Browse files
osctobeksacilotto
authored andcommitted
regulator: fix memory leak with repeated set_machine_constraints()
BugLink: https://bugs.launchpad.net/bugs/1908561 commit 57a6ad4 upstream. 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. Fixes: aea6cb9 ("regulator: resolve supply after creating regulator") Cc: stable@vger.kernel.org Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl> Tested-by: Ahmad Fatoum <a.fatoum@pengutronix.de> # stpmic1 Link: https://lore.kernel.org/r/78c3d4016cebc08d441aad18cb924b4e4d9cf9df.1605226675.git.mirq-linux@rere.qmqm.pl Signed-off-by: Mark Brown <broonie@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Ian May <ian.may@canonical.com>
1 parent d5392fe commit 04b44d2

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

drivers/regulator/core.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,29 +1275,18 @@ static int _regulator_do_enable(struct regulator_dev *rdev);
12751275
/**
12761276
* set_machine_constraints - sets regulator constraints
12771277
* @rdev: regulator source
1278-
* @constraints: constraints to apply
12791278
*
12801279
* Allows platform initialisation code to define and constrain
12811280
* regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
12821281
* Constraints *must* be set by platform code in order for some
12831282
* regulator operations to proceed i.e. set_voltage, set_current_limit,
12841283
* set_mode.
12851284
*/
1286-
static int set_machine_constraints(struct regulator_dev *rdev,
1287-
const struct regulation_constraints *constraints)
1285+
static int set_machine_constraints(struct regulator_dev *rdev)
12881286
{
12891287
int ret = 0;
12901288
const struct regulator_ops *ops = rdev->desc->ops;
12911289

1292-
if (constraints)
1293-
rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1294-
GFP_KERNEL);
1295-
else
1296-
rdev->constraints = kzalloc(sizeof(*constraints),
1297-
GFP_KERNEL);
1298-
if (!rdev->constraints)
1299-
return -ENOMEM;
1300-
13011290
ret = machine_constraints_voltage(rdev, rdev->constraints);
13021291
if (ret != 0)
13031292
return ret;
@@ -5014,7 +5003,6 @@ struct regulator_dev *
50145003
regulator_register(const struct regulator_desc *regulator_desc,
50155004
const struct regulator_config *cfg)
50165005
{
5017-
const struct regulation_constraints *constraints = NULL;
50185006
const struct regulator_init_data *init_data;
50195007
struct regulator_config *config = NULL;
50205008
static atomic_t regulator_no = ATOMIC_INIT(-1);
@@ -5153,14 +5141,23 @@ regulator_register(const struct regulator_desc *regulator_desc,
51535141

51545142
/* set regulator constraints */
51555143
if (init_data)
5156-
constraints = &init_data->constraints;
5144+
rdev->constraints = kmemdup(&init_data->constraints,
5145+
sizeof(*rdev->constraints),
5146+
GFP_KERNEL);
5147+
else
5148+
rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5149+
GFP_KERNEL);
5150+
if (!rdev->constraints) {
5151+
ret = -ENOMEM;
5152+
goto wash;
5153+
}
51575154

51585155
if (init_data && init_data->supply_regulator)
51595156
rdev->supply_name = init_data->supply_regulator;
51605157
else if (regulator_desc->supply_name)
51615158
rdev->supply_name = regulator_desc->supply_name;
51625159

5163-
ret = set_machine_constraints(rdev, constraints);
5160+
ret = set_machine_constraints(rdev);
51645161
if (ret == -EPROBE_DEFER) {
51655162
/* Regulator might be in bypass mode and so needs its supply
51665163
* to set the constraints */
@@ -5169,7 +5166,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
51695166
* that is just being created */
51705167
ret = regulator_resolve_supply(rdev);
51715168
if (!ret)
5172-
ret = set_machine_constraints(rdev, constraints);
5169+
ret = set_machine_constraints(rdev);
51735170
else
51745171
rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
51755172
ERR_PTR(ret));

0 commit comments

Comments
 (0)