Commit e4a44b89 authored by Armin Wolf's avatar Armin Wolf Committed by Qi Xi
Browse files

ACPI: battery: Simplify battery hook locking

stable inclusion
from stable-v4.19.323
commit 079b8c72b42747af8a7d49e19bbf91d2960ee792
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBBN6V

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=079b8c72b42747af8a7d49e19bbf91d2960ee792



--------------------------------

[ Upstream commit 86309cbed26139e1caae7629dcca1027d9a28e75 ]

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.

Reviewed-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Reviewed-by: default avatarPali Rohár <pali@kernel.org>
Signed-off-by: default avatarArmin Wolf <W_Armin@gmx.de>
Link: https://patch.msgid.link/20241001212835.341788-2-W_Armin@gmx.de


Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
Stable-dep-of: 76959aff14a0 ("ACPI: battery: Fix possible crash when unregistering a battery hook")
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarQi Xi <xiqi2@huawei.com>
parent 1dcf34fc
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -683,27 +683,27 @@ 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) {
		hook->remove_battery(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);

@@ -729,7 +729,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;
		}
	}
@@ -766,7 +766,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);
@@ -799,7 +799,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);
}