Skip to contentSkip to conversation

core-parking: shrink and relocate core_parking_cpunum[]

PATCH Superseded Jan Beulich 2 participants 4 messages Nov 12, 2025 – Mar 9

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 core-parking: shrink and relocate core_parking_cpunum[]

This NR_CPUS-dimensioned array is likely unused on most installations. Therefore it is especially wasteful for it to consume more space than really needed. Use the smallest possible type.

Further the array having all fields set to -1 is actually useless. Nothing relies on it, and core_parking_remove() doesn't restore the sentinel for vacated slots. Drop the initializers, moving the array to .bss.

Finally take the opportunity and update an adjacent variable's type, where a fixed-width type was pretty clearly inappropriate to use.

---
I assume there is a reason this is acting (mostly) as a LIFO. Else a simple cpumask_t would suffice.

An alternative would be to use the new BRK allocator, at least for NR_CPUS above a certain threshold.

xen/common/core_parking.c +10 -2
@@ -27,8 +27,16 @@
 #define CORE_PARKING_DECREMENT 2
 
 static DEFINE_SPINLOCK(accounting_lock);
-static uint32_t cur_idle_nums;
-static unsigned int core_parking_cpunum[NR_CPUS] = {[0 ... NR_CPUS-1] = -1};
+static unsigned int cur_idle_nums;
+static
+#if CONFIG_NR_CPUS <= (1U << BITS_PER_BYTE)
+    unsigned char
+#elif CONFIG_NR_CPUS <= (1U << (BITS_PER_BYTE * __SIZEOF_SHORT__))
+    unsigned short
+#else
+    unsigned int
+#endif
+    core_parking_cpunum[CONFIG_NR_CPUS];
 
 struct cp_policy {
     char name[30];
#1 lore raw
Quoted 11 lines · Jan Beulich

This NR_CPUS-dimensioned array is likely unused on most installations. Therefore it is especially wasteful for it to consume more space than really needed. Use the smallest possible type.

Further the array having all fields set to -1 is actually useless. Nothing relies on it, and core_parking_remove() doesn't restore the sentinel for vacated slots. Drop the initializers, moving the array to .bss.

Finally take the opportunity and update an adjacent variable's type, where a fixed-width type was pretty clearly inappropriate to use.

Quoted 7 lines

---
I assume there is a reason this is acting (mostly) as a LIFO. Else a simple cpumask_t would suffice.

An alternative would be to use the new BRK allocator, at least for NR_CPUS above a certain threshold.

Can't we just allocate this memory using xvzalloc_array()? If we do care about it being too big certainly allocating only when needed, and based on the number of possible CPUs on the system would be much better than playing games with the array type?

Thanks, Roger.

#2 lore raw
Quoted 17 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich

This NR_CPUS-dimensioned array is likely unused on most installations. Therefore it is especially wasteful for it to consume more space than really needed. Use the smallest possible type.

Further the array having all fields set to -1 is actually useless. Nothing relies on it, and core_parking_remove() doesn't restore the sentinel for vacated slots. Drop the initializers, moving the array to .bss.

Finally take the opportunity and update an adjacent variable's type, where a fixed-width type was pretty clearly inappropriate to use.

---
I assume there is a reason this is acting (mostly) as a LIFO. Else a simple cpumask_t would suffice.

Quoted 7 lines
Quoted 2 lines

An alternative would be to use the new BRK allocator, at least for NR_CPUS above a certain threshold.

Can't we just allocate this memory using xvzalloc_array()? If we do care about it being too big certainly allocating only when needed, and based on the number of possible CPUs on the system would be much better than playing games with the array type?

Hmm, yes, how did it not occur to me to dynamically allocate the array? It can't be used ahead of core_parking_init(). (Not shrinking the array element type will still be a little wasteful, but perhaps that's acceptable to keep the code simple.)

Jan

#3 lore raw
Quoted 26 lines · Jan Beulich
Quoted 17 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich

This NR_CPUS-dimensioned array is likely unused on most installations. Therefore it is especially wasteful for it to consume more space than really needed. Use the smallest possible type.

Further the array having all fields set to -1 is actually useless. Nothing relies on it, and core_parking_remove() doesn't restore the sentinel for vacated slots. Drop the initializers, moving the array to .bss.

Finally take the opportunity and update an adjacent variable's type, where a fixed-width type was pretty clearly inappropriate to use.

---
I assume there is a reason this is acting (mostly) as a LIFO. Else a simple cpumask_t would suffice.

Quoted 7 lines
Quoted 2 lines

An alternative would be to use the new BRK allocator, at least for NR_CPUS above a certain threshold.

Can't we just allocate this memory using xvzalloc_array()? If we do care about it being too big certainly allocating only when needed, and based on the number of possible CPUs on the system would be much better than playing games with the array type?

Quoted 4 lines

Hmm, yes, how did it not occur to me to dynamically allocate the array? It can't be used ahead of core_parking_init(). (Not shrinking the array element type will still be a little wasteful, but perhaps that's acceptable to keep the code simple.)

We don't shrink other similar element types based on the supported CPU count. Iff we ever wanted to do this (which I'm not sure) we should introduce a new typedef that generalizes the optimization rather than open coding it (ie: cpu_id_t or similar?)

Thanks, Roger.