* [PATCH] HID: steam: fix deadlock with input devices.
@ 2019-03-15 19:09 Rodrigo Rivas Costa
2019-03-18 13:44 ` Jiri Kosina
0 siblings, 1 reply; 2+ messages in thread
From: Rodrigo Rivas Costa @ 2019-03-15 19:09 UTC (permalink / raw)
To: Benjamin Tissoires, Jiri Kosina, Pierre-Loup A. Griffais, lkml,
linux-input, Simon Gene Gottlieb
Cc: Rodrigo Rivas Costa
When using this driver with the wireless dongle and some usermode
program that monitors every input device (acpid, for example), while
another usermode client opens and closes the low-level device
repeadedly, the system eventually deadlocks.
The reason is that steam_input_register_device() must not be called with
the mutex held, because the input subsystem has its own synchronization
that clashes with this one: it is possible that steam_input_open() is
called before input_register_device() returns, and since
steam_input_open() needs to lock the mutex, it deadlocks.
However we must hold the mutex when calling any function that sends
commands to the controller. If not, random commands end up falling fail.
Reported-by: Simon Gene Gottlieb <simon@gottliebtfreitag.de>
Signed-off-by: Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
Tested-by: Simon Gene Gottlieb <simon@gottliebtfreitag.de>
---
drivers/hid/hid-steam.c | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/drivers/hid/hid-steam.c b/drivers/hid/hid-steam.c
index 8141cadfca0e..8dae0f9b819e 100644
--- a/drivers/hid/hid-steam.c
+++ b/drivers/hid/hid-steam.c
@@ -499,6 +499,7 @@ static void steam_battery_unregister(struct steam_device *steam)
static int steam_register(struct steam_device *steam)
{
int ret;
+ bool client_opened;
/*
* This function can be called several times in a row with the
@@ -511,9 +512,11 @@ static int steam_register(struct steam_device *steam)
* Unlikely, but getting the serial could fail, and it is not so
* important, so make up a serial number and go on.
*/
+ mutex_lock(&steam->mutex);
if (steam_get_serial(steam) < 0)
strlcpy(steam->serial_no, "XXXXXXXXXX",
sizeof(steam->serial_no));
+ mutex_unlock(&steam->mutex);
hid_info(steam->hdev, "Steam Controller '%s' connected",
steam->serial_no);
@@ -528,13 +531,15 @@ static int steam_register(struct steam_device *steam)
}
mutex_lock(&steam->mutex);
- if (!steam->client_opened) {
+ client_opened = steam->client_opened;
+ if (!client_opened)
steam_set_lizard_mode(steam, lizard_mode);
+ mutex_unlock(&steam->mutex);
+
+ if (!client_opened)
ret = steam_input_register(steam);
- } else {
+ else
ret = 0;
- }
- mutex_unlock(&steam->mutex);
return ret;
}
@@ -630,14 +635,21 @@ static void steam_client_ll_close(struct hid_device *hdev)
{
struct steam_device *steam = hdev->driver_data;
+ unsigned long flags;
+ bool connected;
+
+ spin_lock_irqsave(&steam->lock, flags);
+ connected = steam->connected;
+ spin_unlock_irqrestore(&steam->lock, flags);
+
mutex_lock(&steam->mutex);
steam->client_opened = false;
+ if (connected)
+ steam_set_lizard_mode(steam, lizard_mode);
mutex_unlock(&steam->mutex);
- if (steam->connected) {
- steam_set_lizard_mode(steam, lizard_mode);
+ if (connected)
steam_input_register(steam);
- }
}
static int steam_client_ll_raw_request(struct hid_device *hdev,
--
2.21.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] HID: steam: fix deadlock with input devices.
2019-03-15 19:09 [PATCH] HID: steam: fix deadlock with input devices Rodrigo Rivas Costa
@ 2019-03-18 13:44 ` Jiri Kosina
0 siblings, 0 replies; 2+ messages in thread
From: Jiri Kosina @ 2019-03-18 13:44 UTC (permalink / raw)
To: Rodrigo Rivas Costa
Cc: Benjamin Tissoires, Pierre-Loup A. Griffais, lkml, linux-input,
Simon Gene Gottlieb
On Fri, 15 Mar 2019, Rodrigo Rivas Costa wrote:
> When using this driver with the wireless dongle and some usermode
> program that monitors every input device (acpid, for example), while
> another usermode client opens and closes the low-level device
> repeadedly, the system eventually deadlocks.
>
> The reason is that steam_input_register_device() must not be called with
> the mutex held, because the input subsystem has its own synchronization
> that clashes with this one: it is possible that steam_input_open() is
> called before input_register_device() returns, and since
> steam_input_open() needs to lock the mutex, it deadlocks.
>
> However we must hold the mutex when calling any function that sends
> commands to the controller. If not, random commands end up falling fail.
>
> Reported-by: Simon Gene Gottlieb <simon@gottliebtfreitag.de>
> Signed-off-by: Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
> Tested-by: Simon Gene Gottlieb <simon@gottliebtfreitag.de>
Applied to for-5.1/upstream-fixes, thanks.
--
Jiri Kosina
SUSE Labs
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-03-18 13:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-15 19:09 [PATCH] HID: steam: fix deadlock with input devices Rodrigo Rivas Costa
2019-03-18 13:44 ` Jiri Kosina
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).