Skip to contentSkip to conversation

Avoid mask based num_possible_cpus and num_online_cpus

PATCH In review Venkatesh Pallipadi 10 participants 48 messages Jan 18, 2012 – Mar 1, 2012

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid mask based num_possible_cpus and num_online_cpus

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |    8 ++++++--
 kernel/cpu.c            |    9 +++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/cpumask.h +6 -2
index 4f7a632..2eb04dd 100644
@@ -80,9 +80,13 @@ extern const struct cpumask *const cpu_online_mask;
 extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
+extern int nr_online_cpus;
+
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_cpu_ids)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +9 -0
index 2060c6e..eed2169 100644
@@ -622,6 +622,13 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
 EXPORT_SYMBOL(cpu_active_mask);
 
+#ifdef CONFIG_HOTPLUG_CPU
+int nr_online_cpus;
+#else
+int nr_online_cpus __read_mostly;
+#endif
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
 	if (possible)
@@ -644,6 +651,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
Signature · 1 line
1.7.7.3
#1 lore raw
Quoted 35 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  *  that it is possible might ever be plugged in at anytime during the
  *  life of that system boot.
  #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
  include/linux/cpumask.h |    8 ++++++--
  kernel/cpu.c            |    9 +++++++++
  2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/cpumask.h +0 -0
index 4f7a632..2eb04dd 100644
@@ -80,9 +80,13 @@ extern const struct cpumask *const cpu_online_mask;
  extern const struct cpumask *const cpu_present_mask;

include/linux/cpumask.h extern const struct cpumask *const cpu_online_mask; · line 88

Quoted 10 lines
include/linux/cpumask.h +5 -2
  extern const struct cpumask *const cpu_active_mask;
 
+extern int nr_online_cpus;
+
  #if NR_CPUS>  1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_cpu_ids)

nr_cpu_ids mean maximum cpu id of cpus. if cpu id are sparse, maximum id doesn't match number of cpus.

Quoted 9 lines
include/linux/cpumask.h +1 -0
+
  #define num_present_cpus()	cpumask_weight(cpu_present_mask)
  #define num_active_cpus()	cpumask_weight(cpu_active_mask)
  #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +0 -0
index 2060c6e..eed2169 100644

kernel/cpu.c static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; · line 630

Quoted 9 lines
kernel/cpu.c +6 -0
@@ -622,6 +622,13 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
  const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
  EXPORT_SYMBOL(cpu_active_mask);
 
+#ifdef CONFIG_HOTPLUG_CPU
+int nr_online_cpus;
+#else
+int nr_online_cpus __read_mostly;
+#endif
+EXPORT_SYMBOL(nr_online_cpus);

You can always mark this to __read_mostly. other cpu hotplug stuff do so. Because of, I guess, cpu hotplug developers don't think hotplugging is frequently event.

kernel/cpu.c void set_cpu_online(unsigned int cpu, bool online) · line 656

Quoted 10 lines
kernel/cpu.c +2 -0
  void set_cpu_possible(unsigned int cpu, bool possible)
  {
  	if (possible)
@@ -644,6 +651,8 @@ void set_cpu_online(unsigned int cpu, bool online)
  		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
  	else
  		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
  }

I like this change. :)

#2 lore raw
Quoted 38 lines · KOSAKI Motohiro
Quoted 37 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  *  that it is possible might ever be plugged in at anytime during the
  *  life of that system boot.

  #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
  include/linux/cpumask.h |    8 ++++++--
  kernel/cpu.c            |    9 +++++++++

  2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/cpumask.h +0 -0
index 4f7a632..2eb04dd 100644
@@ -80,9 +80,13 @@ extern const struct cpumask *const cpu_online_mask;
   extern const struct cpumask *const cpu_present_mask;
   extern const struct cpumask *const cpu_active_mask;
 
Quoted 12 lines

include/linux/cpumask.h extern const struct cpumask *const cpu_online_mask; · line 88

Quoted 8 lines
include/linux/cpumask.h +5 -2
+extern int nr_online_cpus;
+
   #if NR_CPUS>  1
-#define num_online_cpus()    cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()  cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()    (nr_online_cpus)
+#define num_possible_cpus()  (nr_cpu_ids)

nr_cpu_ids mean maximum cpu id of cpus. if cpu id are sparse, maximum id doesn't match number of cpus.

Yes. But will it be sparse in any arch? I saw some of the users of num_possible_cpus() doing things like allocating a buffer for that size and then indexing it using get_cpu(). So, I thought it would be better to use the existing nr_cpu_ids instead of inventing another variable. If indeed any arch is depending on this being sparse, we can have a new variable similar to num_possible_cpus and also audit all users of num_possible_cpus to see whether they should be using nr_cpu_ids instead.

Quoted 14 lines
Quoted 14 lines
include/linux/cpumask.h +1 -0
+
   #define num_present_cpus()  cpumask_weight(cpu_present_mask)
   #define num_active_cpus()   cpumask_weight(cpu_active_mask)
   #define cpu_online(cpu)             cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +0 -0
index 2060c6e..eed2169 100644
@@ -622,6 +622,13 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
   const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
   EXPORT_SYMBOL(cpu_active_mask);
 
Quoted 11 lines

kernel/cpu.c static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; · line 630

Quoted 6 lines
kernel/cpu.c +6 -0
+#ifdef CONFIG_HOTPLUG_CPU
+int nr_online_cpus;
+#else
+int nr_online_cpus __read_mostly;
+#endif
+EXPORT_SYMBOL(nr_online_cpus);

You can always mark this to __read_mostly. other cpu hotplug stuff do so. Because of, I guess, cpu hotplug developers don't think hotplugging is frequently event.

OK. Agree.

Quoted 15 lines
Quoted 11 lines
kernel/cpu.c +0 -0
   void set_cpu_possible(unsigned int cpu, bool possible)
   {
       if (possible)
@@ -644,6 +651,8 @@ void set_cpu_online(unsigned int cpu, bool online)
               cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));

      else
              cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); +
+     nr_online_cpus = cpumask_weight(cpu_online_mask); }

I like this change. :)

Thanks,
Venki

#3 lore raw
Quoted 53 lines · Venki Pallipadi
Quoted 38 lines · KOSAKI Motohiro
Quoted 37 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

   *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
   *  that it is possible might ever be plugged in at anytime during the
   *  life of that system boot.
   #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
   include/linux/cpumask.h |    8 ++++++--
   kernel/cpu.c            |    9 +++++++++
   2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/cpumask.h +0 -0
index 4f7a632..2eb04dd 100644
@@ -80,9 +80,13 @@ extern const struct cpumask *const cpu_online_mask;
   extern const struct cpumask *const cpu_present_mask;
   extern const struct cpumask *const cpu_active_mask;
 
Quoted 12 lines

include/linux/cpumask.h extern const struct cpumask *const cpu_online_mask; · line 88

Quoted 8 lines
include/linux/cpumask.h +5 -2
+extern int nr_online_cpus;
+
   #if NR_CPUS>    1
-#define num_online_cpus()    cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()  cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()    (nr_online_cpus)
+#define num_possible_cpus()  (nr_cpu_ids)

nr_cpu_ids mean maximum cpu id of cpus. if cpu id are sparse, maximum id doesn't match number of cpus.

Quoted 8 lines

Yes. But will it be sparse in any arch? I saw some of the users of num_possible_cpus() doing things like allocating a buffer for that size and then indexing it using get_cpu(). So, I thought it would be better to use the existing nr_cpu_ids instead of inventing another variable. If indeed any arch is depending on this being sparse, we can have a new variable similar to num_possible_cpus and also audit all users of num_possible_cpus to see whether they should be using nr_cpu_ids instead.

If my remember is correct, sparc does.

#4 lore raw

PATCH 1/1 Avoid mask based num_possible_cpus and num_online_cpus

Does this look better? Will send separate patch to fix code using num_possible_cpus() when they actually need nr_cpu_ids.

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |   10 ++++++++--
 kernel/cpu.c            |    5 +++++
 kernel/smp.c            |    4 ++++
 3 files changed, 17 insertions(+), 2 deletions(-)
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
 #if NR_CPUS == 1
 #define nr_cpu_ids		1
+#define nr_possible_cpus	1
 #else
 extern int nr_cpu_ids;
+extern int nr_possible_cpus;
 #endif
 
+extern int nr_online_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +5 -0
index 2060c6e..f179baa 100644
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
 EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
 	if (possible)
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
kernel/smp.c +4 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
 int nr_cpu_ids __read_mostly = NR_CPUS;
 EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;
+EXPORT_SYMBOL(nr_possible_cpus);
+
 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
 void __init setup_nr_cpu_ids(void)
 {
 	nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 /* Called by boot processor to activate the rest. */
Signature · 1 line
1.7.7.3
#5 lore raw
Quoted 2 lines · Venkatesh Pallipadi

Does this look better? Will send separate patch to fix code using num_possible_cpus() when they actually need nr_cpu_ids.

Sound ok to me. but I have two requests.

- Please mesure how much time (or cycle) spented by cpumask_weight(cpu_possible_mask).
- After your patch, nr_possible_cpus() return different value from before. Please verify this change doesn't makes any side effect.

#6 lore raw
Quoted 7 lines · KOSAKI Motohiro
Quoted 2 lines · Venkatesh Pallipadi

Does this look better? Will send separate patch to fix code using num_possible_cpus() when they actually need nr_cpu_ids.

Sound ok to me. but I have two requests.

 - Please mesure how much time (or cycle) spented by cpumask_weight(cpu_possible_mask).

On two of the recentish x86 server CPUs, __sw_hweight64() base mask_weight() takes ~300 cycles. popcnt instruction based weight takes <10 cycles.

Quoted 1 line

 - After your patch, nr_possible_cpus() return different value from before.

Yes. I have tested this on x86 and haven't seen any problem.

Quoted 3 lines

  Please verify this change doesn't makes any side effect.

#7 lore raw
In reply to Venkatesh Pallipadi

Hi,

Quoted 72 lines · Venkatesh Pallipadi

Does this look better? Will send separate patch to fix code using num_possible_cpus() when they actually need nr_cpu_ids.

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |   10 ++++++++--
 kernel/cpu.c            |    5 +++++
 kernel/smp.c            |    4 ++++
 3 files changed, 17 insertions(+), 2 deletions(-)
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
 #if NR_CPUS == 1
 #define nr_cpu_ids		1
+#define nr_possible_cpus	1
 #else
 extern int nr_cpu_ids;
+extern int nr_possible_cpus;
 #endif
 
+extern int nr_online_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +0 -0
index 2060c6e..f179baa 100644

kernel/cpu.c static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; · line 630

Quoted 9 lines
kernel/cpu.c +3 -0
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
 EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
 	if (possible)

Did you forget to add:

	nr_possible_cpus = cpumask_weight(cpu_possible_mask);

inside set_cpu_possible() ?

Quoted 18 lines
kernel/cpu.c +2 -0
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
kernel/smp.c +1 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
 int nr_cpu_ids __read_mostly = NR_CPUS;
 EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;

kernel/smp.c early_param("maxcpus", maxcpus); · line 671

Quoted 10 lines
kernel/smp.c +3 -0
+EXPORT_SYMBOL(nr_possible_cpus);
+
 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
 void __init setup_nr_cpu_ids(void)
 {
 	nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 /* Called by boot processor to activate the rest. */

Regards,
Srivatsa S. Bhat

#8 lore raw
Quoted 89 lines · Srivatsa S. Bhat

Hi,

Quoted 72 lines · Venkatesh Pallipadi

Does this look better? Will send separate patch to fix code using num_possible_cpus() when they actually need nr_cpu_ids.

Kernel's notion of possible cpus (from include/linux/cpumask.h)
 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

 #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |   10 ++++++++--
 kernel/cpu.c            |    5 +++++
 kernel/smp.c            |    4 ++++

 3 files changed, 17 insertions(+), 2 deletions(-)
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
  #if NR_CPUS == 1
  #define nr_cpu_ids           1
+#define nr_possible_cpus     1
  #else
  extern int nr_cpu_ids;
+extern int nr_possible_cpus;
  #endif
 
+extern int nr_online_cpus;
+
  #ifdef CONFIG_CPUMASK_OFFSTACK
  /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
   * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
  extern const struct cpumask *const cpu_active_mask;
 
  #if NR_CPUS > 1
-#define num_online_cpus()    cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()  cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()    (nr_online_cpus)
+#define num_possible_cpus()  (nr_possible_cpus)
+
  #define num_present_cpus()   cpumask_weight(cpu_present_mask)
  #define num_active_cpus()    cpumask_weight(cpu_active_mask)
  #define cpu_online(cpu)              cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +0 -0
index 2060c6e..f179baa 100644

kernel/cpu.c static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; · line 630

Quoted 9 lines
kernel/cpu.c +3 -0
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
  const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
  EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
  void set_cpu_possible(unsigned int cpu, bool possible)
  {
       if (possible)

Did you forget to add:

Quoted 3 lines

       nr_possible_cpus = cpumask_weight(cpu_possible_mask);

inside set_cpu_possible() ?

No. That was intentional as I have that coupled with nr_cpu_ids and set once after all the bits are set in setup_nr_cpu_ids() instead of doing for each bit set.

Quoted 35 lines
Quoted 20 lines
kernel/cpu.c +0 -0
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
               cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));

      else
              cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); +
+     nr_online_cpus = cpumask_weight(cpu_online_mask); }

 void set_cpu_active(unsigned int cpu, bool active)

kernel/smp.c +1 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
  int nr_cpu_ids __read_mostly = NR_CPUS;
  EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;

kernel/smp.c early_param("maxcpus", maxcpus); · line 671

Quoted 10 lines
kernel/smp.c +3 -0
+EXPORT_SYMBOL(nr_possible_cpus);
+
  /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
  void __init setup_nr_cpu_ids(void)
  {
       nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+     nr_possible_cpus = cpumask_weight(cpu_possible_mask);
  }
 
  /* Called by boot processor to activate the rest. */

Regards,
Srivatsa S. Bhat

#9 lore raw
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
kernel/cpu.c +3 -0
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+

 void set_cpu_possible(unsigned int cpu, bool possible) {
      if (possible)

Did you forget to add:

Quoted 7 lines
Quoted 3 lines

       nr_possible_cpus = cpumask_weight(cpu_possible_mask);

inside set_cpu_possible() ?

No. That was intentional as I have that coupled with nr_cpu_ids and set once after all the bits are set in setup_nr_cpu_ids() instead of doing for each bit set.

But, Srivatsa's way seems more safer, no? Is there any advantage to make couple with nr_cpu_ids?

#10 lore raw
Quoted 12 lines · KOSAKI Motohiro
Quoted 12 lines
Quoted 12 lines
Quoted 6 lines
kernel/cpu.c +3 -0
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+

 void set_cpu_possible(unsigned int cpu, bool possible) {
      if (possible)

Did you forget to add:

       nr_possible_cpus = cpumask_weight(cpu_possible_mask);

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

inside set_cpu_possible() ?

No. That was intentional as I have that coupled with nr_cpu_ids and set once after all the bits are set in setup_nr_cpu_ids() instead of doing for each bit set.

But, Srivatsa's way seems more safer, no? Is there any advantage to make couple with nr_cpu_ids?

I think it is a tradeoff between safer and cleaner :). infact, that's how I had coded the patch first. But, then I changed it to be in sync with nr_cpu_ids as it seemed a bit cleaner (and also to make sure 2048 CPU guys won't come after me for doing the mask calculation 2048 times during the boot).

#11 lore raw
Quoted 20 lines · Venki Pallipadi
Quoted 18 lines · KOSAKI Motohiro
Quoted 17 lines
Quoted 10 lines

kernel/cpu.c static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly; · line 630

Quoted 6 lines
kernel/cpu.c +3 -0
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
      if (possible)

Did you forget to add:

Quoted 3 lines
       nr_possible_cpus = cpumask_weight(cpu_possible_mask);

inside set_cpu_possible() ?

No. That was intentional as I have that coupled with nr_cpu_ids and set once after all the bits are set in setup_nr_cpu_ids() instead of doing for each bit set.

Quoted 9 lines
Quoted 2 lines

But, Srivatsa's way seems more safer, no? Is there any advantage to make couple with nr_cpu_ids?

I think it is a tradeoff between safer and cleaner :). infact, that's how I had coded the patch first. But, then I changed it to be in sync with nr_cpu_ids as it seemed a bit cleaner (and also to make sure 2048 CPU guys won't come after me for doing the mask calculation 2048 times during the boot).

I knew you were trying to optimize further when I saw your patch. That's precisely the reason I cross-checked the code to ensure that the optimization didn't go beyond correctness :)

And this is what I found:

start_kernel()
setup_nr_cpu_ids() // This is not the end of setting up cpu_possible_mask rest_init()

    kernel_init()
      smp_prepare_cpus();

And on x86, this becomes:

      native_smp_prepare_cpus();
        smp_sanity_check(); // cpu_possible_mask & nr_cpu_ids can change here!
           ^^^^^^^^^

And there is another place where things can change:
prefill_possible_map(). But this is called in setup_arch(), which is called before setup_nr_cpu_ids(). So we need not worry about this.

(Btw, I checked only the x86 arch. Not sure how other architectures handle things.)

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

#12 lore raw
Quoted 45 lines · Srivatsa S. Bhat
Quoted 20 lines · Venki Pallipadi
Quoted 18 lines · KOSAKI Motohiro
Quoted 17 lines
Quoted 10 lines
Quoted 6 lines
kernel/cpu.c +3 -0
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+

 void set_cpu_possible(unsigned int cpu, bool possible) {
      if (possible)

Did you forget to add:

Quoted 3 lines

       nr_possible_cpus = cpumask_weight(cpu_possible_mask);

inside set_cpu_possible() ?

No. That was intentional as I have that coupled with nr_cpu_ids and set once after all the bits are set in setup_nr_cpu_ids() instead of doing for each bit set.

Quoted 9 lines
Quoted 2 lines

But, Srivatsa's way seems more safer, no? Is there any advantage to make couple with nr_cpu_ids?

I think it is a tradeoff between safer and cleaner :). infact, that's how I had coded the patch first. But, then I changed it to be in sync with nr_cpu_ids as it seemed a bit cleaner (and also to make sure 2048 CPU guys won't come after me for doing the mask calculation 2048 times during the boot).

I knew you were trying to optimize further when I saw your patch. That's precisely the reason I cross-checked the code to ensure that the optimization didn't go beyond correctness :)

And this is what I found:

start_kernel()
 setup_nr_cpu_ids() // This is not the end of setting up cpu_possible_mask rest_init()
   kernel_init()
     smp_prepare_cpus();

Quoted 5 lines

And on x86, this becomes:
     native_smp_prepare_cpus();
       smp_sanity_check(); // cpu_possible_mask & nr_cpu_ids can change here! ^^^^^^^^^

Ack. Thanks for pointing this out. I missed this in my testing as this is under if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)

Proper way to handle this would be to call
setup_nr_cpu_ids();
After set_cpu_possible() loop.

I did a grep for any other place where nr_cpu_ids may be getting written. And only other place I find arch/powerpc/platforms/iseries/setup.c: nr_cpu_ids = max(nr_cpu_ids, 64); but it does not have a accompanying set_cpu_possible(). So, the change here should not affect this.

-Venki

Quoted 11 lines

And there is another place where things can change:
prefill_possible_map(). But this is called in setup_arch(), which is called before setup_nr_cpu_ids(). So we need not worry about this.

(Btw, I checked only the x86 arch. Not sure how other architectures handle things.)

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

#13 lore raw

PATCH 1/1 Avoid mask based num_possible_cpus and num_online_cpus -v3

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 arch/x86/kernel/smpboot.c |    2 +-
 include/linux/cpumask.h   |   10 ++++++++--
 kernel/cpu.c              |    5 +++++
 kernel/smp.c              |    4 ++++
 4 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +1 -1
index 66d250c..f87fcde 100644
@@ -947,7 +947,7 @@ static int __init smp_sanity_check(unsigned max_cpus)
 			nr++;
 		}
 
-		nr_cpu_ids = 8;
+		setup_nr_cpu_ids();
 	}
 #endif
 
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
 #if NR_CPUS == 1
 #define nr_cpu_ids		1
+#define nr_possible_cpus	1
 #else
 extern int nr_cpu_ids;
+extern int nr_possible_cpus;
 #endif
 
+extern int nr_online_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +5 -0
index 2060c6e..f179baa 100644
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
 EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
 	if (possible)
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
kernel/smp.c +4 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
 int nr_cpu_ids __read_mostly = NR_CPUS;
 EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;
+EXPORT_SYMBOL(nr_possible_cpus);
+
 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
 void __init setup_nr_cpu_ids(void)
 {
 	nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 /* Called by boot processor to activate the rest. */
Signature · 1 line
1.7.7.3
#14 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v3

Quoted 108 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 arch/x86/kernel/smpboot.c |    2 +-
 include/linux/cpumask.h   |   10 ++++++++--
 kernel/cpu.c              |    5 +++++
 kernel/smp.c              |    4 ++++
 4 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +1 -1
index 66d250c..f87fcde 100644
@@ -947,7 +947,7 @@ static int __init smp_sanity_check(unsigned max_cpus)
 			nr++;
 		}
 
-		nr_cpu_ids = 8;
+		setup_nr_cpu_ids();
 	}
 #endif
 
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
 #if NR_CPUS == 1
 #define nr_cpu_ids		1
+#define nr_possible_cpus	1
 #else
 extern int nr_cpu_ids;
+extern int nr_possible_cpus;
 #endif
 
+extern int nr_online_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +5 -0
index 2060c6e..f179baa 100644
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
 EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 void set_cpu_possible(unsigned int cpu, bool possible)
 {
 	if (possible)
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
kernel/smp.c +1 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
 int nr_cpu_ids __read_mostly = NR_CPUS;
 EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;

kernel/smp.c early_param("maxcpus", maxcpus); · line 671

Quoted 10 lines
kernel/smp.c +3 -0
+EXPORT_SYMBOL(nr_possible_cpus);
+
 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
 void __init setup_nr_cpu_ids(void)
 {
 	nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 /* Called by boot processor to activate the rest. */

This patch still has problems, IMHO.

A quick grep on the source for set_cpu_possible() revealed the following problematic areas:
1. arch/alpha/kernel/process.c: common_shutdown_1() set_cpu_possible() with second parameter as 'false' is used here. Though this is the shutdown code, a disparity between what num_possible_cpus() reports and what is actually there in cpu_possible_mask is not such a good idea, at any point in time.

2. arch/cris/arch-v32/kernel/smp.c: smp_prepare_boot_cpu() smp_prepare_boot_cpu() is called after setup_nr_cpu_ids().

(Btw, I think things are really messed up in cris as it is, if I am not totally mistaken).

3. arch/mn10300/kernel/smp.c: smp_prepare_cpus()
This function calls set_cpu_possible(). smp_prepare_cpus() is called in kernel_init(), which is much later than setup_nr_cpu_ids().

4. arch/um/kernel/smp.c: smp_prepare_cpus()
Same as in 3.

5. As far as arch/x86/xen is concerned, I can see code such as the following, among other things:

    xen_smp_prepare_cpus():

    /* Restrict the possible_map according to max_cpus. */
    while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
            for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
                    continue;
            set_cpu_possible(cpu, false);
    }

If I am not missing anything obvious, applying this patch can effectively convert the above code into an infinite loop, among other damages!

I still feel it would be safer to edit set_cpu_possible() such that nr_possible_cpus is updated whenever cpu_possible_mask is altered.

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

#15 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v3

Quoted 5 lines

If I am not missing anything obvious, applying this patch can effectively convert the above code into an infinite loop, among other damages!

I still feel it would be safer to edit set_cpu_possible() such that nr_possible_cpus is updated whenever cpu_possible_mask is altered.

Yup, I agree with you. The important thing is, even if we use set_cpu_possible(), x86 performance doesn't drop. So, I don't understand why we need to take current strategy.

#16 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v3

In reply to Srivatsa S. Bhat
Quoted 158 lines · Srivatsa S. Bhat
Quoted 108 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

 #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 arch/x86/kernel/smpboot.c |    2 +-
 include/linux/cpumask.h   |   10 ++++++++--
 kernel/cpu.c              |    5 +++++
 kernel/smp.c              |    4 ++++

 4 files changed, 18 insertions(+), 3 deletions(-)
arch/x86/kernel/smpboot.c +1 -1
index 66d250c..f87fcde 100644
@@ -947,7 +947,7 @@ static int __init smp_sanity_check(unsigned max_cpus)
                       nr++;
               }
 
-             nr_cpu_ids = 8;
+             setup_nr_cpu_ids();
       }
  #endif
 
include/linux/cpumask.h +8 -2
index 4f7a632..ac3113b 100644
@@ -23,10 +23,14 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 
  #if NR_CPUS == 1
  #define nr_cpu_ids           1
+#define nr_possible_cpus     1
  #else
  extern int nr_cpu_ids;
+extern int nr_possible_cpus;
  #endif
 
+extern int nr_online_cpus;
+
  #ifdef CONFIG_CPUMASK_OFFSTACK
  /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
   * not all bits may be allocated. */
@@ -81,8 +85,10 @@ extern const struct cpumask *const cpu_present_mask;
  extern const struct cpumask *const cpu_active_mask;
 
  #if NR_CPUS > 1
-#define num_online_cpus()    cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()  cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()    (nr_online_cpus)
+#define num_possible_cpus()  (nr_possible_cpus)
+
  #define num_present_cpus()   cpumask_weight(cpu_present_mask)
  #define num_active_cpus()    cpumask_weight(cpu_active_mask)
  #define cpu_online(cpu)              cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +3 -0
index 2060c6e..f179baa 100644
@@ -622,6 +622,9 @@ static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
  const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
  EXPORT_SYMBOL(cpu_active_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
  void set_cpu_possible(unsigned int cpu, bool possible)
  {
       if (possible)
@@ -644,6 +647,8 @@ void set_cpu_online(unsigned int cpu, bool online)
               cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));

      else
              cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits)); +
+     nr_online_cpus = cpumask_weight(cpu_online_mask); }

 void set_cpu_active(unsigned int cpu, bool active)

kernel/smp.c +1 -0
index db197d6..106e519 100644
@@ -658,10 +658,14 @@ early_param("maxcpus", maxcpus);
  int nr_cpu_ids __read_mostly = NR_CPUS;
  EXPORT_SYMBOL(nr_cpu_ids);
 
+int nr_possible_cpus __read_mostly = NR_CPUS;

kernel/smp.c early_param("maxcpus", maxcpus); · line 671

Quoted 10 lines
kernel/smp.c +3 -0
+EXPORT_SYMBOL(nr_possible_cpus);
+
  /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
  void __init setup_nr_cpu_ids(void)
  {
       nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
+     nr_possible_cpus = cpumask_weight(cpu_possible_mask);
  }
 
  /* Called by boot processor to activate the rest. */

This patch still has problems, IMHO.

A quick grep on the source for set_cpu_possible() revealed the following problematic areas:
1. arch/alpha/kernel/process.c: common_shutdown_1() set_cpu_possible() with second parameter as 'false' is used here. Though this is the shutdown code, a disparity between what num_possible_cpus() reports and what is actually there in cpu_possible_mask is not such a good idea, at any point in time.

2. arch/cris/arch-v32/kernel/smp.c: smp_prepare_boot_cpu() smp_prepare_boot_cpu() is called after setup_nr_cpu_ids().

  (Btw, I think things are really messed up in cris as it is, if I am not totally mistaken).

3. arch/mn10300/kernel/smp.c: smp_prepare_cpus()
  This function calls set_cpu_possible(). smp_prepare_cpus() is called in kernel_init(), which is much later than setup_nr_cpu_ids().

4. arch/um/kernel/smp.c: smp_prepare_cpus()
  Same as in 3.

5. As far as arch/x86/xen is concerned, I can see code such as the following, among other things:

   xen_smp_prepare_cpus():

   /* Restrict the possible_map according to max_cpus. */ while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) { for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--) continue;
           set_cpu_possible(cpu, false);
   }

Quoted 6 lines

If I am not missing anything obvious, applying this patch can effectively convert the above code into an infinite loop, among other damages!

I still feel it would be safer to edit set_cpu_possible() such that nr_possible_cpus is updated whenever cpu_possible_mask is altered.

With the xen example above you have easily convinced me to not mess with this code too much.
Will go back to the drawing board looking for simplest possible change. Thanks again for being vigilant about this.

-Venki

Quoted 4 lines

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

#17 lore raw

PATCH 1/1 Avoid mask based num_possible_cpus and num_online_cpus -v4

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |    9 +++++++--
 kernel/cpu.c            |   13 +++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
include/linux/cpumask.h +7 -2
index 4f7a632..ec0f6c9 100644
@@ -27,6 +27,9 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 extern int nr_cpu_ids;
 #endif
 
+extern int nr_online_cpus;
+extern int nr_possible_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +84,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +13 -0
index 2060c6e..58894e0 100644
@@ -604,16 +604,23 @@ EXPORT_SYMBOL(cpu_all_bits);
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
 	= CPU_BITS_ALL;
+int nr_possible_cpus __read_mostly = NR_CPUS;
 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+int nr_possible_cpus __read_mostly;
 #endif
 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
 EXPORT_SYMBOL(cpu_possible_mask);
 
+EXPORT_SYMBOL(nr_possible_cpus);
+
 static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
 EXPORT_SYMBOL(cpu_online_mask);
 
+int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
 EXPORT_SYMBOL(cpu_present_mask);
@@ -628,6 +635,8 @@ void set_cpu_possible(unsigned int cpu, bool possible)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
+
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 void set_cpu_present(unsigned int cpu, bool present)
@@ -644,6 +653,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
@@ -662,9 +673,11 @@ void init_cpu_present(const struct cpumask *src)
 void init_cpu_possible(const struct cpumask *src)
 {
 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 void init_cpu_online(const struct cpumask *src)
 {
 	cpumask_copy(to_cpumask(cpu_online_bits), src);
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
Signature · 1 line
1.7.7.3
Acked-by on v1 1/1
#18 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v4

Quoted 19 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Quoted 6 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---

Looks good to me now :-)

Regards,
Srivatsa S. Bhat

Acked-by on v1 1/1
#19 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v4

In reply to Venkatesh Pallipadi
Quoted 19 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Quoted 6 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---

Looks good to me now :-)

Regards,
Srivatsa S. Bhat

#20 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v4

2012/1/26 Srivatsa S. Bhat <[email protected]>:

Quoted 25 lines · Venkatesh Pallipadi
Quoted 24 lines

Kernel's notion of possible cpus (from include/linux/cpumask.h)
 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

 #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Quoted 7 lines
Quoted 2 lines

---

Looks good to me now :-)

I'm ok too.

#21 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v4

In reply to Venkatesh Pallipadi
Quoted 32 lines · On Tue, 24 Jan 2012 15:25:41 -0800 Venkatesh Pallipadi <[email protected]> wrote:

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

...

include/linux/cpumask.h +3 -0
+extern int nr_online_cpus;
+extern int nr_possible_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */

include/linux/cpumask.h extern const struct cpumask *const cpu_present_mask; · line 89

Quoted 8 lines
include/linux/cpumask.h +3 -2
@@ -81,8 +84,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)

This changes the return types from "unsigned int" to int. Worse, the return types become dependent upon CONFIG_SMP.

s/int/unsigned int/g, methinks.

kernel/cpu.c EXPORT_SYMBOL(cpu_all_bits); · line 607

Quoted 10 lines

...

kernel/cpu.c +1 -0
@@ -604,16 +604,23 @@ EXPORT_SYMBOL(cpu_all_bits);
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
 	= CPU_BITS_ALL;
+int nr_possible_cpus __read_mostly = NR_CPUS;

It looks strange to see cpu_possible_bits using CONFIG_NR_CPUS whereas nr_possible_cpus uses NR_CPUS. I suggest using CONFIG_NR_CPUS for both.

Aside: that FIXME in include/linux/threads.h should get fixed - it's stupid. We should fix the Kconfigs.

And the legacy NR_CPUS should be banished from the kernel altogether.

kernel/cpu.c EXPORT_SYMBOL(cpu_all_bits); · line 615

Quoted 8 lines
kernel/cpu.c +2 -0
 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+int nr_possible_cpus __read_mostly;
 #endif
 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
 EXPORT_SYMBOL(cpu_possible_mask);
 
+EXPORT_SYMBOL(nr_possible_cpus);

It's better to place the export immediately following the nr_possible_cpus definition(s).

#22 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v4

Quoted 47 lines · Andrew Morton
Quoted 32 lines · On Tue, 24 Jan 2012 15:25:41 -0800 Venkatesh Pallipadi <[email protected]> wrote:

Kernel's notion of possible cpus (from include/linux/cpumask.h)
 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

 #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

...

include/linux/cpumask.h +3 -0
+extern int nr_online_cpus;
+extern int nr_possible_cpus;
+

 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */

include/linux/cpumask.h extern const struct cpumask *const cpu_present_mask; · line 89

Quoted 8 lines
include/linux/cpumask.h +3 -2
@@ -81,8 +84,10 @@ extern const struct cpumask *const cpu_present_mask;
  extern const struct cpumask *const cpu_active_mask;
 
  #if NR_CPUS > 1
-#define num_online_cpus()    cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()  cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()    (nr_online_cpus)
+#define num_possible_cpus()  (nr_possible_cpus)

This changes the return types from "unsigned int" to int.  Worse, the return types become dependent upon CONFIG_SMP.

Quoted 2 lines

s/int/unsigned int/g, methinks.

It will be kind of confusing with nr_cpu_ids being int and online or possible_cpus being unsigned int. But, I agree that for the sake of existing callers, this should be unsigned int.

Quoted 15 lines
Quoted 10 lines

...

kernel/cpu.c +0 -0
@@ -604,16 +604,23 @@ EXPORT_SYMBOL(cpu_all_bits);
  #ifdef CONFIG_INIT_ALL_POSSIBLE

 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly = CPU_BITS_ALL;
+int nr_possible_cpus __read_mostly = NR_CPUS;

It looks strange to see cpu_possible_bits using CONFIG_NR_CPUS whereas nr_possible_cpus uses NR_CPUS.  I suggest using CONFIG_NR_CPUS for both.

OK. Will change. I saw NR_CPUS generously sprinkled around in include/linux/cpumask.h and thought that was the preferred one to use.

Quoted 5 lines

Aside: that FIXME in include/linux/threads.h should get fixed - it's stupid.  We should fix the Kconfigs.

And the legacy NR_CPUS should be banished from the kernel altogether.

Silly-grep shows

$ git grep NR_CPUS | grep -v CONFIG_NR_CPUS | wc -l
748
$ git grep CONFIG_NR_CPUS | wc -l
57

So, that will involve a huge patch :-).

Quoted 11 lines
Quoted 8 lines

 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly; +int nr_possible_cpus __read_mostly;
 #endif
 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits); EXPORT_SYMBOL(cpu_possible_mask);

+EXPORT_SYMBOL(nr_possible_cpus);

It's better to place the export immediately following the nr_possible_cpus definition(s).

OK. Will do.

Thanks,
Venki

#23 lore raw

PATCH 1/1 Avoid mask based num_possible_cpus and num_online_cpus -v5

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

---
 include/linux/cpumask.h |    9 +++++++--
 kernel/cpu.c            |   13 +++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
include/linux/cpumask.h +7 -2
index 4f7a632..930e255 100644
@@ -27,6 +27,9 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
 extern int nr_cpu_ids;
 #endif
 
+extern unsigned int nr_online_cpus;
+extern unsigned int nr_possible_cpus;
+
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
  * not all bits may be allocated. */
@@ -81,8 +84,10 @@ extern const struct cpumask *const cpu_present_mask;
 extern const struct cpumask *const cpu_active_mask;
 
 #if NR_CPUS > 1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +13 -0
index 2060c6e..d520d34 100644
@@ -604,9 +604,13 @@ EXPORT_SYMBOL(cpu_all_bits);
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
 	= CPU_BITS_ALL;
+unsigned int nr_possible_cpus __read_mostly = CONFIG_NR_CPUS;
 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+unsigned int nr_possible_cpus __read_mostly;
 #endif
+EXPORT_SYMBOL(nr_possible_cpus);
+
 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
 EXPORT_SYMBOL(cpu_possible_mask);
 
@@ -614,6 +618,9 @@ static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
 EXPORT_SYMBOL(cpu_online_mask);
 
+unsigned int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
 static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
 const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
 EXPORT_SYMBOL(cpu_present_mask);
@@ -628,6 +635,8 @@ void set_cpu_possible(unsigned int cpu, bool possible)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
+
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 void set_cpu_present(unsigned int cpu, bool present)
@@ -644,6 +653,8 @@ void set_cpu_online(unsigned int cpu, bool online)
 		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
 	else
 		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
 
 void set_cpu_active(unsigned int cpu, bool active)
@@ -662,9 +673,11 @@ void init_cpu_present(const struct cpumask *src)
 void init_cpu_possible(const struct cpumask *src)
 {
 	cpumask_copy(to_cpumask(cpu_possible_bits), src);
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
 }
 
 void init_cpu_online(const struct cpumask *src)
 {
 	cpumask_copy(to_cpumask(cpu_online_bits), src);
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
 }
Signature · 1 line
1.7.7.3
#24 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 15 lines · On Tue, 31 Jan 2012 16:17:19 -0800 Venkatesh Pallipadi <[email protected]> wrote:

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.

#define num_possible_cpus() cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

Quoted 7 lines

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Looks good to me.

kernel/cpu.c EXPORT_SYMBOL(cpu_all_bits); · line 612

Quoted 12 lines
kernel/cpu.c +3 -0
@@ -604,9 +604,13 @@ EXPORT_SYMBOL(cpu_all_bits);
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
 	= CPU_BITS_ALL;
+unsigned int nr_possible_cpus __read_mostly = CONFIG_NR_CPUS;
 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+unsigned int nr_possible_cpus __read_mostly;
 #endif
+EXPORT_SYMBOL(nr_possible_cpus);

What the heck is CONFIG_INIT_ALL_POSSIBLE?

<blames Rusty>

: 1) Some archs (m32, parisc, s390) set possible_map to all 1, so we add a
: CONFIG_INIT_ALL_POSSIBLE for this rather than break them.

Seems strange. Do these architectures really need to initialise cpu_possible_map at compile-time, when all the other architectures manage to do it at runtime?

#25 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 5 lines · Andrew Morton
Quoted 3 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Looks good to me.

But, I wonder who's asking num_possible_cpus(). It's not a very useful thing to know, though some arch's "know" it's contiguous, so can cheat.

Optimizing it seems particularly foolish. We either audit and wean everyone off who's using it incorrectly, or insist on contiguous CPU numbers and drop the mask altogether.

Quoted 15 lines

kernel/cpu.c EXPORT_SYMBOL(cpu_all_bits); · line 612

Quoted 12 lines
kernel/cpu.c +3 -0
@@ -604,9 +604,13 @@ EXPORT_SYMBOL(cpu_all_bits);
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
 	= CPU_BITS_ALL;
+unsigned int nr_possible_cpus __read_mostly = CONFIG_NR_CPUS;
 #else
 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+unsigned int nr_possible_cpus __read_mostly;
 #endif
+EXPORT_SYMBOL(nr_possible_cpus);

What the heck is CONFIG_INIT_ALL_POSSIBLE?

Quoted 8 lines

<blames Rusty>

: 1) Some archs (m32, parisc, s390) set possible_map to all 1, so we add a
: CONFIG_INIT_ALL_POSSIBLE for this rather than break them.

Seems strange. Do these architectures really need to initialise cpu_possible_map at compile-time, when all the other architectures manage to do it at runtime?

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

Cheers,
Rusty.

#26 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 11 lines · On Fri, 03 Feb 2012 06:33:02 +1030 Rusty Russell <[email protected]> wrote:
Quoted 5 lines · Andrew Morton
Quoted 3 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Looks good to me.

But, I wonder who's asking num_possible_cpus(). It's not a very useful thing to know, though some arch's "know" it's contiguous, so can cheat.

Optimizing it seems particularly foolish.

We're fools for optimisations!

Quoted 3 lines

We either audit and wean
everyone off who's using it incorrectly, or insist on contiguous CPU numbers and drop the mask altogether.

drivers/block/nvme.c looks like it's assuming a contiguous map. Maybe also drivers/scsi/bnx2fc (wtf?). I didn't see anything else outside arch code.

#27 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Fri, 03 Feb 2012 06:33:02 +1030 Rusty Russell <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
Quoted 3 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

Looks good to me.

But, I wonder who's asking num_possible_cpus().  It's not a very useful thing to know, though some arch's "know" it's contiguous, so can cheat.

Optimizing it seems particularly foolish.

We're fools for optimisations!

I would think that if we are giving an API people will abuse it sooner or later :-).

Quoted 8 lines
Quoted 3 lines

We either audit and wean
everyone off who's using it incorrectly, or insist on contiguous CPU numbers and drop the mask altogether.

drivers/block/nvme.c looks like it's assuming a contiguous map.  Maybe also drivers/scsi/bnx2fc (wtf?).  I didn't see anything else outside arch code.

Yes. I found a bunch of them which seemed obviously wrong. Doing things like allocating space based on num_possible_cpus() and accessing the space with get_cpu() or doing cou_online() check etc.

arch/x86/kernel/apic/x2apic_uv_x.c
arch/x86/kernel/cpu/mcheck/mce-inject.c
arch/x86/platform/uv/tlb_uv.c
drivers/scsi/bnx2fc/bnx2fc_io.c
kernel/debug/kdb/kdb_bt.c

I have a patch to fix these obvious ones. But, there are other users which were not very obvious to me and also I am know of code in older kernels (code which since have been rewritten) which can get benefit of this API change.

Thanks,
Venki

#28 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

In reply to Rusty Russell
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:

[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay. Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

-Tony

#29 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 22 lines · Tony Luck
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:
[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay.  Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

Quoted 6 lines

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

-Tony

What is NR_CPUS set to here? And how many actual CPUs are there on this machine?

#30 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

In reply to Tony Luck
Quoted 19 lines · Tony Luck
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:

[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay. Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

Quoted 8 lines
[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

Oh no!! ia64 directly uses cpu_set() and cpu_clear() on cpu_online_map!! Grr.. It means num_online_cpus can be different from the actual number of online cpus because it doesn't go through the set_cpu_online() path.. I haven't yet pin-pointed the exact problem, but this definitely doesn't look good...

Regards,
Srivatsa S. Bhat

#31 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 31 lines · Srivatsa S. Bhat
Quoted 19 lines · Tony Luck
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:
[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay.  Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

Quoted 8 lines

[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

Quoted 5 lines

Oh no!! ia64 directly uses cpu_set() and cpu_clear() on cpu_online_map!! Grr.. It means num_online_cpus can be different from the actual number of online cpus because it doesn't go through the set_cpu_online() path.. I haven't yet pin-pointed the exact problem, but this definitely doesn't look good...

This feels like a minefield in general. ia64, mips and um seems to have cpu_set and cpu_clear of cpu_online_map and/or cpu_possible_map in there.

#32 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 39 lines · Venki Pallipadi
Quoted 31 lines · Srivatsa S. Bhat
Quoted 19 lines · Tony Luck
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:

[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay. Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

Quoted 8 lines
[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

Quoted 5 lines

Oh no!! ia64 directly uses cpu_set() and cpu_clear() on cpu_online_map!! Grr.. It means num_online_cpus can be different from the actual number of online cpus because it doesn't go through the set_cpu_online() path.. I haven't yet pin-pointed the exact problem, but this definitely doesn't look good...

Quoted 4 lines

This feels like a minefield in general. ia64, mips and um seems to have cpu_set and cpu_clear of cpu_online_map and/or cpu_possible_map in there.

Since I had almost never seen code using "cpu_online_map" instead of "cpu_online_mask", I hadn't checked it while reviewing your patch... :-( Honestly, it is only now that I realized that there is this other variant too!

Regards,
Srivatsa S. Bhat

#33 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

In reply to Srivatsa S. Bhat
Quoted 31 lines · Srivatsa S. Bhat
Quoted 19 lines · Tony Luck
Quoted 2 lines · Rusty Russell

IIRC playing with 3 archs boot code seemed like a recipe for disaster. Feel free to try to fix this in -next though, and see what breaks...

ia64 is what breaks ... well not actually broken ... but some very weird delays that
show up in different places depending on whether this patch is present.

First linux-next kernel to be blessed with this patch was next-20120210. Booting it
I see:

[    7.164233] Switching to clocksource itc
[  146.077315] pnp: PnP ACPI init

An ugly 138.913 second delay. Digging in the code showed that the bad bits happened inside stop_machine()

Reverting just this patch makes this big delay disappear:

Quoted 8 lines
[   32.780232] Switching to clocksource itc
[   32.832100] pnp: PnP ACPI init

but notice that it takes 25 extra seconds to get to this point in the boot (and while
we expect to save some time by not re-computing num_online_cpus each time we need it ... this looks to be a lot more than I'd expect!)

Quoted 5 lines

Oh no!! ia64 directly uses cpu_set() and cpu_clear() on cpu_online_map!! Grr.. It means num_online_cpus can be different from the actual number of online cpus because it doesn't go through the set_cpu_online() path.. I haven't yet pin-pointed the exact problem, but this definitely doesn't look good...

Hmm.. interesting.. The only calls that ia64 uses which updates the num_online_cpus macro seem to be init_cpu_online(cpumask_of(0)); Atleast this is what the mainline code tells me (haven't checked linux-next).

So, if I am not mistaken, is the value of num_online_cpus() always 1 when Venki's patch is applied?

IOW, what output do you see from the following printk from arch/ia64/kernel/smpboot.c?

printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",

         (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);

Regards,
Srivatsa S. Bhat

#34 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 5 lines · Srivatsa S. Bhat

IOW, what output do you see from the following printk from arch/ia64/kernel/smpboot.c?

printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n", (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);

That is a complicated question - because linux-next also has patches by Arjan that
change how (when) cpus are brought online. Initially I blamed his patches and tried
reverting them ... and saw the symptom you are wondering about (message said "Total of 1 processors", but the BogoMIPs was a number big enough to be all of them. Thanks to you, I can now understand why.

Fix will be to stop ia64 from messing directly with cpu_online_map?

-Tony

#35 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 16 lines · Tony Luck
Quoted 5 lines · Srivatsa S. Bhat

IOW, what output do you see from the following printk from arch/ia64/kernel/smpboot.c?

printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n", (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);

That is a complicated question - because linux-next also has patches by Arjan that
change how (when) cpus are brought online. Initially I blamed his patches and tried
reverting them ... and saw the symptom you are wondering about (message said "Total of 1 processors", but the BogoMIPs was a number big enough to be all of them. Thanks to you, I can now understand why.

Quoted 1 line

Fix will be to stop ia64 from messing directly with cpu_online_map?

Yes, and the other architectures.

We're well within reach of removing cpu_*_map now I think.

Cheers,
Rusty.

#36 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 17 lines · Rusty Russell
Quoted 16 lines · Tony Luck
Quoted 5 lines · Srivatsa S. Bhat

IOW, what output do you see from the following printk from arch/ia64/kernel/smpboot.c?

printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",

        (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);

That is a complicated question - because linux-next also has patches by Arjan that
change how (when) cpus are brought online. Initially I blamed his patches and tried
reverting them ... and saw the symptom you are wondering about (message said "Total of 1 processors", but the BogoMIPs was a number big enough to be all of them. Thanks to you, I can now understand why.

Quoted 4 lines
Quoted 1 line

Fix will be to stop ia64 from messing directly with cpu_online_map?

Yes, and the other architectures.

Right. And we should also ensure that nobody messes directly with cpu_possible_map as well. I have written up a patch for ia64 (see below). Sorry, I haven't even compile tested it - I neither have the toolchain nor the hardware. I hope it works!

But don't expect the above printk statement to print the right value if you are running a kernel which has the patch posted at https://lkml.org/lkml/2012/1/31/286
applied. That is another patch that can alter boot-up time and many related things. So, it would be best to test Venki's patch + following fix without having Arjan's patch applied.

---
From: Srivatsa S. Bhat <[email protected]>
Subject: [PATCH] ia64: Don't use cpu_set()/cpu_clear() over cpu_[online|possible]_map

Directly using cpu_set() and cpu_clear() on cpu_online_map or cpu_possible_map is strongly discouraged. Use the functions set_cpu_online() and set_cpu_possible() instead. This also means that the new implementation of num_[online|possible]_cpus can track all changes to cpu_[online|possible]_mask and hence give the correct results always.

---

 0 files changed, 0 insertions(+), 0 deletions(-)
arch/ia64/kernel/setup.c +1 -1
index cd57d73..4d1a550 100644
@@ -486,7 +486,7 @@ mark_bsp_online (void)
 {
 #ifdef CONFIG_SMP
 	/* If we register an early console, allow CPU 0 to printk */
-	cpu_set(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), true);
 #endif
 }
 
arch/ia64/kernel/smp.c +1 -1
index 0bd537b..8551979 100644
@@ -77,7 +77,7 @@ stop_this_cpu(void)
 	/*
 	 * Remove this CPU:
 	 */
-	cpu_clear(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), false);
 	max_xtp();
 	local_irq_disable();
 	cpu_halt();
arch/ia64/kernel/smpboot.c +6 -6
index 5590979..35f6e3c 100644
@@ -401,7 +401,7 @@ smp_callin (void)
 	/* Setup the per cpu irq handling data structures */
 	__setup_vector_irq(cpuid);
 	notify_cpu_starting(cpuid);
-	cpu_set(cpuid, cpu_online_map);
+	set_cpu_online(cpuid, true);
 	per_cpu(cpu_state, cpuid) = CPU_ONLINE;
 	spin_unlock(&vector_lock);
 	ipi_call_unlock_irq();
@@ -548,7 +548,7 @@ do_rest:
 	if (!cpu_isset(cpu, cpu_callin_map)) {
 		printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
 		ia64_cpu_to_sapicid[cpu] = -1;
-		cpu_clear(cpu, cpu_online_map);  /* was set in smp_callin() */
+		set_cpu_online(cpu, false);  /* was set in smp_callin() */
 		return -EINVAL;
 	}
 	return 0;
@@ -609,7 +609,7 @@ smp_prepare_cpus (unsigned int max_cpus)
 	/*
 	 * We have the boot CPU online for sure.
 	 */
-	cpu_set(0, cpu_online_map);
+	set_cpu_online(0, true);
 	cpu_set(0, cpu_callin_map);
 
 	local_cpu_data->loops_per_jiffy = loops_per_jiffy;
@@ -633,7 +633,7 @@ smp_prepare_cpus (unsigned int max_cpus)
 
 void __devinit smp_prepare_boot_cpu(void)
 {
-	cpu_set(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), true);
 	cpu_set(smp_processor_id(), cpu_callin_map);
 	set_numa_node(cpu_to_node_map[smp_processor_id()]);
 	per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
@@ -732,10 +732,10 @@ int __cpu_disable(void)
 			return -EBUSY;
 	}
 
-	cpu_clear(cpu, cpu_online_map);
+	set_cpu_online(cpu, false);
 
 	if (migrate_platform_irqs(cpu)) {
-		cpu_set(cpu, cpu_online_map);
+		set_cpu_online(cpu, true);
 		return -EBUSY;
 	}
 
Tested-by
#37 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 4 lines · Srivatsa S. Bhat

Right. And we should also ensure that nobody messes directly with cpu_possible_map as well. I have written up a patch for ia64 (see below). Sorry, I haven't even compile tested it - I neither have the toolchain nor the hardware. I hope it works!

Thanks for doing this - compiles and seems to work.

Can we get this added to the series - so it gets applied along with Venki's patch.

Quoted 1 line
 0 files changed, 0 insertions(+), 0 deletions(-)

I think your patch generation script needs some attention - I see

 arch/ia64/kernel/setup.c   |    2 +-
 arch/ia64/kernel/smp.c     |    2 +-
 arch/ia64/kernel/smpboot.c |   12 ++++++------
 3 files changed, 8 insertions(+), 8 deletions(-)

arch/ia64/kernel/smpboot.c smp_prepare_cpus (unsigned int max_cpus) · line 615

Quoted 9 lines
arch/ia64/kernel/smpboot.c +1 -1
@@ -609,7 +609,7 @@ smp_prepare_cpus (unsigned int max_cpus)
        /*
         * We have the boot CPU online for sure.
         */
-       cpu_set(0, cpu_online_map);
+       set_cpu_online(0, true);
        cpu_set(0, cpu_callin_map);
 
        local_cpu_data->loops_per_jiffy = loops_per_jiffy;

Generic code has already marked cpu 0 online ... so this one could just be dropped (and the preceding comment too). Though it does no harm to set it again.

-Tony

#38 lore raw

cover Cleanup raw handling of online/possible map

In reply to Rusty Russell
Quoted 1 line

Yes, and the other architectures.

Here are the patches for other instances I see in plain git grep.

I have been brave (foolish) enough to send this without any testing. So, this comes with 'use it at your own risk' tag :-).

Thanks,
Venki

#39 lore raw

PATCH 1/3 hexagon: Avoid raw handling of cpu_possible_map

Use set_cpu_possible instead.

---
 arch/hexagon/kernel/smp.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/hexagon/kernel/smp.c +1 -1
index c871a2c..8962705 100644
@@ -272,5 +272,5 @@ void smp_start_cpus(void)
 	int i;
 
 	for (i = 0; i < NR_CPUS; i++)
-		cpu_set(i, cpu_possible_map);
+		set_cpu_possible(i, true);
 }
Signature · 1 line
1.7.7.3
#40 lore raw

PATCH 2/3 mips: Avoid raw handling of cpu_possible_map/cpu_online_map

In reply to Venkatesh Pallipadi

Use set_cpu_* and init_cpu_* variants instead.

---
 arch/mips/cavium-octeon/smp.c       |    2 +-
 arch/mips/kernel/smp.c              |    4 ++--
 arch/mips/netlogic/xlr/smp.c        |    4 ++--
 arch/mips/pmc-sierra/yosemite/smp.c |    4 ++--
 arch/mips/sgi-ip27/ip27-smp.c       |    2 +-
 arch/mips/sibyte/bcm1480/smp.c      |    5 ++---
 arch/mips/sibyte/sb1250/smp.c       |    5 ++---
 7 files changed, 12 insertions(+), 14 deletions(-)
arch/mips/cavium-octeon/smp.c +1 -1
index efcfff4..5cce09c 100644
@@ -268,7 +268,7 @@ static int octeon_cpu_disable(void)
 
 	spin_lock(&smp_reserve_lock);
 
-	cpu_clear(cpu, cpu_online_map);
+	set_cpu_online(cpu, false);
 	cpu_clear(cpu, cpu_callin_map);
 	local_irq_disable();
 	fixup_irqs();
arch/mips/kernel/smp.c +2 -2
index 32c1e95..28777ff 100644
@@ -148,7 +148,7 @@ static void stop_this_cpu(void *dummy)
 	/*
 	 * Remove this CPU:
 	 */
-	cpu_clear(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), false);
 	for (;;) {
 		if (cpu_wait)
 			(*cpu_wait)();		/* Wait if available. */
@@ -248,7 +248,7 @@ int __cpuinit __cpu_up(unsigned int cpu)
 	while (!cpu_isset(cpu, cpu_callin_map))
 		udelay(100);
 
-	cpu_set(cpu, cpu_online_map);
+	set_cpu_online(cpu, true);
 
 	return 0;
 }
arch/mips/netlogic/xlr/smp.c +2 -2
index 080284d..8084221 100644
@@ -154,7 +154,7 @@ void __init nlm_smp_setup(void)
 	cpu_set(boot_cpu, phys_cpu_present_map);
 	__cpu_number_map[boot_cpu] = 0;
 	__cpu_logical_map[0] = boot_cpu;
-	cpu_set(0, cpu_possible_map);
+	set_cpu_possible(0, true);
 
 	num_cpus = 1;
 	for (i = 0; i < NR_CPUS; i++) {
@@ -166,7 +166,7 @@ void __init nlm_smp_setup(void)
 			cpu_set(i, phys_cpu_present_map);
 			__cpu_number_map[i] = num_cpus;
 			__cpu_logical_map[num_cpus] = i;
-			cpu_set(num_cpus, cpu_possible_map);
+			set_cpu_possible(num_cpus, true);
 			++num_cpus;
 		}
 	}
arch/mips/pmc-sierra/yosemite/smp.c +2 -2
index 2608752..b2b23eb 100644
@@ -155,10 +155,10 @@ static void __init yos_smp_setup(void)
 {
 	int i;
 
-	cpus_clear(cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
 
 	for (i = 0; i < 2; i++) {
-		cpu_set(i, cpu_possible_map);
+		set_cpu_possible(i, true);
 		__cpu_number_map[i]	= i;
 		__cpu_logical_map[i]	= i;
 	}
arch/mips/sgi-ip27/ip27-smp.c +1 -1
index c6851df..735b43b 100644
@@ -76,7 +76,7 @@ static int do_cpumask(cnodeid_t cnode, nasid_t nasid, int highest)
 			/* Only let it join in if it's marked enabled */
 			if ((acpu->cpu_info.flags & KLINFO_ENABLE) &&
 			    (tot_cpus_found != NR_CPUS)) {
-				cpu_set(cpuid, cpu_possible_map);
+				set_cpu_possible(cpuid, true);
 				alloc_cpupda(cpuid, tot_cpus_found);
 				cpus_found++;
 				tot_cpus_found++;
arch/mips/sibyte/bcm1480/smp.c +2 -3
index d667875..63d2211 100644
@@ -147,14 +147,13 @@ static void __init bcm1480_smp_setup(void)
 {
 	int i, num;
 
-	cpus_clear(cpu_possible_map);
-	cpu_set(0, cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
 	__cpu_number_map[0] = 0;
 	__cpu_logical_map[0] = 0;
 
 	for (i = 1, num = 0; i < NR_CPUS; i++) {
 		if (cfe_cpu_stop(i) == 0) {
-			cpu_set(i, cpu_possible_map);
+			set_cpu_possible(i, true);
 			__cpu_number_map[i] = ++num;
 			__cpu_logical_map[num] = i;
 		}
arch/mips/sibyte/sb1250/smp.c +2 -3
index 38e7f6b..77f0df5 100644
@@ -135,14 +135,13 @@ static void __init sb1250_smp_setup(void)
 {
 	int i, num;
 
-	cpus_clear(cpu_possible_map);
-	cpu_set(0, cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
 	__cpu_number_map[0] = 0;
 	__cpu_logical_map[0] = 0;
 
 	for (i = 1, num = 0; i < NR_CPUS; i++) {
 		if (cfe_cpu_stop(i) == 0) {
-			cpu_set(i, cpu_possible_map);
+			set_cpu_possible(i, true);
 			__cpu_number_map[i] = ++num;
 			__cpu_logical_map[num] = i;
 		}
Signature · 1 line
1.7.7.3
Acked-by on v1 2/3
#41 lore raw

Re: [PATCH 2/3] mips: Avoid raw handling of cpu_possible_map/cpu_online_map

Quoted 3 lines · Venkatesh Pallipadi

Use set_cpu_* and init_cpu_* variants instead.

I came up with the same thing, so...

Ralf: If you too were to Acknowledge the patch, we might get it merged.

David Daney

Quoted 139 lines
---
  arch/mips/cavium-octeon/smp.c       |    2 +-
  arch/mips/kernel/smp.c              |    4 ++--
  arch/mips/netlogic/xlr/smp.c        |    4 ++--
  arch/mips/pmc-sierra/yosemite/smp.c |    4 ++--
  arch/mips/sgi-ip27/ip27-smp.c       |    2 +-
  arch/mips/sibyte/bcm1480/smp.c      |    5 ++---
  arch/mips/sibyte/sb1250/smp.c       |    5 ++---
  7 files changed, 12 insertions(+), 14 deletions(-)
arch/mips/cavium-octeon/smp.c +1 -1
index efcfff4..5cce09c 100644
@@ -268,7 +268,7 @@ static int octeon_cpu_disable(void)
 
  	spin_lock(&smp_reserve_lock);
 
-	cpu_clear(cpu, cpu_online_map);
+	set_cpu_online(cpu, false);
  	cpu_clear(cpu, cpu_callin_map);
  	local_irq_disable();
  	fixup_irqs();
arch/mips/kernel/smp.c +2 -2
index 32c1e95..28777ff 100644
@@ -148,7 +148,7 @@ static void stop_this_cpu(void *dummy)
  	/*
  	 * Remove this CPU:
  	 */
-	cpu_clear(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), false);
  	for (;;) {
  		if (cpu_wait)
  			(*cpu_wait)();		/* Wait if available. */
@@ -248,7 +248,7 @@ int __cpuinit __cpu_up(unsigned int cpu)
  	while (!cpu_isset(cpu, cpu_callin_map))
  		udelay(100);
 
-	cpu_set(cpu, cpu_online_map);
+	set_cpu_online(cpu, true);
 
  	return 0;
  }
arch/mips/netlogic/xlr/smp.c +2 -2
index 080284d..8084221 100644
@@ -154,7 +154,7 @@ void __init nlm_smp_setup(void)
  	cpu_set(boot_cpu, phys_cpu_present_map);
  	__cpu_number_map[boot_cpu] = 0;
  	__cpu_logical_map[0] = boot_cpu;
-	cpu_set(0, cpu_possible_map);
+	set_cpu_possible(0, true);
 
  	num_cpus = 1;
  	for (i = 0; i<  NR_CPUS; i++) {
@@ -166,7 +166,7 @@ void __init nlm_smp_setup(void)
  			cpu_set(i, phys_cpu_present_map);
  			__cpu_number_map[i] = num_cpus;
  			__cpu_logical_map[num_cpus] = i;
-			cpu_set(num_cpus, cpu_possible_map);
+			set_cpu_possible(num_cpus, true);
  			++num_cpus;
  		}
  	}
arch/mips/pmc-sierra/yosemite/smp.c +2 -2
index 2608752..b2b23eb 100644
@@ -155,10 +155,10 @@ static void __init yos_smp_setup(void)
  {
  	int i;
 
-	cpus_clear(cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
 
  	for (i = 0; i<  2; i++) {
-		cpu_set(i, cpu_possible_map);
+		set_cpu_possible(i, true);
  		__cpu_number_map[i]	= i;
  		__cpu_logical_map[i]	= i;
  	}
arch/mips/sgi-ip27/ip27-smp.c +1 -1
index c6851df..735b43b 100644
@@ -76,7 +76,7 @@ static int do_cpumask(cnodeid_t cnode, nasid_t nasid, int highest)
  			/* Only let it join in if it's marked enabled */
  			if ((acpu->cpu_info.flags&  KLINFO_ENABLE)&&
  			(tot_cpus_found != NR_CPUS)) {
-				cpu_set(cpuid, cpu_possible_map);
+				set_cpu_possible(cpuid, true);
  				alloc_cpupda(cpuid, tot_cpus_found);
  				cpus_found++;
  				tot_cpus_found++;
arch/mips/sibyte/bcm1480/smp.c +2 -3
index d667875..63d2211 100644
@@ -147,14 +147,13 @@ static void __init bcm1480_smp_setup(void)
  {
  	int i, num;
 
-	cpus_clear(cpu_possible_map);
-	cpu_set(0, cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
  	__cpu_number_map[0] = 0;
  	__cpu_logical_map[0] = 0;
 
  	for (i = 1, num = 0; i<  NR_CPUS; i++) {
  		if (cfe_cpu_stop(i) == 0) {
-			cpu_set(i, cpu_possible_map);
+			set_cpu_possible(i, true);
  			__cpu_number_map[i] = ++num;
  			__cpu_logical_map[num] = i;
  		}
arch/mips/sibyte/sb1250/smp.c +2 -3
index 38e7f6b..77f0df5 100644
@@ -135,14 +135,13 @@ static void __init sb1250_smp_setup(void)
  {
  	int i, num;
 
-	cpus_clear(cpu_possible_map);
-	cpu_set(0, cpu_possible_map);
+	init_cpu_possible(cpumask_of(0));
  	__cpu_number_map[0] = 0;
  	__cpu_logical_map[0] = 0;
 
  	for (i = 1, num = 0; i<  NR_CPUS; i++) {
  		if (cfe_cpu_stop(i) == 0) {
-			cpu_set(i, cpu_possible_map);
+			set_cpu_possible(i, true);
  			__cpu_number_map[i] = ++num;
  			__cpu_logical_map[num] = i;
  		}
#42 lore raw

PATCH 3/3 um: Avoid raw handling of cpu_online_map

In reply to Venkatesh Pallipadi

Use init_cpu_online and set_cpu_online instead.

---
 arch/um/kernel/skas/process.c |    2 +-
 arch/um/kernel/smp.c          |    8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/um/kernel/skas/process.c +1 -1
index 2e9852c..5daa0f5 100644
@@ -41,7 +41,7 @@ static int __init start_kernel_proc(void *unused)
 	cpu_tasks[0].pid = pid;
 	cpu_tasks[0].task = current;
 #ifdef CONFIG_SMP
-	cpu_online_map = cpumask_of_cpu(0);
+	init_cpu_online(cpumask_of(0));
 #endif
 	start_kernel();
 	return 0;
arch/um/kernel/smp.c +4 -4
index 155206a..b5d2ca9 100644
@@ -76,7 +76,7 @@ static int idle_proc(void *cpup)
 		cpu_relax();
 
 	notify_cpu_starting(cpu);
-	cpu_set(cpu, cpu_online_map);
+	set_cpu_online(cpu, true);
 	default_idle();
 	return 0;
 }
@@ -110,8 +110,8 @@ void smp_prepare_cpus(unsigned int maxcpus)
 	for (i = 0; i < ncpus; ++i)
 		set_cpu_possible(i, true);
 
-	cpu_clear(me, cpu_online_map);
-	cpu_set(me, cpu_online_map);
+	set_cpu_online(me, false);
+	set_cpu_online(me, true);
 	cpu_set(me, cpu_callin_map);
 
 	err = os_pipe(cpu_data[me].ipi_pipe, 1, 1);
@@ -138,7 +138,7 @@ void smp_prepare_cpus(unsigned int maxcpus)
 
 void smp_prepare_boot_cpu(void)
 {
-	cpu_set(smp_processor_id(), cpu_online_map);
+	set_cpu_online(smp_processor_id(), true);
 }
 
 int __cpu_up(unsigned int cpu)
Signature · 1 line
1.7.7.3
#43 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

In reply to Venkatesh Pallipadi
Quoted 19 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  *  that it is possible might ever be plugged in at anytime during the
  *  life of that system boot.
  #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Quoted 7 lines

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

How is it that this patch got merged to linux-next before all the cleanup patches for nr_online_cpus?

From the looks of your follow-on patches it would seem that all MIPS, hexagon, and um are now broken.

I know for a fact that MIPS doesn't boot because of this.

David Daney

Quoted 90 lines
---
  include/linux/cpumask.h |    9 +++++++--
  kernel/cpu.c            |   13 +++++++++++++
  2 files changed, 20 insertions(+), 2 deletions(-)
include/linux/cpumask.h +7 -2
index 4f7a632..930e255 100644
@@ -27,6 +27,9 @@ typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  extern int nr_cpu_ids;
  #endif
 
+extern unsigned int nr_online_cpus;
+extern unsigned int nr_possible_cpus;
+
  #ifdef CONFIG_CPUMASK_OFFSTACK
  /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
   * not all bits may be allocated. */
@@ -81,8 +84,10 @@ extern const struct cpumask *const cpu_present_mask;
  extern const struct cpumask *const cpu_active_mask;
 
  #if NR_CPUS>  1
-#define num_online_cpus()	cpumask_weight(cpu_online_mask)
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+
+#define num_online_cpus()	(nr_online_cpus)
+#define num_possible_cpus()	(nr_possible_cpus)
+
  #define num_present_cpus()	cpumask_weight(cpu_present_mask)
  #define num_active_cpus()	cpumask_weight(cpu_active_mask)
  #define cpu_online(cpu)		cpumask_test_cpu((cpu), cpu_online_mask)
kernel/cpu.c +13 -0
index 2060c6e..d520d34 100644
@@ -604,9 +604,13 @@ EXPORT_SYMBOL(cpu_all_bits);
  #ifdef CONFIG_INIT_ALL_POSSIBLE
  static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
  	= CPU_BITS_ALL;
+unsigned int nr_possible_cpus __read_mostly = CONFIG_NR_CPUS;
  #else
  static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
+unsigned int nr_possible_cpus __read_mostly;
  #endif
+EXPORT_SYMBOL(nr_possible_cpus);
+
  const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
  EXPORT_SYMBOL(cpu_possible_mask);
 
@@ -614,6 +618,9 @@ static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
  const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
  EXPORT_SYMBOL(cpu_online_mask);
 
+unsigned int nr_online_cpus __read_mostly;
+EXPORT_SYMBOL(nr_online_cpus);
+
  static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
  const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
  EXPORT_SYMBOL(cpu_present_mask);
@@ -628,6 +635,8 @@ void set_cpu_possible(unsigned int cpu, bool possible)
  		cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
  	else
  		cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
+
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
  }
 
  void set_cpu_present(unsigned int cpu, bool present)
@@ -644,6 +653,8 @@ void set_cpu_online(unsigned int cpu, bool online)
  		cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
  	else
  		cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
+
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
  }
 
  void set_cpu_active(unsigned int cpu, bool active)
@@ -662,9 +673,11 @@ void init_cpu_present(const struct cpumask *src)
  void init_cpu_possible(const struct cpumask *src)
  {
  	cpumask_copy(to_cpumask(cpu_possible_bits), src);
+	nr_possible_cpus = cpumask_weight(cpu_possible_mask);
  }
 
  void init_cpu_online(const struct cpumask *src)
  {
  	cpumask_copy(to_cpumask(cpu_online_bits), src);
+	nr_online_cpus = cpumask_weight(cpu_online_mask);
  }
#44 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 24 lines · On Mon, 27 Feb 2012 13:55:55 -0800 David Daney <[email protected]> wrote:
Quoted 23 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  *  that it is possible might ever be plugged in at anytime during the
  *  life of that system boot.
  #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Quoted 6 lines
Quoted 3 lines

How is it that this patch got merged to linux-next before all the cleanup patches for nr_online_cpus?

<spends five minutes searching mailing list archives>

I for one do not have a clue what patches the term "cleanup patches for nr_online_cpus" refers to. Patches have names - please use them!

Quoted 4 lines

From the looks of your follow-on patches it would seem that all MIPS,
hexagon, and um are now broken.

I know for a fact that MIPS doesn't boot because of this.

I shall drop cpumask-avoid-mask-based-num_possible_cpus-and-num_online_cpus.patch.

That patch was sent as a single standalone patch and the changelog had no mention of any needed preparatory patches. If resending, please send *all* patches in a single sequence-numbered series. We know how to do this.

#45 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 31 lines · Andrew Morton
Quoted 28 lines · On Mon, 27 Feb 2012 13:55:55 -0800 David Daney<[email protected]> wrote:
Quoted 26 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
* cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

   *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
   *  that it is possible might ever be plugged in at anytime during the
   *  life of that system boot.
   #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Quoted 8 lines
Quoted 2 lines

How is it that this patch got merged to linux-next before all the cleanup patches for nr_online_cpus?

<spends five minutes searching mailing list archives>

I for one do not have a clue what patches the term "cleanup patches for nr_online_cpus" refers to. Patches have names - please use them!

Sorry about that. I was a little hasty:

From: Venkatesh Pallipadi <[email protected]>
Subject: [PATCH 0/3] Cleanup raw handling of online/possible map
Date: Tue, 14 Feb 2012 14:49:41 -0800
Message-Id: <[email protected]>

I don't really know a better way to refer to them.

David Daney

Quoted 5 lines
Quoted 4 lines

From the looks of your follow-on patches it would seem that all MIPS,
hexagon, and um are now broken.

I know for a fact that MIPS doesn't boot because of this.

Quoted 7 lines

I shall drop cpumask-avoid-mask-based-num_possible_cpus-and-num_online_cpus.patch.

That patch was sent as a single standalone patch and the changelog had no mention of any needed preparatory patches. If resending, please send *all* patches in a single sequence-numbered series. We know how to do this.

It is possible that Venkatesh did not know about the breakage when the original patch was sent

#46 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

Quoted 71 lines · David Daney
Quoted 37 lines · Andrew Morton
Quoted 33 lines · On Mon, 27 Feb 2012 13:55:55 -0800 David Daney<[email protected]>  wrote:
Quoted 30 lines · Venkatesh Pallipadi

Kernel's notion of possible cpus (from include/linux/cpumask.h)
  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable

  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
  *  that it is possible might ever be plugged in at anytime during the
  *  life of that system boot.

  #define num_possible_cpus()     cpumask_weight(cpu_possible_mask)

and on x86 cpumask_weight() calls hweight64 and hweight64 (on older kernels
and systems with !X86_FEATURE_POPCNT) or a popcnt based alternative.

i.e, We needlessly go through this mask based calculation everytime num_possible_cpus() is called.

The problem is there with cpu_online_mask() as well, which is fixed value at
boot time in !CONFIG_HOTPLUG_CPU case and should not change that often even
in HOTPLUG case.

Though most of the callers of these two routines are init time (with few exceptions of runtime calls), it is cleaner to use variables and not go through this repeated mask based calculation.

Quoted 9 lines
Quoted 2 lines

How is it that this patch got merged to linux-next before all the cleanup patches for nr_online_cpus?

<spends five minutes searching mailing list archives>

I for one do not have a clue what patches the term "cleanup patches for nr_online_cpus" refers to.  Patches have names - please use them!

Sorry about that.  I was a little hasty:

 From: Venkatesh Pallipadi <[email protected]>
 Subject: [PATCH 0/3] Cleanup raw handling of online/possible map
 Date: Tue, 14 Feb 2012 14:49:41 -0800
 Message-Id: <[email protected]>

I don't really know a better way to refer to them.

David Daney

Quoted 9 lines
Quoted 4 lines

  From the looks of your follow-on patches it would seem that all MIPS, hexagon, and um are now broken.

I know for a fact that MIPS doesn't boot because of this.

I shall drop cpumask-avoid-mask-based-num_possible_cpus-and-num_online_cpus.patch.

Quoted 8 lines
Quoted 4 lines

That patch was sent as a single standalone patch and the changelog had no mention of any needed preparatory patches.  If resending, please send *all* patches in a single sequence-numbered series.  We know how to do this.

It is possible that Venkatesh did not know about the breakage when the original patch was sent

Sorry about all the confusion with this change. The long story goes something like this:

Yes. The problems with this patch "Avoid mask based num_possible_cpus and num_online_cpus"
were uncovered after it went into mm. First report was on ia64 here
https://lkml.org/lkml/2012/2/13/327

And there were potential problems in other archs as well. Srivatsa and I sent out a bunch of cleanups
https://lkml.org/lkml/2012/2/14/355
https://lkml.org/lkml/2012/2/14/380

Rusty followed up with a superset of these cleanups
https://lkml.org/lkml/2012/2/15/4

I asked Rusty to pick up my original patch as the last patch in his series.
https://lkml.org/lkml/2012/2/15/516

That's all the happenings that I remember. I am not sure what the status of Rusty's or my cleanup patches is right now. I am looking at various trees now trying to find these cleanup patches. I can resubmit this particular change once the cleanups have made through.

Thanks,
Venki

#47 lore raw

Re: [PATCH] Avoid mask based num_possible_cpus and num_online_cpus -v5

In reply to Andrew Morton

Hi Andrew,

Quoted 3 lines · On Mon, 27 Feb 2012 14:07:46 -0800 Andrew Morton <[email protected]>…

I shall drop cpumask-avoid-mask-based-num_possible_cpus-and-num_online_cpus.patch.

I have dropped that from the akpm tree today.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]