Received: by 10.213.65.68 with SMTP id h4csp487848imn; Wed, 4 Apr 2018 01:54:30 -0700 (PDT) X-Google-Smtp-Source: AIpwx49innLl4DEC5ntHZnygeyrT6KjVF9adDjfmolx4UQUfiOGdqlRM1TQt84sF9VcGN1l+yj+j X-Received: by 2002:a17:902:9692:: with SMTP id n18-v6mr17430229plp.175.1522832070134; Wed, 04 Apr 2018 01:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522832070; cv=none; d=google.com; s=arc-20160816; b=OzTEVcRcqQXhTZ++WTs0h6uVdeFwbSN3DGii4KrftnSMpvUQ74ITdC+yJKK3jTYJcH gnVv1aGHldQI/unX1xtxPzj3WIeC52Z2B4JE05I1ICRV2sYedMVM73uUrVMox1BLYrwW bm3nfaXA1yA6YZznWRaQPJ1v/OKDEx+zptad06MIhb/F+S/22kvssA5comhcNbNRr3d4 MQbzl53Jiw9O+/GUc7hq3XuQHsBUBBhr2ck6/v0H91d86CSXe7rPDXpA0mpD7s1LRaWj DpjNtotjn225ATnKx5JyruWIPGKOkMXFTh8spe5e2C030QnBlW/+Syd4gJ/g7TvA4K1Q ndiQ== 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=Toic2HEUuim5pI+7ieMXlQ2A1BrJgv0yGhq5r3fGYak=; b=iXrQt4vkbEvdBtYMdrl2jmdzWapqeKv5x7tZYlDedHmw1HwpJcj2UWHgJZhJnWEtxs eYs8wVglE+NXe2ycsodMVKBHCfy0sT5Z9MIp9/QwJ9wkqHHFAy9SN7YwgDbtKi1TTfB1 3NU6aHm+2WBeHYcvIKfh/rJQ1R11n4q6LNB+Azq1k8X2FvhN67w4NhVgvGlxwroWjIDi 24yU6jXkPreheOmqOUb8zUCp5pAyAGgpcVuv6HXU4h4xQ/ZeOinjJmThvCEh2OKtHpSH rlED6LH3MtDVel2zmW0mzPgCSKxv5clPixDGXUKzbQ9Rv9fhC8RI7B3vAjS8Nd2zPKPF 5BNA== 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 a3si2060074pgv.522.2018.04.04.01.54.16; Wed, 04 Apr 2018 01:54:30 -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 S1751582AbeDDIxJ (ORCPT + 99 others); Wed, 4 Apr 2018 04:53:09 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:46297 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751519AbeDDIxH (ORCPT ); Wed, 4 Apr 2018 04:53:07 -0400 Received: from 79.184.255.92.ipv4.supernova.orange.pl (79.184.255.92) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83) id ee51b9a9d40814d5; Wed, 4 Apr 2018 10:53:05 +0200 From: "Rafael J. Wysocki" To: Linux PM Cc: Peter Zijlstra , Frederic Weisbecker , Thomas Gleixner , Paul McKenney , Thomas Ilsche , Doug Smythies , Rik van Riel , Aubrey Li , Mike Galbraith , LKML , Len Brown Subject: [PATCH v9 02/10] sched: idle: Do not stop the tick upfront in the idle loop Date: Wed, 04 Apr 2018 10:34:49 +0200 Message-ID: <5840863.93D3y0Ai16@aspire.rjw.lan> In-Reply-To: <1736751.LdhZHb50jq@aspire.rjw.lan> References: <1736751.LdhZHb50jq@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 Reviewed-by: Frederic Weisbecker --- v8 -> v9: * No changes in the patch. * Tag from Frederic. --- 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) { }