From: Jaegyun Lee <dreams200260@gmail.com>
To: Sebastian Reichel <sre@kernel.org>
Cc: Jaegyun Lee <dreams200260@gmail.com>,
Krzysztof Kozlowski <krzk@kernel.org>,
linux-pm@vger.kernel.org, Jinmo Yang <jinmo44.yang@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] power: supply: core: do not queue changed_work once psy->removing is set
Date: Mon, 30 Nov 2026 01:55:23 +0000 [thread overview]
Message-ID: <20261130015529.2128266-1-dreams200260@gmail.com> (raw)
power_supply_unregister() sets psy->removing, drains ->changed_work and
->deferred_register_work, and then calls device_unregister(), which
frees the supply unless another reference is still held. Two paths can
queue changed_work again in between:
* power_supply_changed() queues it unconditionally, after dropping
changed_lock;
* power_supply_deferred_register_work() checks ->removing only when
device_trylock() on the parent fails. If its trylock succeeds after
cancel_work_sync() has drained changed_work, it calls
power_supply_changed() while unregister waits for it in
cancel_delayed_work_sync().
The supply is then freed with changed_work still queued, and the work
runs on freed memory.
Check ->removing and queue changed_work in the same changed_lock section
of power_supply_changed(), and set ->removing under changed_lock in
power_supply_unregister().
Fixes: 7f1a57fdd6cb ("power_supply: Fix possible NULL pointer dereference on early uevent")
Cc: stable@vger.kernel.org
Co-developed-by: Jinmo Yang <jinmo44.yang@gmail.com>
Signed-off-by: Jinmo Yang <jinmo44.yang@gmail.com>
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
drivers/power/supply/power_supply_core.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 1cc203f2780b..f8b21dcabb56 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -154,10 +154,12 @@ void power_supply_changed(struct power_supply *psy)
dev_dbg(&psy->dev, "%s\n", __func__);
spin_lock_irqsave(&psy->changed_lock, flags);
- psy->changed = true;
- pm_stay_awake(&psy->dev);
+ if (!psy->removing) {
+ psy->changed = true;
+ pm_stay_awake(&psy->dev);
+ schedule_work(&psy->changed_work);
+ }
spin_unlock_irqrestore(&psy->changed_lock, flags);
- schedule_work(&psy->changed_work);
}
EXPORT_SYMBOL_GPL(power_supply_changed);
@@ -1955,8 +1957,10 @@ void power_supply_unregister(struct power_supply *psy)
* cover, and it may still dereference driver data that the caller is
* about to free.
*/
- scoped_guard(rwsem_write, &psy->epc_sem)
+ scoped_guard(rwsem_write, &psy->epc_sem) {
+ guard(spinlock_irqsave)(&psy->changed_lock);
psy->removing = true;
+ }
cancel_work_sync(&psy->changed_work);
cancel_delayed_work_sync(&psy->deferred_register_work);
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.53.0
reply other threads:[~2026-11-30 1:56 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261130015529.2128266-1-dreams200260@gmail.com \
--to=dreams200260@gmail.com \
--cc=jinmo44.yang@gmail.com \
--cc=krzk@kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=sre@kernel.org \
--cc=stable@vger.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