From patchwork Wed May 24 07:06:21 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: 98333 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2650982vqo; Wed, 24 May 2023 00:10:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6aKqKobIO8fS0lHYrZ5Ykd0coM6eHQKYVr8dSBXUpdMraF0+ZTE68j79Qpi8+/Cf/Kdp9A X-Received: by 2002:a05:6a20:160e:b0:10b:6b1f:acf1 with SMTP id l14-20020a056a20160e00b0010b6b1facf1mr12150746pzj.29.1684912249796; Wed, 24 May 2023 00:10:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684912249; cv=none; d=google.com; s=arc-20160816; b=tT7wq1OkBx65XW6d9E4Eqfx1tj9qIVpAxfvIxk34WmdBedUg5RApQu8BEC/nuFYSri aZo1vlNjeuOnn4ajgdgmnaPCd3GUkh8rPH19pZHrIcnoDTI2CqpoGUBwbrkjuCtTP1Tz E/Iq/qOCGitN9a1TeUT/K0lnVJKt1NX+5aKyykaBIn9htT351DDEYua9dJ+IQ3jqRlcg qqOgFLf3eXeOyeq6rJjtoe9EkEYeD9TuA3AtpkVGCDv5X43kuvT35FQTsVsT7Vx5zzf1 RhuxwxTFdW8aWZ5cpo078mvDg2mpbfg9ZWDFKKcxqwgQco/QNKnyWHp1gtNfpVUEjJl3 +2/g== 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=r6aAJpUsFbdAiX81BLzrgrPjSrKyBdcLGR/988cXOog=; b=uMGXTJaCrqDfo3l3CWuERGpICPDPN+ut1Yom26pDEb7XGlm6cqRXzxr12vms2rM4il WB/pbOBuVh0q96dIF3YQRN1heZjR1xZZLzdJ/yGlJXRDmTPtzJZVx9WQov59dM0xzA7C 88VpaOBZS8tLnQY1w2DS7gYIW0D74IcuZZEEEz3c0F4WypPJTOcAIhw9UYOejOTukjSL bXBv8lG3PwtusOBraV4kLi8xhCekrS+u7LHxpGnGsywPbaVSXZ5HqQwwPA735FsEod/v M4PsUBl8daYN5q/WyIFzr0e25U2SKNaTRm5S0Zy1bb4YZpicttpyJHn7yM7fSyTIccH/ cXrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=dotju3Hn; dkim=neutral (no key) header.i=@linutronix.de header.b=NY8bKGCP; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z28-20020aa79e5c000000b0064354230c2asi429821pfq.367.2023.05.24.00.10.37; Wed, 24 May 2023 00:10:49 -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; dkim=pass header.i=@linutronix.de header.s=2020 header.b=dotju3Hn; dkim=neutral (no key) header.i=@linutronix.de header.b=NY8bKGCP; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239941AbjEXHHz (ORCPT + 99 others); Wed, 24 May 2023 03:07:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239860AbjEXHHO (ORCPT ); Wed, 24 May 2023 03:07:14 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6261B1A7 for ; Wed, 24 May 2023 00:07:04 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1684912022; 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=r6aAJpUsFbdAiX81BLzrgrPjSrKyBdcLGR/988cXOog=; b=dotju3Hn6qkQGsykvxfeiHzdDDTEMSn5UJX94gDlmxsBbeo2gk1Uao0P/JJhqVqG2+IQK+ 223ALp6/P+ZME0M4pYKnWqHYUNzikSZ/glwLMO8GRewxoeCA6LB9DGxAxe3MmdaYW5kAgU aAX1cI8VGgxUvBhBIQbeASIZKcvV7pfH1u/kBgavVB/sSwEdFBDyTdr5ZcMYsf1o5Ob/W/ 72OPmK1LeGzrVqsb0kKUJ7i37MpffOa7ACt8MVJ9eWnmvxZKpBYzeUsB3iqBytPbV3y4h/ c59ApQaapOxjyn7nGk8mvsEWT8krbSBGa9egMV5nzv+mx4e18PQXZ8Tpafbncg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1684912022; 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=r6aAJpUsFbdAiX81BLzrgrPjSrKyBdcLGR/988cXOog=; b=NY8bKGCPTTmbfMpu7VN7uc27lpyCP0SGl5UQuM/cv0smZEQTlQYK5kdoEHNDLLCUgNXMof 15sd8FJPNbnH4PDw== 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" , Anna-Maria Behnsen , Richard Cochran , Frederic Weisbecker Subject: [PATCH v7 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately Date: Wed, 24 May 2023 09:06:21 +0200 Message-Id: <20230524070629.6377-14-anna-maria@linutronix.de> In-Reply-To: <20230524070629.6377-1-anna-maria@linutronix.de> References: <20230524070629.6377-1-anna-maria@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766758546956142121?= X-GMAIL-MSGID: =?utf-8?q?1766758546956142121?= For the conversion of the NOHZ timer placement to a pull at expiry time model it's required to have separate expiry times for the pinned and the non-pinned (movable) timers. Therefore struct timer_events is introduced. No functional change Originally-by: Richard Cochran (linutronix GmbH) Signed-off-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- kernel/time/timer.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 010c8877fa85..e4b50760da3f 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -221,6 +221,11 @@ struct timer_base { static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]); +struct timer_events { + u64 local; + u64 global; +}; + #ifdef CONFIG_NO_HZ_COMMON static DEFINE_STATIC_KEY_FALSE(timers_nohz_active); @@ -1997,17 +2002,17 @@ static void forward_base_clk(struct timer_base *base, unsigned long nextevt, */ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) { + struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX }; unsigned long nextevt, nextevt_local, nextevt_global; struct timer_base *base_local, *base_global; bool local_first, is_idle; - u64 expires = KTIME_MAX; /* * Pretend that there is no timer pending if the cpu is offline. * Possible pending timers will be migrated later to an active cpu. */ if (cpu_is_offline(smp_processor_id())) - return expires; + return tevt.local; base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]); base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]); @@ -2052,16 +2057,46 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) /* We need to mark both bases in sync */ base_local->is_idle = base_global->is_idle = is_idle; - if (base_local->timers_pending || base_global->timers_pending) { + /* + * If the bases are not marked idle, i.e one of the events is at + * max. one tick away, then the CPU can't go into a NOHZ idle + * sleep. Use the earlier event of both and store it in the local + * expiry value. The next global event is irrelevant in this case + * and can be left as KTIME_MAX. CPU will wakeup on time. + */ + if (!is_idle) { /* If we missed a tick already, force 0 delta */ if (time_before(nextevt, basej)) nextevt = basej; - expires = basem + (u64)(nextevt - basej) * TICK_NSEC; + tevt.local = basem + (u64)(nextevt - basej) * TICK_NSEC; + goto unlock; } + + /* + * If the bases are marked idle, i.e. the next event on both the + * local and the global queue are farther away than a tick, + * evaluate both bases. No need to check whether one of the bases + * has an already expired timer as this is caught by the !is_idle + * condition above. + */ + if (base_local->timers_pending) + tevt.local = basem + (u64)(nextevt_local - basej) * TICK_NSEC; + + /* + * If the local queue expires first, then the global event can be + * ignored. The CPU wakes up before that. If the global queue is + * empty, nothing to do either. + */ + if (!local_first && base_global->timers_pending) + tevt.global = basem + (u64)(nextevt_global - basej) * TICK_NSEC; + +unlock: raw_spin_unlock(&base_global->lock); raw_spin_unlock(&base_local->lock); - return cmp_next_hrtimer_event(basem, expires); + tevt.local = min_t(u64, tevt.local, tevt.global); + + return cmp_next_hrtimer_event(basem, tevt.local); } /**