Received: by 10.213.65.68 with SMTP id h4csp565830imn; Tue, 20 Mar 2018 09:45:49 -0700 (PDT) X-Google-Smtp-Source: AG47ELszKQZfchlpyl6LVG4zRgBDoDQMlrPOaR/JTViMuuHEw/Tc2QLDoc7Ygs5TE8FeXyHgBwta X-Received: by 10.101.97.139 with SMTP id c11mr12431068pgv.439.1521564348998; Tue, 20 Mar 2018 09:45:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521564348; cv=none; d=google.com; s=arc-20160816; b=xctEuwm9SkDX8sOfEj2BDj8sdaVMFx94o2ZFi+ppJjQ6au4QbuvTO54RrRiJKEPZRF SvfK5vmodyyPEqk4i52qzZ9rUBNCdLXuLSOOI1NiIYK1R1tvr1Oj3GfZwdBisH4iWD1Q 25/5P1trEvWBK5TFdUlQEAqs/yMrvmCfRy5IGiFkSTTLLmW/2j4HsFNuuhe+0hAzzLly BsFFe1mq6zv885dZ13WwQ5D4HfPjQEFRqJGeTvbHcprO7EE6/bHj1XDzRHYVSX9Sf91m 1bTcZM7RVYpcrfKmLbT+C4Xw8SttuJI/kbL3Tiw4P3BGKnRsaCKFwn8r1ZcIu15N1Kw0 QlFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=OrwEi1fEh2CjlVkqXvmtwTE6CkdyYLTPqqYOiIjuPw4=; b=KGT+rRyy4XUEGr02uezvrkFDlPn+qoFOSHSWce4LdRS3j1klX7qqQZUZizSbXzSZCX gTucXiat+HH0RuEkmNY3N45vhZEhKukok04NFD2KriSGnpbqRC3TbGzN2HGlLQ8XjPRD WipC0pWYOw1+G3+xpJb/K3qJfHXjLmo6T4/XAXkyUlTOQ/Xe1eGWODl6aisS4tI4BV2G VC45TlFkDR9os5jyN9sBCClagJtfuJdJkwP8+9QCJ46YZcNioBD2t9WZ/Dps6vWRMaUy DyHmPlO0oneen0zZcImwjtEaT6Hafo+8nZmTZ9wqH3ElZmIWtLQnvR3GZeW1r4tlUIbh BDJw== 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 g12si1371965pgs.417.2018.03.20.09.45.34; Tue, 20 Mar 2018 09:45:48 -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 S1751959AbeCTQoM (ORCPT + 99 others); Tue, 20 Mar 2018 12:44:12 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:62077 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751738AbeCTQnq (ORCPT ); Tue, 20 Mar 2018 12:43:46 -0400 Received: from 79.184.254.228.ipv4.supernova.orange.pl (79.184.254.228) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83) id f164d700c9d42ffb; Tue, 20 Mar 2018 17:43:44 +0100 From: "Rafael J. Wysocki" To: Peter Zijlstra , Linux PM Cc: Frederic Weisbecker , Thomas Gleixner , Paul McKenney , Thomas Ilsche , Doug Smythies , Rik van Riel , Aubrey Li , Mike Galbraith , LKML Subject: [RFT][PATCH v7 3/8] sched: idle: Do not stop the tick upfront in the idle loop Date: Tue, 20 Mar 2018 16:15:34 +0100 Message-ID: <5325267.FN4F6lrTUU@aspire.rjw.lan> In-Reply-To: <2390019.oHdSGtR3EE@aspire.rjw.lan> References: <2390019.oHdSGtR3EE@aspire.rjw.lan> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki Push the decision whether or not to stop the tick somewhat deeper into the idle loop. Stopping the tick upfront leads to unpleasant outcomes in case the idle governor doesn't agree with the nohz code on the duration of the upcoming idle period. Specifically, if the tick has been stopped and the idle governor predicts short idle, the situation is bad regardless of whether or not the prediction is accurate. If it is accurate, the tick has been stopped unnecessarily which means excessive overhead. If it is not accurate, the CPU is likely to spend too much time in the (shallow, because short idle has been predicted) idle state selected by the governor [1]. As the first step towards addressing this problem, change the code to make the tick stopping decision inside of the loop in do_idle(). In particular, do not stop the tick in the cpu_idle_poll() code path. Also don't do that in tick_nohz_irq_exit() which doesn't really have enough information on whether or not to stop the tick. Link: https://marc.info/?l=linux-pm&m=150116085925208&w=2 # [1] Link: https://tu-dresden.de/zih/forschung/ressourcen/dateien/projekte/haec/powernightmares.pdf Suggested-by: Frederic Weisbecker Signed-off-by: Rafael J. Wysocki --- include/linux/tick.h | 2 ++ kernel/sched/idle.c | 9 ++++++--- kernel/time/tick-sched.c | 26 ++++++++++++++++++-------- 3 files changed, 26 insertions(+), 11 deletions(-) Index: linux-pm/kernel/sched/idle.c =================================================================== --- linux-pm.orig/kernel/sched/idle.c +++ linux-pm/kernel/sched/idle.c @@ -221,13 +221,13 @@ static void do_idle(void) __current_set_polling(); tick_nohz_idle_enter(); - tick_nohz_idle_stop_tick_protected(); while (!need_resched()) { check_pgt_cache(); rmb(); if (cpu_is_offline(cpu)) { + tick_nohz_idle_stop_tick_protected(); cpuhp_report_idle_dead(); arch_cpu_idle_dead(); } @@ -241,10 +241,13 @@ static void do_idle(void) * broadcast device expired for us, we don't want to go deep * idle as we know that the IPI is going to arrive right away. */ - if (cpu_idle_force_poll || tick_check_broadcast_expired()) + if (cpu_idle_force_poll || tick_check_broadcast_expired()) { + tick_nohz_idle_restart_tick(); cpu_idle_poll(); - else + } else { + tick_nohz_idle_stop_tick(); cpuidle_idle_call(); + } arch_cpu_idle_exit(); } Index: linux-pm/kernel/time/tick-sched.c =================================================================== --- linux-pm.orig/kernel/time/tick-sched.c +++ linux-pm/kernel/time/tick-sched.c @@ -984,12 +984,10 @@ void tick_nohz_irq_exit(void) { struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched); - if (ts->inidle) { + if (ts->inidle) tick_nohz_start_idle(ts); - __tick_nohz_idle_stop_tick(ts); - } else { + else tick_nohz_full_update_tick(ts); - } } /** @@ -1050,6 +1048,20 @@ static void tick_nohz_account_idle_ticks #endif } +static void __tick_nohz_idle_restart_tick(struct tick_sched *ts, ktime_t now) +{ + tick_nohz_restart_sched_tick(ts, now); + tick_nohz_account_idle_ticks(ts); +} + +void tick_nohz_idle_restart_tick(void) +{ + struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched); + + if (ts->tick_stopped) + __tick_nohz_idle_restart_tick(ts, ktime_get()); +} + /** * tick_nohz_idle_exit - restart the idle tick from the idle task * @@ -1074,10 +1086,8 @@ void tick_nohz_idle_exit(void) if (ts->idle_active) tick_nohz_stop_idle(ts, now); - if (ts->tick_stopped) { - tick_nohz_restart_sched_tick(ts, now); - tick_nohz_account_idle_ticks(ts); - } + if (ts->tick_stopped) + __tick_nohz_idle_restart_tick(ts, now); local_irq_enable(); } Index: linux-pm/include/linux/tick.h =================================================================== --- linux-pm.orig/include/linux/tick.h +++ linux-pm/include/linux/tick.h @@ -115,6 +115,7 @@ enum tick_dep_bits { extern bool tick_nohz_enabled; extern int tick_nohz_tick_stopped(void); extern void tick_nohz_idle_stop_tick(void); +extern void tick_nohz_idle_restart_tick(void); extern void tick_nohz_idle_enter(void); extern void tick_nohz_idle_exit(void); extern void tick_nohz_irq_exit(void); @@ -135,6 +136,7 @@ static inline void tick_nohz_idle_stop_t #define tick_nohz_enabled (0) static inline int tick_nohz_tick_stopped(void) { return 0; } static inline void tick_nohz_idle_stop_tick(void) { } +static inline void tick_nohz_idle_restart_tick(void) { } static inline void tick_nohz_idle_enter(void) { } static inline void tick_nohz_idle_exit(void) { }