mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chunyan Zhang <chunyan.zhang@unisoc.com>
To: Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org>
Cc: Vincent Wang <vincent.wang@unisoc.com>,
	Quentin Perret <quentin.perret@arm.com>,
	<linux-kernel@vger.kernel.org>,
	Chunyan Zhang <zhang.lyra@gmail.com>,
	Chunyan Zhang <chunyan.zhang@unisoc.com>
Subject: [PATCH V3] sched/cpufreq: calculate util / cap in advance in map_util_freq()
Date: Mon, 28 Jan 2019 14:27:21 +0800	[thread overview]
Message-ID: <1548656841-3440-1-git-send-email-chunyan.zhang@unisoc.com> (raw)

From: "vincent.wang" <vincent.wang@unisoc.com>

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 <vincent.wang@unisoc.com>
Signed-off-by: Chunyan Zhang <chunyan.zhang@unisoc.com>
---
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


             reply	other threads:[~2019-01-28  6:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-28  6:27 Chunyan Zhang [this message]
2019-01-29 10:40 ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1548656841-3440-1-git-send-email-chunyan.zhang@unisoc.com \
    --to=chunyan.zhang@unisoc.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=quentin.perret@arm.com \
    --cc=vincent.wang@unisoc.com \
    --cc=zhang.lyra@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®