[PATCH] Input: maple_keyb - use guard notation when acquiring mutex

Dmitry Torokhov posted 1 patch 1 month ago
There is a newer version of this series
drivers/input/keyboard/maple_keyb.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
[PATCH] Input: maple_keyb - use guard notation when acquiring mutex
Posted by Dmitry Torokhov 1 month ago
Using guard notation makes the code more compact and error handling
more robust by ensuring that mutexes are released in all code paths
when control leaves critical section.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/keyboard/maple_keyb.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/input/keyboard/maple_keyb.c b/drivers/input/keyboard/maple_keyb.c
index 91a1d2958109..fca7df1e3ae3 100644
--- a/drivers/input/keyboard/maple_keyb.c
+++ b/drivers/input/keyboard/maple_keyb.c
@@ -132,14 +132,11 @@ static void dc_kbd_callback(struct mapleq *mq)
 	 * We should always get the lock because the only
 	 * time it may be locked is if the driver is in the cleanup phase.
 	 */
-	if (likely(mutex_trylock(&maple_keyb_mutex))) {
-
+	scoped_guard(mutex_try, &maple_keyb_mutex) {
 		if (buf[1] == mapledev->function) {
 			memcpy(kbd->new, buf + 2, 8);
 			dc_scan_kbd(kbd);
 		}
-
-		mutex_unlock(&maple_keyb_mutex);
 	}
 }
 
@@ -211,14 +208,12 @@ static int remove_maple_kbd(struct device *dev)
 	struct maple_device *mdev = to_maple_dev(dev);
 	struct dc_kbd *kbd = maple_get_drvdata(mdev);
 
-	mutex_lock(&maple_keyb_mutex);
+	guard(mutex, &maple_keyb_mutex);
 
 	input_unregister_device(kbd->dev);
 	kfree(kbd);
 
 	maple_set_drvdata(mdev, NULL);
-
-	mutex_unlock(&maple_keyb_mutex);
 	return 0;
 }
 
-- 
2.47.0.163.g1226f6d8fa-goog


-- 
Dmitry
Re: [PATCH] Input: maple_keyb - use guard notation when acquiring mutex
Posted by kernel test robot 1 month ago
Hi Dmitry,

kernel test robot noticed the following build errors:

[auto build test ERROR on dtor-input/next]
[also build test ERROR on dtor-input/for-linus hid/for-next linus/master v6.12-rc4 next-20241025]
[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#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Dmitry-Torokhov/Input-maple_keyb-use-guard-notation-when-acquiring-mutex/20241025-094751
base:   https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git next
patch link:    https://lore.kernel.org/r/Zxr4TeGwDGIIyzwH%40google.com
patch subject: [PATCH] Input: maple_keyb - use guard notation when acquiring mutex
config: sh-dreamcast_defconfig (https://download.01.org/0day-ci/archive/20241026/202410261312.femv4WzA-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241026/202410261312.femv4WzA-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410261312.femv4WzA-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/input/keyboard/maple_keyb.c: In function 'remove_maple_kbd':
>> drivers/input/keyboard/maple_keyb.c:211:39: error: macro "guard" passed 2 arguments, but takes just 1
     211 |         guard(mutex, &maple_keyb_mutex);
         |                                       ^
   In file included from include/linux/irqflags.h:17,
                    from arch/sh/include/asm/cmpxchg-irq.h:5,
                    from arch/sh/include/asm/cmpxchg.h:20,
                    from arch/sh/include/asm/atomic.h:19,
                    from include/linux/atomic.h:7,
                    from include/asm-generic/bitops/atomic.h:5,
                    from arch/sh/include/asm/bitops.h:23,
                    from include/linux/bitops.h:68,
                    from include/linux/kernel.h:23,
                    from drivers/input/keyboard/maple_keyb.c:9:
   include/linux/cleanup.h:166: note: macro "guard" defined here
     166 | #define guard(_name) \
         | 
>> drivers/input/keyboard/maple_keyb.c:211:9: error: 'guard' undeclared (first use in this function)
     211 |         guard(mutex, &maple_keyb_mutex);
         |         ^~~~~
   drivers/input/keyboard/maple_keyb.c:211:9: note: each undeclared identifier is reported only once for each function it appears in


vim +/guard +211 drivers/input/keyboard/maple_keyb.c

   205	
   206	static int remove_maple_kbd(struct device *dev)
   207	{
   208		struct maple_device *mdev = to_maple_dev(dev);
   209		struct dc_kbd *kbd = maple_get_drvdata(mdev);
   210	
 > 211		guard(mutex, &maple_keyb_mutex);
   212	
   213		input_unregister_device(kbd->dev);
   214		kfree(kbd);
   215	
   216		maple_set_drvdata(mdev, NULL);
   217		return 0;
   218	}
   219	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
[PATCH v2] Input: maple_keyb - use guard notation when acquiring mutex
Posted by Dmitry Torokhov 4 weeks ago
Using guard notation makes the code more compact and error handling
more robust by ensuring that mutexes are released in all code paths
when control leaves critical section.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---

v2: fix typo pointed out by the kernel test robot

 drivers/input/keyboard/maple_keyb.c |    9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/input/keyboard/maple_keyb.c b/drivers/input/keyboard/maple_keyb.c
index 91a1d2958109..1a8f1fa53fbb 100644
--- a/drivers/input/keyboard/maple_keyb.c
+++ b/drivers/input/keyboard/maple_keyb.c
@@ -132,14 +132,11 @@ static void dc_kbd_callback(struct mapleq *mq)
 	 * We should always get the lock because the only
 	 * time it may be locked is if the driver is in the cleanup phase.
 	 */
-	if (likely(mutex_trylock(&maple_keyb_mutex))) {
-
+	scoped_guard(mutex_try, &maple_keyb_mutex) {
 		if (buf[1] == mapledev->function) {
 			memcpy(kbd->new, buf + 2, 8);
 			dc_scan_kbd(kbd);
 		}
-
-		mutex_unlock(&maple_keyb_mutex);
 	}
 }
 
@@ -211,14 +208,12 @@ static int remove_maple_kbd(struct device *dev)
 	struct maple_device *mdev = to_maple_dev(dev);
 	struct dc_kbd *kbd = maple_get_drvdata(mdev);
 
-	mutex_lock(&maple_keyb_mutex);
+	guard(mutex)(&maple_keyb_mutex);
 
 	input_unregister_device(kbd->dev);
 	kfree(kbd);
 
 	maple_set_drvdata(mdev, NULL);
-
-	mutex_unlock(&maple_keyb_mutex);
 	return 0;
 }
 

-- 
Dmitry