Skip to contentSkip to conversation

PM: runtime: Allocate pm_wq with WQ_HIGHPRI

PATCH In review Chung-Kai (Michael) Mei 2 participants 4 messages Oct 27 – Nov 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: runtime: Allocate pm_wq with WQ_HIGHPRI

Asynchronous runtime PM work (pm_runtime_work()) and genpd power-off work (genpd_power_off_work_fn()) run on pm_wq. While such a work item executes a ->runtime_suspend() or ->runtime_resume() callback, the device's runtime_status is RPM_SUSPENDING or RPM_RESUMING with dev->power.lock dropped, and genpd_power_off_work_fn() holds the genpd lock.

If another task calls pm_runtime_get_sync() on the device in that window, rpm_resume() sleeps on dev->power.wait_queue in TASK_UNINTERRUPTIBLE until the work item completes. A wait queue has no owner, so the waiter cannot lend its priority to the kworker, and its latency is bounded by how quickly the pm_wq worker gets CPU time. Because asynchronous runtime PM work is queued onto pm_wq by the PM core, individual drivers cannot select a different workqueue.

pm_wq workers run at nice 0 and can be kept off the CPU for a long time by higher-weight CFS tasks. On an arm64 Android device whose GPU driver uses runtime PM autosuspend, render threads were observed blocked in rpm_resume() for 5 to 19 ms while the runtime PM callback itself needed less than 60 us of CPU time. For more than 99% of that time the pm_wq worker was runnable, preempted by nice -20 tasks.

Allocate pm_wq with WQ_HIGHPRI so that its work items are served by the unbound high-priority worker pools (nice -20), like other latency-sensitive kernel workqueues such as kblockd. This does not change the ordering or concurrency semantics of pm_wq. It only reduces the time a runtime PM transition spends waiting for CPU time, and with it the latency seen by synchronous runtime PM callers.

---
 kernel/power/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/power/main.c +1 -1
index 5429e9f19b655..1e8ae76356b23 100644
@@ -1125,7 +1125,7 @@ EXPORT_SYMBOL_GPL(pm_wq);
 
 static int __init pm_start_workqueues(void)
 {
-	pm_wq = alloc_workqueue("pm", WQ_UNBOUND, 0);
+	pm_wq = alloc_workqueue("pm", WQ_UNBOUND | WQ_HIGHPRI, 0);
 	if (!pm_wq)
 		return -ENOMEM;
 

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222

Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
#1 lore raw
Quoted 16 lines · Chung-Kai (Michael) Mei

Asynchronous runtime PM work (pm_runtime_work()) and genpd power-off work (genpd_power_off_work_fn()) run on pm_wq. While such a work item executes a ->runtime_suspend() or ->runtime_resume() callback, the device's runtime_status is RPM_SUSPENDING or RPM_RESUMING with dev->power.lock dropped, and genpd_power_off_work_fn() holds the genpd lock.

If another task calls pm_runtime_get_sync() on the device in that window, rpm_resume() sleeps on dev->power.wait_queue in TASK_UNINTERRUPTIBLE until the work item completes. A wait queue has no owner, so the waiter cannot lend its priority to the kworker, and its latency is bounded by how quickly the pm_wq worker gets CPU time. Because asynchronous runtime PM work is queued onto pm_wq by the PM core, individual drivers cannot select a different workqueue.

Quoted 6 lines

pm_wq workers run at nice 0 and can be kept off the CPU for a long time by higher-weight CFS tasks. On an arm64 Android device whose GPU driver uses runtime PM autosuspend, render threads were observed blocked in rpm_resume() for 5 to 19 ms while the runtime PM callback itself needed less than 60 us of CPU time. For more than 99% of that time the pm_wq worker was runnable, preempted by nice -20 tasks.

Interesting.

Quoted 6 lines

Allocate pm_wq with WQ_HIGHPRI so that its work items are served by the unbound high-priority worker pools (nice -20), like other latency-sensitive kernel workqueues such as kblockd. This does not change the ordering or concurrency semantics of pm_wq. It only reduces the time a runtime PM transition spends waiting for CPU time, and with it the latency seen by synchronous runtime PM callers.

I suppose that there is a measurable performance benefit from this change, so it would be good to say what it is (say, an increased frame rate or similar).

Essentially, we're talking about redistributing CPU time between tasks, so some tasks will get more latency as a result of this change, which means that it is not an obviously right thing to do.

Quoted 24 lines
---
 kernel/power/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/power/main.c +1 -1
index 5429e9f19b655..1e8ae76356b23 100644
@@ -1125,7 +1125,7 @@ EXPORT_SYMBOL_GPL(pm_wq);
 
 static int __init pm_start_workqueues(void)
 {
-       pm_wq = alloc_workqueue("pm", WQ_UNBOUND, 0);
+       pm_wq = alloc_workqueue("pm", WQ_UNBOUND | WQ_HIGHPRI, 0);
        if (!pm_wq)
                return -ENOMEM;
 

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222 --
2.56.0.307.g5fe7bd680c-goog

#2 lore raw
Quoted 5 lines · Rafael J. Wysocki

Interesting.

I suppose that there is a measurable performance benefit from this change, so it would be good to say what it is (say, an increased frame rate or similar).

Below is A/B data from two arm64 Android phones (6.12-based kernels) comparing unbound pm_wq at nice 0 vs. WQ_HIGHPRI (nice -20), with this patch as the only difference.

The workload is an automated UI test that repeatedly swipes open and collapses the notification shade on a 120 Hz display, driving frequent GPU and display runtime PM suspend/resume transitions alongside active frame composition:

Platform A: 8 CPUs, 245 vs. 245 runs (220,524 vs. 217,954 work items)
---------------------------------------------------------------------

  pm_runtime_work CPU wait mean    151.0 -> 107.5 us        (-28.8%)
  pm_runtime_work CPU wait P99     1,867 -> 1,102 us        (-41.0%)
  pm_runtime_work CPU wait max     15.08 -> 5.77 ms         (-61.7%)
  pm_runtime_work wait >= 2 ms/run  7.51 -> 0.94            (-87.5%)
  pm_runtime_work wait >= 5 ms/run 0.457 -> 0.008           (-98.2%)
  UI rpm_resume() waker wait P90   662.7 -> 156.7 us        (-76.4%)
  UI rpm_resume() D-state P90      717.4 -> 213.7 us        (-70.2%)
  FrameTimeline app missed/run     0.204 -> 0.171           (-16.0%)
  FrameTimeline SF missed/run      1.086 -> 1.074           (-1.1%)

Platform B: 7 CPUs, 196 vs. 245 runs (85,957 vs. 104,452 work items)
---------------------------------------------------------------------

  pm_runtime_work CPU wait mean    134.5 -> 100.9 us        (-25.0%)
  pm_runtime_work CPU wait P99     1,809 -> 1,122 us        (-38.0%)
  pm_runtime_work CPU wait max     15.77 -> 3.71 ms         (-76.5%)
  pm_runtime_work wait >= 2 ms/run  3.40 -> 0.53            (-84.5%)
  pm_runtime_work wait >= 5 ms/run 0.189 -> 0.000          (-100.0%)
  UI rpm_resume() waker wait P90   207.5 -> 23.3 us         (-88.8%)
  UI rpm_resume() D-state P90      217.1 -> 73.3 us         (-66.2%)
  FrameTimeline SF missed/run      0.551 -> 0.531           (-3.7%)
Quoted 3 lines

Essentially, we're talking about redistributing CPU time between tasks, so some tasks will get more latency as a result of this change, which means that it is not an obviously right thing to do.

Agreed, this is a scheduling trade-off: pm_wq runs arbitrary driver callbacks and genpd work whose cost is driver- and platform-dependent, so the impact on other tasks cannot be assumed negligible across all systems.

If changing the global default is undesirable, would exposing pm_wq with WQ_SYSFS (so platforms that need it can tune /sys/devices/virtual/workqueue/pm/nice) be a cleaner alternative?

Best regards,
Chungkai

#3 lore raw
Quoted 44 lines · Chung-Kai (Michael) Mei
Quoted 5 lines · Rafael J. Wysocki

Interesting.

I suppose that there is a measurable performance benefit from this change, so it would be good to say what it is (say, an increased frame rate or similar).

Below is A/B data from two arm64 Android phones (6.12-based kernels) comparing unbound pm_wq at nice 0 vs. WQ_HIGHPRI (nice -20), with this patch as the only difference.

The workload is an automated UI test that repeatedly swipes open and collapses the notification shade on a 120 Hz display, driving frequent GPU and display runtime PM suspend/resume transitions alongside active frame composition:

Platform A: 8 CPUs, 245 vs. 245 runs (220,524 vs. 217,954 work items)
---------------------------------------------------------------------

  pm_runtime_work CPU wait mean    151.0 -> 107.5 us        (-28.8%)
  pm_runtime_work CPU wait P99     1,867 -> 1,102 us        (-41.0%)
  pm_runtime_work CPU wait max     15.08 -> 5.77 ms         (-61.7%)
  pm_runtime_work wait >= 2 ms/run  7.51 -> 0.94            (-87.5%)
  pm_runtime_work wait >= 5 ms/run 0.457 -> 0.008           (-98.2%)
  UI rpm_resume() waker wait P90   662.7 -> 156.7 us        (-76.4%)
  UI rpm_resume() D-state P90      717.4 -> 213.7 us        (-70.2%)
  FrameTimeline app missed/run     0.204 -> 0.171           (-16.0%)
  FrameTimeline SF missed/run      1.086 -> 1.074           (-1.1%)

Platform B: 7 CPUs, 196 vs. 245 runs (85,957 vs. 104,452 work items)
---------------------------------------------------------------------

  pm_runtime_work CPU wait mean    134.5 -> 100.9 us        (-25.0%)
  pm_runtime_work CPU wait P99     1,809 -> 1,122 us        (-38.0%)
  pm_runtime_work CPU wait max     15.77 -> 3.71 ms         (-76.5%)
  pm_runtime_work wait >= 2 ms/run  3.40 -> 0.53            (-84.5%)
  pm_runtime_work wait >= 5 ms/run 0.189 -> 0.000          (-100.0%)
  UI rpm_resume() waker wait P90   207.5 -> 23.3 us         (-88.8%)
  UI rpm_resume() D-state P90      217.1 -> 73.3 us         (-66.2%)
  FrameTimeline SF missed/run      0.551 -> 0.531           (-3.7%)
Quoted 3 lines

Essentially, we're talking about redistributing CPU time between tasks, so some tasks will get more latency as a result of this change, which means that it is not an obviously right thing to do.

Quoted 6 lines

Agreed, this is a scheduling trade-off: pm_wq runs arbitrary driver callbacks and genpd work whose cost is driver- and platform-dependent, so the impact on other tasks cannot be assumed negligible across all systems.

If changing the global default is undesirable,

I'm not saying it's undesirable, I'm just saying that there could be more analysis of its impact IMV.

The FrameTimeline numbers don't seem to be changing significantly although there is measurable improvement.

What about other workloads? Are they affected in any way and if so then how?

Quoted 3 lines

would exposing pm_wq
with WQ_SYSFS (so platforms that need it can tune /sys/devices/virtual/workqueue/pm/nice) be a cleaner alternative?

I'm not a big fan of this TBH.