platform/x86: dell-laptop: Fix keyboard backlight and charge threshold handling
Subsystems (2)
v2 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 as unsigned 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
The kbd_get_state() failure path of patch 2 was not triggered.
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: Parse the charge end threshold as an int
drivers/platform/x86/dell/dell-laptop.c | 47 +++++++++++++++---------- 1 file changed, 29 insertions(+), 18 deletions(-)
Signature · 1 line
2.56.1
PATCH v2 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.
---
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
@@ -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
PATCH v2 2/3 platform/x86: dell-laptop: Don't use an unread keyboard state at init
In reply to Rosen Penevkbd_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.
---
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
@@ -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
PATCH v2 3/3 platform/x86: dell-laptop: Parse the charge end threshold as an int
In reply to Rosen Penevcharge_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. Use kstrtoint() like charge_control_start_threshold_store(). Values outside 0 to 100 are rejected either way.
--- drivers/platform/x86/dell/dell-laptop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/dell/dell-laptop.c +1 -1
@@ -2382,7 +2382,7 @@ static ssize_t charge_control_end_threshold_store(struct device *dev, {int ret, end; - ret =kstrtouint (buf,10 , &end); + ret =kstrtoint (buf,10 , &end);if (ret)return ret;if (end <0 || end >100 )
Signature · 1 line
2.56.1
Ilpo Järvinen
maintainerRe: [PATCH v2 3/3] platform/x86: dell-laptop: Parse the charge end threshold as an int
Quoted 5 lines · Rosen Penev
charge_control_end_threshold_store()passes a pointer to an int tokstrtouint(), which only builds because the kernel disables -Wpointer-sign, and makes the following "end < 0" check dead code. Usekstrtoint()likecharge_control_start_threshold_store(). Values outside 0 to 100 are rejected either way.
Why the <0 check has to be kept on the driver side?
Why not change the variable type instead and remove the then dead code check?
It's somewhat disappointing this even has to be pointed out by review.
Quoted 19 lines
Signed-off-by Rosen Penev <[email protected]>--- drivers/platform/x86/dell/dell-laptop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/platform/x86/dell/dell-laptop.c +1
-1@@ -2382,7 +2382,7 @@ static ssize_t charge_control_end_threshold_store(struct device *dev, {int ret, end; - ret =kstrtouint (buf,10 , &end); + ret =kstrtoint (buf,10 , &end);if (ret)return ret;if (end <0 || end >100 )
Signature · 1 line
i.
Re: [PATCH v2 3/3] platform/x86: dell-laptop: Parse the charge end threshold as an int
Quoted 10 lines · Ilpo Järvinen
Quoted 5 lines · Rosen Penev
charge_control_end_threshold_store()passes a pointer to an int tokstrtouint(), which only builds because the kernel disables -Wpointer-sign, and makes the following "end < 0" check dead code. Usekstrtoint()likecharge_control_start_threshold_store(). Values outside 0 to 100 are rejected either way.Why the <0 check has to be kept on the driver side?
It's currently a one line change instead of three.
Quoted 29 lines
Why not change the variable type instead and remove the then dead code check?
It's somewhat disappointing this even has to be pointed out by review.
Quoted 11 lines
Signed-off-by Rosen Penev <[email protected]>--- drivers/platform/x86/dell/dell-laptop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/platform/x86/dell/dell-laptop.c +0
-0drivers/platform/x86/dell/dell-laptop.c static ssize_t charge_control_end_threshold_store(struct device *dev, · line 2388
Quoted 8 lines
drivers/platform/x86/dell/dell-laptop.c +1
-1@@ -2382,7 +2382,7 @@ static ssize_t charge_control_end_threshold_store(struct device *dev, {int ret, end; - ret =kstrtouint (buf,10 , &end); + ret =kstrtoint (buf,10 , &end);if (ret)return ret;if (end <0 || end >100 )--
i.