From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DAC46C433B4 for ; Thu, 13 May 2021 06:51:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A6F8D6142C for ; Thu, 13 May 2021 06:51:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231374AbhEMGwb (ORCPT ); Thu, 13 May 2021 02:52:31 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:2470 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230063AbhEMGwa (ORCPT ); Thu, 13 May 2021 02:52:30 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4Fgj1153LnzBtwk; Thu, 13 May 2021 14:48:37 +0800 (CST) Received: from [10.174.179.5] (10.174.179.5) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Thu, 13 May 2021 14:51:16 +0800 Subject: Re: [RFC PATCH] timer: Fix bucket_expiry calculation To: Thomas Gleixner , , CC: References: <1620821729-40694-1-git-send-email-wangxiongfeng2@huawei.com> <87y2ckdnea.ffs@nanos.tec.linutronix.de> From: Xiongfeng Wang Message-ID: <60582932-1332-ed33-d7e0-3eae78793449@huawei.com> Date: Thu, 13 May 2021 14:51:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <87y2ckdnea.ffs@nanos.tec.linutronix.de> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.5] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thomas, On 2021/5/12 22:42, Thomas Gleixner wrote: > Xiongfeng, > > On Wed, May 12 2021 at 20:15, Xiongfeng Wang wrote: >> When I use schedule_timeout(5) to put a process into sleep on my machine >> with HZ = 100. It always sleep about 60ms. I enable the timer trace and >> find out, when the timer_list expires, 'now' is always equal to >> 'expires + 1'. I print 'base->next_expiry' in '__run_timers' and find out >> 'next_expiry' is always equal to 'expires + 1'; >> >> It is because we use the following equation to calculate bucket_expiry. >> >> bucket_expiry = ((expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl)) << LVL_SHIFT(lvl) >> >> 'bucket_expiry' is equal to 'expires + 1' when lvl = 0. So modify the >> equation as follows to fix the issue. >> >> bucket_expiry = ((expires + LVL_GRAN(lvl) - 1) >> LVL_SHIFT(lvl)) << LVL_SHIFT(lvl) > > That's wrong because you move the expiry of each timer one jiffie ahead, > which violates the guarantee that a timer sleeps at least for one jiffie > for real and not measured in jiffies. > > jiffies = 0 > schedule_timeout(1) > > local_irq_disable() > -> timer interrupt is raised in HW > timer->expires = jiffies + 1 <- 1 > add_timer(timer) > local_irq_enable() > timer interrupt > jiffies++; > softirq() > expire(timer); -> timer is expired immediately > > So the off by one has a reason and is required to prevent too short > timeouts. There is nothing you can do about that because that's a > property of low granularity tick based timer wheels. > > That's even documented in the comment above the code you modified: > > /* > * The timer wheel has to guarantee that a timer does not fire > * early. Early expiry can happen due to: > * - Timer is armed at the edge of a tick > * - Truncation of the expiry time in the outer wheel levels > * > * Round up with level granularity to prevent this. > */ Thanks for your explanation. I got it ! Thanks, Xiongfeng > > Thanks, > > tglx > . >