mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload
@ 2026-11-21 13:18 Luo Qiu
  2026-11-23 10:15 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Luo Qiu @ 2026-11-21 13:18 UTC (permalink / raw)
  To: Jianmin Lv, Huacai Chen, Yao Zi, Andy Shevchenko,
	Rafael J . Wysocki, Zixing Liu
  Cc: loongarch, linux-kernel

The driver registers a platform backlight device during module init but
discards the returned pointer. Consequently, the device is never
unregistered when the module is removed, leaving its ops callbacks
pointing into freed module text. After module removal, reading the
backlight's actual_brightness attribute can call the stale callback and
crash the kernel. Registration failures are also silently ignored.

Store the returned pointer, use it for resume, and unregister it on
module exit. Check and report backlight registration failures.

Fixes: 6246ed09111f ("LoongArch: Add ACPI-based generic laptop driver")
Signed-off-by: Luo Qiu <luoqiu@kylinsec.com.cn>
---
Changes in v2:
- Add a NULL check around backlight_device_unregister().
- Correct the changelog to state that registration failures are checked
  and reported, rather than propagated.
 drivers/platform/loongarch/loongson-laptop.c | 21 ++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/drivers/platform/loongarch/loongson-laptop.c b/drivers/platform/loongarch/loongson-laptop.c
index 4ec56621db24..65e92a250614 100644
--- a/drivers/platform/loongarch/loongson-laptop.c
+++ b/drivers/platform/loongarch/loongson-laptop.c
@@ -56,6 +56,7 @@ static acpi_handle hotkey_handle;
 static struct key_entry hotkey_keycode_map[GENERIC_HOTKEY_MAP_MAX];
 
 static bool bl_powered;
+static struct backlight_device *backlight_dev;
 static int loongson_laptop_backlight_update(struct backlight_device *bd);
 
 /* 2. ACPI Helpers and device model */
@@ -194,13 +195,12 @@ static int loongson_hotkey_resume(struct device *dev)
 {
 	int status = 0;
 	struct key_entry ke;
-	struct backlight_device *bd;
 
-	bd = backlight_device_get_by_type(BACKLIGHT_PLATFORM);
-	if (bd) {
-		loongson_laptop_backlight_update(bd) ?
+	if (backlight_dev) {
+		loongson_laptop_backlight_update(backlight_dev) ?
 		pr_warn("Loongson_backlight: resume brightness failed") :
-		pr_info("Loongson_backlight: resume brightness %d\n", bd->props.brightness);
+		pr_info("Loongson_backlight: resume brightness %d\n",
+				backlight_dev->props.brightness);
 	}
 
 	/*
@@ -409,6 +409,7 @@ static int laptop_backlight_register(void)
 {
 	int status = 0, ret;
 	struct backlight_properties props;
+	struct backlight_device *bd;
 
 	memset(&props, 0, sizeof(props));
 
@@ -426,9 +427,12 @@ static int laptop_backlight_register(void)
 	props.power = BACKLIGHT_POWER_ON;
 	props.type = BACKLIGHT_PLATFORM;
 
-	backlight_device_register("loongson_laptop",
+	bd = backlight_device_register("loongson_laptop",
 				NULL, NULL, &backlight_laptop_ops, &props);
+	if (IS_ERR(bd))
+		return PTR_ERR(bd);
 
+	backlight_dev = bd;
 
 	return 0;
 }
@@ -622,6 +626,11 @@ static void __exit generic_acpi_laptop_exit(void)
 				generic_subdriver_exit(&generic_sub_drivers[i]);
 		}
 	}
+
+	if (backlight_dev) {
+		backlight_device_unregister(backlight_dev);
+		backlight_dev = NULL;
+	}
 }
 
 module_init(generic_acpi_laptop_init);
-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload
  2026-11-21 13:18 [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload Luo Qiu
@ 2026-11-23 10:15 ` Andy Shevchenko
  2026-11-23 13:29   ` Luo Qiu
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-11-23 10:15 UTC (permalink / raw)
  To: Luo Qiu
  Cc: Jianmin Lv, Huacai Chen, Yao Zi, Rafael J . Wysocki, Zixing Liu,
	loongarch, linux-kernel

On Sat, Nov 21, 2026 at 09:18:20PM +0800, Luo Qiu wrote:
> The driver registers a platform backlight device during module init but
> discards the returned pointer. Consequently, the device is never
> unregistered when the module is removed, leaving its ops callbacks
> pointing into freed module text. After module removal, reading the
> backlight's actual_brightness attribute can call the stale callback and
> crash the kernel. Registration failures are also silently ignored.
> 
> Store the returned pointer, use it for resume, and unregister it on
> module exit. Check and report backlight registration failures.

...

> static int loongson_hotkey_resume(struct device *dev)
>  {
>  	int status = 0;
>  	struct key_entry ke;
> -	struct backlight_device *bd;
>  
> -	bd = backlight_device_get_by_type(BACKLIGHT_PLATFORM);
> -	if (bd) {
> -		loongson_laptop_backlight_update(bd) ?
> +	if (backlight_dev) {
> +		loongson_laptop_backlight_update(backlight_dev) ?
>  		pr_warn("Loongson_backlight: resume brightness failed") :
> -		pr_info("Loongson_backlight: resume brightness %d\n", bd->props.brightness);

> +		pr_info("Loongson_backlight: resume brightness %d\n",
> +				backlight_dev->props.brightness);

Indentation is broken here.

>  	}

...

> static int laptop_backlight_register(void)
>  {
>  	int status = 0, ret;
>  	struct backlight_properties props;
> +	struct backlight_device *bd;
>  
>  	memset(&props, 0, sizeof(props));


>  	props.power = BACKLIGHT_POWER_ON;
>  	props.type = BACKLIGHT_PLATFORM;
>  
> -	backlight_device_register("loongson_laptop",
> +	bd = backlight_device_register("loongson_laptop",
>  				NULL, NULL, &backlight_laptop_ops, &props);

...and here.

> +	if (IS_ERR(bd))
> +		return PTR_ERR(bd);
>  
> +	backlight_dev = bd;
>  
>  	return 0;
>  }

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload
  2026-11-23 10:15 ` Andy Shevchenko
@ 2026-11-23 13:29   ` Luo Qiu
  0 siblings, 0 replies; 3+ messages in thread
From: Luo Qiu @ 2026-11-23 13:29 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jianmin Lv, Huacai Chen, Yao Zi, Rafael J . Wysocki, Zixing Liu,
	loongarch, linux-kernel


On 11/23/26 6:15 PM, Andy Shevchenko wrote:
> On Sat, Nov 21, 2026 at 09:18:20PM +0800, Luo Qiu wrote:
>> The driver registers a platform backlight device during module init but
>> discards the returned pointer. Consequently, the device is never
>> unregistered when the module is removed, leaving its ops callbacks
>> pointing into freed module text. After module removal, reading the
>> backlight's actual_brightness attribute can call the stale callback and
>> crash the kernel. Registration failures are also silently ignored.
>>
>> Store the returned pointer, use it for resume, and unregister it on
>> module exit. Check and report backlight registration failures.
> ...
>
>> static int loongson_hotkey_resume(struct device *dev)
>>   {
>>   	int status = 0;
>>   	struct key_entry ke;
>> -	struct backlight_device *bd;
>>   
>> -	bd = backlight_device_get_by_type(BACKLIGHT_PLATFORM);
>> -	if (bd) {
>> -		loongson_laptop_backlight_update(bd) ?
>> +	if (backlight_dev) {
>> +		loongson_laptop_backlight_update(backlight_dev) ?
>>   		pr_warn("Loongson_backlight: resume brightness failed") :
>> -		pr_info("Loongson_backlight: resume brightness %d\n", bd->props.brightness);
>> +		pr_info("Loongson_backlight: resume brightness %d\n",
>> +				backlight_dev->props.brightness);
> Indentation is broken here.
Hi Andy,

Thanks for the review. I have fixed the formatting issues in v3.

https://lore.kernel.org/all/C95EAA0918240E5D+20261123131944.77149-1-luoqiu@kylinsec.com.cn/


Qiu Luo
>
>>   	}
> ...
>
>> static int laptop_backlight_register(void)
>>   {
>>   	int status = 0, ret;
>>   	struct backlight_properties props;
>> +	struct backlight_device *bd;
>>   
>>   	memset(&props, 0, sizeof(props));
>
>>   	props.power = BACKLIGHT_POWER_ON;
>>   	props.type = BACKLIGHT_PLATFORM;
>>   
>> -	backlight_device_register("loongson_laptop",
>> +	bd = backlight_device_register("loongson_laptop",
>>   				NULL, NULL, &backlight_laptop_ops, &props);
> ...and here.
>
>> +	if (IS_ERR(bd))
>> +		return PTR_ERR(bd);
>>   
>> +	backlight_dev = bd;
>>   
>>   	return 0;
>>   }


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-23 13:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 13:18 [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload Luo Qiu
2026-11-23 10:15 ` Andy Shevchenko
2026-11-23 13:29   ` Luo Qiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®