Skip to content

Commit 9a68fa0

Browse files
jlelligregkh
authored andcommitted
sched/features: Fix hrtick reprogramming
[ Upstream commit 156ec6f ] Hung tasks and RCU stall cases were reported on systems which were not 100% busy. Investigation of such unexpected cases (no sign of potential starvation caused by tasks hogging the system) pointed out that the periodic sched tick timer wasn't serviced anymore after a certain point and that caused all machinery that depends on it (timers, RCU, etc.) to stop working as well. This issues was however only reproducible if HRTICK was enabled. Looking at core dumps it was found that the rbtree of the hrtimer base used also for the hrtick was corrupted (i.e. next as seen from the base root and actual leftmost obtained by traversing the tree are different). Same base is also used for periodic tick hrtimer, which might get "lost" if the rbtree gets corrupted. Much alike what described in commit 1f71add ("tick/sched: Do not mess with an enqueued hrtimer") there is a race window between hrtimer_set_expires() in hrtick_start and hrtimer_start_expires() in __hrtick_restart() in which the former might be operating on an already queued hrtick hrtimer, which might lead to corruption of the base. Use hrtick_start() (which removes the timer before enqueuing it back) to ensure hrtick hrtimer reprogramming is entirely guarded by the base lock, so that no race conditions can occur. Signed-off-by: Juri Lelli <juri.lelli@redhat.com> Signed-off-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com> Signed-off-by: Daniel Bristot de Oliveira <bristot@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lkml.kernel.org/r/20210208073554.14629-2-juri.lelli@redhat.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 25ceaad commit 9a68fa0

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

‎kernel/sched/core.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,9 @@ static enum hrtimer_restart hrtick(struct hrtimer *timer)
363363
static void __hrtick_restart(struct rq *rq)
364364
{
365365
struct hrtimer *timer = &rq->hrtick_timer;
366+
ktime_t time = rq->hrtick_time;
366367

367-
hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
368+
hrtimer_start(timer, time, HRTIMER_MODE_ABS_PINNED_HARD);
368369
}
369370

370371
/*
@@ -388,17 +389,14 @@ static void __hrtick_start(void *arg)
388389
void hrtick_start(struct rq *rq, u64 delay)
389390
{
390391
struct hrtimer *timer = &rq->hrtick_timer;
391-
ktime_t time;
392392
s64 delta;
393393

394394
/*
395395
* Don't schedule slices shorter than 10000ns, that just
396396
* doesn't make sense and can cause timer DoS.
397397
*/
398398
delta = max_t(s64, delay, 10000LL);
399-
time = ktime_add_ns(timer->base->get_time(), delta);
400-
401-
hrtimer_set_expires(timer, time);
399+
rq->hrtick_time = ktime_add_ns(timer->base->get_time(), delta);
402400

403401
if (rq == this_rq())
404402
__hrtick_restart(rq);

‎kernel/sched/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ struct rq {
10181018
call_single_data_t hrtick_csd;
10191019
#endif
10201020
struct hrtimer hrtick_timer;
1021+
ktime_t hrtick_time;
10211022
#endif
10221023

10231024
#ifdef CONFIG_SCHEDSTATS

0 commit comments

Comments
 (0)