Skip to contentSkip to conversation

cpuidle : use percpu cpuidle in the core code

PATCH Superseded Daniel Lezcano 3 participants 7 messages Mar 30, 2012 – May 30, 2012 1 message not in the archive CPU idle time management framework

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpuidle : use percpu cpuidle in the core code

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
 drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
 include/linux/cpuidle.h   |    3 +++
 2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +34 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
 EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;
+
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		cpuidle_unregister_device(dev);
+	}
+
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
 /**
  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  * @dev: the cpu
include/linux/cpuidle.h +3 -0
index f3ebbba..17e3d33 100644
@@ -133,6 +133,7 @@ struct cpuidle_driver {
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
 extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 struct cpuidle_driver *cpuidle_get_driver(void);
 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
@@ -150,6 +151,8 @@ extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
 #else
 static inline void disable_cpuidle(void) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 47 lines · Daniel Lezcano

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
 drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
 include/linux/cpuidle.h   |    3 +++
 2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +9 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
 EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 412

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+

Isn't this racy with respect to CPU hotplug?

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 421

Quoted 9 lines
drivers/cpuidle/cpuidle.c +9 -0
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		cpuidle_unregister_device(dev);
+	}
+

This could be improved I guess.. What if the registration fails for the first cpu itself? Then looping over entire online cpumask would be a waste of time..

Here is a discussion on some very similar code:
https://lkml.org/lkml/2012/3/22/72
https://lkml.org/lkml/2012/3/22/143

Quoted 22 lines
drivers/cpuidle/cpuidle.c +6 -0
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
 /**
  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  * @dev: the cpu
include/linux/cpuidle.h +1 -0
index f3ebbba..17e3d33 100644
@@ -133,6 +133,7 @@ struct cpuidle_driver {
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
 extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 struct cpuidle_driver *cpuidle_get_driver(void);
 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);

include/linux/cpuidle.h extern int cpuidle_wrap_enter(struct cpuidle_device *dev, · line 158

Quoted 8 lines
include/linux/cpuidle.h +2 -0
@@ -150,6 +151,8 @@ extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
 #else
 static inline void disable_cpuidle(void) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }

Regards,
Srivatsa S. Bhat

#2 lore raw
Quoted 54 lines · Srivatsa S. Bhat
Quoted 52 lines · Daniel Lezcano

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
  drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
  include/linux/cpuidle.h   |    3 +++
  2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +14 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
  #include "cpuidle.h"
 
  DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
  DEFINE_MUTEX(cpuidle_lock);
  LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
  EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;
+
+	for_each_online_cpu(cpu) {
+		dev =&per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
Quoted 8 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 412

Quoted 5 lines
drivers/cpuidle/cpuidle.c +5 -0
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+

Isn't this racy with respect to CPU hotplug?

No, I don't think so. Do you see a race ?

Quoted 6 lines
Quoted 6 lines
drivers/cpuidle/cpuidle.c +6 -0
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(cpu) {
+		dev =&per_cpu(cpuidle_device, cpu);
Quoted 8 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 421

Quoted 3 lines
drivers/cpuidle/cpuidle.c +3 -0
+		cpuidle_unregister_device(dev);
+	}
+

This could be improved I guess.. What if the registration fails for the first cpu itself? Then looping over entire online cpumask would be a waste of time..

Certainly in a critical section that would make sense, but for 4,8 or 16 cpus in an initialization path at boot time... Anyway, I can add what is proposed in https://lkml.org/lkml/2012/3/22/143.

Thanks
-- Daniel

Quoted 40 lines

Here is a discussion on some very similar code:
https://lkml.org/lkml/2012/3/22/72
https://lkml.org/lkml/2012/3/22/143

Quoted 22 lines
drivers/cpuidle/cpuidle.c +6 -0
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
  /**
   * cpuidle_unregister_device - unregisters a CPU's idle PM feature
   * @dev: the cpu
include/linux/cpuidle.h +1 -0
index f3ebbba..17e3d33 100644
@@ -133,6 +133,7 @@ struct cpuidle_driver {
  #ifdef CONFIG_CPU_IDLE
  extern void disable_cpuidle(void);
  extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
  extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  struct cpuidle_driver *cpuidle_get_driver(void);
  extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);

include/linux/cpuidle.h extern int cpuidle_wrap_enter(struct cpuidle_device *dev, · line 158

Quoted 8 lines
include/linux/cpuidle.h +2 -0
@@ -150,6 +151,8 @@ extern int cpuidle_wrap_enter(struct cpuidle_device *dev,
  #else
  static inline void disable_cpuidle(void) { }
  static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
  static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  {return -ENODEV; }
  static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }

Regards,
Srivatsa S. Bhat

Signature · 10 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-pm
#3 lore raw
Quoted 65 lines · Daniel Lezcano
Quoted 55 lines · Srivatsa S. Bhat
Quoted 53 lines · Daniel Lezcano

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
  drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
  include/linux/cpuidle.h   |    3 +++
  2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +14 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
  #include "cpuidle.h"
 
  DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
  DEFINE_MUTEX(cpuidle_lock);
  LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct
 cpuidle_device *dev)
 
  EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+    int ret, cpu;
+    struct cpuidle_device *dev;
+
+    ret = cpuidle_register_driver(drv);
+    if (ret)
+        return ret;
+
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
+        dev->cpu = cpu;
+
Quoted 8 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 412

Quoted 5 lines
drivers/cpuidle/cpuidle.c +5 -0
+        ret = cpuidle_register_device(dev);
+        if (ret)
+            goto out_unregister;
+    }
+

Isn't this racy with respect to CPU hotplug?

Quoted 1 line

No, I don't think so. Do you see a race ?

Well, that depends on when/where this function gets called. This patch introduces the function. Where is the caller?

As of now, if you are calling this in boot-up code, its not racy. However, there have been attempts to speed up boot times by trying to online cpus in parallel with the rest of the kernel initialization[1]. In that case, unless your call is an early init call, it can race with CPU hotplug.

[1]. https://lkml.org/lkml/2012/1/30/647

Quoted 16 lines
Quoted 15 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 421

Quoted 10 lines
drivers/cpuidle/cpuidle.c +9 -0
+out:
+    return ret;
+
+out_unregister:
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
+        cpuidle_unregister_device(dev);
+    }
+

This could be improved I guess.. What if the registration fails for the first cpu itself? Then looping over entire online cpumask would be a waste of time..

Quoted 4 lines

Certainly in a critical section that would make sense, but for 4,8 or 16 cpus in an initialization path at boot time... Anyway, I can add what is proposed in https://lkml.org/lkml/2012/3/22/143.

What about servers with a lot more CPUs, like say 128 or even more? :-)

Moreover I don't see any downsides to the optimization. So should be good to add it in any case...

Regards,
Srivatsa S. Bhat

Quoted 39 lines
Quoted 39 lines

Here is a discussion on some very similar code:
https://lkml.org/lkml/2012/3/22/72
https://lkml.org/lkml/2012/3/22/143

Quoted 32 lines
drivers/cpuidle/cpuidle.c +6 -0
+    cpuidle_unregister_driver(drv);
+
+    goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
  /**
   * cpuidle_unregister_device - unregisters a CPU's idle PM feature

* @dev: the cpu

include/linux/cpuidle.h +3 -0
index f3ebbba..17e3d33 100644
@@ -133,6 +133,7 @@ struct cpuidle_driver {
  #ifdef CONFIG_CPU_IDLE
  extern void disable_cpuidle(void);
  extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
  extern int cpuidle_register_driver(struct cpuidle_driver *drv);
  struct cpuidle_driver *cpuidle_get_driver(void);
  extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
@@ -150,6 +151,8 @@ extern int cpuidle_wrap_enter(struct
 cpuidle_device *dev,
  #else
  static inline void disable_cpuidle(void) { }
  static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
  static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
  {return -ENODEV; }

static inline struct cpuidle_driver *cpuidle_get_driver(void)
{return NULL; }

#4 lore raw
Quoted 67 lines · Srivatsa S. Bhat
Quoted 65 lines · Daniel Lezcano
Quoted 63 lines · Srivatsa S. Bhat
Quoted 48 lines · Daniel Lezcano

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
   drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
   include/linux/cpuidle.h   |    3 +++
   2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +9 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
   #include "cpuidle.h"
 
   DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
   DEFINE_MUTEX(cpuidle_lock);
   LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct
 cpuidle_device *dev)
 
   EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+    int ret, cpu;
+    struct cpuidle_device *dev;
+
+    ret = cpuidle_register_driver(drv);
+    if (ret)
+        return ret;

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct · line 413

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
+        dev->cpu = cpu;
+
+        ret = cpuidle_register_device(dev);
+        if (ret)
+            goto out_unregister;
+    }
+

Isn't this racy with respect to CPU hotplug?

Quoted 5 lines
Quoted 1 line

No, I don't think so. Do you see a race ?

Well, that depends on when/where this function gets called. This patch introduces the function. Where is the caller?

There is no caller for the moment because they are in the different arch specific code in the different trees.

But the callers will be in the init calls at boot up.

Quoted 1 line

As of now, if you are calling this in boot-up code, its not racy.

Most of the caller are in the boot-up code, in device_init or module_init. The other ones are doing some specific initialization on the cpuidle_device (cpuinit, like acpi) and can't use the cpuidle_register function.

Quoted 6 lines

However, there have been attempts to speed up boot times by trying to online cpus in parallel with the rest of the kernel initialization[1]. In that case, unless your call is an early init call, it can race with CPU hotplug.

[1]. https://lkml.org/lkml/2012/1/30/647

Aha ! Now I understand the race you were talking about. Thanks for the pointer. It is very interesting.

I realize if the cpus boot up in parallel, that will break a lot of things and, for my concern, that will break most of the cpuidle drivers.

So far the cpu bootup parallelization is not there, so from my POV, my patch is correct as we will factor out in a single place some code which will be potentially broken by this parallelization in the future. It will be easier to fix that in a single place rather in multiple drivers.

Thanks for spotting this potential problem. This is something I will keep in mind for the future.

Quoted 21 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/cpuidle/cpuidle.c +6 -0
+out:
+    return ret;
+
+out_unregister:
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
Quoted 8 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 421

Quoted 3 lines
drivers/cpuidle/cpuidle.c +3 -0
+        cpuidle_unregister_device(dev);
+    }
+

This could be improved I guess.. What if the registration fails for the first cpu itself? Then looping over entire online cpumask would be a waste of time..

Quoted 4 lines

Certainly in a critical section that would make sense, but for 4,8 or 16 cpus in an initialization path at boot time... Anyway, I can add what is proposed in https://lkml.org/lkml/2012/3/22/143.

Quoted 4 lines

What about servers with a lot more CPUs, like say 128 or even more? :-)

Moreover I don't see any downsides to the optimization. So should be good to add it in any case...

Yes, no problem. I will add it.

Thanks !
-- Daniel

Signature · 10 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-pm
#5 lore raw
Quoted 91 lines · Daniel Lezcano
Quoted 67 lines · Srivatsa S. Bhat
Quoted 65 lines · Daniel Lezcano
Quoted 63 lines · Srivatsa S. Bhat
Quoted 48 lines · Daniel Lezcano

The usual cpuidle initialization routines are to register the driver, then register a cpuidle device per cpu.

With the device's state count default initialization with the driver's state count, the code initialization remains mostly the same in the different drivers.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
   drivers/cpuidle/cpuidle.c |   34 ++++++++++++++++++++++++++++++++++
   include/linux/cpuidle.h   |    3 +++
   2 files changed, 37 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +9 -0
index b8a1faf..2a174e8 100644
@@ -23,6 +23,7 @@
   #include "cpuidle.h"
 
   DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
   DEFINE_MUTEX(cpuidle_lock);
   LIST_HEAD(cpuidle_detected_devices);
@@ -391,6 +392,39 @@ int cpuidle_register_device(struct
 cpuidle_device *dev)
 
   EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+    int ret, cpu;
+    struct cpuidle_device *dev;
+
+    ret = cpuidle_register_driver(drv);
+    if (ret)
+        return ret;

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct · line 413

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
+        dev->cpu = cpu;
+
+        ret = cpuidle_register_device(dev);
+        if (ret)
+            goto out_unregister;
+    }
+

Isn't this racy with respect to CPU hotplug?

Quoted 5 lines
Quoted 1 line

No, I don't think so. Do you see a race ?

Well, that depends on when/where this function gets called. This patch introduces the function. Where is the caller?

There is no caller for the moment because they are in the different arch specific code in the different trees.

But the callers will be in the init calls at boot up.

Quoted 1 line

As of now, if you are calling this in boot-up code, its not racy.

Most of the caller are in the boot-up code, in device_init or module_init. The other ones are doing some specific initialization on the cpuidle_device (cpuinit, like acpi) and can't use the cpuidle_register function.

Quoted 5 lines

However, there have been attempts to speed up boot times by trying to online cpus in parallel with the rest of the kernel initialization[1]. In that case, unless your call is an early init call, it can race with CPU hotplug.

Quoted 8 lines
Quoted 1 line

[1]. https://lkml.org/lkml/2012/1/30/647

Aha ! Now I understand the race you were talking about. Thanks for the pointer. It is very interesting.

I realize if the cpus boot up in parallel, that will break a lot of things and, for my concern, that will break most of the cpuidle drivers.

Exactly!

Quoted 8 lines

So far the cpu bootup parallelization is not there, so from my POV, my patch is correct as we will factor out in a single place some code which will be potentially broken by this parallelization in the future. It will be easier to fix that in a single place rather in multiple drivers.

Thanks for spotting this potential problem. This is something I will keep in mind for the future.

Sure, that would be great!

Quoted 21 lines
Quoted 21 lines
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
drivers/cpuidle/cpuidle.c +6 -0
+out:
+    return ret;
+
+out_unregister:
+    for_each_online_cpu(cpu) {
+        dev =&per_cpu(cpuidle_device, cpu);
Quoted 8 lines

drivers/cpuidle/cpuidle.c int cpuidle_register_device(struct cpuidle_device *dev) · line 421

Quoted 3 lines
drivers/cpuidle/cpuidle.c +3 -0
+        cpuidle_unregister_device(dev);
+    }
+

This could be improved I guess.. What if the registration fails for the first cpu itself? Then looping over entire online cpumask would be a waste of time..

Certainly in a critical section that would make sense, but for 4,8 or 16 cpus in an initialization path at boot time... Anyway, I can add what is proposed in https://lkml.org/lkml/2012/3/22/143.

Quoted 7 lines
Quoted 4 lines

What about servers with a lot more CPUs, like say 128 or even more? :-)

Moreover I don't see any downsides to the optimization. So should be good to add it in any case...

Yes, no problem. I will add it.

Thanks!

Regards,
Srivatsa S. Bhat
IBM Linux Technology Center

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Hey Daniel,

Sorry for the late review/response but perhaps this will still be useful as your cpuidle work is ongoing.

Quoted 4 lines

Most of the caller are in the boot-up code, in device_init or module_init. The other ones are doing some specific initialization on the cpuidle_device (cpuinit, like acpi) and can't use the cpuidle_register function.

What about adding a callback pointer parameter to the cpuidle_register function that allows platform specific device initialization to occur before the cpuidle_device_register call is made? For reference, see my old cpuidle common init code that does this at
git://git.linaro.org/people/rob_lee/linux.git cpuidle_init.

With this callback, acpi and the other platforms that need to modify the cpuidle_device data can use this interface also. The necessary acpi cpuidle changes can also be found at the above git URL (although they haven't been reviewed so they are possibly flawed).

Rob

_______________________________________________

Quoted 3 lines

linaro-dev mailing list [email protected]
http://lists.linaro.org/mailman/listinfo/linaro-dev