Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp6798346imb; Sat, 9 Mar 2019 06:39:35 -0800 (PST) X-Google-Smtp-Source: APXvYqyjXj4KzI3y5wlklQJqOG4KQwqn6f30ZFo1zhBpWl4a3CfLrKu8Zwqp0cjKPmU6bb6M+E+A X-Received: by 2002:a63:2a86:: with SMTP id q128mr21885191pgq.424.1552142375180; Sat, 09 Mar 2019 06:39:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552142375; cv=none; d=google.com; s=arc-20160816; b=H1+c+nWhj7FSgSrmouU4ubgT/1Qp2r1k6sqoCVsSFW+VNkb495qmboCp4H9SR/n4oW Hft92V8DMwaJqktgMFu5wQrL2END6XPg7W1vsOl+t8h80k0U92iRtaQlMklkqLf+QOk6 4UsrQnCEPvXzAZbxRWPdVUvVxi97vawOFglB/fzg813V/h0lyP/auV3ahfClibOJpgwV 6Lm740mGDCibLP2w7MXN6YI5oV7VB/0HDz75H9ujlrN98uNda5+oTE4tbXeZHNIM+AG8 bhtuAhTBjzdXDxmytmjo7ufmOMNTu/N4hrjvc0nnwwyzM2QHuXtf+0acuIcVPimLFw97 oIWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=06l763c1TEQIeMhXhhjp0bxbojI+kmPTq5sQ7w0N0Uo=; b=CMp6iq9RoE85uHx8gJv01xOSGQzbrjFt5/JmytZ5Xiuzco0TvvczsMySrYPcqmeXm7 iLhu+m4uFLu4FLis0PsgwCUGo+UDcfstM0Fb1+JYwMEvCa793e51gvGXfAKaYJoCMLz5 DNYSgxABEyzgLSO6gcLY/I4ZkwlWmyeXlECPArmKXlezq4xe5iGXYfHQgKLQ9WrqZZu/ l0oboblocXkj8D+2zlhN6DbatuzNvI+8n+WoMDq4ESu0da9enY4FuF25PaVyW67MLYyF fWkaiz5FRTP/9bqmJSAHRCQdJBah97a27v/Tlmr24O2vsyt2rT5k9HF3WZZ6TyqUBS0K yMng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x2si631689plo.360.2019.03.09.06.39.19; Sat, 09 Mar 2019 06:39:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726524AbfCIOhC (ORCPT + 99 others); Sat, 9 Mar 2019 09:37:02 -0500 Received: from terminus.zytor.com ([198.137.202.136]:43775 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbfCIOhA (ORCPT ); Sat, 9 Mar 2019 09:37:00 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x29EZExv3108541 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 9 Mar 2019 06:35:14 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x29EZCAp3108532; Sat, 9 Mar 2019 06:35:12 -0800 Date: Sat, 9 Mar 2019 06:35:12 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Peter Zijlstra Message-ID: Cc: riel@surriel.com, tglx@linutronix.de, bp@alien8.de, quentin.perret@arm.com, torvalds@linux-foundation.org, hpa@zytor.com, vincent.wang@unisoc.com, luto@kernel.org, rjw@rjwysocki.net, dave.hansen@linux.intel.com, peterz@infradead.org, zhang.lyra@gmail.com, rafael.j.wysocki@intel.com, linux-kernel@vger.kernel.org, mingo@kernel.org Reply-To: hpa@zytor.com, vincent.wang@unisoc.com, luto@kernel.org, rjw@rjwysocki.net, tglx@linutronix.de, bp@alien8.de, riel@surriel.com, quentin.perret@arm.com, torvalds@linux-foundation.org, mingo@kernel.org, dave.hansen@linux.intel.com, peterz@infradead.org, zhang.lyra@gmail.com, rafael.j.wysocki@intel.com, linux-kernel@vger.kernel.org In-Reply-To: <20190305083202.GU32494@hirez.programming.kicks-ass.net> References: <20190305083202.GU32494@hirez.programming.kicks-ass.net> To: linux-tip-commits@vger.kernel.org Subject: [tip:sched/urgent] sched/cpufreq: Fix 32-bit math overflow Git-Commit-ID: f1212844e9dc3a31d41f99713c5522acf92ff291 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=1.4 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_48_96,FREEMAIL_FORGED_REPLYTO autolearn=no autolearn_force=no version=3.4.2 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: f1212844e9dc3a31d41f99713c5522acf92ff291 Gitweb: https://git.kernel.org/tip/f1212844e9dc3a31d41f99713c5522acf92ff291 Author: Peter Zijlstra AuthorDate: Tue, 5 Mar 2019 09:32:02 +0100 Committer: Ingo Molnar CommitDate: Sat, 9 Mar 2019 14:03:51 +0100 sched/cpufreq: Fix 32-bit math overflow Vincent Wang reported that get_next_freq() has a mult overflow bug on 32-bit platforms in the IOWAIT boost case, since in that case {util,max} are in freq units instead of capacity units. Solve this by moving the IOWAIT boost to capacity units. And since this means @max is constant; simplify the code. Reported-by: Vincent Wang Tested-by: Vincent Wang Signed-off-by: Peter Zijlstra (Intel) Acked-by: Rafael J. Wysocki Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Chunyan Zhang Cc: Dave Hansen Cc: H. Peter Anvin Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Quentin Perret Cc: Rafael J. Wysocki Cc: Rik van Riel Cc: Thomas Gleixner Link: https://lkml.kernel.org/r/20190305083202.GU32494@hirez.programming.kicks-ass.net Signed-off-by: Ingo Molnar --- kernel/sched/cpufreq_schedutil.c | 58 +++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 033ec7c45f13..5a8932ee5112 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -48,10 +48,10 @@ struct sugov_cpu { bool iowait_boost_pending; unsigned int iowait_boost; - unsigned int iowait_boost_max; u64 last_update; unsigned long bw_dl; + unsigned long min; unsigned long max; /* The field below is for single-CPU policies only: */ @@ -303,8 +303,7 @@ static bool sugov_iowait_reset(struct sugov_cpu *sg_cpu, u64 time, if (delta_ns <= TICK_NSEC) return false; - sg_cpu->iowait_boost = set_iowait_boost - ? sg_cpu->sg_policy->policy->min : 0; + sg_cpu->iowait_boost = set_iowait_boost ? sg_cpu->min : 0; sg_cpu->iowait_boost_pending = set_iowait_boost; return true; @@ -344,14 +343,12 @@ static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, /* Double the boost at each request */ if (sg_cpu->iowait_boost) { - sg_cpu->iowait_boost <<= 1; - if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max) - sg_cpu->iowait_boost = sg_cpu->iowait_boost_max; + sg_cpu->iowait_boost = min(sg_cpu->iowait_boost << 1, SCHED_CAPACITY_SCALE); return; } /* First wakeup after IO: start with minimum boost */ - sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min; + sg_cpu->iowait_boost = sg_cpu->min; } /** @@ -373,47 +370,38 @@ static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, * This mechanism is designed to boost high frequently IO waiting tasks, while * being more conservative on tasks which does sporadic IO operations. */ -static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time, - unsigned long *util, unsigned long *max) +static unsigned long sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time, + unsigned long util, unsigned long max) { - unsigned int boost_util, boost_max; + unsigned long boost; /* No boost currently required */ if (!sg_cpu->iowait_boost) - return; + return util; /* Reset boost if the CPU appears to have been idle enough */ if (sugov_iowait_reset(sg_cpu, time, false)) - return; + return util; - /* - * An IO waiting task has just woken up: - * allow to further double the boost value - */ - if (sg_cpu->iowait_boost_pending) { - sg_cpu->iowait_boost_pending = false; - } else { + if (!sg_cpu->iowait_boost_pending) { /* - * Otherwise: reduce the boost value and disable it when we - * reach the minimum. + * No boost pending; reduce the boost value. */ sg_cpu->iowait_boost >>= 1; - if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) { + if (sg_cpu->iowait_boost < sg_cpu->min) { sg_cpu->iowait_boost = 0; - return; + return util; } } + sg_cpu->iowait_boost_pending = false; + /* - * Apply the current boost value: a CPU is boosted only if its current - * utilization is smaller then the current IO boost level. + * @util is already in capacity scale; convert iowait_boost + * into the same scale so we can compare. */ - boost_util = sg_cpu->iowait_boost; - boost_max = sg_cpu->iowait_boost_max; - if (*util * boost_max < *max * boost_util) { - *util = boost_util; - *max = boost_max; - } + boost = (sg_cpu->iowait_boost * max) >> SCHED_CAPACITY_SHIFT; + return max(boost, util); } #ifdef CONFIG_NO_HZ_COMMON @@ -460,7 +448,7 @@ static void sugov_update_single(struct update_util_data *hook, u64 time, util = sugov_get_util(sg_cpu); max = sg_cpu->max; - sugov_iowait_apply(sg_cpu, time, &util, &max); + util = sugov_iowait_apply(sg_cpu, time, util, max); next_f = get_next_freq(sg_policy, util, max); /* * Do not reduce the frequency if the CPU has not been idle @@ -500,7 +488,7 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) j_util = sugov_get_util(j_sg_cpu); j_max = j_sg_cpu->max; - sugov_iowait_apply(j_sg_cpu, time, &j_util, &j_max); + j_util = sugov_iowait_apply(j_sg_cpu, time, j_util, j_max); if (j_util * max > j_max * util) { util = j_util; @@ -837,7 +825,9 @@ static int sugov_start(struct cpufreq_policy *policy) memset(sg_cpu, 0, sizeof(*sg_cpu)); sg_cpu->cpu = cpu; sg_cpu->sg_policy = sg_policy; - sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; + sg_cpu->min = + (SCHED_CAPACITY_SCALE * policy->cpuinfo.min_freq) / + policy->cpuinfo.max_freq; } for_each_cpu(cpu, policy->cpus) {