From: "Dmitry Adamushko" <dmitry.adamushko@gmail.com>
To: "Andy Whitcroft" <apw@shadowen.org>
Cc: linux-kernel@vger.kernel.org, "Ingo Molnar" <mingo@elte.hu>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Peter Zijlstra" <a.p.zijlstra@chello.nl>,
"Rusty Russel" <rusty@rustcorp.com.au>
Subject: Re: [PATCH, RFC] kthread: (possibly) a missing memory barrier in kthread_stop()
Date: Tue, 19 Feb 2008 14:11:30 +0100 [thread overview]
Message-ID: <b647ffbd0802190511u44fc8353wa4cbb290bcf371ae@mail.gmail.com> (raw)
In-Reply-To: <20080219130033.GK24479@shadowen.org>
On 19/02/2008, Andy Whitcroft <apw@shadowen.org> wrote:
> [ ... ]
>
> > diff --git a/kernel/kthread.c b/kernel/kthread.c
> > index 0ac8878..5167110 100644
> > --- a/kernel/kthread.c
> > +++ b/kernel/kthread.c
> > @@ -211,6 +211,10 @@ int kthread_stop(struct task_struct *k)
> >
> > /* Now set kthread_should_stop() to true, and wake it up. */
> > kthread_stop_info.k = k;
> > +
> > + /* The previous store operation must not get ahead of the wakeup. */
> > + smp_mb();
> > +
> > wake_up_process(k);
> > put_task_struct(k);
>
> The rules as written do seem to support your theory. The CPU has every
> right to delay the .k = k as late as the UNLOCK operation.
>
> On the read-side there is a full barrier implied by the
> set_current_state(TASK_INTERRUPTIBLE), however this synchronises us with
> the current global state, which may well not have the updated version
> of .k.
yes.
>
> That seems to imply that a write memory barrier would be sufficient to
> cover this.
>
> So three comments. First, should this not be an smp_wmb.
No. We also need to be sure that ".k = k" is updated by the moment we
check for a state of the task in try_to_wake_up(), so that's write vs.
read ops.
The point is that a 'kthread' loop does :
(1) set TASK_INTERRUPTIBLE
(2) check for .k == k
and kthread_stop() must do it in the _reverse_ order:
(1) .k = k
(2) check for a task state and wakeup if necessary.
Only this way we ensure that a wakeup is not lost.
> Second, this
> memory barrier is paired with the barrier in
> set_current_state(TASK_INTERRUPTIBLE) and that probabally should be
> documented as part of this patch. Finally, I think the comment as is is
> hard to understand I got the sense of it backwards on first reading;
> perhaps something like this:
>
> /*
> * Ensure kthread_stop_info.k is visible before wakeup, paired
> * with barrier in set_current_state().
> */
Yes, I'll try to come up with a better description.
>
> -apw
>
--
Best regards,
Dmitry Adamushko
prev parent reply other threads:[~2008-02-19 13:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-18 23:03 [PATCH, RFC] kthread: (possibly) a missing memory barrier in kthread_stop() Dmitry Adamushko
2008-02-19 6:44 ` Nick Piggin
2008-02-19 9:24 ` Peter Zijlstra
2008-02-19 9:53 ` Dmitry Adamushko
2008-02-19 13:41 ` Dmitry Adamushko
2008-02-19 22:52 ` Dmitry Adamushko
2008-02-19 13:00 ` Andy Whitcroft
2008-02-19 13:11 ` Dmitry Adamushko [this message]
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=b647ffbd0802190511u44fc8353wa4cbb290bcf371ae@mail.gmail.com \
--to=dmitry.adamushko@gmail.com \
--cc=a.p.zijlstra@chello.nl \
--cc=akpm@linux-foundation.org \
--cc=apw@shadowen.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=rusty@rustcorp.com.au \
/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