mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yajun Deng <yajun.deng@linux.dev>
To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	bristot@redhat.com, vschneid@redhat.com
Cc: linux-kernel@vger.kernel.org, Yajun Deng <yajun.deng@linux.dev>
Subject: [PATCH v2 1/2] sched/fair: Return NULL when entity isn't a task in task_of()
Date: Wed,  6 Dec 2023 14:33:59 +0800	[thread overview]
Message-ID: <20231206063400.3206346-2-yajun.deng@linux.dev> (raw)
In-Reply-To: <20231206063400.3206346-1-yajun.deng@linux.dev>

Before calling task_of(), we need to make sure that the entity is a task.
There is also a warning in task_of() if the entity isn't a task. That
means we need to check the entity twice. If the entity isn't a task,
return the task struct is meaningless.

Return NULL when entity isn't a task in task_of(), and call task_of()
instead of entity_is_task() when we need a task_struct.

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 kernel/sched/fair.c  | 50 +++++++++++++++++++++-----------------------
 kernel/sched/sched.h |  4 +++-
 2 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index bcd0f230e21f..5003c8501398 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -470,8 +470,10 @@ static int cfs_rq_is_idle(struct cfs_rq *cfs_rq)
 
 static int se_is_idle(struct sched_entity *se)
 {
-	if (entity_is_task(se))
-		return task_has_idle_policy(task_of(se));
+	struct task_struct *p = task_of(se);
+
+	if (p)
+		return task_has_idle_policy(p);
 	return cfs_rq_is_idle(group_cfs_rq(se));
 }
 
@@ -1156,6 +1158,7 @@ s64 update_curr_common(struct rq *rq)
 static void update_curr(struct cfs_rq *cfs_rq)
 {
 	struct sched_entity *curr = cfs_rq->curr;
+	struct task_struct *p;
 	s64 delta_exec;
 
 	if (unlikely(!curr))
@@ -1169,8 +1172,9 @@ static void update_curr(struct cfs_rq *cfs_rq)
 	update_deadline(cfs_rq, curr);
 	update_min_vruntime(cfs_rq);
 
-	if (entity_is_task(curr))
-		update_curr_task(task_of(curr), delta_exec);
+	p = task_of(curr);
+	if (p)
+		update_curr_task(p, delta_exec);
 
 	account_cfs_rq_runtime(cfs_rq, delta_exec);
 }
@@ -1184,24 +1188,19 @@ static inline void
 update_stats_wait_start_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
 	struct sched_statistics *stats;
-	struct task_struct *p = NULL;
 
 	if (!schedstat_enabled())
 		return;
 
 	stats = __schedstats_from_se(se);
 
-	if (entity_is_task(se))
-		p = task_of(se);
-
-	__update_stats_wait_start(rq_of(cfs_rq), p, stats);
+	__update_stats_wait_start(rq_of(cfs_rq), task_of(se), stats);
 }
 
 static inline void
 update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
 	struct sched_statistics *stats;
-	struct task_struct *p = NULL;
 
 	if (!schedstat_enabled())
 		return;
@@ -1217,27 +1216,20 @@ update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	if (unlikely(!schedstat_val(stats->wait_start)))
 		return;
 
-	if (entity_is_task(se))
-		p = task_of(se);
-
-	__update_stats_wait_end(rq_of(cfs_rq), p, stats);
+	__update_stats_wait_end(rq_of(cfs_rq), task_of(se), stats);
 }
 
 static inline void
 update_stats_enqueue_sleeper_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
 	struct sched_statistics *stats;
-	struct task_struct *tsk = NULL;
 
 	if (!schedstat_enabled())
 		return;
 
 	stats = __schedstats_from_se(se);
 
-	if (entity_is_task(se))
-		tsk = task_of(se);
-
-	__update_stats_enqueue_sleeper(rq_of(cfs_rq), tsk, stats);
+	__update_stats_enqueue_sleeper(rq_of(cfs_rq), task_of(se), stats);
 }
 
 /*
@@ -1263,6 +1255,7 @@ update_stats_enqueue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int fl
 static inline void
 update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 {
+	struct task_struct *tsk;
 
 	if (!schedstat_enabled())
 		return;
@@ -1274,8 +1267,8 @@ update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int fl
 	if (se != cfs_rq->curr)
 		update_stats_wait_end_fair(cfs_rq, se);
 
-	if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
-		struct task_struct *tsk = task_of(se);
+	tsk = task_of(se);
+	if ((flags & DEQUEUE_SLEEP) && tsk) {
 		unsigned int state;
 
 		/* XXX racy against TTWU */
@@ -3569,12 +3562,14 @@ static inline void update_scan_period(struct task_struct *p, int new_cpu)
 static void
 account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
+	struct task_struct *p = task_of(se);
+
 	update_load_add(&cfs_rq->load, se->load.weight);
 #ifdef CONFIG_SMP
-	if (entity_is_task(se)) {
+	if (p) {
 		struct rq *rq = rq_of(cfs_rq);
 
-		account_numa_enqueue(rq, task_of(se));
+		account_numa_enqueue(rq, p);
 		list_add(&se->group_node, &rq->cfs_tasks);
 	}
 #endif
@@ -3586,10 +3581,12 @@ account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
 static void
 account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
+	struct task_struct *p = task_of(se);
+
 	update_load_sub(&cfs_rq->load, se->load.weight);
 #ifdef CONFIG_SMP
-	if (entity_is_task(se)) {
-		account_numa_dequeue(rq_of(cfs_rq), task_of(se));
+	if (p) {
+		account_numa_dequeue(rq_of(cfs_rq), p);
 		list_del_init(&se->group_node);
 	}
 #endif
@@ -5323,9 +5320,10 @@ static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
 static void
 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 {
+	struct task_struct *p = task_of(se);
 	int action = UPDATE_TG;
 
-	if (entity_is_task(se) && task_on_rq_migrating(task_of(se)))
+	if (p && task_on_rq_migrating(p))
 		action |= DO_DETACH;
 
 	/*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 001fe047bd5d..6baba9ebcde1 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1432,7 +1432,9 @@ static inline void update_idle_core(struct rq *rq) { }
 #ifdef CONFIG_FAIR_GROUP_SCHED
 static inline struct task_struct *task_of(struct sched_entity *se)
 {
-	SCHED_WARN_ON(!entity_is_task(se));
+	if (!entity_is_task(se))
+		return NULL;
+
 	return container_of(se, struct task_struct, se);
 }
 
-- 
2.25.1


  reply	other threads:[~2023-12-06  6:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06  6:33 [PATCH v2 0/2] sched: Return NULL when entity isn't a task Yajun Deng
2023-12-06  6:33 ` Yajun Deng [this message]
2024-01-23  3:48   ` [PATCH v2 1/2] sched/fair: Return NULL when entity isn't a task in task_of() Steven Rostedt
2024-01-24  8:08     ` Yajun Deng
2023-12-06  6:34 ` [PATCH v2 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of() Yajun Deng
2024-01-23  3:19 ` [PATCH v2 0/2] sched: Return NULL when entity isn't a task Yajun Deng

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=20231206063400.3206346-2-yajun.deng@linux.dev \
    --to=yajun.deng@linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®