FAILED: patch "[PATCH] cpufreq: Cache post-resolution target frequency in" failed to apply to 5.10-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 166207cac45191637c47a457a5c16ca6b6773a16
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112528-unscrew-recall-9dd7@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 166207cac45191637c47a457a5c16ca6b6773a16 Mon Sep 17 00:00:00 2001
From: "Rafael J. Wysocki" <[email protected]>
Date: Fri, 6 Nov 2026 16:04:05 +0100
Subject: [PATCH] cpufreq: Cache post-resolution target frequency in
__resolve_freq()
get_next_freq() in the schedutil governor returns the resolved frequency from cpufreq_driver_resolve_freq(), and that resolved value is then passed to cpufreq_driver_fast_switch(). The only reader of cached_target_freq, acpi_cpufreq_fast_switch(), compares it with the post-resolution target frequency, but it holds the pre-resolution clamped frequency, so any request that falls between table entries misses the cache and falls back to a table scan.
Address this by making __resolve_freq() store the post-resolution frequency in cached_target_freq.
drivers/cpufreq/cpufreq.c +2 -2
@@ -488,8 +488,8 @@ static unsigned int __resolve_freq(struct cpufreq_policy *policy, idx =cpufreq_frequency_table_target (policy, target_freq, min, max, relation); policy->cached_resolved_idx = idx; - policy->cached_target_freq = target_freq; -return policy->freq_table[idx].frequency; + policy->cached_target_freq = policy->freq_table[idx].frequency; +return policy->cached_target_freq; }/**
Sasha Levin
maintainerPATCH 1/4 docs/zh_CN: add cpu-freq cpu-drivers.rst translation
From: Yanteng Si <[email protected]>
[ Upstream commit 8b6d5ae8a9968c0a3807587d33b5952cdf71de1b ]
This patch translates Documention/cpu-freq/cpu-drivers.rst into Chinese.
Stable-dep-of: 166207cac451 ("cpufreq: Cache post-resolution target frequency in __resolve_freq()")
--- .../zh_CN/cpu-freq/cpu-drivers.rst | 259 ++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 Documentation/translations/zh_CN/cpu-freq/cpu-drivers.rst
Documentation/translations/zh_CN/cpu-freq/cpu-drivers.rst new file +259 -0
@@ -0,0 +1,259 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. include:: ../disclaimer-zh_CN.rst + +:Original: :doc:`../../../cpu-freq/cpu-drivers` +:Translator: Yanteng Si <[email protected]> + +.. _cn_cpu-drivers.rst: + + +======================================= +如何实现一个新的CPUFreq处理器驱动程序? +======================================= + +作者: + + + - Dominik Brodowski <[email protected]> + - Rafael J. Wysocki <[email protected]> + - Viresh Kumar <[email protected]> + +.. Contents + + 1. 怎么做? + 1.1 初始化 + 1.2 Per-CPU 初始化 + 1.3 验证 + 1.4 target/target_index 或 setpolicy? + 1.5 target/target_index + 1.6 setpolicy + 1.7 get_intermediate 与 target_intermediate + 2. 频率表助手 + + + +1. 怎么做? +=========== + +如此,你刚刚得到了一个全新的CPU/芯片组及其数据手册,并希望为这个CPU/芯片组添加cpufreq +支持?很好,这里有一些至关重要的提示: + + +1.1 初始化 +---------- + +首先,在__initcall_level_7 (module_init())或更靠后的函数中检查这个内核是否 +运行在正确的CPU和正确的芯片组上。如果是,则使用cpufreq_register_driver()向 +CPUfreq核心层注册一个cpufreq_driver结构体。 + +结构体cpufreq_driver应该包含什么成员? + + .name - 驱动的名字。 + + .init - 一个指向per-policy初始化函数的指针。 + + .verify - 一个指向"verification"函数的指针。 + + .setpolicy 或 .fast_switch 或 .target 或 .target_index - 差异见 + 下文。 + +并且可选择 + + .flags - cpufreq核的提示。 + + .driver_data - cpufreq驱动程序的特定数据。 + + .resolve_freq - 返回最适合目标频率的频率。不过并不能改变频率。 + + .get_intermediate 和 target_intermediate - 用于在改变CPU频率时切换到稳定 + 的频率。 + + .get - 返回CPU的当前频率。 + + .bios_limit - 返回HW/BIOS对CPU的最大频率限制值。 + + .exit - 一个指向per-policy清理函数的指针,该函数在cpu热插拔过程的CPU_POST_DEAD + 阶段被调用。 + + .stop_cpu - 一个指向per-policy停止函数的指针,该函数在cpu热插拔过程的CPU_DOWN_PREPARE + 阶段被调用。 + + .suspend - 一个指向per-policy暂停函数的指针,该函数在关中断且在该策略的调节器停止 + 后被调用。 + + .resume - 一个指向per-policy恢复函数的指针,该函数在关中断且在调节器再一次开始前被 + 调用。 + + .ready - 一个指向per-policy准备函数的指针,该函数在策略完全初始化之后被调用。 + + .attr - 一个指向NULL结尾的"struct freq_attr"列表的指针,该函数允许导出值到 + sysfs。 + + .boost_enabled - 如果设置,则启用提升(boost)频率。 + + .set_boost - 一个指向per-policy函数的指针,该函数用来开启/关闭提升(boost)频率功能。 + + +1.2 Per-CPU 初始化 +------------------ + +每当一个新的CPU被注册到设备模型中,或者在cpufreq驱动注册自己之后,如果此CPU的cpufreq策 +略不存在,则会调用per-policy的初始化函数cpufreq_driver.init。请注意,.init()和.exit()程序 +只对策略调用一次,而不是对策略管理的每个CPU调用一次。它需要一个 ``struct cpufreq_policy +*policy`` 作为参数。现在该怎么做呢? + +如果有必要,请在你的CPU上激活CPUfreq功能支持。 + +然后,驱动程序必须填写以下数值: + ++-----------------------------------+--------------------------------------+ +|policy->cpuinfo.min_freq 和 | | +|policy->cpuinfo.max_freq | 该CPU支持的最低和最高频率(kHz) | +| | | +| | | ++-----------------------------------+--------------------------------------+ +|policy->cpuinfo.transition_latency | | +| | CPU在两个频率之间切换所需的时间,以 | +| | 纳秒为单位(如适用,否则指定 | +| | CPUFREQ_ETERNAL) | ++-----------------------------------+--------------------------------------+ +|policy->cur | 该CPU当前的工作频率(如适用) | +| | | ++-----------------------------------+--------------------------------------+ +|policy->min, | | +|policy->max, | | +|policy->policy and, if necessary, | | +|policy->governor | 必须包含该cpu的 “默认策略”。稍后 | +| | 会用这些值调用 | +| | cpufreq_driver.verify and either | +| | cpufreq_driver.setpolicy or | +| | cpufreq_driver.target/target_index | +| | | ++-----------------------------------+--------------------------------------+ +|policy->cpus | 用与这个CPU一起做DVFS的(在线+离线) | +| | CPU(即与它共享时钟/电压轨)的掩码更新 | +| | 这个 | +| | | ++-----------------------------------+--------------------------------------+ + +对于设置其中的一些值(cpuinfo.min[max]_freq, policy->min[max]),频率表助手可能会有帮 +助。关于它们的更多信息,请参见第2节。 + + +1.3 验证 +-------- + +当用户决定设置一个新的策略(由 “policy,governor,min,max组成”)时,必须对这个策略进行验证, +以便纠正不兼容的值。为了验证这些值,cpufreq_verify_within_limits(``struct cpufreq_policy +*policy``, ``unsigned int min_freq``, ``unsigned int max_freq``)函数可能会有帮助。 +关于频率表助手的详细内容请参见第2节。 + +您需要确保至少有一个有效频率(或工作范围)在 policy->min 和 policy->max 范围内。如果有必 +要,先增加policy->max,只有在没有办法的情况下,才减少policy->min。 + + +1.4 target 或 target_index 或 setpolicy 或 fast_switch? +------------------------------------------------------- + +大多数cpufreq驱动甚至大多数cpu频率升降算法只允许将CPU频率设置为预定义的固定值。对于这些,你 +可以使用->target(),->target_index()或->fast_switch()回调。 + +有些cpufreq功能的处理器可以自己在某些限制之间切换频率。这些应使用->setpolicy()回调。 + + +1.5. target/target_index +------------------------ + +target_index调用有两个参数:``struct cpufreq_policy * policy``和``unsigned int`` +索引(于列出的频率表)。 + +当调用这里时,CPUfreq驱动必须设置新的频率。实际频率必须由freq_table[index].frequency决定。 + +它应该总是在错误的情况下恢复到之前的频率(即policy->restore_freq),即使我们之前切换到中间频率。 + +已弃用 +---------- +目标调用有三个参数。``struct cpufreq_policy * policy``, unsigned int target_frequency, +unsigned int relation. + +CPUfreq驱动在调用这里时必须设置新的频率。实际的频率必须使用以下规则来确定。 + +- 紧跟 "目标频率"。 +- policy->min <= new_freq <= policy->max (这必须是有效的!!!) +- 如果 relation==CPUFREQ_REL_L,尝试选择一个高于或等于 target_freq 的 new_freq。("L代表 + 最低,但不能低于") +- 如果 relation==CPUFREQ_REL_H,尝试选择一个低于或等于 target_freq 的 new_freq。("H代表 + 最高,但不能高于") + +这里,频率表助手可能会帮助你--详见第2节。 + +1.6. fast_switch +---------------- + +这个函数用于从调度器的上下文进行频率切换。并非所有的驱动都要实现它,因为不允许在这个回调中睡眠。这 +个回调必须经过高度优化,以尽可能快地进行切换。 + +这个函数有两个参数: ``struct cpufreq_policy *policy`` 和 ``unsigned int target_frequency``。 + + +1.7 setpolicy +------------- + +setpolicy调用只需要一个``struct cpufreq_policy * policy``作为参数。需要将处理器内或芯片组内动态频 +率切换的下限设置为policy->min,上限设置为policy->max,如果支持的话,当policy->policy为 +CPUFREQ_POLICY_PERFORMANCE时选择面向性能的设置,当CPUFREQ_POLICY_POWERSAVE时选择面向省电的设置。 +也可以查看drivers/cpufreq/longrun.c中的参考实现。 + +1.8 get_intermediate 和 target_intermediate +-------------------------------------------- + +仅适用于 target_index() 和 CPUFREQ_ASYNC_NOTIFICATION 未设置的驱动。 + +get_intermediate应该返回一个平台想要切换到的稳定的中间频率,target_intermediate()应该将CPU设置为 +该频率,然后再跳转到'index'对应的频率。核心会负责发送通知,驱动不必在target_intermediate()或 +target_index()中处理。 + +在驱动程序不想因为某个目标频率切换到中间频率的情况下,它们可以从get_intermediate()中返回'0'。在这种情况 +下,核心将直接调用->target_index()。 + +注意:->target_index()应该在失败的情况下恢复到policy->restore_freq,因为core会为此发送通知。 + + +2. 频率表助手 +============= + +由于大多数cpufreq处理器只允许被设置为几个特定的频率,因此,一个带有一些函数的 “频率表”可能会辅助处理器驱动 +程序的一些工作。这样的 "频率表" 由一个cpufreq_frequency_table条目构成的数组组成,"driver_data" 中包 +含了驱动程序的具体数值,"frequency" 中包含了相应的频率,并设置了标志。在表的最后,需要添加一个 +cpufreq_frequency_table条目,频率设置为CPUFREQ_TABLE_END。而如果想跳过表中的一个条目,则将频率设置为 +CPUFREQ_ENTRY_INVALID。这些条目不需要按照任何特定的顺序排序,但如果它们是cpufreq 核心会对它们进行快速的DVFS, +因为搜索最佳匹配会更快。 + +如果策略在其policy->freq_table字段中包含一个有效的指针,cpufreq表就会被核心自动验证。 + +cpufreq_frequency_table_verify()保证至少有一个有效的频率在policy->min和policy->max范围内,并且所有其他 +标准都被满足。这对->verify调用很有帮助。 + +cpufreq_frequency_table_target()是对应于->target阶段的频率表助手。只要把数值传递给这个函数,这个函数就会返 +回包含CPU要设置的频率的频率表条目。 + +以下宏可以作为cpufreq_frequency_table的迭代器。 + +cpufreq_for_each_entry(pos, table) - 遍历频率表的所有条目。 + +cpufreq_for_each_valid_entry(pos, table) - 该函数遍历所有条目,不包括CPUFREQ_ENTRY_INVALID频率。 +使用参数 "pos"-一个``cpufreq_frequency_table * `` 作为循环变量,使用参数 "table"-作为你想迭代 +的``cpufreq_frequency_table * `` 。 + +例如:: + + struct cpufreq_frequency_table *pos, *driver_freq_table; + + cpufreq_for_each_entry(pos, driver_freq_table) { + /* Do something with pos */ + pos->frequency = ... + } + +如果你需要在driver_freq_table中处理pos的位置,不要减去指针,因为它的代价相当高。相反,使用宏 +cpufreq_for_each_entry_idx() 和 cpufreq_for_each_valid_entry_idx() 。
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 cpufreq: Reuse cpufreq_driver_resolve_freq() in __cpufreq_driver_target()
From: Viresh Kumar <[email protected]>
[ Upstream commit f9ccdec24d91ffddf1c6f4173b0e191fc08c7d14 ]
__cpufreq_driver_target() open codes cpufreq_driver_resolve_freq(), lets make the former reuse the later.
Separate out __resolve_freq() to accept relation as well as an argument and use it at both the locations.
Stable-dep-of: 166207cac451 ("cpufreq: Cache post-resolution target frequency in __resolve_freq()")
--- drivers/cpufreq/cpufreq.c | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-)
drivers/cpufreq/cpufreq.c +22 -20
@@ -529,19 +529,8 @@ void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) }EXPORT_SYMBOL_GPL (cpufreq_disable_fast_switch); -/** -* cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported -* one. -* @policy: associated policy to interrogate -* @target_freq: target frequency to resolve. -* -* The target to driver frequency mapping is cached in the policy. -* -* Return: Lowest driver-supported frequency greater than or equal to the -* given target_freq, subject to policy (min/max) and driver limitations. -*/ -unsigned int cpufreq_driver_resolve_freq (struct cpufreq_policy *policy, -unsigned int target_freq) +static unsigned int __resolve_freq (struct cpufreq_policy *policy, +unsigned int target_freq,unsigned int relation) { target_freq =clamp_val (target_freq, policy->min, policy->max); policy->cached_target_freq = target_freq;
@@ -550,7 +539,7 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,unsigned int idx; idx =cpufreq_frequency_table_target (policy, target_freq, -CPUFREQ_RELATION_L ); + relation); policy->cached_resolved_idx = idx;return policy->freq_table[idx].frequency; }
@@ -560,6 +549,23 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,return target_freq; } + +/** +* cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported +* one. +* @policy: associated policy to interrogate +* @target_freq: target frequency to resolve. +* +* The target to driver frequency mapping is cached in the policy. +* +* Return: Lowest driver-supported frequency greater than or equal to the +* given target_freq, subject to policy (min/max) and driver limitations. +*/ +unsigned int cpufreq_driver_resolve_freq (struct cpufreq_policy *policy, +unsigned int target_freq) +{ +return __resolve_freq (policy, target_freq,CPUFREQ_RELATION_L ); +}EXPORT_SYMBOL_GPL (cpufreq_driver_resolve_freq);unsigned int cpufreq_policy_transition_delay_us (struct cpufreq_policy *policy)
@@ -2257,13 +2263,11 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int relation) {unsigned int old_target_freq = target_freq; -int index;if (cpufreq_disabled ())return -ENODEV ; -/* Make sure that target_freq is within supported range */ - target_freq =clamp_val (target_freq, policy->min, policy->max); + target_freq =__resolve_freq (policy, target_freq, relation);pr_debug ("target for CPU %u: %u kHz, relation %u, requested %u kHz\n" , policy->cpu, target_freq, relation, old_target_freq);
@@ -2287,9 +2291,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,if (!cpufreq_driver->target_index)return -EINVAL ; - index =cpufreq_frequency_table_target (policy, target_freq, relation); - -return __target_index (policy, index); +return __target_index (policy, policy->cached_resolved_idx); }EXPORT_SYMBOL_GPL (__cpufreq_driver_target);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 cpufreq: Remove ->resolve_freq()
In reply to Sasha LevinFrom: Viresh Kumar <[email protected]>
[ Upstream commit b3beca76181681fce9cf72f37d19c3030e3353c0 ]
Commit e3c062360870 ("cpufreq: add cpufreq_driver_resolve_freq()") introduced this callback, back in 2016, for drivers that provide the ->target() callback.
The kernel hasn't seen a single user of it in the past 5 years and it is not likely to be used any time soon.
Remove it for now.
Stable-dep-of: 166207cac451 ("cpufreq: Cache post-resolution target frequency in __resolve_freq()")
--- Documentation/cpu-freq/cpu-drivers.rst | 3 --- .../zh_CN/cpu-freq/cpu-drivers.rst | 2 -- drivers/cpufreq/cpufreq.c | 21 +++++++------------ include/linux/cpufreq.h | 9 -------- 4 files changed, 8 insertions(+), 27 deletions(-)
Documentation/cpu-freq/cpu-drivers.rst +0 -3
@@ -58,9 +58,6 @@ And optionally .driver_data - cpufreq driver specific data. - .resolve_freq - Returns the most appropriate frequency for a target - frequency. Doesn't change the frequency though. - .get_intermediate and target_intermediate - Used to switch to stable frequency while changing CPU frequency.
Documentation/translations/zh_CN/cpu-freq/cpu-drivers.rst +0 -2
@@ -64,8 +64,6 @@ CPUfreq核心层注册一个cpufreq_driver结构体。 .driver_data - cpufreq驱动程序的特定数据。 - .resolve_freq - 返回最适合目标频率的频率。不过并不能改变频率。 - .get_intermediate 和 target_intermediate - 用于在改变CPU频率时切换到稳定 的频率。
drivers/cpufreq/cpufreq.c +8 -13
@@ -532,22 +532,17 @@ EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);static unsigned int __resolve_freq (struct cpufreq_policy *policy,unsigned int target_freq,unsigned int relation) { - target_freq =clamp_val (target_freq, policy->min, policy->max); - policy->cached_target_freq = target_freq; +unsigned int idx; -if (cpufreq_driver->target_index) { -unsigned int idx; - - idx =cpufreq_frequency_table_target (policy, target_freq, - relation); - policy->cached_resolved_idx = idx; -return policy->freq_table[idx].frequency; - } + target_freq =clamp_val (target_freq, policy->min, policy->max); -if (cpufreq_driver->resolve_freq) -return cpufreq_driver->resolve_freq (policy, target_freq); +if (!cpufreq_driver->target_index) +return target_freq; -return target_freq; + idx =cpufreq_frequency_table_target (policy, target_freq, relation); + policy->cached_resolved_idx = idx; + policy->cached_target_freq = target_freq; +return policy->freq_table[idx].frequency; }/**
include/linux/cpufreq.h +0 -9
@@ -329,15 +329,6 @@ struct cpufreq_driver {unsigned int (*fast_switch)(struct cpufreq_policy *policy,unsigned int target_freq); -/* -* Caches and returns the lowest driver-supported frequency greater than -* or equal to the target frequency, subject to any driver limitations. -* Does not set the frequency. Only to be implemented for drivers with -* target(). -*/ -unsigned int (*resolve_freq)(struct cpufreq_policy *policy, -unsigned int target_freq); -/* * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION * unset.
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 cpufreq: Cache post-resolution target frequency in __resolve_freq()
In reply to Sasha LevinFrom: "Rafael J. Wysocki" <[email protected]>
[ Upstream commit 166207cac45191637c47a457a5c16ca6b6773a16 ]
get_next_freq() in the schedutil governor returns the resolved frequency from cpufreq_driver_resolve_freq(), and that resolved value is then passed to cpufreq_driver_fast_switch(). The only reader of cached_target_freq, acpi_cpufreq_fast_switch(), compares it with the post-resolution target frequency, but it holds the pre-resolution clamped frequency, so any request that falls between table entries misses the cache and falls back to a table scan.
Address this by making __resolve_freq() store the post-resolution frequency in cached_target_freq.
--- drivers/cpufreq/cpufreq.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cpufreq/cpufreq.c +2 -2
@@ -541,8 +541,8 @@ static unsigned int __resolve_freq(struct cpufreq_policy *policy, idx =cpufreq_frequency_table_target (policy, target_freq, relation); policy->cached_resolved_idx = idx; - policy->cached_target_freq = target_freq; -return policy->freq_table[idx].frequency; + policy->cached_target_freq = policy->freq_table[idx].frequency; +return policy->cached_target_freq; }/**
Signature · 1 line
2.53.0