From: Andrea Parri <andrea.parri@amarulasolutions.com>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
"Paul E. McKenney" <paulmck@linux.ibm.com>,
Alan Stern <stern@rowland.harvard.edu>,
Will Deacon <will.deacon@arm.com>
Subject: Re: [PATCH] sched: Use READ_ONCE()/WRITE_ONCE() in task_cpu()/__set_task_cpu()
Date: Mon, 21 Jan 2019 11:51:21 +0100 [thread overview]
Message-ID: <20190121104707.GA5883@andrea> (raw)
In-Reply-To: <20190116184218.13757-1-andrea.parri@amarulasolutions.com>
On Wed, Jan 16, 2019 at 07:42:18PM +0100, Andrea Parri wrote:
> The smp_wmb() in move_queued_task() (c.f., __set_task_cpu()) pairs with
> the composition of the dependency and the ACQUIRE in task_rq_lock():
>
> 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 honour
> this address dependency between loads; also, mark the store to ->cpu in
> __set_task_cpu() by using WRITE_ONCE() in order to tell the compiler to
> not mess/tear this (synchronizing) memory access.
In the light of the recent discussion about the integration of plain
accesses in the LKMM (c.f., e.g., [1] and discussion thereof), I was
considering even further changes to this in order to "reinforce" the
above smp_wmb(). Here's two approaches (one of):
1) replace this smp_wmb()+WRITE_ONCE() with an smp_store_release();
2) or keep this smp_wmb()+WRITE_ONCE(), but use {WRITE,READ}_ONCE()
also for the accesses to ->on_rq.
What do you think? (maybe I'm just being too paranoid?)
Adding Will to the Cc: ((1) should be "painless" for x86, not sure
about arm64...)
Andrea
[1] http://lkml.kernel.org/r/20190118155638.GA24442@andrea
>
> 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>
> ---
> include/linux/sched.h | 4 ++--
> kernel/sched/sched.h | 4 ++--
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 224666226e87b..2bb02c9635bd8 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1753,9 +1753,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/sched.h b/kernel/sched/sched.h
> index d04530bf251fe..270a3333589d2 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
> --
> 2.17.1
>
next prev parent reply other threads:[~2019-01-21 10:51 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-16 18:42 [PATCH] sched: Use READ_ONCE()/WRITE_ONCE() in task_cpu()/__set_task_cpu() Andrea Parri
2019-01-21 10:51 ` Andrea Parri [this message]
2019-01-21 12:25 ` Peter Zijlstra
2019-01-21 15:34 ` Andrea Parri
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=20190121104707.GA5883@andrea \
--to=andrea.parri@amarulasolutions.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=paulmck@linux.ibm.com \
--cc=peterz@infradead.org \
--cc=stern@rowland.harvard.edu \
--cc=will.deacon@arm.com \
/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;
as well as URLs for NNTP newsgroup(s).