Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1099405pxv; Fri, 9 Jul 2021 17:53:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2ydUa4OSt6MSqS8UCkVHYbfjg+GV/i2cTVLWMlBA3h/a+lpee+aKdcT7fna7CNtIsovcw X-Received: by 2002:a02:9109:: with SMTP id a9mr34494250jag.93.1625878410864; Fri, 09 Jul 2021 17:53:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625878410; cv=none; d=google.com; s=arc-20160816; b=AZTMLU/zaHvVQXwWlCWAEEyM6RNmZkJNwawbFubif1UzXXuZxTzemieVgOgsyYiVMT pbn9g0os+hB0d9d31IORUA4XuBVdavGI3MGxUaoE/lbdQGsfmb60AuhKUH3hz0PXwwJZ D+6Uq0iRQY0Oed2zcTgKBhODjS6GVzscgDnKypG4V4esMK0OGyX2A0eUijXZP1qUY1Dh Vfx94Jm4k8OzuOWvv7D7XxN59TXI/6N2L3chxYMX/90lVBkuLVvAjAUiYOmJ7S5Q28GL 0zDUn1xq2qD12wCxiGp3y/m1lPzNjeecG8WKSrsRzBAVRT1cHNBW0HlkdujQxrFp+6YN 7//Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=R5ln95yhruCY+3g5ZaC7RnpTk7NrNN6rmvQ4QTzejmU=; b=PoPsCMt/PXSupfmd4XMvp8hf4hG2V2i2B0CpdJEAtvr/l0vz2RuWG8FvoqEPFD73Hl X1BDPb9GX9enUPihHFb1bosxvCb0OONSkvK0xv3TprZVdUs+QhKbhcfsrA5p+6S8a//4 c8CZZWS7pWmkVrK7X5b26DOtaF3oUAv/xaRdarFg9fOZDyWECcAo+jixwOcjY5gpUv6p Bvwc4/c79Z6EUX8JAcLfH7BSeqbCS2saQo8mMy3+i2AWwvZbeHQmFO/a5/BJwmhE72DK wihc4SfRFWO9PiXhCZjcQ0zguw2ALt33N9+EjMb0uJmSsWAPG+BPWDjSiXIM1TkbrDH8 SvMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nRLVEEyb; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o17si7478334ill.146.2021.07.09.17.53.18; Fri, 09 Jul 2021 17:53:30 -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=pass header.i=@kernel.org header.s=k20201202 header.b=nRLVEEyb; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230476AbhGJAza (ORCPT + 99 others); Fri, 9 Jul 2021 20:55:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:58424 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229931AbhGJAza (ORCPT ); Fri, 9 Jul 2021 20:55:30 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6CEAC613BF; Sat, 10 Jul 2021 00:52:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625878365; bh=NJp6PfKPBhF9FzKqtEXxMZ26tOfT4MTzbfvemX8jly0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nRLVEEybkxBU2V5Y+vMe+lttN40BM1s5yLR5U4enJOtiFd7/g1t69j7uFNqD2Xqxy ILXajiAzVGymPWkLQxSk1JIzLidti5jolVptPpkaBfhc+4AGnkm0lBXrnmt7fgVmxm GRe8RicXlXsUvAURooBhT3CobTEuvgWWpomgDp1Ef+VhkUUf9+SApDYErB5LQeXd01 bmlIOxugamlWx/PQHiLLgzRQ0oi04XBZqMPrPjZsdQSFla6YlFqwMBrl/zLb5DO2Zr /PpGNfXlv8Xk3V7rjf2TbTIXe8Si/5BLKZiZ/uP18EbUkXR+bZSjTaLXWw8zCb/06v OAXnfZ7ceRGBw== Date: Sat, 10 Jul 2021 02:52:43 +0200 From: Frederic Weisbecker To: Nicolas Saenz Julienne Cc: He Zhe , anna-maria@linutronix.de, linux-kernel@vger.kernel.org, tglx@linutronix.de Subject: Re: [PATCH] timers: Fix get_next_timer_interrupt() with no timers pending Message-ID: <20210710005243.GA23956@lothringen> References: <20200723151641.12236-1-frederic@kernel.org> <20210708153620.GA6716@lothringen> <20210709084303.GA17239@lothringen> <11e85cd8-40ac-09fe-e1fe-0eafa351072c@windriver.com> <4409fa71931446d9cabd849431ee0098c9b31292.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4409fa71931446d9cabd849431ee0098c9b31292.camel@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 09, 2021 at 04:13:25PM +0200, Nicolas Saenz Julienne wrote: > 31cd0e119d50 ("timers: Recalculate next timer interrupt only when > necessary") subtly altered get_next_timer_interrupt()'s behaviour. The > function no longer consistently returns KTIME_MAX with no timers > pending. > > In order to decide if there are any timers pending we check whether the > next expiry will happen NEXT_TIMER_MAX_DELTA jiffies from now. > Unfortunately, the next expiry time and the timer base clock are no > longer updated in unison. The former changes upon certain timer > operations (enqueue, expire, detach), whereas the latter keeps track of > jiffies as they move forward. Ultimately breaking the logic above. > > A simplified example: > > - Upon entering get_next_timer_interrupt() with: > > jiffies = 1 > base->clk = 0; > base->next_expiry = NEXT_TIMER_MAX_DELTA; > > 'base->next_expiry == base->clk + NEXT_TIMER_MAX_DELTA', the function > returns KTIME_MAX. > > - 'base->clk' is updated to the jiffies value. > > - The next time we enter get_next_timer_interrupt(), taking into account > no timer operations happened: > > base->clk = 1; > base->next_expiry = NEXT_TIMER_MAX_DELTA; > > 'base->next_expiry != base->clk + NEXT_TIMER_MAX_DELTA', the function > returns a valid expire time, which is incorrect. > > This ultimately might unnecessarily rearm sched's timer on nohz_full > setups, and add latency to the system[1]. > > So, introduce 'base->timers_pending'[2], update it every time > 'base->next_expiry' changes, and use it in get_next_timer_interrupt(). > > [1] See tick_nohz_stop_tick(). > [2] A quick pahole check on x86_64 and arm64 shows it doesn't make > 'struct timer_base' any bigger. > > Fixes: 31cd0e119d50 ("timers: Recalculate next timer interrupt only when necessary") > Signed-off-by: Nicolas Saenz Julienne Very good catch. And the fix looks good: Acked-by: Frederic Weisbecker I guess later we can turn this .timers_pending into .timers_count and that would spare us the costly call to __next_timer_interrupt() up to the last level after the last timer is dequeued. Anyway, thanks a lot!