cpufreq: governor: Fix overflow when calculating idle time
PATCH v3 1/1 cpufreq: governor: Fix overflow when calculating idle time
It was reported that after Commit 0df35026c6a5 ("cpufreq: governor:
Fix negative idle_time when configured with CONFIG_HZ_PERIODIC"), cpufreq ondemand governor started to act oddly. Without any load, with freshly booted system, it pumped cpu frequency up to maximum at some point of time and stayed there. The problem is caused by jiffies overflow in get_cpu_idle_time:
After booting up 5 minutes, the jiffies will round up to zero. As a result, the following condition in cpu governor will always be true:
if (cur_idle_time <= j_cdbs->prev_cpu_idle) idle_time = 0;
which caused problems.
For example, once cur_idle_time has rounded up to zero, meanwhile prev_cpu_idle still remains negative(because of jiffies initial value of -300HZ, which is very big after converted to unsigned), thus above condition is met, thus we get a zero of idle running time during this sample, which causes a high busy time, thus governor always requests for the highest freq.
This patch fixes this problem by updating prev_cpu_idle for each sample period, even if prev_cpu_idle is bigger than cur_idle_time, thus to prevent the scenario of 'prev_cpu_idle always bigger than cur_idle_time' from happening.
---
v3:
- Do not use INITIAL_JIFFIES because it should be transparent
to user, meanwhile keep original semanteme to use delta
of time slice.
---
v2:
- Send this patch to a wider scope, including timing-system maintainers, as well as some modifications in the commit message to make it more clear.
--- drivers/cpufreq/cpufreq.c | 4 ++++ drivers/cpufreq/cpufreq_governor.c | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +4 -0
@@ -132,6 +132,10 @@ struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) }EXPORT_SYMBOL_GPL (cpufreq_frequency_get_table); +/** +* The wall time and idle time are both possible to round up, +* people should use delta rather than the value itself. +*/ static inline u64 get_cpu_idle_time_jiffy (unsigned int cpu,u64 *wall) {u64 idle_time;
drivers/cpufreq/cpufreq_governor.c +7 -1
@@ -197,8 +197,14 @@ unsigned int dbs_update(struct cpufreq_policy *policy) idle_time =0 ; }else { idle_time = cur_idle_time - j_cdbs->prev_cpu_idle; - j_cdbs->prev_cpu_idle = cur_idle_time; } +/* +* It is possible prev_cpu_idle being bigger than cur_idle_time, +* when 32bit rounds up if !CONFIG_VIRT_CPU_ACCOUNTING, +* thus get a 0% idle estimation. So update prev_cpu_idle during +* each sample period to avoid this situation lasting too long. +*/ + j_cdbs->prev_cpu_idle = cur_idle_time;if (ignore_nice) {u64 cur_nice =kcpustat_cpu (j).cpustat[CPUTIME_NICE ];
Signature · 1 line
1.8.4.2
Quoted 22 lines · Chen Yu
It was reported that after Commit
0df35026c6a5("cpufreq: governor:
Fix negativeidle_timewhen configured withCONFIG_HZ_PERIODIC"), cpufreq ondemand governor started to act oddly. Without any load, with freshly booted system, it pumped cpu frequency up to maximum at some point of time and stayed there. The problem is caused by jiffies overflow inget_cpu_idle_time:After booting up 5 minutes, the jiffies will round up to zero. As a result, the following condition in cpu governor will always be true:
if (cur_idle_time <= j_cdbs->prev_cpu_idle) idle_time = 0;which caused problems.
For example, once
cur_idle_timehas rounded up to zero, meanwhileprev_cpu_idlestill remainsnegative(because of jiffies initial value of -300HZ, which is very big after converted to unsigned), thus above condition is met, thus we get a zero of idle running time during this sample, which causes a high busy time, thus governor always requests for the highest freq.
Quoted 8 lines
This patch fixes this problem by updating
prev_cpu_idlefor each sample period, even ifprev_cpu_idleis bigger thancur_idle_time, thus to prevent the scenario of 'prev_cpu_idlealways bigger thancur_idle_time' from happening.Reported-by Timo Valtoaho <[email protected]>Signed-off-by Chen Yu <[email protected]>
This looks better than the previous versions to me, but ->
Quoted 19 lines
---
v3:
- Do not useINITIAL_JIFFIESbecause it should be transparent
to user, meanwhile keep original semanteme to use delta
of time slice.
---
v2:
- Send this patch to a wider scope, including timing-system maintainers, as well as some modifications in the commit message to make it more clear.--- drivers/cpufreq/cpufreq.c | 4 ++++ drivers/cpufreq/cpufreq_governor.c | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/cpufreq/cpufreq.c +0
-0
drivers/cpufreq/cpufreq.c struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) · line 136
Quoted 5 lines
drivers/cpufreq/cpufreq.c +2
-0@@ -132,6 +132,10 @@ struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) }EXPORT_SYMBOL_GPL (cpufreq_frequency_get_table); +/** +* The wall time and idle time are both possible to round up,
That's difficult to parse. I guess you wanted to say that they may overflow?
drivers/cpufreq/cpufreq.c struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) · line 138
Quoted 2 lines
drivers/cpufreq/cpufreq.c +2
-0+* people should use delta rather than the value itself. +*/
-> this new comment doesn't really belong to the fix. You can send a separate patch adding it.
Moreover -->
Quoted 10 lines
drivers/cpufreq/cpufreq.c +0
-0static inline u64 get_cpu_idle_time_jiffy (unsigned int cpu,u64 *wall) {u64 idle_time;drivers/cpufreq/cpufreq_governor.c +0
-0@@ -197,8 +197,14 @@ unsigned int dbs_update(struct cpufreq_policy *policy) idle_time =0 ; }else {
drivers/cpufreq/cpufreq_governor.c unsigned int dbs_update(struct cpufreq_policy *policy) · line 207
Quoted 10 lines
drivers/cpufreq/cpufreq_governor.c +7
-1idle_time = cur_idle_time - j_cdbs->prev_cpu_idle; - j_cdbs->prev_cpu_idle = cur_idle_time; } +/* +* It is possible prev_cpu_idle being bigger than cur_idle_time, +* when 32bit rounds up if !CONFIG_VIRT_CPU_ACCOUNTING, +* thus get a 0% idle estimation. So update prev_cpu_idle during +* each sample period to avoid this situation lasting too long. +*/ + j_cdbs->prev_cpu_idle = cur_idle_time;
--> it looks like the bug is that we are comparing signed values as unsigned.
drivers/cpufreq/cpufreq_governor.c unsigned int dbs_update(struct cpufreq_policy *policy) · line 210
Quoted 4 lines
drivers/cpufreq/cpufreq_governor.c +0
-0if (ignore_nice) {u64 cur_nice =kcpustat_cpu (j).cpustat[CPUTIME_NICE ];
So what about the simple change below?
--- drivers/cpufreq/cpufreq_governor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq_governor.c +1 -1
@@ -146,7 +146,7 @@ unsigned int dbs_update(struct cpufreq_p wall_time = cur_wall_time - j_cdbs->prev_cpu_wall; j_cdbs->prev_cpu_wall = cur_wall_time; -if (cur_idle_time <= j_cdbs->prev_cpu_idle) { +if ((s64 )cur_idle_time <= (s64 )j_cdbs->prev_cpu_idle) { idle_time =0 ; }else { idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
Rafael J. Wysocki
maintainerQuoted 93 lines · Rafael J. Wysocki
Quoted 22 lines · Chen Yu
It was reported that after Commit
0df35026c6a5("cpufreq: governor:
Fix negativeidle_timewhen configured withCONFIG_HZ_PERIODIC"), cpufreq ondemand governor started to act oddly. Without any load, with freshly booted system, it pumped cpu frequency up to maximum at some point of time and stayed there. The problem is caused by jiffies overflow inget_cpu_idle_time:After booting up 5 minutes, the jiffies will round up to zero. As a result, the following condition in cpu governor will always be true:
if (cur_idle_time <= j_cdbs->prev_cpu_idle) idle_time = 0;which caused problems.
For example, once
cur_idle_timehas rounded up to zero, meanwhileprev_cpu_idlestill remainsnegative(because of jiffies initial value of -300HZ, which is very big after converted to unsigned), thus above condition is met, thus we get a zero of idle running time during this sample, which causes a high busy time, thus governor always requests for the highest freq.Quoted 8 lines
This patch fixes this problem by updating
prev_cpu_idlefor each sample period, even ifprev_cpu_idleis bigger thancur_idle_time, thus to prevent the scenario of 'prev_cpu_idlealways bigger thancur_idle_time' from happening.Reported-by Timo Valtoaho <[email protected]>Signed-off-by Chen Yu <[email protected]>This looks better than the previous versions to me, but ->
Quoted 19 lines
---
v3:
- Do not useINITIAL_JIFFIESbecause it should be transparent
to user, meanwhile keep original semanteme to use delta
of time slice.
---
v2:
- Send this patch to a wider scope, including timing-system maintainers, as well as some modifications in the commit message to make it more clear.--- drivers/cpufreq/cpufreq.c | 4 ++++ drivers/cpufreq/cpufreq_governor.c | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/cpufreq/cpufreq.c +0
-0drivers/cpufreq/cpufreq.c struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) · line 136
Quoted 5 lines
drivers/cpufreq/cpufreq.c +2
-0@@ -132,6 +132,10 @@ struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) }EXPORT_SYMBOL_GPL (cpufreq_frequency_get_table); +/** +* The wall time and idle time are both possible to round up, That's difficult to parse. I guess you wanted to say that they may overflow?
drivers/cpufreq/cpufreq.c struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) · line 138
Quoted 2 lines
drivers/cpufreq/cpufreq.c +2
-0+* people should use delta rather than the value itself. +*/ -> this new comment doesn't really belong to the fix. You can send a separate patch adding it.
Moreover -->
Quoted 10 lines
drivers/cpufreq/cpufreq.c +0
-0static inline u64 get_cpu_idle_time_jiffy (unsigned int cpu,u64 *wall) {u64 idle_time;drivers/cpufreq/cpufreq_governor.c +0
-0@@ -197,8 +197,14 @@ unsigned int dbs_update(struct cpufreq_policy *policy) idle_time =0 ; }else {drivers/cpufreq/cpufreq_governor.c unsigned int dbs_update(struct cpufreq_policy *policy) · line 207
Quoted 10 lines
drivers/cpufreq/cpufreq_governor.c +7
-1idle_time = cur_idle_time - j_cdbs->prev_cpu_idle; - j_cdbs->prev_cpu_idle = cur_idle_time; } +/* +* It is possible prev_cpu_idle being bigger than cur_idle_time, +* when 32bit rounds up if !CONFIG_VIRT_CPU_ACCOUNTING, +* thus get a 0% idle estimation. So update prev_cpu_idle during +* each sample period to avoid this situation lasting too long. +*/ + j_cdbs->prev_cpu_idle = cur_idle_time;--> it looks like the bug is that we are comparing signed values as unsigned.
Quoted 1 line
Quoted 5 lines
Quoted 3 lines
if (ignore_nice) { u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];So what about the simple change below?
Well, it doesn't make sense, sorry about the confusion.
Quoted 18 lines
--- drivers/cpufreq/cpufreq_governor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Index:
linux-pm/drivers/cpufreq/cpufreq_governor.c
===================================================================drivers/cpufreq/cpufreq_governor.c +1
-1@@ -146,7 +146,7 @@ unsigned int dbs_update(struct cpufreq_p wall_time = cur_wall_time - j_cdbs->prev_cpu_wall; j_cdbs->prev_cpu_wall = cur_wall_time; -if (cur_idle_time <= j_cdbs->prev_cpu_idle) { +if ((s64 )cur_idle_time <= (s64 )j_cdbs->prev_cpu_idle) { idle_time =0 ; }else { idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;