From patchwork Wed May 10 07:28:09 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: 91935 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3437059vqo; Wed, 10 May 2023 00:48:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5BvDIFSFnFnYxrRM1nU2+ZGZnTuGzBWjjMX4nagVAW7NJXnP3o0DfLerZM8tWMnV/m4dzt X-Received: by 2002:a05:6a20:7345:b0:da:d4eb:9e07 with SMTP id v5-20020a056a20734500b000dad4eb9e07mr20095680pzc.30.1683704929490; Wed, 10 May 2023 00:48:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683704929; cv=none; d=google.com; s=arc-20160816; b=gW7iSvJKYhegKzjS3YLDspCc5eaVKIglm2rDkZhmoq0TivtAhP5sGOiTUQ50Yk1/gx FErkA9zvisl1GlZ5KD6RYDxpKIgv6FZkoCnNemmVIDtbIZsZW/UXqStbScPqCW5gGJux Qm2ETgd9rLwZ7SMPPHqAGn8KYaBUZ5auKWEvbo2p525noybGkdA+BsRulTVsk9cFLXpJ P5OFVWTdIZR1hcji9KnXISQcBmz4KNA4Kq+LdXurBqHg3f3s9dksqzxEjdCI4wYwUJf2 EoBZVrk9m9nxB+7p+dx7mK0iTQKq4Y+f9hUygJiewEVtQEyWj90AgC1SR8E1ps02RV3n JT9Q== 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=vrYK9vJKG+GQbJyhuOmY4VOCSVy9Kwsa9yzyjUzeoA/k0hBMROen7EOHEMJL4JrWzM HKfL6UslNcPf5N0XzP/TlSph4dbCWtB+n3RkQAFowrTHUM+X0dOw+JYUy0HViNVVmaaB 4rVkou9moESH7jopO1tArhP1mu3Z5ftjCM23gAPEQaYN8BrrtgfHICMk904uEYE7Zb7Z p3tPGWaTGMRjBM1F8xUYPCTQNrJPoHsUgWCaw7/ZLRtmtkNhgYn2Q9HiAnMxFZkCE1Pw Sr8P5sc/D3VwbrxCZgpSAbeJ73NOLJTEux9PXW2LIPIcmTSlCyAndpnOp82oK2/hTFGB n7Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=U1qzWtaP; dkim=neutral (no key) header.i=@linutronix.de; 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 h64-20020a638343000000b00517ce377586si3366432pge.270.2023.05.10.00.48.36; Wed, 10 May 2023 00:48: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=U1qzWtaP; dkim=neutral (no key) header.i=@linutronix.de; 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 S235403AbjEJHaO (ORCPT + 99 others); Wed, 10 May 2023 03:30:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235899AbjEJH3Z (ORCPT ); Wed, 10 May 2023 03:29:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B90B17DA2 for ; Wed, 10 May 2023 00:28:56 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1683703723; 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=U1qzWtaP5VaA4c5Gn3/d+vMWtAIE2AX1garui7ElQZ8mI6xApNDhg0v611ZwnMYNh5D3v4 UsjTyLh56EkntJ4CEAWU+m7KTtya2+VCNnJqTaCKngMl6qhMAlAOPh3PGPDUVexKqk+iTz VTXAZfd1VywJiTR71X1+lt4sJPt7WirNHVd0EWXTEWy8atbZrnhmCgAe+eo18wPFucGIuR Pq7ZfboLAUFbaBR4phGessx06Ncy3vCPzdGkpEYS6f4PmQYY6urHn6QT5vb97JNhdaz9K8 5ERezJV00k7iKP1YaHxxBcTqqze0tHJebbyLGTfcIS/o9mBQ9YcOM3cWDy7alA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1683703723; 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=c12yqfObzDwTr7vt8H+HUPGAbrRoXeMKVlBS0TvGTsnKcHGnTX5RdvI70jZJ+VTLgIfvAz dFml2vEGj0HBAJCA== 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 v6 13/21] timer: Retrieve next expiry of pinned/non-pinned timers separately Date: Wed, 10 May 2023 09:28:09 +0200 Message-Id: <20230510072817.116056-14-anna-maria@linutronix.de> In-Reply-To: <20230510072817.116056-1-anna-maria@linutronix.de> References: <20230510072817.116056-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,URIBL_BLOCKED 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?1765492580321057668?= X-GMAIL-MSGID: =?utf-8?q?1765492580321057668?= 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); } /**