Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp91980lqs; Mon, 4 Mar 2024 16:28:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVvpFrvALYVicyx05FSqbQ3mUEZeD90pI2ac4JQrGL2F87NaqHBi73mgrqV7mhINgPpn8fLDzcv8jv1+QCfUPS2Svgk65kvc2uaZrh4VQ== X-Google-Smtp-Source: AGHT+IG2mHvEgAHcKWd1DA5ZvJEi8O79/8umjW+gk22hvI/6t5IEe1IYDB2gYt/ZlrhoebNYJOsa X-Received: by 2002:a05:6a20:f14:b0:19c:9ebc:93b1 with SMTP id fl20-20020a056a200f1400b0019c9ebc93b1mr265641pzb.59.1709598521513; Mon, 04 Mar 2024 16:28:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709598521; cv=pass; d=google.com; s=arc-20160816; b=k0MolI46WY9GOoQSHOPLNZrrj0JIqUblF8W+bmcbdtPZkHZGhDgxla1gtE9BrbZ8uk xCPzqSDXTYrvY49wZOepjonESQ1kH0d5GFNzIW3gSMxE+eSEWuL2+ZxR3sOt5avFwDhV Yi6jNgVYORdm3GARwAsrOc95nafZx6Ps1oRgIVbvV3j/CObKNLXzFtRFKaBiLVEJN1zZ BPTCQjcbcYg9bGkfrZvuuWpPCVn1XzlRUz8ni4Gk46pK+80QMUm6+Z9Bg2C/JtZA45IT BEC929iMHDCnnVlg2I/mhQd/lZ6MHPr02ZDt8LST197Vxqnmyf73hHhY4qb1XgONRxx3 6nGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=5Jn5k9PFBtl9Rtj3H0zWHhyLlH0sueExrZ0pMMfy+S0=; fh=XYGdR8Zmf5kR+Aw/VnTqVlujPjUVVy/FXFDNf11mBOc=; b=ohhOGzS5W8YUhw0NlmDzB4A8ezPGceJ6Vz/cGofla+kui99wxOCa5Nq/2qwPi3RmgC 1M/p8C1346hHyZvZ+mhXpnyANbkwbIvyZberAsT4k5HPU8pesjKzo3d1YdYyr2A97+la plsJZnAboqaGMeB2GKhuOiBXCvzNe8r1Wm6IQwcCGu/YSkFe/Q1SdWQGhGeH+DTICOua A506sp3nw8qBuw3bfiYcV0slCspxGEeuYCh+e5Cox/L7TifrvyEp3DQrqnNqTheMrlOR 1RkVjzIobIRQ3Wo0z3PsAPfD9UBZ4wpFSfgGMrwws8nFKQMYdyZcssXQu063GpKhqMTm 2osw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rc5+QLUe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-91454-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91454-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bd20-20020a056a00279400b006e4cf833cc7si8958765pfb.233.2024.03.04.16.28.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 16:28:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91454-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Rc5+QLUe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-91454-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91454-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 09EE4B21286 for ; Tue, 5 Mar 2024 00:28:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 185285C83; Tue, 5 Mar 2024 00:28:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Rc5+QLUe" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 378464C96 for ; Tue, 5 Mar 2024 00:28:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709598508; cv=none; b=n9tp+L+O1kzeU8Hd3XwK5rKIDwCKESv3ZwUdGrmZCFdkIOKfy29cQmh6PcKq6+DvK6erqku4mWyseTvYy80BxBYsvFkyzBq8bkY1jvny9YGNPwXl30D3zw1nggTBHHm7ajPCQ5Cuofd+gyHyTViEfT6/P6UQSNf73yW62RQsXhc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709598508; c=relaxed/simple; bh=S/MxXC0LyuU+rBL34uUkqZ7q4SGqxtyGFgsbajGBEcs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=L+VwmNRY968pfgzjUFjd/jxGKs36G34pERO8H0h2JcIGx4n/B9TTnN1t59yqqjnmUOjsi/lax0m0xDFjI6EvamwIjejqrFzRtLl1iKb1MFC4gmcJc6miroBTOJSAAWh98/SRz3B19uSyAnHQ+jXX3HKlwUaxl0Kzuuaov0fFGo4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Rc5+QLUe; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC955C433F1; Tue, 5 Mar 2024 00:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709598507; bh=S/MxXC0LyuU+rBL34uUkqZ7q4SGqxtyGFgsbajGBEcs=; h=From:To:Cc:Subject:Date:From; b=Rc5+QLUe9qB1IOVNuhSSLxyA/R0diXuNTa/lnQ0xWy3WI3ViHgxYif7nCRHy1Afob YmFhQyaHqqsAkqr+dFrk96K24BdWJPn+o480Tr1RiqpM1AuYk0rSdVdS44PY1hBJjW oucE87ka0LPwAMFvotTo7hG3t9Rl4kK04LmqYLc/d+YsdLDY2hMRXT8l0AzgoQBIA+ lYa8yr2l6dE+7T+G3/z3WtOJ67YxIeWPptUlZGhEcGviv45PHjODjF5+uJFvx2uo4Y 3lfF2+g1qXDuYtld67zDYYxm8tm7SWEQqq8ZMHSm82xGSMi+2DTn34RUL7QDWEzby6 68X47U8y8D4+g== From: Frederic Weisbecker To: Anna-Maria Behnsen , Thomas Gleixner Cc: LKML , Frederic Weisbecker Subject: [PATCH] timer/migration: Fix quick check reporting late expiry Date: Tue, 5 Mar 2024 01:28:22 +0100 Message-ID: <20240305002822.18130-1-frederic@kernel.org> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When a CPU is the last active in the hierarchy and it tries to enter into idle, the quick check looking up the next event towards cpuidle heuristics may report a too late expiry, such as in the following scenario: [GRP1:0] migrator = NONE active = NONE nextevt = T0:0, T0:1 / \ [GRP0:0] [GRP0:1] migrator = NONE migrator = NONE active = NONE active = NONE nextevt = T0, T1 nextevt = T2 / \ / \ 0 1 2 3 idle idle idle idle 0) The whole system is idle, and CPU 0 was the last migrator. CPU 0 has a timer (T0), CPU 1 has a timer (T1) and CPU 2 has a timer (T2). The expire order is T0 < T1 < T2. [GRP1:0] migrator = GRP0:0 active = GRP0:0 nextevt = T0:0(i), T0:1 / \ [GRP0:0] [GRP0:1] migrator = CPU0 migrator = NONE active = CPU0 active = NONE nextevt = T0(i), T1 nextevt = T2 / \ / \ 0 1 2 3 active idle idle idle 1) CPU 0 becomes active. The (i) means a now ignored timer. [GRP1:0] migrator = GRP0:0 active = GRP0:0 nextevt = T0:1 / \ [GRP0:0] [GRP0:1] migrator = CPU0 migrator = NONE active = CPU0 active = NONE nextevt = T1 nextevt = T2 / \ / \ 0 1 2 3 active idle idle idle 2) CPU 0 handles remote. No timer actually expired but ignored timers have been cleaned out and their sibling's timers haven't been propagated. As a result the top level's next event is T2 and not T1. 3) CPU 0 tries to enter idle without any global timer enqueued and calls tmigr_quick_check(). The expiry of T2 is returned instead of the expiry of T1. When the quick check returns an expiry that is too late, the cpuidle governor may pick up a C-state that is too deep. This may be result into undesired CPU wake up latency if the next timer is actually close enough. Fix this with assuming that expiries aren't sorted top-down while performing the quick check. Pick up instead the earliest encountered one while walking up the hierarchy. Signed-off-by: Frederic Weisbecker --- kernel/time/timer_migration.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/kernel/time/timer_migration.c b/kernel/time/timer_migration.c index d85aa2afb969..085b1d86aba9 100644 --- a/kernel/time/timer_migration.c +++ b/kernel/time/timer_migration.c @@ -1385,11 +1385,11 @@ u64 tmigr_cpu_deactivate(u64 nextexp) * single group active on the way to top level) * * nextevt - when CPU is offline and has to handle timer on his own * or when on the way to top in every group only a single - * child is active and but @nextevt is before next_expiry - * of top level group - * * next_expiry (top) - value of top level group, when on the way to top in - * every group only a single child is active and @nextevt - * is after this value active child. + * child is active but @nextevt is before the lowest + * next_expiry encountered while walking up to top level. + * * next_expiry - value of lowest expiry encountered while walking groups + * if only a single child is active on each and @nextevt + * is after this lowest expiry. */ u64 tmigr_quick_check(u64 nextevt) { @@ -1408,10 +1408,16 @@ u64 tmigr_quick_check(u64 nextevt) do { if (!tmigr_check_lonely(group)) { return KTIME_MAX; - } else if (!group->parent) { - u64 first_global = READ_ONCE(group->next_expiry); - - return min_t(u64, nextevt, first_global); + } else { + /* + * Since current CPU is active, events may not be sorted + * from bottom to the top because the CPU's event is ignored + * up to the top and its sibling's events not propagated upwards. + * Thus keep track of the lowest observed expiry. + */ + nextevt = min_t(u64, nextevt, READ_ONCE(group->next_expiry)); + if (!group->parent) + return nextevt; } group = group->parent; } while (group); -- 2.44.0