* [PATCH RESEND] power: supply: core: give each supply's epc_sem its own lockdep class
@ 2026-11-25 23:23 Ryan Brue
2026-11-26 10:27 ` André Draszik
0 siblings, 1 reply; 2+ messages in thread
From: Ryan Brue @ 2026-11-25 23:23 UTC (permalink / raw)
To: Sebastian Reichel, Sebastian Reichel, Hang Nan
Cc: linux-pm, linux-kernel, Ryan Brue
Since commit 9f7f7a859451 ("power_supply: serialize property access with
unregister"), power_supply_get_property() takes the target supply's
epc_sem for read. Every supply's epc_sem is initialised at the same site,
so they all share one lockdep class.
A driver that reads another supply while holding a lock that its
get_property() callback also takes is then reported as a circular
dependency, even though the two semaphores belong to different supplies.
sc27xx_fgu_get_property() does this with data->lock and the charger, and
a fuel gauge driver in preparation did the same with its charger:
WARNING: possible circular locking dependency detected
kworker/2:1/60 is trying to acquire lock:
(mutex_#2){+.+.}-{4:4}, at: mt6397_battery_get_property+0x50/0x23c
but task is already holding lock:
(&psy->epc_sem){.+.+}-{4:4}, at: power_supply_get_property+0x34/0x80
Lockdep then turns itself off for the rest of the boot.
Register a lockdep key for each supply, so that only a supply's own epc_sem
is ordered against its own locks.
Fixes: 9f7f7a859451 ("power_supply: serialize property access with unregister")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
Since v7.4-rc1 power_supply_get_property() takes the target supply's
epc_sem, and every supply's epc_sem shares one lockdep class. A driver
that reads another supply under a lock its own get_property() takes is
then reported as a circular dependency, and lockdep switches itself off.
Tested on an MT8173 tablet (Amazon Fire HD 10, 2017) with PROVE_LOCKING:
the report came at boot with a fuel gauge that reads its charger under
its own mutex, and doesn't come with this patch (debug_locks stays 1).
This is a resend of the 11-10 patch, now also to Sebastian's Collabora
address, which I left off the first time. The code is unchanged, and it
still applies to v7.4-rc4 and to the power-supply for-next branch.
This patch was developed with the help of an LLM coding assistant.
---
Changes in RESEND:
- Added Sebastian's Collabora address; no code change.
- Link to v1: https://lore.kernel.org/r/20261110-power-supply-epc-lockdep-key-v1-1-39bd0a237a6c@gmail.com
---
drivers/power/supply/power_supply_core.c | 3 +++
include/linux/power_supply.h | 3 +++
2 files changed, 6 insertions(+)
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index de15314ef954..76bbe8772143 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -1662,6 +1662,7 @@ static void power_supply_dev_release(struct device *dev)
struct power_supply *psy = to_power_supply(dev);
dev_dbg(dev, "%s\n", __func__);
+ lockdep_unregister_key(&psy->epc_key);
kfree(psy);
}
@@ -1765,6 +1766,7 @@ __power_supply_register(struct device *parent,
if (!psy)
return ERR_PTR(-ENOMEM);
+ lockdep_register_key(&psy->epc_key);
dev = &psy->dev;
device_initialize(dev);
@@ -1810,6 +1812,7 @@ __power_supply_register(struct device *parent,
spin_lock_init(&psy->changed_lock);
init_rwsem(&psy->epc_sem);
+ lockdep_set_class(&psy->epc_sem, &psy->epc_key);
init_rwsem(&psy->extensions_sem);
INIT_LIST_HEAD(&psy->extensions);
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index cce61137aef4..3cfe2520862e 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -371,8 +371,11 @@ struct power_supply {
* the callback is still using it.
* Must not be shared with extensions_sem: callbacks may read their own
* properties, which takes that one for read.
+ * Each supply has its own lockdep class: a driver may read another
+ * supply's properties while holding a lock its own callbacks take.
*/
struct rw_semaphore epc_sem;
+ struct lock_class_key epc_key;
struct rw_semaphore extensions_sem; /* protects "extensions" */
struct list_head extensions;
#ifdef CONFIG_THERMAL
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261103-power-supply-epc-lockdep-key-2af05a195ccc
Best regards,
--
Ryan Brue <ryanbrue.dev@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH RESEND] power: supply: core: give each supply's epc_sem its own lockdep class
2026-11-25 23:23 [PATCH RESEND] power: supply: core: give each supply's epc_sem its own lockdep class Ryan Brue
@ 2026-11-26 10:27 ` André Draszik
0 siblings, 0 replies; 2+ messages in thread
From: André Draszik @ 2026-11-26 10:27 UTC (permalink / raw)
To: Ryan Brue, Sebastian Reichel, Sebastian Reichel, Hang Nan
Cc: linux-pm, linux-kernel
Hi Ryan,
On Wed, 2026-11-25 at 17:23 -0600, Ryan Brue wrote:
> Since commit 9f7f7a859451 ("power_supply: serialize property access with
> unregister"), power_supply_get_property() takes the target supply's
> epc_sem for read. Every supply's epc_sem is initialised at the same site,
> so they all share one lockdep class.
>
> A driver that reads another supply while holding a lock that its
> get_property() callback also takes is then reported as a circular
> dependency, even though the two semaphores belong to different supplies.
> sc27xx_fgu_get_property() does this with data->lock and the charger, and
> a fuel gauge driver in preparation did the same with its charger:
>
> WARNING: possible circular locking dependency detected
> kworker/2:1/60 is trying to acquire lock:
> (mutex_#2){+.+.}-{4:4}, at: mt6397_battery_get_property+0x50/0x23c
> but task is already holding lock:
> (&psy->epc_sem){.+.+}-{4:4}, at: power_supply_get_property+0x34/0x80
>
> Lockdep then turns itself off for the rest of the boot.
>
> Register a lockdep key for each supply, so that only a supply's own epc_sem
> is ordered against its own locks.
>
> Fixes: 9f7f7a859451 ("power_supply: serialize property access with unregister")
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
Thanks for the patch — I hit the same shared-class issue on Pixel 6 (Oriole)
with max77759-charger reading properties from its TCPM supplier via
power_supply_get_property_from_supplier(), which triggers a "possible
recursive locking detected" splat.
Two minor suggestions:
1. It would be helpful to also mention the "possible recursive locking
detected" case in the commit message (e.g. consumer callbacks calling
power_supply_get_property_from_supplier() or power_supply_get_property()
on another supply), so people hitting that variant can easily find this
fix.
> [...]
> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
> index de15314ef954..76bbe8772143 100644
> --- a/drivers/power/supply/power_supply_core.c
> +++ b/drivers/power/supply/power_supply_core.c
> @@ -1662,6 +1662,7 @@ static void power_supply_dev_release(struct device *dev)
> struct power_supply *psy = to_power_supply(dev);
>
> dev_dbg(dev, "%s\n", __func__);
> + lockdep_unregister_key(&psy->epc_key);
> kfree(psy);
> }
>
> @@ -1765,6 +1766,7 @@ __power_supply_register(struct device *parent,
> if (!psy)
> return ERR_PTR(-ENOMEM);
>
> + lockdep_register_key(&psy->epc_key);
> dev = &psy->dev;
>
> device_initialize(dev);
> @@ -1810,6 +1812,7 @@ __power_supply_register(struct device *parent,
>
> spin_lock_init(&psy->changed_lock);
> init_rwsem(&psy->epc_sem);
> + lockdep_set_class(&psy->epc_sem, &psy->epc_key);
2. Because lockdep_set_class() stringifies the key argument, the lock shows
up in /proc/lockdep (and any future splats) as "&psy->epc_key" instead of
"&psy->epc_sem":
ffff0008068e26e0 FD: 167 BD: 13 .+.+: &psy->epc_key#2
-> [ffff000816c3eee0] &psy->epc_key
Replacing init_rwsem() + lockdep_set_class() with:
__init_rwsem(&psy->epc_sem, "&psy->epc_sem", &psy->epc_key);
like block/blk-crypto-profile.c does, preserves the lock name while
using the per-supply key.
Tested-by: André Draszik <andre.draszik@linaro.org>
Cheers,
Andre'
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 10:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 23:23 [PATCH RESEND] power: supply: core: give each supply's epc_sem its own lockdep class Ryan Brue
2026-11-26 10:27 ` André Draszik
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®