Skip to content

Commit 09dc4ab

Browse files
Roman GushchinIngo Molnar
authored andcommitted
sched/fair: Fix tg_set_cfs_bandwidth() deadlock on rq->lock
tg_set_cfs_bandwidth() sets cfs_b->timer_active to 0 to force the period timer restart. It's not safe, because can lead to deadlock, described in commit 927b54f: "__start_cfs_bandwidth calls hrtimer_cancel while holding rq->lock, waiting for the hrtimer to finish. However, if sched_cfs_period_timer runs for another loop iteration, the hrtimer can attempt to take rq->lock, resulting in deadlock." Three CPUs must be involved: CPU0 CPU1 CPU2 take rq->lock period timer fired ... take cfs_b lock ... ... tg_set_cfs_bandwidth() throttle_cfs_rq() release cfs_b lock take cfs_b lock ... distribute_cfs_runtime() timer_active = 0 take cfs_b->lock wait for rq->lock ... __start_cfs_bandwidth() {wait for timer callback break if timer_active == 1} So, CPU0 and CPU1 are deadlocked. Instead of resetting cfs_b->timer_active, tg_set_cfs_bandwidth can wait for period timer callbacks (ignoring cfs_b->timer_active) and restart the timer explicitly. Signed-off-by: Roman Gushchin <[email protected]> Reviewed-by: Ben Segall <[email protected]> Signed-off-by: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/87wqdi9g8e.wl\%[email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: Linus Torvalds <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent 0f397f2 commit 09dc4ab

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

kernel/sched/core.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7751,8 +7751,7 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
77517751
/* restart the period timer (if active) to handle new period expiry */
77527752
if (runtime_enabled && cfs_b->timer_active) {
77537753
/* force a reprogram */
7754-
cfs_b->timer_active = 0;
7755-
__start_cfs_bandwidth(cfs_b);
7754+
__start_cfs_bandwidth(cfs_b, true);
77567755
}
77577756
raw_spin_unlock_irq(&cfs_b->lock);
77587757

kernel/sched/fair.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
31303130
*/
31313131
if (!cfs_b->timer_active) {
31323132
__refill_cfs_bandwidth_runtime(cfs_b);
3133-
__start_cfs_bandwidth(cfs_b);
3133+
__start_cfs_bandwidth(cfs_b, false);
31343134
}
31353135

31363136
if (cfs_b->runtime > 0) {
@@ -3309,7 +3309,7 @@ static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
33093309
raw_spin_lock(&cfs_b->lock);
33103310
list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
33113311
if (!cfs_b->timer_active)
3312-
__start_cfs_bandwidth(cfs_b);
3312+
__start_cfs_bandwidth(cfs_b, false);
33133313
raw_spin_unlock(&cfs_b->lock);
33143314
}
33153315

@@ -3691,7 +3691,7 @@ static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
36913691
}
36923692

36933693
/* requires cfs_b->lock, may release to reprogram timer */
3694-
void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
3694+
void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b, bool force)
36953695
{
36963696
/*
36973697
* The timer may be active because we're trying to set a new bandwidth
@@ -3706,7 +3706,7 @@ void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
37063706
cpu_relax();
37073707
raw_spin_lock(&cfs_b->lock);
37083708
/* if someone else restarted the timer then we're done */
3709-
if (cfs_b->timer_active)
3709+
if (!force && cfs_b->timer_active)
37103710
return;
37113711
}
37123712

kernel/sched/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
278278
extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
279279

280280
extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
281-
extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
281+
extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b, bool force);
282282
extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
283283

284284
extern void free_rt_sched_group(struct task_group *tg);

0 commit comments

Comments
 (0)