Skip to contentSkip to conversation

sched: simplify sched_init_numa()

PATCH In review Yury Norov 3 participants 4 messages Jul 19, 2025 – Aug 19, 2025 Scheduler

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 sched: simplify sched_init_numa()

From: Yury Norov (NVIDIA) <[email protected]>

The function opencodes for_each_set_bit() macro.

---
 kernel/sched/topology.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/topology.c +3 -4
index b958fe48e020..7dc3c79aa480 100644
@@ -1932,10 +1932,9 @@ void sched_init_numa(int offline_node)
 		return;
 	}
 
-	for (i = 0, j = 0; i < nr_levels; i++, j++) {
-		j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
-		distances[i] = j;
-	}
+	for_each_set_bit(j, distance_map, NR_DISTANCE_VALUES)
+		distances[i++] = j;
+
 	rcu_assign_pointer(sched_domains_numa_distance, distances);
 
 	bitmap_free(distance_map);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 14 lines · Yury Norov

From: Yury Norov (NVIDIA) <[email protected]>

The function opencodes for_each_set_bit() macro.

---
 kernel/sched/topology.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/topology.c +0 -0
index b958fe48e020..7dc3c79aa480 100644

kernel/sched/topology.c void sched_init_numa(int offline_node) · line 1937

Quoted 10 lines
kernel/sched/topology.c +3 -4
@@ -1932,10 +1932,9 @@ void sched_init_numa(int offline_node)
 		return;
 	}
 
-	for (i = 0, j = 0; i < nr_levels; i++, j++) {
-		j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
-		distances[i] = j;
-	}
+	for_each_set_bit(j, distance_map, NR_DISTANCE_VALUES)
+		distances[i++] = j;
+

Dont we need to reset the value of i;

Also now we may be iterating for NR_DISTANCE_VALUES instead of nr_levels. It should be okay, since NR_DISTANCE_VALUES is just 8.

Quoted 6 lines
kernel/sched/topology.c +0 -0
 	rcu_assign_pointer(sched_domains_numa_distance, distances);
 
 	bitmap_free(distance_map);
Signature · 1 line
2.43.0
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#2 lore raw
Quoted 19 lines · Srikar Dronamraju
Quoted 17 lines · Yury Norov

From: Yury Norov (NVIDIA) <[email protected]>

The function opencodes for_each_set_bit() macro.

---
 kernel/sched/topology.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/topology.c +0 -0
index b958fe48e020..7dc3c79aa480 100644
@@ -1932,10 +1932,9 @@ void sched_init_numa(int offline_node)
             return;
     }
 
Quoted 10 lines

kernel/sched/topology.c void sched_init_numa(int offline_node) · line 1937

Quoted 7 lines
kernel/sched/topology.c +3 -4
-	for (i = 0, j = 0; i < nr_levels; i++, j++) {
-		j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
-		distances[i] = j;
-	}
+	for_each_set_bit(j, distance_map, NR_DISTANCE_VALUES)
+		distances[i++] = j;
+

Dont we need to reset the value of i;

That.

Quoted 3 lines

Also now we may be iterating for NR_DISTANCE_VALUES instead of nr_levels. It should be okay, since NR_DISTANCE_VALUES is just 8.

DISTANCE_BITS is 8, NR_DISTANCE_VALUES is 1 << 8 aka 256.

But here the use of for_each_set_bit() means we'll get just one extra iteration compared to using nr_levels. That said, since we *have* to compute nr_levels to allocate sched_domains_numa_distance, IMO we're not gaining much by using for_each_set_bit() here.

Quoted 10 lines
Quoted 6 lines
kernel/sched/topology.c +0 -1
     rcu_assign_pointer(sched_domains_numa_distance, distances);
 
     bitmap_free(distance_map);
--

2.43.0

--
Thanks and Regards
Srikar Dronamraju

author
#3 lore raw
Quoted 31 lines · Valentin Schneider
Quoted 19 lines · Srikar Dronamraju
Quoted 17 lines · Yury Norov

From: Yury Norov (NVIDIA) <[email protected]>

The function opencodes for_each_set_bit() macro.

---
 kernel/sched/topology.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/topology.c +0 -0
index b958fe48e020..7dc3c79aa480 100644
@@ -1932,10 +1932,9 @@ void sched_init_numa(int offline_node)
             return;
     }
 
Quoted 10 lines

kernel/sched/topology.c void sched_init_numa(int offline_node) · line 1937

Quoted 7 lines
kernel/sched/topology.c +3 -4
-	for (i = 0, j = 0; i < nr_levels; i++, j++) {
-		j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
-		distances[i] = j;
-	}
+	for_each_set_bit(j, distance_map, NR_DISTANCE_VALUES)
+		distances[i++] = j;
+

Dont we need to reset the value of i;

Quoted 1 line

That.

Yes we need.

Quoted 5 lines
Quoted 3 lines

Also now we may be iterating for NR_DISTANCE_VALUES instead of nr_levels. It should be okay, since NR_DISTANCE_VALUES is just 8.

DISTANCE_BITS is 8, NR_DISTANCE_VALUES is 1 << 8 aka 256.

I mistaken this DISTANCE_BITS vs NR_DISTANCE_VALUES. The first one is 8, so we can benefit from small_const_nbits() optimization. But we actually iterate over NR_DISTANCE_VALUES - and no benefit for using it over nr_levels.

Quoted 4 lines

But here the use of for_each_set_bit() means we'll get just one extra iteration compared to using nr_levels. That said, since we *have* to compute nr_levels to allocate sched_domains_numa_distance, IMO we're not gaining much by using for_each_set_bit() here.

I'm not sure I understand that. If we switch the loop terminator to nr_levels, the for_each() will be the exact functional equivalent.

I'll get back to this function shortly and send v2.

Thanks for the discussion.