Skip to content

Commit febce40

Browse files
committed
intel_pstate: Avoid extra invocation of intel_pstate_sample()
The initialization of intel_pstate for a given CPU involves populating the fields of its struct cpudata that represent the previous sample, but currently that is done in a problematic way. Namely, intel_pstate_init_cpu() makes an extra call to intel_pstate_sample() so it reads the current register values that will be used to populate the "previous sample" record during the next invocation of intel_pstate_sample(). However, after commit a4675fb (cpufreq: intel_pstate: Replace timers with utilization update callbacks) that doesn't work for last_sample_time, because the time value is passed to intel_pstate_sample() as an argument now. Passing 0 to it from intel_pstate_init_cpu() is problematic, because that causes cpu->last_sample_time == 0 to be visible in get_target_pstate_use_performance() (and hence the extra cpu->last_sample_time > 0 check in there) and effectively allows the first invocation of intel_pstate_sample() from intel_pstate_update_util() to happen immediately after the initialization which may lead to a significant "turn on" effect in the governor algorithm. To mitigate that issue, rework the initialization to avoid the extra intel_pstate_sample() call from intel_pstate_init_cpu(). Instead, make intel_pstate_sample() return false if it has been called with cpu->sample.time equal to zero, which will make intel_pstate_update_util() skip the sample in that case, and reset cpu->sample.time from intel_pstate_set_update_util_hook() to make the algorithm start properly every time the hook is set. Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent bb6ab52 commit febce40

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

drivers/cpufreq/intel_pstate.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,14 @@ static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
910910
cpu->prev_aperf = aperf;
911911
cpu->prev_mperf = mperf;
912912
cpu->prev_tsc = tsc;
913-
return true;
913+
/*
914+
* First time this function is invoked in a given cycle, all of the
915+
* previous sample data fields are equal to zero or stale and they must
916+
* be populated with meaningful numbers for things to work, so assume
917+
* that sample.time will always be reset before setting the utilization
918+
* update hook and make the caller skip the sample then.
919+
*/
920+
return !!cpu->last_sample_time;
914921
}
915922

916923
static inline int32_t get_avg_frequency(struct cpudata *cpu)
@@ -984,8 +991,7 @@ static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
984991
* enough period of time to adjust our busyness.
985992
*/
986993
duration_ns = cpu->sample.time - cpu->last_sample_time;
987-
if ((s64)duration_ns > pid_params.sample_rate_ns * 3
988-
&& cpu->last_sample_time > 0) {
994+
if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
989995
sample_ratio = div_fp(int_tofp(pid_params.sample_rate_ns),
990996
int_tofp(duration_ns));
991997
core_busy = mul_fp(core_busy, sample_ratio);
@@ -1100,7 +1106,6 @@ static int intel_pstate_init_cpu(unsigned int cpunum)
11001106
intel_pstate_get_cpu_pstates(cpu);
11011107

11021108
intel_pstate_busy_pid_reset(cpu);
1103-
intel_pstate_sample(cpu, 0);
11041109

11051110
cpu->update_util.func = intel_pstate_update_util;
11061111

@@ -1121,9 +1126,13 @@ static unsigned int intel_pstate_get(unsigned int cpu_num)
11211126
return get_avg_frequency(cpu);
11221127
}
11231128

1124-
static void intel_pstate_set_update_util_hook(unsigned int cpu)
1129+
static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
11251130
{
1126-
cpufreq_set_update_util_data(cpu, &all_cpu_data[cpu]->update_util);
1131+
struct cpudata *cpu = all_cpu_data[cpu_num];
1132+
1133+
/* Prevent intel_pstate_update_util() from using stale data. */
1134+
cpu->sample.time = 0;
1135+
cpufreq_set_update_util_data(cpu_num, &cpu->update_util);
11271136
}
11281137

11291138
static void intel_pstate_clear_update_util_hook(unsigned int cpu)

0 commit comments

Comments
 (0)