Re: [PATCH] kernel/sched: fix KMSAN uninit-value error

From: Steven Rostedt
Date: Mon Sep 05 2022 - 11:14:01 EST


On Sun, 4 Sep 2022 19:37:14 -0700
Bernard Zhao <bernard@xxxxxxxx> wrote:

> syzbot link:
> https://syzkaller.appspot.com/bug?id=d04c5407207d11e46007775517b97764174bc45d
>
> Signed-off-by: Bernard Zhao <bernard@xxxxxxxx>
> ---
> kernel/sched/psi.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
> index ecb4b4ff4ce0..46f048121520 100644
> --- a/kernel/sched/psi.c
> +++ b/kernel/sched/psi.c
> @@ -195,6 +195,9 @@ static void group_init(struct psi_group *group)
> init_waitqueue_head(&group->poll_wait);
> timer_setup(&group->poll_timer, poll_timer_fn, 0);
> rcu_assign_pointer(group->poll_task, NULL);
> + memset(group->avg_total, 0, sizeof(group->avg_total));
> + memset(group->total, 0, sizeof(group->total));
> + memset(group->avg, 0, sizeof(group->avg));
> }

group_init() is only called in two places. One for a static variable which
will already have all the non-set fields initialized to zero. The other can
have kmalloc() converted to kzalloc() and not worry about zeroing any of
the fields in initialization.

-- Steve

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index ec66b40bdd40..00d62681ea6a 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -957,7 +957,7 @@ int psi_cgroup_alloc(struct cgroup *cgroup)
if (static_branch_likely(&psi_disabled))
return 0;

- cgroup->psi = kmalloc(sizeof(struct psi_group), GFP_KERNEL);
+ cgroup->psi = kzalloc(sizeof(struct psi_group), GFP_KERNEL);
if (!cgroup->psi)
return -ENOMEM;