Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9bb0f69e authored by Rodrigo Rivas Costa's avatar Rodrigo Rivas Costa Committed by Siarhei Vishniakou
Browse files

UPSTREAM: HID: steam: fix deadlock with input devices.

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.

(cherry picked from commit 6b538cc21334b83f09b25dec4aa2d2726bf07ed0 ("HID: steam: fix deadlock with input devices."))
https://github.com/torvalds/linux/commit/6b538cc21334b83f09b25dec4aa2d2726bf07ed0



Bug: 136263708

Reported-by: default avatarSimon Gene Gottlieb <simon@gottliebtfreitag.de>
Signed-off-by: default avatarRodrigo Rivas Costa <rodrigorivascosta@gmail.com>
Tested-by: default avatarSimon Gene Gottlieb <simon@gottliebtfreitag.de>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
Signed-off-by: default avatarSiarhei Vishniakou <svv@google.com>
Change-Id: Ia0c37b3117dc605a30b2e1fb5030282bf2e11a11
parent 1b988daf
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -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,15 +635,22 @@ 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,
				unsigned char reportnum, u8 *buf,