[PATCH] platform/loongarch: laptop: Fix backlight device lifetime on unload
From: Luo Qiu
Date: Thu Nov 19 2026 - 07:23:15 EST
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. Propagate registration failures to avoid silently
continuing without a backlight device.
Fixes: 6246ed09111f ("LoongArch: Add ACPI-based generic laptop driver")
Signed-off-by: Luo Qiu <luoqiu@xxxxxxxxxxxxxxx>
---
drivers/platform/loongarch/loongson-laptop.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/drivers/platform/loongarch/loongson-laptop.c b/drivers/platform/loongarch/loongson-laptop.c
index 4ec56621db24..ec0742e5ed46 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,9 @@ static void __exit generic_acpi_laptop_exit(void)
generic_subdriver_exit(&generic_sub_drivers[i]);
}
}
+
+ backlight_device_unregister(backlight_dev);
+ backlight_dev = NULL;
}
module_init(generic_acpi_laptop_init);
--
2.56.1