From patchwork Thu Nov 3 12:07:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Qiao X-Patchwork-Id: 14834 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp485421wru; Thu, 3 Nov 2022 05:06:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4WwjioxQZQTAzUORk0PoKwLJUs08Eu6MaK+iUgRYv5m72Zx4X7WlLo/XhPM/35eiCVV+zN X-Received: by 2002:a17:90b:3446:b0:213:4990:fa2 with SMTP id lj6-20020a17090b344600b0021349900fa2mr49470323pjb.73.1667477187699; Thu, 03 Nov 2022 05:06:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667477187; cv=none; d=google.com; s=arc-20160816; b=ej6KVE1PtW0Rnk2VmFgkDkZYYBZkjqi+QNeJhlor/1P+dzm59W/teBFlpfCDpHqYis dJU+QmW9Qgk/APg8PNBLIytIM2CR3yY3T9oYz6zgr+tiFycOYt1a5z6Zof4D21xcMHHf ThRxJsclOagjyLvm/kHHycZDMXiqdm0gc6FfjsKvjNivqcSquG6T7+AAf1p6h7dYaarw kFeDzeTi1Y26u16UEDbl50ToNJyiJmG1IlupSzIg6lam69dx4iQ4NxCnVx425t3gz0C8 xI0al5G6s8PnzMMUiobVgU/Zw0JPxr04bin6DdqiGMxMKCk68Nrx8KgIaLJUf2gkuEVe YUlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=N1b4P6zRfCpiYzokWBUJ/Dz2w0jA/v8u+AravTtPlB0=; b=zDVGq3fJsekG+NxfWTDCVsJkMDT1JFKYZEv97nh6P3LGrip82tR8z2MJftYqtk9C5x sF40/AI0jJHoLzByjLSMMdkrGjLShmy95i9GCedyvz+kXZdYJx02RhzjRyvmxo737z0S GoYTunM/Q/rxNUfMqx5XX1LoPmlQNNZNbEZZLNWKVsDnSM3Ctpgc95I0QMS/dgKZHb+e wM5Bak1Jdpf8zApalZ13BTnJxisKEkPXCQYHG8VtI4/4222m2vn6F7H6D0v0BfwK2Dyd DBBp0rwr6jZEyGGnXkqacUgq4NI39HGBikBzHKkJGFFX19MPS07ToYbQTvEb7M6pF82s YdNA== ARC-Authentication-Results: i=1; mx.google.com; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020a170902760300b0017d6ffda9e3si504879pll.369.2022.11.03.05.06.00; Thu, 03 Nov 2022 05:06:27 -0700 (PDT) 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; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230203AbiKCLim (ORCPT + 99 others); Thu, 3 Nov 2022 07:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231381AbiKCLiZ (ORCPT ); Thu, 3 Nov 2022 07:38:25 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74EAA11C3B for ; Thu, 3 Nov 2022 04:38:24 -0700 (PDT) Received: from dggpeml500020.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4N31sC4BtVzJnL5; Thu, 3 Nov 2022 19:35:27 +0800 (CST) Received: from dggpeml500018.china.huawei.com (7.185.36.186) by dggpeml500020.china.huawei.com (7.185.36.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 3 Nov 2022 19:38:22 +0800 Received: from huawei.com (10.67.174.191) by dggpeml500018.china.huawei.com (7.185.36.186) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 3 Nov 2022 19:38:22 +0800 From: Zhang Qiao To: , , , , CC: , , , , , , , , Zhang Qiao Subject: [PATCH v2 1/2] sched: Init new task's vruntime after select cpu Date: Thu, 3 Nov 2022 20:07:19 +0800 Message-ID: <20221103120720.39873-2-zhangqiao22@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221103120720.39873-1-zhangqiao22@huawei.com> References: <20221103120720.39873-1-zhangqiao22@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.191] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500018.china.huawei.com (7.185.36.186) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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?1748476559804664434?= X-GMAIL-MSGID: =?utf-8?q?1748476559804664434?= When create a new task, we initialize vruntime of the new task at sched_cgroup_fork(). However, this action is executed too early and may be incorrect, because it use current cpu to init the vruntime, but the new task actually runs on the cpu assigned at wake_up_new_task(). So the patch call task_fork() after select fork cpu and use the ready cpu(the child will run on it) init the new task. Signed-off-by: Zhang Qiao --- v1->v2: make sched_task_fork static. kernel/sched/core.c | 7 ++++++- kernel/sched/fair.c | 7 +------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index e4ce124ec701..21481bd22bdf 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4627,9 +4627,13 @@ void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs) * so use __set_task_cpu(). */ __set_task_cpu(p, smp_processor_id()); + raw_spin_unlock_irqrestore(&p->pi_lock, flags); +} + +static void sched_task_fork(struct task_struct *p) +{ if (p->sched_class->task_fork) p->sched_class->task_fork(p); - raw_spin_unlock_irqrestore(&p->pi_lock, flags); } void sched_post_fork(struct task_struct *p) @@ -4682,6 +4686,7 @@ void wake_up_new_task(struct task_struct *p) #endif rq = __task_rq_lock(p, &rf); update_rq_clock(rq); + sched_task_fork(p); post_init_entity_util_avg(p); activate_task(rq, p, ENQUEUE_NOCLOCK); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index e4a0b8bd941c..34845d425180 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -11603,12 +11603,8 @@ static void task_fork_fair(struct task_struct *p) struct cfs_rq *cfs_rq; struct sched_entity *se = &p->se, *curr; struct rq *rq = this_rq(); - struct rq_flags rf; - rq_lock(rq, &rf); - update_rq_clock(rq); - - cfs_rq = task_cfs_rq(current); + cfs_rq = task_cfs_rq(p); curr = cfs_rq->curr; if (curr) { update_curr(cfs_rq); @@ -11626,7 +11622,6 @@ static void task_fork_fair(struct task_struct *p) } se->vruntime -= cfs_rq->min_vruntime; - rq_unlock(rq, &rf); } /* From patchwork Thu Nov 3 12:07:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Qiao X-Patchwork-Id: 14831 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp481134wru; Thu, 3 Nov 2022 04:59:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5BHYgmpFOB+2cMYz6iGzxErCZrEUyqhQLaFtjFpo15Oj2+UOcjWYHaI5luh5WfkKJz76+B X-Received: by 2002:a17:90b:4a42:b0:213:383f:cd21 with SMTP id lb2-20020a17090b4a4200b00213383fcd21mr47454583pjb.23.1667476782423; Thu, 03 Nov 2022 04:59:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667476782; cv=none; d=google.com; s=arc-20160816; b=IMWUUk8sIgw8a1GFgXDAD+mLfvbwX1xttms1o6DBbdOZ4gYEHfOJv5zXVa0FNwol84 Z17B71HS4txUUGsv2l0GGc97CcgwO1tIZoQSwqWe+eBQISlLSOdup0wgxdrxcWq76+ez gmpaqvO6Icsj8ZV6BmvLq326jrgNEA+sxLLZDBjSoZneZ+YOADEs26iMK2AJ9An4uhVR 8WEtiIyeauUdSOSnuhznVpAaAlpU9bzMf3uStmGBS5kCCWevO4nxWunnjDN2DTGppV3e U3z/RKmZPi4avZEEenAs1htkd51mL157+KF9uA3jgpapCcquUH08GgCHPg1MZGZ9bvbP iXVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=lSPZpYby2b6UH/F6EZ/aIVQmyJFUJUfRu2BvlNOzVsc=; b=UAaqjVSzvn7mbVjpPAbh8riQL2ZoQMdPSwNzHbNXp1QgKvyzeSOwWKnlvJMK2ydqds tuaK7DS2+/rYPOqIelLBU1lcBAJ81RCX2L60RLiNnYgKBdpxJc1e3aenIleydjDbXmoR AUUE/GFIYYVJcWvOWGLnX5P7Ajtu0pOrjKDYkqvZ1GldGygeF6Qevq8ATdIU3naIjUWg iVoQAOvPPJ/VhUlcm8Xtl7nm7nqyG7EgWtRVBvhh5E0eRaGjivMj2AS0SRkqD55ffI3f QBtVmM1NVNb6Gqan4Jx1gt1gUIqVgeE0DOIiUNOuBrF7g/V8jm0J7WSb0nApuo7JAX5/ /Eew== ARC-Authentication-Results: i=1; mx.google.com; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g18-20020a056a001a1200b005627d896458si861530pfv.142.2022.11.03.04.59.27; Thu, 03 Nov 2022 04:59:42 -0700 (PDT) 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; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230175AbiKCLiu (ORCPT + 99 others); Thu, 3 Nov 2022 07:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231462AbiKCLi1 (ORCPT ); Thu, 3 Nov 2022 07:38:27 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2713B1209D for ; Thu, 3 Nov 2022 04:38:25 -0700 (PDT) Received: from dggpeml500022.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4N31wV6FFmz15MKj; Thu, 3 Nov 2022 19:38:18 +0800 (CST) Received: from dggpeml500018.china.huawei.com (7.185.36.186) by dggpeml500022.china.huawei.com (7.185.36.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 3 Nov 2022 19:38:23 +0800 Received: from huawei.com (10.67.174.191) by dggpeml500018.china.huawei.com (7.185.36.186) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 3 Nov 2022 19:38:23 +0800 From: Zhang Qiao To: , , , , CC: , , , , , , , , Zhang Qiao Subject: [PATCH v2 2/2] sched: Fix sched_child_runs_first Date: Thu, 3 Nov 2022 20:07:20 +0800 Message-ID: <20221103120720.39873-3-zhangqiao22@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221103120720.39873-1-zhangqiao22@huawei.com> References: <20221103120720.39873-1-zhangqiao22@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.191] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500018.china.huawei.com (7.185.36.186) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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?1748476134285615777?= X-GMAIL-MSGID: =?utf-8?q?1748476134285615777?= There are two cases that the sched_child_runs_first maybe not work fine: 1) when call clone3() with CLONE_INTO_CGROUP flags, will creating the child task into a cgroup different from the parent's cgroup, so that child and parent's cfs_rq is diffent. 2) Assign a different cpu to the new task when fork balancing. Above two case, the child and the parent will attach to different cpu and cfs_rq. At this time, we can't swap the child and parent's vruntime, and i think only do swap vruntime when the parent and child in the same cfs_rq. This patch will add the cfs_rq check before swap vruntime. Signed-off-by: Zhang Qiao --- kernel/sched/fair.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 34845d425180..6061ceb1b7cb 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -11612,7 +11612,9 @@ static void task_fork_fair(struct task_struct *p) } place_entity(cfs_rq, se, 1); - if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { + if (sysctl_sched_child_runs_first && + cfs_rq == task_cfs_rq(current) && + curr && entity_before(curr, se)) { /* * Upon rescheduling, sched_class::put_prev_task() will place * 'current' within the tree based on its new key value.