PM: runtime: avoid priority inversion on PREEMPT_RT
Subsystems (4)
PATCH v2 1/1 PM: runtime: avoid priority inversion on PREEMPT_RT
With PREEMPT_RT the cpu_relax() loops in rpm_suspend and rpm_resume can cause unbounded latency if they preempt an asynchronous suspend. The main scenario where this can happen is when a realtime thread resumes a device while it is asynchronously suspending on a worker thread.
I'm not convinced this can actually happen in the rpm_suspend case, or at least it's a lot less likely for a synchronous suspend to run at the same time as an asynchronous suspend, but both functions are updated here for symmetry.
For devices setting power.irq_safe, it is possible that RPM functions will be called with a spinlock held (for example in pl330_issue_pending()). This means a normal call to schedule() can't be used, but to avoid the priority inversion it is necessary to wait and schedule. schedule_rtlock() is only available when CONFIG_PREEMPT_RT is defined, so even though the logic is correct without any preprocessor guards around schedule_rtlock(), they are necessary for compilation.
---
Changes since v1:
- Use schedule_rtlock() instead of schedule() for PREEMPT_RT & irq_safe
- Rewritten commit description
drivers/base/power/runtime.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/base/power/runtime.c +14 -4
@@ -596,7 +596,7 @@ static int rpm_suspend(struct device *dev, int rpmflags) goto out; } -if (dev->power.irq_safe) { +if (dev->power.irq_safe && !IS_ENABLED (CONFIG_PREEMPT_RT )) {spin_unlock (&dev->power.lock);cpu_relax ();
@@ -614,7 +614,12 @@ static int rpm_suspend(struct device *dev, int rpmflags)spin_unlock_irq (&dev->power.lock); -schedule (); +#ifdef CONFIG_PREEMPT_RT +if (dev->power.irq_safe) +schedule_rtlock (); + else +#endif +schedule ();spin_lock_irq (&dev->power.lock); }
@@ -779,7 +784,7 @@ static int rpm_resume(struct device *dev, int rpmflags) goto out; } -if (dev->power.irq_safe) { +if (dev->power.irq_safe && !IS_ENABLED (CONFIG_PREEMPT_RT )) {spin_unlock (&dev->power.lock);cpu_relax ();
@@ -798,7 +803,12 @@ static int rpm_resume(struct device *dev, int rpmflags)spin_unlock_irq (&dev->power.lock); -schedule (); +#ifdef CONFIG_PREEMPT_RT +if (dev->power.irq_safe) +schedule_rtlock (); + else +#endif +schedule ();spin_lock_irq (&dev->power.lock); }
Signature · 1 line
2.34.0
Rafael J. Wysocki
maintainerQuoted 36 lines · John Keeping
With
PREEMPT_RTthecpu_relax()loops inrpm_suspendandrpm_resumecan cause unbounded latency if they preempt an asynchronous suspend. The main scenario where this can happen is when a realtime thread resumes a device while it is asynchronously suspending on a worker thread.I'm not convinced this can actually happen in the
rpm_suspendcase, or at least it's a lot less likely for a synchronous suspend to run at the same time as an asynchronous suspend, but both functions are updated here for symmetry.For devices setting power.irq_safe, it is possible that RPM functions will be called with a spinlock held (for example in
pl330_issue_pending()). This means a normal call toschedule()can't be used, but to avoid the priority inversion it is necessary to wait and schedule.schedule_rtlock()is only available whenCONFIG_PREEMPT_RTis defined, so even though the logic is correct without any preprocessor guards aroundschedule_rtlock(), they are necessary for compilation.Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra (Intel) <[email protected]>Cc Ingo Molnar <[email protected]>Signed-off-by John Keeping <[email protected]>---
Changes since v1:
- Useschedule_rtlock()instead ofschedule()forPREEMPT_RT&irq_safe
- Rewritten commit descriptiondrivers/base/power/runtime.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)drivers/base/power/runtime.c +0
-0
drivers/base/power/runtime.c static int rpm_suspend(struct device *dev, int rpmflags) · line 599
Quoted 5 lines
drivers/base/power/runtime.c +1
-1@@ -596,7 +596,7 @@ static int rpm_suspend(struct device *dev, int rpmflags) goto out; } -if (dev->power.irq_safe) { +if (dev->power.irq_safe && !IS_ENABLED (CONFIG_PREEMPT_RT )) {
Please add a helper to avoid code duplication related to this (even though there is a small amount of it).
drivers/base/power/runtime.c static int rpm_suspend(struct device *dev, int rpmflags) · line 621
Quoted 13 lines
drivers/base/power/runtime.c +5
-1spin_unlock (&dev->power.lock);cpu_relax ();@@ -614,7 +614,12 @@ static int rpm_suspend(struct device *dev, int rpmflags)spin_unlock_irq (&dev->power.lock); -schedule (); +#ifdef CONFIG_PREEMPT_RT +if (dev->power.irq_safe) +schedule_rtlock (); + else +#endif
Same here, and please use the #ifdet inside the helper.
Quoted 28 lines
drivers/base/power/runtime.c +8
-3+schedule ();spin_lock_irq (&dev->power.lock); }@@ -779,7 +784,7 @@ static int rpm_resume(struct device *dev, int rpmflags) goto out; } -if (dev->power.irq_safe) { +if (dev->power.irq_safe && !IS_ENABLED (CONFIG_PREEMPT_RT )) {spin_unlock (&dev->power.lock);cpu_relax ();@@ -798,7 +803,12 @@ static int rpm_resume(struct device *dev, int rpmflags)spin_unlock_irq (&dev->power.lock); -schedule (); +#ifdef CONFIG_PREEMPT_RT +if (dev->power.irq_safe) +schedule_rtlock (); + else +#endif +schedule ();spin_lock_irq (&dev->power.lock); } --
Quoted 40 lines · On Wed, 17 Nov 2021 19:53:47 +0100 "Rafael J. Wysocki" <[email protected]>…
Quoted 39 lines · John Keeping
With
PREEMPT_RTthecpu_relax()loops inrpm_suspendandrpm_resumecan cause unbounded latency if they preempt an asynchronous suspend. The main scenario where this can happen is when a realtime thread resumes a device while it is asynchronously suspending on a worker thread.I'm not convinced this can actually happen in the
rpm_suspendcase, or at least it's a lot less likely for a synchronous suspend to run at the same time as an asynchronous suspend, but both functions are updated here for symmetry.For devices setting power.irq_safe, it is possible that RPM functions will be called with a spinlock held (for example in
pl330_issue_pending()). This means a normal call toschedule()can't be used, but to avoid the priority inversion it is necessary to wait and schedule.schedule_rtlock()is only available whenCONFIG_PREEMPT_RTis defined, so even though the logic is correct without any preprocessor guards aroundschedule_rtlock(), they are necessary for compilation.Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra (Intel) <[email protected]>Cc Ingo Molnar <[email protected]>Signed-off-by John Keeping <[email protected]>---
Changes since v1:
- Useschedule_rtlock()instead ofschedule()forPREEMPT_RT&irq_safe
- Rewritten commit descriptiondrivers/base/power/runtime.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)drivers/base/power/runtime.c +0
-0@@ -596,7 +596,7 @@ static int rpm_suspend(struct device *dev, int rpmflags) goto out; }
Quoted 5 lines
drivers/base/power/runtime.c static int rpm_suspend(struct device *dev, int rpmflags) · line 599
Quoted 2 lines
drivers/base/power/runtime.c +1
-1-if (dev->power.irq_safe) { +if (dev->power.irq_safe && !IS_ENABLED (CONFIG_PREEMPT_RT )) {Please add a helper to avoid code duplication related to this (even though there is a small amount of it).
Ack. I'd like some feedback on the schedule_rtlock() approach from the scheduler & RT people, so I'll wait a bit before sending a v3.