Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp7039804ybf; Fri, 6 Mar 2020 09:12:09 -0800 (PST) X-Google-Smtp-Source: ADFU+vs/M4y43+7Eu8SptA9CkGge9I+x+CxzNNLZjpOWShjIKJg8494PRDjzat16Av/U17xPjcLh X-Received: by 2002:a05:6808:a9c:: with SMTP id q28mr3443344oij.34.1583514729008; Fri, 06 Mar 2020 09:12:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583514729; cv=none; d=google.com; s=arc-20160816; b=ROX9XVgFX2Txg2bCUvifl+Iig1F39WYQWfGhqnFvJUlGdk7EBhq6gXOGQT1MGo0G7U QgACeRD6bdzW3IuLGevR/8+NZALMuAYyqF7f8tkY4wECnUpo+1/6BFrHOoR+e7fDW64w 9KfGS4ksoqqUWjPZtroHWki8edj37c4lDsLWi/Jn5pPYgyu1eoIe5xWRLZnbDMsbHLpp Ljp3A98aQYTquS3Ocnbhpy0FAm9fntKoC6IAsJd00kJdUCYGLfaMqUQvlI3/nIFWdWUV T5gi6os/nU553KOPwVawiAtk2Cpx5ISukyrlNTtwDJuTueXUnabRTrPosLjszY+bnW2t ow9g== 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:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=9qmGi6po+iTuI18gntjQDLm1MnUoIWCwXH50Fcp8fv0=; b=TIoyY/v77V00dIVX5+yw6tbRyYFs9monWksmUUqDXIe352CrUOXOaIH+PrzgafifjT umc5tjXhCjXhe/BYge5+QXGJR0pGA+/B96KnIWsddWc77KFXpUGzG6h580GUG8s1LhZv 9fvxMJKKWkzgU828mdYqI2WT5VkVciYAMODAqGmG69ub7sZP4OffvBDoI93b3b8N4akn rAFXvaihHpWcQftJ8U5CWr9se+3XchHkwFMWNfcfSETdL7yMc/YsHuHBMffhWX0G8KF9 FWouTsS6bJIne3KklkUmC2Ef1QxP9ccdhMqLcOlNx/cHa7g1xApXQvMDbcOq4dn4/RTF kL4A== ARC-Authentication-Results: i=1; mx.google.com; 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 a14si8779oib.31.2020.03.06.09.11.52; Fri, 06 Mar 2020 09:12:08 -0800 (PST) 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; 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 S1726676AbgCFRJv (ORCPT + 99 others); Fri, 6 Mar 2020 12:09:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:58096 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726171AbgCFRJu (ORCPT ); Fri, 6 Mar 2020 12:09:50 -0500 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BCF7020658; Fri, 6 Mar 2020 17:09:46 +0000 (UTC) Date: Fri, 6 Mar 2020 12:09:45 -0500 From: Steven Rostedt To: Cc: heiko@sntech.de, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, bsegall@google.com, mgorman@suse.de, akpm@linux-foundation.org, tglx@linutronix.de, mpe@ellerman.id.au, surenb@google.com, ben.dooks@codethink.co.uk, anshuman.khandual@arm.com, catalin.marinas@arm.com, will@kernel.org, keescook@chromium.org, luto@amacapital.net, wad@chromium.org, mark.rutland@arm.com, geert+renesas@glider.be, george_davis@mentor.com, sudeep.holla@arm.com, linux@armlinux.org.uk, gregkh@linuxfoundation.org, info@metux.net, kstewart@linuxfoundation.org, allison@lohutok.net, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, huangtao@rock-chips.com Subject: Re: [PATCH v3 1/1] kthread: do not preempt current task if it is going to call schedule() Message-ID: <20200306120945.6a197172@gandalf.local.home> In-Reply-To: <20200306070133.18335-2-cl@rock-chips.com> References: <20200306070133.18335-1-cl@rock-chips.com> <20200306070133.18335-2-cl@rock-chips.com> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 6 Mar 2020 15:01:33 +0800 wrote: > From: Liang Chen > > when we create a kthread with ktrhead_create_on_cpu(),the child thread > entry is ktread.c:ktrhead() which will be preempted by the parent after > call complete(done) while schedule() is not called yet,then the parent > will call wait_task_inactive(child) but the child is still on the runqueue, > so the parent will schedule_hrtimeout() for 1 jiffy,it will waste a lot of > time,especially on startup. > > parent child > ktrhead_create_on_cpu() > wait_fo_completion(&done) -----> ktread.c:ktrhead() > |----- complete(done);--wakeup and preempted by parent > kthread_bind() <------------| |-> schedule();--dequeue here > wait_task_inactive(child) | > schedule_hrtimeout(1 jiffy) -| > > So we hope the child just wakeup parent but not preempted by parent, and the > child is going to call schedule() soon,then the parent will not call > schedule_hrtimeout(1 jiffy) as the child is already dequeue. > > The same issue for ktrhead_park()&&kthread_parkme(). > This patch can save 120ms on rk312x startup with CONFIG_HZ=300. > > Signed-off-by: Liang Chen > --- > kernel/kthread.c | 17 +++++++++++++++-- > 1 file changed, 15 insertions(+), 2 deletions(-) > > diff --git a/kernel/kthread.c b/kernel/kthread.c > index b262f47046ca..bfbfa481be3a 100644 > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -199,8 +199,15 @@ static void __kthread_parkme(struct kthread *self) > if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags)) > break; > > + /* > + * Thread is going to call schedule(), do not preempt it, > + * or the caller of kthread_park() may spend more time in > + * wait_task_inactive(). > + */ > + preempt_disable(); > complete(&self->parked); I first was concerned that this could break PREEMPT_RT, as complete() calls spin_locks() which are turned into sleeping locks when PREEMPT_RT is enabled. But looking at the latest PREEMPT_RT patch, it appears that it converts the locks in complete() into raw_spin_locks (and using swake). I don't see any other issue with this patch. Reviewed-by: Steven Rostedt (VMware) -- Steve > - schedule(); > + schedule_preempt_disabled(); > + preempt_enable(); > } > __set_current_state(TASK_RUNNING); > } > @@ -245,8 +252,14 @@ static int kthread(void *_create) > /* OK, tell user we're spawned, wait for stop or wakeup */ > __set_current_state(TASK_UNINTERRUPTIBLE); > create->result = current; > + /* > + * Thread is going to call schedule(), do not preempt it, > + * or the creator may spend more time in wait_task_inactive(). > + */ > + preempt_disable(); > complete(done); > - schedule(); > + schedule_preempt_disabled(); > + preempt_enable(); > > ret = -EINTR; > if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {