Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp332538rwb; Thu, 27 Jul 2023 13:31:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlEpGVGa51E4jYdxeqmXCFvlcvQb61mkwYR0UJDBdxFiUOmXfeHhXc8xsrcxFhE2nUrVP+9H X-Received: by 2002:a2e:80d2:0:b0:2b9:5791:88cc with SMTP id r18-20020a2e80d2000000b002b9579188ccmr61962ljg.37.1690489889519; Thu, 27 Jul 2023 13:31:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690489889; cv=none; d=google.com; s=arc-20160816; b=Qhc5qxmFYLwsyyQ23EGOqAT5dOAcZAvAp4uA9fFkbtejV8IgKKgqM/MYnGeExgpWv7 +LGbCHUHxZlei1yn+We13D+JXhlxaimLk3X7LXFMlj37tBT0/SgMAr1p15mtMcVuKrPg updEE8L5mVgO9yFiUAre7Rk2TVM6mgSKWqELmTO7X1acM6tH5OoCECJfchpogLtsm7ZF WiGovulaeVyCa1Y9IDQbPTR8+UR5eu+FY81tlPth2ES+vTtoFvkEMuOz4aa4d1lcTdfl DsRCPduqmsvxKgRBZHpDXThvkniGYiHLrL9Tnnv0UdkkUeeszXi5btiUVS5XyA21MzxO DMUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=j8120/bi8YMktJRRE+fxWRLX3mODqy/VEouEmAOGUQM=; fh=vcb1WTwJPUMTZX6OxkyO+5cKBEoTRHpgQZ5gmsle4eE=; b=tA8Ztbz7EQdAf3HiX6dWxQhRqp+G5YYXKgR0Sm9VO85KUP5KHK38Nqe/oK39OMi1sy SRTWx4G9mRSKUYLfIBuZJbRM4H6jy4LEE6UaHn5249FJkytDIpL+AtZly/SVPS7rwqZP 9wzQNe07Ez5TEwLpIj+87h+JCPulnuyZSQ++v4NNaBVd4Dg/M9OJfubU/3SfNPGWHaDo T5QrVp4ZnOMwv/rgvsZ6poWJpNjVZmXVxGomck244H8bONglDwDrOOHxpBxtpXWX/Bbv FerbJUsBqm+iHJwnnD0d7H3nOvdjtn9jiMmWp27e8O0Yljg9RpNV+nY/y9RaeojrjF4u ECYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v15-20020a170906488f00b0099bcf3c0ac6si1555975ejq.472.2023.07.27.13.31.05; Thu, 27 Jul 2023 13:31:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232185AbjG0UFi (ORCPT + 99 others); Thu, 27 Jul 2023 16:05:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230468AbjG0UFh (ORCPT ); Thu, 27 Jul 2023 16:05:37 -0400 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0133213F; Thu, 27 Jul 2023 13:05:35 -0700 (PDT) Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 5.2.0) id 31a11507767cd30a; Thu, 27 Jul 2023 22:05:33 +0200 Authentication-Results: v370.home.net.pl; spf=softfail (domain owner discourages use of this host) smtp.mailfrom=rjwysocki.net (client-ip=195.136.19.94; helo=[195.136.19.94]; envelope-from=rjw@rjwysocki.net; receiver=) Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 290E7661E19; Thu, 27 Jul 2023 22:05:33 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Peter Zijlstra , Anna-Maria Behnsen , Frederic Weisbecker , Kajetan Puchalski Subject: [PATCH v1] cpuidle: teo: Update idle duration estimate when choosing shallower state Date: Thu, 27 Jul 2023 22:05:33 +0200 Message-ID: <4506480.LvFx2qVVIh@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedviedrieeggdduudekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkfgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepffffffekgfehheffleetieevfeefvefhleetjedvvdeijeejledvieehueevueffnecukfhppeduleehrddufeeirdduledrleegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelhedrudefiedrudelrdelgedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohepiedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehpvghtvghriiesihhnfhhrrgguvggrugdrohhrghdprhgtphhtthhopegrnhhnrgdqmhgrrhhirgeslhhinhhuthhrohhnihigrdguvgdprhgtphhtthhopehfrhgvuggv rhhitgeskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgrjhgvthgrnhdrphhutghhrghlshhkihesrghrmhdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=6 Fuz1=6 Fuz2=6 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki The TEO governor takes CPU utilization into account by refining idle state selection when the utilization is above a certain threshold. The idle state selection is then refined by choosing an idle state shallower than the previously selected one. However, when this is done, the idle duration estimate needs to be updated so as to prevent the scheduler tick from being stopped while the candidate idle state is shallow, which may lead to excessive energy usage if the CPU is not interrupted quickly enough going forward. Moreover, in case the scheduler tick has been stopped already and the new idle duration estimate is too small, the replacement candidate state cannot be used. Modify the relevant code to take the above observations into account. Fixes: 9ce0f7c4bc64 ("cpuidle: teo: Introduce util-awareness") Signed-off-by: Rafael J. Wysocki --- @Peter: This doesn't attempt to fix the tick stopping problem, it just makes the current behavior consistent. @Anna-Maria: This is likely to basically prevent the tick from being stopped at all if the CPU utilization is above a certain threshold. I'm wondering if your results will be affected by it and in what way. --- drivers/cpuidle/governors/teo.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) Index: linux-pm/drivers/cpuidle/governors/teo.c =================================================================== --- linux-pm.orig/drivers/cpuidle/governors/teo.c +++ linux-pm/drivers/cpuidle/governors/teo.c @@ -397,13 +397,22 @@ static int teo_select(struct cpuidle_dri * the shallowest non-polling state and exit. */ if (drv->state_count < 3 && cpu_data->utilized) { - for (i = 0; i < drv->state_count; ++i) { - if (!dev->states_usage[i].disable && - !(drv->states[i].flags & CPUIDLE_FLAG_POLLING)) { - idx = i; + /* + * If state 0 is enabled and it is not a polling one, select it + * right away and update the idle duration estimate accordingly, + * unless the scheduler tick has been stopped. + */ + if (!idx && !(drv->states[0].flags & CPUIDLE_FLAG_POLLING)) { + s64 span_ns = teo_middle_of_bin(0, drv); + + if (teo_time_ok(span_ns)) { + duration_ns = span_ns; goto end; } } + /* Assume that state 1 is not a polling one and select it. */ + idx = 1; + goto end; } /* @@ -539,10 +548,20 @@ static int teo_select(struct cpuidle_dri /* * If the CPU is being utilized over the threshold, choose a shallower - * non-polling state to improve latency + * non-polling state to improve latency, unless the scheduler tick has + * been stopped already and the shallower state's target residency is + * not sufficiently large. */ - if (cpu_data->utilized) - idx = teo_find_shallower_state(drv, dev, idx, duration_ns, true); + if (cpu_data->utilized) { + s64 span_ns; + + i = teo_find_shallower_state(drv, dev, idx, duration_ns, true); + span_ns = teo_middle_of_bin(i, drv); + if (teo_time_ok(span_ns)) { + idx = i; + duration_ns = span_ns; + } + } end: /*