Skip to contentSkip to conversation

workqueue: validate cpumask_first() result in llc_populate_cpu_shard_id()

PATCH Superseded Breno Leitao 2 participants 5 messages Apr 10 – Apr 13 Workqueue

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: validate cpumask_first() result in llc_populate_cpu_shard_id()

In llc_populate_cpu_shard_id(), cpumask_first(sibling_cpus) is used to find the leader CPU, and the result is then used to index into cpu_shard_id[]. Add a bounds check with WARN_ON_ONCE to guard against unexpected values before using it as an array index.

Store the result in a local variable to make the code clearer, as also to avoid calling cpumask_first() twice.

Fixes 5920d046f7ae3 ("workqueue: add WQ_AFFN_CACHE_SHARD affinity scope")
---
 kernel/workqueue.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/workqueue.c +6 -1
index 083d8fe301f46..3cb4376a248b0 100644
@@ -8300,6 +8300,7 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
 	int cores_in_shard = 0;
 	/* This is a cursor for the shards. Go from zero to nr_shards - 1*/
 	int shard_id = 0;
+	int leader;
 	int c;
 
 	/* Iterate at every CPU for a given LLC pod, and assign it a shard */
@@ -8318,7 +8319,11 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
 			 * The siblings' shard MUST be the same as the leader.
 			 * never split threads in the same core.
 			 */
-			cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+			leader = cpumask_first(sibling_cpus);
+
+			if (WARN_ON_ONCE(leader >= nr_cpu_ids))
+				continue;
+			cpu_shard_id[c] = cpu_shard_id[leader];
 		}
 	}
 

---
base-commit: 3fa7d958829eb9bc3b469ed07f11de3d2804ef71
change-id: 20260410-workqueue_fix_nios-e6763904aee9

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
maintainer
#1 lore raw

Hello,

Quoted 9 lines · Breno Leitao

In llc_populate_cpu_shard_id(), cpumask_first(sibling_cpus) is used to find the leader CPU, and the result is then used to index into cpu_shard_id[]. Add a bounds check with WARN_ON_ONCE to guard against unexpected values before using it as an array index.

Store the result in a local variable to make the code clearer, as also to avoid calling cpumask_first() twice.

Fixes 5920d046f7ae3 ("workqueue: add WQ_AFFN_CACHE_SHARD affinity scope")

...

kernel/workqueue.c static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus, · line 8326

Quoted 10 lines
kernel/workqueue.c +5 -1
@@ -8318,7 +8319,11 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
 			 * The siblings' shard MUST be the same as the leader.
 			 * never split threads in the same core.
 			 */
-			cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+			leader = cpumask_first(sibling_cpus);
+
+			if (WARN_ON_ONCE(leader >= nr_cpu_ids))
+				continue;
+			cpu_shard_id[c] = cpu_shard_id[leader];

sibling_cpus can't be empty, right? This is mostly to shut up the reported compiler warning? If so, can you please note that in a ocmment and the description?

Thanks.

Signature · 1 line
tejun
#2 lore raw

Hello Tejun,

Quoted 11 lines · Tejun Heo
Quoted 9 lines · Breno Leitao

In llc_populate_cpu_shard_id(), cpumask_first(sibling_cpus) is used to find the leader CPU, and the result is then used to index into cpu_shard_id[]. Add a bounds check with WARN_ON_ONCE to guard against unexpected values before using it as an array index.

Store the result in a local variable to make the code clearer, as also to avoid calling cpumask_first() twice.

Fixes 5920d046f7ae3 ("workqueue: add WQ_AFFN_CACHE_SHARD affinity scope")

...

Quoted 12 lines

Review static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus, · line 8326

Quoted 10 lines
(quoted diff) +5 -1
@@ -8318,7 +8319,11 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
 			 * The siblings' shard MUST be the same as the leader.
 			 * never split threads in the same core.
 			 */
-			cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+			leader = cpumask_first(sibling_cpus);
+
+			if (WARN_ON_ONCE(leader >= nr_cpu_ids))
+				continue;
+			cpu_shard_id[c] = cpu_shard_id[leader];

sibling_cpus can't be empty, right?

Correct. sibling_cpus will have, at least, 'c' set.

Quoted 3 lines

This is mostly to shut up the reported
compiler warning? If so, can you please note that in a ocmment and the description?

Sure. Is something like the following acceptable?

kernel/workqueue.c +12 -1
index 083d8fe301f46..5dc304cdfa7f9 100644
@@ -8300,6 +8300,7 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
        int cores_in_shard = 0;
        /* This is a cursor for the shards. Go from zero to nr_shards - 1*/
        int shard_id = 0;
+       int leader;
        int c;
 
        /* Iterate at every CPU for a given LLC pod, and assign it a shard */
@@ -8318,7 +8319,17 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
                         * The siblings' shard MUST be the same as the leader.
                         * never split threads in the same core.
                         */
-                       cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+                       leader = cpumask_first(sibling_cpus);
+
+                       /*
+                        * sibling_cpus cannot be empty here since 'c'
+                        * is always set in it. This check silences a
+                        * compiler warning about using the unchecked
+                        * cpumask_first() result as an array index.
+                        */
+                       if (WARN_ON_ONCE(leader >= nr_cpu_ids))
+                               continue;
+                       cpu_shard_id[c] = cpu_shard_id[leader];
                }
        }
 
maintainer
#3 lore raw

Hello,

kernel/workqueue.c static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus, · line 8329

Quoted 13 lines · Breno Leitao
kernel/workqueue.c +8 -1
@@ -8318,7 +8319,17 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
                         * The siblings' shard MUST be the same as the leader.
                         * never split threads in the same core.
                         */
-                       cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+                       leader = cpumask_first(sibling_cpus);
+
+                       /*
+                        * sibling_cpus cannot be empty here since 'c'
+                        * is always set in it. This check silences a
+                        * compiler warning about using the unchecked
+                        * cpumask_first() result as an array index.
+                        */

Can you add more details on the warning and update the patch desc accordingly? This only triggers on UP configs, right?

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 19 lines · Tejun Heo

Hello,

kernel/workqueue.c static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus, · line 8329

Quoted 13 lines · Breno Leitao
kernel/workqueue.c +8 -1
@@ -8318,7 +8319,17 @@ static void __init llc_populate_cpu_shard_id(const struct cpumask *pod_cpus,
                         * The siblings' shard MUST be the same as the leader.
                         * never split threads in the same core.
                         */
-                       cpu_shard_id[c] = cpu_shard_id[cpumask_first(sibling_cpus)];
+                       leader = cpumask_first(sibling_cpus);
+
+                       /*
+                        * sibling_cpus cannot be empty here since 'c'
+                        * is always set in it. This check silences a
+                        * compiler warning about using the unchecked
+                        * cpumask_first() result as an array index.
+                        */

Can you add more details on the warning and update the patch desc accordingly?

Ack!

Quoted 1 line

This only triggers on UP configs, right?

yes, this only triggers on UP configs (like nios2) because on SMP NR_CPUS > 1 and the compiler can't prove the index is out of bounds.