2023-12-01 02:27:44

by Yajun Deng

[permalink] [raw]
Subject: [PATCH 0/2] sched: Return NULL when entity isn't a task

If the entity isn't a task, return the task struct is meaningless.
Return NULL when entity isn't a task that makes the code more concise.

Yajun Deng (2):
sched/fair: Return NULL when entity isn't a task in task_of()
sched/rt: Return NULL when rt entity isn't a task in rt_task_of()

kernel/sched/fair.c | 50 +++++++++++++++++------------------
kernel/sched/rt.c | 62 ++++++++++++--------------------------------
kernel/sched/sched.h | 4 ++-
3 files changed, 44 insertions(+), 72 deletions(-)

--
2.25.1


2023-12-01 02:28:10

by Yajun Deng

[permalink] [raw]
Subject: [PATCH 1/2] sched/fair: Return NULL when entity isn't a task in task_of()

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 <[email protected]>
---
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 34e23a8984ac..e8d444fad2d6 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

2023-12-01 02:28:14

by Yajun Deng

[permalink] [raw]
Subject: [PATCH 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of()

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

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

Signed-off-by: Yajun Deng <[email protected]>
---
kernel/sched/rt.c | 62 +++++++++++++----------------------------------
1 file changed, 17 insertions(+), 45 deletions(-)

diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index 3261b067b67e..be4d65e05af1 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -169,9 +169,9 @@ static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)

static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
{
-#ifdef CONFIG_SCHED_DEBUG
- WARN_ON_ONCE(!rt_entity_is_task(rt_se));
-#endif
+ if (!rt_entity_is_task(rt_se))
+ return NULL;
+
return container_of(rt_se, struct task_struct, rt);
}

@@ -941,12 +941,10 @@ static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)

static inline int rt_se_prio(struct sched_rt_entity *rt_se)
{
-#ifdef CONFIG_RT_GROUP_SCHED
struct rt_rq *rt_rq = group_rt_rq(rt_se);

if (rt_rq)
return rt_rq->highest_prio.curr;
-#endif

return rt_task_of(rt_se)->prio;
}
@@ -1266,54 +1264,34 @@ static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_arr
rt_se->on_list = 0;
}

-static inline struct sched_statistics *
-__schedstats_from_rt_se(struct sched_rt_entity *rt_se)
-{
-#ifdef CONFIG_RT_GROUP_SCHED
- /* schedstats is not supported for rt group. */
- if (!rt_entity_is_task(rt_se))
- return NULL;
-#endif
-
- return &rt_task_of(rt_se)->stats;
-}
-
static inline void
update_stats_wait_start_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
{
- struct sched_statistics *stats;
- struct task_struct *p = NULL;
+ struct task_struct *p;

if (!schedstat_enabled())
return;

- if (rt_entity_is_task(rt_se))
- p = rt_task_of(rt_se);
-
- stats = __schedstats_from_rt_se(rt_se);
- if (!stats)
+ p = rt_task_of(rt_se);
+ if (!p)
return;

- __update_stats_wait_start(rq_of_rt_rq(rt_rq), p, stats);
+ __update_stats_wait_start(rq_of_rt_rq(rt_rq), p, &p->stats);
}

static inline void
update_stats_enqueue_sleeper_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
{
- struct sched_statistics *stats;
- struct task_struct *p = NULL;
+ struct task_struct *p;

if (!schedstat_enabled())
return;

- if (rt_entity_is_task(rt_se))
- p = rt_task_of(rt_se);
-
- stats = __schedstats_from_rt_se(rt_se);
- if (!stats)
+ p = rt_task_of(rt_se);
+ if (!p)
return;

- __update_stats_enqueue_sleeper(rq_of_rt_rq(rt_rq), p, stats);
+ __update_stats_enqueue_sleeper(rq_of_rt_rq(rt_rq), p, &p->stats);
}

static inline void
@@ -1330,34 +1308,28 @@ update_stats_enqueue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
static inline void
update_stats_wait_end_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
{
- struct sched_statistics *stats;
- struct task_struct *p = NULL;
+ struct task_struct *p;

if (!schedstat_enabled())
return;

- if (rt_entity_is_task(rt_se))
- p = rt_task_of(rt_se);
-
- stats = __schedstats_from_rt_se(rt_se);
- if (!stats)
+ p = rt_task_of(rt_se);
+ if (!p)
return;

- __update_stats_wait_end(rq_of_rt_rq(rt_rq), p, stats);
+ __update_stats_wait_end(rq_of_rt_rq(rt_rq), p, &p->stats);
}

static inline void
update_stats_dequeue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
int flags)
{
- struct task_struct *p = NULL;
+ struct task_struct *p;

if (!schedstat_enabled())
return;

- if (rt_entity_is_task(rt_se))
- p = rt_task_of(rt_se);
-
+ p = rt_task_of(rt_se);
if ((flags & DEQUEUE_SLEEP) && p) {
unsigned int state;

--
2.25.1

2023-12-06 04:13:49

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of()

Hi Yajun,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/sched/core]
[also build test ERROR on next-20231205]
[cannot apply to linus/master v6.7-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Yajun-Deng/sched-fair-Return-NULL-when-entity-isn-t-a-task-in-task_of/20231201-103036
base: tip/sched/core
patch link: https://lore.kernel.org/r/20231201022704.3526377-3-yajun.deng%40linux.dev
patch subject: [PATCH 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of()
config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20231206/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from kernel/sched/build_policy.c:45:
kernel/sched/rt.c: In function 'rt_se_prio':
>> kernel/sched/rt.c:947:29: error: 'struct rt_rq' has no member named 'highest_prio'
947 | return rt_rq->highest_prio.curr;
| ^~


vim +947 kernel/sched/rt.c

ac086bc22997a2 kernel/sched_rt.c Peter Zijlstra 2008-04-19 941
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 942 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 943 {
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 944 struct rt_rq *rt_rq = group_rt_rq(rt_se);
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 945
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 946 if (rt_rq)
e864c499d9e578 kernel/sched_rt.c Gregory Haskins 2008-12-29 @947 return rt_rq->highest_prio.curr;
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 948
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 949 return rt_task_of(rt_se)->prio;
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 950 }
6f505b16425a51 kernel/sched_rt.c Peter Zijlstra 2008-01-25 951

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2023-12-06 06:32:27

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of()

Hi Yajun,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/sched/core]
[also build test ERROR on next-20231206]
[cannot apply to linus/master v6.7-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Yajun-Deng/sched-fair-Return-NULL-when-entity-isn-t-a-task-in-task_of/20231201-103036
base: tip/sched/core
patch link: https://lore.kernel.org/r/20231201022704.3526377-3-yajun.deng%40linux.dev
patch subject: [PATCH 2/2] sched/rt: Return NULL when rt entity isn't a task in rt_task_of()
config: arm-ixp4xx_defconfig (https://download.01.org/0day-ci/archive/20231206/[email protected]/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from kernel/sched/build_policy.c:45:
>> kernel/sched/rt.c:947:17: error: no member named 'highest_prio' in 'struct rt_rq'
return rt_rq->highest_prio.curr;
~~~~~ ^
1 error generated.


vim +947 kernel/sched/rt.c

ac086bc22997a2b kernel/sched_rt.c Peter Zijlstra 2008-04-19 941
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 942 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 943 {
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 944 struct rt_rq *rt_rq = group_rt_rq(rt_se);
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 945
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 946 if (rt_rq)
e864c499d9e5780 kernel/sched_rt.c Gregory Haskins 2008-12-29 @947 return rt_rq->highest_prio.curr;
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 948
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 949 return rt_task_of(rt_se)->prio;
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 950 }
6f505b16425a512 kernel/sched_rt.c Peter Zijlstra 2008-01-25 951

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki