Skip to content

Commit 1f32cab

Browse files
anna-marialxKAGA-KOKO
authored andcommitted
timers: Use only bucket expiry for base->next_expiry value
The bucket expiry time is the effective expriy time of timers and is greater than or equal to the requested timer expiry time. This is due to the guarantee that timers never expire early and the reduced expiry granularity in the secondary wheel levels. When a timer is enqueued, trigger_dyntick_cpu() checks whether the timer is the new first timer. This check compares next_expiry with the requested timer expiry value and not with the effective expiry value of the bucket into which the timer was queued. Storing the requested timer expiry value in base->next_expiry can lead to base->clk going backwards if the requested timer expiry value is smaller than base->clk. Commit 30c66fc ("timer: Prevent base->clk from moving backward") worked around this by preventing the store when timer->expiry is before base->clk, but did not fix the underlying problem. Use the expiry value of the bucket into which the timer is queued to do the new first timer check. This fixes the base->clk going backward problem. The workaround of commit 30c66fc ("timer: Prevent base->clk from moving backward") in trigger_dyntick_cpu() is not longer necessary as the timers bucket expiry is guaranteed to be greater than or equal base->clk. Signed-off-by: Anna-Maria Behnsen <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 3d2e83a commit 1f32cab

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

kernel/time/timer.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -487,35 +487,39 @@ static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
487487
* Helper function to calculate the array index for a given expiry
488488
* time.
489489
*/
490-
static inline unsigned calc_index(unsigned long expires, unsigned lvl)
490+
static inline unsigned calc_index(unsigned long expires, unsigned lvl,
491+
unsigned long *bucket_expiry)
491492
{
492493
expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
494+
*bucket_expiry = expires << LVL_SHIFT(lvl);
493495
return LVL_OFFS(lvl) + (expires & LVL_MASK);
494496
}
495497

496-
static int calc_wheel_index(unsigned long expires, unsigned long clk)
498+
static int calc_wheel_index(unsigned long expires, unsigned long clk,
499+
unsigned long *bucket_expiry)
497500
{
498501
unsigned long delta = expires - clk;
499502
unsigned int idx;
500503

501504
if (delta < LVL_START(1)) {
502-
idx = calc_index(expires, 0);
505+
idx = calc_index(expires, 0, bucket_expiry);
503506
} else if (delta < LVL_START(2)) {
504-
idx = calc_index(expires, 1);
507+
idx = calc_index(expires, 1, bucket_expiry);
505508
} else if (delta < LVL_START(3)) {
506-
idx = calc_index(expires, 2);
509+
idx = calc_index(expires, 2, bucket_expiry);
507510
} else if (delta < LVL_START(4)) {
508-
idx = calc_index(expires, 3);
511+
idx = calc_index(expires, 3, bucket_expiry);
509512
} else if (delta < LVL_START(5)) {
510-
idx = calc_index(expires, 4);
513+
idx = calc_index(expires, 4, bucket_expiry);
511514
} else if (delta < LVL_START(6)) {
512-
idx = calc_index(expires, 5);
515+
idx = calc_index(expires, 5, bucket_expiry);
513516
} else if (delta < LVL_START(7)) {
514-
idx = calc_index(expires, 6);
517+
idx = calc_index(expires, 6, bucket_expiry);
515518
} else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
516-
idx = calc_index(expires, 7);
519+
idx = calc_index(expires, 7, bucket_expiry);
517520
} else if ((long) delta < 0) {
518521
idx = clk & LVL_MASK;
522+
*bucket_expiry = clk;
519523
} else {
520524
/*
521525
* Force expire obscene large timeouts to expire at the
@@ -524,7 +528,7 @@ static int calc_wheel_index(unsigned long expires, unsigned long clk)
524528
if (delta >= WHEEL_TIMEOUT_CUTOFF)
525529
expires = clk + WHEEL_TIMEOUT_MAX;
526530

527-
idx = calc_index(expires, LVL_DEPTH - 1);
531+
idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry);
528532
}
529533
return idx;
530534
}
@@ -544,16 +548,18 @@ static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
544548
}
545549

546550
static void
547-
__internal_add_timer(struct timer_base *base, struct timer_list *timer)
551+
__internal_add_timer(struct timer_base *base, struct timer_list *timer,
552+
unsigned long *bucket_expiry)
548553
{
549554
unsigned int idx;
550555

551-
idx = calc_wheel_index(timer->expires, base->clk);
556+
idx = calc_wheel_index(timer->expires, base->clk, bucket_expiry);
552557
enqueue_timer(base, timer, idx);
553558
}
554559

555560
static void
556-
trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
561+
trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer,
562+
unsigned long bucket_expiry)
557563
{
558564
if (!is_timers_nohz_active())
559565
return;
@@ -576,31 +582,29 @@ trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
576582
if (!base->is_idle)
577583
return;
578584

579-
/* Check whether this is the new first expiring timer: */
580-
if (time_after_eq(timer->expires, base->next_expiry))
585+
/*
586+
* Check whether this is the new first expiring timer. The
587+
* effective expiry time of the timer is required here
588+
* (bucket_expiry) instead of timer->expires.
589+
*/
590+
if (time_after_eq(bucket_expiry, base->next_expiry))
581591
return;
582592

583593
/*
584594
* Set the next expiry time and kick the CPU so it can reevaluate the
585595
* wheel:
586596
*/
587-
if (time_before(timer->expires, base->clk)) {
588-
/*
589-
* Prevent from forward_timer_base() moving the base->clk
590-
* backward
591-
*/
592-
base->next_expiry = base->clk;
593-
} else {
594-
base->next_expiry = timer->expires;
595-
}
597+
base->next_expiry = bucket_expiry;
596598
wake_up_nohz_cpu(base->cpu);
597599
}
598600

599601
static void
600602
internal_add_timer(struct timer_base *base, struct timer_list *timer)
601603
{
602-
__internal_add_timer(base, timer);
603-
trigger_dyntick_cpu(base, timer);
604+
unsigned long bucket_expiry;
605+
606+
__internal_add_timer(base, timer, &bucket_expiry);
607+
trigger_dyntick_cpu(base, timer, bucket_expiry);
604608
}
605609

606610
#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
@@ -959,9 +963,9 @@ static struct timer_base *lock_timer_base(struct timer_list *timer,
959963
static inline int
960964
__mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
961965
{
966+
unsigned long clk = 0, flags, bucket_expiry;
962967
struct timer_base *base, *new_base;
963968
unsigned int idx = UINT_MAX;
964-
unsigned long clk = 0, flags;
965969
int ret = 0;
966970

967971
BUG_ON(!timer->function);
@@ -1000,7 +1004,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
10001004
}
10011005

10021006
clk = base->clk;
1003-
idx = calc_wheel_index(expires, clk);
1007+
idx = calc_wheel_index(expires, clk, &bucket_expiry);
10041008

10051009
/*
10061010
* Retrieve and compare the array index of the pending
@@ -1059,7 +1063,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
10591063
*/
10601064
if (idx != UINT_MAX && clk == base->clk) {
10611065
enqueue_timer(base, timer, idx);
1062-
trigger_dyntick_cpu(base, timer);
1066+
trigger_dyntick_cpu(base, timer, bucket_expiry);
10631067
} else {
10641068
internal_add_timer(base, timer);
10651069
}

0 commit comments

Comments
 (0)