Skip to contentSkip to conversation

android: binderfs: drop manual unlock via scoped_guard()

PATCH In review Biren Pandya 2 participants 2 messages Jun 16 – Jun 17 Android drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 android: binderfs: drop manual unlock via scoped_guard()

The device_create function manually protects the minor number allocation with binderfs_minors_mutex and relies on multiple manual unlocks on success and error paths. This is error-prone and adds boilerplate.

Refactor the critical section to use the modern scoped_guard(mutex) macro. This strictly binds the lock lifecycle to the scope block, completely eliminating the manual unlocks and guaranteeing lock safety against future code modifications.

---
 drivers/android/binderfs.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/android/binderfs.c +12 -13
index 361d69f756f5..ad5d9439ff87 100644
@@ -129,20 +129,19 @@ static int binderfs_binder_device_create(struct inode *ref_inode,
 #endif
 
 	/* Reserve new minor number for the new device. */
-	mutex_lock(&binderfs_minors_mutex);
-	if (++info->device_count <= info->mount_opts.max)
-		minor = ida_alloc_max(&binderfs_minors,
-				      use_reserve ? BINDERFS_MAX_MINOR - 1 :
-						    BINDERFS_MAX_MINOR_CAPPED - 1,
-				      GFP_KERNEL);
-	else
-		minor = -ENOSPC;
-	if (minor < 0) {
-		--info->device_count;
-		mutex_unlock(&binderfs_minors_mutex);
-		return minor;
+	scoped_guard(mutex, &binderfs_minors_mutex) {
+		if (++info->device_count <= info->mount_opts.max)
+			minor = ida_alloc_max(&binderfs_minors,
+					      use_reserve ? BINDERFS_MAX_MINOR - 1 :
+							    BINDERFS_MAX_MINOR_CAPPED - 1,
+					      GFP_KERNEL);
+		else
+			minor = -ENOSPC;
+		if (minor < 0) {
+			--info->device_count;
+			return minor;
+		}
 	}
-	mutex_unlock(&binderfs_minors_mutex);
 
 	ret = -ENOMEM;
 	device = kzalloc_obj(*device);

base-commit: 64d712aa31f30a125291e7c47209ef7ebd3285a3

Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 8 lines · Biren Pandya

The device_create function manually protects the minor number allocation with binderfs_minors_mutex and relies on multiple manual unlocks on success and error paths. This is error-prone and adds boilerplate.

Refactor the critical section to use the modern scoped_guard(mutex) macro. This strictly binds the lock lifecycle to the scope block, completely eliminating the manual unlocks and guaranteeing lock safety against future code modifications.

We don't do scope guard changes that don't actually fix a bug, as it's not needed. Otherwise we would be rewriting large chunks of the kernel for it.

thanks,

greg k-h