Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp155541pxj; Thu, 13 May 2021 01:33:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwpG8X12C1+D/Lf0Y9FflLPiocvw8F3vI4rUzIq1F0V9NvsA1COwlJ0UbNGYiRwVApIonFw X-Received: by 2002:a17:907:35cd:: with SMTP id ap13mr3562783ejc.527.1620894817164; Thu, 13 May 2021 01:33:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620894817; cv=none; d=google.com; s=arc-20160816; b=T2b+RcYYBSC3Kwipu1jcskQL2CszvzW50+64uQ//tAfN6Bdzfg4j/ycNCabbPsFED5 I+JX7LzvZtN5XKx3skatDZ01hadIe1fLsGJxPiTda73Ua+t6PwEthUCF7rKKbUyHZdWb TS2ru01n2W3egXiVD2xmuha5aolRbnAghGnOa0b1tV226xZicTtRfveHlMkh0NYAYY+l OIIssA2ZoFiCp4DdZe75S4ODI35SJgSMbWkyam22aeecJEw7BAN64pQkTxZZK2jLSvEV t8on9ts4nsLAl5ZgIV0DZlYNRcwbicxvOeQ4G3UX0TgjP4YF0/XYv+y3NkSqPA9mIVEc tJqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=5aaJ2ACXPwfmMryZXRUE2V2qB1XHkrUoS6zqB0RiRIw=; b=xafaLuS39RYIZGSl6P+ipEbcLXEL+UhDg/6uZYGv0vwfkacE58BKKtw/0xOXQIBfOI kTCiLjT0PAG4Pd3DurBGuS6rWP6tJFF/suVRYEycXwiDuhMvckD37fMK6eaQyEIbL18a rz/q4gsSChNA0DoWc0z80LY7vu0aoZHVA3uEtPrV9qPYc1tjAdqaiy41w0cEV107IZDH SA32oRymoNHw8zmNwD7orYzb9ny6g7Zg7NoyCeJrjg2reoRIz/pHNGv8v0oZZpV6t+cw sdzzM4XpZt0n6ifju8SFnk+6PEkMHiGk3pABh629EFhaEVE5uX+eFwiJfuE4NAcBS8jh shHw== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q25si2257615edw.105.2021.05.13.01.33.11; Thu, 13 May 2021 01:33:37 -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; 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=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231273AbhEMGwa (ORCPT + 99 others); Thu, 13 May 2021 02:52:30 -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 > . >