[PATCH] android: binderfs: drop manual unlock via scoped_guard()

From: Biren Pandya

Date: Tue Jun 16 2026 - 13:54:27 EST


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.

Signed-off-by: Biren Pandya <birenpandya@xxxxxxxxx>
---
drivers/android/binderfs.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/drivers/android/binderfs.c b/drivers/android/binderfs.c
index 361d69f756f5..ad5d9439ff87 100644
--- a/drivers/android/binderfs.c
+++ b/drivers/android/binderfs.c
@@ -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
--
2.50.1 (Apple Git-155)