Re: [PATCH v2] platform/loongarch: laptop: Fix backlight device lifetime on unload
From: Andy Shevchenko
Date: Mon Nov 23 2026 - 05:16:04 EST
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