Skip to contentSkip to conversation

platform/x86: dell-laptop: Battery hook fixes

PATCH RESEND In review Armin Wolf 3 participants 11 messages Oct 1, 2024 – Oct 6, 2024

Subsystems (3)
lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

v3 cover platform/x86: dell-laptop: Battery hook fixes

This patch series fixes some issues around the battery hook handling inside the ACPI battery driver and the dell-laptop driver.

The first patch simplifies the locking during battery hook removal as a preparation for the second patch which fixes a possible crash when unregistering a battery hook.

The third patch allows the dell-laptop driver to handle systems with multiple batteries.

All patches where tested on a Dell Inspiron 3505 and appear to work.

Chnages since v2:
- drop boolean flag and use list head instead in patch 2
- drop [email protected] from the CC list since the underlying adress seems to trigger random errors in proccessing (help?)

Changes since v1:
- fix the underlying issue inside the ACPI battery driver
- reword patch for dell-laptop

Armin Wolf (3):
ACPI: battery: Simplify battery hook locking
ACPI: battery: Fix possible crash when unregistering a battery hook platform/x86: dell-laptop: Do not fail when encountering unsupported
batteries

 drivers/acpi/battery.c                  | 28 +++++++++++++++----------
 drivers/platform/x86/dell/dell-laptop.c | 15 ++++++++++---
 2 files changed, 29 insertions(+), 14 deletions(-)

--
2.39.5

author
#1 lore raw

PATCH RESEND v3 1/3 ACPI: battery: Simplify battery hook locking

Move the conditional locking from __battery_hook_unregister() into battery_hook_unregister() and rename the low-level function to simplify the locking during battery hook removal.

---
 drivers/acpi/battery.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/battery.c +9 -9
index f4599261cfc3..dda59ee5a11e 100644
@@ -703,28 +703,28 @@ static LIST_HEAD(acpi_battery_list);
 static LIST_HEAD(battery_hook_list);
 static DEFINE_MUTEX(hook_mutex);
 
-static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
+static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 {
 	struct acpi_battery *battery;
+
 	/*
 	 * In order to remove a hook, we first need to
 	 * de-register all the batteries that are registered.
 	 */
-	if (lock)
-		mutex_lock(&hook_mutex);
 	list_for_each_entry(battery, &acpi_battery_list, list) {
 		if (!hook->remove_battery(battery->bat, hook))
 			power_supply_changed(battery->bat);
 	}
 	list_del(&hook->list);
-	if (lock)
-		mutex_unlock(&hook_mutex);
+
 	pr_info("extension unregistered: %s\n", hook->name);
 }
 
 void battery_hook_unregister(struct acpi_battery_hook *hook)
 {
-	__battery_hook_unregister(hook, 1);
+	mutex_lock(&hook_mutex);
+	battery_hook_unregister_unlocked(hook);
+	mutex_unlock(&hook_mutex);
 }
 EXPORT_SYMBOL_GPL(battery_hook_unregister);
 
@@ -750,7 +750,7 @@ void battery_hook_register(struct acpi_battery_hook *hook)
 			 * hooks.
 			 */
 			pr_err("extension failed to load: %s", hook->name);
-			__battery_hook_unregister(hook, 0);
+			battery_hook_unregister_unlocked(hook);
 			goto end;
 		}
 
@@ -804,7 +804,7 @@ static void battery_hook_add_battery(struct acpi_battery *battery)
 			 */
 			pr_err("error in extension, unloading: %s",
 					hook_node->name);
-			__battery_hook_unregister(hook_node, 0);
+			battery_hook_unregister_unlocked(hook_node);
 		}
 	}
 	mutex_unlock(&hook_mutex);
@@ -837,7 +837,7 @@ static void __exit battery_hook_exit(void)
 	 * need to remove the hooks.
 	 */
 	list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
-		__battery_hook_unregister(hook, 1);
+		battery_hook_unregister(hook);
 	}
 	mutex_destroy(&hook_mutex);
 }

--
2.39.5

#2 lore raw

Re: [PATCH RESEND v3 1/3] ACPI: battery: Simplify battery hook locking

Quoted 7 lines · Armin Wolf

Move the conditional locking from __battery_hook_unregister() into battery_hook_unregister() and rename the low-level function to simplify the locking during battery hook removal.

Well, "looks good to me" doesn't usually mean "Reviewed-by", but you can retain the tag in this particular case.

But in the future please don't add Reviewed-by or Acked-by tags from people to patches if you have not actually received those tags.

Quoted 75 lines
---
 drivers/acpi/battery.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/battery.c +9 -10
index f4599261cfc3..dda59ee5a11e 100644
@@ -703,28 +703,28 @@ static LIST_HEAD(acpi_battery_list);
 static LIST_HEAD(battery_hook_list);
 static DEFINE_MUTEX(hook_mutex);
 
-static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
+static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 {
        struct acpi_battery *battery;
+
        /*
         * In order to remove a hook, we first need to
         * de-register all the batteries that are registered.
         */
-       if (lock)
-               mutex_lock(&hook_mutex);
        list_for_each_entry(battery, &acpi_battery_list, list) {
                if (!hook->remove_battery(battery->bat, hook))
                        power_supply_changed(battery->bat);
        }
        list_del(&hook->list);
-       if (lock)
-               mutex_unlock(&hook_mutex);
+
        pr_info("extension unregistered: %s\n", hook->name);
 }
 
 void battery_hook_unregister(struct acpi_battery_hook *hook)
 {
-       __battery_hook_unregister(hook, 1);
+       mutex_lock(&hook_mutex);
+       battery_hook_unregister_unlocked(hook);
+       mutex_unlock(&hook_mutex);
 }
 EXPORT_SYMBOL_GPL(battery_hook_unregister);
 
@@ -750,7 +750,7 @@ void battery_hook_register(struct acpi_battery_hook *hook)
                         * hooks.
                         */
                        pr_err("extension failed to load: %s", hook->name);
-                       __battery_hook_unregister(hook, 0);
+                       battery_hook_unregister_unlocked(hook);
                        goto end;
                }
 
@@ -804,7 +804,7 @@ static void battery_hook_add_battery(struct acpi_battery *battery)
                         */
                        pr_err("error in extension, unloading: %s",
                                        hook_node->name);
-                       __battery_hook_unregister(hook_node, 0);
+                       battery_hook_unregister_unlocked(hook_node);
                }
        }
        mutex_unlock(&hook_mutex);
@@ -837,7 +837,7 @@ static void __exit battery_hook_exit(void)
         * need to remove the hooks.
         */
        list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
-               __battery_hook_unregister(hook, 1);
+               battery_hook_unregister(hook);
        }
        mutex_destroy(&hook_mutex);
 }
--

2.39.5

author
#3 lore raw

Re: [PATCH RESEND v3 1/3] ACPI: battery: Simplify battery hook locking

Am 02.10.24 um 14:06 schrieb Rafael J. Wysocki:

Quoted 5 lines · Armin Wolf
Quoted 4 lines

Move the conditional locking from __battery_hook_unregister() into battery_hook_unregister() and rename the low-level function to simplify the locking during battery hook removal.

Quoted 7 lines
Quoted 2 lines

Well, "looks good to me" doesn't usually mean "Reviewed-by", but you can retain the tag in this particular case.

But in the future please don't add Reviewed-by or Acked-by tags from people to patches if you have not actually received those tags.

Sorry, it seems that i mixed up the Reviewed-by tags. I will try to be a bit more careful next time.

Armin Wolf

Quoted 75 lines
Quoted 75 lines
---
  drivers/acpi/battery.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
drivers/acpi/battery.c +9 -10
index f4599261cfc3..dda59ee5a11e 100644
@@ -703,28 +703,28 @@ static LIST_HEAD(acpi_battery_list);
  static LIST_HEAD(battery_hook_list);
  static DEFINE_MUTEX(hook_mutex);
 
-static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
+static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
  {
         struct acpi_battery *battery;
+
         /*
          * In order to remove a hook, we first need to
          * de-register all the batteries that are registered.
          */
-       if (lock)
-               mutex_lock(&hook_mutex);
         list_for_each_entry(battery, &acpi_battery_list, list) {
                 if (!hook->remove_battery(battery->bat, hook))
                         power_supply_changed(battery->bat);
         }
         list_del(&hook->list);
-       if (lock)
-               mutex_unlock(&hook_mutex);
+
         pr_info("extension unregistered: %s\n", hook->name);
  }
 
  void battery_hook_unregister(struct acpi_battery_hook *hook)
  {
-       __battery_hook_unregister(hook, 1);
+       mutex_lock(&hook_mutex);
+       battery_hook_unregister_unlocked(hook);
+       mutex_unlock(&hook_mutex);
  }
  EXPORT_SYMBOL_GPL(battery_hook_unregister);
 
@@ -750,7 +750,7 @@ void battery_hook_register(struct acpi_battery_hook *hook)
                          * hooks.
                          */
                         pr_err("extension failed to load: %s", hook->name);
-                       __battery_hook_unregister(hook, 0);
+                       battery_hook_unregister_unlocked(hook);
                         goto end;
                 }
 
@@ -804,7 +804,7 @@ static void battery_hook_add_battery(struct acpi_battery *battery)
                          */
                         pr_err("error in extension, unloading: %s",
                                         hook_node->name);
-                       __battery_hook_unregister(hook_node, 0);
+                       battery_hook_unregister_unlocked(hook_node);
                 }
         }
         mutex_unlock(&hook_mutex);
@@ -837,7 +837,7 @@ static void __exit battery_hook_exit(void)
          * need to remove the hooks.
          */
         list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
-               __battery_hook_unregister(hook, 1);
+               battery_hook_unregister(hook);
         }
         mutex_destroy(&hook_mutex);
  }
--

2.39.5

author
#4 lore raw

PATCH RESEND v3 2/3 ACPI: battery: Fix possible crash when unregistering a battery hook

In reply to Armin Wolf

When a battery hook returns an error when adding a new battery, then the battery hook is automatically unregistered.
However the battery hook provider cannot know that, so it will later call battery_hook_unregister() on the already unregistered battery hook, resulting in a crash.

Fix this by using the list head to mark already unregistered battery hooks as already being unregistered so that they can be ignored by battery_hook_unregister().

Fixes fa93854f7a7e ("battery: Add the battery hooking API")
---
 drivers/acpi/battery.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/acpi/battery.c +9 -3
index dda59ee5a11e..1c45ff6dbb83 100644
@@ -715,7 +715,7 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 		if (!hook->remove_battery(battery->bat, hook))
 			power_supply_changed(battery->bat);
 	}
-	list_del(&hook->list);
+	list_del_init(&hook->list);
 
 	pr_info("extension unregistered: %s\n", hook->name);
 }
@@ -723,7 +723,14 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 void battery_hook_unregister(struct acpi_battery_hook *hook)
 {
 	mutex_lock(&hook_mutex);
-	battery_hook_unregister_unlocked(hook);
+	/*
+	 * Ignore already unregistered battery hooks. This might happen
+	 * if a battery hook was previously unloaded due to an error when
+	 * adding a new battery.
+	 */
+	if (!list_empty(&hook->list))
+		battery_hook_unregister_unlocked(hook);
+
 	mutex_unlock(&hook_mutex);
 }
 EXPORT_SYMBOL_GPL(battery_hook_unregister);
@@ -733,7 +740,6 @@ void battery_hook_register(struct acpi_battery_hook *hook)
 	struct acpi_battery *battery;
 
 	mutex_lock(&hook_mutex);
-	INIT_LIST_HEAD(&hook->list);
 	list_add(&hook->list, &battery_hook_list);
 	/*
 	 * Now that the driver is registered, we need

--
2.39.5

maintainer Acked-by on v3 2/3
#5 lore raw

Re: [PATCH RESEND v3 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook

Quoted 7 lines · Armin Wolf

When a battery hook returns an error when adding a new battery, then the battery hook is automatically unregistered.
However the battery hook provider cannot know that, so it will later call battery_hook_unregister() on the already unregistered battery hook, resulting in a crash.

Quoted 6 lines

Fix this by using the list head to mark already unregistered battery hooks as already being unregistered so that they can be ignored by battery_hook_unregister().

Fixes fa93854f7a7e ("battery: Add the battery hooking API")

Hans, are you going to take this series or should I apply it?

Quoted 44 lines
---
 drivers/acpi/battery.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/acpi/battery.c +9 -4
index dda59ee5a11e..1c45ff6dbb83 100644
@@ -715,7 +715,7 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
                if (!hook->remove_battery(battery->bat, hook))
                        power_supply_changed(battery->bat);
        }
-       list_del(&hook->list);
+       list_del_init(&hook->list);
 
        pr_info("extension unregistered: %s\n", hook->name);
 }
@@ -723,7 +723,14 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 void battery_hook_unregister(struct acpi_battery_hook *hook)
 {
        mutex_lock(&hook_mutex);
-       battery_hook_unregister_unlocked(hook);
+       /*
+        * Ignore already unregistered battery hooks. This might happen
+        * if a battery hook was previously unloaded due to an error when
+        * adding a new battery.
+        */
+       if (!list_empty(&hook->list))
+               battery_hook_unregister_unlocked(hook);
+
        mutex_unlock(&hook_mutex);
 }
 EXPORT_SYMBOL_GPL(battery_hook_unregister);
@@ -733,7 +740,6 @@ void battery_hook_register(struct acpi_battery_hook *hook)
        struct acpi_battery *battery;
 
        mutex_lock(&hook_mutex);
-       INIT_LIST_HEAD(&hook->list);
        list_add(&hook->list, &battery_hook_list);
        /*
         * Now that the driver is registered, we need
--

2.39.5

#6 lore raw

Re: [PATCH RESEND v3 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook

Hi,

Quoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Armin Wolf

When a battery hook returns an error when adding a new battery, then the battery hook is automatically unregistered.
However the battery hook provider cannot know that, so it will later call battery_hook_unregister() on the already unregistered battery hook, resulting in a crash.

Fix this by using the list head to mark already unregistered battery hooks as already being unregistered so that they can be ignored by battery_hook_unregister().

Quoted 6 lines
Quoted 2 lines
Fixes fa93854f7a7e ("battery: Add the battery hooking API")

Hans, are you going to take this series or should I apply it?

AFAICT the patches don't really depend on each other, so my plan was that you take patches 1-2 and I take patch 3 as a fix for 6.12-rc# .

Does that work for you ?

Regards,

Hans

Quoted 46 lines
Quoted 45 lines
---
 drivers/acpi/battery.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/acpi/battery.c +9 -4
index dda59ee5a11e..1c45ff6dbb83 100644
@@ -715,7 +715,7 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
                if (!hook->remove_battery(battery->bat, hook))
                        power_supply_changed(battery->bat);
        }
-       list_del(&hook->list);
+       list_del_init(&hook->list);
 
        pr_info("extension unregistered: %s\n", hook->name);
 }
@@ -723,7 +723,14 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
 void battery_hook_unregister(struct acpi_battery_hook *hook)
 {
        mutex_lock(&hook_mutex);
-       battery_hook_unregister_unlocked(hook);
+       /*
+        * Ignore already unregistered battery hooks. This might happen
+        * if a battery hook was previously unloaded due to an error when
+        * adding a new battery.
+        */
+       if (!list_empty(&hook->list))
+               battery_hook_unregister_unlocked(hook);
+
        mutex_unlock(&hook_mutex);
 }
 EXPORT_SYMBOL_GPL(battery_hook_unregister);
@@ -733,7 +740,6 @@ void battery_hook_register(struct acpi_battery_hook *hook)
        struct acpi_battery *battery;
 
        mutex_lock(&hook_mutex);
-       INIT_LIST_HEAD(&hook->list);
        list_add(&hook->list, &battery_hook_list);
        /*
         * Now that the driver is registered, we need
--

2.39.5

#7 lore raw

Re: [PATCH RESEND v3 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook

Quoted 16 lines · Hans de Goede

Hi,

Quoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Armin Wolf

When a battery hook returns an error when adding a new battery, then the battery hook is automatically unregistered.
However the battery hook provider cannot know that, so it will later call battery_hook_unregister() on the already unregistered battery hook, resulting in a crash.

Fix this by using the list head to mark already unregistered battery hooks as already being unregistered so that they can be ignored by battery_hook_unregister().

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Fixes fa93854f7a7e ("battery: Add the battery hooking API")

Hans, are you going to take this series or should I apply it?

AFAICT the patches don't really depend on each other,

OK

Quoted 4 lines

so my plan was that you take patches 1-2 and I take patch 3 as a fix for 6.12-rc# .

Does that work for you ?

Yes, it does, thanks!

#8 lore raw

Re: [PATCH RESEND v3 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook

Quoted 27 lines · Rafael J. Wysocki
Quoted 16 lines · Hans de Goede

Hi,

Quoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Armin Wolf

When a battery hook returns an error when adding a new battery, then the battery hook is automatically unregistered.
However the battery hook provider cannot know that, so it will later call battery_hook_unregister() on the already unregistered battery hook, resulting in a crash.

Fix this by using the list head to mark already unregistered battery hooks as already being unregistered so that they can be ignored by battery_hook_unregister().

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Fixes fa93854f7a7e ("battery: Add the battery hooking API")

Hans, are you going to take this series or should I apply it?

AFAICT the patches don't really depend on each other,

Quoted 8 lines

OK

Quoted 4 lines

so my plan was that you take patches 1-2 and I take patch 3 as a fix for 6.12-rc# .

Does that work for you ?

Yes, it does, thanks!

Now queued up for 6.12-rc along with the [1/3], thanks!

author
#9 lore raw

PATCH RESEND v3 3/3 platform/x86: dell-laptop: Do not fail when encountering unsupported batteries

In reply to Armin Wolf

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.

On machines with multiple batteries however, this will prevent the battery hook from handling the primary battery, since it will always get unregistered upon encountering one of the unsupported batteries.

Fix this by simply ignoring unsupported batteries.

Fixes ab58016c68cc ("platform/x86:dell-laptop: Add knobs to change battery charge settings")
---
 drivers/platform/x86/dell/dell-laptop.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +12 -3
index a3cd0505f282..5671bd0deee7 100644
@@ -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

#10 lore raw

Re: [PATCH RESEND v3 3/3] platform/x86: dell-laptop: Do not fail when encountering unsupported batteries

Hi,

Quoted 9 lines · Armin Wolf

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.

On machines with multiple batteries however, this will prevent the battery hook from handling the primary battery, since it will always get unregistered upon encountering one of the unsupported batteries.

Quoted 5 lines

Fix this by simply ignoring unsupported batteries.

Fixes ab58016c68cc ("platform/x86:dell-laptop: Add knobs to change battery charge settings")

Thank you for your patch/series, I've applied this patch (series) to my review-hans branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in the pdx86 review-hans branch once I've pushed my local branch there, which might take a while.

I will include this patch in my next fixes pull-req to Linus for the current kernel development cycle.

Regards,

Hans

Quoted 44 lines
---
 drivers/platform/x86/dell/dell-laptop.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/platform/x86/dell/dell-laptop.c +12 -4
index a3cd0505f282..5671bd0deee7 100644
@@ -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