Skip to content

Commit 01bb11a

Browse files
YuryNorovkuba-moo
authored andcommitted
sched/topology: fix KASAN warning in hop_cmp()
Despite that prev_hop is used conditionally on cur_hop is not the first hop, it's initialized unconditionally. Because initialization implies dereferencing, it might happen that the code dereferences uninitialized memory, which has been spotted by KASAN. Fix it by reorganizing hop_cmp() logic. Reported-by: Bruno Goncalves <[email protected]> Fixes: cd7f553 ("sched: add sched_numa_find_nth_cpu()") Signed-off-by: Yury Norov <[email protected]> Link: https://lore.kernel.org/r/Y+7avK6V9SyAWsXi@yury-laptop/ Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 3fcdf2d commit 01bb11a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

kernel/sched/topology.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,14 +2079,19 @@ struct __cmp_key {
20792079

20802080
static int hop_cmp(const void *a, const void *b)
20812081
{
2082-
struct cpumask **prev_hop = *((struct cpumask ***)b - 1);
2083-
struct cpumask **cur_hop = *(struct cpumask ***)b;
2082+
struct cpumask **prev_hop, **cur_hop = *(struct cpumask ***)b;
20842083
struct __cmp_key *k = (struct __cmp_key *)a;
20852084

20862085
if (cpumask_weight_and(k->cpus, cur_hop[k->node]) <= k->cpu)
20872086
return 1;
20882087

2089-
k->w = (b == k->masks) ? 0 : cpumask_weight_and(k->cpus, prev_hop[k->node]);
2088+
if (b == k->masks) {
2089+
k->w = 0;
2090+
return 0;
2091+
}
2092+
2093+
prev_hop = *((struct cpumask ***)b - 1);
2094+
k->w = cpumask_weight_and(k->cpus, prev_hop[k->node]);
20902095
if (k->w <= k->cpu)
20912096
return 0;
20922097

0 commit comments

Comments
 (0)