Skip to contentSkip to conversation

platform/x86: dell-laptop: Fix keyboard backlight and charge threshold handling

PATCH New Rosen Penev 1 participant 4 messages Nov 27

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

v4 cover platform/x86: dell-laptop: Fix keyboard backlight and charge threshold handling

Three fixes for dell-laptop: keyboard backlight timeouts that overflow during unit conversion were silently accepted, an unread keyboard state was used at init when the SMBIOS call failed, and the charge end threshold was parsed with kstrtouint() into an int.

These patches were developed with the help of an AI coding assistant (Claude Opus 5.5): it reviewed the drivers, found the issues and drafted the fixes and changelogs. I reviewed every change and I am responsible for them.

All patches build cleanly with W=1.

Tested on a Dell Precision M4800 running v7.2.15, using the equivalent backports built as out-of-tree modules:
- writing 4424214d / 18989323d to stop_timeout set 4m / 2m before patch 1 and is rejected with -EINVAL after it; 5m still works (v1 of patch 1; v2 is compile-tested only)
- charge_control_end_threshold rejects -1 and 101, accepts 85 (v1 and v2 of patch 3; v3 is compile-tested only)
The kbd_get_state() failure path of patch 2 was not triggered.

Changes in v4:
- patches 1, 2: Fixes: now points at 6cff8d60aa0a; 02b2aaaa57ab was reverted by b78695a71de9 and the feature re-added in 6cff8d60aa0a (review feedback)
v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- patch 3: make end unsigned int and drop the dead "< 0" check instead of switching to kstrtoint(); retitled (review feedback)
v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- patch 1: convert to a u64 seconds value instead of failing check_mul_overflow() on the int, so the unit selection below works on the full value; retitled (review feedback)
- patch 2: don't fail kbd_init_info() when kbd_get_state() fails; fall back to level 0 / off mode so the existing code picks sane defaults (review feedback)
v1: https://lore.kernel.org/r/[email protected]

Rosen Penev (3):
platform/x86: dell-laptop: Fix keyboard backlight timeout overflow platform/x86: dell-laptop: Don't use an unread keyboard state at init platform/x86: dell-laptop: Make the charge end threshold unsigned

 drivers/platform/x86/dell/dell-laptop.c | 50 +++++++++++++++----------
 1 file changed, 31 insertions(+), 19 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH v4 1/3 platform/x86: dell-laptop: Fix keyboard backlight timeout overflow

kbd_led_timeout_store() converts a timeout that is too large for its unit to seconds with plain int multiplication. A large enough value wraps, and if the wrapped number happens to be a valid timeout it is accepted. On a Precision M4800, writing "4424214d" (over 12000 years) to stop_timeout sets the timeout to 4 minutes, and "18989323d" sets it to 2 minutes.

Do the conversion and the following unit selection on a u64 seconds value instead. With value <= INT_MAX this cannot overflow, so such inputs are now rejected with -EINVAL like any other timeout that is too large for the hardware, and nothing that was valid before is rejected.

Fixes 6cff8d60aa0a ("platform: x86: dell-laptop: Add support for keyboard backlight")

---
v4: Fixes: 6cff8d60aa0a instead of the reverted 02b2aaaa57ab
v3: unchanged
v2: use a u64 seconds value instead of check_mul_overflow() on the int

 drivers/platform/x86/dell/dell-laptop.c | 34 +++++++++++++++----------
 1 file changed, 20 insertions(+), 14 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +20 -14
index cad2bd06f28a..c1a51bf95c25 100644
@@ -25,6 +25,7 @@
 #include <linux/sysfs.h>
 #include <linux/acpi.h>
 #include <linux/mm.h>
+#include <linux/math64.h>
 #include <linux/i8042.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
@@ -1553,6 +1554,7 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 	struct kbd_state new_state;
 	struct kbd_state state;
 	bool convert;
+	u64 seconds;
 	int value;
 	int ret;
 	char ch;
@@ -1599,38 +1601,42 @@ static ssize_t kbd_led_timeout_store(struct device *dev,
 		convert = true;
 
 	if (convert) {
-		/* Convert value from current units to seconds */
+		/*
+		 * Convert value from current units to seconds. This is done in
+		 * 64 bits so that it cannot overflow: value <= INT_MAX.
+		 */
+		seconds = value;
 		switch (unit) {
 		case KBD_TIMEOUT_DAYS:
-			value *= 24;
+			seconds *= 24;
 			fallthrough;
 		case KBD_TIMEOUT_HOURS:
-			value *= 60;
+			seconds *= 60;
 			fallthrough;
 		case KBD_TIMEOUT_MINUTES:
-			value *= 60;
-			unit = KBD_TIMEOUT_SECONDS;
+			seconds *= 60;
 		}
 
 		if (quirks && quirks->needs_kbd_timeouts) {
 			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
-				if (value <= quirks->kbd_timeouts[i]) {
-					value = quirks->kbd_timeouts[i];
+				if (seconds <= quirks->kbd_timeouts[i]) {
+					seconds = quirks->kbd_timeouts[i];
 					break;
 				}
 			}
 		}
 
-		if (value <= kbd_info.seconds && kbd_info.seconds) {
+		if (seconds <= kbd_info.seconds && kbd_info.seconds) {
+			value = seconds;
 			unit = KBD_TIMEOUT_SECONDS;
-		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
-			value /= 60;
+		} else if (div_u64(seconds, 60) <= kbd_info.minutes && kbd_info.minutes) {
+			value = div_u64(seconds, 60);
 			unit = KBD_TIMEOUT_MINUTES;
-		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
-			value /= (60 * 60);
+		} else if (div_u64(seconds, 60 * 60) <= kbd_info.hours && kbd_info.hours) {
+			value = div_u64(seconds, 60 * 60);
 			unit = KBD_TIMEOUT_HOURS;
-		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
-			value /= (60 * 60 * 24);
+		} else if (div_u64(seconds, 60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
+			value = div_u64(seconds, 60 * 60 * 24);
 			unit = KBD_TIMEOUT_DAYS;
 		} else {
 			return -EINVAL;
Signature · 1 line
2.56.1
#2 lore raw

PATCH v4 2/3 platform/x86: dell-laptop: Don't use an unread keyboard state at init

In reply to Rosen Penev

kbd_init_info() ignores the return value of kbd_get_state(). If the call fails, the uninitialized state on the stack is used to set kbd_previous_level and kbd_previous_mode_bit, which are later sent to the BIOS when triggers are enabled or the backlight is turned back on.

kbd_get_info() has already succeeded at that point, so a failure here may well be transient and is not a reason to give up on the keyboard backlight. Start from level 0 and the off mode instead: the code below then picks level 1 and the first supported mode, as it already does when the backlight is off at boot.

Fixes 6cff8d60aa0a ("platform: x86: dell-laptop: Add support for keyboard backlight")

---
v4: Fixes: 6cff8d60aa0a instead of the reverted 02b2aaaa57ab
v3: unchanged
v2: fall back to level 0 / off mode instead of returning the error

 drivers/platform/x86/dell/dell-laptop.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +8 -3
index c1a51bf95c25..eca348497ac3 100644
@@ -1455,7 +1455,7 @@ static inline int kbd_init_info(void)
 	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
 		kbd_timeout_ac_supported = true;
 
-	kbd_get_state(&state);
+	ret = kbd_get_state(&state);
 
 	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
 	if (kbd_info.seconds > 63)
@@ -1472,8 +1472,13 @@ static inline int kbd_init_info(void)
 	 */
 	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
 
-	kbd_previous_level = kbd_get_level(&state);
-	kbd_previous_mode_bit = state.mode_bit;
+	if (ret == 0) {
+		kbd_previous_level = kbd_get_level(&state);
+		kbd_previous_mode_bit = state.mode_bit;
+	} else {
+		kbd_previous_level = 0;
+		kbd_previous_mode_bit = KBD_MODE_BIT_OFF;
+	}
 
 	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
 		kbd_previous_level = 1;
Signature · 1 line
2.56.1
#3 lore raw

PATCH v4 3/3 platform/x86: dell-laptop: Make the charge end threshold unsigned

In reply to Rosen Penev

charge_control_end_threshold_store() passes a pointer to an int to kstrtouint(), which only builds because the kernel disables -Wpointer-sign, and makes the following "end < 0" check dead code.

Declare end as unsigned int to match kstrtouint() and drop the dead check. kstrtouint() already rejects negative input, so the accepted range is unchanged.

---
v4: unchanged
v3: make end unsigned int and drop the dead check instead of switching

    to kstrtoint(); retitled (review feedback)

v2: unchanged

 drivers/platform/x86/dell/dell-laptop.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +3 -2
index eca348497ac3..1e2867f182c8 100644
@@ -2380,12 +2380,13 @@ static ssize_t charge_control_end_threshold_store(struct device *dev,
 		struct device_attribute *attr,
 		const char *buf, size_t size)
 {
-	int ret, end;
+	unsigned int end;
+	int ret;
 
 	ret = kstrtouint(buf, 10, &end);
 	if (ret)
 		return ret;
-	if (end < 0 || end > 100)
+	if (end > 100)
 		return -EINVAL;
 
 	ret = dell_battery_set_custom_charge_end(end);
Signature · 1 line
2.56.1