Received: by 10.192.165.148 with SMTP id m20csp4975228imm; Tue, 24 Apr 2018 11:29:09 -0700 (PDT) X-Google-Smtp-Source: AIpwx4952KJcQn+J6GafQ68XQuW3ZbXe/GaNNsmyewQfz4fDzovtlgtjnNq6opBDZd9iL1U0tOFR X-Received: by 10.101.98.22 with SMTP id d22mr21524870pgv.344.1524594549269; Tue, 24 Apr 2018 11:29:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524594549; cv=none; d=google.com; s=arc-20160816; b=Or7CK8Hq25tyeN/oX5OHnxj0hgwrefvSlOiLBmYmU+RXsgnc1eStKu/g9rRmr6LanD +EMKk2mZXQciO1Q1Cd22LYlUo4KkJnTKZdVnHaIKWqA0SCqF520CE/JYx+gHc6AT73sE /M+DCrxm/RvQWhvZmloGcca8nT16Wc+FQssGkwe4czpH6ARSjBRmWTCyyeXji6S4QNCs jaxTWFt5t6uQfJNan0SGEzku0D49a2247k/IItST/ol6fZiLNYYIxAAAFO8Ee7DiKeyu o9NwL7lJilvMWw6HhZlFgZwP6zo5dY9iYCdVdaRqEOKekFi+O2dYNB/pwznbVLVnjRlf kTTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=XA7Oa7qPdGiImcxxnmzbMKKJPZj8lnQXEozntwciGdo=; b=NQIuZMCrfI32qalBckvVmONvtYo4KCTaNQIgHOkg2exeNA+dDMCuxlRsAVlKbbiDuE t5/Ygq2hHkoTY4Qn/GxearXAVDxI1FzDfZJuTjIfS9E+Gfsl0phZC4SJdbARiBSmomXj Vum0aYhs2FLLdDZUGYoCLDj4EJG5MpeYWm4+JcqnpP5QNguUPXEahToHlh3LuyA8Df6o r1ilDOA8adGmu+r70oeyYNKTihfG8rqsgbbor1zTVjnPHi5WTLvoDWysfyTl3J2mWC2f rmzqMRkXuU9pHxmeY2X0GU5MVdvy0zWTfaBaVUS+5yU4ELpyu7MOlRhwBbmdwC4iMqSO jMcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=yGnfYpih; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p12-v6si14891164pls.578.2018.04.24.11.28.55; Tue, 24 Apr 2018 11:29:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=yGnfYpih; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753027AbeDXS0l (ORCPT + 99 others); Tue, 24 Apr 2018 14:26:41 -0400 Received: from merlin.infradead.org ([205.233.59.134]:46866 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752937AbeDXS0g (ORCPT ); Tue, 24 Apr 2018 14:26:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=XA7Oa7qPdGiImcxxnmzbMKKJPZj8lnQXEozntwciGdo=; b=yGnfYpihUg2hxk4KTiLeQRsNZ AKFM7yQX3srNTzvUleZwvjkIJilagEe/8gcQRbGp0OuadI8YAlUhQDxEnLE+Sc/Fvz1ZWPUj6F6sp IMUQGK6K9nnUmXjKL/kX9BJzBuw3TRl93sSIuc2XLra7U+aPVl4EC8RfeJhKENzVQCnK/wUj2H94e iPHluVUZ8t89LP6NuB5PZZhcltHH7JTBO3WYuaLGP6li+tP6DMAgDlKlGJIdy1OFxnuDQ09VJs7Uz ycKhZUlUId3V4XQOqgj88Tpmg/vY2EZTPhDOUxqN46G7SzUXYM2hs9LuLFRGZdz0kwGKU/DT/qmHZ 4Gql472Tw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fB2e6-0006zn-8a; Tue, 24 Apr 2018 18:26:30 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id C89F3203BFAEF; Tue, 24 Apr 2018 20:26:28 +0200 (CEST) Date: Tue, 24 Apr 2018 20:26:28 +0200 From: Peter Zijlstra To: Gaurav Kohli Cc: tglx@linutronix.de, mpe@ellerman.id.au, dzickus@redhat.com, mingo@kernel.org, bigeasy@linutronix.de, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Neeraj Upadhyay Subject: Re: [PATCH] kthread/smpboot: Serialize kthread parking against wakeup Message-ID: <20180424182628.GW4043@hirez.programming.kicks-ass.net> References: <1524562105-31026-1-git-send-email-gkohli@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1524562105-31026-1-git-send-email-gkohli@codeaurora.org> User-Agent: Mutt/1.9.3 (2018-01-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 24, 2018 at 02:58:25PM +0530, Gaurav Kohli wrote: > The control cpu thread which initiates hotplug calls kthread_park() > for hotplug thread and sets KTHREAD_SHOULD_PARK. After this control > thread wakes up the hotplug thread. There is a chance that wakeup > code sees the hotplug thread (running on AP core) in INTERRUPTIBLE > state, but sets its state to RUNNING after hotplug thread has entered > kthread_parkme() and changed its state to TASK_PARKED. This can result > in panic later on in kthread_unpark(), as it sees KTHREAD_IS_PARKED > flag set but fails to rebind the kthread, due to it being not in > TASK_PARKED state. Fix this, by serializing wakeup state change, > against state change before parking the kthread. > > Below is the possible race: > > Control thread Hotplug Thread > > kthread_park() > set KTHREAD_SHOULD_PARK > smpboot_thread_fn > set_current_state(TASK_INTERRUPTIBLE); > kthread_parkme > > wake_up_process() > > raw_spin_lock_irqsave(&p->pi_lock, flags); > if (!(p->state & state)) -> this will fail > goto out; > > __kthread_parkme > __set_current_state(TASK_PARKED); > > if (p->on_rq && ttwu_remote(p, wake_flags)) > ttwu_remote() > p->state = TASK_RUNNING; > schedule(); > > So to avoid this race, take pi_lock to serial state changes. > > Suggested-by: Pavankumar Kondeti > Co-developed-by: Neeraj Upadhyay > Signed-off-by: Neeraj Upadhyay > Signed-off-by: Gaurav Kohli > > diff --git a/kernel/smpboot.c b/kernel/smpboot.c > index 1650578..514b232 100644 > --- a/kernel/smpboot.c > +++ b/kernel/smpboot.c > @@ -121,7 +121,9 @@ static int smpboot_thread_fn(void *data) > } > > if (kthread_should_park()) { > + raw_spin_lock(¤t->pi_lock); > __set_current_state(TASK_RUNNING); > + raw_spin_unlock(¤t->pi_lock); > preempt_enable(); > if (ht->park && td->status == HP_THREAD_ACTIVE) { > BUG_ON(td->cpu != smp_processor_id()); Note how in your scenario above you didn't actually need the TASK_RUNNING state; so how is this change going to fix anything? But yes, I suspect it is right, but it definitely needs a comment explaining wth we take that lock there. Like I said earlier, my brain is entirely fried for the day; but I'll have a try tomorrow.