Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751620AbdGRANb (ORCPT ); Mon, 17 Jul 2017 20:13:31 -0400 Received: from mail-pf0-f170.google.com ([209.85.192.170]:35057 "EHLO mail-pf0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751335AbdGRANa (ORCPT ); Mon, 17 Jul 2017 20:13:30 -0400 From: Junaid Shahid To: Andrew Morton , Thomas Gleixner , Tejun Heo Cc: linux-kernel@vger.kernel.org, andreslc@google.com, gthelen@google.com, rusty@rustcorp.com.au Subject: Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark() Date: Mon, 17 Jul 2017 17:13:27 -0700 Message-ID: <25013855.TJYia4lJXz@js-desktop.svl.corp.google.com> Organization: Google User-Agent: KMail/4.13.3 (Linux/4.4.0-78-generic; KDE/4.13.3; x86_64; ; ) In-Reply-To: <20170530154002.010800f3d2f74777ab9801f2@linux-foundation.org> References: <20170429023236.60452-1-junaids@google.com> <4713763.QsSBOgQyOh@js-desktop.svl.corp.google.com> <20170530154002.010800f3d2f74777ab9801f2@linux-foundation.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2326 Lines: 64 Hi, Has anyone been able to take a look at this? Thanks, Junaid On Tuesday, May 30, 2017 03:40:02 PM Andrew Morton wrote: > (cc tglx & tj) > > On Tue, 30 May 2017 15:37:23 -0700 Junaid Shahid 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 > > > --- > > > 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); > > >