Skip to contentSkip to conversation

core-parking: shrink core_parking_cpunum[]

PATCH In review Jan Beulich 2 participants 5 messages Mar 9 – Mar 11

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 core-parking: shrink 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. Allocate it dynamically ahead of registering the hooks.

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 altogether, rather than replacing them.

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

Finally drop the redundant initializer from core_parking_init().

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

num_possible_cpus() would be the more correct thing to use as array dimension, yet we don't maintain cpu_possible_map on x86.
---
v2: Use dynamic allocation.

xen/common/core_parking.c +9 -5
@@ -20,6 +20,7 @@
 #include <xen/cpumask.h>
 #include <xen/init.h>
 #include <xen/param.h>
+#include <xen/xvmalloc.h>
 
 #include <asm/smp.h>
 
@@ -27,8 +28,8 @@
 #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 unsigned int *__ro_after_init core_parking_cpunum;
 
 struct cp_policy {
     char name[30];
@@ -188,7 +189,7 @@ long cf_check core_parking_helper(void *
             return ret;
 
         spin_lock(&accounting_lock);
-        BUG_ON(cur_idle_nums >= ARRAY_SIZE(core_parking_cpunum));
+        BUG_ON(cur_idle_nums >= nr_cpu_ids);
         core_parking_cpunum[cur_idle_nums++] = cpu;
         spin_unlock(&accounting_lock);
     }
@@ -263,9 +264,12 @@ static int __init register_core_parking_
 
 static int __init cf_check core_parking_init(void)
 {
-    int ret = 0;
+    int ret;
 
-    if ( core_parking_controller == PERFORMANCE_FIRST )
+    core_parking_cpunum = xvzalloc_array(unsigned int, nr_cpu_ids);
+    if ( !core_parking_cpunum )
+        ret = -ENOMEM;
+    else if ( core_parking_controller == PERFORMANCE_FIRST )
         ret = register_core_parking_policy(&performance_first);
     else
         ret = register_core_parking_policy(&power_first);
#1 lore raw

You possibly want to adjust the subject, instead of shrink I would use "dynamically allocate" or similar.

Quoted 35 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. Allocate it dynamically ahead of registering the hooks.

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 altogether, rather than replacing them.

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

Finally drop the redundant initializer from core_parking_init().

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

num_possible_cpus() would be the more correct thing to use as array dimension, yet we don't maintain cpu_possible_map on x86.
---
v2: Use dynamic allocation.

xen/common/core_parking.c +1 -0
@@ -20,6 +20,7 @@
 #include <xen/cpumask.h>
 #include <xen/init.h>
 #include <xen/param.h>
+#include <xen/xvmalloc.h>
 
 #include <asm/smp.h>
 

xen/common/core_parking.c line 32

Quoted 7 lines
xen/common/core_parking.c +2 -2
@@ -27,8 +28,8 @@
 #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 unsigned int *__ro_after_init core_parking_cpunum;

Don't you need some kind of check in core_parking_remove() to prevent a NULL pointer dereference if core_parking_cpunum hasn't been allocated?

Callers of XEN_SYSCTL_cpu_hotplug can set fn = smt_up_down_helper, and that would call core_parking_remove(). core_parking_helper() already contains a check that prevents accessing core_parking_cpunum if no policy has been registered.

Thanks, Roger.

#2 lore raw
Quoted 2 lines · Roger Pau Monné

You possibly want to adjust the subject, instead of shrink I would use "dynamically allocate" or similar.

I've changed it, albeit the goal really is the shrinking.

Quoted 23 lines · Jan Beulich
Quoted 11 lines
xen/common/core_parking.c +1 -0
@@ -20,6 +20,7 @@
 #include <xen/cpumask.h>
 #include <xen/init.h>
 #include <xen/param.h>
+#include <xen/xvmalloc.h>
 
 #include <asm/smp.h>
 

xen/common/core_parking.c line 32

Quoted 7 lines
xen/common/core_parking.c +2 -2
@@ -27,8 +28,8 @@
 #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 unsigned int *__ro_after_init core_parking_cpunum;

Don't you need some kind of check in core_parking_remove() to prevent a NULL pointer dereference if core_parking_cpunum hasn't been allocated?

Quoted 4 lines

Callers of XEN_SYSCTL_cpu_hotplug can set fn = smt_up_down_helper, and that would call core_parking_remove(). core_parking_helper() already contains a check that prevents accessing core_parking_cpunum if no policy has been registered.

Because of this check, cur_idle_nums can never become non-zero when the array couldn't be allocated. Hence core_parking_remove() will be a somewhat expensive no-op in that case, with no deref of core_parking_cpunum.

Jan

Reviewed-by
#3 lore raw
Quoted 29 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

You possibly want to adjust the subject, instead of shrink I would use "dynamically allocate" or similar.

I've changed it, albeit the goal really is the shrinking.

Quoted 23 lines · Jan Beulich
Quoted 11 lines
xen/common/core_parking.c +1 -0
@@ -20,6 +20,7 @@
 #include <xen/cpumask.h>
 #include <xen/init.h>
 #include <xen/param.h>
+#include <xen/xvmalloc.h>
 
 #include <asm/smp.h>
 

xen/common/core_parking.c line 32

Quoted 7 lines
xen/common/core_parking.c +2 -2
@@ -27,8 +28,8 @@
 #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 unsigned int *__ro_after_init core_parking_cpunum;

Don't you need some kind of check in core_parking_remove() to prevent a NULL pointer dereference if core_parking_cpunum hasn't been allocated?

Quoted 8 lines
Quoted 4 lines

Callers of XEN_SYSCTL_cpu_hotplug can set fn = smt_up_down_helper, and that would call core_parking_remove(). core_parking_helper() already contains a check that prevents accessing core_parking_cpunum if no policy has been registered.

Because of this check, cur_idle_nums can never become non-zero when the array couldn't be allocated. Hence core_parking_remove() will be a somewhat expensive no-op in that case, with no deref of core_parking_cpunum.

Oh, I see, both loops in core_parking_remove() are bounded to cur_idle_nums.

Thanks, Roger.

#4 lore raw
In reply to Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

You possibly want to adjust the subject, instead of shrink I would use "dynamically allocate" or similar.

I've changed it, albeit the goal really is the shrinking.

And it's only now that I notice that I committed the patch with its old title, sorry. I failed to properly propagate the adjustment.

Jan