Re: [PATCH] sched/dl: Fix race between dl_task_timer() and sched_setaffinity()

From: Kirill Tkhai
Date: Tue May 20 2014 - 05:33:56 EST


20.05.2014, 12:16, "Juri Lelli" <juri.lelli@xxxxxxxxx>:
> Hi,
>
> On Tue, 20 May 2014 09:53:15 +0200
> Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
>>  On Tue, May 20, 2014 at 09:08:53AM +0400, Kirill Tkhai wrote:
>>>  20.05.2014, 04:00, "Peter Zijlstra" <peterz@xxxxxxxxxxxxx>:
>>>>  On Mon, May 19, 2014 at 11:31:19PM +0400, Kirill Tkhai wrote:
>>>>>   @@ -513,9 +513,17 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
>>>>>                                                         struct sched_dl_entity,
>>>>>                                                         dl_timer);
>>>>>            struct task_struct *p = dl_task_of(dl_se);
>>>>>   - struct rq *rq = task_rq(p);
>>>>>   + struct rq *rq;
>>>>>   +again:
>>>>>   + rq = task_rq(p);
>>>>>            raw_spin_lock(&rq->lock);
>>>>>
>>>>>   + if (unlikely(rq != task_rq(p))) {
>>>>>   + /* Task was moved, retrying. */
>>>>>   + raw_spin_unlock(&rq->lock);
>>>>>   + goto again;
>>>>>   + }
>>>>>   +
>>>>  That thing is called: rq = __task_rq_lock(p);
>>>  But p->pi_lock is not held. The problem is __task_rq_lock() has lockdep assert.
>>>  Should we change it?
>>  Ok, so now that I'm awake ;-)
>>
>>  So the trivial problem as described by your initial changelog isn't
>>  right, because we cannot call sched_setaffinity() on deadline tasks, or
>>  rather we can, but we can't actually change the affinity mask.
>
> Well, if we disable AC we can. And I was able to recreate that race in
> that case.
>
>>  Now I suppose the problem can still actually happen when you change the
>>  root domain and trigger a effective affinity change that way.
>
> Yeah, I think here too.
>
>>  That said, no leave it as you proposed, adding a *task_rq_lock() variant
>>  without lockdep assert in will only confuse things, as normally we
>>  really should be also taking ->pi_lock.
>>
>>  The only reason we don't strictly need ->pi_lock now is because we're
>>  guaranteed to have p->state == TASK_RUNNING here and are thus free of
>>  ttwu races.
>
> Maybe we could add this as part of the comment.

Peter, Juri, thanks for comment. Hope, I understood you right :)

[PATCH] sched/dl: Fix race in dl_task_timer()

Throttled task is still on rq, and it may be moved to other cpu
if user is playing with sched_setaffinity(). Therefore, unlocked
task_rq() access makes the race.

Juri Lelli reports he got this race when dl_bandwidth_enabled()
was not set.

Other thing, pointed by Peter Zijlstra:

"Now I suppose the problem can still actually happen when
you change the root domain and trigger a effective affinity
change that way".

To fix that we do the same as made in __task_rq_lock(). We do not
use __task_rq_lock() itself, because it has a useful lockdep check,
which is not correct in case of dl_task_timer(). We do not need
pi_lock locked here. This case is an exception (PeterZ):

"The only reason we don't strictly need ->pi_lock now is because
we're guaranteed to have p->state == TASK_RUNNING here and are
thus free of ttwu races".

Signed-off-by: Kirill Tkhai <tkhai@xxxxxxxxx>
CC: Juri Lelli <juri.lelli@xxxxxxxxx>
CC: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CC: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: <stable@xxxxxxxxxxxxxxx> # v3.14
---
kernel/sched/deadline.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 800e99b..14bc348 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -513,9 +513,17 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
struct sched_dl_entity,
dl_timer);
struct task_struct *p = dl_task_of(dl_se);
- struct rq *rq = task_rq(p);
+ struct rq *rq;
+again:
+ rq = task_rq(p);
raw_spin_lock(&rq->lock);

+ if (rq != task_rq(p)) {
+ /* Task was moved, retrying. */
+ raw_spin_unlock(&rq->lock);
+ goto again;
+ }
+
/*
* We need to take care of a possible races here. In fact, the
* task might have changed its scheduling policy to something
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/