Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3130050imu; Sun, 27 Jan 2019 22:29:35 -0800 (PST) X-Google-Smtp-Source: ALg8bN7y/1TTSxg3MHE7liXmGcz/cOlilqRUCw1LCXrJoJ0XehWlgFw/4veq4co6Nzomppoc15Ei X-Received: by 2002:a62:83ce:: with SMTP id h197mr20597233pfe.187.1548656975084; Sun, 27 Jan 2019 22:29:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548656975; cv=none; d=google.com; s=arc-20160816; b=Tg4LATaxqgqp96XSskevZjn+jl9GDtp+NteOOG5HI/yiOX/jRz7vGRWjAyuEH+ZxdU 6/OCjEE2E8ItLHnau+JIRCkd/qqQYwAg8WnRphBGiJHHriDa6aDYtDQSoe7c7FGf7sqK ZNaMSUZP4kPA9y5Sv2U16tjF0qy3Ip4ps1J+LxmPPPiFRTNUOCyGfTFyqZHTy6ZutXGz +JTiWwhIGrTGa6qEWP54lQE4nq9imdVGiOS24EdbAes1ykbFAW6A8PRpVBIpDe1Oaijd skBh4eZ2KuUouplYZscU+/dQJxO2bB7yTxY38izJ+xQ6ChAQ01ZQG68wOqiRmEj+TDUB Gdkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=vo62mnmXBAOUxP4UUgcsCLn7v5+q9VOYiCblJ9aHw7w=; b=icDl4yRRA8Qbrxt36KHYG9DVyWM+3KTWcV57W2t4E9dUMrBxE9feiHI2USRu11YzBD v3wyEpm6oJv42c2l32IdV7uVlmJcBdA8INlOISA8keXhSkCluGetpgflslkyBoJlM0tb 1ABPgX6IAuY45agCYnnZLiq7coFXubn4juRc/mJ0gEh+lG7aP7uThuT5xzsLrsKNcsuu NencZUslLf6sgKAQVfdWZAFKU5Kbplj4+HTfzK4idpGrWaj0MWNrI/59g9NkA+j/Mlmh 78g/SegdKQOugJiIM61KHsvRSdyVO5ZGKmh3QkDjc5sVqYv2hItm+dR1/BTlZdykch5v 7hbA== 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 v200si3582864pgb.15.2019.01.27.22.29.07; Sun, 27 Jan 2019 22:29: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 S1726647AbfA1G3D (ORCPT + 99 others); Mon, 28 Jan 2019 01:29:03 -0500 Received: from mx1.unisoc.com ([222.66.158.135]:52678 "EHLO SHSQR01.spreadtrum.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725782AbfA1G3C (ORCPT ); Mon, 28 Jan 2019 01:29:02 -0500 Received: from ig2.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by SHSQR01.spreadtrum.com with ESMTPS id x0S6RIY5095436 (version=TLSv1 cipher=AES256-SHA bits=256 verify=NO); Mon, 28 Jan 2019 14:27:18 +0800 (CST) (envelope-from Chunyan.Zhang@unisoc.com) Received: from localhost (10.0.93.106) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.847.32; Mon, 28 Jan 2019 14:27:28 +0800 From: Chunyan Zhang To: Ingo Molnar , Peter Zijlstra CC: Vincent Wang , Quentin Perret , , Chunyan Zhang , Chunyan Zhang Subject: [PATCH V3] sched/cpufreq: calculate util / cap in advance in map_util_freq() Date: Mon, 28 Jan 2019 14:27:21 +0800 Message-ID: <1548656841-3440-1-git-send-email-chunyan.zhang@unisoc.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.0.93.106] X-ClientProxiedBy: SHCAS02.spreadtrum.com (10.0.1.202) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com x0S6RIY5095436 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "vincent.wang" 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 Signed-off-by: Chunyan Zhang --- 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