Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp6999220pxv; Fri, 30 Jul 2021 07:41:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxwQX7VgFKkGom9ome3bac/mLLE3aS/UXa8U8cUcUIPHcagyDZgtQj1Mp8Op2Mt3YSH4/h X-Received: by 2002:a05:6e02:1d8a:: with SMTP id h10mr1243291ila.20.1627656090033; Fri, 30 Jul 2021 07:41:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627656090; cv=none; d=google.com; s=arc-20160816; b=gJm1A6ZREcCOE6H/WESPcuEqMgtnBAuwAyFFBJ7a6dgEPew2l5b6Yh5HvXj1W6YbVz 2YemWktby9EH9hWecJ9X82NmZBOzg6STW/P+51cfWy3727za2cQnMl0maZ8ZiMGjejJ8 69aoqF/wR776RvOrv5gqSTfAYNgQKZpmsxg5RSU3gPTJBdRygQyQN2J44Tkd9iFcTxS8 8QRJPuRIgrZyu04w8QL2dIahi9Hb/Ofw0z1b2J5EpFtCdlsv7sobpauW4ZfOSw/A1v4g EedwEhuGJn6vUPZMpuv6GyYIj3hSLpSZopNfr7l5i/Mrd+EFv3ZRwHwNL9Oko+aMOylG /v4g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=iChjSvV691b+Upvu1mBOdzSAEROUzBjXdE+R/5MqYec=; b=U+RVxjx+HsAog/NtU+qYRRUahX+MDHDmoZxy03OLP0tcufpZsLIfw5V0hiapNoNfvh FC0ItEcBdrkqnkzWjBXxMeHEpzxVIABG5QwdKZt6iGA92K4n7h7Ni0em1lriuFJ1t2C/ zsWwmiZl9B0Rg/zyMUfRZSbohd2ya2gRERcD1keNbdZy8NJyRwIuPgcCgpSoGC16+mM9 ExIVY2Amuaut35PbnW3JyXoa882dWZl0uveruh9JXbeVoITzuJiGp/j58/syyiVSG+Aa UE/RrKQh2QqgR3YmS/uKZd3QQlJG3RKpbBuq2wgp1vRqbxNodF6c5y7wuIx2CZNZwvfb P4aQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d21si1888955jam.9.2021.07.30.07.41.18; Fri, 30 Jul 2021 07:41:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239421AbhG3Ojt (ORCPT + 99 others); Fri, 30 Jul 2021 10:39:49 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:46184 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239339AbhG3Ojl (ORCPT ); Fri, 30 Jul 2021 10:39:41 -0400 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 3.0.0) id 846070bec0f569d8; Fri, 30 Jul 2021 16:39:35 +0200 Received: from kreacher.localnet (89-64-81-8.dynamic.chello.pl [89.64.81.8]) (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 78DF7669F3A; Fri, 30 Jul 2021 16:39:34 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux PM , Doug Smythies Cc: LKML , "Rafael J. Wysocki" Subject: [PATCH v1 1/2] cpuidle: teo: Fix alternative idle state lookup Date: Fri, 30 Jul 2021 16:38:07 +0200 Message-ID: <1794352.tdWV9SEqCh@kreacher> In-Reply-To: <4336554.LvFx2qVVIh@kreacher> References: <4336554.LvFx2qVVIh@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 89.64.81.8 X-CLIENT-HOSTNAME: 89-64-81-8.dynamic.chello.pl X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvtddrheehgdejgecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufffkfgjfhgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepvdejlefghfeiudektdelkeekvddugfeghffggeejgfeukeejleevgffgvdeluddtnecukfhppeekledrieegrdekuddrkeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeekledrieegrdekuddrkedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegushhmhihthhhivghssehtvghluhhsrdhnvghtpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheprhgrfhgrvghlsehkvghrnhgvlhdrohhrgh X-DCC--Metrics: v370.home.net.pl 1024; Body=4 Fuz1=4 Fuz2=4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki There are three mistakes in the loop in teo_select() that is looking for an alternative candidate idle state. First, it should walk all of the idle states shallower than the current candidate one, including all of the disabled ones, but it terminates after the first enabled idle state. Second, it should not terminate its last step if idle state 0 is disabled (which is related to the first issue). Finally, it may return the current alternative candidate idle state prematurely if the time span criterion is not met by the idle state under consideration at the moment. To address the issues mentioned above, make the loop in question walk all of the idle states shallower than the current candidate idle state all the way down to idle state 0 and rearrange the checks in it. Fixes: 77577558f25d ("cpuidle: teo: Rework most recent idle duration values treatment") Reported-by: Doug Smythies Signed-off-by: Rafael J. Wysocki --- drivers/cpuidle/governors/teo.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 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,32 +397,46 @@ static int teo_select(struct cpuidle_dri intercept_sum = 0; recent_sum = 0; - for (i = idx - 1; i >= idx0; i--) { + for (i = idx - 1; i >= 0; i--) { struct teo_bin *bin = &cpu_data->state_bins[i]; s64 span_ns; intercept_sum += bin->intercepts; recent_sum += bin->recent; + span_ns = teo_middle_of_bin(i, drv); + + if ((!alt_recent || 2 * recent_sum > idx_recent_sum) && + (!alt_intercepts || + 2 * intercept_sum > idx_intercept_sum)) { + if (teo_time_ok(span_ns) && + !dev->states_usage[i].disable) { + idx = i; + duration_ns = span_ns; + } else { + /* + * The current state is too shallow or + * disabled, so take the first enabled + * deeper state with suitable time span. + */ + idx = last_enabled_idx; + duration_ns = last_enabled_span_ns; + } + break; + } + if (dev->states_usage[i].disable) continue; - span_ns = teo_middle_of_bin(i, drv); if (!teo_time_ok(span_ns)) { /* - * The current state is too shallow, so select - * the first enabled deeper state. + * The current state is too shallow, but if an + * alternative candidate state has been found, + * it may still turn out to be a better choice. */ - duration_ns = last_enabled_span_ns; - idx = last_enabled_idx; - break; - } + if (last_enabled_idx != idx) + continue; - if ((!alt_recent || 2 * recent_sum > idx_recent_sum) && - (!alt_intercepts || - 2 * intercept_sum > idx_intercept_sum)) { - idx = i; - duration_ns = span_ns; break; }