From patchwork Fri Dec 1 09:26:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 172350 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp991780vqy; Fri, 1 Dec 2023 01:28:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2on7B6pt1/HH3OYiS/UgiqM+8peI1lqkMNOog3152icdCzEOrZKITOLgTzItEqd7setJM X-Received: by 2002:a05:6358:70b:b0:16d:fe33:4c63 with SMTP id e11-20020a056358070b00b0016dfe334c63mr27182966rwj.24.1701422885071; Fri, 01 Dec 2023 01:28:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701422885; cv=none; d=google.com; s=arc-20160816; b=IIT9wb2V8RwUGa7CAiUgt2RSwhoFcWdqNbyjIRkKBKK/VpicejZABA+FvfpLPKFYMe r8GURFYUmLGvkKcDoc6/ywJCgfKUizlvZLggHU3BQbzl2trSB1vhW2+OBHekJm65aDIn WVzZHOGt71ybAPqYkpJ0XlImU3H+lFIx/zJgIwHzMfOZ+bryyAF4XGGGrppRv4TAnIZz 3zul0ZmsYumCmiDprX2r1B5c3a4QxNJQmY7zQce90b5pLiQQqn1ntQrHSANUP1BRVEQ+ vAF9noVBXIcOa346OD9I+eHjMCYhYnkZEve0ftytAviYnzRAXf4mHu/78dL5e0RpxqZu lBtg== 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:dkim-signature :dkim-signature:from; bh=OjHpvhdurB+xA/Q5Qjg90zYVhHrCM9ixI3FXScsHyt8=; fh=PG6uS4TiiUSDyl8D/joYkWbwCgDm4ug0ir2h7tHBJXQ=; b=CahPC9GDSpvdRMkmBOSD/ld0Mt0f5Gy7CixOcBiRpWxu4OyIYBDq7l5S8z/NoNNPS3 L4OFpx3GVN3WDFSl6H0k4hkU0zBWaYTbWIUMI8wQ358J5z+/pBThlUxfVva8AaKqoPLU fZ/X2e+ebfNj60HqxsfUlpfQNw5u6FzJVo6CiJlMfoqNRIgqfb13yoS7FUJDt3x4tB6H Pa767ypAnmj9mlzGfXSgIEzXRjQUiwVs8aYcsPD36+REVFAwF5/U8dnajB3OpbflE8D7 q7A8m18ic+oMWoaVajJNUS5RYCU6u1ff6EpRyjvQTNX59h6ECfnQuxSD7bpfHc9QX+to HQCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=KKGSI58S; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Aml3ir39; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id h8-20020a056a00218800b006cba60de063si2990987pfi.22.2023.12.01.01.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 01:28:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=KKGSI58S; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Aml3ir39; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 276F2825F14B; Fri, 1 Dec 2023 01:27:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378122AbjLAJ1p (ORCPT + 99 others); Fri, 1 Dec 2023 04:27:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378050AbjLAJ1W (ORCPT ); Fri, 1 Dec 2023 04:27:22 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03BF8171C for ; Fri, 1 Dec 2023 01:27:14 -0800 (PST) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1701422833; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OjHpvhdurB+xA/Q5Qjg90zYVhHrCM9ixI3FXScsHyt8=; b=KKGSI58SUmBD5Umq/6Q4hIqdbuQjSnzca6zjvAccM2ulw1J6bMv4sXp813ZrMIM1hVtQVH MspT53qWCRzuqvVxUVkoZ04eH80zx/S7MKiw82z+OJKUhRBCi4XQI0/1WlXc2sqf13LW5W IliQXqnyGckto5IWaqLX7GyU0+RzB8zepKOF/AnVpcJ+6MNpR5HCo26arlDmQf94v6jG3J wUax+CBx48cBjE5Axx9p7sO19r8k8gYwRvLBrc5EXDJDpWBcX66pOL4Ys/OiTDUdjW/xc+ t8UrhivzaOHFUQCi/N8COFa/nSqBBhaGOO7CG3xoQe4tqPVGSUhVWd0rxerE7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1701422833; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OjHpvhdurB+xA/Q5Qjg90zYVhHrCM9ixI3FXScsHyt8=; b=Aml3ir39NvV+AcjCiHTJCqqN6AvMpP5DwCTpdnC7/AHu45ZPNUabml56+ThbNpQsMZFpb2 eyfv64zM1EqeFwBg== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Thomas Gleixner , Eric Dumazet , "Rafael J . Wysocki" , Arjan van de Ven , "Paul E . McKenney" , Frederic Weisbecker , Rik van Riel , Steven Rostedt , Sebastian Siewior , Giovanni Gherdovich , Lukasz Luba , "Gautham R . Shenoy" , Srinivas Pandruvada , K Prateek Nayak , Anna-Maria Behnsen Subject: [PATCH v9 11/32] timers: Rework idle logic Date: Fri, 1 Dec 2023 10:26:33 +0100 Message-Id: <20231201092654.34614-12-anna-maria@linutronix.de> In-Reply-To: <20231201092654.34614-1-anna-maria@linutronix.de> References: <20231201092654.34614-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 01 Dec 2023 01:27:55 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784071203084274753 X-GMAIL-MSGID: 1784071203084274753 From: Thomas Gleixner To improve readability of the code, split base->idle calculation and expires calculation into separate parts. While at it, update the comment about timer base idle marking. Thereby the following subtle change happens if the next event is just one jiffy ahead and the tick was already stopped: Originally base->is_idle remains true in this situation. Now base->is_idle turns to false. This may spare an IPI if a timer is enqueued remotely to an idle CPU that is going to tick on the next jiffy. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- v9: Re-ordering to not hurt the eyes and update comment v4: Change condition to force 0 delta and update commit message (Frederic) --- kernel/time/timer.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index fee42dda8237..0826018d9873 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1943,22 +1943,23 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) */ __forward_timer_base(base, basej); - if (time_before_eq(nextevt, basej)) { - expires = basem; - base->is_idle = false; - } else { - if (base->timers_pending) - expires = basem + (u64)(nextevt - basej) * TICK_NSEC; - /* - * If we expect to sleep more than a tick, mark the base idle. - * Also the tick is stopped so any added timer must forward - * the base clk itself to keep granularity small. This idle - * logic is only maintained for the BASE_STD base, deferrable - * timers may still see large granularity skew (by design). - */ - if ((expires - basem) > TICK_NSEC) - base->is_idle = true; + if (base->timers_pending) { + /* If we missed a tick already, force 0 delta */ + if (time_before(nextevt, basej)) + nextevt = basej; + expires = basem + (u64)(nextevt - basej) * TICK_NSEC; } + + /* + * Base is idle if the next event is more than a tick away. + * + * If the base is marked idle then any timer add operation must forward + * the base clk itself to keep granularity small. This idle logic is + * only maintained for the BASE_STD base, deferrable timers may still + * see large granularity skew (by design). + */ + base->is_idle = time_after(nextevt, basej + 1); + trace_timer_base_idle(base->is_idle, base->cpu); raw_spin_unlock(&base->lock);