From patchwork Mon Apr 4 16:32:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Yu X-Patchwork-Id: 8742721 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B146EC0553 for ; Mon, 4 Apr 2016 16:32:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C26252028D for ; Mon, 4 Apr 2016 16:32:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B0C932024F for ; Mon, 4 Apr 2016 16:32:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932521AbcDDQbo (ORCPT ); Mon, 4 Apr 2016 12:31:44 -0400 Received: from mga03.intel.com ([134.134.136.65]:7090 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932471AbcDDQ0u (ORCPT ); Mon, 4 Apr 2016 12:26:50 -0400 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP; 04 Apr 2016 09:26:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,441,1455004800"; d="scan'208";a="947753626" Received: from unknown (HELO localhost.localdomain.sh.intel.com) ([10.239.160.87]) by orsmga002.jf.intel.com with ESMTP; 04 Apr 2016 09:26:48 -0700 From: Chen Yu To: "Rafael J. Wysocki" , Viresh Kumar Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Chen Yu Subject: [PATCH][RFC] cpufreq: governor: Fix overflow when calculating idle time Date: Tue, 5 Apr 2016 00:32:33 +0800 Message-Id: <1459787553-20092-1-git-send-email-yu.c.chen@intel.com> X-Mailer: git-send-email 1.8.4.2 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It was reported that after Commit 0df35026c6a5 ("cpufreq: governor: Fix negative idle_time when configured with CONFIG_HZ_PERIODIC"), cpufreq ondemand governor started to act oddly. Without any load, with freshly booted system, it pumped cpu frequency up to maximum at some point of time and stayed there. The problem is caused by jiffies oveflow in get_cpu_idle_time: Since jiffies is not counted from zero but from -300HZ, after booting up 5 minutes, the jiffies will round up to zero. As a result, the following condition will always be true: if (cur_idle_time < j_cdbs->prev_cpu_idle) cur_idle_time = j_cdbs->prev_cpu_idle; Say, once cur_idle_time rounds up to zero, while prev_cpu_idle remains negative(because of -300HZ), the cur_idle_time will be modified to the same as prev_cpu_idle(negative), thus we get a zero of idle running time during this sample(cur_idle_time - prev_cpu_idle), which causes a high busy time, thus governor always requests for the highest freq. So the patch mentioned above has triggered this overflow issue more easily. This patch fixes this problem by doing two modifications in the get_cpu_idle_time_jiffy: 1. Count the wall time from INITIAL_JIFFIES rather than 0. 2. Use 64bit rather than 32bit to convert jiffies64 to usec. In this way, either the wall time or the idle time will be monotonically increased, thus avoid the jiffies overflow problem. (If CONFIG_HZ_1000=y, it would be at least 500000000 years for 64bit to overflow, which would not possibly happen.) Link: https://bugzilla.kernel.org/show_bug.cgi?id=115261 Reported-by: Timo Valtoaho Signed-off-by: Chen Yu --- drivers/cpufreq/cpufreq.c | 6 +++--- include/asm-generic/cputime_jiffies.h | 2 ++ include/linux/cputime.h | 5 +++++ include/linux/jiffies.h | 1 + kernel/time/time.c | 20 ++++++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index b87596b..2dc327a 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -138,7 +138,7 @@ static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) u64 cur_wall_time; u64 busy_time; - cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); + cur_wall_time = jiffies64_to_cputime64(get_jiffies_64() - INITIAL_JIFFIES); busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; @@ -149,9 +149,9 @@ static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) idle_time = cur_wall_time - busy_time; if (wall) - *wall = cputime_to_usecs(cur_wall_time); + *wall = cputime64_to_usecs(cur_wall_time); - return cputime_to_usecs(idle_time); + return cputime64_to_usecs(idle_time); } u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) diff --git a/include/asm-generic/cputime_jiffies.h b/include/asm-generic/cputime_jiffies.h index fe386fc..ff1cf39 100644 --- a/include/asm-generic/cputime_jiffies.h +++ b/include/asm-generic/cputime_jiffies.h @@ -36,6 +36,8 @@ typedef u64 __nocast cputime64_t; jiffies_to_cputime(usecs_to_jiffies(__usec)) #define usecs_to_cputime64(__usec) \ jiffies64_to_cputime64(nsecs_to_jiffies64((__usec) * 1000)) +#define cputime64_to_usecs(__ct) \ + jiffies64_to_usecs(cputime64_to_jiffies64(__ct)) /* * Convert cputime to seconds and back. diff --git a/include/linux/cputime.h b/include/linux/cputime.h index f2eb2ee..ffa8665 100644 --- a/include/linux/cputime.h +++ b/include/linux/cputime.h @@ -13,4 +13,9 @@ usecs_to_cputime((__nsecs) / NSEC_PER_USEC) #endif +#ifndef cputime64_to_usecs +#define cputime64_to_usecs(__ct) \ + cputime_to_usecs(__ct) +#endif + #endif /* __LINUX_CPUTIME_H */ diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index 5fdc553..f26ceb4 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h @@ -283,6 +283,7 @@ extern unsigned long preset_lpj; */ extern unsigned int jiffies_to_msecs(const unsigned long j); extern unsigned int jiffies_to_usecs(const unsigned long j); +extern u64 jiffies64_to_usecs(const u64 j); static inline u64 jiffies_to_nsecs(const unsigned long j) { diff --git a/kernel/time/time.c b/kernel/time/time.c index be115b0..5167c4b 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -286,6 +286,26 @@ unsigned int jiffies_to_usecs(const unsigned long j) } EXPORT_SYMBOL(jiffies_to_usecs); +u64 jiffies64_to_usecs(const u64 j) +{ + /* + * Hz usually doesn't go much further MSEC_PER_SEC. + * jiffies_to_usecs() and usecs_to_jiffies() depend on that. + */ + BUILD_BUG_ON(HZ > USEC_PER_SEC); + +#if !(USEC_PER_SEC % HZ) + return (USEC_PER_SEC / HZ) * j; +#else +# if BITS_PER_LONG == 32 + return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; +# else + return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; +# endif +#endif +} +EXPORT_SYMBOL(jiffies64_to_usecs); + /** * timespec_trunc - Truncate timespec to a granularity * @t: Timespec