linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.ibm.com>
To: Jann Horn <jannh@google.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	kernel list <linux-kernel@vger.kernel.org>,
	Linux API <linux-api@vger.kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrea Parri <parri.andrea@gmail.com>,
	Andy Lutomirski <luto@kernel.org>, Avi Kivity <avi@scylladb.com>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Dave Watson <davejwatson@fb.com>, David Sehr <sehr@google.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Maged Michael <maged.michael@gmail.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Paul Mackerras <paulus@samba.org>,
	Russell King <linux@armlinux.org.uk>, Will Deacon <will>
Subject: Re: [PATCH] Fix: membarrier: racy access to p->mm in membarrier_global_expedited()
Date: Mon, 28 Jan 2019 15:22:36 -0800	[thread overview]
Message-ID: <20190128232236.GG4240@linux.ibm.com> (raw)
In-Reply-To: <CAG48ez1+Vx7=ZqeLJ0waceSneCQhEcoS=bv9ggb3Dbveb+W6AQ@mail.gmail.com>

On Mon, Jan 28, 2019 at 11:45:32PM +0100, Jann Horn wrote:
> On Mon, Jan 28, 2019 at 11:39 PM Paul E. McKenney <paulmck@linux.ibm.com> wrote:
> > On Mon, Jan 28, 2019 at 05:07:07PM -0500, Mathieu Desnoyers wrote:
> > > Jann Horn identified a racy access to p->mm in the global expedited
> > > command of the membarrier system call.
> > >
> > > The suggested fix is to hold the task_lock() around the accesses to
> > > p->mm and to the mm_struct membarrier_state field to guarantee the
> > > existence of the mm_struct.
> > >
> > > Link: https://lore.kernel.org/lkml/CAG48ez2G8ctF8dHS42TF37pThfr3y0RNOOYTmxvACm4u8Yu3cw@mail.gmail.com
> > > Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> [...]
> > > --- a/kernel/sched/membarrier.c
> > > +++ b/kernel/sched/membarrier.c
> > > @@ -81,12 +81,27 @@ static int membarrier_global_expedited(void)
> > >
> > >               rcu_read_lock();
> > >               p = task_rcu_dereference(&cpu_rq(cpu)->curr);
> > > -             if (p && p->mm && (atomic_read(&p->mm->membarrier_state) &
> > > -                                MEMBARRIER_STATE_GLOBAL_EXPEDITED)) {
> > > -                     if (!fallback)
> > > -                             __cpumask_set_cpu(cpu, tmpmask);
> > > -                     else
> > > -                             smp_call_function_single(cpu, ipi_mb, NULL, 1);
> > > +             /*
> > > +              * Skip this CPU if the runqueue's current task is NULL or if
> > > +              * it is a kernel thread.
> > > +              */
> > > +             if (p && READ_ONCE(p->mm)) {
> > > +                     bool mm_match;
> > > +
> > > +                     /*
> > > +                      * Read p->mm and access membarrier_state while holding
> > > +                      * the task lock to ensure existence of mm.
> > > +                      */
> > > +                     task_lock(p);
> > > +                     mm_match = p->mm && (atomic_read(&p->mm->membarrier_state) &
> >
> > Are we guaranteed that this p->mm will be the same as the one loaded via
> > READ_ONCE() above?
> 
> No; the way I read it, that's just an optimization and has no effect
> on correctness.
> 
> > Either way, wouldn't it be better to READ_ONCE() it a
> > single time and use the same value everywhere?
> 
> No; the first READ_ONCE() returns a pointer that you can't access
> because it wasn't read under a lock. You can only use it for a NULL
> check.

Ah, of course!  Thank you both!

							Thanx, Paul

  reply	other threads:[~2019-01-28 23:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-28 22:07 [PATCH] Fix: membarrier: racy access to p->mm in membarrier_global_expedited() Mathieu Desnoyers
2019-01-28 22:39 ` Paul E. McKenney
2019-01-28 22:45   ` Jann Horn
2019-01-28 23:22     ` Paul E. McKenney [this message]
2019-01-28 22:46   ` Mathieu Desnoyers

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=20190128232236.GG4240@linux.ibm.com \
    --to=paulmck@linux.ibm.com \
    --cc=avi@scylladb.com \
    --cc=benh@kernel.crashing.org \
    --cc=boqun.feng@gmail.com \
    --cc=davejwatson@fb.com \
    --cc=hpa@zytor.com \
    --cc=jannh@google.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=luto@kernel.org \
    --cc=maged.michael@gmail.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=parri.andrea@gmail.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=sehr@google.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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).