Skip to content

Commit 0ccf98e

Browse files
leitaoclsotog
authored andcommitted
workqueue: validate cpumask_first() result in llc_populate_cpu_shard_id()
On uniprocessor (UP) configs such as nios2, NR_CPUS is 1, so cpu_shard_id[] is a single-element array (int[1]). In llc_populate_cpu_shard_id(), cpumask_first(sibling_cpus) returns an unsigned int that the compiler cannot prove is always 0, triggering a -Warray-bounds warning when the result is used to index cpu_shard_id[]: kernel/workqueue.c:8321:55: warning: array subscript 1 is above array bounds of 'int[1]' [-Warray-bounds] 8321 | cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)]; | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is a false positive: sibling_cpus can never be empty here because 'c' itself is always set in it, so cpumask_first() will always return a valid CPU. However, the compiler cannot prove this statically, and the warning only manifests on UP configs where the array size is 1. Add a bounds check with WARN_ON_ONCE to silence the warning, and store the result in a local variable to make the code clearer and avoid calling cpumask_first() twice. Fixes: 5920d04 ("workqueue: add WQ_AFFN_CACHE_SHARD affinity scope") Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202604022343.GQtkF2vO-lkp@intel.com/ Signed-off-by: Breno Leitao <leitao@debian.org> Signed-off-by: Tejun Heo <tj@kernel.org> (cherry picked from commit 76af546) Signed-off-by: Carol L Soto <csoto@nvidia.com>
1 parent d06c47e commit 0ccf98e

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

kernel/workqueue.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8255,6 +8255,7 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
82558255
const struct cpumask *sibling_cpus;
82568256
/* Count the number of cores in the current shard_id */
82578257
int cores_in_shard = 0;
8258+
unsigned int leader;
82588259
/* This is a cursor for the shards. Go from zero to nr_shards - 1*/
82598260
int shard_id = 0;
82608261
int c;
@@ -8275,7 +8276,17 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
82758276
* The siblings' shard MUST be the same as the leader.
82768277
* never split threads in the same core.
82778278
*/
8278-
cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
8279+
leader = cpumask_first(sibling_cpus);
8280+
8281+
/*
8282+
* This check silences a Warray-bounds warning on UP
8283+
* configs where NR_CPUS=1 makes cpu_shard_id[]
8284+
* a single-element array, and the compiler can't
8285+
* prove the index is always 0.
8286+
*/
8287+
if (WARN_ON_ONCE(leader >= nr_cpu_ids))
8288+
continue;
8289+
cpu_shard_id[c] = cpu_shard_id[leader];
82798290
}
82808291
}
82818292

0 commit comments

Comments
 (0)