[4/7] regulator: push allocation in set_consumer_device_supply() out of lock
diff mbox series

Message ID 2a978483b064b810b00e072fcdb6c3d24a896bd8.1597107682.git.mirq-linux@rere.qmqm.pl
State Superseded
Headers show
Series
  • regulator: fix deadlock vs memory reclaim
Related show

Commit Message

Michał Mirosław Aug. 11, 2020, 1:07 a.m. UTC
Pull regulator_list_mutex into set_consumer_device_supply() and keep
allocations outside of it. Fourth of the fs_reclaim deadlock case.

Cc: stable@vger.kernel.org
Fixes: 45389c47526d ("regulator: core: Add early supply resolution for regulators")
Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
---
 drivers/regulator/core.c | 44 +++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 19 deletions(-)

Comments

kernel test robot Aug. 11, 2020, 5:23 a.m. UTC | #1
Hi "Michał,

I love your patch! Perhaps something to improve:

[auto build test WARNING on regulator/for-next]
[also build test WARNING on v5.8 next-20200810]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Micha-Miros-aw/regulator-fix-deadlock-vs-memory-reclaim/20200811-091414
base:   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
config: x86_64-randconfig-m001-20200810 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

New smatch warnings:
drivers/regulator/core.c:1491 set_consumer_device_supply() warn: overwrite may leak 'node'

Old smatch warnings:
drivers/regulator/core.c:129 regulator_ops_is_valid() error: we previously assumed 'rdev->constraints' could be null (see line 128)
drivers/regulator/core.c:3051 regulator_get_hardware_vsel_register() warn: inconsistent indenting

vim +/node +1491 drivers/regulator/core.c

3801b86aa482d2 Mark Brown      2011-06-09  1448  
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1449  /**
06c63f9396133f Randy Dunlap    2010-11-18  1450   * set_consumer_device_supply - Bind a regulator to a symbolic supply
69279fb9a95051 Mark Brown      2008-12-31  1451   * @rdev:         regulator source
40f9244f4da897 Mark Brown      2009-06-17  1452   * @consumer_dev_name: dev_name() string for device supply applies to
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1453   * @supply:       symbolic name for supply
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1454   *
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1455   * Allows platform initialisation code to map physical regulator
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1456   * sources to symbolic names for supplies for use by devices.  Devices
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1457   * should use these symbolic names to request regulators, avoiding the
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1458   * need to provide board-specific regulator names as platform data.
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1459   */
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1460  static int set_consumer_device_supply(struct regulator_dev *rdev,
737f360d5bef5e Mark Brown      2012-02-02  1461  				      const char *consumer_dev_name,
40f9244f4da897 Mark Brown      2009-06-17  1462  				      const char *supply)
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1463  {
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1464  	struct regulator_map *node;
9ed2099edca26d Mark Brown      2009-07-21  1465  	int has_dev;
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1466  
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1467  	if (supply == NULL)
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1468  		return -EINVAL;
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1469  
9ed2099edca26d Mark Brown      2009-07-21  1470  	if (consumer_dev_name != NULL)
9ed2099edca26d Mark Brown      2009-07-21  1471  		has_dev = 1;
9ed2099edca26d Mark Brown      2009-07-21  1472  	else
9ed2099edca26d Mark Brown      2009-07-21  1473  		has_dev = 0;
9ed2099edca26d Mark Brown      2009-07-21  1474  
bcadf558ba875d Michał Mirosław 2020-08-11  1475  	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
bcadf558ba875d Michał Mirosław 2020-08-11  1476  	if (node == NULL)
bcadf558ba875d Michał Mirosław 2020-08-11  1477  		return -ENOMEM;
bcadf558ba875d Michał Mirosław 2020-08-11  1478  
bcadf558ba875d Michał Mirosław 2020-08-11  1479  	node->regulator = rdev;
bcadf558ba875d Michał Mirosław 2020-08-11  1480  	node->supply = supply;
bcadf558ba875d Michał Mirosław 2020-08-11  1481  
bcadf558ba875d Michał Mirosław 2020-08-11  1482  	if (has_dev) {
bcadf558ba875d Michał Mirosław 2020-08-11  1483  		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
bcadf558ba875d Michał Mirosław 2020-08-11  1484  		if (node->dev_name == NULL) {
bcadf558ba875d Michał Mirosław 2020-08-11  1485  			kfree(node);
bcadf558ba875d Michał Mirosław 2020-08-11  1486  			return -ENOMEM;
bcadf558ba875d Michał Mirosław 2020-08-11  1487  		}
bcadf558ba875d Michał Mirosław 2020-08-11  1488  	}
bcadf558ba875d Michał Mirosław 2020-08-11  1489  
bcadf558ba875d Michał Mirosław 2020-08-11  1490  	mutex_lock(&regulator_list_mutex);
6001e13c5f708e David Brownell  2008-12-31 @1491  	list_for_each_entry(node, &regulator_map_list, list) {
23b5cc2ab67832 Jani Nikula     2010-04-29  1492  		if (node->dev_name && consumer_dev_name) {
23b5cc2ab67832 Jani Nikula     2010-04-29  1493  			if (strcmp(node->dev_name, consumer_dev_name) != 0)
6001e13c5f708e David Brownell  2008-12-31  1494  				continue;
23b5cc2ab67832 Jani Nikula     2010-04-29  1495  		} else if (node->dev_name || consumer_dev_name) {
23b5cc2ab67832 Jani Nikula     2010-04-29  1496  			continue;
23b5cc2ab67832 Jani Nikula     2010-04-29  1497  		}
23b5cc2ab67832 Jani Nikula     2010-04-29  1498  
6001e13c5f708e David Brownell  2008-12-31  1499  		if (strcmp(node->supply, supply) != 0)
6001e13c5f708e David Brownell  2008-12-31  1500  			continue;
6001e13c5f708e David Brownell  2008-12-31  1501  
737f360d5bef5e Mark Brown      2012-02-02  1502  		pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
737f360d5bef5e Mark Brown      2012-02-02  1503  			 consumer_dev_name,
6001e13c5f708e David Brownell  2008-12-31  1504  			 dev_name(&node->regulator->dev),
6001e13c5f708e David Brownell  2008-12-31  1505  			 node->regulator->desc->name,
6001e13c5f708e David Brownell  2008-12-31  1506  			 supply,
1083c39346d482 Mark Brown      2009-10-22  1507  			 dev_name(&rdev->dev), rdev_get_name(rdev));
bcadf558ba875d Michał Mirosław 2020-08-11  1508  		goto fail;
6001e13c5f708e David Brownell  2008-12-31  1509  	}
6001e13c5f708e David Brownell  2008-12-31  1510  
bcadf558ba875d Michał Mirosław 2020-08-11  1511  	list_add(&node->list, &regulator_map_list);
bcadf558ba875d Michał Mirosław 2020-08-11  1512  	mutex_unlock(&regulator_list_mutex);
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1513  
bcadf558ba875d Michał Mirosław 2020-08-11  1514  	return 0;
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1515  
bcadf558ba875d Michał Mirosław 2020-08-11  1516  fail:
bcadf558ba875d Michał Mirosław 2020-08-11  1517  	mutex_unlock(&regulator_list_mutex);
bcadf558ba875d Michał Mirosław 2020-08-11  1518  	kfree(node->dev_name);
40f9244f4da897 Mark Brown      2009-06-17  1519  	kfree(node);
bcadf558ba875d Michał Mirosław 2020-08-11  1520  	return -EBUSY;
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1521  }
a5766f11cfd3a0 Liam Girdwood   2008-10-10  1522  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Michał Mirosław Aug. 11, 2020, 5:28 p.m. UTC | #2
On Tue, Aug 11, 2020 at 01:23:47PM +0800, kernel test robot wrote:
[...]
> New smatch warnings:
> drivers/regulator/core.c:1491 set_consumer_device_supply() warn: overwrite may leak 'node'

Ah! Good catch!

Patch
diff mbox series

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index c95397798275..0408c4e1d9a8 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1445,6 +1445,22 @@  static int set_consumer_device_supply(struct regulator_dev *rdev,
 	else
 		has_dev = 0;
 
+	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
+	if (node == NULL)
+		return -ENOMEM;
+
+	node->regulator = rdev;
+	node->supply = supply;
+
+	if (has_dev) {
+		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
+		if (node->dev_name == NULL) {
+			kfree(node);
+			return -ENOMEM;
+		}
+	}
+
+	mutex_lock(&regulator_list_mutex);
 	list_for_each_entry(node, &regulator_map_list, list) {
 		if (node->dev_name && consumer_dev_name) {
 			if (strcmp(node->dev_name, consumer_dev_name) != 0)
@@ -1462,26 +1478,19 @@  static int set_consumer_device_supply(struct regulator_dev *rdev,
 			 node->regulator->desc->name,
 			 supply,
 			 dev_name(&rdev->dev), rdev_get_name(rdev));
-		return -EBUSY;
-	}
-
-	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
-	if (node == NULL)
-		return -ENOMEM;
-
-	node->regulator = rdev;
-	node->supply = supply;
-
-	if (has_dev) {
-		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
-		if (node->dev_name == NULL) {
-			kfree(node);
-			return -ENOMEM;
-		}
+		goto fail;
 	}
 
 	list_add(&node->list, &regulator_map_list);
+	mutex_unlock(&regulator_list_mutex);
+
 	return 0;
+
+fail:
+	mutex_unlock(&regulator_list_mutex);
+	kfree(node->dev_name);
+	kfree(node);
+	return -EBUSY;
 }
 
 static void unset_regulator_supplies(struct regulator_dev *rdev)
@@ -5239,19 +5248,16 @@  regulator_register(const struct regulator_desc *regulator_desc,
 
 	/* add consumers devices */
 	if (init_data) {
-		mutex_lock(&regulator_list_mutex);
 		for (i = 0; i < init_data->num_consumer_supplies; i++) {
 			ret = set_consumer_device_supply(rdev,
 				init_data->consumer_supplies[i].dev_name,
 				init_data->consumer_supplies[i].supply);
 			if (ret < 0) {
-				mutex_unlock(&regulator_list_mutex);
 				dev_err(dev, "Failed to set supply %s\n",
 					init_data->consumer_supplies[i].supply);
 				goto unset_supplies;
 			}
 		}
-		mutex_unlock(&regulator_list_mutex);
 	}
 
 	if (!rdev->desc->ops->get_voltage &&