All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhang Qiao <zhangqiao22@huawei.com>
To: <mingo@redhat.com>, <peterz@infradead.org>,
	<juri.lelli@redhat.com>, <vincent.guittot@linaro.org>
Cc: <zhangqiao22@huawei.com>, <dietmar.eggemann@arm.com>,
	<rostedt@goodmis.org>, <bsegall@google.com>, <mgorman@suse.de>,
	<bristot@redhat.com>, <vschneid@redhat.com>, <brauner@kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH next 1/2] sched: Init new task's vruntime after select cpu
Date: Mon, 31 Oct 2022 20:51:12 +0800	[thread overview]
Message-ID: <20221031125113.72980-2-zhangqiao22@huawei.com> (raw)
In-Reply-To: <20221031125113.72980-1-zhangqiao22@huawei.com>

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 <zhangqiao22@huawei.com>
---
 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..ca5677206efd 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);
+}
+
+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);
 }
 
 /*
-- 
2.17.1


  reply	other threads:[~2022-10-31 12:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-31 12:51 [PATCH next 0/2] sched: sched_fork() optimizations Zhang Qiao
2022-10-31 12:51 ` Zhang Qiao [this message]
2022-10-31 14:23   ` [PATCH next 1/2] sched: Init new task's vruntime after select cpu kernel test robot
2022-11-01 10:46   ` kernel test robot
2022-10-31 12:51 ` [PATCH next 2/2] sched: Fix sched_child_runs_first Zhang Qiao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221031125113.72980-2-zhangqiao22@huawei.com \
    --to=zhangqiao22@huawei.com \
    --cc=brauner@kernel.org \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.