Linux ACPI
 help / color / mirror / Atom feed
* [PATCH] platform/x86: dell-laptop: Fix crash when unregistering battery hook
@ 2024-09-19  6:33 Armin Wolf
  2024-09-19  7:13 ` Andres Salomon
  0 siblings, 1 reply; 3+ messages in thread
From: Armin Wolf @ 2024-09-19  6:33 UTC (permalink / raw)
  To: mjg59, pali, dilinger
  Cc: rafael, lenb, hdegoede, ilpo.jarvinen, platform-driver-x86,
	linux-acpi, linux-kernel

If the battery hook encounters a unsupported battery, it will
return an error. This in turn will cause the battery driver to
automatically unregister the battery hook.

However as soon as the driver itself attempts to unregister the
already unregistered battery hook, a crash occurs due to a
corrupted linked list.

Fix this by simply ignoring unsupported batteries.

Tested on a Dell Inspiron 3505.

Fixes: ab58016c68cc ("platform/x86:dell-laptop: Add knobs to change battery charge settings")
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
I CCed the maintainers of the ACPI battery driver since i believe
that this patch highlights a general issue inside the battery hook
mechanism.

This is because the same crash will be triggered should for example
device_add_groups() fail.

Any ideas on how to solve this problem?
---
 drivers/platform/x86/dell/dell-laptop.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/x86/dell/dell-laptop.c b/drivers/platform/x86/dell/dell-laptop.c
index a3cd0505f282..5671bd0deee7 100644
--- a/drivers/platform/x86/dell/dell-laptop.c
+++ b/drivers/platform/x86/dell/dell-laptop.c
@@ -2391,12 +2391,18 @@ static struct attribute *dell_battery_attrs[] = {
 };
 ATTRIBUTE_GROUPS(dell_battery);

+static bool dell_battery_supported(struct power_supply *battery)
+{
+	/* We currently only support the primary battery */
+	return strcmp(battery->desc->name, "BAT0") == 0;
+}
+
 static int dell_battery_add(struct power_supply *battery,
 		struct acpi_battery_hook *hook)
 {
-	/* this currently only supports the primary battery */
-	if (strcmp(battery->desc->name, "BAT0") != 0)
-		return -ENODEV;
+	/* Return 0 instead of an error to avoid being unloaded */
+	if (!dell_battery_supported(battery))
+		return 0;

 	return device_add_groups(&battery->dev, dell_battery_groups);
 }
@@ -2404,6 +2410,9 @@ static int dell_battery_add(struct power_supply *battery,
 static int dell_battery_remove(struct power_supply *battery,
 		struct acpi_battery_hook *hook)
 {
+	if (!dell_battery_supported(battery))
+		return 0;
+
 	device_remove_groups(&battery->dev, dell_battery_groups);
 	return 0;
 }
--
2.39.5


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

end of thread, other threads:[~2024-09-19  7:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-19  6:33 [PATCH] platform/x86: dell-laptop: Fix crash when unregistering battery hook Armin Wolf
2024-09-19  7:13 ` Andres Salomon
2024-09-19  7:48   ` Armin Wolf

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