Received: by 10.192.165.148 with SMTP id m20csp234956imm; Tue, 1 May 2018 22:17:49 -0700 (PDT) X-Google-Smtp-Source: AB8JxZomUlU1X+gfvK0qkNrjAu0TLpdivuDXHcEbHUGasVxVXe/obYaMyrdSiLEaQylx8oRruAy/ X-Received: by 10.98.139.7 with SMTP id j7mr18001858pfe.28.1525238269610; Tue, 01 May 2018 22:17:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525238269; cv=none; d=google.com; s=arc-20160816; b=c+MVbYVyT8H7URqauIe5+rjroJFsVB4HypxlAEItd1oacWeUdbx78RL7f7X4sMe3hU DQcbuVKOiMDSnL6K+92njdh1QvI1Uln5t03BIKfrp2NIMHJrgwfbAQKctXNDPMEL+NW5 pskxtEz/Cn04v7RMe+pG0NZ4MMkygtFprITazoXZ9xSrfAfs4abFggltE8A8Ab3aTLSM xHTk4yvNaSCgPcQX0fI03teqosQnWgbuhYubLb5PVFSDZ1sPjL3I8NDEwB8t78akcQ4u aqRbd0oRF9+JL5fbMX5z5Pw4m9jwJbI+Jq3kf9g8aVrAcxid859hnij2XMdZAOOp2x/0 bBpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dmarc-filter :dkim-signature:dkim-signature:arc-authentication-results; bh=ICIhB6ZJKHH1cCT5t524p+B3ybtSpy4ZFlr3+uY5rUs=; b=0IyyhcfnvijwXKNhcDgU6TrB5oR7JBP0Jzspm4VHpWFAxtAxXymB66KCp+XobHVQLB U2EKcM+imtYmAlTm98WIYwI56y8dqbzCykuOOFufsNlow3vV82iuOcWxPeVlkfmNB9s+ f9t2DmknLk9Zmd+g0Uuwb5f39nl3FtYjzaiGgbW5hqWIrG3ixn2COEvDLC4tM7h1kQDH GzcbQmzQMySfpou/0jMT4xsiWfkosIsa4yy98UoKmRZ40wRmGaW3VBqcksPTuNo397GH pPwh0TAzR+6sfCz9mSYijvWtWe2Huc+zs8Hq2MoIGX5qvDySOsWNYgCZq/WTbVm8j1GA aYQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=QRHAvuOd; dkim=pass header.i=@codeaurora.org header.s=default header.b=n0QTOs+D; 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 x25si11427246pfj.347.2018.05.01.22.17.35; Tue, 01 May 2018 22:17:49 -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=pass header.i=@codeaurora.org header.s=default header.b=QRHAvuOd; dkim=pass header.i=@codeaurora.org header.s=default header.b=n0QTOs+D; 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 S1751127AbeEBFQH (ORCPT + 99 others); Wed, 2 May 2018 01:16:07 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:55362 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751020AbeEBFQD (ORCPT ); Wed, 2 May 2018 01:16:03 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id E248260115; Wed, 2 May 2018 05:16:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1525238162; bh=y3pNgiYNI8CCWpJGpzjo5HBT1+oBudcUnvVGTREsX+g=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=QRHAvuOdTGCPrLt2i7mwf3vdS0E+mEc5Sdl+/uxw8khHECm7jfNtsK5n/qxeN9NCZ d0xookD30IdJYDT6hQsYBfiv9hl9lfDJBHJNZO4ybeHRDOWlGw+IMEo6P9WNCdDF8v lDvqjr+Z9HhNou9ejouNnwGPrdYQ33z6aHVsMiO0= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from [10.252.221.122] (blr-c-bdr-fw-01_globalnat_allzones-outside.qualcomm.com [103.229.19.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: gkohli@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 261F960585; Wed, 2 May 2018 05:15:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1525238161; bh=y3pNgiYNI8CCWpJGpzjo5HBT1+oBudcUnvVGTREsX+g=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=n0QTOs+DkVlOlOGzHAglCdy1M5TxL3CqZU7RjEOME5dcoiJwjMDoFargoKNhvay+y dAjSE0+H5sV9tp61pGHdLgj9vEmIKi2LnN5tHu+K9tU5PNKn2PQrIOXx/Wmd6CP4A1 cbzlFxpkRSomSyPW4ph+kSCjktIzyJYXOzCd0eKo= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 261F960585 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=gkohli@codeaurora.org Subject: Re: [PATCH v1] kthread/smpboot: Serialize kthread parking against wakeup To: Peter Zijlstra Cc: tglx@linutronix.de, mpe@ellerman.id.au, mingo@kernel.org, bigeasy@linutronix.de, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Neeraj Upadhyay , Will Deacon , Oleg Nesterov References: <20180425200917.GZ4082@hirez.programming.kicks-ass.net> <20180426084131.GV4129@hirez.programming.kicks-ass.net> <20180426085719.GW4129@hirez.programming.kicks-ass.net> <4d3f68f8-e599-6b27-a2e8-9e96b401d57a@codeaurora.org> <20180430111744.GE4082@hirez.programming.kicks-ass.net> <3af3365b-4e3f-e388-8e90-45a3bd4120fd@codeaurora.org> <20180501101845.GE12217@hirez.programming.kicks-ass.net> <20180501113132.GF12217@hirez.programming.kicks-ass.net> <745d762d-9ab3-0749-9b87-9bb03d913071@codeaurora.org> <20180501131904.GG12217@hirez.programming.kicks-ass.net> From: "Kohli, Gaurav" Message-ID: <9b289790-9b3a-73bd-7166-bf39f32cefd8@codeaurora.org> Date: Wed, 2 May 2018 10:45:52 +0530 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180501131904.GG12217@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/1/2018 6:49 PM, Peter Zijlstra wrote: >> On 5/1/2018 5:01 PM, Peter Zijlstra wrote: > >>> Let me ponder what the best solution is, it's a bit of a mess. > > So there's: > > - TASK_PARKED, which we can make a special state; this solves the > problem because then wait_task_inactive() is guaranteed to see > TASK_PARKED and DTRT. Yes this should work, as it takes pi-lock and blocking kthread_parkme. > > - complete(&kthread->parked), which we can do inside schedule(); this > solves the problem because then kthread_park() will not return early > and the task really is blocked. I think complete will not help, as problem is like below : Control Thread CPUHP thread cpuhp_thread_fun Wake control thread complete(&st->done); takedown_cpu kthread_park set_bit(KTHREAD_SHOULD_PARK Here cpuhp is looping, //success case Generally when issue is not coming it schedule out by below : ht->thread_should_run(td->cpu scheduler //failure case before schedule loop check (kthread_should_park() enter here as PARKED set wake_up_process(k) __kthread_parkme complete(&self->parked); SETS RUNNING schedule wait_for_completion(&kthread->parked); So even we protect complete, it may see TASK_RUNNING as final state. That's why we took pi-lock , So wake_up_process either see TASK_RUNNING so return or it will exit early before parkme call. Please correct me , if i misunderstood it. With pi-lock approach we are not seeing issue. SO you first solution should fix this(put parked in special state). Regards Gaurav > > and I'm fairly sure I thought of a 3rd way to cure things, but now that > I'm writing things down I cannot seem to remember :/ -- could be we muck > with wait_task_inactive(). > > In any case, I hate all of them, but I think the completion one is the > least horrible because it gives the strongest guarantees and cleans up > most. But promoting TASK_PARKED to special certainly is the earier > patch. > > The below boots, but that's about all I did with it. Opinions? > > --- > > --- a/include/linux/kthread.h > +++ b/include/linux/kthread.h > @@ -62,6 +62,7 @@ void *kthread_probe_data(struct task_str > int kthread_park(struct task_struct *k); > void kthread_unpark(struct task_struct *k); > void kthread_parkme(void); > +void kthread_park_complete(struct task_struct *k); > > int kthreadd(void *unused); > extern struct task_struct *kthreadd_task; > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -55,7 +55,6 @@ enum KTHREAD_BITS { > KTHREAD_IS_PER_CPU = 0, > KTHREAD_SHOULD_STOP, > KTHREAD_SHOULD_PARK, > - KTHREAD_IS_PARKED, > }; > > static inline void set_kthread_struct(void *kthread) > @@ -177,14 +176,12 @@ void *kthread_probe_data(struct task_str > > static void __kthread_parkme(struct kthread *self) > { > - __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); > + for (;;) { > + set_current_state(TASK_PARKED); > + if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags)) > + break; > schedule(); > - __set_current_state(TASK_PARKED); > } > - clear_bit(KTHREAD_IS_PARKED, &self->flags); > __set_current_state(TASK_RUNNING); > } > > @@ -194,6 +191,11 @@ void kthread_parkme(void) > } > EXPORT_SYMBOL_GPL(kthread_parkme); > > +void kthread_park_complete(struct task_struct *k) > +{ > + complete(&to_kthread(k)->parked); > +} > + > static int kthread(void *_create) > { > /* Copy data: it's on kthread's stack */ > @@ -450,22 +452,15 @@ void kthread_unpark(struct task_struct * > { > struct kthread *kthread = to_kthread(k); > > - clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags); > /* > - * We clear the IS_PARKED bit here as we don't wait > - * until the task has left the park code. So if we'd > - * park before that happens we'd see the IS_PARKED bit > - * which might be about to be cleared. > + * Newly created kthread was parked when the CPU was offline. > + * The binding was lost and we need to set it again. > */ > - if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) { > - /* > - * Newly created kthread was parked when the CPU was offline. > - * The binding was lost and we need to set it again. > - */ > - if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags)) > - __kthread_bind(k, kthread->cpu, TASK_PARKED); > - wake_up_state(k, TASK_PARKED); > - } > + if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags)) > + __kthread_bind(k, kthread->cpu, TASK_PARKED); > + > + clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags); > + wake_up_state(k, TASK_PARKED); > } > EXPORT_SYMBOL_GPL(kthread_unpark); > > @@ -488,12 +483,13 @@ int kthread_park(struct task_struct *k) > if (WARN_ON(k->flags & PF_EXITING)) > return -ENOSYS; > > - if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) { > - set_bit(KTHREAD_SHOULD_PARK, &kthread->flags); > - if (k != current) { > - wake_up_process(k); > - wait_for_completion(&kthread->parked); > - } > + if (WARN_ON_ONCE(test_bit(KTHREAD_SHOULD_PARK, &kthread->flags))) > + return -EBUSY; > + > + set_bit(KTHREAD_SHOULD_PARK, &kthread->flags); > + if (k != current) { > + wake_up_process(k); > + wait_for_completion(&kthread->parked); > } > > return 0; > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -7,6 +7,8 @@ > */ > #include "sched.h" > > +#include > + > #include > #include > > @@ -2718,20 +2720,28 @@ static struct rq *finish_task_switch(str > membarrier_mm_sync_core_before_usermode(mm); > mmdrop(mm); > } > - if (unlikely(prev_state == TASK_DEAD)) { > - if (prev->sched_class->task_dead) > - prev->sched_class->task_dead(prev); > + if (unlikely(prev_state & (TASK_DEAD|TASK_PARKED))) { > + switch (prev_state) { > + case TASK_DEAD: > + if (prev->sched_class->task_dead) > + prev->sched_class->task_dead(prev); > + > + /* > + * Remove function-return probe instances associated with this > + * task and put them back on the free list. > + */ > + kprobe_flush_task(prev); > > - /* > - * Remove function-return probe instances associated with this > - * task and put them back on the free list. > - */ > - kprobe_flush_task(prev); > + /* Task is done with its stack. */ > + put_task_stack(prev); > > - /* Task is done with its stack. */ > - put_task_stack(prev); > + put_task_struct(prev); > + break; > > - put_task_struct(prev); > + case TASK_PARKED: > + kthread_park_complete(prev); > + break; > + } > } > > tick_nohz_task_switch(); > -- Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.