linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
@ 2017-09-11  6:51 Zhou Chengming
  2017-09-12  2:11 ` zhouchengming
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Zhou Chengming @ 2017-09-11  6:51 UTC (permalink / raw)
  To: linux-kernel; +Cc: mingo, peterz, rostedt, huawei.libin, zhouchengming1

push_rt_task() pick the first pushable task and find an eligible
lowest_rq, then double_lock_balance(rq, lowest_rq). So if
double_lock_balance() unlock the rq (when double_lock_balance() return 1),
we have to check if this task is still on the rq.

The problem is that the check conditions are not sufficient:

if (unlikely(task_rq(task) != rq ||
	     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
	     task_running(rq, task) ||
	     !rt_task(task) ||
	     !task_on_rq_queued(task))) {

cpu2				cpu1			cpu0
push_rt_task(rq1)
  pick task_A on rq1
  find rq0
    double_lock_balance(rq1, rq0)
      unlock(rq1)
				rq1 __schedule
				  pick task_A run
				task_A sleep (dequeued)
      lock(rq0)
      lock(rq1)
    do_above_check(task_A)
      task_rq(task_A) == rq1
      cpus_allowed unchanged
      task_running == false
      rt_task(task_A) == true
							try_to_wake_up(task_A)
							  select_cpu = cpu3
							  enqueue(rq3, task_A)
							  task_A->on_rq = 1
      task_on_rq_queued(task_A)
    above_check passed, return rq0
    ...
    migrate task_A from rq1 to rq0

So we can't rely on these checks of task_A to make sure the task_A is
still on the rq1, even though we hold the rq1->lock. This patch will
repick the first pushable task to be sure the task is still on the rq.

Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
---
 kernel/sched/rt.c | 49 +++++++++++++++++++++++--------------------------
 1 file changed, 23 insertions(+), 26 deletions(-)

diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 45caf93..787b721 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -1703,6 +1703,26 @@ static int find_lowest_rq(struct task_struct *task)
 	return -1;
 }
 
+static struct task_struct *pick_next_pushable_task(struct rq *rq)
+{
+	struct task_struct *p;
+
+	if (!has_pushable_tasks(rq))
+		return NULL;
+
+	p = plist_first_entry(&rq->rt.pushable_tasks,
+			      struct task_struct, pushable_tasks);
+
+	BUG_ON(rq->cpu != task_cpu(p));
+	BUG_ON(task_current(rq, p));
+	BUG_ON(p->nr_cpus_allowed <= 1);
+
+	BUG_ON(!task_on_rq_queued(p));
+	BUG_ON(!rt_task(p));
+
+	return p;
+}
+
 /* Will lock the rq it finds */
 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
 {
@@ -1734,13 +1754,10 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
 			 * We had to unlock the run queue. In
 			 * the mean time, task could have
 			 * migrated already or had its affinity changed.
-			 * Also make sure that it wasn't scheduled on its rq.
 			 */
-			if (unlikely(task_rq(task) != rq ||
-				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
-				     task_running(rq, task) ||
-				     !rt_task(task) ||
-				     !task_on_rq_queued(task))) {
+			struct task_struct *next_task = pick_next_pushable_task(rq);
+			if (unlikely(next_task != task ||
+				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed))) {
 
 				double_unlock_balance(rq, lowest_rq);
 				lowest_rq = NULL;
@@ -1760,26 +1777,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
 	return lowest_rq;
 }
 
-static struct task_struct *pick_next_pushable_task(struct rq *rq)
-{
-	struct task_struct *p;
-
-	if (!has_pushable_tasks(rq))
-		return NULL;
-
-	p = plist_first_entry(&rq->rt.pushable_tasks,
-			      struct task_struct, pushable_tasks);
-
-	BUG_ON(rq->cpu != task_cpu(p));
-	BUG_ON(task_current(rq, p));
-	BUG_ON(p->nr_cpus_allowed <= 1);
-
-	BUG_ON(!task_on_rq_queued(p));
-	BUG_ON(!rt_task(p));
-
-	return p;
-}
-
 /*
  * If the current CPU has more than one RT task, see if the non
  * running task can migrate over to a CPU that is running a task
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-11  6:51 [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq Zhou Chengming
@ 2017-09-12  2:11 ` zhouchengming
  2017-09-25 11:53 ` zhouchengming
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: zhouchengming @ 2017-09-12  2:11 UTC (permalink / raw)
  To: Zhou Chengming
  Cc: linux-kernel, mingo, peterz, rostedt, Thomas Gleixner, Zefan Li,
	miaoxie (A),
	Li Bin

polite ping and +cc, thanks!

On 2017/9/11 14:51, Zhou Chengming wrote:
> push_rt_task() pick the first pushable task and find an eligible
> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
> we have to check if this task is still on the rq.
>
> The problem is that the check conditions are not sufficient:
>
> if (unlikely(task_rq(task) != rq ||
> 	     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
> 	     task_running(rq, task) ||
> 	     !rt_task(task) ||
> 	     !task_on_rq_queued(task))) {
>
> cpu2				cpu1			cpu0
> push_rt_task(rq1)
>    pick task_A on rq1
>    find rq0
>      double_lock_balance(rq1, rq0)
>        unlock(rq1)
> 				rq1 __schedule
> 				  pick task_A run
> 				task_A sleep (dequeued)
>        lock(rq0)
>        lock(rq1)
>      do_above_check(task_A)
>        task_rq(task_A) == rq1
>        cpus_allowed unchanged
>        task_running == false
>        rt_task(task_A) == true
> 							try_to_wake_up(task_A)
> 							  select_cpu = cpu3
> 							  enqueue(rq3, task_A)
> 							  task_A->on_rq = 1
>        task_on_rq_queued(task_A)
>      above_check passed, return rq0
>      ...
>      migrate task_A from rq1 to rq0
>
> So we can't rely on these checks of task_A to make sure the task_A is
> still on the rq1, even though we hold the rq1->lock. This patch will
> repick the first pushable task to be sure the task is still on the rq.
>
> Signed-off-by: Zhou Chengming<zhouchengming1@huawei.com>
> ---
>   kernel/sched/rt.c | 49 +++++++++++++++++++++++--------------------------
>   1 file changed, 23 insertions(+), 26 deletions(-)
>
> diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
> index 45caf93..787b721 100644
> --- a/kernel/sched/rt.c
> +++ b/kernel/sched/rt.c
> @@ -1703,6 +1703,26 @@ static int find_lowest_rq(struct task_struct *task)
>   	return -1;
>   }
>
> +static struct task_struct *pick_next_pushable_task(struct rq *rq)
> +{
> +	struct task_struct *p;
> +
> +	if (!has_pushable_tasks(rq))
> +		return NULL;
> +
> +	p = plist_first_entry(&rq->rt.pushable_tasks,
> +			      struct task_struct, pushable_tasks);
> +
> +	BUG_ON(rq->cpu != task_cpu(p));
> +	BUG_ON(task_current(rq, p));
> +	BUG_ON(p->nr_cpus_allowed<= 1);
> +
> +	BUG_ON(!task_on_rq_queued(p));
> +	BUG_ON(!rt_task(p));
> +
> +	return p;
> +}
> +
>   /* Will lock the rq it finds */
>   static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   {
> @@ -1734,13 +1754,10 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   			 * We had to unlock the run queue. In
>   			 * the mean time, task could have
>   			 * migrated already or had its affinity changed.
> -			 * Also make sure that it wasn't scheduled on its rq.
>   			 */
> -			if (unlikely(task_rq(task) != rq ||
> -				     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
> -				     task_running(rq, task) ||
> -				     !rt_task(task) ||
> -				     !task_on_rq_queued(task))) {
> +			struct task_struct *next_task = pick_next_pushable_task(rq);
> +			if (unlikely(next_task != task ||
> +				     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed))) {
>
>   				double_unlock_balance(rq, lowest_rq);
>   				lowest_rq = NULL;
> @@ -1760,26 +1777,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   	return lowest_rq;
>   }
>
> -static struct task_struct *pick_next_pushable_task(struct rq *rq)
> -{
> -	struct task_struct *p;
> -
> -	if (!has_pushable_tasks(rq))
> -		return NULL;
> -
> -	p = plist_first_entry(&rq->rt.pushable_tasks,
> -			      struct task_struct, pushable_tasks);
> -
> -	BUG_ON(rq->cpu != task_cpu(p));
> -	BUG_ON(task_current(rq, p));
> -	BUG_ON(p->nr_cpus_allowed<= 1);
> -
> -	BUG_ON(!task_on_rq_queued(p));
> -	BUG_ON(!rt_task(p));
> -
> -	return p;
> -}
> -
>   /*
>    * If the current CPU has more than one RT task, see if the non
>    * running task can migrate over to a CPU that is running a task

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-11  6:51 [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq Zhou Chengming
  2017-09-12  2:11 ` zhouchengming
@ 2017-09-25 11:53 ` zhouchengming
  2017-09-25 19:40 ` Steven Rostedt
  2018-04-10 22:05 ` Steven Rostedt
  3 siblings, 0 replies; 10+ messages in thread
From: zhouchengming @ 2017-09-25 11:53 UTC (permalink / raw)
  To: Zhou Chengming, linux-kernel, mingo, peterz, rostedt; +Cc: huawei.libin

ping...
Or it isn't a real problem ?

Thanks.

On 2017/9/11 14:51, Zhou Chengming wrote:
> push_rt_task() pick the first pushable task and find an eligible
> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
> we have to check if this task is still on the rq.
>
> The problem is that the check conditions are not sufficient:
>
> if (unlikely(task_rq(task) != rq ||
> 	     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
> 	     task_running(rq, task) ||
> 	     !rt_task(task) ||
> 	     !task_on_rq_queued(task))) {
>
> cpu2				cpu1			cpu0
> push_rt_task(rq1)
>    pick task_A on rq1
>    find rq0
>      double_lock_balance(rq1, rq0)
>        unlock(rq1)
> 				rq1 __schedule
> 				  pick task_A run
> 				task_A sleep (dequeued)
>        lock(rq0)
>        lock(rq1)
>      do_above_check(task_A)
>        task_rq(task_A) == rq1
>        cpus_allowed unchanged
>        task_running == false
>        rt_task(task_A) == true
> 							try_to_wake_up(task_A)
> 							  select_cpu = cpu3
> 							  enqueue(rq3, task_A)
> 							  task_A->on_rq = 1
>        task_on_rq_queued(task_A)
>      above_check passed, return rq0
>      ...
>      migrate task_A from rq1 to rq0
>
> So we can't rely on these checks of task_A to make sure the task_A is
> still on the rq1, even though we hold the rq1->lock. This patch will
> repick the first pushable task to be sure the task is still on the rq.
>
> Signed-off-by: Zhou Chengming<zhouchengming1@huawei.com>
> ---
>   kernel/sched/rt.c | 49 +++++++++++++++++++++++--------------------------
>   1 file changed, 23 insertions(+), 26 deletions(-)
>
> diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
> index 45caf93..787b721 100644
> --- a/kernel/sched/rt.c
> +++ b/kernel/sched/rt.c
> @@ -1703,6 +1703,26 @@ static int find_lowest_rq(struct task_struct *task)
>   	return -1;
>   }
>
> +static struct task_struct *pick_next_pushable_task(struct rq *rq)
> +{
> +	struct task_struct *p;
> +
> +	if (!has_pushable_tasks(rq))
> +		return NULL;
> +
> +	p = plist_first_entry(&rq->rt.pushable_tasks,
> +			      struct task_struct, pushable_tasks);
> +
> +	BUG_ON(rq->cpu != task_cpu(p));
> +	BUG_ON(task_current(rq, p));
> +	BUG_ON(p->nr_cpus_allowed<= 1);
> +
> +	BUG_ON(!task_on_rq_queued(p));
> +	BUG_ON(!rt_task(p));
> +
> +	return p;
> +}
> +
>   /* Will lock the rq it finds */
>   static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   {
> @@ -1734,13 +1754,10 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   			 * We had to unlock the run queue. In
>   			 * the mean time, task could have
>   			 * migrated already or had its affinity changed.
> -			 * Also make sure that it wasn't scheduled on its rq.
>   			 */
> -			if (unlikely(task_rq(task) != rq ||
> -				     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
> -				     task_running(rq, task) ||
> -				     !rt_task(task) ||
> -				     !task_on_rq_queued(task))) {
> +			struct task_struct *next_task = pick_next_pushable_task(rq);
> +			if (unlikely(next_task != task ||
> +				     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed))) {
>
>   				double_unlock_balance(rq, lowest_rq);
>   				lowest_rq = NULL;
> @@ -1760,26 +1777,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>   	return lowest_rq;
>   }
>
> -static struct task_struct *pick_next_pushable_task(struct rq *rq)
> -{
> -	struct task_struct *p;
> -
> -	if (!has_pushable_tasks(rq))
> -		return NULL;
> -
> -	p = plist_first_entry(&rq->rt.pushable_tasks,
> -			      struct task_struct, pushable_tasks);
> -
> -	BUG_ON(rq->cpu != task_cpu(p));
> -	BUG_ON(task_current(rq, p));
> -	BUG_ON(p->nr_cpus_allowed<= 1);
> -
> -	BUG_ON(!task_on_rq_queued(p));
> -	BUG_ON(!rt_task(p));
> -
> -	return p;
> -}
> -
>   /*
>    * If the current CPU has more than one RT task, see if the non
>    * running task can migrate over to a CPU that is running a task

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-11  6:51 [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq Zhou Chengming
  2017-09-12  2:11 ` zhouchengming
  2017-09-25 11:53 ` zhouchengming
@ 2017-09-25 19:40 ` Steven Rostedt
  2017-09-26  1:23   ` zhouchengming
  2018-04-10 22:05 ` Steven Rostedt
  3 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2017-09-25 19:40 UTC (permalink / raw)
  To: Zhou Chengming; +Cc: linux-kernel, mingo, peterz, huawei.libin

On Mon, 11 Sep 2017 14:51:49 +0800
Zhou Chengming <zhouchengming1@huawei.com> wrote:

> push_rt_task() pick the first pushable task and find an eligible
> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
> we have to check if this task is still on the rq.
> 
> The problem is that the check conditions are not sufficient:
> 
> if (unlikely(task_rq(task) != rq ||
> 	     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
> 	     task_running(rq, task) ||
> 	     !rt_task(task) ||
> 	     !task_on_rq_queued(task))) {
> 
> cpu2				cpu1			cpu0
> push_rt_task(rq1)
>   pick task_A on rq1
>   find rq0
>     double_lock_balance(rq1, rq0)
>       unlock(rq1)
> 				rq1 __schedule
> 				  pick task_A run
> 				task_A sleep (dequeued)
>       lock(rq0)
>       lock(rq1)
>     do_above_check(task_A)
>       task_rq(task_A) == rq1
>       cpus_allowed unchanged
>       task_running == false
>       rt_task(task_A) == true
> 							try_to_wake_up(task_A)
> 							  select_cpu = cpu3
> 							  enqueue(rq3, task_A)

How can this happen? The try_to_wake_up(task_A) needs to grab the rq
that task A is on, and we have that rq lock. 

/me confused.

-- Steve


> 							  task_A->on_rq = 1
>       task_on_rq_queued(task_A)
>     above_check passed, return rq0
>     ...
>     migrate task_A from rq1 to rq0
> 
> So we can't rely on these checks of task_A to make sure the task_A is
> still on the rq1, even though we hold the rq1->lock. This patch will
> repick the first pushable task to be sure the task is still on the rq.
> 
> Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-25 19:40 ` Steven Rostedt
@ 2017-09-26  1:23   ` zhouchengming
  2017-09-26  3:18     ` Steven Rostedt
  0 siblings, 1 reply; 10+ messages in thread
From: zhouchengming @ 2017-09-26  1:23 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: linux-kernel, mingo, peterz, huawei.libin

On 2017/9/26 3:40, Steven Rostedt wrote:
> On Mon, 11 Sep 2017 14:51:49 +0800
> Zhou Chengming<zhouchengming1@huawei.com>  wrote:
>
>> push_rt_task() pick the first pushable task and find an eligible
>> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
>> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
>> we have to check if this task is still on the rq.
>>
>> The problem is that the check conditions are not sufficient:
>>
>> if (unlikely(task_rq(task) != rq ||
>> 	     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
>> 	     task_running(rq, task) ||
>> 	     !rt_task(task) ||
>> 	     !task_on_rq_queued(task))) {
>>
>> cpu2				cpu1			cpu0
>> push_rt_task(rq1)
>>    pick task_A on rq1
>>    find rq0
>>      double_lock_balance(rq1, rq0)
>>        unlock(rq1)
>> 				rq1 __schedule
>> 				  pick task_A run
>> 				task_A sleep (dequeued)
>>        lock(rq0)
>>        lock(rq1)
>>      do_above_check(task_A)
>>        task_rq(task_A) == rq1
>>        cpus_allowed unchanged
>>        task_running == false
>>        rt_task(task_A) == true
>> 							try_to_wake_up(task_A)
>> 							  select_cpu = cpu3
>> 							  enqueue(rq3, task_A)
> How can this happen? The try_to_wake_up(task_A) needs to grab the rq
> that task A is on, and we have that rq lock.
>
> /me confused.
>
> -- Steve

Thanks for the reply!
After the task_A sleep on cpu1, the try_to_wake_up(task_A) on cpu0 select a different cpu3,
so it will grab the rq3 lock, not the rq1 lock.

Thanks.

>
>> 							task_A->on_rq = 1
>>        task_on_rq_queued(task_A)
>>      above_check passed, return rq0
>>      ...
>>      migrate task_A from rq1 to rq0
>>
>> So we can't rely on these checks of task_A to make sure the task_A is
>> still on the rq1, even though we hold the rq1->lock. This patch will
>> repick the first pushable task to be sure the task is still on the rq.
>>
>> Signed-off-by: Zhou Chengming<zhouchengming1@huawei.com>
>>
> .
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-26  1:23   ` zhouchengming
@ 2017-09-26  3:18     ` Steven Rostedt
  2017-10-07  3:30       ` zhouchengming
  0 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2017-09-26  3:18 UTC (permalink / raw)
  To: zhouchengming; +Cc: linux-kernel, mingo, peterz, huawei.libin

On Tue, 26 Sep 2017 09:23:20 +0800
zhouchengming <zhouchengming1@huawei.com> wrote:

> On 2017/9/26 3:40, Steven Rostedt wrote:
> > On Mon, 11 Sep 2017 14:51:49 +0800
> > Zhou Chengming<zhouchengming1@huawei.com>  wrote:
> >  
> >> push_rt_task() pick the first pushable task and find an eligible
> >> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
> >> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
> >> we have to check if this task is still on the rq.
> >>
> >> The problem is that the check conditions are not sufficient:
> >>
> >> if (unlikely(task_rq(task) != rq ||
> >> 	     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
> >> 	     task_running(rq, task) ||
> >> 	     !rt_task(task) ||
> >> 	     !task_on_rq_queued(task))) {
> >>
> >> cpu2				cpu1			cpu0
> >> push_rt_task(rq1)
> >>    pick task_A on rq1
> >>    find rq0
> >>      double_lock_balance(rq1, rq0)
> >>        unlock(rq1)
> >> 				rq1 __schedule
> >> 				  pick task_A run
> >> 				task_A sleep (dequeued)
> >>        lock(rq0)
> >>        lock(rq1)
> >>      do_above_check(task_A)
> >>        task_rq(task_A) == rq1
> >>        cpus_allowed unchanged
> >>        task_running == false
> >>        rt_task(task_A) == true
> >> 							try_to_wake_up(task_A)
> >> 							  select_cpu = cpu3
> >> 							  enqueue(rq3, task_A)  
> > How can this happen? The try_to_wake_up(task_A) needs to grab the rq
> > that task A is on, and we have that rq lock.
> >
> > /me confused.
> >
> > -- Steve  
> 
> Thanks for the reply!
> After the task_A sleep on cpu1, the try_to_wake_up(task_A) on cpu0 select a different cpu3,
> so it will grab the rq3 lock, not the rq1 lock.

Ah crap. This is caused by 7608dec2ce20 ("sched: Drop the rq argument
to sched_class::select_task_rq()"). Because this code depends on
try_to_wake_up() grabbing the task's rq lock. But it no longer does
that, and it causes this race.

OK, I need to look at this deeper when I'm not so jetlagged and typing
this because I can't sleep at 5am.

Thanks for pointing this out!

It may be fixed by simply grabbing the run queue lock on migration, as
that would sync things up.

Peter?


-- Steve

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-26  3:18     ` Steven Rostedt
@ 2017-10-07  3:30       ` zhouchengming
  0 siblings, 0 replies; 10+ messages in thread
From: zhouchengming @ 2017-10-07  3:30 UTC (permalink / raw)
  To: Steven Rostedt, peterz; +Cc: linux-kernel, mingo, huawei.libin

Hi Steven, Peter,

On 2017/9/26 11:18, Steven Rostedt wrote:
> On Tue, 26 Sep 2017 09:23:20 +0800
> zhouchengming<zhouchengming1@huawei.com>  wrote:
>
>> On 2017/9/26 3:40, Steven Rostedt wrote:
>>> On Mon, 11 Sep 2017 14:51:49 +0800
>>> Zhou Chengming<zhouchengming1@huawei.com>   wrote:
>>>
>>>> push_rt_task() pick the first pushable task and find an eligible
>>>> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
>>>> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
>>>> we have to check if this task is still on the rq.
>>>>
>>>> The problem is that the check conditions are not sufficient:
>>>>
>>>> if (unlikely(task_rq(task) != rq ||
>>>> 	     !cpumask_test_cpu(lowest_rq->cpu,&task->cpus_allowed) ||
>>>> 	     task_running(rq, task) ||
>>>> 	     !rt_task(task) ||
>>>> 	     !task_on_rq_queued(task))) {
>>>>
>>>> cpu2				cpu1			cpu0
>>>> push_rt_task(rq1)
>>>>     pick task_A on rq1
>>>>     find rq0
>>>>       double_lock_balance(rq1, rq0)
>>>>         unlock(rq1)
>>>> 				rq1 __schedule
>>>> 				  pick task_A run
>>>> 				task_A sleep (dequeued)
>>>>         lock(rq0)
>>>>         lock(rq1)
>>>>       do_above_check(task_A)
>>>>         task_rq(task_A) == rq1
>>>>         cpus_allowed unchanged
>>>>         task_running == false
>>>>         rt_task(task_A) == true
>>>> 							try_to_wake_up(task_A)
>>>> 							  select_cpu = cpu3
>>>> 							  enqueue(rq3, task_A)
>>> How can this happen? The try_to_wake_up(task_A) needs to grab the rq
>>> that task A is on, and we have that rq lock.
>>>
>>> /me confused.
>>>
>>> -- Steve
>> Thanks for the reply!
>> After the task_A sleep on cpu1, the try_to_wake_up(task_A) on cpu0 select a different cpu3,
>> so it will grab the rq3 lock, not the rq1 lock.
> Ah crap. This is caused by 7608dec2ce20 ("sched: Drop the rq argument
> to sched_class::select_task_rq()"). Because this code depends on
> try_to_wake_up() grabbing the task's rq lock. But it no longer does
> that, and it causes this race.
>
> OK, I need to look at this deeper when I'm not so jetlagged and typing
> this because I can't sleep at 5am.
>
> Thanks for pointing this out!
>
> It may be fixed by simply grabbing the run queue lock on migration, as
> that would sync things up.

Is there any new solution? I don't think grabbing the rq lock without the task->pi_lock
will fix this problem. And I think my patch is correct and the changes are small.

Thanks!

> Peter?
>
>
> -- Steve
>
>
>
> .
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2017-09-11  6:51 [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq Zhou Chengming
                   ` (2 preceding siblings ...)
  2017-09-25 19:40 ` Steven Rostedt
@ 2018-04-10 22:05 ` Steven Rostedt
  2018-04-11 10:26   ` Peter Zijlstra
  3 siblings, 1 reply; 10+ messages in thread
From: Steven Rostedt @ 2018-04-10 22:05 UTC (permalink / raw)
  To: Zhou Chengming; +Cc: linux-kernel, mingo, peterz, huawei.libin


Peter,

Going through my inbox, I stumbled across this one. And it doesn't
appear to be addressed.

I think this patch is a reasonable solution.

One small nit below though, but other than that.

Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>


On Mon, 11 Sep 2017 14:51:49 +0800
Zhou Chengming <zhouchengming1@huawei.com> wrote:

> push_rt_task() pick the first pushable task and find an eligible
> lowest_rq, then double_lock_balance(rq, lowest_rq). So if
> double_lock_balance() unlock the rq (when double_lock_balance() return 1),
> we have to check if this task is still on the rq.
> 
> The problem is that the check conditions are not sufficient:
> 
> if (unlikely(task_rq(task) != rq ||
> 	     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
> 	     task_running(rq, task) ||
> 	     !rt_task(task) ||
> 	     !task_on_rq_queued(task))) {
> 
> cpu2				cpu1			cpu0
> push_rt_task(rq1)
>   pick task_A on rq1
>   find rq0
>     double_lock_balance(rq1, rq0)
>       unlock(rq1)
> 				rq1 __schedule
> 				  pick task_A run
> 				task_A sleep (dequeued)
>       lock(rq0)
>       lock(rq1)
>     do_above_check(task_A)
>       task_rq(task_A) == rq1
>       cpus_allowed unchanged
>       task_running == false
>       rt_task(task_A) == true
> 							try_to_wake_up(task_A)
> 							  select_cpu = cpu3
> 							  enqueue(rq3, task_A)
> 							  task_A->on_rq = 1
>       task_on_rq_queued(task_A)
>     above_check passed, return rq0
>     ...
>     migrate task_A from rq1 to rq0
> 
> So we can't rely on these checks of task_A to make sure the task_A is
> still on the rq1, even though we hold the rq1->lock. This patch will
> repick the first pushable task to be sure the task is still on the rq.
> 
> Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
> ---
>  kernel/sched/rt.c | 49 +++++++++++++++++++++++--------------------------
>  1 file changed, 23 insertions(+), 26 deletions(-)
> 
> diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
> index 45caf93..787b721 100644
> --- a/kernel/sched/rt.c
> +++ b/kernel/sched/rt.c
> @@ -1703,6 +1703,26 @@ static int find_lowest_rq(struct task_struct *task)
>  	return -1;
>  }
>  
> +static struct task_struct *pick_next_pushable_task(struct rq *rq)
> +{
> +	struct task_struct *p;
> +
> +	if (!has_pushable_tasks(rq))
> +		return NULL;
> +
> +	p = plist_first_entry(&rq->rt.pushable_tasks,
> +			      struct task_struct, pushable_tasks);
> +
> +	BUG_ON(rq->cpu != task_cpu(p));
> +	BUG_ON(task_current(rq, p));
> +	BUG_ON(p->nr_cpus_allowed <= 1);
> +
> +	BUG_ON(!task_on_rq_queued(p));
> +	BUG_ON(!rt_task(p));
> +
> +	return p;
> +}
> +
>  /* Will lock the rq it finds */
>  static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>  {
> @@ -1734,13 +1754,10 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>  			 * We had to unlock the run queue. In
>  			 * the mean time, task could have
>  			 * migrated already or had its affinity changed.
> -			 * Also make sure that it wasn't scheduled on its rq.
>  			 */
> -			if (unlikely(task_rq(task) != rq ||
> -				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
> -				     task_running(rq, task) ||
> -				     !rt_task(task) ||
> -				     !task_on_rq_queued(task))) {
> +			struct task_struct *next_task = pick_next_pushable_task(rq);

I would put the above declaration before the above comment.

-- Steve

> +			if (unlikely(next_task != task ||
> +				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed))) {
>  
>  				double_unlock_balance(rq, lowest_rq);
>  				lowest_rq = NULL;
> @@ -1760,26 +1777,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
>  	return lowest_rq;
>  }
>  
> -static struct task_struct *pick_next_pushable_task(struct rq *rq)
> -{
> -	struct task_struct *p;
> -
> -	if (!has_pushable_tasks(rq))
> -		return NULL;
> -
> -	p = plist_first_entry(&rq->rt.pushable_tasks,
> -			      struct task_struct, pushable_tasks);
> -
> -	BUG_ON(rq->cpu != task_cpu(p));
> -	BUG_ON(task_current(rq, p));
> -	BUG_ON(p->nr_cpus_allowed <= 1);
> -
> -	BUG_ON(!task_on_rq_queued(p));
> -	BUG_ON(!rt_task(p));
> -
> -	return p;
> -}
> -
>  /*
>   * If the current CPU has more than one RT task, see if the non
>   * running task can migrate over to a CPU that is running a task

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2018-04-10 22:05 ` Steven Rostedt
@ 2018-04-11 10:26   ` Peter Zijlstra
  2018-04-12  8:54     ` Libin (Huawei)
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Zijlstra @ 2018-04-11 10:26 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Zhou Chengming, linux-kernel, mingo, huawei.libin

On Tue, Apr 10, 2018 at 06:05:46PM -0400, Steven Rostedt wrote:
> 
> Peter,
> 
> Going through my inbox, I stumbled across this one. And it doesn't
> appear to be addressed.
> 
> I think this patch is a reasonable solution.

Urgh, yeah, also seem to have forgotten about it. The proposed solution
is in fact simpler than the existing code. Also, I think deadline.c has
the exact same problem.

Zhou, could you respin and fix both?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq
  2018-04-11 10:26   ` Peter Zijlstra
@ 2018-04-12  8:54     ` Libin (Huawei)
  0 siblings, 0 replies; 10+ messages in thread
From: Libin (Huawei) @ 2018-04-12  8:54 UTC (permalink / raw)
  To: Peter Zijlstra, Steven Rostedt
  Cc: Zhou Chengming, linux-kernel, mingo, guohanjun


在 2018/4/11 18:26, Peter Zijlstra 写道:
> On Tue, Apr 10, 2018 at 06:05:46PM -0400, Steven Rostedt wrote:
>>
>> Peter,
>>
>> Going through my inbox, I stumbled across this one. And it doesn't
>> appear to be addressed.
>>
>> I think this patch is a reasonable solution.
> 
> Urgh, yeah, also seem to have forgotten about it. The proposed solution
> is in fact simpler than the existing code. Also, I think deadline.c has
> the exact same problem.
> 
> Zhou, could you respin and fix both?

Thanks for your reply, and I will fix the deadline.c and resend the two 
patches together.

Thanks,
Li Bin

> 
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-04-12  8:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-11  6:51 [PATCH] sched/rt.c: pick and check task if double_lock_balance() unlock the rq Zhou Chengming
2017-09-12  2:11 ` zhouchengming
2017-09-25 11:53 ` zhouchengming
2017-09-25 19:40 ` Steven Rostedt
2017-09-26  1:23   ` zhouchengming
2017-09-26  3:18     ` Steven Rostedt
2017-10-07  3:30       ` zhouchengming
2018-04-10 22:05 ` Steven Rostedt
2018-04-11 10:26   ` Peter Zijlstra
2018-04-12  8:54     ` Libin (Huawei)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).