Skip to contentSkip to conversation

platform/x86: thinkpad_acpi: fix deadlock in hotkey input open/close

PATCH In review Dmitry Torokhov 2 participants 2 messages Oct 10 – Oct 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: thinkpad_acpi: fix deadlock in hotkey input open/close

Commit d6479c25ecef ("platform/x86: thinkpad_acpi: Use input_device_enabled()") changed hotkey_poll_setup() to acquire tpacpi_inputdev->mutex and call input_device_enabled(tpacpi_inputdev).

However, the input core already holds dev->mutex when invoking a device's open() and close() callbacks. Because hotkey_inputdev_open() and hotkey_inputdev_close() call into hotkey_poll_setup(), attempting to acquire tpacpi_inputdev->mutex again results in an immediate self-deadlock.

Additionally, locking tpacpi_inputdev->mutex inside hotkey_poll_setup() while holding hotkey_mutex introduces an ABBA lock ordering inversion with hotkey_inputdev_open() and hotkey_inputdev_close(), which acquire hotkey_mutex while holding tpacpi_inputdev->mutex. Finally, input_inhibit_device() calls dev->close() before setting dev->inhibited, so input_device_enabled() still returns true inside hotkey_inputdev_close().

Track whether the input device is active in a driver-local boolean protected by hotkey_mutex instead of locking tpacpi_inputdev->mutex and querying input_device_enabled().

---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/platform/x86/lenovo/thinkpad_acpi.c +11 -5
index 1225b01ed14d..40e9763a12f8 100644
@@ -1887,6 +1887,7 @@ static u32 hotkey_reserved_mask;	/* events better left disabled */
 static u32 hotkey_driver_mask;		/* events needed by the driver */
 static u32 hotkey_user_mask;		/* events visible to userspace */
 static u32 hotkey_acpi_mask;		/* events enabled in firmware */
+static bool hotkey_input_enabled;
 
 static bool tpacpi_driver_event(const unsigned int hkey_event);
 static void hotkey_poll_setup(const bool may_warn);
@@ -2544,10 +2545,8 @@ static void hotkey_poll_setup(const bool may_warn)
 
 	lockdep_assert_held(&hotkey_mutex);
 
-	guard(mutex)(&tpacpi_inputdev->mutex);
 	if (hotkey_poll_freq > 0 &&
-	    (poll_driver_mask ||
-	     (poll_user_mask && input_device_enabled(tpacpi_inputdev)))) {
+	    (poll_driver_mask || (poll_user_mask && hotkey_input_enabled))) {
 		if (!tpacpi_hotkey_task) {
 			tpacpi_hotkey_task = kthread_run(hotkey_kthread,
 					NULL, TPACPI_NVRAM_KTHREAD_NAME);
@@ -2602,7 +2601,10 @@ static int hotkey_inputdev_open(struct input_dev *dev)
 	switch (tpacpi_lifecycle) {
 	case TPACPI_LIFE_INIT:
 	case TPACPI_LIFE_RUNNING:
-		hotkey_poll_setup_safe(false);
+		scoped_guard(mutex, &hotkey_mutex) {
+			hotkey_input_enabled = true;
+			hotkey_poll_setup(false);
+		}
 		return 0;
 	case TPACPI_LIFE_EXITING:
 		return -EBUSY;
@@ -2615,10 +2617,14 @@ static int hotkey_inputdev_open(struct input_dev *dev)
 
 static void hotkey_inputdev_close(struct input_dev *dev)
 {
+	guard(mutex)(&hotkey_mutex);
+
+	hotkey_input_enabled = false;
+
 	/* disable hotkey polling when possible */
 	if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
 	    !(hotkey_source_mask & hotkey_driver_mask))
-		hotkey_poll_setup_safe(false);
+		hotkey_poll_setup(false);
 }
 
 /* sysfs hotkey enable ------------------------------------------------- */
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
maintainer
#1 lore raw
Quoted 11 lines · Dmitry Torokhov

Commit d6479c25ecef ("platform/x86: thinkpad_acpi: Use input_device_enabled()") changed hotkey_poll_setup() to acquire tpacpi_inputdev->mutex and call input_device_enabled(tpacpi_inputdev).

However, the input core already holds dev->mutex when invoking a device's open() and close() callbacks. Because hotkey_inputdev_open() and hotkey_inputdev_close() call into hotkey_poll_setup(), attempting to acquire tpacpi_inputdev->mutex again results in an immediate self-deadlock.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.

The list of commits applied:
[1/1] platform/x86: thinkpad_acpi: fix deadlock in hotkey input open/close

      commit: 16a7b5e0e4e3ba9997658b218559fff7ac1ccfb3

--
i.