PM: clk: Fix false-positive recursive PM clock mutex locking
Subsystems (4)
PATCH 1/1 PM: clk: Fix false-positive recursive PM clock mutex locking
pm_clk_suspend() and pm_clk_resume() hold psd->clock_mutex across clk_prepare() and clk_unprepare() for sleeping clocks, with every instance sharing one lockdep class. If a PM clock is provided by a device that also uses PM clocks, clk_prepare() resumes the provider, which takes its own clock mutex, triggering a lockdep report.
This shows up on the Qualcomm X1 with the LPASS WSA/RX/TX macros and the varargs macro providing the "fsgen" clock:
WARNING: possible recursive locking detected
...
pm_clk_resume
va_macro_runtime_resume [snd_soc_lpass_va_macro]
...
clk_prepare
pm_clk_resume
wsa_macro_runtime_resume [snd_soc_lpass_wsa_macro]
Lock ordering is always consumer -> provider, so this is false-positive, resulting in lockdep being turned off.
Fix this by giving each pm_clk_create()/devm_pm_clk_create() call site its own lock class.
Scenario analysis and patch development was assisted by Claude Opus 5.5 under my instruction; detection and validation was performed on my Lenovo Slim 7x with a X1E80100.
--- drivers/base/power/clock_ops.c | 23 ++++++++++++++++------- include/linux/pm_clock.h | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-)
drivers/base/power/clock_ops.c +16 -7
@@ -398,17 +398,26 @@ void pm_clk_init(struct device *dev)EXPORT_SYMBOL_GPL (pm_clk_init);/** -* pm_clk_create - Create and initialize a device's list of PM clocks. +* __pm_clk_create - Create and initialize a device's list of PM clocks. * @dev: Device to create and initialize the list of PM clocks for. +* @key: Lockdep class key for the clock mutex. * * Allocate a struct pm_subsys_data object, initialize its lock and clock_list * members and make the @dev's power.subsys_data field point to it. */ -int pm_clk_create (struct device *dev) +int __pm_clk_create (struct device *dev,struct lock_class_key *key) { -return dev_pm_get_subsys_data (dev); +int ret; + + ret =dev_pm_get_subsys_data (dev); +if (ret) +return ret; + +lockdep_set_class_and_name (&dev_to_psd (dev)->clock_mutex, key, +"&psd->clock_mutex" ); +return 0 ; } -EXPORT_SYMBOL_GPL (pm_clk_create); +EXPORT_SYMBOL_GPL (__pm_clk_create);/** * pm_clk_destroy - Destroy a device's list of power management clocks.
@@ -451,17 +460,17 @@ static void pm_clk_destroy_action(void *data)pm_clk_destroy (data); } -int devm_pm_clk_create (struct device *dev) +int __devm_pm_clk_create (struct device *dev,struct lock_class_key *key) {int ret; - ret =pm_clk_create (dev); + ret =__pm_clk_create (dev, key);if (ret)return ret;return devm_add_action_or_reset (dev, pm_clk_destroy_action, dev); } -EXPORT_SYMBOL_GPL (devm_pm_clk_create); +EXPORT_SYMBOL_GPL (__devm_pm_clk_create);/** * pm_clk_suspend - Disable clocks in a device's PM clock list.
include/linux/pm_clock.h +17 -2
@@ -37,7 +37,7 @@ static inline bool pm_clk_no_clocks(struct device *dev) }extern void pm_clk_init (struct device *dev); -extern int pm_clk_create (struct device *dev); +extern int __pm_clk_create (struct device *dev,struct lock_class_key *key);extern void pm_clk_destroy (struct device *dev);extern int pm_clk_add (struct device *dev,const char *con_id);extern int pm_clk_add_clk (struct device *dev,struct clk *clk);
@@ -45,7 +45,22 @@ extern int of_pm_clk_add_clks(struct device *dev);extern void pm_clk_remove_clk (struct device *dev,struct clk *clk);extern int pm_clk_suspend (struct device *dev);extern int pm_clk_resume (struct device *dev); -extern int devm_pm_clk_create (struct device *dev); +extern int __devm_pm_clk_create (struct device *dev,struct lock_class_key *key); + +/* Per-call-site lockdep class, as clock mutexes of different devices nest */ +#define pm_clk_create (dev) \ +({ \ + static struct lock_class_key __key; \ + \ + __pm_clk_create(dev, &__key); \ +}) + +#define devm_pm_clk_create (dev) \ +({ \ + static struct lock_class_key __key; \ + \ + __devm_pm_clk_create(dev, &__key); \ +})#else static inline bool pm_clk_no_clocks (struct device *dev) {
Signature · 1 line
2.55.0
Quoted 25 lines · Daniel J Blueman
pm_clk_suspend()andpm_clk_resume()holdpsd->clock_mutexacrossclk_prepare()andclk_unprepare()for sleeping clocks, with every instance sharing one lockdep class. If a PM clock is provided by a device that also uses PM clocks,clk_prepare()resumes the provider, which takes its own clock mutex, triggering a lockdep report.This shows up on the Qualcomm X1 with the LPASS WSA/RX/TX macros and the varargs macro providing the "fsgen" clock:
WARNING: possible recursive locking detected
...
pm_clk_resume
va_macro_runtime_resume[snd_soc_lpass_va_macro]
...
clk_prepare
pm_clk_resume
wsa_macro_runtime_resume[snd_soc_lpass_wsa_macro]Lock ordering is always consumer -> provider, so this is false-positive, resulting in lockdep being turned off.
Fix this by giving each pm_clk_create()/devm_pm_clk_create() call site its own lock class.
Quoted 6 lines
Scenario analysis and patch development was assisted by Claude Opus 5.5 under my instruction; detection and validation was performed on my Lenovo Slim 7x with a X1E80100.
Fixes eb667d0fbdd3 ("ASoC: codecs: lpass-va-macro: Switch to PM clock framework for runtime PM")Signed-off-by Daniel J Blueman <[email protected]>
Assuming we agree that using separate lock classes is the right approach here, and that using nested locking is not practical in this case, then the overall solution looks reasonable to me. I only have a few suggestions below:
Quoted 23 lines
--- drivers/base/power/clock_ops.c | 23 ++++++++++++++++------- include/linux/pm_clock.h | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-)drivers/base/power/clock_ops.c +3
-2@@ -398,17 +398,26 @@ void pm_clk_init(struct device *dev)EXPORT_SYMBOL_GPL (pm_clk_init);/** -* pm_clk_create - Create and initialize a device's list of PM clocks. +* __pm_clk_create - Create and initialize a device's list of PM clocks. * @dev: Device to create and initialize the list of PM clocks for. +* @key: Lockdep class key for the clock mutex. * * Allocate a struct pm_subsys_data object, initialize its lock and clock_list * members and make the @dev's power.subsys_data field point to it. */ -int pm_clk_create (struct device *dev) +int __pm_clk_create (struct device *dev,struct lock_class_key *key)
drivers/base/power/clock_ops.c void pm_clk_init(struct device *dev) · line 417
Quoted 10 lines
drivers/base/power/clock_ops.c +8
-1{ -return dev_pm_get_subsys_data (dev); +int ret; + + ret =dev_pm_get_subsys_data (dev); +if (ret) +return ret; + +lockdep_set_class_and_name (&dev_to_psd (dev)->clock_mutex, key, +"&psd->clock_mutex" );
You could simply use lockdep_set_class() and avoid introducing the somewhat unusual &psd->clock_mutex name. If a custom name is desired, something like "pm_clock_mutex" would be more descriptive.
It would also be helpful to add an inline comment explaining why a per-call-site lock class is required here, and why the consumer->provider locking order is guaranteed, since the whole approach relies on that assumption to justify splitting the lock classes.
Quoted 69 lines
drivers/base/power/clock_ops.c +5
-4+return 0 ; } -EXPORT_SYMBOL_GPL (pm_clk_create); +EXPORT_SYMBOL_GPL (__pm_clk_create);/** * pm_clk_destroy - Destroy a device's list of power management clocks. @@ -451,17 +460,17 @@ static void pm_clk_destroy_action(void *data)pm_clk_destroy (data); } -int devm_pm_clk_create (struct device *dev) +int __devm_pm_clk_create (struct device *dev,struct lock_class_key *key) {int ret; - ret =pm_clk_create (dev); + ret =__pm_clk_create (dev, key);if (ret)return ret;return devm_add_action_or_reset (dev, pm_clk_destroy_action, dev); } -EXPORT_SYMBOL_GPL (devm_pm_clk_create); +EXPORT_SYMBOL_GPL (__devm_pm_clk_create);/** * pm_clk_suspend - Disable clocks in a device's PM clock list. include/linux/pm_clock.h +17
-3@@ -37,7 +37,7 @@ static inline bool pm_clk_no_clocks(struct device *dev) }extern void pm_clk_init (struct device *dev); -extern int pm_clk_create (struct device *dev); +extern int __pm_clk_create (struct device *dev,struct lock_class_key *key);extern void pm_clk_destroy (struct device *dev);extern int pm_clk_add (struct device *dev,const char *con_id);extern int pm_clk_add_clk (struct device *dev,struct clk *clk);@@ -45,7 +45,22 @@ extern int of_pm_clk_add_clks(struct device *dev);extern void pm_clk_remove_clk (struct device *dev,struct clk *clk);extern int pm_clk_suspend (struct device *dev);extern int pm_clk_resume (struct device *dev); -extern int devm_pm_clk_create (struct device *dev); +extern int __devm_pm_clk_create (struct device *dev,struct lock_class_key *key); + +/* Per-call-site lockdep class, as clock mutexes of different devices nest */ +#define pm_clk_create (dev) \ +({ \ + static struct lock_class_key __key; \ + \ + __pm_clk_create(dev, &__key); \ +}) + +#define devm_pm_clk_create (dev) \ +({ \ + static struct lock_class_key __key; \ + \ + __devm_pm_clk_create(dev, &__key); \ +})#else static inline bool pm_clk_no_clocks (struct device *dev) { --2.55.0