PM: runtime: add might_sleep to PM runtime functions
Subsystems (4)
PATCH v2 1/1 PM: runtime: add might_sleep to PM runtime functions
Some of the entry points to pm runtime are not safe to call in atomic context unless pm_runtime_irq_safe() has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside a spin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.
If a driver incorrectly calls the pm_runtime apis, it can cause sleeping and irq processing when it expects to stay in atomic context.
Add might_sleep_if to all the __pm_runtime_* entry points to enforce correct usage.
Add pm_runtime_put_sync_autosuspend to the list of functions that can be called in atomic context.
--- Documentation/power/runtime_pm.txt | 1 + drivers/base/power/runtime.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
Documentation/power/runtime_pm.txt +1 -0
@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend() 5. Run-time PM Initialization, Device Probing and Removal
drivers/base/power/runtime.c +12 -3
@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle (struct device *dev,int rpmflags) {unsigned long flags;int retval; +might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +if (rpmflags &RPM_GET_PUT ) {if (!atomic_dec_and_test (&dev->power.usage_count))return 0 ;
@@ -760,13 +763,16 @@ EXPORT_SYMBOL_GPL(__pm_runtime_idle);* return immediately if it is larger than zero. Then carry out a suspend, * either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_suspend (struct device *dev,int rpmflags) {unsigned long flags;int retval; +might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +if (rpmflags &RPM_GET_PUT ) {if (!atomic_dec_and_test (&dev->power.usage_count))return 0 ;
@@ -788,13 +794,16 @@ EXPORT_SYMBOL_GPL(__pm_runtime_suspend);* If the RPM_GET_PUT flag is set, increment the device's usage count. Then * carry out a resume, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_resume (struct device *dev,int rpmflags) {unsigned long flags;int retval; +might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +if (rpmflags &RPM_GET_PUT )atomic_inc (&dev->power.usage_count);
Signature · 1 line
1.7.4.1
Quoted 44 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>--- Documentation/power/runtime_pm.txt | 1 + drivers/base/power/runtime.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend() 5. Run-time PM Initialization, Device Probing and Removaldrivers/base/power/runtime.c +0
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set.
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 10 lines
drivers/base/power/runtime.c +4
-0+* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle (struct device *dev,int rpmflags) {unsigned long flags;int retval; +might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +
Now that I think of it, perhaps it's better to put the might_sleep() annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.
Thanks,
Rafael
Quoted 44 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>--- Documentation/power/runtime_pm.txt | 1 + drivers/base/power/runtime.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend() 5. Run-time PM Initialization, Device Probing and Removaldrivers/base/power/runtime.c +0
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set.
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 10 lines
drivers/base/power/runtime.c +4
-0+* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle (struct device *dev,int rpmflags) {unsigned long flags;int retval; +might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +
Now that I think of it, perhaps it's better to put the might_sleep() annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.
Thanks,
Rafael
Quoted 53 lines · Rafael J. Wysocki
Quoted 52 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>---
Documentation/power/runtime_pm.txt | 1 +drivers/base/power/runtime.c| 15 ++++++++++++---2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend()5. Run-time PM Initialization, Device Probing and Removal
drivers/base/power/runtime.c +2
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle(struct device *dev, int rpmflags) { unsigned long flags; int retval;
Quoted 8 lines
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 2 lines
drivers/base/power/runtime.c +2
-0+ might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); +Now that I think of it, perhaps it's better to put the
might_sleep()annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.
You can't put the might_sleep after the spin_lock_irqsave(), because you are always in atomic context, and you can't put it after the spin_unlock_irq() that triggers the problem because you have already unconditionally left atomic context.
Anyways, the sleeps happen in a farily rare case, so putting the might_sleep in a more specific location will hide the errors when developers perform simple tests. For example, every kmalloc ends up calling might_sleep_if(flags & __GFP_WAIT), so that putting kmalloc(..., GFP_KERNEL) will print a stack trace every time, instead of only the very rare case when kmalloc has to block in a low memory condition.
The calls are very low overhead - the condition in the might_sleep_if(), and then in the common case:
if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || ...) return;
Quoted 53 lines · Rafael J. Wysocki
Quoted 52 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>---
Documentation/power/runtime_pm.txt | 1 +drivers/base/power/runtime.c| 15 ++++++++++++---2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend()5. Run-time PM Initialization, Device Probing and Removal
drivers/base/power/runtime.c +2
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle(struct device *dev, int rpmflags) { unsigned long flags; int retval;
Quoted 8 lines
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 2 lines
drivers/base/power/runtime.c +2
-0+ might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); +Now that I think of it, perhaps it's better to put the
might_sleep()annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.
You can't put the might_sleep after the spin_lock_irqsave(), because you are always in atomic context, and you can't put it after the spin_unlock_irq() that triggers the problem because you have already unconditionally left atomic context.
Anyways, the sleeps happen in a farily rare case, so putting the might_sleep in a more specific location will hide the errors when developers perform simple tests. For example, every kmalloc ends up calling might_sleep_if(flags & __GFP_WAIT), so that putting kmalloc(..., GFP_KERNEL) will print a stack trace every time, instead of only the very rare case when kmalloc has to block in a low memory condition.
The calls are very low overhead - the condition in the might_sleep_if(), and then in the common case:
if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || ...) return;
Quoted 7 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.
Quoted 6 lines
If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.
Minor: s/all/most of/, or something similar since in v2, it doesn't annotate all of the functions anymore, just the main ones likely to be (mis)used by drivers.
Other than that, looks good...
Quoted 4 lines
Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>
Kevin
Quoted 7 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.
Quoted 6 lines
If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.
Minor: s/all/most of/, or something similar since in v2, it doesn't annotate all of the functions anymore, just the main ones likely to be (mis)used by drivers.
Other than that, looks good...
Quoted 4 lines
Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>
Kevin
Quoted 76 lines · Colin Cross
Quoted 53 lines · Rafael J. Wysocki
Quoted 52 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>--- Documentation/power/runtime_pm.txt | 1 + drivers/base/power/runtime.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend() 5. Run-time PM Initialization, Device Probing and Removaldrivers/base/power/runtime.c +2
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle (struct device *dev,int rpmflags) {unsigned long flags;int retval;Quoted 8 lines
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 2 lines
drivers/base/power/runtime.c +2
-0+might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +Now that I think of it, perhaps it's better to put the
might_sleep()annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.You can't put the
might_sleepafter thespin_lock_irqsave(), because you are always in atomic context, and you can't put it after thespin_unlock_irq()that triggers the problem because you have already unconditionally left atomic context.Anyways, the sleeps happen in a farily rare case, so putting the
might_sleepin a more specific location will hide the errors when developers perform simple tests. For example, every kmalloc ends up callingmight_sleep_if(flags & __GFP_WAIT), so that puttingkmalloc(..., GFP_KERNEL)will print a stack trace every time, instead of only the very rare case when kmalloc has to block in a low memory condition.
Quoted 4 lines
The calls are very low overhead - the condition in the
might_sleep_if(), and then in the common case:if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || ...) return;
OK, I'm going to take the $subject patch for 3.2.
Thanks,
Rafael
Quoted 76 lines · Colin Cross
Quoted 53 lines · Rafael J. Wysocki
Quoted 52 lines · Colin Cross
Some of the entry points to pm runtime are not safe to call in atomic context unless
pm_runtime_irq_safe()has been called. Inspecting the code, it is not immediately obvious that the functions sleep at all, as they run inside aspin_lock_irqsave, but under some conditions they can drop the lock and turn on irqs.If a driver incorrectly calls the
pm_runtimeapis, it can cause sleeping and irq processing when it expects to stay in atomic context.Add
might_sleep_ifto all the __pm_runtime_* entry points to enforce correct usage.Add
pm_runtime_put_sync_autosuspendto the list of functions that can be called in atomic context.Signed-off-by Colin Cross <[email protected]>--- Documentation/power/runtime_pm.txt | 1 + drivers/base/power/runtime.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)Documentation/power/runtime_pm.txt +1
-0@@ -469,6 +469,7 @@ pm_runtime_resume() pm_runtime_get_sync() pm_runtime_put_sync() pm_runtime_put_sync_suspend() +pm_runtime_put_sync_autosuspend() 5. Run-time PM Initialization, Device Probing and Removaldrivers/base/power/runtime.c +2
-1@@ -731,13 +731,16 @@ EXPORT_SYMBOL_GPL(pm_schedule_suspend);* return immediately if it is larger than zero. Then carry out an idle * notification, either synchronous or asynchronous. * -* This routine may be called in atomic context if the RPM_ASYNC flag is set. +* This routine may be called in atomic context if the RPM_ASYNC flag is set, +* or if pm_runtime_irq_safe() has been called. */ int __pm_runtime_idle (struct device *dev,int rpmflags) {unsigned long flags;int retval;Quoted 8 lines
drivers/base/power/runtime.c EXPORT_SYMBOL_GPL(pm_schedule_suspend); · line 743
Quoted 2 lines
drivers/base/power/runtime.c +2
-0+might_sleep_if (!(rpmflags &RPM_ASYNC ) && !dev->power.irq_safe); +Now that I think of it, perhaps it's better to put the
might_sleep()annotations into the actual code paths that should trigger them instead of checking the conditions upfront on every call? This way we'll avoid quite some overhead that's only necessary for debugging.You can't put the
might_sleepafter thespin_lock_irqsave(), because you are always in atomic context, and you can't put it after thespin_unlock_irq()that triggers the problem because you have already unconditionally left atomic context.Anyways, the sleeps happen in a farily rare case, so putting the
might_sleepin a more specific location will hide the errors when developers perform simple tests. For example, every kmalloc ends up callingmight_sleep_if(flags & __GFP_WAIT), so that puttingkmalloc(..., GFP_KERNEL)will print a stack trace every time, instead of only the very rare case when kmalloc has to block in a low memory condition.
Quoted 4 lines
The calls are very low overhead - the condition in the
might_sleep_if(), and then in the common case:if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || ...) return;
OK, I'm going to take the $subject patch for 3.2.
Thanks,
Rafael