Skip to content

Commit 76959af

Browse files
Wer-Wolfrafaeljw
authored andcommitted
ACPI: battery: Fix possible crash when unregistering a battery hook
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: fa93854 ("battery: Add the battery hooking API") Signed-off-by: Armin Wolf <[email protected]> Link: https://patch.msgid.link/[email protected] Cc: All applicable <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 86309cb commit 76959af

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

drivers/acpi/battery.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,15 +715,22 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
715715
if (!hook->remove_battery(battery->bat, hook))
716716
power_supply_changed(battery->bat);
717717
}
718-
list_del(&hook->list);
718+
list_del_init(&hook->list);
719719

720720
pr_info("extension unregistered: %s\n", hook->name);
721721
}
722722

723723
void battery_hook_unregister(struct acpi_battery_hook *hook)
724724
{
725725
mutex_lock(&hook_mutex);
726-
battery_hook_unregister_unlocked(hook);
726+
/*
727+
* Ignore already unregistered battery hooks. This might happen
728+
* if a battery hook was previously unloaded due to an error when
729+
* adding a new battery.
730+
*/
731+
if (!list_empty(&hook->list))
732+
battery_hook_unregister_unlocked(hook);
733+
727734
mutex_unlock(&hook_mutex);
728735
}
729736
EXPORT_SYMBOL_GPL(battery_hook_unregister);
@@ -733,7 +740,6 @@ void battery_hook_register(struct acpi_battery_hook *hook)
733740
struct acpi_battery *battery;
734741

735742
mutex_lock(&hook_mutex);
736-
INIT_LIST_HEAD(&hook->list);
737743
list_add(&hook->list, &battery_hook_list);
738744
/*
739745
* Now that the driver is registered, we need

0 commit comments

Comments
 (0)