From b56b74618ee2e6938848305a1ff0f453619d90b4 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Sat, 28 Nov 2026 03:10:26 -0500 Subject: [PATCH 1/3] thermal: apple-pmp: avoid P-core wakeups for cool idle polling When the die is cool and idle (below 95 C with no active cooling level or fault), apple-pmp-thermal currently runs its 100 ms poll_timer hrtimer pinned to whichever CPU armed it (often a P-core) and queues poll_work onto CPU-bound system_highpri_wq, waking a P-core ~10 times/s at idle. power_work also runs on CPU-bound system_wq every 500 ms. Move die polling onto a dedicated single-active unbound high-priority workqueue (WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM). Keep the 5 ms self-rearming hrtimer unchanged whenever fast_poll is true, and hand off to an unbound delayed_work at the same 100 ms slow cadence when !fast_poll so idle timers and workers can run on E-cores without weakening any thermal cadence or slope window. Queue power_work on system_unbound_wq at the same 500 ms interval. Serialize setup, cleanup, and PM prepare/complete under lifecycle_lock and publish live polling only after cpufreq thermal ownership handoff succeeds. Signed-off-by: Erik Bourget --- drivers/thermal/apple-pmp-thermal.c | 152 +++++++++++++++++++++++----- 1 file changed, 127 insertions(+), 25 deletions(-) diff --git a/drivers/thermal/apple-pmp-thermal.c b/drivers/thermal/apple-pmp-thermal.c index 67a29098206f6..aaed4af390f2c 100644 --- a/drivers/thermal/apple-pmp-thermal.c +++ b/drivers/thermal/apple-pmp-thermal.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ /* Inside the cooling slope window (50-250 ms); used while cool and idle. */ #define APPLE_PMP_SLOW_POLL_MS 100 #define APPLE_PMP_GOVERNOR_NS 100000000ULL +#define APPLE_PMP_POWER_POLL_MS 500 struct apple_pmp_cluster { struct cpufreq_policy *policy; @@ -55,10 +57,14 @@ static struct { struct thermal_zone_device *zone; struct apple_pmp_cluster clusters[2]; struct mutex lock; /* Serializes sensor reads, mode changes and guard updates. */ + struct mutex lifecycle_lock; /* Serializes setup, cleanup, prepare and complete. */ + spinlock_t poll_lock; /* Serializes poll_armed, fast_timer_armed and scheduling. */ + struct workqueue_struct *poll_wq; struct apple_pmp_cooling cooling; struct apple_pmp_power_policy power; struct delayed_work setup_work; struct delayed_work power_work; + struct delayed_work slow_poll_work; struct device *power_hwmon; unsigned int rail_mw[3]; int power_error; @@ -78,6 +84,8 @@ static struct { struct hrtimer poll_timer; struct work_struct poll_work; bool polling; + bool poll_armed; + bool fast_timer_armed; bool fast_poll; bool owner; /* apple-soc-cpufreq's unmanaged cap is released */ bool critical_dumped; @@ -85,6 +93,8 @@ static struct { bool sleep_paused; } pmp_thermal = { .lock = __MUTEX_INITIALIZER(pmp_thermal.lock), + .lifecycle_lock = __MUTEX_INITIALIZER(pmp_thermal.lifecycle_lock), + .poll_lock = __SPIN_LOCK_UNLOCKED(pmp_thermal.poll_lock), }; /* Read-only cadence diagnostics: reading temperatures must not advance these. */ @@ -250,7 +260,8 @@ static void apple_pmp_power_work(struct work_struct *work) if (ret) pr_warn_ratelimited("apple-pmp-thermal: power telemetry error %d, retaining minimum CPU/GPU ceilings\n", ret); - schedule_delayed_work(&pmp_thermal.power_work, msecs_to_jiffies(500)); + queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); } static umode_t apple_pmp_power_visible(const void *data, @@ -521,6 +532,30 @@ static void apple_pmp_critical(struct thermal_zone_device *zone) } } +static void apple_pmp_poll_ensure_fast(void) +{ + guard(spinlock_irqsave)(&pmp_thermal.poll_lock); + if (!pmp_thermal.poll_armed || !READ_ONCE(pmp_thermal.fast_poll) || + pmp_thermal.fast_timer_armed) + return; + cancel_delayed_work(&pmp_thermal.slow_poll_work); + pmp_thermal.fast_timer_armed = true; + hrtimer_start(&pmp_thermal.poll_timer, + ms_to_ktime(APPLE_PMP_POLL_MS), + HRTIMER_MODE_REL); +} + +static void apple_pmp_poll_stop(void) +{ + scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) + pmp_thermal.poll_armed = false; + hrtimer_cancel(&pmp_thermal.poll_timer); + scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) + pmp_thermal.fast_timer_armed = false; + cancel_delayed_work_sync(&pmp_thermal.slow_poll_work); + cancel_work_sync(&pmp_thermal.poll_work); +} + static void apple_pmp_poll_work(struct work_struct *work) { u64 now; @@ -534,6 +569,26 @@ static void apple_pmp_poll_work(struct work_struct *work) pmp_thermal.governor_count++; thermal_zone_device_update(pmp_thermal.zone, THERMAL_EVENT_UNSPECIFIED); } + apple_pmp_poll_ensure_fast(); +} + +static void apple_pmp_slow_poll_work(struct work_struct *work) +{ + guard(spinlock_irqsave)(&pmp_thermal.poll_lock); + if (!pmp_thermal.poll_armed) + return; + queue_work(pmp_thermal.poll_wq, &pmp_thermal.poll_work); + if (READ_ONCE(pmp_thermal.fast_poll)) { + if (!pmp_thermal.fast_timer_armed) { + pmp_thermal.fast_timer_armed = true; + hrtimer_start(&pmp_thermal.poll_timer, + ms_to_ktime(APPLE_PMP_POLL_MS), + HRTIMER_MODE_REL); + } + return; + } + queue_delayed_work(system_unbound_wq, &pmp_thermal.slow_poll_work, + msecs_to_jiffies(APPLE_PMP_SLOW_POLL_MS)); } static enum hrtimer_restart apple_pmp_poll_timer(struct hrtimer *timer) @@ -541,22 +596,36 @@ static enum hrtimer_restart apple_pmp_poll_timer(struct hrtimer *timer) /* Sampling/QoS may sleep, so never perform either in timer context. * One work item coalesces missed ticks; no burst of stale confirmations. */ - queue_work(system_highpri_wq, &pmp_thermal.poll_work); - hrtimer_forward_now(timer, ms_to_ktime(READ_ONCE(pmp_thermal.fast_poll) ? - APPLE_PMP_POLL_MS : - APPLE_PMP_SLOW_POLL_MS)); - return HRTIMER_RESTART; + guard(spinlock_irqsave)(&pmp_thermal.poll_lock); + if (!pmp_thermal.poll_armed) { + pmp_thermal.fast_timer_armed = false; + return HRTIMER_NORESTART; + } + queue_work(pmp_thermal.poll_wq, &pmp_thermal.poll_work); + if (READ_ONCE(pmp_thermal.fast_poll)) { + hrtimer_forward_now(timer, ms_to_ktime(APPLE_PMP_POLL_MS)); + pmp_thermal.fast_timer_armed = true; + return HRTIMER_RESTART; + } + pmp_thermal.fast_timer_armed = false; + queue_delayed_work(system_unbound_wq, &pmp_thermal.slow_poll_work, + msecs_to_jiffies(APPLE_PMP_SLOW_POLL_MS)); + return HRTIMER_NORESTART; } static int apple_pmp_change_mode(struct thermal_zone_device *zone, enum thermal_device_mode mode) { + int ret; + guard(mutex)(&pmp_thermal.lock); pmp_thermal.enabled = mode == THERMAL_DEVICE_ENABLED; apple_pmp_gpu_publish(pmp_thermal.sensors, 0, 8, 0); apple_pmp_cooling_init(&pmp_thermal.cooling); WRITE_ONCE(pmp_thermal.fast_poll, true); - return apple_pmp_guard(8); + ret = apple_pmp_guard(8); + apple_pmp_poll_ensure_fast(); + return ret; } static bool apple_pmp_should_bind(struct thermal_zone_device *zone, @@ -658,10 +727,13 @@ static void apple_pmp_thermal_cleanup(void) pmp_thermal.power_hwmon = NULL; } if (pmp_thermal.polling) { - hrtimer_cancel(&pmp_thermal.poll_timer); - cancel_work_sync(&pmp_thermal.poll_work); + apple_pmp_poll_stop(); pmp_thermal.polling = false; } + if (pmp_thermal.poll_wq) { + destroy_workqueue(pmp_thermal.poll_wq); + pmp_thermal.poll_wq = NULL; + } /* Drain the publisher before the final invalidation. */ apple_pmp_gpu_publish(pmp_thermal.sensors, 0, 8, 0); if (pmp_thermal.zone) { @@ -899,13 +971,17 @@ static int apple_pmp_thermal_setup(void) pmp_thermal.power_hwmon = NULL; goto fail; } - pmp_thermal.power_polling = true; - schedule_delayed_work(&pmp_thermal.power_work, msecs_to_jiffies(500)); - pmp_thermal.polling = true; - pmp_thermal.fast_poll = true; - hrtimer_start(&pmp_thermal.poll_timer, ms_to_ktime(APPLE_PMP_POLL_MS), - HRTIMER_MODE_REL); - /* Both guards start at the minimum frequency: hand over the cpufreq cap. */ + pmp_thermal.poll_wq = alloc_workqueue("apple-pmp-thermal", + WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM, + 1); + if (!pmp_thermal.poll_wq) { + ret = -ENOMEM; + goto fail; + } + /* Both guards start at the minimum frequency: hand over the cpufreq cap + * before publishing live polling so a handoff failure never races with + * PM suspend/resume. + */ pmp_thermal.owner = true; for (i = 0; i < ARRAY_SIZE(pmp_thermal.clusters); i++) { ret = apple_soc_cpufreq_set_thermal_owner(pmp_thermal.clusters[i].policy, @@ -913,6 +989,20 @@ static int apple_pmp_thermal_setup(void) if (ret) goto fail; } + pmp_thermal.power_polling = true; + pmp_thermal.polling = true; + WRITE_ONCE(pmp_thermal.fast_poll, true); + if (!pmp_thermal.sleep_paused) { + queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); + scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) { + pmp_thermal.poll_armed = true; + pmp_thermal.fast_timer_armed = true; + hrtimer_start(&pmp_thermal.poll_timer, + ms_to_ktime(APPLE_PMP_POLL_MS), + HRTIMER_MODE_REL); + } + } pr_info("apple-pmp-thermal: T8132 provisional policy: CPU/GPU cooling from 106 C, target 110 C, full protection 115 C, three >=120 C samples for shutdown, 5 ms sampling from 95 C or while cooling (100 ms below), 100 ms governor, %s\n", pmp_thermal.power.unbudgeted ? "no power budget (fan cooled; rail faults still restrict)" : @@ -926,7 +1016,10 @@ static int apple_pmp_thermal_setup(void) static void apple_pmp_thermal_setup_work(struct work_struct *work) { - int ret = apple_pmp_thermal_setup(); + int ret; + + scoped_guard(mutex, &pmp_thermal.lifecycle_lock) + ret = apple_pmp_thermal_setup(); /* cpufreq policy creation is not a driver bind, so it need not retrigger * deferred platform probes. Retry only this startup dependency while @@ -957,6 +1050,7 @@ static int apple_pmp_thermal_probe(struct platform_device *pdev) pmp_thermal.governor_ns = 0; INIT_DELAYED_WORK(&pmp_thermal.setup_work, apple_pmp_thermal_setup_work); INIT_DELAYED_WORK(&pmp_thermal.power_work, apple_pmp_power_work); + INIT_DELAYED_WORK(&pmp_thermal.slow_poll_work, apple_pmp_slow_poll_work); INIT_WORK(&pmp_thermal.poll_work, apple_pmp_poll_work); hrtimer_setup(&pmp_thermal.poll_timer, apple_pmp_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); @@ -973,10 +1067,9 @@ static int apple_pmp_thermal_prepare(struct device *dev) { if (!pm_suspend_via_firmware()) return 0; - if (pmp_thermal.polling) { - hrtimer_cancel(&pmp_thermal.poll_timer); - cancel_work_sync(&pmp_thermal.poll_work); - } + guard(mutex)(&pmp_thermal.lifecycle_lock); + if (pmp_thermal.polling) + apple_pmp_poll_stop(); if (pmp_thermal.power_polling) cancel_delayed_work_sync(&pmp_thermal.power_work); pmp_thermal.sleep_paused = true; @@ -987,6 +1080,7 @@ static void apple_pmp_thermal_complete(struct device *dev) { struct apple_pmp_power_policy *p = &pmp_thermal.power; + guard(mutex)(&pmp_thermal.lifecycle_lock); if (!pmp_thermal.sleep_paused) return; pmp_thermal.sleep_paused = false; @@ -1001,10 +1095,17 @@ static void apple_pmp_thermal_complete(struct device *dev) p->level[2], p->last_ns); } if (pmp_thermal.power_polling) - schedule_delayed_work(&pmp_thermal.power_work, msecs_to_jiffies(500)); - if (pmp_thermal.polling) - hrtimer_start(&pmp_thermal.poll_timer, ms_to_ktime(APPLE_PMP_POLL_MS), - HRTIMER_MODE_REL); + queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); + if (pmp_thermal.polling) { + scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) { + pmp_thermal.poll_armed = true; + pmp_thermal.fast_timer_armed = true; + hrtimer_start(&pmp_thermal.poll_timer, + ms_to_ktime(APPLE_PMP_POLL_MS), + HRTIMER_MODE_REL); + } + } } static const struct dev_pm_ops apple_pmp_thermal_pm_ops = { @@ -1015,6 +1116,7 @@ static const struct dev_pm_ops apple_pmp_thermal_pm_ops = { static void apple_pmp_thermal_remove(struct platform_device *pdev) { cancel_delayed_work_sync(&pmp_thermal.setup_work); + guard(mutex)(&pmp_thermal.lifecycle_lock); apple_pmp_thermal_cleanup(); } From 6ffed106f4815ff0a67531f501eefd229c45a079 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Sat, 28 Nov 2026 15:42:42 -0500 Subject: [PATCH 2/3] thermal: apple-pmp: pause samplers on s2idle and guard slow-poll race apple_pmp_thermal_prepare() previously returned early on !pm_suspend_via_firmware(), leaving the unbound poll/power workqueues and fast hrtimer unpaused across s2idle and creating a conflict hazard if any caller or merge cancels poll_timer without setting sleep_paused (leaving fast_timer_armed latched and die polling stopped permanently after hot s2idle resume, causing -EAGAIN power telemetry errors). Pause both die and power polling unconditionally across every system sleep in apple_pmp_thermal_prepare() under lifecycle_lock so apple_pmp_poll_stop() and apple_pmp_thermal_complete() always stay paired. Also guard apple_pmp_slow_poll_work() against fast_timer_armed so a racing slow_poll_work callback cannot re-queue itself if apple_pmp_poll_ensure_fast() already armed poll_timer. Add host tests in tools/asahi/pmp-thermal-poll-tests.py covering s2idle prepare/complete across fast and slow polling modes and the slow_poll_work vs fast_timer_armed race. Signed-off-by: Erik Bourget --- drivers/thermal/apple-pmp-thermal.c | 21 +- tools/asahi/pmp-thermal-poll-tests.py | 280 ++++++++++++++++++++++++++ tools/asahi/run-host-tests.py | 1 + 3 files changed, 290 insertions(+), 12 deletions(-) create mode 100755 tools/asahi/pmp-thermal-poll-tests.py diff --git a/drivers/thermal/apple-pmp-thermal.c b/drivers/thermal/apple-pmp-thermal.c index aaed4af390f2c..f5370c18e59cb 100644 --- a/drivers/thermal/apple-pmp-thermal.c +++ b/drivers/thermal/apple-pmp-thermal.c @@ -575,16 +575,14 @@ static void apple_pmp_poll_work(struct work_struct *work) static void apple_pmp_slow_poll_work(struct work_struct *work) { guard(spinlock_irqsave)(&pmp_thermal.poll_lock); - if (!pmp_thermal.poll_armed) + if (!pmp_thermal.poll_armed || pmp_thermal.fast_timer_armed) return; queue_work(pmp_thermal.poll_wq, &pmp_thermal.poll_work); if (READ_ONCE(pmp_thermal.fast_poll)) { - if (!pmp_thermal.fast_timer_armed) { - pmp_thermal.fast_timer_armed = true; - hrtimer_start(&pmp_thermal.poll_timer, - ms_to_ktime(APPLE_PMP_POLL_MS), - HRTIMER_MODE_REL); - } + pmp_thermal.fast_timer_armed = true; + hrtimer_start(&pmp_thermal.poll_timer, + ms_to_ktime(APPLE_PMP_POLL_MS), + HRTIMER_MODE_REL); return; } queue_delayed_work(system_unbound_wq, &pmp_thermal.slow_poll_work, @@ -1059,14 +1057,13 @@ static int apple_pmp_thermal_probe(struct platform_device *pdev) } /* - * A deep sleep stops the PMP: sampling on through it would read its sensors stopped and - * restrict the CPUs and the GPU for seconds after the wake. Nothing runs to heat the die - * from the first device callback to the last, so neither sampler runs in between. + * Deep sleep stops the PMP and s2idle parks all clusters in WFI: sampling on + * through either sleep would read stopped/dark sensors and restrict the CPUs + * and the GPU after wake. Nothing runs to heat the die from the first device + * callback to the last, so pause both samplers across every system sleep. */ static int apple_pmp_thermal_prepare(struct device *dev) { - if (!pm_suspend_via_firmware()) - return 0; guard(mutex)(&pmp_thermal.lifecycle_lock); if (pmp_thermal.polling) apple_pmp_poll_stop(); diff --git a/tools/asahi/pmp-thermal-poll-tests.py b/tools/asahi/pmp-thermal-poll-tests.py new file mode 100755 index 0000000000000..abe1706bc1a5d --- /dev/null +++ b/tools/asahi/pmp-thermal-poll-tests.py @@ -0,0 +1,280 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Host tests for apple-pmp-thermal polling state machine and sleep pause/resume.""" +import os +import subprocess +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +SRC = (ROOT / 'drivers/thermal/apple-pmp-thermal.c').read_text() + + +def extract_fn(name): + start = SRC.index(name) + end = SRC.index('\n}\n', start) + 3 + return SRC[start:end] + + +# Ensure prepare() never bypasses sleep pausing or cancels poll_timer outside apple_pmp_poll_stop(). +prep_fn = extract_fn('static int apple_pmp_thermal_prepare(') +assert 'pm_suspend_via_firmware' not in prep_fn, 'prepare() must pause samplers across s2idle too' +assert 'hrtimer_cancel' not in prep_fn, 'prepare() must stop polling via apple_pmp_poll_stop()' + +code = '\n'.join([ + extract_fn('static void apple_pmp_poll_ensure_fast('), + extract_fn('static void apple_pmp_poll_stop('), + extract_fn('static void apple_pmp_slow_poll_work('), + extract_fn('static enum hrtimer_restart apple_pmp_poll_timer('), + prep_fn, + extract_fn('static void apple_pmp_thermal_complete('), +]) + +HARNESS = r''' +#include +#include +#include +#include +#include + +typedef uint64_t u64; +typedef uint64_t ktime_t; +struct device { int dummy; }; +struct work_struct { int dummy; }; +struct delayed_work { struct work_struct work; }; +struct hrtimer { int dummy; }; +struct workqueue_struct { int dummy; }; +struct mutex { int locked; }; +typedef struct { int locked; } spinlock_t; +enum hrtimer_restart { HRTIMER_NORESTART = 0, HRTIMER_RESTART = 1 }; +#define HRTIMER_MODE_REL 0 +#define BIT(x) (1U << (x)) +#define APPLE_PMP_POLL_MS 5 +#define APPLE_PMP_SLOW_POLL_MS 100 +#define APPLE_PMP_POWER_POLL_MS 500 +#define READ_ONCE(x) (x) +#define WRITE_ONCE(x, v) ((x) = (v)) + +static struct workqueue_struct mock_unbound_wq, mock_poll_wq; +static struct workqueue_struct *system_unbound_wq = &mock_unbound_wq; + +static bool timer_active; +static bool slow_work_pending; +static bool power_work_pending; +static unsigned int poll_work_queued; +static unsigned int gpu_publishes; +static u64 mock_now_ns = 1000000000ULL; + +static u64 ktime_get_ns(void) { return mock_now_ns; } +static ktime_t ms_to_ktime(unsigned int ms) { return (ktime_t)ms * 1000000ULL; } +static unsigned long msecs_to_jiffies(unsigned int ms) { return ms; } + +static void hrtimer_start(struct hrtimer *t, ktime_t tim, int mode) +{ + (void)t; (void)tim; (void)mode; + timer_active = true; +} + +static int hrtimer_cancel(struct hrtimer *t) +{ + (void)t; + bool was = timer_active; + timer_active = false; + return was; +} + +static u64 hrtimer_forward_now(struct hrtimer *t, ktime_t interval) +{ + (void)t; (void)interval; + return 1; +} + +struct apple_pmp_power_policy { + u64 last_ns; + unsigned int active; + unsigned int level[3]; +}; + +static struct { + const void *sensors; + struct mutex lock; + struct mutex lifecycle_lock; + spinlock_t poll_lock; + struct workqueue_struct *poll_wq; + struct apple_pmp_power_policy power; + struct delayed_work power_work; + struct delayed_work slow_poll_work; + u64 gpu_epoch; + bool power_polling; + struct hrtimer poll_timer; + struct work_struct poll_work; + bool polling; + bool poll_armed; + bool fast_timer_armed; + bool fast_poll; + bool sleep_paused; +} pmp_thermal = { + .poll_wq = &mock_poll_wq, +}; + +static bool queue_work(struct workqueue_struct *wq, struct work_struct *w) +{ + assert(wq == pmp_thermal.poll_wq); + if (w == &pmp_thermal.poll_work) + poll_work_queued++; + return true; +} + +static bool queue_delayed_work(struct workqueue_struct *wq, + struct delayed_work *dw, unsigned long delay) +{ + assert(wq == system_unbound_wq); + (void)delay; + if (dw == &pmp_thermal.slow_poll_work) + slow_work_pending = true; + else if (dw == &pmp_thermal.power_work) + power_work_pending = true; + return true; +} + +static bool cancel_delayed_work(struct delayed_work *dw) +{ + if (dw == &pmp_thermal.slow_poll_work) { + bool was = slow_work_pending; + slow_work_pending = false; + return was; + } + if (dw == &pmp_thermal.power_work) { + bool was = power_work_pending; + power_work_pending = false; + return was; + } + return false; +} + +static bool cancel_delayed_work_sync(struct delayed_work *dw) +{ + return cancel_delayed_work(dw); +} + +static bool cancel_work_sync(struct work_struct *w) +{ + if (w == &pmp_thermal.poll_work) + poll_work_queued = 0; + return true; +} + +static void apple_pmp_gpu_publish(const void *s, u64 epoch, unsigned int level, u64 ns) +{ + (void)s; (void)epoch; (void)level; (void)ns; + gpu_publishes++; +} + +static inline struct mutex *guard_mutex_enter(struct mutex *m) +{ + assert(!m->locked); + m->locked = 1; + return m; +} +static inline void guard_mutex_exit(struct mutex **m) +{ + assert((*m)->locked); + (*m)->locked = 0; +} +static inline spinlock_t *guard_spin_enter(spinlock_t *s) +{ + assert(!s->locked); + s->locked = 1; + return s; +} +static inline void guard_spin_exit(spinlock_t **s) +{ + assert((*s)->locked); + (*s)->locked = 0; +} + +#define CONCAT2(a, b) a##b +#define CONCAT(a, b) CONCAT2(a, b) +#define guard_mutex(m) struct mutex *CONCAT(_gm_, __LINE__) __attribute__((cleanup(guard_mutex_exit))) = guard_mutex_enter(m) +#define guard_spinlock_irqsave(s) spinlock_t *CONCAT(_gs_, __LINE__) __attribute__((cleanup(guard_spin_exit))) = guard_spin_enter(s) +#define guard(type) guard_##type +#define scoped_guard_mutex(m) for (struct mutex *_sgm __attribute__((cleanup(guard_mutex_exit))) = guard_mutex_enter(m), *_once = (struct mutex *)1; _once; _once = NULL) +#define scoped_guard_spinlock_irqsave(s) for (spinlock_t *_sgs __attribute__((cleanup(guard_spin_exit))) = guard_spin_enter(s), *_once = (spinlock_t *)1; _once; _once = NULL) +#define scoped_guard(type, arg) scoped_guard_##type(arg) + +''' + code + r''' + +static void reset_live(bool fast) +{ + memset(&pmp_thermal, 0, sizeof(pmp_thermal)); + pmp_thermal.poll_wq = &mock_poll_wq; + pmp_thermal.polling = true; + pmp_thermal.power_polling = true; + pmp_thermal.poll_armed = true; + pmp_thermal.fast_poll = fast; + pmp_thermal.power.last_ns = 500000000ULL; + pmp_thermal.power.active = BIT(0) | BIT(1) | BIT(2); + pmp_thermal.power.level[2] = 3; + timer_active = fast; + pmp_thermal.fast_timer_armed = fast; + slow_work_pending = !fast; + power_work_pending = true; + poll_work_queued = 0; + gpu_publishes = 0; +} + +int main(void) +{ + struct device dev = {}; + + /* 1. Hot s2idle: prepare stops fast timer + power_work, complete re-arms both. */ + reset_live(true); + assert(apple_pmp_thermal_prepare(&dev) == 0); + assert(pmp_thermal.sleep_paused); + assert(!pmp_thermal.poll_armed && !pmp_thermal.fast_timer_armed); + assert(!timer_active && !slow_work_pending && !power_work_pending); + + mock_now_ns = 20000000000ULL; + apple_pmp_thermal_complete(&dev); + assert(!pmp_thermal.sleep_paused); + assert(pmp_thermal.poll_armed && pmp_thermal.fast_timer_armed); + assert(timer_active && power_work_pending); + assert(pmp_thermal.power.last_ns == mock_now_ns); + assert(gpu_publishes == 1); + assert(apple_pmp_poll_timer(&pmp_thermal.poll_timer) == HRTIMER_RESTART); + assert(poll_work_queued == 1 && timer_active && pmp_thermal.fast_timer_armed); + + /* 2. Cool s2idle: prepare stops slow_poll_work + power_work, complete re-arms and hands off. */ + reset_live(false); + assert(apple_pmp_thermal_prepare(&dev) == 0); + assert(pmp_thermal.sleep_paused && !timer_active && !slow_work_pending && !power_work_pending); + apple_pmp_thermal_complete(&dev); + assert(!pmp_thermal.sleep_paused && timer_active && power_work_pending); + assert(apple_pmp_poll_timer(&pmp_thermal.poll_timer) == HRTIMER_NORESTART); + assert(!pmp_thermal.fast_timer_armed && slow_work_pending); + + /* 3. Race: slow_poll_work enters after ensure_fast() armed poll_timer and fast_poll flipped false. */ + reset_live(false); + slow_work_pending = false; /* timer fired and worker is entering callback */ + pmp_thermal.fast_poll = true; + apple_pmp_poll_ensure_fast(); + assert(timer_active && pmp_thermal.fast_timer_armed); + pmp_thermal.fast_poll = false; + poll_work_queued = 0; + apple_pmp_slow_poll_work(&pmp_thermal.slow_poll_work.work); + assert(!slow_work_pending && poll_work_queued == 0 && timer_active && pmp_thermal.fast_timer_armed); + + puts("PASS pmp-thermal-poll: s2idle/sleep prepare+complete and fast/slow handoff invariants"); + return 0; +} +''' + +with tempfile.TemporaryDirectory() as td: + c = Path(td) / 'pmp-thermal-poll.c' + b = Path(td) / 'pmp-thermal-poll' + c.write_text(HARNESS) + subprocess.run([os.environ.get('CC', 'cc'), '-std=gnu11', '-Wall', '-Wextra', '-Werror', + '-Wno-unused-function', '-Wno-unused-parameter', + '-fsanitize=undefined', '-fno-sanitize-recover=all', + str(c), '-o', str(b)], check=True) + subprocess.run([str(b)], check=True) diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 58a397b0886f7..0262facac5151 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -426,6 +426,7 @@ def mutations(flag, names, base, expect=KILL): 'pmp-agx-vote-tests.py': [(out('--output'), PASS), (out('--output') + ['--mutations'], PASS)], 'pmp-floor-tests.py': [([], PASS)] + mutations('--mutant', ['no-hold', 'no-rate', 'no-streak', 'bad-word', 'foreign-ok', 'no-names', 'no-sum', 'zero-ok', 'lane-wide', 'class-twice'], []), 'pmp-j613-hwmon-tests.py': [([], PASS)], + 'pmp-thermal-poll-tests.py': [([], PASS)], 'pmu-rtc-tests.py': [(out(), PASS)] + mutations('--mutation', ('ack-clobber', 'arm-ack', 'offset-seconds', 'offset-sign', 'event-bit', 'wrong-control', From 83913030d4d54fa17f9a29319728763a278caff6 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Sun, 29 Nov 2026 03:10:21 -0500 Subject: [PATCH 3/3] thermal: apple-pmp: queue the unbound pollers on system_dfl_wq main marks system_unbound_wq deprecated, and the first power poll after boot now warns: workqueue: work func apple_pmp_power_work [apple_pmp_thermal] enqueued on deprecated workqueue. Use system_{percpu|dfl}_wq instead. system_dfl_wq is the unbound system workqueue that replaces it, with the same WQ_UNBOUND flags, max_active and default CPU mask, so the pollers still run on any CPU and are not pinned to the P-cores at idle. The poll host test's workqueue mock follows the rename. Signed-off-by: Erik Bourget --- drivers/thermal/apple-pmp-thermal.c | 10 +++++----- tools/asahi/pmp-thermal-poll-tests.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/thermal/apple-pmp-thermal.c b/drivers/thermal/apple-pmp-thermal.c index f5370c18e59cb..fc41126d0d842 100644 --- a/drivers/thermal/apple-pmp-thermal.c +++ b/drivers/thermal/apple-pmp-thermal.c @@ -260,7 +260,7 @@ static void apple_pmp_power_work(struct work_struct *work) if (ret) pr_warn_ratelimited("apple-pmp-thermal: power telemetry error %d, retaining minimum CPU/GPU ceilings\n", ret); - queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + queue_delayed_work(system_dfl_wq, &pmp_thermal.power_work, msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); } @@ -585,7 +585,7 @@ static void apple_pmp_slow_poll_work(struct work_struct *work) HRTIMER_MODE_REL); return; } - queue_delayed_work(system_unbound_wq, &pmp_thermal.slow_poll_work, + queue_delayed_work(system_dfl_wq, &pmp_thermal.slow_poll_work, msecs_to_jiffies(APPLE_PMP_SLOW_POLL_MS)); } @@ -606,7 +606,7 @@ static enum hrtimer_restart apple_pmp_poll_timer(struct hrtimer *timer) return HRTIMER_RESTART; } pmp_thermal.fast_timer_armed = false; - queue_delayed_work(system_unbound_wq, &pmp_thermal.slow_poll_work, + queue_delayed_work(system_dfl_wq, &pmp_thermal.slow_poll_work, msecs_to_jiffies(APPLE_PMP_SLOW_POLL_MS)); return HRTIMER_NORESTART; } @@ -991,7 +991,7 @@ static int apple_pmp_thermal_setup(void) pmp_thermal.polling = true; WRITE_ONCE(pmp_thermal.fast_poll, true); if (!pmp_thermal.sleep_paused) { - queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + queue_delayed_work(system_dfl_wq, &pmp_thermal.power_work, msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) { pmp_thermal.poll_armed = true; @@ -1092,7 +1092,7 @@ static void apple_pmp_thermal_complete(struct device *dev) p->level[2], p->last_ns); } if (pmp_thermal.power_polling) - queue_delayed_work(system_unbound_wq, &pmp_thermal.power_work, + queue_delayed_work(system_dfl_wq, &pmp_thermal.power_work, msecs_to_jiffies(APPLE_PMP_POWER_POLL_MS)); if (pmp_thermal.polling) { scoped_guard(spinlock_irqsave, &pmp_thermal.poll_lock) { diff --git a/tools/asahi/pmp-thermal-poll-tests.py b/tools/asahi/pmp-thermal-poll-tests.py index abe1706bc1a5d..53609335ae13c 100755 --- a/tools/asahi/pmp-thermal-poll-tests.py +++ b/tools/asahi/pmp-thermal-poll-tests.py @@ -55,8 +55,8 @@ def extract_fn(name): #define READ_ONCE(x) (x) #define WRITE_ONCE(x, v) ((x) = (v)) -static struct workqueue_struct mock_unbound_wq, mock_poll_wq; -static struct workqueue_struct *system_unbound_wq = &mock_unbound_wq; +static struct workqueue_struct mock_dfl_wq, mock_poll_wq; +static struct workqueue_struct *system_dfl_wq = &mock_dfl_wq; static bool timer_active; static bool slow_work_pending; @@ -128,7 +128,7 @@ def extract_fn(name): static bool queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *dw, unsigned long delay) { - assert(wq == system_unbound_wq); + assert(wq == system_dfl_wq); (void)delay; if (dw == &pmp_thermal.slow_poll_work) slow_work_pending = true;