Skip to contentSkip to conversation

cpufreq, intel_pstate, Fix rounding errors

PATCH In review Prarit Bhargava 4 participants 7 messages Nov 20, 2015 – Nov 23, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover cpufreq, intel_pstate, Fix rounding errors

I have a Intel (6,63) processor with a "marketing" frequency (from /proc/cpuinfo) of 2100MHz, and a max turbo frequency of 2600MHz. I can execute

cpupower frequency-set -g powersave --min 1200MHz --max 2100MHz

and the max_freq_pct is set to 80. When adding load to the system I noticed that the cpu frequency only reached 2000MHZ and not 2100MHz as expected.

I wrote a little test program to set the frequencies in decrements of 100MHz and compared the targeted frequency (the frequency set through the cpupower command) and the actual frequency (from /proc/cpuinfo), as well as dumping out the value of the MSR_IA32_PERF_CTL.

Target Achieved Difference MSR(0x199)

 3300        2900          -400          0x1e00
 3200        2900          -300          0x1e00
 3100        2900          -200          0x1e00
 3000        2900          -100          0x1d00
 2900        2800          -100          0x1c00
 2800        2700          -100          0x1b00
 2700        2600          -100          0x1a00
 2600        2500          -100          0x1900
 2500        2400          -100          0x1800
 2400        2300          -100          0x1700
 2300        2200          -100          0x1600
 2200        2100          -100          0x1500
 2100        2000          -100          0x1400
 2000        1900          -100          0x1300
 1900        1800          -100          0x1200
 1800        1700          -100          0x1100
 1700        1600          -100          0x1000
 1600        1500          -100          0xf00
 1500        1400          -100          0xe00
 1400        1300          -100          0xd00
 1300        1200          -100          0xc00
 1200        1200             0          0xc00

As can be seen the frequencies are consistently off by 100MHz. After some examination I found a rounding error in intel_pstate_set_policy() for the calculation of limits->max_policy_pct which needs to be rounded up to the nearest percentage point. For example, setting a frequency of 2100MHz on this system results in limits->max_policy_pct = ((2100 * 100) / 2600) = 80.
However, ((2100 * 100) / 2600) is actually 80.7, or 81. This is fixed
by expanding the calculation an extra decimal point and rounding to the nearest percentage point.

A second rounding error was found in the calculation of limits->max_perf in intel_pstate_set_policy(), which is used to calculate the max and min pstate values in intel_pstate_get_min_max(). In this case, limits->max_perf is truncated to 2 hex digits such that, for example, 0x169 was incorrectly truncated to 0x16 instead of 0x17. This resulted in the pstate being set one level too low.

After applying these two fixes we consistently reach the targeted frequency.

Target Achieved Difference MSR(0x199)

 3300        2900          -400          0x1e00
 3200        2900          -300          0x1e00
 3100        2900          -200          0x1e00
 3000        2900          -100          0x1d00
 2900        2900             0          0x1d00
 2800        2800             0          0x1c00
 2700        2700             0          0x1b00
 2600        2600             0          0x1a00
 2500        2500             0          0x1900
 2400        2400             0          0x1800
 2300        2300             0          0x1700
 2200        2200             0          0x1600
 2100        2100             0          0x1500
 2000        2000             0          0x1400
 1900        1900             0          0x1300
 1800        1800             0          0x1200
 1700        1700             0          0x1100
 1600        1600             0          0x1000
 1500        1500             0          0xf00
 1400        1400             0          0xe00
 1300        1300             0          0xd00
 1200        1200             0          0xc00

Additional tests were run on a (6,78) with HWP enabled and a (6,79) system. Testing on both systems showed that the problem was resolved.

Cc Srinivas Pandruvada <[email protected]>
Cc Len Brown <[email protected]>
Cc Alexandra Yates <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>

[v2]: Separate into two patches, and rebase onto git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next [v3]: calculate limits->max_policy_pct once

Prarit Bhargava (2):
cpufreq, intel_pstate, Fix limits->max_policy_pct rounding error cpufreq, intel_pstate, fix limits->max_perf rounding error

 drivers/cpufreq/intel_pstate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v3 1/2 cpufreq, intel_pstate, Fix limits->max_policy_pct rounding error

I have a Intel (6,63) processor with a "marketing" frequency (from /proc/cpuinfo) of 2100MHz, and a max turbo frequency of 2600MHz. I can execute

cpupower frequency-set -g powersave --min 1200MHz --max 2100MHz

and the max_freq_pct is set to 80. When adding load to the system I noticed that the cpu frequency only reached 2000MHZ and not 2100MHz as expected.

This is because limits->max_policy_pct is calculated as 2100 * 100 /2600 = 80.7 and is rounded down to 80 when it should be rounded up to 81. This patch adds a DIV_ROUND_UP() which will return the correct value.

Cc Srinivas Pandruvada <[email protected]>
Cc Len Brown <[email protected]>
Cc Alexandra Yates <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
---
 drivers/cpufreq/intel_pstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/intel_pstate.c +2 -1
index 001a532..eb75053 100644
@@ -1108,7 +1108,8 @@ static int intel_pstate_set_policy(struct cpufreq_policy *policy)
 	limits = &powersave_limits;
 	limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
 	limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
-	limits->max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
+	limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
+					      policy->cpuinfo.max_freq);
 	limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
 
 	/* Normalize user input to [min_policy_pct, max_policy_pct] */
Signature · 1 line
1.8.3.1
Acked-by Srinivas Pandruvada on v3 1/2
#2 lore raw

Re: [PATCH 1/2] cpufreq, intel_pstate, Fix limits->max_policy_pct rounding error

Quoted 13 lines · Prarit Bhargava

I have a Intel (6,63) processor with a "marketing" frequency (from /proc/cpuinfo) of 2100MHz, and a max turbo frequency of 2600MHz. I can execute

cpupower frequency-set -g powersave --min 1200MHz --max 2100MHz

and the max_freq_pct is set to 80. When adding load to the system I noticed that the cpu frequency only reached 2000MHZ and not 2100MHz as expected.

This is because limits->max_policy_pct is calculated as 2100 * 100 /2600 = 80.7 and is rounded down to 80 when it should be rounded up to 81. This patch adds a DIV_ROUND_UP() which will return the correct value.

Quoted 8 lines
Cc Srinivas Pandruvada <[email protected]>
Cc Len Brown <[email protected]>
Cc Alexandra Yates <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
Quoted 18 lines
---
 drivers/cpufreq/intel_pstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/intel_pstate.c +2 -1
index 001a532..eb75053 100644
@@ -1108,7 +1108,8 @@ static int intel_pstate_set_policy(struct cpufreq_policy *policy)
 	limits = &powersave_limits;
 	limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
 	limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
-	limits->max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
+	limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
+					      policy->cpuinfo.max_freq);
 	limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
 
 	/* Normalize user input to [min_policy_pct, max_policy_pct] */
#3 lore raw

PATCH v3 2/2 cpufreq, intel_pstate, fix limits->max_perf rounding error

In reply to Prarit Bhargava

A rounding error was found in the calculation of limits->max_perf in intel_pstate_set_policy(), which is used to calculate the max and min pstate values in intel_pstate_get_min_max(). In that code, limits->max_perf is truncated to 2 hex digits such that, for example, 0x169 was incorrectly calculated to 0x16 instead of 0x17. This resulted in the pstate being set one level too low. This patch rounds the value of limits->max_perf up instead of down so that the correct max pstate can be reached.

Cc Srinivas Pandruvada <[email protected]>
Cc Len Brown <[email protected]>
Cc Alexandra Yates <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
---
 drivers/cpufreq/intel_pstate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/cpufreq/intel_pstate.c +1 -0
index eb75053..8ad1f95 100644
@@ -1121,6 +1121,7 @@ static int intel_pstate_set_policy(struct cpufreq_policy *policy)
 				   limits->max_sysfs_pct);
 	limits->max_perf_pct = max(limits->min_policy_pct,
 				   limits->max_perf_pct);
+	limits->max_perf = round_up(limits->max_perf, 8);
 
 	/* Make sure min_perf_pct <= max_perf_pct */
 	limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Signature · 1 line
1.8.3.1
Acked-by Srinivas Pandruvada on v3 2/2
#4 lore raw

Re: [PATCH 2/2] cpufreq, intel_pstate, fix limits->max_perf rounding error

Quoted 9 lines · Prarit Bhargava

A rounding error was found in the calculation of limits->max_perf in intel_pstate_set_policy(), which is used to calculate the max and min pstate values in intel_pstate_get_min_max(). In that code, limits->max_perf is truncated to 2 hex digits such that, for example, 0x169 was incorrectly calculated to 0x16 instead of 0x17. This resulted in the pstate being set one level too low. This patch rounds the value of limits->max_perf up instead of down so that the correct max pstate can be reached.

Quoted 8 lines
Cc Srinivas Pandruvada <[email protected]>
Cc Len Brown <[email protected]>
Cc Alexandra Yates <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Viresh Kumar <[email protected]>
Quoted 16 lines
---
 drivers/cpufreq/intel_pstate.c | 1 +
 1 file changed, 1 insertion(+)
drivers/cpufreq/intel_pstate.c +1 -0
index eb75053..8ad1f95 100644
@@ -1121,6 +1121,7 @@ static int intel_pstate_set_policy(struct cpufreq_policy *policy)
 				   limits->max_sysfs_pct);
 	limits->max_perf_pct = max(limits->min_policy_pct,
 				   limits->max_perf_pct);
+	limits->max_perf = round_up(limits->max_perf, 8);
 
 	/* Make sure min_perf_pct <= max_perf_pct */
 	limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
#6 lore raw
Quoted 5 lines · Viresh Kumar
Quoted 2 lines · Prarit Bhargava

[v2]: Separate into two patches, and rebase onto git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next [v3]: calculate limits->max_policy_pct once

Both patches applied, thanks!

Rafael