Skip to content

Commit 4468897

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
timers: Add comments about calc_index() ceiling work
calc_index() adds 1 unit of the level granularity to the expiry passed in parameter to ensure that the timer doesn't expire too early. Add a comment to explain that and the resulting layout in the wheel. Suggested-by: Thomas Gleixner <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Juri Lelli <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 9a2b764 commit 4468897

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/time/timer.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ EXPORT_SYMBOL(jiffies_64);
156156

157157
/*
158158
* The time start value for each level to select the bucket at enqueue
159-
* time.
159+
* time. We start from the last possible delta of the previous level
160+
* so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
160161
*/
161162
#define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
162163

@@ -490,6 +491,15 @@ static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
490491
static inline unsigned calc_index(unsigned long expires, unsigned lvl,
491492
unsigned long *bucket_expiry)
492493
{
494+
495+
/*
496+
* The timer wheel has to guarantee that a timer does not fire
497+
* early. Early expiry can happen due to:
498+
* - Timer is armed at the edge of a tick
499+
* - Truncation of the expiry time in the outer wheel levels
500+
*
501+
* Round up with level granularity to prevent this.
502+
*/
493503
expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
494504
*bucket_expiry = expires << LVL_SHIFT(lvl);
495505
return LVL_OFFS(lvl) + (expires & LVL_MASK);

0 commit comments

Comments
 (0)