Skip to contentSkip to conversation

regulator: core: fix race condition in regulator_put()

PATCH Applied Ashay Jaiswal 2 participants 2 messages Jan 8, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 regulator: core: fix race condition in regulator_put()

The regulator framework maintains a list of consumer regulators for a regulator device and protects it from concurrent access using the regulator device's mutex lock.

In the case of regulator_put() the consumer is removed and regulator device's parameters are updated without holding the regulator device's mutex. This would lead to a race condition between the regulator_put() and any function which traverses the consumer list or modifies regulator device's parameters.
Fix this race condition by holding the regulator device's mutex in case of regulator_put.

---
Changes:
V1-->V2:
- Updated comment on top of _regulator_put function
- Updated lock region

 drivers/regulator/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/regulator/core.c +3 -1
index c2554d8..f70a02c 100644
@@ -1488,7 +1488,7 @@ struct regulator *regulator_get_optional(struct device *dev, const char *id)
 }
 EXPORT_SYMBOL_GPL(regulator_get_optional);
 
-/* Locks held by regulator_put() */
+/* regulator_list_mutex lock held by regulator_put() */
 static void _regulator_put(struct regulator *regulator)
 {
 	struct regulator_dev *rdev;
@@ -1503,12 +1503,14 @@ static void _regulator_put(struct regulator *regulator)
 	/* remove any sysfs entries */
 	if (regulator->dev)
 		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
+	mutex_lock(&rdev->mutex);
 	kfree(regulator->supply_name);
 	list_del(&regulator->list);
 	kfree(regulator);
 
 	rdev->open_count--;
 	rdev->exclusive = 0;
+	mutex_unlock(&rdev->mutex);
 
 	module_put(rdev->owner);
 }

--
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation

maintainer Applied
#1 lore raw
Quoted 3 lines · Ashay Jaiswal

The regulator framework maintains a list of consumer regulators for a regulator device and protects it from concurrent access using the regulator device's mutex lock.

Applied, thanks but...

drivers/regulator/core.c static void _regulator_put(struct regulator *regulator) · line 1513

Quoted 8 lines
drivers/regulator/core.c +2 -0
+	mutex_lock(&rdev->mutex);
 	kfree(regulator->supply_name);
 	list_del(&regulator->list);
 	kfree(regulator);
 
 	rdev->open_count--;
 	rdev->exclusive = 0;
+	mutex_unlock(&rdev->mutex);

I'll do a followup which moves the frees out of the locked region (reordering things a bit) - they're the main bit I was worried about with time, they should be fast since they're just frees but it's easy enough to avoid having to think about it.