Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2078210ybh; Fri, 24 Jul 2020 04:00:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbl/+uDVPZJTssxnj2dTSuwc6vV1W1UAyPNJN1kMpbvZi1lNSbdsrzcV2Ir3aq5H/zbcIh X-Received: by 2002:a17:906:5346:: with SMTP id j6mr480470ejo.311.1595588425493; Fri, 24 Jul 2020 04:00:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595588425; cv=none; d=google.com; s=arc-20160816; b=G0lWb+MF9mFO15ohgJ/pF9Rl4cQKkpNiLkjcXxgIaoV+igz8UBbiNnG8vlezDb1e+q pfL5Am70UXzWrsHcGiC0uAn0aDlE1rcfWV5SsAXJ25mR7DQIl3MWOL1s7FEHOszGnK2p c2JQiBo3Il6R2ENeW+38SlQjOgEn1L3Khb7DOxDrx413aSbh+oaIkI/RpKacLXOuJSVZ 1UbdmDjJ2Spe/L0ahKY8PWZ3pMpuf3zuFBpdsdH3wzvBN6vg56UavHLIJ47EJGnNNhu0 kLMOOBPrixTMzId0iNDjXnW0KkR7lTCTHFVq2P1ziIpU/OZvX2ptfBezmjbRzR12Qe3q d96g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:dkim-signature :dkim-signature:date; bh=+o1o9WpW2esxHjKTdhQnvedFWXTWqLYXBNVNq7q3OiE=; b=TD92bhh3NmJ851cQbCIBtoets4LaucSHOVKf1coafvo0Rut+L8OFwcvpG2bQ1bSAE9 lcZci/E35KAM9DRP484PlVnijGUnCLHs0tFTViiWsj8+SVM06Hg+cN38nx4Mnc+1yNHH 8Fos9H4gp481NBdoTAmcXm2yPYsOiJHFfLm6hzpiQZiyjBs/voqj6l5bRszdVAYEs4ik tJNvFiKg7jIP0iDMOSp8kTgo13TY39Yo/jHoKCJbD7h5Yb0d8RzAVXe8MTxOsXtFPnnS 7S7V2/3EY4Ztnef3WnZfPFP4HLLo7tcdklSc8rM89lB2VDxsk8imuP0YS6ulvpBgQ2yp CGNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linutronix.de header.s=2020 header.b=EFXRzzYd; dkim=neutral (no key) header.i=@vger.kernel.org; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n15si340597ejk.642.2020.07.24.04.00.03; Fri, 24 Jul 2020 04:00:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@linutronix.de header.s=2020 header.b=EFXRzzYd; dkim=neutral (no key) header.i=@vger.kernel.org; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbgGXK7n (ORCPT + 99 others); Fri, 24 Jul 2020 06:59:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726114AbgGXK7n (ORCPT ); Fri, 24 Jul 2020 06:59:43 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05B28C0619D3; Fri, 24 Jul 2020 03:59:42 -0700 (PDT) Date: Fri, 24 Jul 2020 10:59:38 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1595588380; 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=+o1o9WpW2esxHjKTdhQnvedFWXTWqLYXBNVNq7q3OiE=; b=EFXRzzYdshIvNvumE97pRVt9mbCcZ0CU3CByu7zxDamfFnccyCo6ao4lIAr2755BwfpfGr kst1XE9iujS1j8x/1IXlSxzPWA4ou7aKc+/ySuhhDEBWjrar3OTSFOONhzrzaHiWciEauE 98+a/5IqF7fWlr7qG6iqwAwKdWLzxI9sWjO8CL1Ls7CNnR1nGRM2uy/nXUVzOxlbIdxgdX JDJFxKWi2/ckPT2lYaiBt6mrnhTXVV7FFi1EitIQ/cM6vgTupbodCvJCg0Foi1VbT/nzPh UbLvVU0IkqILHIH6SFOmWhEgy4kh2fFexWi6XiZ7tAUliRJjs+5KT73LG9cAqQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1595588380; 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=+o1o9WpW2esxHjKTdhQnvedFWXTWqLYXBNVNq7q3OiE=; b=i1z5PlgRIs7GoANShAW12xHuIndfjF7sgbzrbb5FS/+5iecjk48yPmqYdyMw5/vHGe0qYY sd/38hlNR7giV1Bw== From: "tip-bot2 for Frederic Weisbecker" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] timers: Recalculate next timer interrupt only when necessary Cc: Frederic Weisbecker , Thomas Gleixner , x86 , LKML In-Reply-To: <20200723151641.12236-1-frederic@kernel.org> References: <20200723151641.12236-1-frederic@kernel.org> MIME-Version: 1.0 Message-ID: <159558837898.4006.13597592790372222044.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the timers/core branch of tip: Commit-ID: 31cd0e119d50cf27ebe214d1a8f7ca36692f13a5 Gitweb: https://git.kernel.org/tip/31cd0e119d50cf27ebe214d1a8f7ca36692f13a5 Author: Frederic Weisbecker AuthorDate: Thu, 23 Jul 2020 17:16:41 +02:00 Committer: Thomas Gleixner CommitterDate: Fri, 24 Jul 2020 12:49:40 +02:00 timers: Recalculate next timer interrupt only when necessary The nohz tick code recalculates the timer wheel's next expiry on each idle loop iteration. On the other hand, the base next expiry is now always cached and updated upon timer enqueue and execution. Only timer dequeue may leave base->next_expiry out of date (but then its stale value won't ever go past the actual next expiry to be recalculated). Since recalculating the next_expiry isn't a free operation, especially when the last wheel level is reached to find out that no timer has been enqueued at all, reuse the next expiry cache when it is known to be reliable, which it is most of the time. Signed-off-by: Frederic Weisbecker Signed-off-by: Thomas Gleixner Link: https://lkml.kernel.org/r/20200723151641.12236-1-frederic@kernel.org --- kernel/time/timer.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 77e21e9..96d802e 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -204,6 +204,7 @@ struct timer_base { unsigned long clk; unsigned long next_expiry; unsigned int cpu; + bool next_expiry_recalc; bool is_idle; DECLARE_BITMAP(pending_map, WHEEL_SIZE); struct hlist_head vectors[WHEEL_SIZE]; @@ -593,6 +594,7 @@ static void enqueue_timer(struct timer_base *base, struct timer_list *timer, * can reevaluate the wheel: */ base->next_expiry = bucket_expiry; + base->next_expiry_recalc = false; trigger_dyntick_cpu(base, timer); } } @@ -836,8 +838,10 @@ static int detach_if_pending(struct timer_list *timer, struct timer_base *base, if (!timer_pending(timer)) return 0; - if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) + if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) { __clear_bit(idx, base->pending_map); + base->next_expiry_recalc = true; + } detach_timer(timer, clear_pending); return 1; @@ -1571,6 +1575,9 @@ static unsigned long __next_timer_interrupt(struct timer_base *base) clk >>= LVL_CLK_SHIFT; clk += adj; } + + base->next_expiry_recalc = false; + return next; } @@ -1631,9 +1638,11 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) return expires; raw_spin_lock(&base->lock); - nextevt = __next_timer_interrupt(base); + if (base->next_expiry_recalc) + base->next_expiry = __next_timer_interrupt(base); + nextevt = base->next_expiry; is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA); - base->next_expiry = nextevt; + /* * We have a fresh next event. Check whether we can forward the * base. We can only do that when @basej is past base->clk @@ -1725,6 +1734,12 @@ static inline void __run_timers(struct timer_base *base) while (time_after_eq(jiffies, base->clk) && time_after_eq(jiffies, base->next_expiry)) { levels = collect_expired_timers(base, heads); + /* + * The only possible reason for not finding any expired + * timer at this clk is that all matching timers have been + * dequeued. + */ + WARN_ON_ONCE(!levels && !base->next_expiry_recalc); base->clk++; base->next_expiry = __next_timer_interrupt(base);