From f735894bca19ea5b028c3a9e7dbaba504c8a0714 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 01:21:14 -0500 Subject: [PATCH 01/11] cpufreq: apple-soc: enable lazy verification fast-switch on T6050 T6050 shares the command-state readback DVFS interface with T8140, where schedutil fast-switch requests are verified asynchronously by timer rather than blocking in ->target_index(). Before enabling .lazy_verify = true on soc_t6050_info, tighten two edge cases in the lazy-verify path: - Clamp deferred pending_pstate writes in the verify timer and restored asked_pstate writes in apple_soc_cpufreq_lazy_cap() against the current policy->min / policy->max bounds as well as the thermal ceiling. - Drop any queued pending_pstate and mark waited when fast-switch ownership ends (policy->fast_switch_enabled becomes false), so a pending schedutil request cannot fire after switching to a static governor such as powersave whose target already equals policy->cur. Enable .lazy_verify = true on soc_t6050_info so schedutil uses fast-switch without per-cluster sugov threads while ->target_index() paths keep synchronous verification. Add KUnit coverage for deferred policy-limit clamping and fast-switch handoff. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 285 ++++++++++++++++++++++- drivers/cpufreq/apple-soc-cpufreq.c | 247 +++++++++++++++++--- 2 files changed, 495 insertions(+), 37 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index 8a207c186d590..267eafe8eb8cc 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -109,7 +109,9 @@ static struct cft_ctx *cft_setup(struct kunit *test, ctx->policy->min = ctx->table[0].frequency; ctx->policy->max = ctx->table[CFT_NR_STATES - 1].frequency; ctx->policy->cur = ctx->table[0].frequency; + ctx->policy->fast_switch_enabled = info->lazy_verify; ctx->policy->driver_data = ctx->priv; + ctx->priv->policy = ctx->policy; test->priv = ctx; return ctx; @@ -1289,7 +1291,74 @@ static void cft_test_fast_die_init_invalid(struct kunit *test) KUNIT_EXPECT_EQ(test, cft_qos_max(ctx), FREQ_QOS_MAX_DEFAULT_VALUE); } +/* + * A deferred pending request and a lifted thermal cap must both respect a + * newer policy->max, and losing fast-switch ownership must drop any pending + * request instead of writing it under a non-fast-switch governor. + */ +static void cft_test_lazy_policy_limits_and_handoff(struct kunit *test) +{ + struct cft_ctx *ctx = cft_setup(test, &soc_t6050_info, 1); + unsigned int freq; + int moved; + + /* Queue a high pending state while BUSY, then lower policy->max. */ + cft_set_cmd(ctx->regs, 1 | APPLE_DVFS_CMD_BUSY); + KUNIT_EXPECT_TRUE(test, cft_lazy_request(ctx, 4, &freq)); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, 15); + + ctx->policy->max = 1800000; + cft_set_cmd(ctx->regs, 1); + KUNIT_EXPECT_TRUE(test, cft_verify(ctx)); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 7ULL); + KUNIT_EXPECT_EQ(test, ctx->priv->expected_pstate, 7U); + + /* Thermal cap restore must also stay within the lowered policy->max. */ + KUNIT_EXPECT_TRUE(test, cft_lazy_cap(ctx, 4, 1200000, &moved)); + KUNIT_EXPECT_EQ(test, moved, 4); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 4ULL); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1200000U); + KUNIT_EXPECT_TRUE(test, cft_lazy_cap(ctx, 15, 3000000, &moved)); + KUNIT_EXPECT_EQ(test, moved, 7); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 7ULL); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); + + /* + * Once QoS publishes a higher policy->max after a cap lift, re-running + * lazy_cap restores asked_pstate even without a new schedutil request. + */ + ctx->policy->max = 3000000; + KUNIT_EXPECT_TRUE(test, cft_lazy_cap(ctx, 15, 3000000, &moved)); + KUNIT_EXPECT_EQ(test, moved, 15); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 15ULL); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 3000000U); + + /* + * Switching away from a fast-switch governor clears + * fast_switch_enabled: the timer must drop the pending state and + * thermal samples must leave hardware and policy->cur untouched. + */ + cft_set_cmd(ctx->regs, 7 | APPLE_DVFS_CMD_BUSY); + ctx->priv->expected_pstate = 7; + ctx->policy->cur = 1800000; + KUNIT_EXPECT_TRUE(test, cft_lazy_request(ctx, 4, &freq)); + KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, 15); + + ctx->policy->fast_switch_enabled = false; + cft_set_cmd(ctx->regs, 7); + KUNIT_EXPECT_FALSE(test, cft_verify(ctx)); + KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 7ULL); + KUNIT_EXPECT_FALSE(test, ctx->priv->transition_failed); + + KUNIT_EXPECT_FALSE(test, cft_lazy_cap(ctx, 1, 600000, &moved)); + KUNIT_EXPECT_EQ(test, moved, -1); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); +} + static struct kunit_case apple_soc_cpufreq_test_cases[] = { + KUNIT_CASE(cft_test_lazy_policy_limits_and_handoff), KUNIT_CASE_PARAM(cft_test_layout, cft_layout_gen_params), KUNIT_CASE(cft_test_layout_invariants), KUNIT_CASE_PARAM(cft_test_write, cft_write_gen_params), @@ -1370,11 +1439,13 @@ static int cft_live_target(struct cpufreq_policy *policy, unsigned int index) static struct cpufreq_driver cft_live_driver = { .name = "apple-cpufreq", - .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY, + .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | + CPUFREQ_NEED_UPDATE_LIMITS, .verify = cpufreq_generic_frequency_table_verify, .init = cft_live_init, .exit = cft_live_exit, .target_index = cft_live_target, + .fast_switch = apple_soc_cpufreq_fast_switch, }; static int cft_live_suite_init(struct kunit_suite *suite) @@ -1524,9 +1595,221 @@ static void cft_test_thermal_owner(struct kunit *test) cpufreq_cpu_put(policy); } +struct cft_cap_lift_ctx { + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv; + bool gov_stopped; +}; + +static void cft_cap_lift_cleanup(void *data) +{ + struct cft_cap_lift_ctx *ctx = data; + struct cpufreq_policy *policy = ctx->policy; + struct apple_cpu_priv *priv = ctx->priv; + + hrtimer_cancel(&priv->verify_timer); + scoped_guard(cpufreq_policy_write, policy) { + policy->suspend_freq = 0; + policy->fast_switch_enabled = false; + priv->policy = NULL; + priv->policy_bounds_valid = false; + priv->waited = true; + if (ctx->gov_stopped) + cpufreq_start_governor(policy); + } + cpufreq_cpu_put(policy); +} + +static void cft_remove_qos_req(void *data) +{ + struct freq_qos_request *req = data; + + if (freq_qos_request_active(req)) + freq_qos_remove_request(req); +} + +static void cft_test_verify_policy_cap_lift(struct kunit *test) +{ + unsigned int cpu = cpumask_first(cpu_online_mask); + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); + struct apple_cpu_priv *priv = cft_live_priv; + struct cft_cap_lift_ctx *ctx; + struct freq_qos_request *thermal_req, *user_req, *min_req; + struct cpufreq_policy_data data = {}; + unsigned long flags; + unsigned int freq; + int moved; + + KUNIT_ASSERT_NOT_NULL(test, policy); + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) { + cpufreq_cpu_put(policy); + KUNIT_FAIL_AND_ABORT(test, "failed to allocate test context"); + } + ctx->policy = policy; + ctx->priv = priv; + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, cft_cap_lift_cleanup, + ctx), + 0); + /* + * Stop the live governor before enabling fast_switch_enabled on the + * shared policy so scheduler utilization callbacks cannot race this + * test over cft_live_regs or priv state. + */ + scoped_guard(cpufreq_policy_write, policy) { + cpufreq_stop_governor(policy); + ctx->gov_stopped = true; + } + thermal_req = kunit_kzalloc(test, sizeof(*thermal_req), GFP_KERNEL); + user_req = kunit_kzalloc(test, sizeof(*user_req), GFP_KERNEL); + min_req = kunit_kzalloc(test, sizeof(*min_req), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, thermal_req); + KUNIT_ASSERT_NOT_NULL(test, user_req); + KUNIT_ASSERT_NOT_NULL(test, min_req); + KUNIT_EXPECT_TRUE(test, + apple_soc_cpufreq_driver.flags & CPUFREQ_NEED_UPDATE_LIMITS); + KUNIT_ASSERT_GE(test, + freq_qos_add_request(&policy->constraints, thermal_req, + FREQ_QOS_MAX, 3000000), + 0); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, cft_remove_qos_req, + thermal_req), + 0); + KUNIT_ASSERT_GE(test, + freq_qos_add_request(&policy->constraints, user_req, + FREQ_QOS_MAX, 3000000), + 0); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, cft_remove_qos_req, + user_req), + 0); + KUNIT_ASSERT_GE(test, + freq_qos_add_request(&policy->constraints, min_req, + FREQ_QOS_MIN, 600000), + 0); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, cft_remove_qos_req, + min_req), + 0); + flush_work(&policy->update); + priv->info = &soc_t6050_info; + priv->policy = policy; + priv->policy_bounds_valid = false; + priv->cap_pstate = soc_t6050_info.max_pstate; + priv->expected_pstate = 1; + priv->pending_pstate = -1; + priv->waited = true; + cft_set_cmd(cft_live_regs, 1); + + policy->min = cft_live_table[0].frequency; + policy->max = cft_live_table[CFT_NR_STATES - 1].frequency; + policy->cur = cft_live_table[0].frequency; + policy->fast_switch_enabled = true; + + /* Schedutil asks for 3 GHz (pstate 15) through the core fast-switch wrapper. */ + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 15ULL); + + /* + * Fast-die drops to 1.2 GHz and publishes its QoS ceiling, while a + * separate userspace ceiling stays at 1.8 GHz. Serialize policy->update + * then simulate the stale-policy window: a core fast-switch for 3 GHz + * stays clamped to 1.2 GHz by the live QoS aggregate. + */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1800000), 0); + KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 1200000), 0); + flush_work(&policy->update); + policy->max = 3000000; + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 4, 1200000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 4); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + + /* + * When the thermal cap lifts back to 3 GHz and apple_fast_die_work() + * updates its QoS request, re-applying lazy_cap() restores immediately + * to the 1.8 GHz userspace ceiling (pstate 7) even while policy->max + * still reads 1.2 GHz. + */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 3000000), 0); + flush_work(&policy->update); + policy->max = 1200000; + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 15, 3000000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 7); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 7ULL); + KUNIT_EXPECT_EQ(test, policy->cur, 1800000U); + + /* + * Maximum-first OPP resolution: QoS min=max=1.5 GHz resolves max down + * to 1.2 GHz (pstate 4) instead of promoting 1.2 GHz up to 1.8 GHz, + * and a ceiling below the lowest OPP clamps to the lowest OPP (600 MHz). + */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1500000), 0); + KUNIT_ASSERT_GE(test, freq_qos_update_request(min_req, 1500000), 0); + flush_work(&policy->update); + raw_spin_lock_irqsave(&priv->lock, flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 4), 4U); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 15), 4U); + raw_spin_unlock_irqrestore(&priv->lock, flags); + + KUNIT_ASSERT_GE(test, freq_qos_update_request(min_req, 600000), 0); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 400000), 0); + flush_work(&policy->update); + raw_spin_lock_irqsave(&priv->lock, flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 15), 1U); + raw_spin_unlock_irqrestore(&priv->lock, flags); + + /* Lifting the userspace ceiling restores full 3 GHz on the next fast-switch. */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + data.cpu = cpu; + data.freq_table = cft_live_table; + data.cpuinfo.min_freq = cft_live_table[0].frequency; + data.cpuinfo.max_freq = cft_live_table[CFT_NR_STATES - 1].frequency; + data.min = cft_live_table[0].frequency; + data.max = 3000000; + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify_policy(&data), 0); + policy->max = 3000000; + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 15ULL); + + /* + * Suspend must enforce the live ceiling even when suspend_freq is set + * and policy->min is still stale at 3 GHz. + */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 1200000), 0); + flush_work(&policy->update); + policy->min = 3000000; + policy->suspend_freq = 3000000; + priv->pending_pstate = 15; + priv->cap_pstate = 4; + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); +} + static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), + KUNIT_CASE(cft_test_verify_policy_cap_lift), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index beb06e86090a6..e5ba5af1b6aa2 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -220,6 +220,7 @@ struct apple_soc_cpufreq_info { }; struct apple_cpu_priv { + struct cpufreq_policy *policy; struct apple_fast_die *fast_die; struct device *cpu_dev; void __iomem *reg_base; @@ -251,6 +252,9 @@ struct apple_cpu_priv { unsigned int cap_pstate; unsigned int cap_freq; unsigned int asked_pstate; + unsigned int policy_min_freq; + unsigned int policy_max_freq; + bool policy_bounds_valid; bool waited; /* A failure the verify timer has yet to report, and its command. */ bool failure_unreported; @@ -352,6 +356,7 @@ static const struct apple_soc_cpufreq_info soc_t8132_info = { /* Command-state readback, not a measurement of the clock frequency. */ static const struct apple_soc_cpufreq_info soc_t6050_info = { .verify_transition = true, + .lazy_verify = true, .needs_firmware_thermal_control = true, .transition_timeout_us = 2000, .max_pstate = 31, @@ -425,6 +430,15 @@ static unsigned int apple_soc_cpufreq_get_rate(unsigned int cpu) priv = policy->driver_data; + if (priv->info->lazy_verify && !READ_ONCE(policy->fast_switch_enabled)) { + unsigned long flags; + + raw_spin_lock_irqsave(&priv->lock, flags); + priv->waited = true; + priv->pending_pstate = -1; + raw_spin_unlock_irqrestore(&priv->lock, flags); + } + if (priv->info->cur_pstate_mask) { u32 reg = readl_relaxed(priv->reg_base + APPLE_DVFS_STATUS); @@ -614,6 +628,95 @@ static void apple_soc_cpufreq_verify_later(struct apple_cpu_priv *priv) APPLE_DVFS_VERIFY_SLACK_NS, HRTIMER_MODE_REL_HARD); } +/* Returns the frequency-table frequency of @pstate, or 0. */ +static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, + unsigned int pstate) +{ + struct cpufreq_frequency_table *pos; + + cpufreq_for_each_valid_entry(pos, policy->freq_table) + if (pos->driver_data == pstate) + return pos->frequency; + return 0; +} + +/* + * Clamps a deferred or restored lazy-verify P-state against the current + * policy bounds and thermal ceiling using cpufreq's maximum-first OPP + * resolution. Called with priv->lock held. + */ +static unsigned int apple_soc_cpufreq_clamp_pstate(struct apple_cpu_priv *priv, + unsigned int pstate) +{ + struct cpufreq_policy *policy = priv->policy; + + if (policy && policy->freq_table) { + unsigned int max_f = priv->policy_bounds_valid ? + priv->policy_max_freq : + READ_ONCE(policy->max); + unsigned int min_f = priv->policy_bounds_valid ? + priv->policy_min_freq : + READ_ONCE(policy->min); + unsigned int cur_f = apple_soc_cpufreq_pstate_freq(policy, pstate); + unsigned int low_f = UINT_MAX, low_p = pstate; + unsigned int eff_max_f = 0, eff_max_p = pstate; + unsigned int eff_min_f = UINT_MAX, eff_min_p = pstate; + struct cpufreq_frequency_table *pos; + + if (policy->constraints.max_freq.default_value) { + s32 qos_max = freq_qos_read_value(&policy->constraints, + FREQ_QOS_MAX); + s32 qos_min = freq_qos_read_value(&policy->constraints, + FREQ_QOS_MIN); + + if (qos_max > 0) + max_f = policy->cpuinfo.max_freq ? + min_t(unsigned int, qos_max, + policy->cpuinfo.max_freq) : + (unsigned int)qos_max; + if (qos_min > 0) + min_f = max_t(unsigned int, qos_min, + policy->cpuinfo.min_freq); + } + + /* Resolve maximum first; a ceiling below all OPPs selects the lowest OPP. */ + cpufreq_for_each_valid_entry(pos, policy->freq_table) { + if (pos->frequency < low_f) { + low_f = pos->frequency; + low_p = pos->driver_data; + } + if (pos->frequency <= max_f && pos->frequency >= eff_max_f) { + eff_max_f = pos->frequency; + eff_max_p = pos->driver_data; + } + } + if (!eff_max_f && low_f != UINT_MAX) { + eff_max_f = low_f; + eff_max_p = low_p; + } + if (eff_max_f) { + min_f = min(min_f, eff_max_f); + cpufreq_for_each_valid_entry(pos, policy->freq_table) { + if (pos->frequency >= min_f && + pos->frequency <= eff_max_f && + pos->frequency <= eff_min_f) { + eff_min_f = pos->frequency; + eff_min_p = pos->driver_data; + } + } + if (eff_min_f == UINT_MAX) { + eff_min_f = eff_max_f; + eff_min_p = eff_max_p; + } + if (cur_f > eff_max_f) + pstate = eff_max_p; + else if (cur_f && cur_f < eff_min_f) + pstate = eff_min_p; + } + } + return min(pstate, priv->cap_pstate); +} + static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) { struct apple_cpu_priv *priv = container_of(timer, struct apple_cpu_priv, @@ -635,6 +738,9 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) if (reg & APPLE_DVFS_CMD_BUSY) { ktime_t now = ktime_get(); + if (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled)) + priv->pending_pstate = -1; + if (ktime_after(now, priv->verify_deadline)) apple_soc_cpufreq_fail(priv, reg); else if (priv->pending_pstate >= 0) @@ -644,11 +750,18 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) hrtimer_start(timer, priv->verify_deadline, HRTIMER_MODE_ABS_HARD); } else if (priv->pending_pstate >= 0) { - unsigned int pstate = priv->pending_pstate; - - priv->pending_pstate = -1; - if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) - apple_soc_cpufreq_verify_later(priv); + if (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled)) { + priv->pending_pstate = -1; + if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) + apple_soc_cpufreq_fail(priv, reg); + } else { + unsigned int pstate = + apple_soc_cpufreq_clamp_pstate(priv, priv->pending_pstate); + + priv->pending_pstate = -1; + if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) + apple_soc_cpufreq_verify_later(priv); + } } else if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) { apple_soc_cpufreq_fail(priv, reg); } @@ -730,9 +843,14 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy raw_spin_lock_irqsave(&priv->lock, flags); priv->waited = false; priv->asked_pstate = pstate; - if (pstate > priv->cap_pstate) { - pstate = priv->cap_pstate; - freq = priv->cap_freq; + pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); + if (pstate != priv->asked_pstate) { + unsigned int clamped_freq = apple_soc_cpufreq_pstate_freq(policy, pstate); + + if (clamped_freq) + freq = clamped_freq; + else if (pstate == priv->cap_pstate && priv->cap_freq) + freq = priv->cap_freq; } ret = apple_soc_cpufreq_lazy_set(priv, pstate); raw_spin_unlock_irqrestore(&priv->lock, flags); @@ -766,10 +884,19 @@ static int apple_soc_cpufreq_lazy_cap(struct apple_cpu_priv *priv, priv->cap_freq = freq; requested = priv->pending_pstate >= 0 ? priv->pending_pstate : priv->expected_pstate; - pstate = min(priv->asked_pstate, cap); - if (!priv->waited && pstate != requested && - !apple_soc_cpufreq_lazy_set(priv, pstate)) + pstate = apple_soc_cpufreq_clamp_pstate(priv, priv->asked_pstate); + if (!priv->waited && + (!priv->policy || READ_ONCE(priv->policy->fast_switch_enabled)) && + pstate != requested && + !apple_soc_cpufreq_lazy_set(priv, pstate)) { moved = pstate; + if (priv->policy && priv->policy->freq_table) { + unsigned int cur = apple_soc_cpufreq_pstate_freq(priv->policy, moved); + + if (cur) + WRITE_ONCE(priv->policy->cur, cur); + } + } raw_spin_unlock_irqrestore(&priv->lock, flags); return moved; } @@ -798,6 +925,8 @@ static int apple_soc_cpufreq_drain(struct apple_cpu_priv *priv) raw_spin_lock_irqsave(&priv->lock, flags); pstate = priv->pending_pstate; priv->pending_pstate = -1; + if (pstate >= 0) + pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); report = priv->failure_unreported; priv->failure_unreported = false; reg = priv->failure_command; @@ -809,6 +938,32 @@ static int apple_soc_cpufreq_drain(struct apple_cpu_priv *priv) return pstate; } +static int apple_soc_cpufreq_verify_policy(struct cpufreq_policy_data *data) +{ + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv; + int ret; + + ret = cpufreq_generic_frequency_table_verify(data); + if (ret) + return ret; + + policy = cpufreq_cpu_get_raw(data->cpu); + if (!policy) + return 0; + priv = policy->driver_data; + if (priv && priv->info->lazy_verify) { + unsigned long flags; + + raw_spin_lock_irqsave(&priv->lock, flags); + priv->policy_min_freq = data->min; + priv->policy_max_freq = data->max; + priv->policy_bounds_valid = true; + raw_spin_unlock_irqrestore(&priv->lock, flags); + } + return 0; +} + static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) { @@ -821,18 +976,6 @@ static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy, return apple_soc_cpufreq_request(policy, index); } -/* Returns the frequency-table frequency of @pstate, or 0. */ -static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, - unsigned int pstate) -{ - struct cpufreq_frequency_table *pos; - - cpufreq_for_each_valid_entry(pos, policy->freq_table) - if (pos->driver_data == pstate) - return pos->frequency; - return 0; -} - /* * On a lazy_verify SoC policy->cur is the last fast-switch request, which the * cluster may not hold: it may still be pending, be lost to a failure that @@ -862,16 +1005,40 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) if (priv->info->lazy_verify) { int pstate = apple_soc_cpufreq_drain(priv); unsigned int freq; + unsigned long flags; int ret; + if (policy->suspend_freq && policy->freq_table) { + int idx = cpufreq_table_find_index_h(policy, + policy->suspend_freq, + false); + if (idx >= 0) + pstate = policy->freq_table[idx].driver_data; + } + raw_spin_lock_irqsave(&priv->lock, flags); if (pstate < 0) pstate = priv->expected_pstate; + pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); + raw_spin_unlock_irqrestore(&priv->lock, flags); + + freq = apple_soc_cpufreq_pstate_freq(policy, pstate); + if (policy->suspend_freq && freq && freq != policy->cur) { + struct cpufreq_freqs freqs = { + .old = policy->cur, + .new = freq, + }; + + cpufreq_freq_transition_begin(policy, &freqs); + ret = apple_soc_cpufreq_set_pstate(priv, pstate); + cpufreq_freq_transition_end(policy, &freqs, ret); + return ret; + } ret = apple_soc_cpufreq_set_pstate(priv, pstate); if (ret) return ret; - freq = apple_soc_cpufreq_pstate_freq(policy, pstate); if (freq) policy->cur = freq; + return 0; } return cpufreq_generic_suspend(policy); @@ -1026,7 +1193,6 @@ static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit) struct apple_cpu_priv *priv = policy->driver_data; struct cpufreq_frequency_table *pos; unsigned int cap = 0, freq = 0; - int moved; if (!priv->info->lazy_verify) return; @@ -1041,15 +1207,10 @@ static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit) if (!freq) return; /* - * For scaling_cur_freq and the statistics until the next fast-switch - * request, which sets policy->cur itself. + * apple_soc_cpufreq_lazy_cap() updates policy->cur under priv->lock + * so a static-governor handoff cannot race with a stale cur write. */ - moved = apple_soc_cpufreq_lazy_cap(priv, cap, freq); - if (moved < 0) - return; - freq = apple_soc_cpufreq_pstate_freq(policy, moved); - if (freq) - WRITE_ONCE(policy->cur, freq); + apple_soc_cpufreq_lazy_cap(priv, cap, freq); } /* Take a sample and queue an update if the limit changes. Call with @lock held. */ @@ -1092,8 +1253,19 @@ static void apple_fast_die_work(struct work_struct *work) int ret; ret = freq_qos_update_request(&fd->req, limit); - if (ret >= 0) + if (ret >= 0) { + /* + * Re-apply once the QoS aggregate reflects the lifted limit so + * a tickless cluster restores immediately without waiting for + * policy->update or a scheduler utilization callback. Hold + * fd->lock across the limit read and cap update so a concurrent + * lower sample cannot be overwritten by a stale high limit. + */ + spin_lock_bh(&fd->lock); + apple_fast_die_cap(fd, fd->limit); + spin_unlock_bh(&fd->lock); return; + } dev_warn_ratelimited(priv->cpu_dev, "failed to update the fast die-temperature limit: %d\n", ret); @@ -1505,6 +1677,7 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy) } } + priv->policy = policy; priv->cpu_dev = cpu_dev; priv->reg_base = reg_base; priv->info = info; @@ -1613,8 +1786,10 @@ static int apple_soc_cpufreq_offline(struct cpufreq_policy *policy) static struct cpufreq_driver apple_soc_cpufreq_driver = { .name = "apple-cpufreq", .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | - CPUFREQ_NEED_INITIAL_FREQ_CHECK | CPUFREQ_IS_COOLING_DEV, - .verify = cpufreq_generic_frequency_table_verify, + CPUFREQ_NEED_INITIAL_FREQ_CHECK | + CPUFREQ_IS_COOLING_DEV | + CPUFREQ_NEED_UPDATE_LIMITS, + .verify = apple_soc_cpufreq_verify_policy, .get = apple_soc_cpufreq_get_rate, .init = apple_soc_cpufreq_init, .exit = apple_soc_cpufreq_exit, From ebb208262921f80dac7ac43592b9165907df9348 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 12:54:07 -0500 Subject: [PATCH 02/11] cpufreq: apple-soc: treat zero QoS limits as real bounds in clamp_pstate store_scaling_max_freq() and store_scaling_min_freq() pass 0 through to freq_qos_update_request(), so freq_qos_read_value() returns 0 rather than a positive frequency when a ceiling or floor is set to 0. Consume qos_max >= 0 and qos_min >= 0 in apple_soc_cpufreq_clamp_pstate() so a zero ceiling enforces the lowest valid OPP and a zero floor drops back to cpuinfo.min_freq immediately, before the asynchronous policy->update worker runs. Extend cft_test_verify_policy_cap_lift() to queue a pending fast-switch request, drop the userspace FREQ_QOS_MAX request to 0 while holding policy->rwsem so policy->update stays blocked, and verify that both timer replay and subsequent fast-switch requests clamp to the lowest valid OPP. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 38 ++++++++++++++++++++++++ drivers/cpufreq/apple-soc-cpufreq.c | 4 +-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index 267eafe8eb8cc..a6dcc87aef631 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1791,6 +1791,44 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 15ULL); + /* + * Dropping the QoS ceiling to 0 (as store_scaling_max_freq("0") does) + * must clamp a queued pending fast request to the lowest valid OPP when + * the verify timer replays it, even before policy->update reconciles + * policy->max. + */ + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + priv->expected_pstate = 4; + policy->cur = 1200000; + freq = cpufreq_driver_fast_switch(policy, 3000000); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + + scoped_guard(cpufreq_policy_write, policy) { + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 0), 0); + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); + cft_set_cmd(cft_live_regs, 4); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); + KUNIT_EXPECT_EQ(test, + FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), + 1ULL); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 600000U); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + } + flush_work(&policy->update); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + policy->max = 3000000; + /* * Suspend must enforce the live ceiling even when suspend_freq is set * and policy->min is still stale at 3 GHz. diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index e5ba5af1b6aa2..b2ab1eb950e1b 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -669,12 +669,12 @@ static unsigned int apple_soc_cpufreq_clamp_pstate(struct apple_cpu_priv *priv, s32 qos_min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); - if (qos_max > 0) + if (qos_max >= 0) max_f = policy->cpuinfo.max_freq ? min_t(unsigned int, qos_max, policy->cpuinfo.max_freq) : (unsigned int)qos_max; - if (qos_min > 0) + if (qos_min >= 0) min_f = max_t(unsigned int, qos_min, policy->cpuinfo.min_freq); } From c858fe9190fb05c94a45c9d0ae03a02f6d925578 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 13:35:43 -0500 Subject: [PATCH 03/11] cpufreq: apple-soc: reconcile accepted P-state on verify When fast-switch is enabled, schedutil's ->limits() callback only marks limits_changed instead of invoking cpufreq_policy_apply_limits(), so a QoS ceiling drop below an already-accepted high P-state leaves hardware above the ceiling until the next scheduler utilization callback runs. Extract apple_soc_cpufreq_lazy_reconcile() and call it from apple_soc_cpufreq_verify_policy() so the policy worker clamps an active or pending P-state immediately when new policy bounds tighten, even with no pending request and no scheduler callback. Narrow the comment in apple_fast_die_work() to note that immediate restore applies when asked_pstate has not been overwritten by a post-reconcile capped fast_switch(). Wire apple_soc_cpufreq_verify_policy into cft_live_driver, add cft_test_verify_policy_ceiling_drop covering high accepted P-state clamping with no pending request and no scheduler callback, and update cft_test_verify_policy_cap_lift to test against the reconciled policy->max. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 234 ++++++++++++++++------- drivers/cpufreq/apple-soc-cpufreq.c | 64 +++++-- 2 files changed, 214 insertions(+), 84 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index a6dcc87aef631..11c0cd2dbba69 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1441,7 +1441,7 @@ static struct cpufreq_driver cft_live_driver = { .name = "apple-cpufreq", .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | CPUFREQ_NEED_UPDATE_LIMITS, - .verify = cpufreq_generic_frequency_table_verify, + .verify = apple_soc_cpufreq_verify_policy, .init = cft_live_init, .exit = cft_live_exit, .target_index = cft_live_target, @@ -1628,17 +1628,12 @@ static void cft_remove_qos_req(void *data) freq_qos_remove_request(req); } -static void cft_test_verify_policy_cap_lift(struct kunit *test) +static struct cpufreq_policy *cft_live_policy_setup(struct kunit *test) { unsigned int cpu = cpumask_first(cpu_online_mask); struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); struct apple_cpu_priv *priv = cft_live_priv; struct cft_cap_lift_ctx *ctx; - struct freq_qos_request *thermal_req, *user_req, *min_req; - struct cpufreq_policy_data data = {}; - unsigned long flags; - unsigned int freq; - int moved; KUNIT_ASSERT_NOT_NULL(test, policy); ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); @@ -1661,39 +1656,29 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) cpufreq_stop_governor(policy); ctx->gov_stopped = true; } - thermal_req = kunit_kzalloc(test, sizeof(*thermal_req), GFP_KERNEL); - user_req = kunit_kzalloc(test, sizeof(*user_req), GFP_KERNEL); - min_req = kunit_kzalloc(test, sizeof(*min_req), GFP_KERNEL); - KUNIT_ASSERT_NOT_NULL(test, thermal_req); - KUNIT_ASSERT_NOT_NULL(test, user_req); - KUNIT_ASSERT_NOT_NULL(test, min_req); - KUNIT_EXPECT_TRUE(test, - apple_soc_cpufreq_driver.flags & CPUFREQ_NEED_UPDATE_LIMITS); - KUNIT_ASSERT_GE(test, - freq_qos_add_request(&policy->constraints, thermal_req, - FREQ_QOS_MAX, 3000000), - 0); - KUNIT_ASSERT_EQ(test, - kunit_add_action_or_reset(test, cft_remove_qos_req, - thermal_req), - 0); - KUNIT_ASSERT_GE(test, - freq_qos_add_request(&policy->constraints, user_req, - FREQ_QOS_MAX, 3000000), - 0); - KUNIT_ASSERT_EQ(test, - kunit_add_action_or_reset(test, cft_remove_qos_req, - user_req), - 0); - KUNIT_ASSERT_GE(test, - freq_qos_add_request(&policy->constraints, min_req, - FREQ_QOS_MIN, 600000), + return policy; +} + +static void cft_live_add_qos(struct kunit *test, struct cpufreq_policy *policy, + struct freq_qos_request **req_out, + enum freq_qos_req_type type, s32 value) +{ + struct freq_qos_request *req = kunit_kzalloc(test, sizeof(*req), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, req); + KUNIT_ASSERT_GE(test, freq_qos_add_request(&policy->constraints, req, + type, value), 0); KUNIT_ASSERT_EQ(test, - kunit_add_action_or_reset(test, cft_remove_qos_req, - min_req), + kunit_add_action_or_reset(test, cft_remove_qos_req, req), 0); - flush_work(&policy->update); + *req_out = req; +} + +static void cft_live_reset_t6050(struct cpufreq_policy *policy) +{ + struct apple_cpu_priv *priv = cft_live_priv; + priv->info = &soc_t6050_info; priv->policy = policy; priv->policy_bounds_valid = false; @@ -1707,6 +1692,80 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) policy->max = cft_live_table[CFT_NR_STATES - 1].frequency; policy->cur = cft_live_table[0].frequency; policy->fast_switch_enabled = true; +} + +/* + * A QoS ceiling drop below a high accepted P-state must clamp immediately when + * the policy worker runs, even with no pending request and no scheduler + * utilization callback. + */ +static void cft_test_verify_policy_ceiling_drop(struct kunit *test) +{ + struct cpufreq_policy *policy = cft_live_policy_setup(test); + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int freq; + + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 15U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + cft_set_cmd(cft_live_regs, 15); + + /* Lower the QoS ceiling and flush policy->update without any fast_switch(). */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1200000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->max, 1200000U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + + /* Dropping the ceiling to 0 clamps the accepted P-state to the lowest OPP. */ + cft_set_cmd(cft_live_regs, 4); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 0), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->max, 600000U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + + /* Lifting the ceiling again leaves the cluster until the governor requests higher. */ + cft_set_cmd(cft_live_regs, 1); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); +} + +static void cft_test_verify_policy_cap_lift(struct kunit *test) +{ + struct cpufreq_policy *policy = cft_live_policy_setup(test); + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *thermal_req, *user_req, *min_req; + unsigned long flags; + unsigned int freq; + int moved; + + KUNIT_EXPECT_TRUE(test, + apple_soc_cpufreq_driver.flags & CPUFREQ_NEED_UPDATE_LIMITS); + cft_live_add_qos(test, policy, &thermal_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); /* Schedutil asks for 3 GHz (pstate 15) through the core fast-switch wrapper. */ freq = cpufreq_driver_fast_switch(policy, 3000000); @@ -1716,43 +1775,89 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 15ULL); /* - * Fast-die drops to 1.2 GHz and publishes its QoS ceiling, while a - * separate userspace ceiling stays at 1.8 GHz. Serialize policy->update - * then simulate the stale-policy window: a core fast-switch for 3 GHz - * stays clamped to 1.2 GHz by the live QoS aggregate. + * Fast-die drops to 1.2 GHz while a separate userspace ceiling stays at + * 1.8 GHz. Before policy->update runs, policy->max still reads 3 GHz: + * a core fast-switch for 3 GHz is clamped to 1.2 GHz by the live QoS + * aggregate while asked_pstate stays at 15. + */ + scoped_guard(cpufreq_policy_write, policy) { + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1800000), 0); + KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 1200000), 0); + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 4, 1200000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 4); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, priv->asked_pstate, 15U); + } + flush_work(&policy->update); + KUNIT_EXPECT_EQ(test, policy->max, 1200000U); + + /* + * On a tickless cluster where no fast_switch() ran after policy->max + * reconciled, lifting the thermal cap restores immediately to the + * 1.8 GHz userspace ceiling (pstate 7) once the QoS request lifts, + * even while policy->max still reads 1.2 GHz. */ + scoped_guard(cpufreq_policy_write, policy) { + KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 3000000), 0); + KUNIT_EXPECT_EQ(test, policy->max, 1200000U); + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 15, 3000000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 7); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 7ULL); + KUNIT_EXPECT_EQ(test, policy->cur, 1800000U); + } + flush_work(&policy->update); + KUNIT_EXPECT_EQ(test, policy->max, 1800000U); + + /* + * Once policy->max has reconciled to 1.2 GHz, a core fast-switch for + * 3 GHz passes 1.2 GHz to the driver and overwrites asked_pstate to 4, + * so lifting the thermal cap stays at 1.2 GHz until schedutil runs + * after policy->update reconciles policy->max back to 1.8 GHz. + */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, priv->asked_pstate, 15U); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1800000), 0); KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 1200000), 0); flush_work(&policy->update); - policy->max = 3000000; - local_irq_save(flags); - moved = apple_soc_cpufreq_lazy_cap(priv, 4, 1200000); hrtimer_cancel(&priv->verify_timer); - local_irq_restore(flags); - KUNIT_EXPECT_EQ(test, moved, 4); + KUNIT_EXPECT_EQ(test, policy->max, 1200000U); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); freq = cpufreq_driver_fast_switch(policy, 3000000); hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, freq, 1200000U); - KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, priv->asked_pstate, 4U); - /* - * When the thermal cap lifts back to 3 GHz and apple_fast_die_work() - * updates its QoS request, re-applying lazy_cap() restores immediately - * to the 1.8 GHz userspace ceiling (pstate 7) even while policy->max - * still reads 1.2 GHz. - */ KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 3000000), 0); - flush_work(&policy->update); - policy->max = 1200000; local_irq_save(flags); moved = apple_soc_cpufreq_lazy_cap(priv, 15, 3000000); hrtimer_cancel(&priv->verify_timer); local_irq_restore(flags); - KUNIT_EXPECT_EQ(test, moved, 7); + KUNIT_EXPECT_EQ(test, moved, -1); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + flush_work(&policy->update); + KUNIT_EXPECT_EQ(test, policy->max, 1800000U); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1800000U); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 7ULL); - KUNIT_EXPECT_EQ(test, policy->cur, 1800000U); /* * Maximum-first OPP resolution: QoS min=max=1.5 GHz resolves max down @@ -1762,6 +1867,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1500000), 0); KUNIT_ASSERT_GE(test, freq_qos_update_request(min_req, 1500000), 0); flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); raw_spin_lock_irqsave(&priv->lock, flags); KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 4), 4U); KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 15), 4U); @@ -1770,6 +1876,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_ASSERT_GE(test, freq_qos_update_request(min_req, 600000), 0); KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 400000), 0); flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); raw_spin_lock_irqsave(&priv->lock, flags); KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_clamp_pstate(priv, 15), 1U); raw_spin_unlock_irqrestore(&priv->lock, flags); @@ -1777,14 +1884,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) /* Lifting the userspace ceiling restores full 3 GHz on the next fast-switch. */ KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); flush_work(&policy->update); - data.cpu = cpu; - data.freq_table = cft_live_table; - data.cpuinfo.min_freq = cft_live_table[0].frequency; - data.cpuinfo.max_freq = cft_live_table[CFT_NR_STATES - 1].frequency; - data.min = cft_live_table[0].frequency; - data.max = 3000000; - KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify_policy(&data), 0); - policy->max = 3000000; + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); freq = cpufreq_driver_fast_switch(policy, 3000000); hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, freq, 3000000U); @@ -1827,7 +1927,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) flush_work(&policy->update); KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); flush_work(&policy->update); - policy->max = 3000000; + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); /* * Suspend must enforce the live ceiling even when suspend_freq is set @@ -1835,6 +1935,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) */ KUNIT_ASSERT_GE(test, freq_qos_update_request(thermal_req, 1200000), 0); flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); policy->min = 3000000; policy->suspend_freq = 3000000; priv->pending_pstate = 15; @@ -1847,6 +1948,7 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), + KUNIT_CASE(cft_test_verify_policy_ceiling_drop), KUNIT_CASE(cft_test_verify_policy_cap_lift), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index b2ab1eb950e1b..fe1062f380d7b 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -857,6 +857,38 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy return ret ? 0 : freq; } +/* + * Reconciles a lazy_verify cluster's accepted or pending P-state toward + * @target, clamped to current policy bounds and thermal cap, and updates + * policy->cur when hardware moves. Called with priv->lock held. + * + * Returns the P-state the cluster was moved to, or -1. + */ +static int apple_soc_cpufreq_lazy_reconcile(struct apple_cpu_priv *priv, + unsigned int target) +{ + unsigned int requested, pstate; + + if (priv->waited || + (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled))) + return -1; + + requested = priv->pending_pstate >= 0 ? priv->pending_pstate : + priv->expected_pstate; + pstate = apple_soc_cpufreq_clamp_pstate(priv, target); + if (pstate == requested || apple_soc_cpufreq_lazy_set(priv, pstate)) + return -1; + + if (priv->policy && priv->policy->freq_table) { + unsigned int cur = apple_soc_cpufreq_pstate_freq(priv->policy, + pstate); + + if (cur) + WRITE_ONCE(priv->policy->cur, cur); + } + return pstate; +} + /* * Applies a new fast die-temperature limit, whose highest P-state is @cap at * @freq, to a lazy_verify cluster at once. schedutil picks up a lower limit @@ -875,28 +907,13 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy static int apple_soc_cpufreq_lazy_cap(struct apple_cpu_priv *priv, unsigned int cap, unsigned int freq) { - unsigned int requested, pstate; unsigned long flags; - int moved = -1; + int moved; raw_spin_lock_irqsave(&priv->lock, flags); priv->cap_pstate = cap; priv->cap_freq = freq; - requested = priv->pending_pstate >= 0 ? priv->pending_pstate : - priv->expected_pstate; - pstate = apple_soc_cpufreq_clamp_pstate(priv, priv->asked_pstate); - if (!priv->waited && - (!priv->policy || READ_ONCE(priv->policy->fast_switch_enabled)) && - pstate != requested && - !apple_soc_cpufreq_lazy_set(priv, pstate)) { - moved = pstate; - if (priv->policy && priv->policy->freq_table) { - unsigned int cur = apple_soc_cpufreq_pstate_freq(priv->policy, moved); - - if (cur) - WRITE_ONCE(priv->policy->cur, cur); - } - } + moved = apple_soc_cpufreq_lazy_reconcile(priv, priv->asked_pstate); raw_spin_unlock_irqrestore(&priv->lock, flags); return moved; } @@ -954,11 +971,20 @@ static int apple_soc_cpufreq_verify_policy(struct cpufreq_policy_data *data) priv = policy->driver_data; if (priv && priv->info->lazy_verify) { unsigned long flags; + unsigned int requested; raw_spin_lock_irqsave(&priv->lock, flags); priv->policy_min_freq = data->min; priv->policy_max_freq = data->max; priv->policy_bounds_valid = true; + /* + * Schedutil skips cpufreq_policy_apply_limits() when + * fast_switch_enabled is set, so reconcile the active or + * pending P-state immediately if new bounds clamp it. + */ + requested = priv->pending_pstate >= 0 ? priv->pending_pstate : + priv->expected_pstate; + apple_soc_cpufreq_lazy_reconcile(priv, requested); raw_spin_unlock_irqrestore(&priv->lock, flags); } return 0; @@ -1256,7 +1282,9 @@ static void apple_fast_die_work(struct work_struct *work) if (ret >= 0) { /* * Re-apply once the QoS aggregate reflects the lifted limit so - * a tickless cluster restores immediately without waiting for + * a tickless cluster whose last fast-switch request + * (asked_pstate) has not been overwritten by a post-reconcile + * capped fast_switch() restores immediately without waiting for * policy->update or a scheduler utilization callback. Hold * fd->lock across the limit read and cap update so a concurrent * lower sample cannot be overwritten by a stale high limit. From 0b0f4932776bdfec748d07115bd4fa7915180f8a Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 14:02:53 -0500 Subject: [PATCH 04/11] cpufreq: apple-soc: coordinate fast-switch and replay policy->cur publication When the verify timer replays a pending fast-switch request after a QoS ceiling drops, apple_soc_cpufreq_verify() commands the clamped P-state without updating policy->cur, leaving policy->cur stale even after policy->update runs when expected_pstate is already within bounds. Additionally, cpufreq_driver_fast_switch() writes policy->cur after ->fast_switch() returns, so a concurrent policy->update or lazy_cap() reconciliation running right after priv->lock is released can publish the lower frequency before cpufreq_driver_fast_switch() overwrites policy->cur with the older value. Add CPUFREQ_DRIVER_UPDATES_CUR so drivers that synchronize hardware and policy->cur under an internal lock publish policy->cur directly and cpufreq_driver_fast_switch() reads READ_ONCE(policy->cur) instead of overwriting it. Extract apple_soc_cpufreq_publish_cur(), call it from pending-request replay when the command is accepted, publish policy->cur under priv->lock in apple_soc_cpufreq_lazy_request() and in apple_soc_cpufreq_fast_switch(), and add KUnit coverage for both the zero-ceiling replay without a follow-up fast switch and the unlock-to-core reconciliation race. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 144 +++++++++++++++++++++-- drivers/cpufreq/apple-soc-cpufreq.c | 37 ++++-- drivers/cpufreq/cpufreq.c | 10 +- include/linux/cpufreq.h | 7 ++ 4 files changed, 180 insertions(+), 18 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index 11c0cd2dbba69..d4019515ea360 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1344,6 +1344,7 @@ static void cft_test_lazy_policy_limits_and_handoff(struct kunit *test) ctx->policy->cur = 1800000; KUNIT_EXPECT_TRUE(test, cft_lazy_request(ctx, 4, &freq)); KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, 15); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 3000000U); ctx->policy->fast_switch_enabled = false; cft_set_cmd(ctx->regs, 7); @@ -1354,7 +1355,7 @@ static void cft_test_lazy_policy_limits_and_handoff(struct kunit *test) KUNIT_EXPECT_FALSE(test, cft_lazy_cap(ctx, 1, 600000, &moved)); KUNIT_EXPECT_EQ(test, moved, -1); - KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 3000000U); } static struct kunit_case apple_soc_cpufreq_test_cases[] = { @@ -1437,15 +1438,32 @@ static int cft_live_target(struct cpufreq_policy *policy, unsigned int index) return 0; } +static void (*cft_live_after_fast_switch)(struct cpufreq_policy *policy); + +static unsigned int cft_live_fast_switch(struct cpufreq_policy *policy, + unsigned int target_freq) +{ + unsigned int freq = apple_soc_cpufreq_fast_switch(policy, target_freq); + + if (cft_live_after_fast_switch) { + void (*hook)(struct cpufreq_policy *policy) = cft_live_after_fast_switch; + + cft_live_after_fast_switch = NULL; + hook(policy); + } + return freq; +} + static struct cpufreq_driver cft_live_driver = { .name = "apple-cpufreq", .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | - CPUFREQ_NEED_UPDATE_LIMITS, + CPUFREQ_NEED_UPDATE_LIMITS | + CPUFREQ_DRIVER_UPDATES_CUR, .verify = apple_soc_cpufreq_verify_policy, .init = cft_live_init, .exit = cft_live_exit, .target_index = cft_live_target, - .fast_switch = apple_soc_cpufreq_fast_switch, + .fast_switch = cft_live_fast_switch, }; static int cft_live_suite_init(struct kunit_suite *suite) @@ -1489,6 +1507,7 @@ static void cft_live_test_exit(struct kunit *test) { if (cft_live_ret) return; + cft_live_after_fast_switch = NULL; cft_live_priv->info = &soc_default_info; cft_set_cmd(cft_live_regs, 4); cft_set_status(cft_live_regs, 0); @@ -1893,15 +1912,17 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) /* * Dropping the QoS ceiling to 0 (as store_scaling_max_freq("0") does) - * must clamp a queued pending fast request to the lowest valid OPP when - * the verify timer replays it, even before policy->update reconciles - * policy->max. + * must clamp a queued pending fast request to the lowest valid OPP and + * publish policy->cur when the verify timer replays it, so policy->cur + * is already 600 MHz after policy->update runs with no further + * fast_switch(). */ cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); priv->expected_pstate = 4; policy->cur = 1200000; freq = cpufreq_driver_fast_switch(policy, 3000000); KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); scoped_guard(cpufreq_policy_write, policy) { @@ -1918,13 +1939,43 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + } + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->max, 600000U); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); - freq = cpufreq_driver_fast_switch(policy, 3000000); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + KUNIT_EXPECT_EQ(test, policy->max, 3000000U); + + /* Same replay when hardware was already at P-state 1: cur must still drop. */ + cft_set_cmd(cft_live_regs, 1 | APPLE_DVFS_CMD_BUSY); + priv->expected_pstate = 1; + policy->cur = 600000; + freq = cpufreq_driver_fast_switch(policy, 3000000); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + + scoped_guard(cpufreq_policy_write, policy) { + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 0), 0); + cft_set_cmd(cft_live_regs, 1); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); hrtimer_cancel(&priv->verify_timer); - KUNIT_EXPECT_EQ(test, freq, 600000U); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); KUNIT_EXPECT_EQ(test, policy->cur, 600000U); } flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->max, 600000U); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); flush_work(&policy->update); KUNIT_EXPECT_EQ(test, policy->max, 3000000U); @@ -1945,11 +1996,88 @@ static void cft_test_verify_policy_cap_lift(struct kunit *test) KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); } +static struct { + struct kunit *test; + struct freq_qos_request *user_req; +} cft_race_ctx; + +static void cft_race_verify_after_unlock(struct cpufreq_policy *policy) +{ + struct apple_cpu_priv *priv = policy->driver_data; + + KUNIT_EXPECT_EQ(cft_race_ctx.test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(cft_race_ctx.test, priv->expected_pstate, 15U); + cft_set_cmd(cft_live_regs, 15); + KUNIT_ASSERT_GE(cft_race_ctx.test, + freq_qos_update_request(cft_race_ctx.user_req, 1200000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(cft_race_ctx.test, policy->cur, 1200000U); +} + +static void cft_race_cap_after_unlock(struct cpufreq_policy *policy) +{ + struct apple_cpu_priv *priv = policy->driver_data; + unsigned long flags; + int moved; + + KUNIT_EXPECT_EQ(cft_race_ctx.test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(cft_race_ctx.test, priv->expected_pstate, 15U); + cft_set_cmd(cft_live_regs, 15); + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 4, 1200000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(cft_race_ctx.test, moved, 4); + KUNIT_EXPECT_EQ(cft_race_ctx.test, policy->cur, 1200000U); +} + +/* + * Reconciliation (from policy->update or lazy_cap) running after ->fast_switch() + * drops priv->lock and before cpufreq_driver_fast_switch() finishes must not + * have its lower policy->cur overwritten by the stale fast-switch return value. + */ +static void cft_test_fast_switch_reconcile_race(struct kunit *test) +{ + struct cpufreq_policy *policy = cft_live_policy_setup(test); + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int freq; + + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); + + cft_race_ctx.test = test; + cft_race_ctx.user_req = user_req; + cft_live_after_fast_switch = cft_race_verify_after_unlock; + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->max, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); + + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + cft_set_cmd(cft_live_regs, 4); + cft_live_after_fast_switch = cft_race_cap_after_unlock; + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); +} + static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), KUNIT_CASE(cft_test_verify_policy_ceiling_drop), KUNIT_CASE(cft_test_verify_policy_cap_lift), + KUNIT_CASE(cft_test_fast_switch_reconcile_race), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index fe1062f380d7b..941f02b8b7f8b 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -640,6 +640,23 @@ static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, return 0; } +/* Called with priv->lock held. */ +static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, + unsigned int pstate) +{ + struct cpufreq_policy *policy = priv->policy; + unsigned int cur; + + if (!policy || !policy->freq_table) + return; + + cur = apple_soc_cpufreq_pstate_freq(policy, pstate); + if (!cur && pstate == priv->cap_pstate) + cur = priv->cap_freq; + if (cur) + WRITE_ONCE(policy->cur, cur); +} + /* * Clamps a deferred or restored lazy-verify P-state against the current * policy bounds and thermal ceiling using cpufreq's maximum-first OPP @@ -761,6 +778,8 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) priv->pending_pstate = -1; if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) apple_soc_cpufreq_verify_later(priv); + if (!priv->transition_failed) + apple_soc_cpufreq_publish_cur(priv, pstate); } } else if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) { apple_soc_cpufreq_fail(priv, reg); @@ -853,6 +872,8 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy freq = priv->cap_freq; } ret = apple_soc_cpufreq_lazy_set(priv, pstate); + if (!ret) + WRITE_ONCE(policy->cur, freq); raw_spin_unlock_irqrestore(&priv->lock, flags); return ret ? 0 : freq; } @@ -879,13 +900,7 @@ static int apple_soc_cpufreq_lazy_reconcile(struct apple_cpu_priv *priv, if (pstate == requested || apple_soc_cpufreq_lazy_set(priv, pstate)) return -1; - if (priv->policy && priv->policy->freq_table) { - unsigned int cur = apple_soc_cpufreq_pstate_freq(priv->policy, - pstate); - - if (cur) - WRITE_ONCE(priv->policy->cur, cur); - } + apple_soc_cpufreq_publish_cur(priv, pstate); return pstate; } @@ -1110,6 +1125,7 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy, * by a concurrent limits update. target_freq is already clamped. */ int idx = cpufreq_table_find_index_l(policy, target_freq, false); + unsigned int freq; if (idx < 0) return 0; @@ -1118,7 +1134,9 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy, if (apple_soc_cpufreq_request(policy, idx)) return 0; - return policy->freq_table[idx].frequency; + freq = policy->freq_table[idx].frequency; + WRITE_ONCE(policy->cur, freq); + return freq; } static int apple_soc_cpufreq_find_cluster(struct cpufreq_policy *policy, @@ -1816,7 +1834,8 @@ static struct cpufreq_driver apple_soc_cpufreq_driver = { .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | CPUFREQ_NEED_INITIAL_FREQ_CHECK | CPUFREQ_IS_COOLING_DEV | - CPUFREQ_NEED_UPDATE_LIMITS, + CPUFREQ_NEED_UPDATE_LIMITS | + CPUFREQ_DRIVER_UPDATES_CUR, .verify = apple_soc_cpufreq_verify_policy, .get = apple_soc_cpufreq_get_rate, .init = apple_soc_cpufreq_init, diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index e7cc77632e567..aefba91744057 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -2220,7 +2220,15 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, if (!freq) return 0; - policy->cur = freq; + /* + * Drivers that also update hardware from asynchronous limit or timer + * paths publish policy->cur under their own lock so a concurrent + * clamp cannot be overwritten by an older ->fast_switch() result. + */ + if (cpufreq_driver->flags & CPUFREQ_DRIVER_UPDATES_CUR) + freq = READ_ONCE(policy->cur); + else + policy->cur = freq; arch_set_freq_scale(policy->related_cpus, freq, arch_scale_freq_ref(policy->cpu)); cpufreq_stats_record_transition(policy, freq); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index aa8dadcbd10d9..080f2e7772751 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -486,6 +486,13 @@ struct cpufreq_driver { */ #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING BIT(6) +/* + * Set by drivers whose ->fast_switch() publishes policy->cur under internal + * synchronization because asynchronous limit or verification paths may + * concurrently update hardware and policy->cur. + */ +#define CPUFREQ_DRIVER_UPDATES_CUR BIT(7) + int cpufreq_register_driver(struct cpufreq_driver *driver_data); void cpufreq_unregister_driver(struct cpufreq_driver *driver_data); From 8d590f14110f42082d473ca6da940477b351703f Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 14:25:58 -0500 Subject: [PATCH 05/11] cpufreq: apple-soc: record cpufreq_stats on async clamps and fast-switch Standalone QoS ceiling drops, thermal lazy_cap() updates, and clamped pending-request replays update hardware and policy->cur under priv->lock without notifying cpufreq_stats, so stats/time_in_state keeps charging residency to the old frequency until another transition occurs. Have drivers setting CPUFREQ_DRIVER_UPDATES_CUR record cpufreq_stats under their own lock alongside policy->cur instead of calling cpufreq_stats_record_transition() after ->fast_switch() returns in cpufreq_driver_fast_switch(), preventing an older fast-switch completion from overwriting a concurrent clamp's stats. Export cpufreq_stats_record_transition() for modular builds, extract apple_soc_cpufreq_record_cur() so fast_switch, async verify replays, policy->update reconciliations, lazy_cap(), and suspend fallthrough all update policy->cur and cpufreq_stats atomically under priv->lock, enable CONFIG_CPU_FREQ_STAT in apple-soc-cpufreq.kunitconfig, and add KUnit coverage for standalone QoS/thermal/replay clamps and fast-switch races. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 297 ++++++++++++++++++ drivers/cpufreq/apple-soc-cpufreq.c | 17 +- drivers/cpufreq/apple-soc-cpufreq.kunitconfig | 1 + drivers/cpufreq/cpufreq.c | 12 +- drivers/cpufreq/cpufreq_stats.c | 1 + include/linux/cpufreq.h | 6 +- 6 files changed, 321 insertions(+), 13 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index d4019515ea360..5600f73eb6acd 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -20,7 +20,9 @@ #include #include +#include #include +#include #define CFT_REGS_SIZE 0x100 #define CFT_NR_STATES 5 @@ -1710,6 +1712,7 @@ static void cft_live_reset_t6050(struct cpufreq_policy *policy) policy->min = cft_live_table[0].frequency; policy->max = cft_live_table[CFT_NR_STATES - 1].frequency; policy->cur = cft_live_table[0].frequency; + cpufreq_stats_record_transition(policy, policy->cur); policy->fast_switch_enabled = true; } @@ -2072,12 +2075,306 @@ static void cft_test_fast_switch_reconcile_race(struct kunit *test) KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 4ULL); } +static void cft_read_stats_attr(struct kunit *test, struct cpufreq_policy *policy, + const char *name, char *buf) +{ + struct kernfs_node *stats_kn, *attr_kn; + ssize_t len; + + stats_kn = kernfs_find_and_get(policy->kobj.sd, "stats"); + KUNIT_ASSERT_NOT_NULL(test, stats_kn); + attr_kn = kernfs_find_and_get(stats_kn, name); + kernfs_put(stats_kn); + KUNIT_ASSERT_NOT_NULL(test, attr_kn); + KUNIT_ASSERT_NOT_NULL(test, policy->kobj.ktype); + KUNIT_ASSERT_NOT_NULL(test, policy->kobj.ktype->sysfs_ops); + KUNIT_ASSERT_NOT_NULL(test, policy->kobj.ktype->sysfs_ops->show); + + len = policy->kobj.ktype->sysfs_ops->show(&policy->kobj, attr_kn->priv, buf); + kernfs_put(attr_kn); + KUNIT_ASSERT_GT(test, len, 0); +} + +static unsigned int cft_stats_total_trans(struct kunit *test, + struct cpufreq_policy *policy) +{ + char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); + unsigned int total; + int ret; + + KUNIT_ASSERT_NOT_NULL(test, buf); + cft_read_stats_attr(test, policy, "total_trans", buf); + ret = kstrtouint(strim(buf), 10, &total); + KUNIT_ASSERT_EQ(test, ret, 0); + return total; +} + +static unsigned int cft_stats_trans_count(struct kunit *test, + struct cpufreq_policy *policy, + unsigned int from_freq, + unsigned int to_freq) +{ + char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); + char *p, *line; + int to_col = -1; + + KUNIT_ASSERT_NOT_NULL(test, buf); + cft_read_stats_attr(test, policy, "trans_table", buf); + + p = buf; + while ((line = strsep(&p, "\n")) != NULL) { + char *colon = strchr(line, ':'); + char *s; + unsigned int row_freq; + int col = 0; + + if (!colon) + continue; + *colon = '\0'; + s = skip_spaces(line); + if (!*s) { + s = colon + 1; + while (*(s = skip_spaces(s))) { + char *tok = strsep(&s, " "); + unsigned int col_freq; + int ret; + + ret = kstrtouint(tok, 10, &col_freq); + KUNIT_ASSERT_EQ(test, ret, 0); + if (col_freq == to_freq) + to_col = col; + col++; + if (!s) + break; + } + continue; + } + + if (kstrtouint(strim(s), 10, &row_freq) || row_freq != from_freq) + continue; + + KUNIT_ASSERT_GE(test, to_col, 0); + s = colon + 1; + while (*(s = skip_spaces(s))) { + char *tok = strsep(&s, " "); + unsigned int count; + int ret; + + ret = kstrtouint(tok, 10, &count); + KUNIT_ASSERT_EQ(test, ret, 0); + if (col == to_col) + return count; + col++; + if (!s) + break; + } + KUNIT_FAIL(test, "missing column %d for %u->%u", to_col, + from_freq, to_freq); + return 0; + } + + KUNIT_FAIL(test, "missing row %u in trans_table", from_freq); + return 0; +} + +static u64 cft_stats_time_in_state(struct kunit *test, + struct cpufreq_policy *policy, + unsigned int freq) +{ + char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); + char *p, *line; + + KUNIT_ASSERT_NOT_NULL(test, buf); + cft_read_stats_attr(test, policy, "time_in_state", buf); + + p = buf; + while ((line = strsep(&p, "\n")) != NULL) { + unsigned int row_freq; + u64 ticks; + + if (!*line) + continue; + if (sscanf(line, "%u %llu", &row_freq, &ticks) == 2 && + row_freq == freq) + return ticks; + } + + KUNIT_FAIL(test, "missing %u in time_in_state", freq); + return 0; +} + +/* + * Standalone QoS clamps, pending-request replays, and lazy_cap() updates must + * record cpufreq_stats transitions and residency immediately, and an older + * fast_switch() completing after a concurrent clamp must not overwrite stats. + */ +static void cft_test_stats_clamp_and_race(struct kunit *test) +{ + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int total, c_600_3000, c_3000_1200, c_3000_600, c_1200_3000; + unsigned long flags; + unsigned int freq; + u64 t3000_before, t3000_after, t1200_before, t1200_after; + int moved; + + if (!IS_ENABLED(CONFIG_CPU_FREQ_STAT)) + kunit_skip(test, "CONFIG_CPU_FREQ_STAT is disabled"); + + policy = cft_live_policy_setup(test); + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); + + /* Standalone QoS ceiling drop (3 GHz -> 1.2 GHz) updates stats and residency. */ + total = cft_stats_total_trans(test, policy); + c_600_3000 = cft_stats_trans_count(test, policy, 600000, 3000000); + c_3000_1200 = cft_stats_trans_count(test, policy, 3000000, 1200000); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), + c_600_3000 + 1); + cft_set_cmd(cft_live_regs, 15); + + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1200000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), + c_3000_1200 + 1); + + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + msleep(25); + t3000_after = cft_stats_time_in_state(test, policy, 3000000); + t1200_after = cft_stats_time_in_state(test, policy, 1200000); + KUNIT_EXPECT_EQ(test, t3000_after, t3000_before); + KUNIT_EXPECT_GT(test, t1200_after, t1200_before); + + /* Pending-request replay clamped to 600 MHz records 3 GHz -> 600 MHz. */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + priv->expected_pstate = 4; + total = cft_stats_total_trans(test, policy); + c_3000_600 = cft_stats_trans_count(test, policy, 3000000, 600000); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + + scoped_guard(cpufreq_policy_write, policy) { + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 0), 0); + cft_set_cmd(cft_live_regs, 4); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + } + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 600000), + c_3000_600 + 1); + + /* Standalone lazy_cap() clamp (3 GHz -> 1.2 GHz) records transition. */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + cft_set_cmd(cft_live_regs, 15); + total = cft_stats_total_trans(test, policy); + c_3000_1200 = cft_stats_trans_count(test, policy, 3000000, 1200000); + + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 4, 1200000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 4); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), + c_3000_1200 + 1); + priv->cap_pstate = soc_t6050_info.max_pstate; + + /* + * Concurrent policy->update or lazy_cap() clamp after priv->lock unlock + * must not have its stats overwritten by the older fast_switch(). + */ + freq = cpufreq_driver_fast_switch(policy, 600000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 600000U); + cft_set_cmd(cft_live_regs, 1); + + total = cft_stats_total_trans(test, policy); + c_600_3000 = cft_stats_trans_count(test, policy, 600000, 3000000); + c_3000_1200 = cft_stats_trans_count(test, policy, 3000000, 1200000); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + + cft_race_ctx.test = test; + cft_race_ctx.user_req = user_req; + cft_live_after_fast_switch = cft_race_verify_after_unlock; + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), + c_600_3000 + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), + c_3000_1200 + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 3000000), + t3000_before); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + freq = cpufreq_driver_fast_switch(policy, 600000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 600000U); + cft_set_cmd(cft_live_regs, 1); + + total = cft_stats_total_trans(test, policy); + c_600_3000 = cft_stats_trans_count(test, policy, 600000, 3000000); + c_3000_1200 = cft_stats_trans_count(test, policy, 3000000, 1200000); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + + cft_live_after_fast_switch = cft_race_cap_after_unlock; + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), + c_600_3000 + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), + c_3000_1200 + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); +} + static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), KUNIT_CASE(cft_test_verify_policy_ceiling_drop), KUNIT_CASE(cft_test_verify_policy_cap_lift), KUNIT_CASE(cft_test_fast_switch_reconcile_race), + KUNIT_CASE(cft_test_stats_clamp_and_race), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 941f02b8b7f8b..ffa7f8ee44340 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -640,6 +640,13 @@ static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, return 0; } +static void apple_soc_cpufreq_record_cur(struct cpufreq_policy *policy, + unsigned int cur) +{ + WRITE_ONCE(policy->cur, cur); + cpufreq_stats_record_transition(policy, cur); +} + /* Called with priv->lock held. */ static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, unsigned int pstate) @@ -654,7 +661,7 @@ static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, if (!cur && pstate == priv->cap_pstate) cur = priv->cap_freq; if (cur) - WRITE_ONCE(policy->cur, cur); + apple_soc_cpufreq_record_cur(policy, cur); } /* @@ -873,7 +880,7 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy } ret = apple_soc_cpufreq_lazy_set(priv, pstate); if (!ret) - WRITE_ONCE(policy->cur, freq); + apple_soc_cpufreq_record_cur(policy, freq); raw_spin_unlock_irqrestore(&priv->lock, flags); return ret ? 0 : freq; } @@ -881,7 +888,7 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy /* * Reconciles a lazy_verify cluster's accepted or pending P-state toward * @target, clamped to current policy bounds and thermal cap, and updates - * policy->cur when hardware moves. Called with priv->lock held. + * policy->cur and cpufreq_stats when hardware moves. Called with priv->lock held. * * Returns the P-state the cluster was moved to, or -1. */ @@ -1078,7 +1085,7 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) if (ret) return ret; if (freq) - policy->cur = freq; + apple_soc_cpufreq_record_cur(policy, freq); return 0; } @@ -1135,7 +1142,7 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy, return 0; freq = policy->freq_table[idx].frequency; - WRITE_ONCE(policy->cur, freq); + apple_soc_cpufreq_record_cur(policy, freq); return freq; } diff --git a/drivers/cpufreq/apple-soc-cpufreq.kunitconfig b/drivers/cpufreq/apple-soc-cpufreq.kunitconfig index 68ca699bc91a4..b264e6300e154 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.kunitconfig +++ b/drivers/cpufreq/apple-soc-cpufreq.kunitconfig @@ -5,5 +5,6 @@ CONFIG_OF=y CONFIG_PM=y CONFIG_SUSPEND=y CONFIG_CPU_FREQ=y +CONFIG_CPU_FREQ_STAT=y CONFIG_ARM_APPLE_SOC_CPUFREQ=y CONFIG_ARM_APPLE_SOC_CPUFREQ_KUNIT_TEST=y diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index aefba91744057..a8545530959b3 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -2222,16 +2222,18 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, /* * Drivers that also update hardware from asynchronous limit or timer - * paths publish policy->cur under their own lock so a concurrent - * clamp cannot be overwritten by an older ->fast_switch() result. + * paths publish policy->cur and record cpufreq stats under their own + * lock so a concurrent clamp cannot overwrite policy->cur or stats + * with an older ->fast_switch() result. */ - if (cpufreq_driver->flags & CPUFREQ_DRIVER_UPDATES_CUR) + if (cpufreq_driver->flags & CPUFREQ_DRIVER_UPDATES_CUR) { freq = READ_ONCE(policy->cur); - else + } else { policy->cur = freq; + cpufreq_stats_record_transition(policy, freq); + } arch_set_freq_scale(policy->related_cpus, freq, arch_scale_freq_ref(policy->cpu)); - cpufreq_stats_record_transition(policy, freq); if (trace_cpu_frequency_enabled()) { for_each_cpu(cpu, policy->cpus) diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c index 670a10c3ecfad..8ec9bf2045435 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -287,3 +287,4 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy, stats->trans_table[old_index * stats->max_state + new_index]++; stats->total_trans++; } +EXPORT_SYMBOL_GPL(cpufreq_stats_record_transition); diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 080f2e7772751..2fe8a1e3b270a 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -487,9 +487,9 @@ struct cpufreq_driver { #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING BIT(6) /* - * Set by drivers whose ->fast_switch() publishes policy->cur under internal - * synchronization because asynchronous limit or verification paths may - * concurrently update hardware and policy->cur. + * Set by drivers whose ->fast_switch() publishes policy->cur and records + * cpufreq stats under internal synchronization because asynchronous limit or + * verification paths may concurrently update hardware and policy->cur. */ #define CPUFREQ_DRIVER_UPDATES_CUR BIT(7) From 5c269a610a54c30f9e109e6acaedac6236f368e2 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 14:47:31 -0500 Subject: [PATCH 06/11] cpufreq: apple-soc: record cpufreq_stats only on accepted transitions When DVFS is busy, apple_soc_cpufreq_lazy_set() queues pending_pstate without writing hardware or updating expected_pstate. Recording cpufreq_stats alongside optimistic policy->cur publication in apple_soc_cpufreq_lazy_request() and apple_soc_cpufreq_lazy_reconcile() caused stats/time_in_state to charge residency to a queued frequency that hardware never entered if the request was later clamped or dropped at the verify deadline. Separate cpufreq_stats_record_transition() from optimistic policy->cur updates so lazy-path requests and reconciliations only record stats when hardware accepts the P-state (an immediate non-busy write, an accepted timer replay, or an idle cluster whose expected_pstate already matches), while busy-queued requests update policy->cur only. Add KUnit coverage for busy-then-discarded requests and accepted timer replays. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 193 ++++++++++++++++++++++- drivers/cpufreq/apple-soc-cpufreq.c | 43 +++-- 2 files changed, 215 insertions(+), 21 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index 5600f73eb6acd..a25f203f24cac 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1511,6 +1511,8 @@ static void cft_live_test_exit(struct kunit *test) return; cft_live_after_fast_switch = NULL; cft_live_priv->info = &soc_default_info; + cft_live_priv->transition_failed = false; + cft_live_priv->failure_unreported = false; cft_set_cmd(cft_live_regs, 4); cft_set_status(cft_live_regs, 0); } @@ -1706,6 +1708,8 @@ static void cft_live_reset_t6050(struct cpufreq_policy *policy) priv->cap_pstate = soc_t6050_info.max_pstate; priv->expected_pstate = 1; priv->pending_pstate = -1; + priv->transition_failed = false; + priv->failure_unreported = false; priv->waited = true; cft_set_cmd(cft_live_regs, 1); @@ -2213,7 +2217,8 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) struct cpufreq_policy *policy; struct apple_cpu_priv *priv = cft_live_priv; struct freq_qos_request *user_req, *min_req; - unsigned int total, c_600_3000, c_3000_1200, c_3000_600, c_1200_3000; + unsigned int total, c_600_3000, c_3000_1200, c_1200_3000, c_1200_600; + unsigned int c_3000_600; unsigned long flags; unsigned int freq; u64 t3000_before, t3000_after, t1200_before, t1200_after; @@ -2257,17 +2262,26 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) KUNIT_EXPECT_EQ(test, t3000_after, t3000_before); KUNIT_EXPECT_GT(test, t1200_after, t1200_before); - /* Pending-request replay clamped to 600 MHz records 3 GHz -> 600 MHz. */ + /* + * Busy request defers stats until hardware accepts the clamped replay: + * queueing 3 GHz behind busy 1.2 GHz leaves stats on 1.2 GHz, and + * clamped replay to 600 MHz records 1.2 GHz -> 600 MHz. + */ KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); flush_work(&policy->update); cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); priv->expected_pstate = 4; total = cft_stats_total_trans(test, policy); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + c_1200_600 = cft_stats_trans_count(test, policy, 1200000, 600000); c_3000_600 = cft_stats_trans_count(test, policy, 3000000, 600000); freq = cpufreq_driver_fast_switch(policy, 3000000); KUNIT_EXPECT_EQ(test, freq, 3000000U); - KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); scoped_guard(cpufreq_policy_write, policy) { KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 0), 0); @@ -2281,9 +2295,11 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) } flush_work(&policy->update); hrtimer_cancel(&priv->verify_timer); - KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), + c_1200_600 + 1); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 600000), - c_3000_600 + 1); + c_3000_600); /* Standalone lazy_cap() clamp (3 GHz -> 1.2 GHz) records transition. */ KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); @@ -2368,6 +2384,172 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) c_1200_3000); } +/* + * Busy DVFS during fast_switch(), policy->update, or lazy_cap() must not + * record cpufreq_stats until hardware accepts the P-state; if busy persists + * past the verify deadline and drops the pending request, stats must remain on + * the frequency hardware actually entered. + */ +static void cft_test_stats_busy_then_discard(struct kunit *test) +{ + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int total, c_1200_3000, c_1200_1800, c_1200_600; + u64 t1200_before, t1800_before, t3000_before, t600_before; + unsigned long flags; + unsigned int freq; + int moved; + + if (!IS_ENABLED(CONFIG_CPU_FREQ_STAT)) + kunit_skip(test, "CONFIG_CPU_FREQ_STAT is disabled"); + + policy = cft_live_policy_setup(test); + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); + + /* Put hardware cleanly at 1.2 GHz (pstate 4). */ + freq = cpufreq_driver_fast_switch(policy, 1200000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + cft_set_cmd(cft_live_regs, 4); + + /* + * Busy during fast_switch() and policy->update clamp, then deadline + * expiry drops the queued request: stats stay on 1.2 GHz. + */ + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + total = cft_stats_total_trans(test, policy); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + c_1200_1800 = cft_stats_trans_count(test, policy, 1200000, 1800000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + t1800_before = cft_stats_time_in_state(test, policy, 1800000); + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1800000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 7); + KUNIT_EXPECT_EQ(test, policy->cur, 1800000U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 1800000), + c_1200_1800); + + priv->verify_deadline = ktime_sub_us(ktime_get(), 1); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 1800000), + c_1200_1800); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 1800000), + t1800_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 3000000), + t3000_before); + + /* + * Busy during lazy_cap(), then deadline expiry drops the pending cap: + * stats also stay on 1.2 GHz and never charge 600 MHz. + */ + cft_live_reset_t6050(policy); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + freq = cpufreq_driver_fast_switch(policy, 1200000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + + total = cft_stats_total_trans(test, policy); + c_1200_600 = cft_stats_trans_count(test, policy, 1200000, 600000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + t600_before = cft_stats_time_in_state(test, policy, 600000); + + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 1, 600000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, 1); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 1); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), + c_1200_600); + + priv->verify_deadline = ktime_sub_us(ktime_get(), 1); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), + c_1200_600); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 600000), + t600_before); + + /* + * When DVFS clears BUSY and the timer replay writes 3 GHz, stats + * record 1.2 GHz -> 3 GHz and start charging 3 GHz residency then. + */ + cft_live_reset_t6050(policy); + freq = cpufreq_driver_fast_switch(policy, 1200000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + + total = cft_stats_total_trans(test, policy); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + + cft_set_cmd(cft_live_regs, 4); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 15U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000 + 1); + + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 3000000), + t3000_before); +} + static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), @@ -2375,6 +2557,7 @@ static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE(cft_test_verify_policy_cap_lift), KUNIT_CASE(cft_test_fast_switch_reconcile_race), KUNIT_CASE(cft_test_stats_clamp_and_race), + KUNIT_CASE(cft_test_stats_busy_then_discard), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index ffa7f8ee44340..dcec75e73d9e7 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -641,15 +641,17 @@ static unsigned int apple_soc_cpufreq_pstate_freq(struct cpufreq_policy *policy, } static void apple_soc_cpufreq_record_cur(struct cpufreq_policy *policy, - unsigned int cur) + unsigned int cur, bool record_stats) { WRITE_ONCE(policy->cur, cur); - cpufreq_stats_record_transition(policy, cur); + if (record_stats) + cpufreq_stats_record_transition(policy, cur); } /* Called with priv->lock held. */ static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, - unsigned int pstate) + unsigned int pstate, + bool record_stats) { struct cpufreq_policy *policy = priv->policy; unsigned int cur; @@ -661,7 +663,7 @@ static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, if (!cur && pstate == priv->cap_pstate) cur = priv->cap_freq; if (cur) - apple_soc_cpufreq_record_cur(policy, cur); + apple_soc_cpufreq_record_cur(policy, cur, record_stats); } /* @@ -786,7 +788,7 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) apple_soc_cpufreq_verify_later(priv); if (!priv->transition_failed) - apple_soc_cpufreq_publish_cur(priv, pstate); + apple_soc_cpufreq_publish_cur(priv, pstate, true); } } else if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) { apple_soc_cpufreq_fail(priv, reg); @@ -805,14 +807,18 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) } /* - * Requests @pstate without waiting, see "Lazy verification" above. - * Called with priv->lock held. + * Requests @pstate without waiting, see "Lazy verification" above. Sets + * *@accepted to true when hardware accepted @pstate (written immediately or + * already matching expected_pstate), and false when queued behind busy DVFS + * or on error. Called with priv->lock held. */ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv, - unsigned int pstate) + unsigned int pstate, bool *accepted) { u64 reg; + *accepted = false; + if (priv->transition_failed) return -EIO; @@ -845,6 +851,7 @@ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv, hrtimer_start(&priv->verify_timer, 0, HRTIMER_MODE_REL_HARD); return -EIO; } + *accepted = true; return 0; } @@ -861,6 +868,7 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy unsigned int pstate = policy->freq_table[index].driver_data; unsigned int freq = policy->freq_table[index].frequency; unsigned long flags; + bool accepted; int ret; if (pstate < priv->info->min_pstate || pstate > priv->info->max_pstate) @@ -878,17 +886,18 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy else if (pstate == priv->cap_pstate && priv->cap_freq) freq = priv->cap_freq; } - ret = apple_soc_cpufreq_lazy_set(priv, pstate); + ret = apple_soc_cpufreq_lazy_set(priv, pstate, &accepted); if (!ret) - apple_soc_cpufreq_record_cur(policy, freq); + apple_soc_cpufreq_record_cur(policy, freq, accepted); raw_spin_unlock_irqrestore(&priv->lock, flags); return ret ? 0 : freq; } /* * Reconciles a lazy_verify cluster's accepted or pending P-state toward - * @target, clamped to current policy bounds and thermal cap, and updates - * policy->cur and cpufreq_stats when hardware moves. Called with priv->lock held. + * @target, clamped to current policy bounds and thermal cap, updates + * policy->cur, and records cpufreq_stats when hardware accepts the P-state. + * Called with priv->lock held. * * Returns the P-state the cluster was moved to, or -1. */ @@ -896,6 +905,7 @@ static int apple_soc_cpufreq_lazy_reconcile(struct apple_cpu_priv *priv, unsigned int target) { unsigned int requested, pstate; + bool accepted; if (priv->waited || (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled))) @@ -904,10 +914,11 @@ static int apple_soc_cpufreq_lazy_reconcile(struct apple_cpu_priv *priv, requested = priv->pending_pstate >= 0 ? priv->pending_pstate : priv->expected_pstate; pstate = apple_soc_cpufreq_clamp_pstate(priv, target); - if (pstate == requested || apple_soc_cpufreq_lazy_set(priv, pstate)) + if (pstate == requested || + apple_soc_cpufreq_lazy_set(priv, pstate, &accepted)) return -1; - apple_soc_cpufreq_publish_cur(priv, pstate); + apple_soc_cpufreq_publish_cur(priv, pstate, accepted); return pstate; } @@ -1085,7 +1096,7 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) if (ret) return ret; if (freq) - apple_soc_cpufreq_record_cur(policy, freq); + apple_soc_cpufreq_record_cur(policy, freq, true); return 0; } @@ -1142,7 +1153,7 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy, return 0; freq = policy->freq_table[idx].frequency; - apple_soc_cpufreq_record_cur(policy, freq); + apple_soc_cpufreq_record_cur(policy, freq, true); return freq; } From 8cdb8aea68646a9a3568f0373ad2de72c3a6ef0d Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 15:07:18 -0500 Subject: [PATCH 07/11] cpufreq: apple-soc: keep accepted suspend accounting and update freq scale When a fast-switch request is queued behind busy DVFS, policy->cur holds the optimistic queued frequency while hardware and cpufreq_stats remain on the last accepted P-state. Dropping that request on drain or timer failure left policy->cur optimistic, and calling cpufreq_freq_transition_begin()/end() across a failing suspend wrote the unissued suspend target and rolled cpufreq_stats back to the discarded queued frequency. Restore policy->cur to expected_pstate when dropping a queued request on drain or failure, and settle lazy-verify suspend via apple_soc_cpufreq_set_pstate() before calling apple_soc_cpufreq_record_cur() so a failed suspend keeps policy->cur and cpufreq_stats strictly on the last accepted hardware state. Also update arch_set_freq_scale() from apple_soc_cpufreq_record_cur() on accepted transitions (exporting topology_set_freq_scale() for modular builds) and move the core arch_set_freq_scale() call under the !CPUFREQ_DRIVER_UPDATES_CUR branch in cpufreq_driver_fast_switch() so standalone clamps and replays update scheduler frequency invariance immediately without being overwritten by a concurrent fast-switch completion. Add KUnit coverage for failed suspend with a queued request and frequency-scale updates across clamps, replays, and races. Signed-off-by: Erik Bourget --- drivers/base/arch_topology.c | 1 + drivers/cpufreq/apple-soc-cpufreq-test.c | 144 ++++++++++++++++++++++- drivers/cpufreq/apple-soc-cpufreq.c | 48 +++++--- drivers/cpufreq/cpufreq.c | 9 +- include/linux/cpufreq.h | 7 +- 5 files changed, 183 insertions(+), 26 deletions(-) diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index 92966be712d56..37b07c0a72912 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -160,6 +160,7 @@ void topology_set_freq_scale(const struct cpumask *cpus, unsigned long cur_freq, for_each_cpu(i, cpus) per_cpu(arch_freq_scale, i) = scale; } +EXPORT_SYMBOL_GPL(topology_set_freq_scale); DEFINE_PER_CPU(unsigned long, hw_pressure); diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index a25f203f24cac..1e284b3dfc0ca 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1701,6 +1701,8 @@ static void cft_live_add_qos(struct kunit *test, struct cpufreq_policy *policy, static void cft_live_reset_t6050(struct cpufreq_policy *policy) { struct apple_cpu_priv *priv = cft_live_priv; + unsigned long max_freq = arch_scale_freq_ref(policy->cpu) ?: + policy->cpuinfo.max_freq; priv->info = &soc_t6050_info; priv->policy = policy; @@ -1717,6 +1719,9 @@ static void cft_live_reset_t6050(struct cpufreq_policy *policy) policy->max = cft_live_table[CFT_NR_STATES - 1].frequency; policy->cur = cft_live_table[0].frequency; cpufreq_stats_record_transition(policy, policy->cur); + if (max_freq && cpumask_available(policy->related_cpus) && + !cpumask_empty(policy->related_cpus)) + arch_set_freq_scale(policy->related_cpus, policy->cur, max_freq); policy->fast_switch_enabled = true; } @@ -2207,10 +2212,27 @@ static u64 cft_stats_time_in_state(struct kunit *test, return 0; } +static void cft_expect_freq_scale(struct kunit *test, + struct cpufreq_policy *policy, + unsigned int freq) +{ +#ifdef arch_scale_freq_capacity + unsigned long max_freq = arch_scale_freq_ref(policy->cpu) ?: + policy->cpuinfo.max_freq; + + if (max_freq) + KUNIT_EXPECT_EQ(test, + arch_scale_freq_capacity(policy->cpu), + ((unsigned long)freq << SCHED_CAPACITY_SHIFT) / + max_freq); +#endif +} + /* * Standalone QoS clamps, pending-request replays, and lazy_cap() updates must - * record cpufreq_stats transitions and residency immediately, and an older - * fast_switch() completing after a concurrent clamp must not overwrite stats. + * record cpufreq_stats transitions, residency, and frequency-invariance scale + * immediately, and an older fast_switch() completing after a concurrent clamp + * must not overwrite stats or scheduler freq scale. */ static void cft_test_stats_clamp_and_race(struct kunit *test) { @@ -2241,6 +2263,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) freq = cpufreq_driver_fast_switch(policy, 3000000); hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, freq, 3000000U); + cft_expect_freq_scale(test, policy, 3000000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), c_600_3000 + 1); @@ -2250,6 +2273,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) flush_work(&policy->update); hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), c_3000_1200 + 1); @@ -2279,6 +2303,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) freq = cpufreq_driver_fast_switch(policy, 3000000); KUNIT_EXPECT_EQ(test, freq, 3000000U); KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + cft_expect_freq_scale(test, policy, 1200000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), c_1200_3000); @@ -2292,6 +2317,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) hrtimer_cancel(&priv->verify_timer); local_irq_restore(flags); KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + cft_expect_freq_scale(test, policy, 600000); } flush_work(&policy->update); hrtimer_cancel(&priv->verify_timer); @@ -2316,6 +2342,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) hrtimer_cancel(&priv->verify_timer); local_irq_restore(flags); KUNIT_EXPECT_EQ(test, moved, 4); + cft_expect_freq_scale(test, policy, 1200000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 1200000), c_3000_1200 + 1); @@ -2342,6 +2369,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, freq, 1200000U); KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), c_600_3000 + 1); @@ -2375,6 +2403,7 @@ static void cft_test_stats_clamp_and_race(struct kunit *test) hrtimer_cancel(&priv->verify_timer); KUNIT_EXPECT_EQ(test, freq, 1200000U); KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 2); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), c_600_3000 + 1); @@ -2548,6 +2577,116 @@ static void cft_test_stats_busy_then_discard(struct kunit *test) t1200_before); KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 3000000), t3000_before); + cft_expect_freq_scale(test, policy, 3000000); +} + +/* + * Queueing a fast-switch request behind BUSY and then hitting a failed suspend + * toward suspend_freq must restore policy->cur to the accepted hardware state + * and keep cpufreq_stats residency/transitions strictly on that state, never + * charging the discarded queued request or a spurious suspend target. + */ +static void cft_test_stats_busy_suspend_fail(struct kunit *test) +{ + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int total, c_1200_3000, c_1200_600, c_600_3000, c_600_1200; + unsigned int c_3000_600; + u64 t1200_before, t3000_before, t600_before; + unsigned int freq; + + if (!IS_ENABLED(CONFIG_CPU_FREQ_STAT)) + kunit_skip(test, "CONFIG_CPU_FREQ_STAT is disabled"); + + policy = cft_live_policy_setup(test); + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + cft_live_reset_t6050(policy); + + /* Put hardware cleanly at 1.2 GHz (pstate 4). */ + freq = cpufreq_driver_fast_switch(policy, 1200000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + cft_set_cmd(cft_live_regs, 4); + + total = cft_stats_total_trans(test, policy); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + c_1200_600 = cft_stats_trans_count(test, policy, 1200000, 600000); + c_600_3000 = cft_stats_trans_count(test, policy, 600000, 3000000); + c_600_1200 = cft_stats_trans_count(test, policy, 600000, 1200000); + c_3000_600 = cft_stats_trans_count(test, policy, 3000000, 600000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + t600_before = cft_stats_time_in_state(test, policy, 600000); + + /* Queue 3 GHz behind BUSY; policy->cur is optimistic while stats stay at 1.2 GHz. */ + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + cft_expect_freq_scale(test, policy, 1200000); + + /* + * Suspend toward 600 MHz while BUSY persists: suspend fails without + * charging 3 GHz or 600 MHz. + */ + policy->suspend_freq = 600000; + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), -EIO); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), + c_1200_600); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 3000000), + c_600_3000); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 600000, 1200000), + c_600_1200); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 600000), + c_3000_600); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 3000000), + t3000_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 600000), + t600_before); + + /* + * When DVFS clears BUSY before suspend runs, suspend drops the queued + * 3 GHz request and transitions directly from 1.2 GHz to 600 MHz. + */ + priv->transition_failed = false; + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + + cft_set_cmd(cft_live_regs, 4); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); + KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); + KUNIT_EXPECT_EQ(test, policy->cur, 600000U); + cft_expect_freq_scale(test, policy, 600000); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), + c_1200_600 + 1); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 600000), + c_3000_600); } static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { @@ -2558,6 +2697,7 @@ static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE(cft_test_fast_switch_reconcile_race), KUNIT_CASE(cft_test_stats_clamp_and_race), KUNIT_CASE(cft_test_stats_busy_then_discard), + KUNIT_CASE(cft_test_stats_busy_suspend_fail), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index dcec75e73d9e7..6bbcc9c62a792 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -575,8 +576,14 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy, * * Called with priv->lock held. */ +static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, + unsigned int pstate, + bool record_stats); + static void apple_soc_cpufreq_fail(struct apple_cpu_priv *priv, u64 reg) { + if (priv->pending_pstate >= 0) + apple_soc_cpufreq_publish_cur(priv, priv->expected_pstate, false); priv->transition_failed = true; priv->pending_pstate = -1; /* The scheduler may hold a runqueue lock: the timer reports it. */ @@ -644,8 +651,15 @@ static void apple_soc_cpufreq_record_cur(struct cpufreq_policy *policy, unsigned int cur, bool record_stats) { WRITE_ONCE(policy->cur, cur); - if (record_stats) + if (record_stats) { + unsigned long max_freq = arch_scale_freq_ref(policy->cpu) ?: + policy->cpuinfo.max_freq; + cpufreq_stats_record_transition(policy, cur); + if (max_freq && cpumask_available(policy->related_cpus) && + !cpumask_empty(policy->related_cpus)) + arch_set_freq_scale(policy->related_cpus, cur, max_freq); + } } /* Called with priv->lock held. */ @@ -784,11 +798,12 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) unsigned int pstate = apple_soc_cpufreq_clamp_pstate(priv, priv->pending_pstate); - priv->pending_pstate = -1; if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) apple_soc_cpufreq_verify_later(priv); - if (!priv->transition_failed) + if (!priv->transition_failed) { + priv->pending_pstate = -1; apple_soc_cpufreq_publish_cur(priv, pstate, true); + } } } else if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) { apple_soc_cpufreq_fail(priv, reg); @@ -975,8 +990,10 @@ static int apple_soc_cpufreq_drain(struct apple_cpu_priv *priv) raw_spin_lock_irqsave(&priv->lock, flags); pstate = priv->pending_pstate; priv->pending_pstate = -1; - if (pstate >= 0) + if (pstate >= 0) { + apple_soc_cpufreq_publish_cur(priv, priv->expected_pstate, false); pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); + } report = priv->failure_unreported; priv->failure_unreported = false; reg = priv->failure_command; @@ -1080,21 +1097,18 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); raw_spin_unlock_irqrestore(&priv->lock, flags); - freq = apple_soc_cpufreq_pstate_freq(policy, pstate); - if (policy->suspend_freq && freq && freq != policy->cur) { - struct cpufreq_freqs freqs = { - .old = policy->cur, - .new = freq, - }; - - cpufreq_freq_transition_begin(policy, &freqs); - ret = apple_soc_cpufreq_set_pstate(priv, pstate); - cpufreq_freq_transition_end(policy, &freqs, ret); - return ret; - } ret = apple_soc_cpufreq_set_pstate(priv, pstate); - if (ret) + if (ret) { + if (policy->suspend_freq) { + raw_spin_lock_irqsave(&priv->lock, flags); + apple_soc_cpufreq_publish_cur(priv, + priv->expected_pstate, + false); + raw_spin_unlock_irqrestore(&priv->lock, flags); + } return ret; + } + freq = apple_soc_cpufreq_pstate_freq(policy, pstate); if (freq) apple_soc_cpufreq_record_cur(policy, freq, true); return 0; diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index a8545530959b3..5996b37e66efb 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -2222,8 +2222,9 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, /* * Drivers that also update hardware from asynchronous limit or timer - * paths publish policy->cur and record cpufreq stats under their own - * lock so a concurrent clamp cannot overwrite policy->cur or stats + * paths publish policy->cur, record cpufreq stats, and update + * frequency-invariance scale under their own lock so a concurrent + * clamp cannot overwrite policy->cur, stats, or scheduler freq scale * with an older ->fast_switch() result. */ if (cpufreq_driver->flags & CPUFREQ_DRIVER_UPDATES_CUR) { @@ -2231,9 +2232,9 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, } else { policy->cur = freq; cpufreq_stats_record_transition(policy, freq); + arch_set_freq_scale(policy->related_cpus, freq, + arch_scale_freq_ref(policy->cpu)); } - arch_set_freq_scale(policy->related_cpus, freq, - arch_scale_freq_ref(policy->cpu)); if (trace_cpu_frequency_enabled()) { for_each_cpu(cpu, policy->cpus) diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 2fe8a1e3b270a..b7288e73c9b0f 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -487,9 +487,10 @@ struct cpufreq_driver { #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING BIT(6) /* - * Set by drivers whose ->fast_switch() publishes policy->cur and records - * cpufreq stats under internal synchronization because asynchronous limit or - * verification paths may concurrently update hardware and policy->cur. + * Set by drivers whose ->fast_switch() publishes policy->cur, records cpufreq + * stats, and updates frequency-invariance scale under internal synchronization + * because asynchronous limit or verification paths may concurrently update + * hardware and policy->cur. */ #define CPUFREQ_DRIVER_UPDATES_CUR BIT(7) From db5b9826138be2e4ec4a02ce01d5fec9c0beda35 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 15:26:45 -0500 Subject: [PATCH 08/11] cpufreq: apple-soc: restore policy->cur on all pending-request discards When a fast-switch request is queued behind busy DVFS, policy->cur holds the optimistic queued frequency while hardware and cpufreq_stats remain on expected_pstate. In apple_soc_cpufreq_lazy_set(), clearing pending_pstate before apple_soc_cpufreq_lazy_write() prevented apple_soc_cpufreq_fail() from restoring policy->cur to expected_pstate when a subsequent fast-switch or limit update found an idle register with an unexpected P-state. Three non-fast-switch discard paths in apple_soc_cpufreq_get_rate() and apple_soc_cpufreq_verify() also cleared pending_pstate without restoring policy->cur. Extract apple_soc_cpufreq_drop_pending() to clear pending_pstate and restore policy->cur to expected_pstate without charging cpufreq_stats or frequency scale, use it across all discard paths, and defer clearing pending_pstate in apple_soc_cpufreq_lazy_set() until apple_soc_cpufreq_lazy_write() succeeds. Update the governor handoff KUnit test to expect expected_pstate instead of the discarded queued frequency, and add KUnit coverage for idle-mismatch discards across fast_switch(), lazy_cap(), policy->update, and verify timer replay. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 152 ++++++++++++++++++++++- drivers/cpufreq/apple-soc-cpufreq.c | 40 +++--- 2 files changed, 175 insertions(+), 17 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index 1e284b3dfc0ca..d476a3081f466 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -1339,7 +1339,8 @@ static void cft_test_lazy_policy_limits_and_handoff(struct kunit *test) /* * Switching away from a fast-switch governor clears * fast_switch_enabled: the timer must drop the pending state and - * thermal samples must leave hardware and policy->cur untouched. + * restore policy->cur to expected_pstate, and thermal samples must + * leave hardware and policy->cur untouched. */ cft_set_cmd(ctx->regs, 7 | APPLE_DVFS_CMD_BUSY); ctx->priv->expected_pstate = 7; @@ -1354,10 +1355,26 @@ static void cft_test_lazy_policy_limits_and_handoff(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, -1); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(ctx->regs)), 7ULL); KUNIT_EXPECT_FALSE(test, ctx->priv->transition_failed); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); KUNIT_EXPECT_FALSE(test, cft_lazy_cap(ctx, 1, 600000, &moved)); KUNIT_EXPECT_EQ(test, moved, -1); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); + + /* Dropping a pending request while BUSY under !fast_switch_enabled also restores cur. */ + ctx->priv->cap_pstate = soc_t6050_info.max_pstate; + ctx->policy->fast_switch_enabled = true; + cft_set_cmd(ctx->regs, 7 | APPLE_DVFS_CMD_BUSY); + KUNIT_EXPECT_TRUE(test, cft_lazy_request(ctx, 4, &freq)); + KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, 15); KUNIT_EXPECT_EQ(test, ctx->policy->cur, 3000000U); + + ctx->policy->fast_switch_enabled = false; + ctx->priv->verify_deadline = ktime_add_ms(ktime_get(), 60 * MSEC_PER_SEC); + KUNIT_EXPECT_TRUE(test, cft_verify(ctx)); + KUNIT_EXPECT_EQ(test, ctx->priv->pending_pstate, -1); + KUNIT_EXPECT_FALSE(test, ctx->priv->transition_failed); + KUNIT_EXPECT_EQ(test, ctx->policy->cur, 1800000U); } static struct kunit_case apple_soc_cpufreq_test_cases[] = { @@ -2482,6 +2499,8 @@ static void cft_test_stats_busy_then_discard(struct kunit *test) local_irq_restore(flags); KUNIT_EXPECT_TRUE(test, priv->transition_failed); KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); msleep(25); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); @@ -2531,6 +2550,8 @@ static void cft_test_stats_busy_then_discard(struct kunit *test) local_irq_restore(flags); KUNIT_EXPECT_TRUE(test, priv->transition_failed); KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); msleep(25); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); @@ -2689,6 +2710,134 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) c_3000_600); } +static void cft_live_queue_3g_at_1200(struct kunit *test, + struct cpufreq_policy *policy) +{ + struct apple_cpu_priv *priv = cft_live_priv; + unsigned int freq; + + cft_live_reset_t6050(policy); + freq = cpufreq_driver_fast_switch(policy, 1200000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 1200000U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + + cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); + freq = cpufreq_driver_fast_switch(policy, 3000000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 3000000U); + KUNIT_EXPECT_EQ(test, policy->cur, 3000000U); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); +} + +/* + * Queueing a fast-switch request behind BUSY and then hitting an idle register + * with an unexpected P-state on fast_switch(), lazy_cap(), policy->update, or + * timer replay must restore policy->cur to expected_pstate and keep + * cpufreq_stats and scheduler freq_scale strictly on that accepted state. + */ +static void cft_test_stats_idle_mismatch_discard(struct kunit *test) +{ + struct cpufreq_policy *policy; + struct apple_cpu_priv *priv = cft_live_priv; + struct freq_qos_request *user_req, *min_req; + unsigned int total, c_1200_3000, c_1200_1800; + u64 t1200_before, t1800_before, t3000_before; + unsigned long flags; + unsigned int freq; + int moved; + + if (!IS_ENABLED(CONFIG_CPU_FREQ_STAT)) + kunit_skip(test, "CONFIG_CPU_FREQ_STAT is disabled"); + + policy = cft_live_policy_setup(test); + cft_live_add_qos(test, policy, &user_req, FREQ_QOS_MAX, 3000000); + cft_live_add_qos(test, policy, &min_req, FREQ_QOS_MIN, 600000); + flush_work(&policy->update); + + /* 1. Idle mismatch on follow-up fast_switch() after queueing 3 GHz. */ + cft_live_queue_3g_at_1200(test, policy); + total = cft_stats_total_trans(test, policy); + c_1200_3000 = cft_stats_trans_count(test, policy, 1200000, 3000000); + c_1200_1800 = cft_stats_trans_count(test, policy, 1200000, 1800000); + t1200_before = cft_stats_time_in_state(test, policy, 1200000); + t1800_before = cft_stats_time_in_state(test, policy, 1800000); + t3000_before = cft_stats_time_in_state(test, policy, 3000000); + + cft_set_cmd(cft_live_regs, 9); + freq = cpufreq_driver_fast_switch(policy, 1800000); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_EQ(test, freq, 0U); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + + msleep(25); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 3000000), + c_1200_3000); + KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 1800000), + c_1200_1800); + KUNIT_EXPECT_GT(test, cft_stats_time_in_state(test, policy, 1200000), + t1200_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 1800000), + t1800_before); + KUNIT_EXPECT_EQ(test, cft_stats_time_in_state(test, policy, 3000000), + t3000_before); + + /* 2. Idle mismatch on lazy_cap() after queueing 3 GHz. */ + cft_live_queue_3g_at_1200(test, policy); + total = cft_stats_total_trans(test, policy); + cft_set_cmd(cft_live_regs, 9); + local_irq_save(flags); + moved = apple_soc_cpufreq_lazy_cap(priv, 7, 1800000); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_EQ(test, moved, -1); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + + /* 3. Idle mismatch on policy->update ceiling drop after queueing 3 GHz. */ + cft_live_queue_3g_at_1200(test, policy); + total = cft_stats_total_trans(test, policy); + cft_set_cmd(cft_live_regs, 9); + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 1800000), 0); + flush_work(&policy->update); + hrtimer_cancel(&priv->verify_timer); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); + + /* 4. Idle mismatch on verify timer replay after queueing 3 GHz. */ + KUNIT_ASSERT_GE(test, freq_qos_update_request(user_req, 3000000), 0); + flush_work(&policy->update); + cft_live_queue_3g_at_1200(test, policy); + total = cft_stats_total_trans(test, policy); + cft_set_cmd(cft_live_regs, 9); + local_irq_save(flags); + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_verify(&priv->verify_timer), + HRTIMER_NORESTART); + hrtimer_cancel(&priv->verify_timer); + local_irq_restore(flags); + KUNIT_EXPECT_TRUE(test, priv->transition_failed); + KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); + KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); + KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); + cft_expect_freq_scale(test, policy, 1200000); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); +} + static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE_PARAM(cft_test_get, cft_get_gen_params), KUNIT_CASE(cft_test_thermal_owner), @@ -2698,6 +2847,7 @@ static struct kunit_case apple_soc_cpufreq_policy_test_cases[] = { KUNIT_CASE(cft_test_stats_clamp_and_race), KUNIT_CASE(cft_test_stats_busy_then_discard), KUNIT_CASE(cft_test_stats_busy_suspend_fail), + KUNIT_CASE(cft_test_stats_idle_mismatch_discard), {} }; diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 6bbcc9c62a792..9bef76a50101f 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -418,6 +418,8 @@ static u32 apple_soc_cpufreq_timeout(const struct apple_soc_cpufreq_info *info) return info->transition_timeout_us ?: APPLE_DVFS_TRANSITION_TIMEOUT; } +static int apple_soc_cpufreq_drop_pending(struct apple_cpu_priv *priv); + static unsigned int apple_soc_cpufreq_get_rate(unsigned int cpu) { struct cpufreq_policy *policy; @@ -436,7 +438,7 @@ static unsigned int apple_soc_cpufreq_get_rate(unsigned int cpu) raw_spin_lock_irqsave(&priv->lock, flags); priv->waited = true; - priv->pending_pstate = -1; + apple_soc_cpufreq_drop_pending(priv); raw_spin_unlock_irqrestore(&priv->lock, flags); } @@ -576,16 +578,10 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy, * * Called with priv->lock held. */ -static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, - unsigned int pstate, - bool record_stats); - static void apple_soc_cpufreq_fail(struct apple_cpu_priv *priv, u64 reg) { - if (priv->pending_pstate >= 0) - apple_soc_cpufreq_publish_cur(priv, priv->expected_pstate, false); + apple_soc_cpufreq_drop_pending(priv); priv->transition_failed = true; - priv->pending_pstate = -1; /* The scheduler may hold a runqueue lock: the timer reports it. */ priv->failure_unreported = true; priv->failure_command = reg; @@ -680,6 +676,21 @@ static void apple_soc_cpufreq_publish_cur(struct apple_cpu_priv *priv, apple_soc_cpufreq_record_cur(policy, cur, record_stats); } +/* + * Discards an unaccepted pending request and restores policy->cur to the + * accepted hardware state. Called with priv->lock held. + */ +static int apple_soc_cpufreq_drop_pending(struct apple_cpu_priv *priv) +{ + int pstate = priv->pending_pstate; + + if (pstate >= 0) { + priv->pending_pstate = -1; + apple_soc_cpufreq_publish_cur(priv, priv->expected_pstate, false); + } + return pstate; +} + /* * Clamps a deferred or restored lazy-verify P-state against the current * policy bounds and thermal ceiling using cpufreq's maximum-first OPP @@ -779,7 +790,7 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) ktime_t now = ktime_get(); if (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled)) - priv->pending_pstate = -1; + apple_soc_cpufreq_drop_pending(priv); if (ktime_after(now, priv->verify_deadline)) apple_soc_cpufreq_fail(priv, reg); @@ -791,7 +802,7 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer) HRTIMER_MODE_ABS_HARD); } else if (priv->pending_pstate >= 0) { if (priv->policy && !READ_ONCE(priv->policy->fast_switch_enabled)) { - priv->pending_pstate = -1; + apple_soc_cpufreq_drop_pending(priv); if (FIELD_GET(APPLE_DVFS_CMD_PS1, reg) != priv->expected_pstate) apple_soc_cpufreq_fail(priv, reg); } else { @@ -857,7 +868,6 @@ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv, } /* An idle cluster has accepted, or refused, the previous request. */ - priv->pending_pstate = -1; if (apple_soc_cpufreq_lazy_write(priv, reg, pstate)) { if (!hrtimer_is_queued(&priv->verify_timer)) apple_soc_cpufreq_verify_later(priv); @@ -866,6 +876,7 @@ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv, hrtimer_start(&priv->verify_timer, 0, HRTIMER_MODE_REL_HARD); return -EIO; } + priv->pending_pstate = -1; *accepted = true; return 0; } @@ -988,12 +999,9 @@ static int apple_soc_cpufreq_drain(struct apple_cpu_priv *priv) hrtimer_cancel(&priv->verify_timer); raw_spin_lock_irqsave(&priv->lock, flags); - pstate = priv->pending_pstate; - priv->pending_pstate = -1; - if (pstate >= 0) { - apple_soc_cpufreq_publish_cur(priv, priv->expected_pstate, false); + pstate = apple_soc_cpufreq_drop_pending(priv); + if (pstate >= 0) pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); - } report = priv->failure_unreported; priv->failure_unreported = false; reg = priv->failure_command; From e4d2c89e02e2f5a4bf6bccede29dc44a12ef006c Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 15:45:55 -0500 Subject: [PATCH 09/11] cpufreq: apple-soc: emit cpu_frequency transition on suspend changes In lazy-verify suspend, calling apple_soc_cpufreq_set_pstate() followed by apple_soc_cpufreq_record_cur() kept failed suspends out of cpufreq_stats and scheduler frequency scale, but skipped cpufreq_freq_transition_begin()/end() on successful suspend frequency changes, dropping cpufreq transition notifications and the power:cpu_frequency trace event. Snapshot the accepted hardware frequency before calling apple_soc_cpufreq_set_pstate() in apple_soc_cpufreq_suspend(). When policy->suspend_freq is set and the hardware transition succeeds at a different frequency, report it via cpufreq_freq_transition_begin() and cpufreq_freq_transition_end(..., 0) so power:cpu_frequency, transition notifiers, cpufreq_stats, policy->cur, and arch_set_freq_scale() are all updated once without touching stats or tracepoints when suspend fails. Enable CONFIG_TRACEPOINTS via CONFIG_FTRACE and CONFIG_ENABLE_DEFAULT_TRACERS in apple-soc-cpufreq.kunitconfig and extend cft_test_stats_busy_suspend_fail() to verify power:cpu_frequency trace emission on failed, successful, and no-op suspends. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 66 +++++++++++++++++-- drivers/cpufreq/apple-soc-cpufreq.c | 17 ++++- drivers/cpufreq/apple-soc-cpufreq.kunitconfig | 2 + 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index d476a3081f466..e213bbc897d71 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -23,6 +23,7 @@ #include #include #include +#include #define CFT_REGS_SIZE 0x100 #define CFT_NR_STATES 5 @@ -2601,17 +2602,42 @@ static void cft_test_stats_busy_then_discard(struct kunit *test) cft_expect_freq_scale(test, policy, 3000000); } +struct cft_freq_trace { + unsigned int cpu; + unsigned int count; + unsigned int last_freq; +}; + +static void cft_trace_cpu_freq(void *data, unsigned int freq, + unsigned int cpu_id) +{ + struct cft_freq_trace *t = data; + + if (cpu_id != t->cpu) + return; + t->last_freq = freq; + t->count++; +} + +static void cft_unregister_freq_trace(void *data) +{ + unregister_trace_cpu_frequency(cft_trace_cpu_freq, data); + tracepoint_synchronize_unregister(); +} + /* * Queueing a fast-switch request behind BUSY and then hitting a failed suspend * toward suspend_freq must restore policy->cur to the accepted hardware state - * and keep cpufreq_stats residency/transitions strictly on that state, never - * charging the discarded queued request or a spurious suspend target. + * and keep cpufreq_stats residency/transitions, FIE scale, and trace events + * strictly on that state; a subsequent successful suspend must update + * policy->cur, stats, FIE scale, and emit power:cpu_frequency once. */ static void cft_test_stats_busy_suspend_fail(struct kunit *test) { struct cpufreq_policy *policy; struct apple_cpu_priv *priv = cft_live_priv; struct freq_qos_request *user_req, *min_req; + struct cft_freq_trace trace_ctx = {}; unsigned int total, c_1200_3000, c_1200_600, c_600_3000, c_600_1200; unsigned int c_3000_600; u64 t1200_before, t3000_before, t600_before; @@ -2626,6 +2652,19 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) flush_work(&policy->update); cft_live_reset_t6050(policy); + if (IS_ENABLED(CONFIG_TRACEPOINTS)) { + trace_ctx.cpu = policy->cpu; + KUNIT_ASSERT_EQ(test, + register_trace_cpu_frequency(cft_trace_cpu_freq, + &trace_ctx), + 0); + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, + cft_unregister_freq_trace, + &trace_ctx), + 0); + } + /* Put hardware cleanly at 1.2 GHz (pstate 4). */ freq = cpufreq_driver_fast_switch(policy, 1200000); hrtimer_cancel(&priv->verify_timer); @@ -2656,14 +2695,18 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) /* * Suspend toward 600 MHz while BUSY persists: suspend fails without - * charging 3 GHz or 600 MHz. + * charging 3 GHz or 600 MHz or emitting power:cpu_frequency. */ + trace_ctx.count = 0; + trace_ctx.last_freq = 0; policy->suspend_freq = 600000; KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), -EIO); KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); KUNIT_EXPECT_EQ(test, priv->expected_pstate, 4U); KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); cft_expect_freq_scale(test, policy, 1200000); + if (IS_ENABLED(CONFIG_TRACEPOINTS)) + KUNIT_EXPECT_EQ(test, trace_ctx.count, 0U); msleep(25); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); @@ -2686,7 +2729,8 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) /* * When DVFS clears BUSY before suspend runs, suspend drops the queued - * 3 GHz request and transitions directly from 1.2 GHz to 600 MHz. + * 3 GHz request and transitions directly from 1.2 GHz to 600 MHz, + * emitting power:cpu_frequency once. */ priv->transition_failed = false; cft_set_cmd(cft_live_regs, 4 | APPLE_DVFS_CMD_BUSY); @@ -2695,12 +2739,18 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, freq, 3000000U); KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); + trace_ctx.count = 0; + trace_ctx.last_freq = 0; cft_set_cmd(cft_live_regs, 4); KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); KUNIT_EXPECT_EQ(test, priv->expected_pstate, 1U); KUNIT_EXPECT_EQ(test, policy->cur, 600000U); cft_expect_freq_scale(test, policy, 600000); + if (IS_ENABLED(CONFIG_TRACEPOINTS)) { + KUNIT_EXPECT_EQ(test, trace_ctx.count, 1U); + KUNIT_EXPECT_EQ(test, trace_ctx.last_freq, 600000U); + } KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), c_1200_600 + 1); @@ -2708,6 +2758,14 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) c_1200_3000); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 3000000, 600000), c_3000_600); + + /* Suspending at the already-settled frequency emits no extra trace or transition. */ + trace_ctx.count = 0; + trace_ctx.last_freq = 0; + KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); + if (IS_ENABLED(CONFIG_TRACEPOINTS)) + KUNIT_EXPECT_EQ(test, trace_ctx.count, 0U); + KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); } static void cft_live_queue_3g_at_1200(struct kunit *test, diff --git a/drivers/cpufreq/apple-soc-cpufreq.c b/drivers/cpufreq/apple-soc-cpufreq.c index 9bef76a50101f..00d4e00e55cda 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.c +++ b/drivers/cpufreq/apple-soc-cpufreq.c @@ -1088,7 +1088,7 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) if (priv->info->lazy_verify) { int pstate = apple_soc_cpufreq_drain(priv); - unsigned int freq; + unsigned int old_freq, freq; unsigned long flags; int ret; @@ -1103,6 +1103,10 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) if (pstate < 0) pstate = priv->expected_pstate; pstate = apple_soc_cpufreq_clamp_pstate(priv, pstate); + old_freq = apple_soc_cpufreq_pstate_freq(policy, + priv->expected_pstate); + if (!old_freq && priv->expected_pstate == priv->cap_pstate) + old_freq = priv->cap_freq; raw_spin_unlock_irqrestore(&priv->lock, flags); ret = apple_soc_cpufreq_set_pstate(priv, pstate); @@ -1117,8 +1121,17 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy) return ret; } freq = apple_soc_cpufreq_pstate_freq(policy, pstate); - if (freq) + if (policy->suspend_freq && freq && freq != old_freq) { + struct cpufreq_freqs freqs = { + .old = old_freq ?: READ_ONCE(policy->cur), + .new = freq, + }; + + cpufreq_freq_transition_begin(policy, &freqs); + cpufreq_freq_transition_end(policy, &freqs, 0); + } else if (freq) { apple_soc_cpufreq_record_cur(policy, freq, true); + } return 0; } diff --git a/drivers/cpufreq/apple-soc-cpufreq.kunitconfig b/drivers/cpufreq/apple-soc-cpufreq.kunitconfig index b264e6300e154..3a1532e89cdc3 100644 --- a/drivers/cpufreq/apple-soc-cpufreq.kunitconfig +++ b/drivers/cpufreq/apple-soc-cpufreq.kunitconfig @@ -6,5 +6,7 @@ CONFIG_PM=y CONFIG_SUSPEND=y CONFIG_CPU_FREQ=y CONFIG_CPU_FREQ_STAT=y +CONFIG_FTRACE=y +CONFIG_ENABLE_DEFAULT_TRACERS=y CONFIG_ARM_APPLE_SOC_CPUFREQ=y CONFIG_ARM_APPLE_SOC_CPUFREQ_KUNIT_TEST=y From 085f7d94fce28bb1f6c6535a89ddd94ceb8bd3de Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 15:50:11 -0500 Subject: [PATCH 10/11] cpufreq: apple-soc: allocate KUnit trace_ctx with kunit_kzalloc() KUnit runs deferred cleanup actions in a separate thread after the test function returns, so passing a stack-allocated cft_freq_trace to register_trace_cpu_frequency() risks dereferencing an expired stack object before cft_unregister_freq_trace() unregisters and synchronizes the tracepoint. Allocate trace_ctx in cft_test_stats_busy_suspend_fail() with kunit_kzalloc() before registering the cleanup action so LIFO teardown runs unregister_trace_cpu_frequency() and tracepoint_synchronize_unregister() before the trace context is freed. Signed-off-by: Erik Bourget --- drivers/cpufreq/apple-soc-cpufreq-test.c | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/drivers/cpufreq/apple-soc-cpufreq-test.c b/drivers/cpufreq/apple-soc-cpufreq-test.c index e213bbc897d71..d40d9dc86c952 100644 --- a/drivers/cpufreq/apple-soc-cpufreq-test.c +++ b/drivers/cpufreq/apple-soc-cpufreq-test.c @@ -2637,7 +2637,7 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) struct cpufreq_policy *policy; struct apple_cpu_priv *priv = cft_live_priv; struct freq_qos_request *user_req, *min_req; - struct cft_freq_trace trace_ctx = {}; + struct cft_freq_trace *trace_ctx; unsigned int total, c_1200_3000, c_1200_600, c_600_3000, c_600_1200; unsigned int c_3000_600; u64 t1200_before, t3000_before, t600_before; @@ -2652,16 +2652,18 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) flush_work(&policy->update); cft_live_reset_t6050(policy); + trace_ctx = kunit_kzalloc(test, sizeof(*trace_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, trace_ctx); if (IS_ENABLED(CONFIG_TRACEPOINTS)) { - trace_ctx.cpu = policy->cpu; + trace_ctx->cpu = policy->cpu; KUNIT_ASSERT_EQ(test, register_trace_cpu_frequency(cft_trace_cpu_freq, - &trace_ctx), + trace_ctx), 0); KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, cft_unregister_freq_trace, - &trace_ctx), + trace_ctx), 0); } @@ -2697,8 +2699,8 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) * Suspend toward 600 MHz while BUSY persists: suspend fails without * charging 3 GHz or 600 MHz or emitting power:cpu_frequency. */ - trace_ctx.count = 0; - trace_ctx.last_freq = 0; + trace_ctx->count = 0; + trace_ctx->last_freq = 0; policy->suspend_freq = 600000; KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), -EIO); KUNIT_EXPECT_EQ(test, priv->pending_pstate, -1); @@ -2706,7 +2708,7 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, policy->cur, 1200000U); cft_expect_freq_scale(test, policy, 1200000); if (IS_ENABLED(CONFIG_TRACEPOINTS)) - KUNIT_EXPECT_EQ(test, trace_ctx.count, 0U); + KUNIT_EXPECT_EQ(test, trace_ctx->count, 0U); msleep(25); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total); @@ -2739,8 +2741,8 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, freq, 3000000U); KUNIT_EXPECT_EQ(test, priv->pending_pstate, 15); - trace_ctx.count = 0; - trace_ctx.last_freq = 0; + trace_ctx->count = 0; + trace_ctx->last_freq = 0; cft_set_cmd(cft_live_regs, 4); KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); KUNIT_EXPECT_EQ(test, FIELD_GET(APPLE_DVFS_CMD_PS1, cft_cmd(cft_live_regs)), 1ULL); @@ -2748,8 +2750,8 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) KUNIT_EXPECT_EQ(test, policy->cur, 600000U); cft_expect_freq_scale(test, policy, 600000); if (IS_ENABLED(CONFIG_TRACEPOINTS)) { - KUNIT_EXPECT_EQ(test, trace_ctx.count, 1U); - KUNIT_EXPECT_EQ(test, trace_ctx.last_freq, 600000U); + KUNIT_EXPECT_EQ(test, trace_ctx->count, 1U); + KUNIT_EXPECT_EQ(test, trace_ctx->last_freq, 600000U); } KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); KUNIT_EXPECT_EQ(test, cft_stats_trans_count(test, policy, 1200000, 600000), @@ -2760,11 +2762,11 @@ static void cft_test_stats_busy_suspend_fail(struct kunit *test) c_3000_600); /* Suspending at the already-settled frequency emits no extra trace or transition. */ - trace_ctx.count = 0; - trace_ctx.last_freq = 0; + trace_ctx->count = 0; + trace_ctx->last_freq = 0; KUNIT_EXPECT_EQ(test, apple_soc_cpufreq_suspend(policy), 0); if (IS_ENABLED(CONFIG_TRACEPOINTS)) - KUNIT_EXPECT_EQ(test, trace_ctx.count, 0U); + KUNIT_EXPECT_EQ(test, trace_ctx->count, 0U); KUNIT_EXPECT_EQ(test, cft_stats_total_trans(test, policy), total + 1); } From 3653dfe88ef8be759e2095ed0222b25fddffe910 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Sat, 28 Nov 2026 15:32:15 -0500 Subject: [PATCH 11/11] PM: QoS: export freq_qos_read_value() for modular cpufreq drivers Modular cpufreq drivers that enforce live QoS bounds synchronously from fast-switch or hrtimer paths before the asynchronous policy->update worker runs need to read &policy->constraints directly via freq_qos_read_value(). Without an export, building CONFIG_ARM_APPLE_SOC_CPUFREQ=m fails modpost with undefined symbol freq_qos_read_value. Export freq_qos_read_value() with EXPORT_SYMBOL_GPL(), matching the other freq_qos_* helpers in kernel/power/qos.c. Signed-off-by: Erik Bourget --- kernel/power/qos.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/power/qos.c b/kernel/power/qos.c index 1944dbeb0d4c9..ddf93f9d85e26 100644 --- a/kernel/power/qos.c +++ b/kernel/power/qos.c @@ -602,6 +602,7 @@ s32 freq_qos_read_value(struct freq_constraints *qos, return ret; } +EXPORT_SYMBOL_GPL(freq_qos_read_value); /** * freq_qos_apply - Add/modify/remove frequency QoS request.