mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Junaid Shahid <junaids@google.com>
Cc: linux-kernel@vger.kernel.org, andreslc@google.com,
	gthelen@google.com, rusty@rustcorp.com.au,
	Thomas Gleixner <tglx@linutronix.de>, Tejun Heo <tj@kernel.org>
Subject: Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark()
Date: Tue, 30 May 2017 15:40:02 -0700	[thread overview]
Message-ID: <20170530154002.010800f3d2f74777ab9801f2@linux-foundation.org> (raw)
In-Reply-To: <4713763.QsSBOgQyOh@js-desktop.svl.corp.google.com>

(cc tglx & tj)

On Tue, 30 May 2017 15:37:23 -0700 Junaid Shahid <junaids@google.com> wrote:

> (Resending)
> 
> On Friday, April 28, 2017 07:32:36 PM Junaid Shahid wrote:
> > In general, if kthread_unpark() and kthread_parkme() execute together,
> > the kthread is supposed to be in an unparked state. This is because
> > kthread_unpark() either wakes up the thread if it already got parked,
> > or it cancels a prior kthread_park() call and hence renders the
> > kthread_parkme() moot.
> > 
> > However, if kthread_unpark() happens to execute between the time that
> > kthread_parkme() checks the KTHREAD_SHOULD_STOP flag and sets the
> > KTHREAD_IS_PARKED flag, then kthread_unpark() will not wake up the
> > thread and it will remain in a parked state.
> > 
> > This is fixed by making the checking of KTHREAD_SHOULD_STOP and the
> > setting of KTHREAD_IS_PARKED atomic via a cmpxchg inside kthread_parkme.
> > 
> > Signed-off-by: Junaid Shahid <junaids@google.com>
> > ---
> >  kernel/kthread.c | 16 ++++++++++++----
> >  1 file changed, 12 insertions(+), 4 deletions(-)
> > 
> > diff --git a/kernel/kthread.c b/kernel/kthread.c
> > index 26db528c1d88..651f03baf62f 100644
> > --- a/kernel/kthread.c
> > +++ b/kernel/kthread.c
> > @@ -169,12 +169,20 @@ void *kthread_probe_data(struct task_struct *task)
> >  
> >  static void __kthread_parkme(struct kthread *self)
> >  {
> > +	ulong flags;
> > +
> >  	__set_current_state(TASK_PARKED);
> > -	while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
> > -		if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
> > -			complete(&self->parked);
> > -		schedule();
> > +	flags = self->flags;
> > +
> > +	while (test_bit(KTHREAD_SHOULD_PARK, &flags)) {
> > +		if (cmpxchg(&self->flags, flags,
> > +			    flags | (1 << KTHREAD_IS_PARKED)) == flags) {
> > +			if (!test_bit(KTHREAD_IS_PARKED, &flags))
> > +				complete(&self->parked);
> > +			schedule();
> > +		}
> >  		__set_current_state(TASK_PARKED);
> > +		flags = self->flags;
> >  	}
> >  	clear_bit(KTHREAD_IS_PARKED, &self->flags);
> >  	__set_current_state(TASK_RUNNING);
> > 

  reply	other threads:[~2017-05-30 22:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-29  2:32 Junaid Shahid
2017-05-30 22:37 ` Junaid Shahid
2017-05-30 22:40   ` Andrew Morton [this message]
2017-07-18  0:13     ` Junaid Shahid
2017-07-18  6:35       ` Thomas Gleixner
2017-09-27 12:17 ` Peter Zijlstra
     [not found]   ` <CAL-GctE0BLVk=ps09dnir8pcpnfzg88jMK4CKkRVmY7-ho87nQ@mail.gmail.com>
2017-09-29  7:59     ` Thomas Gleixner
2017-09-29  8:28       ` Peter Zijlstra
2017-09-29 20:58         ` Junaid Shahid

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=20170530154002.010800f3d2f74777ab9801f2@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=andreslc@google.com \
    --cc=gthelen@google.com \
    --cc=junaids@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.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

Powered by JetHome