Skip to content

Commit 8a3fc32

Browse files
rafaeljwgregkh
authored andcommitted
cpufreq: ACPI: Extend frequency tables to cover boost frequencies
commit 3c55e94 upstream. A severe performance regression on AMD EPYC processors when using the schedutil scaling governor was discovered by Phoronix.com and attributed to the following commits: 41ea667 ("x86, sched: Calculate frequency invariance for AMD systems") 976df7e ("x86, sched: Use midpoint of max_boost and max_P for frequency invariance on AMD EPYC") The source of the problem is that the maximum performance level taken for computing the arch_max_freq_ratio value used in the x86 scale- invariance code is higher than the one corresponding to the cpuinfo.max_freq value coming from the acpi_cpufreq driver. This effectively causes the scale-invariant utilization to fall below 100% even if the CPU runs at cpuinfo.max_freq or slightly faster, so the schedutil governor selects a frequency below cpuinfo.max_freq then. That frequency corresponds to a frequency table entry below the maximum performance level necessary to get to the "boost" range of CPU frequencies. However, if the cpuinfo.max_freq value coming from acpi_cpufreq was higher, the schedutil governor would select higher frequencies which in turn would allow acpi_cpufreq to set more adequate performance levels and to get to the "boost" range of CPU frequencies more often. This issue affects any systems where acpi_cpufreq is used and the "boost" (or "turbo") frequencies are enabled, not just AMD EPYC. Moreover, commit db86527 ("cpufreq: Avoid configuring old governors as default with intel_pstate") from the 5.10 development cycle made it extremely easy to default to schedutil even if the preferred driver is acpi_cpufreq as long as intel_pstate is built too, because the mere presence of the latter effectively removes the ondemand governor from the defaults. Distro kernels are likely to include both intel_pstate and acpi_cpufreq on x86, so their users who cannot use intel_pstate or choose to use acpi_cpufreq may easily be affectecd by this issue. To address this issue, extend the frequency table constructed by acpi_cpufreq for each CPU to cover the entire range of available frequencies (including the "boost" ones) if CPPC is available and indicates that "boost" (or "turbo") frequencies are enabled. That causes cpuinfo.max_freq to become the maximum "boost" frequency of the given CPU (instead of the maximum frequency returned by the ACPI _PSS object that corresponds to the "nominal" performance level). Fixes: 41ea667 ("x86, sched: Calculate frequency invariance for AMD systems") Fixes: 976df7e ("x86, sched: Use midpoint of max_boost and max_P for frequency invariance on AMD EPYC") Fixes: db86527 ("cpufreq: Avoid configuring old governors as default with intel_pstate") Link: https://www.phoronix.com/scan.php?page=article&item=linux511-amd-schedutil&num=1 Link: https://lore.kernel.org/linux-pm/20210203135321.12253-2-ggherdovich@suse.cz/ Reported-by: Michael Larabel <Michael@phoronix.com> Diagnosed-by: Giovanni Gherdovich <ggherdovich@suse.cz> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Tested-by: Giovanni Gherdovich <ggherdovich@suse.cz> Reviewed-by: Giovanni Gherdovich <ggherdovich@suse.cz> Tested-by: Michael Larabel <Michael@phoronix.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c930943 commit 8a3fc32

1 file changed

Lines changed: 95 additions & 12 deletions

File tree

‎drivers/cpufreq/acpi-cpufreq.c‎

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/uaccess.h>
2727

2828
#include <acpi/processor.h>
29+
#include <acpi/cppc_acpi.h>
2930

3031
#include <asm/msr.h>
3132
#include <asm/processor.h>
@@ -53,6 +54,7 @@ struct acpi_cpufreq_data {
5354
unsigned int resume;
5455
unsigned int cpu_feature;
5556
unsigned int acpi_perf_cpu;
57+
unsigned int first_perf_state;
5658
cpumask_var_t freqdomain_cpus;
5759
void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val);
5860
u32 (*cpu_freq_read)(struct acpi_pct_register *reg);
@@ -221,10 +223,10 @@ static unsigned extract_msr(struct cpufreq_policy *policy, u32 msr)
221223

222224
perf = to_perf_data(data);
223225

224-
cpufreq_for_each_entry(pos, policy->freq_table)
226+
cpufreq_for_each_entry(pos, policy->freq_table + data->first_perf_state)
225227
if (msr == perf->states[pos->driver_data].status)
226228
return pos->frequency;
227-
return policy->freq_table[0].frequency;
229+
return policy->freq_table[data->first_perf_state].frequency;
228230
}
229231

230232
static unsigned extract_freq(struct cpufreq_policy *policy, u32 val)
@@ -363,6 +365,7 @@ static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
363365
struct cpufreq_policy *policy;
364366
unsigned int freq;
365367
unsigned int cached_freq;
368+
unsigned int state;
366369

367370
pr_debug("%s (%d)\n", __func__, cpu);
368371

@@ -374,7 +377,11 @@ static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
374377
if (unlikely(!data || !policy->freq_table))
375378
return 0;
376379

377-
cached_freq = policy->freq_table[to_perf_data(data)->state].frequency;
380+
state = to_perf_data(data)->state;
381+
if (state < data->first_perf_state)
382+
state = data->first_perf_state;
383+
384+
cached_freq = policy->freq_table[state].frequency;
378385
freq = extract_freq(policy, get_cur_val(cpumask_of(cpu), data));
379386
if (freq != cached_freq) {
380387
/*
@@ -628,16 +635,54 @@ static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
628635
}
629636
#endif
630637

638+
#ifdef CONFIG_ACPI_CPPC_LIB
639+
static u64 get_max_boost_ratio(unsigned int cpu)
640+
{
641+
struct cppc_perf_caps perf_caps;
642+
u64 highest_perf, nominal_perf;
643+
int ret;
644+
645+
if (acpi_pstate_strict)
646+
return 0;
647+
648+
ret = cppc_get_perf_caps(cpu, &perf_caps);
649+
if (ret) {
650+
pr_debug("CPU%d: Unable to get performance capabilities (%d)\n",
651+
cpu, ret);
652+
return 0;
653+
}
654+
655+
highest_perf = perf_caps.highest_perf;
656+
nominal_perf = perf_caps.nominal_perf;
657+
658+
if (!highest_perf || !nominal_perf) {
659+
pr_debug("CPU%d: highest or nominal performance missing\n", cpu);
660+
return 0;
661+
}
662+
663+
if (highest_perf < nominal_perf) {
664+
pr_debug("CPU%d: nominal performance above highest\n", cpu);
665+
return 0;
666+
}
667+
668+
return div_u64(highest_perf << SCHED_CAPACITY_SHIFT, nominal_perf);
669+
}
670+
#else
671+
static inline u64 get_max_boost_ratio(unsigned int cpu) { return 0; }
672+
#endif
673+
631674
static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
632675
{
633-
unsigned int i;
634-
unsigned int valid_states = 0;
635-
unsigned int cpu = policy->cpu;
676+
struct cpufreq_frequency_table *freq_table;
677+
struct acpi_processor_performance *perf;
636678
struct acpi_cpufreq_data *data;
679+
unsigned int cpu = policy->cpu;
680+
struct cpuinfo_x86 *c = &cpu_data(cpu);
681+
unsigned int valid_states = 0;
637682
unsigned int result = 0;
638-
struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
639-
struct acpi_processor_performance *perf;
640-
struct cpufreq_frequency_table *freq_table;
683+
unsigned int state_count;
684+
u64 max_boost_ratio;
685+
unsigned int i;
641686
#ifdef CONFIG_SMP
642687
static int blacklisted;
643688
#endif
@@ -750,8 +795,20 @@ static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
750795
goto err_unreg;
751796
}
752797

753-
freq_table = kcalloc(perf->state_count + 1, sizeof(*freq_table),
754-
GFP_KERNEL);
798+
state_count = perf->state_count + 1;
799+
800+
max_boost_ratio = get_max_boost_ratio(cpu);
801+
if (max_boost_ratio) {
802+
/*
803+
* Make a room for one more entry to represent the highest
804+
* available "boost" frequency.
805+
*/
806+
state_count++;
807+
valid_states++;
808+
data->first_perf_state = valid_states;
809+
}
810+
811+
freq_table = kcalloc(state_count, sizeof(*freq_table), GFP_KERNEL);
755812
if (!freq_table) {
756813
result = -ENOMEM;
757814
goto err_unreg;
@@ -785,6 +842,30 @@ static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
785842
valid_states++;
786843
}
787844
freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
845+
846+
if (max_boost_ratio) {
847+
unsigned int state = data->first_perf_state;
848+
unsigned int freq = freq_table[state].frequency;
849+
850+
/*
851+
* Because the loop above sorts the freq_table entries in the
852+
* descending order, freq is the maximum frequency in the table.
853+
* Assume that it corresponds to the CPPC nominal frequency and
854+
* use it to populate the frequency field of the extra "boost"
855+
* frequency entry.
856+
*/
857+
freq_table[0].frequency = freq * max_boost_ratio >> SCHED_CAPACITY_SHIFT;
858+
/*
859+
* The purpose of the extra "boost" frequency entry is to make
860+
* the rest of cpufreq aware of the real maximum frequency, but
861+
* the way to request it is the same as for the first_perf_state
862+
* entry that is expected to cover the entire range of "boost"
863+
* frequencies of the CPU, so copy the driver_data value from
864+
* that entry.
865+
*/
866+
freq_table[0].driver_data = freq_table[state].driver_data;
867+
}
868+
788869
policy->freq_table = freq_table;
789870
perf->state = 0;
790871

@@ -858,8 +939,10 @@ static void acpi_cpufreq_cpu_ready(struct cpufreq_policy *policy)
858939
{
859940
struct acpi_processor_performance *perf = per_cpu_ptr(acpi_perf_data,
860941
policy->cpu);
942+
struct acpi_cpufreq_data *data = policy->driver_data;
943+
unsigned int freq = policy->freq_table[data->first_perf_state].frequency;
861944

862-
if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
945+
if (perf->states[0].core_frequency * 1000 != freq)
863946
pr_warn(FW_WARN "P-state 0 is not max freq\n");
864947
}
865948

0 commit comments

Comments
 (0)