# Re: \[PATCH v2\] HID: Replace semaphore driver\_lock with mutex

- From: Binoy Jayan \<hidden\>
- Date: 2017-06-14 05:22:34 
- List: linux-input
- Page: https://ratatoskr.run/linux-input/2017/06/5020412
- Thread: https://ratatoskr.run/linux-input/2017/06/5019621/t.md

```text
Hi,

On 14 June 2017 at 01:55, Arnd Bergmann [ref] wrote:

>> The mutex code clearly states mutex_trylock() must not be used in
>> interrupt context (see kernel/locking/mutex.c), hence we used a
>> semaphore here. Unless the mutex code is changed to allow this, we
>> cannot switch away from semaphores.
>
> Right, that makes a lot of sense. I don't think changing the mutex
> code is an option here, but I wonder if we can replace the semaphore
> with something simpler anyway.
>
> From what I can tell, it currently does two things:
>
> 1. it acts as a simple flag to prevent  hid_input_report from derefencing
>     the hid->driver pointer during initialization and exit. I think this could
>     be done equally well using a simple atomic set_bit()/test_bit() or similar.
>
> 2. it prevents the hid->driver pointer from becoming invalid while an
>     asynchronous hid_input_report() is in progress. This actually seems to
>     be a reference counting problem rather than a locking problem.
>     I don't immediately see how to better address it, or how exactly this
>     could go wrong in practice, but I would naively expect that either
>     hdev->driver->remove() needs to wait for the last user of hdev->driver
>     to complete, or we need kref_get/kref_put in hid_input_report()
>     to trigger the actual release function.

Thank you everyone for the comments. I'll resend the patch with Benjamin's
comments incorporated and address the changes in the second semaphore later.

Regards,
Binoy
```
