Re: [peterz-queue:sched/core 13/19] kernel/sched/debug.c:453 print_cfs_group_stats() warn: variable dereferenced before check 'se' (see line 444)
From: Yafang Shao
Date: Mon Sep 13 2021 - 07:04:56 EST
On Mon, Sep 13, 2021 at 4:48 PM Dan Carpenter <dan.carpenter@xxxxxxxxxx> wrote:
>
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git sched/core
> head: 2dfdb3d20ad50e2ae2cb84cbceb0f0fc75e79e5d
> commit: 445d9e8ba05d5e9e4b26956b7fe529223e29d8d1 [13/19] sched: make struct sched_statistics independent of fair sched class
> config: x86_64-randconfig-m001-20210910 (attached as .config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>
>
> smatch warnings:
> kernel/sched/debug.c:453 print_cfs_group_stats() warn: variable dereferenced before check 'se' (see line 444)
>
Sorry, my bad.
Hi Peter,
Could you pls. help amend below change to the original commit ?
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index 52b4426414c0..d59f33ac06fe 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -440,11 +440,8 @@ void dirty_sched_domain_sysctl(int cpu)
#ifdef CONFIG_FAIR_GROUP_SCHED
static void print_cfs_group_stats(struct seq_file *m, int cpu, struct
task_group *tg)
{
- struct sched_statistics __maybe_unused *stats;
struct sched_entity *se = tg->se[cpu];
- stats = __schedstats_from_se(se);
-
#define P(F) SEQ_printf(m, " .%-30s: %lld\n", #F,
(long long)F)
#define P_SCHEDSTAT(F) SEQ_printf(m, " .%-30s: %lld\n", \
#F, (long long)schedstat_val(stats->F))
@@ -460,6 +457,8 @@ static void print_cfs_group_stats(struct seq_file
*m, int cpu, struct task_group
PN(se->sum_exec_runtime);
if (schedstat_enabled()) {
+ struct sched_statistics *stats = __schedstats_from_se(se);
+
PN_SCHEDSTAT(wait_start);
PN_SCHEDSTAT(sleep_start);
PN_SCHEDSTAT(block_start);
> vim +/se +453 kernel/sched/debug.c
>
> 3866e845ed5222 kernel/sched/debug.c Steven Rostedt (Red Hat 2016-02-22 439)
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 440 #ifdef CONFIG_FAIR_GROUP_SCHED
> 5091faa449ee0b kernel/sched_debug.c Mike Galbraith 2010-11-30 441 static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group *tg)
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 442 {
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 443 struct sched_entity *se = tg->se[cpu];
> 445d9e8ba05d5e kernel/sched/debug.c Yafang Shao 2021-09-05 @444 struct sched_statistics *stats = __schedstats_from_se(se);
> ^^
> New unchecked dereference.
>
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 445
> 97fb7a0a8944bd kernel/sched/debug.c Ingo Molnar 2018-03-03 446 #define P(F) SEQ_printf(m, " .%-30s: %lld\n", #F, (long long)F)
> 445d9e8ba05d5e kernel/sched/debug.c Yafang Shao 2021-09-05 447 #define P_SCHEDSTAT(F) SEQ_printf(m, " .%-30s: %lld\n", \
> 445d9e8ba05d5e kernel/sched/debug.c Yafang Shao 2021-09-05 448 #F, (long long)schedstat_val(stats->F))
> 97fb7a0a8944bd kernel/sched/debug.c Ingo Molnar 2018-03-03 449 #define PN(F) SEQ_printf(m, " .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)F))
> 445d9e8ba05d5e kernel/sched/debug.c Yafang Shao 2021-09-05 450 #define PN_SCHEDSTAT(F) SEQ_printf(m, " .%-30s: %lld.%06ld\n", \
> 445d9e8ba05d5e kernel/sched/debug.c Yafang Shao 2021-09-05 451 #F, SPLIT_NS((long long)schedstat_val(stats->F)))
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 452
> cd126afe838d7e kernel/sched/debug.c Yuyang Du 2015-07-15 @453 if (!se)
> ^^^
> The old code assumed "se" can be NULL.
>
> 18bf2805d9b30c kernel/sched/debug.c Ben Segall 2012-10-04 454 return;
> 18bf2805d9b30c kernel/sched/debug.c Ben Segall 2012-10-04 455
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 456 PN(se->exec_start);
> ff9b48c3598732 kernel/sched_debug.c Bharata B Rao 2008-11-10 457 PN(se->vruntime);
>
> ---
> 0-DAY CI Kernel Test Service, Intel Corporation
> https://lists.01.org/hyperkitty/list/kbuild-all@xxxxxxxxxxxx
>
--
Thanks
Yafang