From patchwork Wed Dec 20 15:59:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 181714 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2739674dyi; Wed, 20 Dec 2023 08:01:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IEeDfB41RHQ6yHQd7hGS/qe+h5yP7uS3sXFdyOrfJGvMRjfDZneqbEzU48D6OMPlMgIBhBi X-Received: by 2002:a17:903:947:b0:1d3:be34:7862 with SMTP id ma7-20020a170903094700b001d3be347862mr4432050plb.9.1703088066164; Wed, 20 Dec 2023 08:01:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703088066; cv=none; d=google.com; s=arc-20160816; b=Dvm5p4n1goFMllv3JOgrxdf8FDHiSck71ebcZPpSWdi4K1xxzL7AKggP9356HlKVY2 bEukVBOx5xblOxDvCC8XTmecN332wdJOg0q8ZSYPyW5C4DHp545tlBSERF3A5/PQSwjg hJTzugeFOuCii+AKSnKWuFf+gxwwuzZCliaz7cnE5D0ir8BPPFR00bu/NDVu6He5eVsZ /daDL98sbFtu+MKbtRUhizlM29jf/yof9vX/B/96c40Q1zW+oRT4LD6Nq93kHPMJ2CTX fFDd4zs+57Jn7vOD6dXYgu2oocxpuVnNVTC4AcVvlM/+UsPiwFvi0LQXhcFNxJ22c+0o 7+cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=Td3wSB/DOZuCWjh238SHCOj/5C7U3BaCUMkrlEk/388=; fh=ENrP0zi+TtnXLYbnEXWSvt8M3dhdNVE1J323rl9XJEw=; b=Ys0SH08nTxN9m0ePbS3Q2BlgFUiBx+kZ7G4kY/hoqTWkZryH0+fUylJHCic3qn49Xb 0lKqP8XzLmg1os0c/YqLdrMbb9xr+y2NXvSUCPaKgB9sgHg/1/j4yT8F5MGPdxlC6vBD gbTt6EhlpSsl+lGACEByD457znAPoKOHxn6UDLiGUeIpAAkZOHyPV4kLazAkZojPF5/l 9rjHfbJxptu3BX1qAquzQL6gfGDzLcCmGOlPz13XY7gNH8Yc7PnpqbtjM/JLkYYhwueZ PFuzSO4vbKTrjHl/StrpjAT5z9foWcn5wXvGLTfofyzymoC1AiQUmwJMan/S57aAaa69 XLQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Px0fbsSI; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel+bounces-7226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7226-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d4-20020a170902b70400b001d0ced77a90si21681842pls.510.2023.12.20.08.01.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 08:01:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Px0fbsSI; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel+bounces-7226-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7226-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id C5C96281B5B for ; Wed, 20 Dec 2023 16:01:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 392F1487A2; Wed, 20 Dec 2023 15:59:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Px0fbsSI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uwETzG2N" X-Original-To: linux-kernel@vger.kernel.org Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 2BA454777A; Wed, 20 Dec 2023 15:59:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Wed, 20 Dec 2023 15:59:05 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1703087946; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Td3wSB/DOZuCWjh238SHCOj/5C7U3BaCUMkrlEk/388=; b=Px0fbsSIqmcP197pR6oE38hcdlJFQyVyrm4yU9YHLrfYx/GseXsuqtl49yRt8D5o9EX2uK HFTYpdtKxZ8OC0PYQbYItji4dqPeJ/spwZPYbSKD91E7vcgolCBo2fKGoLPVHeZLhahhD9 TTn/UCsy2XqHMlvPJb/4fo3SgOk2KlwEIi0dYSPOBO8aHbCcE7+r68ONgq6Y5A4yDEl3+r NGy2DyG5IAgW1iVnmqGaf9dRvgCqtLREEWuf2VlQ1W+xaMO2zJ2rM3pnKzEQfRFclU8S/G foDPUMpna2C9f2Okuojx8zAe/n14CBXeAJ55SaZEo+ikEXYD8NIkF3+yeCL12A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1703087946; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Td3wSB/DOZuCWjh238SHCOj/5C7U3BaCUMkrlEk/388=; b=uwETzG2N/2EDF24Pk2vmCqQ2nlmvvuPFyJCZdsoOSr+7LOO6DR0gJtPPEL4ugwiFLSgAXR fmLVxk9kFyGXUECA== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] timers: Rework idle logic Cc: Thomas Gleixner , "Anna-Maria Behnsen" , Frederic Weisbecker , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20231201092654.34614-12-anna-maria@linutronix.de> References: <20231201092654.34614-12-anna-maria@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170308794576.398.14853285355334106633.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784071203084274753 X-GMAIL-MSGID: 1785817272229673404 The following commit has been merged into the timers/core branch of tip: Commit-ID: bb8caad5083f8fbba70faf41f1d3bab7cf09da6d Gitweb: https://git.kernel.org/tip/bb8caad5083f8fbba70faf41f1d3bab7cf09da6d Author: Thomas Gleixner AuthorDate: Fri, 01 Dec 2023 10:26:33 +01:00 Committer: Thomas Gleixner CommitterDate: Wed, 20 Dec 2023 16:49:39 +01:00 timers: Rework idle logic 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 Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Link: https://lore.kernel.org/r/20231201092654.34614-12-anna-maria@linutronix.de --- kernel/time/timer.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 1a73d39..cf51655 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1924,6 +1924,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); u64 expires = KTIME_MAX; unsigned long nextevt; + bool was_idle; /* * Pretend that there is no timer pending if the cpu is offline. @@ -1943,27 +1944,26 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) */ __forward_timer_base(base, basej); - if (time_before_eq(nextevt, basej)) { - expires = basem; - if (base->is_idle) { - base->is_idle = false; - trace_timer_base_idle(false, base->cpu); - } - } 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) { - base->is_idle = true; - trace_timer_base_idle(true, base->cpu); - } + 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). + */ + was_idle = base->is_idle; + base->is_idle = time_after(nextevt, basej + 1); + if (was_idle != base->is_idle) + trace_timer_base_idle(base->is_idle, base->cpu); + raw_spin_unlock(&base->lock); return cmp_next_hrtimer_event(basem, expires);