timers: Clear timer_base::must_forward_clk with timer_base::lock held
authorGaurav Kohli <gkohli@codeaurora.org>
Thu, 2 Aug 2018 08:51:03 +0000 (14:21 +0530)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 2 Aug 2018 10:52:38 +0000 (12:52 +0200)
commit363e934d8811d799c88faffc5bfca782fd728334
tree21cd33b9bf7ab353bd968ee8b0ebe7ccb0b2048a
parentb2ab472dc159b7f89e2bc2d305fbc52fc2241dd6
timers: Clear timer_base::must_forward_clk with timer_base::lock held

timer_base::must_forward_clock is indicating that the base clock might be
stale due to a long idle sleep.

The forwarding of the base clock takes place in the timer softirq or when a
timer is enqueued to a base which is idle. If the enqueue of timer to an
idle base happens from a remote CPU, then the following race can happen:

  CPU0 CPU1
  run_timer_softirq mod_timer

base = lock_timer_base(timer);
  base->must_forward_clk = false
if (base->must_forward_clk)
            forward(base); -> skipped

enqueue_timer(base, timer, idx);
-> idx is calculated high due to
   stale base
unlock_timer_base(timer);
  base = lock_timer_base(timer);
  forward(base);

The root cause is that timer_base::must_forward_clk is cleared outside the
timer_base::lock held region, so the remote queuing CPU observes it as
cleared, but the base clock is still stale. This can cause large
granularity values for timers, i.e. the accuracy of the expiry time
suffers.

Prevent this by clearing the flag with timer_base::lock held, so that the
forwarding takes place before the cleared flag is observable by a remote
CPU.

Signed-off-by: Gaurav Kohli <gkohli@codeaurora.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: john.stultz@linaro.org
Cc: sboyd@kernel.org
Cc: linux-arm-msm@vger.kernel.org
Link: https://lkml.kernel.org/r/1533199863-22748-1-git-send-email-gkohli@codeaurora.org
kernel/time/timer.c