2019-01-28 06:29:35

by Chunyan Zhang

[permalink] [raw]
Subject: [PATCH V3] sched/cpufreq: calculate util / cap in advance in map_util_freq()

From: "vincent.wang" <[email protected]>

When a task that is in_iowait state is enqueued, cpufreq_update_util()
will be invoked with SCHED_CPUFREQ_IOWAIT flag. In this case,the value
of util and cap, which are parameters used in map_util_freq(), will be
cpu frequency, instead of cpu util and capactiy.

For some 32bit architectures, the size of unsigned long is 32. When
calculating freq, there may be an overflow error in this expression:

freq = (freq + (freq >> 2)) * util / cap;

This patch will fix this overflow risk by calulating util / cap in advance,
whether they be frequency or util.

Signed-off-by: Vincent Wang <[email protected]>
Signed-off-by: Chunyan Zhang <[email protected]>
---
Changes from v2:
* Fix for 32bit architectures only.

Changes from V1:
* Rebased onto v5.0-rc1;
* Addressed comments from Quentin Perret.
---
include/linux/sched/cpufreq.h | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/include/linux/sched/cpufreq.h b/include/linux/sched/cpufreq.h
index afa940cd50dc..22128a6c9c91 100644
--- a/include/linux/sched/cpufreq.h
+++ b/include/linux/sched/cpufreq.h
@@ -24,7 +24,15 @@ void cpufreq_remove_update_util_hook(int cpu);
static inline unsigned long map_util_freq(unsigned long util,
unsigned long freq, unsigned long cap)
{
+#ifdef CONFIG_64BIT
return (freq + (freq >> 2)) * util / cap;
+#else
+ /*
+ * calculate util / cap in advance to prevent an overflow error
+ * on 32bit architectures
+ */
+ return ((freq + (freq >> 2)) * ((util << 10) / cap)) >> 10;
+#endif
}
#endif /* CONFIG_CPU_FREQ */

--
2.17.1



2019-01-29 10:41:45

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH V3] sched/cpufreq: calculate util / cap in advance in map_util_freq()

On Mon, Jan 28, 2019 at 02:27:21PM +0800, Chunyan Zhang wrote:
> From: "vincent.wang" <[email protected]>
>
> When a task that is in_iowait state is enqueued, cpufreq_update_util()
> will be invoked with SCHED_CPUFREQ_IOWAIT flag. In this case,the value
> of util and cap, which are parameters used in map_util_freq(), will be
> cpu frequency, instead of cpu util and capactiy.

Didn't I tell you to fix the IOWAIT case to not pass in such large
numbers?