cpufreq: acpi-cpufreq: fix P-state index mismatch between perf->states[] and freq_table[]
v3 cover cpufreq: acpi-cpufreq: fix P-state index mismatch between perf->states[] and freq_table[]
From: Li RongQing <[email protected]>
perf->state is an index into perf->states[], but freq_table[] is built by skipping duplicate _PSS entries, so the two arrays no longer share an index space. Using perf->state to index freq_table[] therefore reads the wrong slot. This series fixes the two remaining places that do so.
Changes since v2:
- Reword patch 1;
- Rewrite patch 2, using perf->state to index perf->states[] (its native index space) instead of the deduplicated freq_table[]
- Fix the Fixes: tags to point at the actual commits that introduced each
Changes since v1:
- Reword patch 1: v1 described it as an out-of-bounds read, which is wrong; it is an index-mismatch / wrong-result bug, not a memory-safety issue.
- Add patch 2 for get_cur_freq_on_cpu().
- Add Fixes: tags (not exact, but reasonable backport targets).
Li RongQing (2):
cpufreq: acpi-cpufreq: fix P-state index mismatch in extract_io()
cpufreq: acpi-cpufreq: fix P-state index mismatch in
get_cur_freq_on_cpu()
drivers/cpufreq/acpi-cpufreq.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
Signature · 1 line
2.9.4
PATCH v3 1/2 cpufreq: acpi-cpufreq: fix P-state index mismatch in extract_io()
From: Li RongQing <[email protected]>
When policy->freq_table is built in acpi_cpufreq_cpu_init(), entries with duplicate frequencies are skipped. For each remaining entry, freq_table[].driver_data stores the original ACPI P-state index, so the index space of freq_table no longer matches perf->states[].
extract_io() currently walks perf->states[] with index i and uses the same i to index policy->freq_table[i]. When duplicate frequencies exist, this can associate a P-state status with the frequency of a different P-state. It can also access an invalid or sentinel entry in freq_table when the number of entries in perf->states[] is greater than the number of entries remaining in the frequency table.
extract_io() is used on ACPI_ADR_SPACE_SYSTEM_IO platforms by the frequency verification path, which is enabled by the acpi_pstate_strict module parameter. With the mismatched lookup, check_freqs() can fail to match the frequency of the P-state that drv_write() has already programmed. It then retries the check for all iterations before returning -EAGAIN, leaving perf->state at its previous value while the hardware is running at the newly requested P-state.
The cpufreq core consequently keeps policy->cur at the old frequency. Since __cpufreq_driver_target() returns early when the requested frequency equals policy->cur, the driver is not called again for that frequency and the control register is not rewritten. The hardware can therefore remain at a frequency that differs from the frequency known to the cpufreq core.
Fix this by iterating over policy->freq_table and using pos->driver_data as the original ACPI P-state index when accessing perf->states[], as extract_msr() already does.
--- drivers/cpufreq/acpi-cpufreq.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/acpi-cpufreq.c +4 -5
@@ -197,14 +197,13 @@ static unsigned extract_io(struct cpufreq_policy *policy, u32 value) {struct acpi_cpufreq_data *data = policy->driver_data;struct acpi_processor_performance *perf; -int i; +struct cpufreq_frequency_table *pos; perf =to_perf_data (data); -for (i =0 ; i < perf->state_count; i++) { -if (value == perf->states[i].status) -return policy->freq_table[i].frequency; - } +cpufreq_for_each_entry (pos, policy->freq_table) +if (value == perf->states[pos->driver_data].status) +return pos->frequency;return 0 ; }
Signature · 1 line
2.9.4
Re: [PATCH v3 1/2] cpufreq: acpi-cpufreq: fix P-state index mismatch in extract_io()
Quoted 4 lines · lirongqing
From: Li RongQing <[email protected]>
When
policy->freq_tableis built inacpi_cpufreq_cpu_init(), entries with duplicate frequencies are skipped. For each remaining entry,
To be precise for applying if needed.
"entries with duplicate frequencies are skipped.
-->
entries that are not lower in frequency than the previous one are skipped."
Quoted 10 lines
freq_table[].driver_data stores the original ACPI P-state index, so the index space of
freq_tableno longer matches perf->states[].
extract_io()currently walks perf->states[] with index i and uses the same i to index policy->freq_table[i]. When duplicate frequencies exist, this can associate a P-state status with the frequency of a different P-state. It can also access an invalid or sentinel entry infreq_tablewhen the number of entries in perf->states[] is greater than the number of entries remaining in the frequency table.
Quoted 6 lines
extract_io()is used onACPI_ADR_SPACE_SYSTEM_IOplatforms by the frequency verification path, which is enabled by theacpi_pstate_strictmodule parameter. With the mismatched lookup,check_freqs()can fail to match the frequency of the P-state thatdrv_write()has already programmed. It then retries the check for all iterations before returning-EAGAIN,
Small nit, just for applying if needed.
"It then retries the check for all iterations before returning -EAGAIN,"
-->
"It then sleeps through all 100 iterations - at least
~1 ms of usleep_range() plus 100 cross-CPU calls and I/O port reads, all with policy->rwsem held, and .target_index() returns -EAGAIN,"
leaving perf->state at its
Quoted 10 lines
previous value while the hardware is running at the newly requested P-state.
The cpufreq core consequently keeps
policy->curat the old frequency. Since__cpufreq_driver_target()returns early when the requested frequency equalspolicy->cur, the driver is not called again for that frequency and the control register is not rewritten. The hardware can therefore remain at a frequency that differs from the frequency known to the cpufreq core.
Quoted 6 lines
Fix this by iterating over
policy->freq_tableand usingpos->driver_dataas the original ACPI P-state index when accessing perf->states[], asextract_msr()already does.Fixes fe27cb358835 ("[CPUFREQ][2/8] acpi: reorganize code to make MSR support addition easier")Signed-off-by Li RongQing <[email protected]>
Quoted 27 lines
--- drivers/cpufreq/acpi-cpufreq.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/acpi-cpufreq.c +4
-5@@ -197,14 +197,13 @@ static unsigned extract_io(struct cpufreq_policy *policy, u32 value) {struct acpi_cpufreq_data *data = policy->driver_data;struct acpi_processor_performance *perf; -int i; +struct cpufreq_frequency_table *pos; perf =to_perf_data (data); -for (i =0 ; i < perf->state_count; i++) { -if (value == perf->states[i].status) -return policy->freq_table[i].frequency; - } +cpufreq_for_each_entry (pos, policy->freq_table) +if (value == perf->states[pos->driver_data].status) +return pos->frequency;return 0 ; }
Signature · 2 lines
Thx and BRs, Zhongqiu Han
PATCH v3 2/2 cpufreq: acpi-cpufreq: fix P-state index mismatch in get_cur_freq_on_cpu()
In reply to lirongqingFrom: Li RongQing <[email protected]>
get_cur_freq_on_cpu() uses perf->state, which is an index into perf->states[], to index policy->freq_table[]. However, freq_table[] is built by filtering _PSS entries with duplicate frequencies, so its index space no longer matches perf->states[]. The original P-state index for each remaining freq_table entry is stored in driver_data.
Once an entry has been skipped, using perf->state as an index into freq_table[] can therefore select the frequency of a different P-state.
The reported current frequency itself remains correct because it is obtained from extract_freq(). The mismatch only affects the cached frequency used by get_cur_freq_on_cpu() to detect a firmware frequency change behind our back, through data->resume.
If the wrong table entry contains a frequency different from the one the CPU is actually running at, the check falsely detects a frequency change and sets data->resume. The next ->target() call then performs a redundant control-register write even if the requested P-state is already the current P-state.
Conversely, if the wrong table entry happens to contain the frequency to which firmware has changed the CPU, the frequency change is missed and data->resume remains clear. A subsequent ->target() call for the P-state that the cpufreq core believes to be current can then short-circuit without rewriting the control register, leaving the CPU at the firmware-selected frequency until a different P-state is requested.
Fix this by taking the cached frequency directly from perf->states[perf->state].core_frequency. perf->state and perf->states[] use the same P-state index space, and converting core_frequency to kHz yields the same value stored in the corresponding freq_table entry during initialization.
--- drivers/cpufreq/acpi-cpufreq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpufreq/acpi-cpufreq.c +4 -1
@@ -353,6 +353,7 @@ static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *datstatic unsigned int get_cur_freq_on_cpu (unsigned int cpu) { +struct acpi_processor_performance *perf;struct acpi_cpufreq_data *data;struct cpufreq_policy *policy;unsigned int freq;
@@ -368,7 +369,9 @@ static unsigned int get_cur_freq_on_cpu(unsigned int cpu)if (unlikely (!data || !policy->freq_table))return 0 ; - cached_freq = policy->freq_table[to_perf_data (data)->state].frequency; + perf =to_perf_data (data); + cached_freq = perf->states[perf->state].core_frequency *1000 ; + freq =extract_freq (policy,get_cur_val (cpumask_of (cpu), data));if (freq != cached_freq) {/*
Signature · 1 line
2.9.4
Re: [PATCH v3 2/2] cpufreq: acpi-cpufreq: fix P-state index mismatch in get_cur_freq_on_cpu()
Quoted 5 lines · lirongqing
From: Li RongQing <[email protected]>
get_cur_freq_on_cpu()usesperf->state, which is an index into perf->states[], to index policy->freq_table[]. However, freq_table[] is built by filtering_PSSentries with duplicate frequencies, so its
To be precise for applying if needed.
"However, freq_table[] is built by filtering _PSS entries with duplicate frequencies"
-->
"However, freq_table[] is built by filtering _PSS entries that are not lower in frequency than the previous one."
Quoted 11 lines
index space no longer matches perf->states[]. The original P-state index for each remaining
freq_tableentry is stored indriver_data.Once an entry has been skipped, using
perf->stateas an index into freq_table[] can therefore select the frequency of a different P-state.The reported current frequency itself remains correct because it is obtained from
extract_freq(). The mismatch only affects the cached frequency used byget_cur_freq_on_cpu()to detect a firmware frequency change behind our back, throughdata->resume.
Just for applying if needed, deleting "through data->resume."
Quoted 21 lines
If the wrong table entry contains a frequency different from the one the CPU is actually running at, the check falsely detects a frequency change and sets
data->resume. The next ->target() call then performs a redundant control-register write even if the requested P-state is already the current P-state.Conversely, if the wrong table entry happens to contain the frequency to which firmware has changed the CPU, the frequency change is missed and
data->resumeremains clear. A subsequent ->target() call for the P-state that the cpufreq core believes to be current can then short-circuit without rewriting the control register, leaving the CPU at the firmware-selected frequency until a different P-state is requested.Fix this by taking the cached frequency directly from perf->states[perf->state].core_frequency.
perf->stateand perf->states[] use the same P-state index space, and convertingcore_frequencyto kHz yields the same value stored in the correspondingfreq_tableentry during initialization.
Quoted 4 lines
Fixes e56a727b023d ("[CPUFREQ] Make acpi-cpufreq more robust against BIOS freq changes behind our back.")Reported-by Zhongqiu Han <[email protected]>Suggested-by Zhongqiu Han <[email protected]>Signed-off-by Li RongQing <[email protected]>
Quoted 27 lines
--- drivers/cpufreq/acpi-cpufreq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/cpufreq/acpi-cpufreq.c +4
-1@@ -353,6 +353,7 @@ static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *datstatic unsigned int get_cur_freq_on_cpu (unsigned int cpu) { +struct acpi_processor_performance *perf;struct acpi_cpufreq_data *data;struct cpufreq_policy *policy;unsigned int freq;@@ -368,7 +369,9 @@ static unsigned int get_cur_freq_on_cpu(unsigned int cpu)if (unlikely (!data || !policy->freq_table))return 0 ; - cached_freq = policy->freq_table[to_perf_data (data)->state].frequency; + perf =to_perf_data (data); + cached_freq = perf->states[perf->state].core_frequency *1000 ; + freq =extract_freq (policy,get_cur_val (cpumask_of (cpu), data));if (freq != cached_freq) {/*
Signature · 2 lines
Thx and BRs, Zhongqiu Han