From: Armin Wolf <W_Armin@gmx.de>
To: mjg59@srcf.ucam.org, pali@kernel.org, dilinger@queued.net
Cc: rafael@kernel.org, lenb@kernel.org, hdegoede@redhat.com,
ilpo.jarvinen@linux.intel.com,
platform-driver-x86@vger.kernel.org, linux-acpi@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook
Date: Sun, 22 Sep 2024 08:40:25 +0200 [thread overview]
Message-ID: <20240922064026.496422-3-W_Armin@gmx.de> (raw)
In-Reply-To: <20240922064026.496422-1-W_Armin@gmx.de>
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 a boolean flag to mark already unregistered battery
hooks as "dead" so that they can be ignored by
battery_hook_unregister().
Fixes: fa93854f7a7e ("battery: Add the battery hooking API")
Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
drivers/acpi/battery.c | 11 ++++++++++-
include/acpi/battery.h | 1 +
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
index 10e9136897a7..b31a6183a082 100644
--- a/drivers/acpi/battery.c
+++ b/drivers/acpi/battery.c
@@ -719,6 +719,7 @@ static void battery_hook_unregister_unlocked(struct acpi_battery_hook *hook)
power_supply_changed(battery->bat);
}
list_del(&hook->list);
+ hook->dead = true;
pr_info("extension unregistered: %s\n", hook->name);
}
@@ -726,7 +727,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 (!hook->dead)
+ battery_hook_unregister_unlocked(hook);
+
mutex_unlock(&hook_mutex);
}
EXPORT_SYMBOL_GPL(battery_hook_unregister);
@@ -737,6 +745,7 @@ void battery_hook_register(struct acpi_battery_hook *hook)
mutex_lock(&hook_mutex);
INIT_LIST_HEAD(&hook->list);
+ hook->dead = false;
list_add(&hook->list, &battery_hook_list);
/*
* Now that the driver is registered, we need
diff --git a/include/acpi/battery.h b/include/acpi/battery.h
index c93f16dfb944..5cfe132bb7f5 100644
--- a/include/acpi/battery.h
+++ b/include/acpi/battery.h
@@ -16,6 +16,7 @@ struct acpi_battery_hook {
int (*add_battery)(struct power_supply *battery, struct acpi_battery_hook *hook);
int (*remove_battery)(struct power_supply *battery, struct acpi_battery_hook *hook);
struct list_head list;
+ bool dead;
};
void battery_hook_register(struct acpi_battery_hook *hook);
--
2.39.5
next prev parent reply other threads:[~2024-09-22 6:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-22 6:40 [PATCH v2 0/3] platform/x86: dell-laptop: Battery hook fixes Armin Wolf
2024-09-22 6:40 ` [PATCH v2 1/3] ACPI: battery: Simplify battery hook locking Armin Wolf
2024-09-23 13:35 ` Ilpo Järvinen
2024-09-22 6:40 ` Armin Wolf [this message]
2024-10-01 18:57 ` [PATCH v2 2/3] ACPI: battery: Fix possible crash when unregistering a battery hook Rafael J. Wysocki
2024-09-22 6:40 ` [PATCH v2 3/3] platform/x86: dell-laptop: Do not fail when encountering unsupported batteries Armin Wolf
2024-09-22 7:45 ` [PATCH v2 0/3] platform/x86: dell-laptop: Battery hook fixes Andres Salomon
2024-09-22 8:53 ` Pali Rohár
2024-09-23 20:14 ` Armin Wolf
2024-09-30 18:53 ` Armin Wolf
2024-09-30 19:03 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240922064026.496422-3-W_Armin@gmx.de \
--to=w_armin@gmx.de \
--cc=dilinger@queued.net \
--cc=hdegoede@redhat.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mjg59@srcf.ucam.org \
--cc=pali@kernel.org \
--cc=platform-driver-x86@vger.kernel.org \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®