linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()
@ 2019-01-21 15:52 Andrea Parri
  2019-01-31 18:25 ` Andrea Parri
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andrea Parri @ 2019-01-21 15:52 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrea Parri, Ingo Molnar, Peter Zijlstra, Paul E. McKenney,
	Alan Stern, Will Deacon

move_queued_task() synchronizes with task_rq_lock() as follows:

	move_queued_task()		task_rq_lock()

	[S] ->on_rq = MIGRATING		[L] rq = task_rq()
	WMB (__set_task_cpu())		ACQUIRE (rq->lock);
	[S] ->cpu = new_cpu		[L] ->on_rq

where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an
address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before
"[L] ->on_rq" by the ACQUIRE itself.

Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honor
this address dependency.  Also, mark the accesses to ->cpu and ->on_rq
with READ_ONCE()/WRITE_ONCE() to comply with the LKMM.

Signed-off-by: Andrea Parri <andrea.parri@amarulasolutions.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Will Deacon <will.deacon@arm.com>
---
 Changes in v2:
 - mark accesses to ->on_rq as well
 - update inline comment for task_rq_lock()
 - minor editing in the subject/changelog
 
 include/linux/sched.h | 4 ++--
 kernel/sched/core.c   | 9 +++++----
 kernel/sched/sched.h  | 6 +++---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index d2f90fa924683..41212d725a0eb 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1754,9 +1754,9 @@ static __always_inline bool need_resched(void)
 static inline unsigned int task_cpu(const struct task_struct *p)
 {
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-	return p->cpu;
+	return READ_ONCE(p->cpu);
 #else
-	return task_thread_info(p)->cpu;
+	return READ_ONCE(task_thread_info(p)->cpu);
 #endif
 }
 
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index a674c7db2f29d..d6e08faaa2843 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -107,11 +107,12 @@ struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
 		 *					[L] ->on_rq
 		 *	RELEASE (rq->lock)
 		 *
-		 * If we observe the old CPU in task_rq_lock, the acquire of
+		 * If we observe the old CPU in task_rq_lock(), the acquire of
 		 * the old rq->lock will fully serialize against the stores.
 		 *
-		 * If we observe the new CPU in task_rq_lock, the acquire will
-		 * pair with the WMB to ensure we must then also see migrating.
+		 * If we observe the new CPU in task_rq_lock(), the address
+		 * dependency headed by '[L] rq = task_rq()' and the acquire
+		 * will pair with the WMB to ensure we then also see migrating.
 		 */
 		if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
 			rq_pin_lock(rq, rf);
@@ -915,7 +916,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
 {
 	lockdep_assert_held(&rq->lock);
 
-	p->on_rq = TASK_ON_RQ_MIGRATING;
+	WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
 	dequeue_task(rq, p, DEQUEUE_NOCLOCK);
 	set_task_cpu(p, new_cpu);
 	rq_unlock(rq, rf);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index d04530bf251fe..425a5589e5f60 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1460,9 +1460,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
 	 */
 	smp_wmb();
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-	p->cpu = cpu;
+	WRITE_ONCE(p->cpu, cpu);
 #else
-	task_thread_info(p)->cpu = cpu;
+	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
 #endif
 	p->wake_cpu = cpu;
 #endif
@@ -1563,7 +1563,7 @@ static inline int task_on_rq_queued(struct task_struct *p)
 
 static inline int task_on_rq_migrating(struct task_struct *p)
 {
-	return p->on_rq == TASK_ON_RQ_MIGRATING;
+	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
 }
 
 /*
-- 
2.17.1


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

* Re: [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()
  2019-01-21 15:52 [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Andrea Parri
@ 2019-01-31 18:25 ` Andrea Parri
  2019-02-01  8:26 ` Peter Zijlstra
  2019-02-04  9:02 ` [tip:sched/core] sched/core: " tip-bot for Andrea Parri
  2 siblings, 0 replies; 4+ messages in thread
From: Andrea Parri @ 2019-01-31 18:25 UTC (permalink / raw)
  To: linux-kernel
  Cc: Ingo Molnar, Peter Zijlstra, Paul E. McKenney, Alan Stern, Will Deacon

On Mon, Jan 21, 2019 at 04:52:40PM +0100, Andrea Parri wrote:
> move_queued_task() synchronizes with task_rq_lock() as follows:
> 
> 	move_queued_task()		task_rq_lock()
> 
> 	[S] ->on_rq = MIGRATING		[L] rq = task_rq()
> 	WMB (__set_task_cpu())		ACQUIRE (rq->lock);
> 	[S] ->cpu = new_cpu		[L] ->on_rq
> 
> where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an
> address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before
> "[L] ->on_rq" by the ACQUIRE itself.
> 
> Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honor
> this address dependency.  Also, mark the accesses to ->cpu and ->on_rq
> with READ_ONCE()/WRITE_ONCE() to comply with the LKMM.
> 
> Signed-off-by: Andrea Parri <andrea.parri@amarulasolutions.com>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: "Paul E. McKenney" <paulmck@linux.ibm.com>
> Cc: Alan Stern <stern@rowland.harvard.edu>
> Cc: Will Deacon <will.deacon@arm.com>

ping

  Andrea


> ---
>  Changes in v2:
>  - mark accesses to ->on_rq as well
>  - update inline comment for task_rq_lock()
>  - minor editing in the subject/changelog
>  
>  include/linux/sched.h | 4 ++--
>  kernel/sched/core.c   | 9 +++++----
>  kernel/sched/sched.h  | 6 +++---
>  3 files changed, 10 insertions(+), 9 deletions(-)
> 
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index d2f90fa924683..41212d725a0eb 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1754,9 +1754,9 @@ static __always_inline bool need_resched(void)
>  static inline unsigned int task_cpu(const struct task_struct *p)
>  {
>  #ifdef CONFIG_THREAD_INFO_IN_TASK
> -	return p->cpu;
> +	return READ_ONCE(p->cpu);
>  #else
> -	return task_thread_info(p)->cpu;
> +	return READ_ONCE(task_thread_info(p)->cpu);
>  #endif
>  }
>  
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index a674c7db2f29d..d6e08faaa2843 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -107,11 +107,12 @@ struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
>  		 *					[L] ->on_rq
>  		 *	RELEASE (rq->lock)
>  		 *
> -		 * If we observe the old CPU in task_rq_lock, the acquire of
> +		 * If we observe the old CPU in task_rq_lock(), the acquire of
>  		 * the old rq->lock will fully serialize against the stores.
>  		 *
> -		 * If we observe the new CPU in task_rq_lock, the acquire will
> -		 * pair with the WMB to ensure we must then also see migrating.
> +		 * If we observe the new CPU in task_rq_lock(), the address
> +		 * dependency headed by '[L] rq = task_rq()' and the acquire
> +		 * will pair with the WMB to ensure we then also see migrating.
>  		 */
>  		if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
>  			rq_pin_lock(rq, rf);
> @@ -915,7 +916,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
>  {
>  	lockdep_assert_held(&rq->lock);
>  
> -	p->on_rq = TASK_ON_RQ_MIGRATING;
> +	WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
>  	dequeue_task(rq, p, DEQUEUE_NOCLOCK);
>  	set_task_cpu(p, new_cpu);
>  	rq_unlock(rq, rf);
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index d04530bf251fe..425a5589e5f60 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -1460,9 +1460,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
>  	 */
>  	smp_wmb();
>  #ifdef CONFIG_THREAD_INFO_IN_TASK
> -	p->cpu = cpu;
> +	WRITE_ONCE(p->cpu, cpu);
>  #else
> -	task_thread_info(p)->cpu = cpu;
> +	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
>  #endif
>  	p->wake_cpu = cpu;
>  #endif
> @@ -1563,7 +1563,7 @@ static inline int task_on_rq_queued(struct task_struct *p)
>  
>  static inline int task_on_rq_migrating(struct task_struct *p)
>  {
> -	return p->on_rq == TASK_ON_RQ_MIGRATING;
> +	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
>  }
>  
>  /*
> -- 
> 2.17.1
> 

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

* Re: [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()
  2019-01-21 15:52 [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Andrea Parri
  2019-01-31 18:25 ` Andrea Parri
@ 2019-02-01  8:26 ` Peter Zijlstra
  2019-02-04  9:02 ` [tip:sched/core] sched/core: " tip-bot for Andrea Parri
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Zijlstra @ 2019-02-01  8:26 UTC (permalink / raw)
  To: Andrea Parri
  Cc: linux-kernel, Ingo Molnar, Paul E. McKenney, Alan Stern, Will Deacon

On Mon, Jan 21, 2019 at 04:52:40PM +0100, Andrea Parri wrote:
> move_queued_task() synchronizes with task_rq_lock() as follows:
> 
> 	move_queued_task()		task_rq_lock()
> 
> 	[S] ->on_rq = MIGRATING		[L] rq = task_rq()
> 	WMB (__set_task_cpu())		ACQUIRE (rq->lock);
> 	[S] ->cpu = new_cpu		[L] ->on_rq
> 
> where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an
> address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before
> "[L] ->on_rq" by the ACQUIRE itself.
> 
> Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honor
> this address dependency.  Also, mark the accesses to ->cpu and ->on_rq
> with READ_ONCE()/WRITE_ONCE() to comply with the LKMM.
> 
> Signed-off-by: Andrea Parri <andrea.parri@amarulasolutions.com>

Thanks!

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

* [tip:sched/core] sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()
  2019-01-21 15:52 [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Andrea Parri
  2019-01-31 18:25 ` Andrea Parri
  2019-02-01  8:26 ` Peter Zijlstra
@ 2019-02-04  9:02 ` tip-bot for Andrea Parri
  2 siblings, 0 replies; 4+ messages in thread
From: tip-bot for Andrea Parri @ 2019-02-04  9:02 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: paulmck, mingo, torvalds, tglx, efault, linux-kernel, stern,
	andrea.parri, hpa, will.deacon, peterz

Commit-ID:  c546951d9c9300065bad253ecdf1ac59ce9d06c8
Gitweb:     https://git.kernel.org/tip/c546951d9c9300065bad253ecdf1ac59ce9d06c8
Author:     Andrea Parri <andrea.parri@amarulasolutions.com>
AuthorDate: Mon, 21 Jan 2019 16:52:40 +0100
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Mon, 4 Feb 2019 09:13:21 +0100

sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()

move_queued_task() synchronizes with task_rq_lock() as follows:

	move_queued_task()		task_rq_lock()

	[S] ->on_rq = MIGRATING		[L] rq = task_rq()
	WMB (__set_task_cpu())		ACQUIRE (rq->lock);
	[S] ->cpu = new_cpu		[L] ->on_rq

where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an
address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before
"[L] ->on_rq" by the ACQUIRE itself.

Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honor
this address dependency.  Also, mark the accesses to ->cpu and ->on_rq
with READ_ONCE()/WRITE_ONCE() to comply with the LKMM.

Signed-off-by: Andrea Parri <andrea.parri@amarulasolutions.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul E. McKenney <paulmck@linux.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Link: https://lkml.kernel.org/r/20190121155240.27173-1-andrea.parri@amarulasolutions.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 include/linux/sched.h | 4 ++--
 kernel/sched/core.c   | 9 +++++----
 kernel/sched/sched.h  | 6 +++---
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 351c0fe64c85..4112639c2a85 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1745,9 +1745,9 @@ static __always_inline bool need_resched(void)
 static inline unsigned int task_cpu(const struct task_struct *p)
 {
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-	return p->cpu;
+	return READ_ONCE(p->cpu);
 #else
-	return task_thread_info(p)->cpu;
+	return READ_ONCE(task_thread_info(p)->cpu);
 #endif
 }
 
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 32e06704565e..ec1b67a195cc 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -107,11 +107,12 @@ struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
 		 *					[L] ->on_rq
 		 *	RELEASE (rq->lock)
 		 *
-		 * If we observe the old CPU in task_rq_lock, the acquire of
+		 * If we observe the old CPU in task_rq_lock(), the acquire of
 		 * the old rq->lock will fully serialize against the stores.
 		 *
-		 * If we observe the new CPU in task_rq_lock, the acquire will
-		 * pair with the WMB to ensure we must then also see migrating.
+		 * If we observe the new CPU in task_rq_lock(), the address
+		 * dependency headed by '[L] rq = task_rq()' and the acquire
+		 * will pair with the WMB to ensure we then also see migrating.
 		 */
 		if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
 			rq_pin_lock(rq, rf);
@@ -916,7 +917,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
 {
 	lockdep_assert_held(&rq->lock);
 
-	p->on_rq = TASK_ON_RQ_MIGRATING;
+	WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
 	dequeue_task(rq, p, DEQUEUE_NOCLOCK);
 	set_task_cpu(p, new_cpu);
 	rq_unlock(rq, rf);
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 99e2a7772d16..c688ef5012e5 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1479,9 +1479,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
 	 */
 	smp_wmb();
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-	p->cpu = cpu;
+	WRITE_ONCE(p->cpu, cpu);
 #else
-	task_thread_info(p)->cpu = cpu;
+	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
 #endif
 	p->wake_cpu = cpu;
 #endif
@@ -1582,7 +1582,7 @@ static inline int task_on_rq_queued(struct task_struct *p)
 
 static inline int task_on_rq_migrating(struct task_struct *p)
 {
-	return p->on_rq == TASK_ON_RQ_MIGRATING;
+	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
 }
 
 /*

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

end of thread, other threads:[~2019-02-04  9:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-21 15:52 [PATCH v2] sched: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Andrea Parri
2019-01-31 18:25 ` Andrea Parri
2019-02-01  8:26 ` Peter Zijlstra
2019-02-04  9:02 ` [tip:sched/core] sched/core: " tip-bot for Andrea Parri

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