Re: [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload

From: Luo Qiu

Date: Mon Nov 23 2026 - 08:30:54 EST



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@xxxxxxxxxxxxxxx/


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;
}