diff mbox series

[06/14] Input: samsung-keypad - use guard notation to acquire mutex

Message ID 20240819045813.2154642-7-dmitry.torokhov@gmail.com
State New
Headers show
Series Remove support for platform data from samsung keypad | expand

Commit Message

Dmitry Torokhov Aug. 19, 2024, 4:58 a.m. UTC
Guard notation is more compact and ensures that the mutex will be
released when control leaves the function.

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

Comments

Dmitry Torokhov Aug. 22, 2024, 6:07 p.m. UTC | #1
On Thu, Aug 22, 2024 at 05:48:33PM +0200, Krzysztof Kozlowski wrote:
> On Sun, Aug 18, 2024 at 09:58:03PM -0700, Dmitry Torokhov wrote:
> > Guard notation is more compact and ensures that the mutex will be
> > released when control leaves the function.
> > 
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > ---
> >  drivers/input/keyboard/samsung-keypad.c | 8 ++------
> >  1 file changed, 2 insertions(+), 6 deletions(-)
> > 
> 
> You need to include cleanup.h (unless some other patch already did it
> and I missed it?)

Guard for mutexes defined in mutex.h which is pulled in indirectly, and
cleanup.h is included there.

If we want to list all the headers that we need instead of relying on
indirect inclusions I think we need a separate patch. But even then I
wonder if things like cleanup.h should be included by drivers
directly...

Thanks.
Krzysztof Kozlowski Aug. 23, 2024, 6:06 a.m. UTC | #2
On 22/08/2024 20:07, Dmitry Torokhov wrote:
> On Thu, Aug 22, 2024 at 05:48:33PM +0200, Krzysztof Kozlowski wrote:
>> On Sun, Aug 18, 2024 at 09:58:03PM -0700, Dmitry Torokhov wrote:
>>> Guard notation is more compact and ensures that the mutex will be
>>> released when control leaves the function.
>>>
>>> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>>> ---
>>>  drivers/input/keyboard/samsung-keypad.c | 8 ++------
>>>  1 file changed, 2 insertions(+), 6 deletions(-)
>>>
>>
>> You need to include cleanup.h (unless some other patch already did it
>> and I missed it?)
> 
> Guard for mutexes defined in mutex.h which is pulled in indirectly, and

guard() is not in mutex.h and in general we are including headers for
the things directly used in the unit.

> cleanup.h is included there.
> 
> If we want to list all the headers that we need instead of relying on

Fixing existing code is another thing. I only propose to add new code
with the header.

I don't have strong opinion anyway, so:

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof
Dmitry Torokhov Aug. 23, 2024, 8:32 a.m. UTC | #3
On Fri, Aug 23, 2024 at 08:06:17AM +0200, Krzysztof Kozlowski wrote:
> On 22/08/2024 20:07, Dmitry Torokhov wrote:
> > On Thu, Aug 22, 2024 at 05:48:33PM +0200, Krzysztof Kozlowski wrote:
> >> On Sun, Aug 18, 2024 at 09:58:03PM -0700, Dmitry Torokhov wrote:
> >>> Guard notation is more compact and ensures that the mutex will be
> >>> released when control leaves the function.
> >>>
> >>> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> >>> ---
> >>>  drivers/input/keyboard/samsung-keypad.c | 8 ++------
> >>>  1 file changed, 2 insertions(+), 6 deletions(-)
> >>>
> >>
> >> You need to include cleanup.h (unless some other patch already did it
> >> and I missed it?)
> > 
> > Guard for mutexes defined in mutex.h which is pulled in indirectly, and
> 
> guard() is not in mutex.h and in general we are including headers for
> the things directly used in the unit.

Oh, but it is:

https://elixir.bootlin.com/linux/v6.10/source/include/linux/mutex.h#L196

DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))

> 
> > cleanup.h is included there.
> > 
> > If we want to list all the headers that we need instead of relying on
> 
> Fixing existing code is another thing. I only propose to add new code
> with the header.
> 
> I don't have strong opinion anyway, so:
> 
> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Thanks!
Krzysztof Kozlowski Aug. 23, 2024, 8:52 a.m. UTC | #4
On 23/08/2024 10:32, Dmitry Torokhov wrote:
> On Fri, Aug 23, 2024 at 08:06:17AM +0200, Krzysztof Kozlowski wrote:
>> On 22/08/2024 20:07, Dmitry Torokhov wrote:
>>> On Thu, Aug 22, 2024 at 05:48:33PM +0200, Krzysztof Kozlowski wrote:
>>>> On Sun, Aug 18, 2024 at 09:58:03PM -0700, Dmitry Torokhov wrote:
>>>>> Guard notation is more compact and ensures that the mutex will be
>>>>> released when control leaves the function.
>>>>>
>>>>> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>>>>> ---
>>>>>  drivers/input/keyboard/samsung-keypad.c | 8 ++------
>>>>>  1 file changed, 2 insertions(+), 6 deletions(-)
>>>>>
>>>>
>>>> You need to include cleanup.h (unless some other patch already did it
>>>> and I missed it?)
>>>
>>> Guard for mutexes defined in mutex.h which is pulled in indirectly, and
>>
>> guard() is not in mutex.h and in general we are including headers for
>> the things directly used in the unit.
> 
> Oh, but it is:
> 
> https://elixir.bootlin.com/linux/v6.10/source/include/linux/mutex.h#L196
> 
> DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))

That's DEFINE_GUARD, not guard().

Best regards,
Krzysztof
Dmitry Torokhov Aug. 23, 2024, 3:41 p.m. UTC | #5
On Fri, Aug 23, 2024 at 10:52:54AM +0200, Krzysztof Kozlowski wrote:
> On 23/08/2024 10:32, Dmitry Torokhov wrote:
> > On Fri, Aug 23, 2024 at 08:06:17AM +0200, Krzysztof Kozlowski wrote:
> >> On 22/08/2024 20:07, Dmitry Torokhov wrote:
> >>> On Thu, Aug 22, 2024 at 05:48:33PM +0200, Krzysztof Kozlowski wrote:
> >>>> On Sun, Aug 18, 2024 at 09:58:03PM -0700, Dmitry Torokhov wrote:
> >>>>> Guard notation is more compact and ensures that the mutex will be
> >>>>> released when control leaves the function.
> >>>>>
> >>>>> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> >>>>> ---
> >>>>>  drivers/input/keyboard/samsung-keypad.c | 8 ++------
> >>>>>  1 file changed, 2 insertions(+), 6 deletions(-)
> >>>>>
> >>>>
> >>>> You need to include cleanup.h (unless some other patch already did it
> >>>> and I missed it?)
> >>>
> >>> Guard for mutexes defined in mutex.h which is pulled in indirectly, and
> >>
> >> guard() is not in mutex.h and in general we are including headers for
> >> the things directly used in the unit.
> > 
> > Oh, but it is:
> > 
> > https://elixir.bootlin.com/linux/v6.10/source/include/linux/mutex.h#L196
> > 
> > DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
> 
> That's DEFINE_GUARD, not guard().

OK, I see what you mean. I believe that cleanup.h is too low level to
include directly by anyone who does not actually define a new guard or
destructor routine. The concrete implementations of needed guards and
all necessary support is pulled together with the headers defining the
primary objects (mutex, spinlock, etc).

Thanks.
diff mbox series

Patch

diff --git a/drivers/input/keyboard/samsung-keypad.c b/drivers/input/keyboard/samsung-keypad.c
index 924327de4d8f..71f5b85b02bd 100644
--- a/drivers/input/keyboard/samsung-keypad.c
+++ b/drivers/input/keyboard/samsung-keypad.c
@@ -519,15 +519,13 @@  static int samsung_keypad_suspend(struct device *dev)
 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
 	struct input_dev *input_dev = keypad->input_dev;
 
-	mutex_lock(&input_dev->mutex);
+	guard(mutex)(&input_dev->mutex);
 
 	if (input_device_enabled(input_dev))
 		samsung_keypad_stop(keypad);
 
 	samsung_keypad_toggle_wakeup(keypad, true);
 
-	mutex_unlock(&input_dev->mutex);
-
 	return 0;
 }
 
@@ -537,15 +535,13 @@  static int samsung_keypad_resume(struct device *dev)
 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
 	struct input_dev *input_dev = keypad->input_dev;
 
-	mutex_lock(&input_dev->mutex);
+	guard(mutex)(&input_dev->mutex);
 
 	samsung_keypad_toggle_wakeup(keypad, false);
 
 	if (input_device_enabled(input_dev))
 		samsung_keypad_start(keypad);
 
-	mutex_unlock(&input_dev->mutex);
-
 	return 0;
 }