mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiongfeng Wang <wangxiongfeng2@huawei.com>
To: Thomas Gleixner <tglx@linutronix.de>, <john.stultz@linaro.org>,
	<sboyd@kernel.org>
Cc: <linux-kernel@vger.kernel.org>
Subject: Re: [RFC PATCH] timer: Fix bucket_expiry calculation
Date: Thu, 13 May 2021 14:51:15 +0800	[thread overview]
Message-ID: <60582932-1332-ed33-d7e0-3eae78793449@huawei.com> (raw)
In-Reply-To: <87y2ckdnea.ffs@nanos.tec.linutronix.de>

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
> .
> 

      reply	other threads:[~2021-05-13  6:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 12:15 Xiongfeng Wang
2021-05-12 14:42 ` Thomas Gleixner
2021-05-13  6:51   ` Xiongfeng Wang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=60582932-1332-ed33-d7e0-3eae78793449@huawei.com \
    --to=wangxiongfeng2@huawei.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®