public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Ben Segall <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, bsegall@google.com, hpa@zytor.com,
	mingo@kernel.org, peterz@infradead.org, tglx@linutronix.de
Subject: [tip:sched/core] sched: Fix hrtimer_cancel()/rq->lock deadlock
Date: Tue, 29 Oct 2013 07:07:31 -0700	[thread overview]
Message-ID: <tip-927b54fccbf04207ec92f669dce6806848cbec7d@git.kernel.org> (raw)
In-Reply-To: <20131016181622.22647.16643.stgit@sword-of-the-dawn.mtv.corp.google.com>

Commit-ID:  927b54fccbf04207ec92f669dce6806848cbec7d
Gitweb:     http://git.kernel.org/tip/927b54fccbf04207ec92f669dce6806848cbec7d
Author:     Ben Segall <bsegall@google.com>
AuthorDate: Wed, 16 Oct 2013 11:16:22 -0700
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Tue, 29 Oct 2013 12:02:21 +0100

sched: Fix hrtimer_cancel()/rq->lock deadlock

__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.

Fix this by ensuring that cfs_b->timer_active is cleared only if the
_latest_ call to do_sched_cfs_period_timer is returning as idle. Then
__start_cfs_bandwidth can just call hrtimer_try_to_cancel and wait for
that to succeed or timer_active == 1.

Signed-off-by: Ben Segall <bsegall@google.com>
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Cc: pjt@google.com
Link: http://lkml.kernel.org/r/20131016181622.22647.16643.stgit@sword-of-the-dawn.mtv.corp.google.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 kernel/sched/fair.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 897d977..f6308cb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3225,6 +3225,13 @@ static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
 	if (idle)
 		goto out_unlock;
 
+	/*
+	 * if we have relooped after returning idle once, we need to update our
+	 * status as actually running, so that other cpus doing
+	 * __start_cfs_bandwidth will stop trying to cancel us.
+	 */
+	cfs_b->timer_active = 1;
+
 	__refill_cfs_bandwidth_runtime(cfs_b);
 
 	if (!throttled) {
@@ -3493,11 +3500,11 @@ void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
 	 * (timer_active==0 becomes visible before the hrtimer call-back
 	 * terminates).  In either case we ensure that it's re-programmed
 	 */
-	while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
+	while (unlikely(hrtimer_active(&cfs_b->period_timer)) &&
+	       hrtimer_try_to_cancel(&cfs_b->period_timer) < 0) {
+		/* bounce the lock to allow do_sched_cfs_period_timer to run */
 		raw_spin_unlock(&cfs_b->lock);
-		/* ensure cfs_b->lock is available while we wait */
-		hrtimer_cancel(&cfs_b->period_timer);
-
+		cpu_relax();
 		raw_spin_lock(&cfs_b->lock);
 		/* if someone else restarted the timer then we're done */
 		if (cfs_b->timer_active)

  reply	other threads:[~2013-10-29 14:08 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-16 18:16 [PATCH 0/5] Fix several races in CFS_BANDWIDTH Ben Segall
2013-10-16 18:16 ` [PATCH 1/5] sched: Fix race on toggling cfs_bandwidth_used Ben Segall
2013-10-29 14:07   ` [tip:sched/core] " tip-bot for Ben Segall
2013-10-16 18:16 ` [PATCH 2/5] sched: Fix cfs_bandwidth misuse of hrtimer_expires_remaining Ben Segall
2013-10-16 18:41   ` Peter Zijlstra
2013-10-16 19:12     ` bsegall
2013-10-29 14:07   ` [tip:sched/core] " tip-bot for Ben Segall
2013-10-16 18:16 ` [PATCH 3/5] sched: Fix hrtimer_cancel/rq->lock deadlock Ben Segall
2013-10-29 14:07   ` tip-bot for Ben Segall [this message]
2013-10-16 18:16 ` [PATCH 4/5] sched: Guarantee new group-entities always have weight Ben Segall
2013-10-16 22:01   ` Peter Zijlstra
2013-10-16 22:16     ` Paul Turner
2013-10-16 22:22       ` Peter Zijlstra
2013-10-16 22:40     ` bsegall
2013-10-29 14:07   ` [tip:sched/core] " tip-bot for Paul Turner
2013-10-16 18:16 ` [PATCH 5/5] sched: Avoid throttle_cfs_rq racing with period_timer stopping Ben Segall
2013-10-16 22:06   ` Peter Zijlstra
2013-10-29 14:07   ` [tip:sched/core] sched: Avoid throttle_cfs_rq() " tip-bot for Ben Segall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-927b54fccbf04207ec92f669dce6806848cbec7d@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=bsegall@google.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox