From patchwork Sat Nov 12 03:39:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 19100 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1096726wru; Fri, 11 Nov 2022 19:51:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf4qiFmC2ZSHDUlbq4qeaPhS6PikzELsfo3yjhXyNfNRF2gGlLDi5aA0wUuk3fM/y8U+GGcq X-Received: by 2002:a17:902:8506:b0:174:a0e6:428 with SMTP id bj6-20020a170902850600b00174a0e60428mr5354185plb.124.1668225111475; Fri, 11 Nov 2022 19:51:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668225111; cv=none; d=google.com; s=arc-20160816; b=dP2inZaA3Vfx9KZ8boLz9HI2JXn2MqXAX3p+pC0pMi3WQkyZbLWXg6/0su0zSIJQc1 cm7ThQMbfa1FDdn54mAwsUz9KHw7txmdx44/SNm4DWAB8jFAaKQ2f4I/cL5K9I8ssrE4 mdU2J1SQBJrc48/YY18FLnEuVnLOIq/4aHgNyixQtmsSEdLn0yUDla93Vd5WXwqCzE7K pK40V+sxxqWsx5BclZNJjCQ9OfXFaCuHOEPzQa+rQSbjWrsMMrj3eVYhPBl4cdddC4bR soaJhbJ+yWMT0HO9vBbbrQ9aaSl/eNV2INJyrHoD+PNVh/e5MGGk+IUoS1RXKSVyLCcL znhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=+G1gOD42Iwq7Wo/rZcbyltI5GH3a8Ol1vlBS/8A/k3c=; b=l54SN7koG3P1HI7caYiW3Or4hukpg/p0xqziE8ixkzJ8nMiGlePnkGb/Mri8hQON5n RvuQygMV+4rD94clNYuYRB7DkISUGNNsiwC50PNhCaQLPlYsggAhyvDkGeqBMnoXyLWm ZGUIg52qCF05VFFnAJpS5z5RBkGyoFquSzUgBasuByO0BGxdqtR73ROQBiJ0IfZF0Pnx GLEqlOgUkZcWENcW70TL1Nf9EfBl+SJaLEt0ppc0OGyUpMWs3s3eb8xf9Pn9PyRtfoRK TTVmqYFrow+d09ZatqC618oCq8qGOUK3NCgjyoSxMQwDBkXwSVDcR5BGa23Sxkiz899K jGDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=lc8M2b57; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk13-20020a056a02028d00b0046fb03051d1si10468pgb.296.2022.11.11.19.51.25; Fri, 11 Nov 2022 19:51:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=lc8M2b57; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234586AbiKLDjk (ORCPT + 99 others); Fri, 11 Nov 2022 22:39:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234116AbiKLDjj (ORCPT ); Fri, 11 Nov 2022 22:39:39 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC1B4A450 for ; Fri, 11 Nov 2022 19:39:37 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id d59-20020a17090a6f4100b00213202d77e1so9292301pjk.2 for ; Fri, 11 Nov 2022 19:39:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+G1gOD42Iwq7Wo/rZcbyltI5GH3a8Ol1vlBS/8A/k3c=; b=lc8M2b57NtP4Q+pFVAKCiTFzTQ2++1cC/WD1eszQ43wEi/SLldivdDwA4q7GxRel8R ubSap2Zvvybmlh5OETHvy1LEoqPt2B8afq076OAzjXyWzud+R1kXBZT9dV19IpAt3TnV i3xw4taUzdn9k41iYIZ1FJdatnWJPDmscw77eq5RSMd1mMpxu7ItA8mBCifrG0hll7yc DAETsGjIMqNnLwK8jbUKV6TawEkGKwwV93FtwvM+jCPLaInRFx+sCpw9zZObjNMRwct+ sn4MpwFkbEJ1QCSLER3+LwPIoIff/oE7s13pInktRW47Hz74wPZXjT52AP/zIfmqr5ir ebWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+G1gOD42Iwq7Wo/rZcbyltI5GH3a8Ol1vlBS/8A/k3c=; b=0nk9HzLu5zLK5Mtz2GB5+gJhAzIK6MZjPLbPHjvV0FokZjBNObT2X5KIUbjTUPJYwu tl/m0n8eS/BAjrER7e7eqqfh2A0TcdsYiQfmjoxIoDmJrTmua5wPxqIpBtqA0zNSgfct gU5oh/RlYUekMQdxHFm27Q1MwWJWJEuqK9FnJxM50dxO1cgQCXSa/fuIeEcTWgSumRyv Uiu0/MwSUK9JlVyCY8MEARu0XIqwcz+1IkOC4PpXMoJlrefpxN6fbBO2+kbj5COQPSKs X18kIEE5Z6HXWfKhPOPJMSKEWuOD7p55Dr2J42uiFBIHuZ44a2PWiWoq1zNNmIaH/usP dRNA== X-Gm-Message-State: ANoB5pnqkQBo10R8C5XjFATwCvFHQOoUjXr2x38yWcv9xDliZBXlOzuB FHLnDhwKK0ibmfPinoszrPKqKw== X-Received: by 2002:a17:90a:9114:b0:212:d636:1543 with SMTP id k20-20020a17090a911400b00212d6361543mr5011793pjo.33.1668224377195; Fri, 11 Nov 2022 19:39:37 -0800 (PST) Received: from localhost.localdomain ([2409:8a28:e66:62a0:11d9:cb41:d3a1:45da]) by smtp.gmail.com with ESMTPSA id j5-20020a170903024500b001867fb4056asm2522371plh.32.2022.11.11.19.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 19:39:36 -0800 (PST) From: Chengming Zhou To: mingo@redhat.com, peterz@infradead.org, vschneid@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de Cc: linux-kernel@vger.kernel.org, Chengming Zhou Subject: [PATCH v3] sched/core: Minor optimize ttwu_runnable() Date: Sat, 12 Nov 2022 11:39:10 +0800 Message-Id: <20221112033910.73273-1-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749260814520772460?= X-GMAIL-MSGID: =?utf-8?q?1749260814520772460?= ttwu_runnable() is used as a fast wakeup path when the wakee task is running on CPU or runnable on RQ, in both cases we can just set its state to TASK_RUNNING to prevent a sleep. If the wakee task is on_cpu running, we don't need to update_rq_clock() or check_preempt_curr(). But if the wakee task is on_rq && !on_cpu (e.g. an IRQ hit before the task got to schedule() and the task been preempted), we should check_preempt_curr() to see if it can preempt the current running. This patch reorg ttwu_do_wakeup() and ttwu_do_activate() to make ttwu_do_wakeup() only mark the task runnable, so it can be used in ttwu_runnable() and try_to_wake_up() fast paths. This also removes the class->task_woken() callback from ttwu_runnable(), which wasn't required per the RT/DL implementations: any required push operation would have been queued during class->set_next_task() when p got preempted. ttwu_runnable() also loses the update to rq->idle_stamp, as by definition the rq cannot be idle in this scenario. Signed-off-by: Chengming Zhou Suggested-by: Valentin Schneider Suggested-by: Peter Zijlstra Reviewed-by: Valentin Schneider --- v3: - Improve the changelog per Valentin Schneider. Thanks! v2: - keep check_preempt_curr() for on_rq && !on_cpu case in ttwu_runnable(), per Valentin Schneider. - reorg ttwu_do_wakeup() and ttwu_do_activate() code, so ttwu_do_wakeup() can be reused in ttwu_runnable(), per Peter Zijlstra. - reuse ttwu_do_wakeup() in try_to_wake_up() (p == current) fast path too, so ttwu_do_wakeup() become the only place we mark task runnable. --- kernel/sched/core.c | 73 ++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 15e167a1c5bd..78b5075191b4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3623,14 +3623,39 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags) } /* - * Mark the task runnable and perform wakeup-preemption. + * Mark the task runnable. */ -static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) +static inline void ttwu_do_wakeup(struct task_struct *p) { - check_preempt_curr(rq, p, wake_flags); WRITE_ONCE(p->__state, TASK_RUNNING); trace_sched_wakeup(p); +} + +static void +ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, + struct rq_flags *rf) +{ + int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; + + lockdep_assert_rq_held(rq); + + if (p->sched_contributes_to_load) + rq->nr_uninterruptible--; + +#ifdef CONFIG_SMP + if (wake_flags & WF_MIGRATED) + en_flags |= ENQUEUE_MIGRATED; + else +#endif + if (p->in_iowait) { + delayacct_blkio_end(p); + atomic_dec(&task_rq(p)->nr_iowait); + } + + activate_task(rq, p, en_flags); + check_preempt_curr(rq, p, wake_flags); + + ttwu_do_wakeup(p); #ifdef CONFIG_SMP if (p->sched_class->task_woken) { @@ -3660,31 +3685,6 @@ static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, #endif } -static void -ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, - struct rq_flags *rf) -{ - int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; - - lockdep_assert_rq_held(rq); - - if (p->sched_contributes_to_load) - rq->nr_uninterruptible--; - -#ifdef CONFIG_SMP - if (wake_flags & WF_MIGRATED) - en_flags |= ENQUEUE_MIGRATED; - else -#endif - if (p->in_iowait) { - delayacct_blkio_end(p); - atomic_dec(&task_rq(p)->nr_iowait); - } - - activate_task(rq, p, en_flags); - ttwu_do_wakeup(rq, p, wake_flags, rf); -} - /* * Consider @p being inside a wait loop: * @@ -3718,9 +3718,15 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags) rq = __task_rq_lock(p, &rf); if (task_on_rq_queued(p)) { - /* check_preempt_curr() may use rq clock */ - update_rq_clock(rq); - ttwu_do_wakeup(rq, p, wake_flags, &rf); + if (!task_on_cpu(rq, p)) { + /* + * When on_rq && !on_cpu the task is preempted, see if + * it should preempt whatever is current there now. + */ + update_rq_clock(rq); + check_preempt_curr(rq, p, wake_flags); + } + ttwu_do_wakeup(p); ret = 1; } __task_rq_unlock(rq, &rf); @@ -4082,8 +4088,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) goto out; trace_sched_waking(p); - WRITE_ONCE(p->__state, TASK_RUNNING); - trace_sched_wakeup(p); + ttwu_do_wakeup(p); goto out; }