Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp655492ybb; Fri, 20 Mar 2020 05:59:15 -0700 (PDT) X-Google-Smtp-Source: ADFU+vu0e6MR62Ujp1lq2zbkCnbVN8abHuZW+xQoEk1UmDj/fWt2AawhkxBQgIGC2LSqmtzv4M37 X-Received: by 2002:aca:ec16:: with SMTP id k22mr6363305oih.177.1584709155449; Fri, 20 Mar 2020 05:59:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584709155; cv=none; d=google.com; s=arc-20160816; b=TBaCvSex9iQ6jFUgeLc1lg+gAVu0xuy7OHDPK18FNzCw/oOEzyvtCsr7SoBsZVNJTu f5Kif0nH1xCVrf9cMBoWTz+MevnUx8vOv7eT41N2W8MRkv7rFu17BmX2NyUCy/B6N3rC 7gukI095Rgy3WItuQv6bvrzrna5I3ljDjoJu2xT31uXmkaWb7g+jlUEvMH0Ol4kKYObg 3AWKo7HS+BgVvbHdEqyDaO3RT/GPoHoncQh9OewvW5yREfSZg3mg08R1n6o3smBAVDI7 kZ7A7P8ELg2w96n/7NeAvDsYVJI9WBlhBpvAM6vUf5hxc11oVGigy7Q4Qa4vNhCKKia0 PD0Q== 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 :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=tlvgYu8P3V2SJyllLeR4bkLIGHWpghgAleHdnjFqzT0=; b=KIBLj+IK6KvG158ANMfGdPHCdHxxlZPqrAB7SwgXdqtmpq0NS4/PclGnQpEsfViCgN r+Ns1pvM+HEMsvaxPcKI6qik0oVTOQUn9PAwyAO3o8/4R7SngYl/ANdV29CWn9mVzlAy UqtjObgr1RUCZTm0Z1ewxjsAm6Sa90wOe3CAvAONk92ccCtvKN7l2Lnze2LH5D7TPUwx oRzrBGGDvZLAjgpujftq8s5UUXzvkHvXMWTkxyF6UP57j7xhaDhMBSkJ3JXDHkZE5tuz q0pZR9yspWuUbRK77F9r26jP6YrYVCdUGktAA+Au7udw3PmAQhPAQq0qOSTr3wHIKYJh xO4A== 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 r21si907470oth.185.2020.03.20.05.59.02; Fri, 20 Mar 2020 05:59:15 -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; 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 S1727411AbgCTM62 (ORCPT + 99 others); Fri, 20 Mar 2020 08:58:28 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:35660 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727365AbgCTM6Y (ORCPT ); Fri, 20 Mar 2020 08:58:24 -0400 Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jFHDw-0003ia-5J; Fri, 20 Mar 2020 13:58:04 +0100 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id C054A1C22BE; Fri, 20 Mar 2020 13:58:03 +0100 (CET) Date: Fri, 20 Mar 2020 12:58:03 -0000 From: "tip-bot2 for Liang Chen" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] kthread: Do not preempt current task if it is going to call schedule() Cc: Liang Chen , "Peter Zijlstra (Intel)" , "Steven Rostedt (VMware)" , x86 , LKML In-Reply-To: <20200306070133.18335-2-cl@rock-chips.com> References: <20200306070133.18335-2-cl@rock-chips.com> MIME-Version: 1.0 Message-ID: <158470908349.28353.3855924848715964020.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: 26c7295be0c5e6da3fa45970e9748be983175b1b Gitweb: https://git.kernel.org/tip/26c7295be0c5e6da3fa45970e9748be983175b1b Author: Liang Chen AuthorDate: Fri, 06 Mar 2020 15:01:33 +08:00 Committer: Peter Zijlstra CommitterDate: Fri, 20 Mar 2020 13:06:20 +01:00 kthread: Do not preempt current task if it is going to call schedule() 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 Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Steven Rostedt (VMware) Link: https://lkml.kernel.org/r/20200306070133.18335-2-cl@rock-chips.com --- kernel/kthread.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index b262f47..bfbfa48 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); - 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)) {