linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] timers: fix LVL_START macro
@ 2022-11-15  2:56 Yun Zhou
  2022-11-15 12:02 ` Frederic Weisbecker
  0 siblings, 1 reply; 5+ messages in thread
From: Yun Zhou @ 2022-11-15  2:56 UTC (permalink / raw)
  To: jstultz, tglx, sboyd; +Cc: linux-kernel

The number of buckets per level should be LVL_SIZE, not LVL_SIZE-1.

Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
---
 kernel/time/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 717fcb9fb14a..1116b208093e 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -161,7 +161,7 @@ EXPORT_SYMBOL(jiffies_64);
  * time. We start from the last possible delta of the previous level
  * so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
  */
-#define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
+#define LVL_START(n)	(LVL_SIZE << (((n) - 1) * LVL_CLK_SHIFT))
 
 /* Size of each clock level */
 #define LVL_BITS	6
-- 
2.35.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] timers: fix LVL_START macro
  2022-11-15  2:56 [PATCH] timers: fix LVL_START macro Yun Zhou
@ 2022-11-15 12:02 ` Frederic Weisbecker
       [not found]   ` <SN6PR11MB300812CA336B497C40E93CA19F049@SN6PR11MB3008.namprd11.prod.outlook.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Frederic Weisbecker @ 2022-11-15 12:02 UTC (permalink / raw)
  To: Yun Zhou; +Cc: jstultz, tglx, sboyd, linux-kernel

Hi Yun Zhou,

On Tue, Nov 15, 2022 at 10:56:14AM +0800, Yun Zhou wrote:
> The number of buckets per level should be LVL_SIZE, not LVL_SIZE-1.
> 
> Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
> ---
>  kernel/time/timer.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/time/timer.c b/kernel/time/timer.c
> index 717fcb9fb14a..1116b208093e 100644
> --- a/kernel/time/timer.c
> +++ b/kernel/time/timer.c
> @@ -161,7 +161,7 @@ EXPORT_SYMBOL(jiffies_64);
>   * time. We start from the last possible delta of the previous level
>   * so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
>   */
> -#define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
> +#define LVL_START(n)	(LVL_SIZE << (((n) - 1) * LVL_CLK_SHIFT))

See the comment above:

   "We start from the last possible delta of the previous level
    so that we can later add an extra LVL_GRAN(n) to n (see calc_index())."

Thanks.

>  
>  /* Size of each clock level */
>  #define LVL_BITS	6
> -- 
> 2.35.2
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] timers: fix LVL_START macro
       [not found]   ` <SN6PR11MB300812CA336B497C40E93CA19F049@SN6PR11MB3008.namprd11.prod.outlook.com>
@ 2022-11-15 22:40     ` Frederic Weisbecker
  2022-11-16 23:48       ` Thomas Gleixner
  0 siblings, 1 reply; 5+ messages in thread
From: Frederic Weisbecker @ 2022-11-15 22:40 UTC (permalink / raw)
  To: Zhou, Yun; +Cc: jstultz, tglx, sboyd, linux-kernel

On Tue, Nov 15, 2022 at 01:15:11PM +0000, Zhou, Yun wrote:
> Hi Frederic,
> 
> The issue now is that a timer may be thrown into the upper level bucket. For example, expires 4090 and 1000 HZ, it should be in level 2, but now it will be placed in the level 3. Is this expected?
> 
>  * HZ 1000 steps
>  * Level Offset  Granularity            Range
>  *  0      0         1 ms                0 ms -         63 ms
>  *  1     64         8 ms               64 ms -        511 ms
>  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
>  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
>  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)

The rule is that a timer is not allowed to expire too early. But it can expire
a bit late. Hence why it is always rounded up. So in the case of 4090, we have
the choice between:

1) expiring at bucket 2 after 4096 - 64 = 4032 ms
2) expiring at bucket 3 after 4096 ms

The 1) rounds down and expires too early. The 2) rounds up and expires a bit
late. So the second solution is preferred.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] timers: fix LVL_START macro
  2022-11-15 22:40     ` Frederic Weisbecker
@ 2022-11-16 23:48       ` Thomas Gleixner
  2022-11-17 12:14         ` Frederic Weisbecker
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2022-11-16 23:48 UTC (permalink / raw)
  To: Frederic Weisbecker, Zhou, Yun; +Cc: jstultz, sboyd, linux-kernel

On Tue, Nov 15 2022 at 23:40, Frederic Weisbecker wrote:
> On Tue, Nov 15, 2022 at 01:15:11PM +0000, Zhou, Yun wrote:
>> Hi Frederic,
>> 
>> The issue now is that a timer may be thrown into the upper level bucket. For example, expires 4090 and 1000 HZ, it should be in level 2, but now it will be placed in the level 3. Is this expected?
>> 
>>  * HZ 1000 steps
>>  * Level Offset  Granularity            Range
>>  *  0      0         1 ms                0 ms -         63 ms
>>  *  1     64         8 ms               64 ms -        511 ms
>>  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
>>  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
>>  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
>
> The rule is that a timer is not allowed to expire too early. But it can expire
> a bit late. Hence why it is always rounded up. So in the case of 4090, we have
> the choice between:
>
> 1) expiring at bucket 2 after 4096 - 64 = 4032 ms
> 2) expiring at bucket 3 after 4096 ms
>
> The 1) rounds down and expires too early. The 2) rounds up and expires a bit
> late. So the second solution is preferred.

It's not only preferred, it's required simply because the timer wheel
has only one guarantee: Not to expire early.

Timer wheel based timers are fundamentaly not precise unless the timeout
is short and hits the first level.

But even hrtimers which are designed to be precise have only one real
guarantee: Not to expire early.

hrtimers do not have the side effect of batching on long timeouts like
timer wheel based timer have, but that's it.

Timers in the kernel come with a choice:

  -  Imprecise and inexpensive to arm and cancel (timer_list)
  -  Precise and expensive to arm and cancel (hrtimer)

You can't have both. That's well documented.

Thanks,

        tglx

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] timers: fix LVL_START macro
  2022-11-16 23:48       ` Thomas Gleixner
@ 2022-11-17 12:14         ` Frederic Weisbecker
  0 siblings, 0 replies; 5+ messages in thread
From: Frederic Weisbecker @ 2022-11-17 12:14 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Zhou, Yun, jstultz, sboyd, linux-kernel

On Thu, Nov 17, 2022 at 12:48:05AM +0100, Thomas Gleixner wrote:
> On Tue, Nov 15 2022 at 23:40, Frederic Weisbecker wrote:
> > On Tue, Nov 15, 2022 at 01:15:11PM +0000, Zhou, Yun wrote:
> >> Hi Frederic,
> >> 
> >> The issue now is that a timer may be thrown into the upper level bucket. For example, expires 4090 and 1000 HZ, it should be in level 2, but now it will be placed in the level 3. Is this expected?
> >> 
> >>  * HZ 1000 steps
> >>  * Level Offset  Granularity            Range
> >>  *  0      0         1 ms                0 ms -         63 ms
> >>  *  1     64         8 ms               64 ms -        511 ms
> >>  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
> >>  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
> >>  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
> >
> > The rule is that a timer is not allowed to expire too early. But it can expire
> > a bit late. Hence why it is always rounded up. So in the case of 4090, we have
> > the choice between:
> >
> > 1) expiring at bucket 2 after 4096 - 64 = 4032 ms
> > 2) expiring at bucket 3 after 4096 ms
> >
> > The 1) rounds down and expires too early. The 2) rounds up and expires a bit
> > late. So the second solution is preferred.
> 
> It's not only preferred, it's required simply because the timer wheel
> has only one guarantee: Not to expire early.
> 
> Timer wheel based timers are fundamentaly not precise unless the timeout
> is short and hits the first level.
> 
> But even hrtimers which are designed to be precise have only one real
> guarantee: Not to expire early.
> 
> hrtimers do not have the side effect of batching on long timeouts like
> timer wheel based timer have, but that's it.
> 
> Timers in the kernel come with a choice:
> 
>   -  Imprecise and inexpensive to arm and cancel (timer_list)
>   -  Precise and expensive to arm and cancel (hrtimer)
> 
> You can't have both. That's well documented.

Actually I'm pretty sure we can manage imprecise and expensive to arm and
cancel. It's a matter of willpower!

Anyway, thanks for confirming what I thought about timers guarantees.

Thanks.

> 
> Thanks,
> 
>         tglx

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-17 12:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-15  2:56 [PATCH] timers: fix LVL_START macro Yun Zhou
2022-11-15 12:02 ` Frederic Weisbecker
     [not found]   ` <SN6PR11MB300812CA336B497C40E93CA19F049@SN6PR11MB3008.namprd11.prod.outlook.com>
2022-11-15 22:40     ` Frederic Weisbecker
2022-11-16 23:48       ` Thomas Gleixner
2022-11-17 12:14         ` Frederic Weisbecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).