Skip to contentSkip to conversation

gpio: sim: fix the configfs lockup racing with mkdir/rmdir

PATCH RESEND New Yu Zhang 2 participants 2 messages Nov 3

Subsystems (2)
lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH RESEND v3 1/1 gpio: sim: fix the configfs lockup racing with mkdir/rmdir

gpio_sim_device_lockup_configfs() depends (on activation) and undepends (on deactivation) the leaf configfs entries of a device. It walks dev->bank_list and bank->line_list without dev->lock, while the lists are modified and their nodes freed under dev->lock by the configfs make/release callbacks. It also walks the lists twice, once to depend and once to undepend, and the two walks can see different entries:

- A hog created while the device is live is never depended on, but it is undepended on deactivation, hitting BUG_ON(sd->s_dependent_count < 1) in configfs_undepend_item(). The following is enough to trigger it:

mkdir $G/dev $G/dev/bank0 $G/dev/bank0/line0 echo 1 > $G/dev/live
mkdir $G/dev/bank0/line0/hog
echo 0 > $G/dev/live

- An entry that is being created or removed when the device goes live cannot be depended on (the WARN_ON() fires), but it is undepended later, hitting the same BUG_ON(). If it has already been unlinked, configfs_depend_item_unlocked() dereferences its NULL ci_group.

Toggling "live" while concurrently creating and removing line and hog directories hits the WARN_ON() and then the BUG_ON() within a second.

Hold the configfs subsystem mutex and dev->lock across the whole live transition. The subsystem mutex is what configfs holds when it links and unlinks entries and when it calls the make_group()/make_item() callbacks, which then take dev->lock, so the lock order is the same. Record the entries that were actually depended on and undepend exactly those. If an entry cannot be depended on because it is being created or removed, undo the dependencies already taken and fail with -EBUSY instead of warning.

configfs_depend_item_unlocked() and configfs_undepend_item_unlocked() only take configfs_dirent_lock for an item of the caller's own subsystem, so calling them under these locks is fine.

Fixes 8bd76b3d3f3a ("gpio: sim: lock up configfs that an instantiated device depends on")

---
v3:
- Count the lines with list_count_nodes() (Bartosz).
v2: https://lore.kernel.org/all/[email protected]/
- Supersedes v1 "gpio: sim: snapshot configfs leaf items before depending on them". The v1 approach of taking references on the entries and depending on them after dropping dev->lock still raced with mkdir/rmdir, as the sashiko review pointed out, so the fix was redone: hold the subsystem mutex and dev->lock across the whole live transition, record the entries that were depended on and fail with -EBUSY instead of warning.
v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/gpio/gpio-sim.c | 108 +++++++++++++++++++++++++++++++---------
 1 file changed, 84 insertions(+), 24 deletions(-)
drivers/gpio/gpio-sim.c +84 -24
index 0c73bec..abc73bb 100644
@@ -560,6 +560,10 @@ struct gpio_sim_device {
 	struct mutex lock;
 
 	struct list_head bank_list;
+
+	/* Leaf entries depended on while the device is live. */
+	struct config_item **deps;
+	unsigned int num_deps;
 };
 
 static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
@@ -974,31 +978,80 @@ static void gpio_sim_device_deactivate(struct gpio_sim_device *dev)
 	gpio_sim_remove_swnode_recursive(swnode);
 }
 
-static void
-gpio_sim_device_lockup_configfs(struct gpio_sim_device *dev, bool lock)
+static void gpio_sim_device_unlock_configfs(struct gpio_sim_device *dev)
+{
+	unsigned int i;
+
+	lockdep_assert_held(&dev->lock);
+
+	for (i = 0; i < dev->num_deps; i++)
+		configfs_undepend_item_unlocked(dev->deps[i]);
+
+	kfree(dev->deps);
+	dev->deps = NULL;
+	dev->num_deps = 0;
+}
+
+static int gpio_sim_device_lockup_configfs(struct gpio_sim_device *dev)
 {
 	struct configfs_subsystem *subsys = dev->group.cg_subsys;
 	struct gpio_sim_bank *bank;
 	struct gpio_sim_line *line;
 	struct config_item *item;
+	unsigned int num = 0;
+	int ret;
+
+	lockdep_assert_held(&subsys->su_mutex);
+	lockdep_assert_held(&dev->lock);
 
 	/*
 	 * The device only needs to depend on leaf entries. This is
 	 * sufficient to lock up all the configfs entries that the
 	 * instantiated, alive device depends on.
+	 *
+	 * dev->lock keeps the bank and line lists stable and the subsystem
+	 * mutex keeps the entries' ci_group stable. An entry that is still
+	 * being created or removed cannot be depended on, in which case the
+	 * device is not brought up. The entries that were actually depended
+	 * on are recorded, so that exactly those are undepended when the
+	 * device goes down.
 	 */
+	list_for_each_entry(bank, &dev->bank_list, siblings)
+		num += list_count_nodes(&bank->line_list);
+
+	if (!num)
+		return 0;
+
+	dev->deps = kzalloc_objs(*dev->deps, num);
+	if (!dev->deps)
+		return -ENOMEM;
+
 	list_for_each_entry(bank, &dev->bank_list, siblings) {
 		list_for_each_entry(line, &bank->line_list, siblings) {
 			item = line->hog ? &line->hog->item
 					 : &line->group.cg_item;
 
-			if (lock)
-				WARN_ON(configfs_depend_item_unlocked(subsys,
-								      item));
-			else
-				configfs_undepend_item_unlocked(item);
+			/* Already unlinked by a concurrent rmdir(). */
+			if (!item->ci_group) {
+				ret = -EBUSY;
+				goto err_unlock;
+			}
+
+			ret = configfs_depend_item_unlocked(subsys, item);
+			if (ret) {
+				ret = -EBUSY;
+				goto err_unlock;
+			}
+
+			dev->deps[dev->num_deps++] = item;
 		}
 	}
+
+	return 0;
+
+err_unlock:
+	gpio_sim_device_unlock_configfs(dev);
+	return ret;
 }
 
 static ssize_t
@@ -1013,26 +1066,32 @@ gpio_sim_device_config_live_store(struct config_item *item,
 	if (ret)
 		return ret;
 
-	if (live)
-		gpio_sim_device_lockup_configfs(dev, true);
-
-	scoped_guard(mutex, &dev->lock) {
-		if (live == gpio_sim_device_is_live(dev))
-			ret = -EPERM;
-		else if (live)
-			ret = gpio_sim_device_activate(dev);
-		else
-			gpio_sim_device_deactivate(dev);
-	}
-
 	/*
-	 * Undepend is required only if device disablement (live == 0)
-	 * succeeds or if device enablement (live == 1) fails.
+	 * Lock order: subsystem mutex, then dev->lock. This is the same
+	 * order as in the make_group()/make_item() callbacks.
 	 */
-	if (live == !!ret)
-		gpio_sim_device_lockup_configfs(dev, false);
+	guard(mutex)(&dev->group.cg_subsys->su_mutex);
+	guard(mutex)(&dev->lock);
+
+	if (live == gpio_sim_device_is_live(dev))
+		return -EPERM;
+
+	if (live) {
+		ret = gpio_sim_device_lockup_configfs(dev);
+		if (ret)
+			return ret;
 
-	return ret ?: count;
+		ret = gpio_sim_device_activate(dev);
+		if (ret) {
+			gpio_sim_device_unlock_configfs(dev);
+			return ret;
+		}
+	} else {
+		gpio_sim_device_deactivate(dev);
+		gpio_sim_device_unlock_configfs(dev);
+	}
+
+	return count;
 }
 
 CONFIGFS_ATTR(gpio_sim_device_config_, live);
@@ -1546,6 +1605,7 @@ static void gpio_sim_device_config_group_release(struct config_item *item)
 
 	mutex_destroy(&dev->lock);
 	ida_free(&gpio_sim_ida, dev->id);
+	kfree(dev->deps);
 	kfree(dev);
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] gpio: sim: empty banks not depended on in gpio_sim_device_lockup_configfs() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] gpio: sim: empty banks not depended on in gpio_sim_device_lockup_configfs() --

commit 912c87ed34604f75f35efcd268b39a5c4d993d4d
Author: Yu Zhang <[email protected]>

gpio: sim: fix the configfs lockup racing with mkdir/rmdir

This commit fixes races between toggling a gpio-sim device's live state and concurrent configfs directory creation or removal by holding both the configfs subsystem mutex and dev->lock across the transition and recording the leaf configfs items that were depended on.

Quoted 4 lines
drivers/gpio/gpio-sim.c +0 -0
index 0c73bec042670..abc73bb0b44b0 100644

[ ... ]

Quoted 48 lines
drivers/gpio/gpio-sim.c +31 -2
@@ -974,31 +978,80 @@ static void gpio_sim_device_deactivate(struct gpio_sim_device *dev)
 	gpio_sim_remove_swnode_recursive(swnode);
 }
 
-static void
-gpio_sim_device_lockup_configfs(struct gpio_sim_device *dev, bool lock)
+static void gpio_sim_device_unlock_configfs(struct gpio_sim_device *dev)
+{
+	unsigned int i;
+
+	lockdep_assert_held(&dev->lock);
+
+	for (i = 0; i < dev->num_deps; i++)
+		configfs_undepend_item_unlocked(dev->deps[i]);
+
+	kfree(dev->deps);
+	dev->deps = NULL;
+	dev->num_deps = 0;
+}
+
+static int gpio_sim_device_lockup_configfs(struct gpio_sim_device *dev)
 {
 	struct configfs_subsystem *subsys = dev->group.cg_subsys;
 	struct gpio_sim_bank *bank;
 	struct gpio_sim_line *line;
 	struct config_item *item;
+	unsigned int num = 0;
+	int ret;
+
+	lockdep_assert_held(&subsys->su_mutex);
+	lockdep_assert_held(&dev->lock);
 
 	/*
 	 * The device only needs to depend on leaf entries. This is
 	 * sufficient to lock up all the configfs entries that the
 	 * instantiated, alive device depends on.
+	 *
+	 * dev->lock keeps the bank and line lists stable and the subsystem
+	 * mutex keeps the entries' ci_group stable. An entry that is still
+	 * being created or removed cannot be depended on, in which case the
+	 * device is not brought up. The entries that were actually depended
+	 * on are recorded, so that exactly those are undepended when the
+	 * device goes down.
 	 */
+	list_for_each_entry(bank, &dev->bank_list, siblings)
+		num += list_count_nodes(&bank->line_list);
+
+	if (!num)

drivers/gpio/gpio-sim.c static void gpio_sim_device_deactivate(struct gpio_sim_device *dev) · line 1032

Quoted 10 lines
drivers/gpio/gpio-sim.c +6 -0
+		return 0;
+
+	dev->deps = kzalloc_objs(*dev->deps, num);
+	if (!dev->deps)
+		return -ENOMEM;
+
 	list_for_each_entry(bank, &dev->bank_list, siblings) {
 		list_for_each_entry(line, &bank->line_list, siblings) {
 			item = line->hog ? &line->hog->item
 					 : &line->group.cg_item;

[Severity: High]
What happens in gpio_sim_device_lockup_configfs() when a bank in dev->bank_list has an empty bank->line_list?

Creating line<offset> subdirectories under a bank is optional (a new bank defaults to num_lines = 1 with an empty bank->line_list). When bank->line_list is empty, the leaf configfs entry for that bank is &bank->group.cg_item, but gpio_sim_device_lockup_configfs() only counts and iterates over bank->line_list.

As a result, &bank->group.cg_item is never passed to configfs_depend_item_unlocked(). This appears to leave two issues:

1. If a device is activated with a bank that has no line subdirectories, userspace can remove the bank directory (and then the device directory) with rmdir while the platform device is still live.

2. A bank being created by configfs_mkdir() or removed by configfs_rmdir() always has an empty bank->line_list. During configfs_rmdir() of a bank, unlink_group() clears bank->group.cg_item.ci_group and drops subsys->su_mutex before config_item_put() calls gpio_sim_bank_config_group_release():

   drivers/gpio/gpio-sim.c:gpio_sim_bank_config_group_release() {
       ...
       scoped_guard(mutex, &dev->lock)
           list_del(&bank->siblings);
       kfree(bank->label);
       kfree(bank);

}

Similarly, during configfs_mkdir() of a bank, gpio_sim_device_config_make_bank_group() adds the bank to dev->bank_list before configfs_mkdir() drops subsys->su_mutex and finishes attaching the group (or unlinks and frees it on error).

If gpio_sim_device_config_live_store() runs during either window, gpio_sim_device_lockup_configfs() skips the empty bank instead of checking &bank->group.cg_item and returning -EBUSY, allowing gpio_sim_device_activate() to use a bank that is about to be unlinked and freed.

Should a bank with an empty line_list be counted and depended on via &bank->group.cg_item as a leaf item?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1