Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp1269259img; Tue, 19 Mar 2019 04:14:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqzJGfk04mn1YE6LQkgUlu1WY/zUPnChkSHUL6qLaaR8R9EVjv6j4GtT3aYeVUhKuWOTWicc X-Received: by 2002:a17:902:9341:: with SMTP id g1mr1819153plp.80.1552994040411; Tue, 19 Mar 2019 04:14:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552994040; cv=none; d=google.com; s=arc-20160816; b=Y1m7BI00yRCxT9/Lxhd1FFvbeC4HJtXBwyerm5v6gKkzoy1Bg4gcmVAiN/MXSxL3Qa n6E9FeUvlsKh9Mku8FR82cMNrGHr35s9hgXdf8V+hS9WtfNiWuTAgJaI5tEIYNxL9n1z ukQmw4HicGPvXDtMFqRzsKa0sHVy78eOxq1MNreejoYDueYxMIrQ2yfWFp1TMhUHEFG/ RFT4u/6nqIXTWDzd0UA/H553v2ZVpfbBFTAoDmLPJvCrTS/fRQnodhhN/3RfJr8a6cE+ et55yT9b9Wwu611MNI0kzfU28ABxYMhTbg/gOfMobumjSqMdhTSn2TsbdcXoDUhOuKeO ESeQ== 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=jlxIV3nnAh2Q11m4QL902tUYZlBPnZL3n9tlbzYEaTg=; b=YkOwQuZBf1Kc/UJm/kxH9w7lzD2kyZRFff9+NadEgxEbRKvp4srtlswFNp7jiQbZcC +tkSaenT45JnbqVExNju/KQs12HlFyspm1OhHMR7N3rvFcJk4u6AYiBdyX8h0NA0dI4Q Vb9l1GA5ZHBuMTKENdh/B2D/IDPhmpxUQs3Rgo1dsHD4YiCQcbyqta5i612xIxgV40xG nDsKypWTUbf3MAOgnsbg7G6K9aZkUkMi5ECwBLirnV7LUgiiZsFPGYtlBRM9Ry+YXjeu u5P0ZeqAUzt+l6/sNo0x/LPK0CezxvQ6XcZMdfppjL+BccB0FsAgnI0AqxqE2SnRZ7Sz Rg4A== 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 c32si12062281plj.282.2019.03.19.04.13.45; Tue, 19 Mar 2019 04:14:00 -0700 (PDT) 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 S1727418AbfCSLLl (ORCPT + 99 others); Tue, 19 Mar 2019 07:11:41 -0400 Received: from terminus.zytor.com ([198.137.202.136]:46157 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbfCSLLl (ORCPT ); Tue, 19 Mar 2019 07:11:41 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x2JBAqpt3395563 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 19 Mar 2019 04:10:52 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x2JBAofk3395560; Tue, 19 Mar 2019 04:10:51 -0700 Date: Tue, 19 Mar 2019 04:10:51 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Peter Zijlstra Message-ID: Cc: rafael.j.wysocki@intel.com, riel@surriel.com, dave.hansen@linux.intel.com, rjw@rjwysocki.net, linux-kernel@vger.kernel.org, quentin.perret@arm.com, bp@alien8.de, peterz@infradead.org, hpa@zytor.com, luto@kernel.org, zhang.lyra@gmail.com, mingo@kernel.org, tglx@linutronix.de, torvalds@linux-foundation.org, vincent.wang@unisoc.com Reply-To: vincent.wang@unisoc.com, mingo@kernel.org, torvalds@linux-foundation.org, tglx@linutronix.de, zhang.lyra@gmail.com, luto@kernel.org, bp@alien8.de, peterz@infradead.org, quentin.perret@arm.com, hpa@zytor.com, linux-kernel@vger.kernel.org, rjw@rjwysocki.net, rafael.j.wysocki@intel.com, dave.hansen@linux.intel.com, riel@surriel.com 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: a23314e9d88d89d49e69db08f60b7caa470f04e1 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=-0.8 required=5.0 tests=ALL_TRUSTED,BAYES_00, FREEMAIL_FORGED_REPLYTO,T_DATE_IN_FUTURE_96_Q autolearn=no autolearn_force=no version=3.4.2 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: a23314e9d88d89d49e69db08f60b7caa470f04e1 Gitweb: https://git.kernel.org/tip/a23314e9d88d89d49e69db08f60b7caa470f04e1 Author: Peter Zijlstra AuthorDate: Tue, 5 Mar 2019 09:32:02 +0100 Committer: Ingo Molnar CommitDate: Tue, 19 Mar 2019 12:06:11 +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 | 59 +++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 033ec7c45f13..1ccf77f6d346 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,13 @@ 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_t(unsigned int, 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 +371,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 +449,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 +489,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 +826,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) {