Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3004579imm; Mon, 13 Aug 2018 04:29:15 -0700 (PDT) X-Google-Smtp-Source: AA+uWPyeZbXSry2Mx5lX6f8/g7NOuMdT2lBo8KMYuO1UNjK92wgvefCLSOyj137xmKDz1+njjwgn X-Received: by 2002:a62:7886:: with SMTP id t128-v6mr18732925pfc.213.1534159755052; Mon, 13 Aug 2018 04:29:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534159755; cv=none; d=google.com; s=arc-20160816; b=W7xyBcosLF2zdaVkwOe7xnJJ6/yn2MwoUDoAW0dTLYQrm/8JzjrJEQhvrkw4v3Aszf NeYE8u58xX+CeiEIJNMPWNxmp2FMTDrxSA0cytchpRZGqkk3lDk3oPS98rZ+VP88z7qo so7eP4TnJ0CYoA+hKaIcWFw7Wct0vdufvZJ77Te/WXFcAunvW3GPkEFAYkbCBbQZD3Ys AOrKSS0mvczEdPLZxgACbrgd7lw1Ao/VZ0d37f8z576oimjHIbOq2mhRmSmK2WGLpf4L 8/Gr8pEIU9N+UA12xbhOt1IoV/B1Mys2aZ3jjZgoN9gLWj9dsYR7EzguPPjMleoGn0u+ NK1g== 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=xvdPBLT5aKJ5xy5+v6+LgZngMfInhHDkjqMCh3dynhk=; b=p1WgKvq0TSXs0HZvFuJKhtss5tEh1TeKaAY2hM5UVJW18NWgSFdOz9AfE8YEbBrzau UEK/Wvka31d3TxfPQeo1xkqEuOnDnA4YmtykTAPKw48XXeDVEvxr/k4SJFDWBwSm2nBj 0MrNpXJHG16UNaWhDoQHy+KqzPzi78HXmidEx3JyQT4gT1+WInMxkIA+A+iM4wCeTbl+ 5TTsBOfxpU/DPbAsf3F1IAZXpTX09qyahrNsco/yFpig5dkmxBlnuMODTzwlt9mKBDj1 M8qE5UeifJyMxfLlPCUeMVs0/7Bl7RyV7teFdyeHiCtCNkdVo0D7jbrTz+DZH1MhKhNA eoXw== 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 b2-v6si13236790plr.382.2018.08.13.04.28.59; Mon, 13 Aug 2018 04:29:15 -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 S1728752AbeHMOKD (ORCPT + 99 others); Mon, 13 Aug 2018 10:10:03 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:42157 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728310AbeHMOKD (ORCPT ); Mon, 13 Aug 2018 10:10:03 -0400 Received: from 79.184.254.66.ipv4.supernova.orange.pl (79.184.254.66) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83) id 3c569caa2f956cd0; Mon, 13 Aug 2018 13:28:08 +0200 From: "Rafael J. Wysocki" To: Linux PM Cc: Peter Zijlstra , LKML , Leo Yan , Frederic Weisbecker Subject: [PATCH v4] cpuidle: menu: Handle stopped tick more aggressively Date: Mon, 13 Aug 2018 13:26:00 +0200 Message-ID: <1582055.9b67urWYFa@aspire.rjw.lan> In-Reply-To: <1754612.IcCR94pSYR@aspire.rjw.lan> References: <1951009.1jlQfyrxio@aspire.rjw.lan> <3174357.2tBMdxG3bF@aspire.rjw.lan> <1754612.IcCR94pSYR@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 Commit 87c9fe6ee495 (cpuidle: menu: Avoid selecting shallow states with stopped tick) missed the case when the target residencies of deep idle states of CPUs are above the tick boundary which may cause the CPU to get stuck in a shallow idle state for a long time. Say there are two CPU idle states available: one shallow, with the target residency much below the tick boundary and one deep, with the target residency significantly above the tick boundary. In that case, if the tick has been stopped already and the expected next timer event is relatively far in the future, the governor will assume the idle duration to be equal to TICK_USEC and it will select the idle state for the CPU accordingly. However, that will cause the shallow state to be selected even though it would have been more energy-efficient to select the deep one. To address this issue, modify the governor to always use the time till the closest timer event instead of the predicted idle duration if the latter is less than the tick period length and the tick has been stopped already. Also make it extend the search for a matching idle state if the tick is stopped to avoid settling on a shallow state if deep states with target residencies above the tick period length are available. In addition, make it always indicate that the tick should be stopped if it has been stopped already for consistency. Fixes: 87c9fe6ee495 (cpuidle: menu: Avoid selecting shallow states with stopped tick) Reported-by: Leo Yan Signed-off-by: Rafael J. Wysocki --- -> v2: Initialize first_idx properly in the stopped tick case. v2 -> v3: Compute data->bucket before checking whether or not the tick has been stopped already to prevent it from becoming stale. v3 -> v4: Allow the usual state selection to be carried out if the tick has been stopped in case the predicted idle duration is greater than the tick period length and a matching state can be found without overriding the prediction result. --- drivers/cpuidle/governors/menu.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) Index: linux-pm/drivers/cpuidle/governors/menu.c =================================================================== --- linux-pm.orig/drivers/cpuidle/governors/menu.c +++ linux-pm/drivers/cpuidle/governors/menu.c @@ -349,14 +349,12 @@ static int menu_select(struct cpuidle_dr * If the tick is already stopped, the cost of possible short * idle duration misprediction is much higher, because the CPU * may be stuck in a shallow idle state for a long time as a - * result of it. In that case say we might mispredict and try - * to force the CPU into a state for which we would have stopped - * the tick, unless a timer is going to expire really soon - * anyway. + * result of it. In that case say we might mispredict and use + * the known time till the closest timer event for the idle + * state selection. */ if (data->predicted_us < TICK_USEC) - data->predicted_us = min_t(unsigned int, TICK_USEC, - ktime_to_us(delta_next)); + data->predicted_us = ktime_to_us(delta_next); } else { /* * Use the performance multiplier and the user-configurable @@ -381,8 +379,20 @@ static int menu_select(struct cpuidle_dr continue; if (idx == -1) idx = i; /* first enabled state */ - if (s->target_residency > data->predicted_us) - break; + if (s->target_residency > data->predicted_us) { + if (!tick_nohz_tick_stopped() || + drv->states[idx].target_residency >= TICK_USEC) + break; + + /* + * If the state selected so far is shallow and the time + * till the closest timer event matches this state's + * target residency, select this one to avoid getting + * stuck in the shallow one for too long. + */ + if (s->target_residency > ktime_to_us(delta_next)) + break; + } if (s->exit_latency > latency_req) { /* * If we break out of the loop for latency reasons, use @@ -403,14 +413,13 @@ static int menu_select(struct cpuidle_dr * Don't stop the tick if the selected state is a polling one or if the * expected idle duration is shorter than the tick period length. */ - if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || - expected_interval < TICK_USEC) { + if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || + expected_interval < TICK_USEC) && !tick_nohz_tick_stopped()) { unsigned int delta_next_us = ktime_to_us(delta_next); *stop_tick = false; - if (!tick_nohz_tick_stopped() && idx > 0 && - drv->states[idx].target_residency > delta_next_us) { + if (idx > 0 && drv->states[idx].target_residency > delta_next_us) { /* * The tick is not going to be stopped and the target * residency of the state to be returned is not within