Skip to contentSkip to conversation

cpuidle: global registration of idle states with per-cpu statistics

RFC PATCH Superseded Trinabh Gupta 3 participants 8 messages Mar 22, 2011 – Apr 2, 2011

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover cpuidle: global registration of idle states with per-cpu statistics

This patch series is an early RFC to discuss the feasibility of avoiding registering of idle states from each cpu.

The core change is to split the cpuidle_device structure into parts that can be global and parts that has to remain per-cpu. The per-cpu pieces are mostly generic statistics that can be independent of current running driver.

Motivation:
* Simplify the cpuidle subsystem framework and have registration/unregistration done by single cpu.

* Minimise the data structure that needs to be maintained for multiple cpuidle drivers

* Reference: https://lkml.org/lkml/2011/2/10/37

Advantages:
* Make the cpuidle framework simple for most use cases where C-States are symmetric. In case there are asymmetric C-States detected, fallback mechanism should be incorporated to maintain the system functional
https://lkml.org/lkml/2011/2/10/257
https://lkml.org/lkml/2011/2/10/37

* Non x86 archs that does not have asymmetric C-States like POWER, may not need the fallback mechanism and hence the framework will be simple for most use cases.

Disadvantages:
* Asymmetric C-States are part of x86 ACPI specification. Incorrect handling may functionally affect the system

* Incorporating per-cpu masks for each state to allow/dis-allow global states on subset of CPUs may result in an implementation that is not better than current solution of having per-cpu states.

This patch series applies on top of the pm_idle cleanup patch
https://lkml.org/lkml/2011/3/22/150 (cpuidle: Cleanup pm_idle and include driver/cpuidle.c in-kernel)

This patch series is tested on x86 Nehalem system with multiple ACPI C-States.

This patch series has limitations of not handling multiple driver registration and switching between drivers on all CPUs mainly due to incomplete handling of per-cpu enable/disable and driver_data.

Please let us know your comments and suggest possible approaches to the problem.

---

Trinabh Gupta (2):
cpuidle: API changes in callers using new cpuidle_state_stats
cpuidle: Data structure changes for global cpuidle device

 drivers/acpi/processor_driver.c    |   19 +---
 drivers/acpi/processor_idle.c      |   45 ++++++----
 drivers/cpuidle/cpuidle.c          |  169 ++++++++++++++++++++++--------------
 drivers/cpuidle/cpuidle.h          |    4 -
 drivers/cpuidle/driver.c           |   24 -----
 drivers/cpuidle/governor.c         |   10 +-
 drivers/cpuidle/governors/Makefile |    2
 drivers/cpuidle/governors/menu.c   |   16 ++-
 drivers/cpuidle/sysfs.c            |   62 +++++++------
 drivers/idle/default_driver.c      |   39 +++-----
 include/linux/cpuidle.h            |   70 ++++++++++-----
 11 files changed, 245 insertions(+), 215 deletions(-)

--
-Trinabh

#1 lore raw

RFC PATCH 1/2 cpuidle: Data structure changes for global cpuidle device

Currently cpuidle subsystem has the following data structures:

* Global cpuidle_driver which is maintained in a list for each registered driver

* cpuidle_device is per-cpu per-driver structure that contains the array of cpuidle_state defined for that CPU

* cpuidle_state contains the definition of the idle routine and place holders for statistics that are used by the governor to make the selection.

Proposed new data structures:

* Basically the cpuidle_state structure along with the cpuidle_device is made global and per-driver. The cpuidle_state contains the idle state definitions.

* The statistics part of the cpuidle_state that is needed by the governor on a per-cpu bases is split into a new per-cpu structure called cpuidle_state_stats. This structure is generic and independent of the cpuidle_driver/device that is currently in force.

<<- Global--->> <<per-cpu>>

cpuidle_driver cpuidle_device_stats
| -> state_stats[]

  \
    -> cpuidle_device
        -> states[]

Open Issues:

* Handle per-cpu, per-driver's driver_data appropriately within the cpuidle_driver structure. For this RFC, it is pushed into cpuidle_device_stats structure.

* Handle double initialization of kobj structs on multiple driver registration.

---

 include/linux/cpuidle.h |   75 +++++++++++++++++++++++++++++++++--------------
 1 files changed, 52 insertions(+), 23 deletions(-)
include/linux/cpuidle.h +52 -23
index a5f5fd0..16423df 100644
@@ -22,6 +22,7 @@
 #define CPUIDLE_DESC_LEN	32
 
 struct cpuidle_device;
+struct cpuidle_state_stats;
 
 
 /****************************
@@ -31,18 +32,21 @@ struct cpuidle_device;
 struct cpuidle_state {
 	char		name[CPUIDLE_NAME_LEN];
 	char		desc[CPUIDLE_DESC_LEN];
-	void		*driver_data;
 
 	unsigned int	flags;
 	unsigned int	exit_latency; /* in US */
 	unsigned int	power_usage; /* in mW */
 	unsigned int	target_residency; /* in US */
 
-	unsigned long long	usage;
-	unsigned long long	time; /* in US */
-
 	int (*enter)	(struct cpuidle_device *dev,
-			 struct cpuidle_state *state);
+			 struct cpuidle_state *state,
+			 struct cpuidle_state_stats *state_stats);
+};
+
+struct cpuidle_state_stats {
+	void		*driver_data;
+	unsigned long long usage;
+	unsigned long long time; /* in US */
 };
 
 /* Idle State Flags */
@@ -55,9 +59,9 @@ struct cpuidle_state {
  * cpuidle_get_statedata - retrieves private driver state data
  * @state: the state
  */
-static inline void * cpuidle_get_statedata(struct cpuidle_state *state)
+static inline void *cpuidle_get_statedata(struct cpuidle_state_stats *stats)
 {
-	return state->driver_data;
+	return stats->driver_data;
 }
 
 /**
@@ -66,9 +70,9 @@ static inline void * cpuidle_get_statedata(struct cpuidle_state *state)
  * @data: the private data
  */
 static inline void
-cpuidle_set_statedata(struct cpuidle_state *state, void *data)
+cpuidle_set_statedata(struct cpuidle_state_stats *state_stats, void *data)
 {
-	state->driver_data = data;
+	state_stats->driver_data = data;
 }
 
 struct cpuidle_state_kobj {
@@ -77,29 +81,42 @@ struct cpuidle_state_kobj {
 	struct kobject kobj;
 };
 
+struct cpuidle_device_stats {
+	unsigned int			enabled:1;
+	unsigned int			cpu;
+	int				last_residency;
+	struct cpuidle_state		*last_state;
+	struct cpuidle_state_stats	state_stats[CPUIDLE_STATE_MAX];
+
+	struct cpuidle_state_kobj	*kobjs[CPUIDLE_STATE_MAX];
+	struct kobject			kobj;
+	struct completion		kobj_unregister;
+
+	struct cpuidle_driver		*drv;
+};
+
 struct cpuidle_device {
 	unsigned int		registered:1;
 	unsigned int		enabled:1;
 	unsigned int		power_specified:1;
-	unsigned int		cpu;
 
-	int			last_residency;
 	int			state_count;
 	struct cpuidle_state	states[CPUIDLE_STATE_MAX];
-	struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX];
-	struct cpuidle_state	*last_state;
 
-	struct list_head 	device_list;
 	struct cpuidle_driver	*drv;
-	struct kobject		kobj;
-	struct completion	kobj_unregister;
 	void			*governor_data;
 	struct cpuidle_state	*safe_state;
 
-	int (*prepare)		(struct cpuidle_device *dev);
+	int (*prepare)		(struct cpuidle_device_stats *dev_stats);
 };
 
-DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+
+DECLARE_PER_CPU(struct cpuidle_device_stats, cpuidle_dev_stats);
+static inline struct cpuidle_state_stats *get_cpuidle_state_stats(int cpu,
+						int count)
+{
+	return &per_cpu(cpuidle_dev_stats, cpu).state_stats[count];
+}
 
 /**
  * cpuidle_get_last_residency - retrieves the last state's residency time
@@ -107,7 +124,7 @@ DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  *
  * NOTE: this value is invalid if CPUIDLE_FLAG_TIME_VALID isn't set
  */
-static inline int cpuidle_get_last_residency(struct cpuidle_device *dev)
+static inline int cpuidle_get_last_residency(struct cpuidle_device_stats *dev)
 {
 	return dev->last_residency;
 }
@@ -122,6 +139,7 @@ struct cpuidle_driver {
 	struct module 		*owner;
 	unsigned int		priority;
 	struct list_head	driver_list;
+	struct cpuidle_device	*dev;
 };
 
 #ifdef CONFIG_CPU_IDLE
@@ -145,12 +163,22 @@ static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
 static inline void cpuidle_unregister_driver(struct cpuidle_driver *drv) { }
 static inline int cpuidle_register_device(struct cpuidle_device *dev)
 {return -ENODEV; }
+static inline int cpuidle_register_device_stats(
+				struct cpuidle_device_stats *dev_stats)
+{return -ENODEV; }
+static inline void cpuidle_unregister_device_stats(
+			struct cpuidle_device_stats *dev_stats) { };
 static inline void cpuidle_unregister_device(struct cpuidle_device *dev) { }
 
 static inline void cpuidle_pause_and_lock(void) { }
 static inline void cpuidle_resume_and_unlock(void) { }
 static inline int cpuidle_enable_device(struct cpuidle_device *dev)
 {return -ENODEV; }
+static inline int cpuidle_enable_device_stats(
+			struct cpuidle_device_stats *dev_stats)
+{return -ENODEV; }
+static inline void cpuidle_disable_device_stats(
+				struct cpuidle_device_stats *dev_stats) { }
 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
 
 #endif
@@ -164,11 +192,12 @@ struct cpuidle_governor {
 	struct list_head 	governor_list;
 	unsigned int		rating;
 
-	int  (*enable)		(struct cpuidle_device *dev);
-	void (*disable)		(struct cpuidle_device *dev);
+	int  (*enable)		(struct cpuidle_device_stats *dev_stats);
+	void (*disable)		(struct cpuidle_device_stats *dev_stats);
 
-	int  (*select)		(struct cpuidle_device *dev);
-	void (*reflect)		(struct cpuidle_device *dev);
+	int  (*select)		(struct cpuidle_device *dev,
+					struct cpuidle_device_stats *dev_stats);
+	void (*reflect)		(struct cpuidle_device_stats *dev_stats);
 
 	struct module 		*owner;
 };
#5 lore raw

RFC PATCH 2/2 cpuidle: API changes in callers using new cpuidle_state_stats

In reply to Trinabh Gupta

cpuidle subsystem users will need to use struct cpuidle_state *state and struct cpuidle_state_stats *state_stats.

Driver's per-cpu context is stored in cpuidle_state_stats for this RFC. This need to be moved into cpuidle_driver.

---

 drivers/acpi/processor_driver.c    |   19 +---
 drivers/acpi/processor_idle.c      |   49 +++++++---
 drivers/cpuidle/cpuidle.c          |  170 +++++++++++++++++++++++-------------
 drivers/cpuidle/cpuidle.h          |    4 -
 drivers/cpuidle/driver.c           |   24 -----
 drivers/cpuidle/governor.c         |   10 +-
 drivers/cpuidle/governors/Makefile |    2 
 drivers/cpuidle/governors/menu.c   |   19 ++--
 drivers/cpuidle/sysfs.c            |   63 +++++++------
 drivers/idle/default_driver.c      |   40 +++-----
 10 files changed, 210 insertions(+), 190 deletions(-)
drivers/acpi/processor_driver.c +3 -16
index 360a74e..4837861 100644
@@ -503,9 +503,7 @@ static int __cpuinit acpi_processor_add(struct acpi_device *device)
 	acpi_processor_get_throttling_info(pr);
 	acpi_processor_get_limit_info(pr);
 
-
-	if (cpuidle_get_driver() == &acpi_idle_driver)
-		acpi_processor_power_init(pr, device);
+	acpi_processor_power_init(pr, device);
 
 	pr->cdev = thermal_cooling_device_register("Processor", device,
 						&processor_cooling_ops);
@@ -800,17 +798,9 @@ static int __init acpi_processor_init(void)
 
 	memset(&errata, 0, sizeof(errata));
 
-	if (!cpuidle_register_driver(&acpi_idle_driver)) {
-		printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
-			acpi_idle_driver.name);
-	} else {
-		printk(KERN_DEBUG "ACPI: acpi_idle yielding to %s\n",
-			cpuidle_get_driver()->name);
-	}
-
 	result = acpi_bus_register_driver(&acpi_processor_driver);
 	if (result < 0)
-		goto out_cpuidle;
+		goto out;
 
 	acpi_processor_install_hotplug_notify();
 
@@ -822,8 +812,7 @@ static int __init acpi_processor_init(void)
 
 	return 0;
 
-out_cpuidle:
-	cpuidle_unregister_driver(&acpi_idle_driver);
+out:
 
 	return result;
 }
@@ -841,8 +830,6 @@ static void __exit acpi_processor_exit(void)
 
 	acpi_bus_unregister_driver(&acpi_processor_driver);
 
-	cpuidle_unregister_driver(&acpi_idle_driver);
-
 	return;
 }
 
drivers/acpi/processor_idle.c +33 -16

127 lines not on this page · raw message

drivers/cpuidle/cpuidle.c +107 -63

329 lines not on this page · raw message

drivers/cpuidle/cpuidle.h +2 -2
index 33e50d5..ea48dd8 100644
@@ -24,8 +24,8 @@ extern int cpuidle_switch_governor(struct cpuidle_governor *gov);
 /* sysfs */
 extern int cpuidle_add_class_sysfs(struct sysdev_class *cls);
 extern void cpuidle_remove_class_sysfs(struct sysdev_class *cls);
-extern int cpuidle_add_state_sysfs(struct cpuidle_device *device);
-extern void cpuidle_remove_state_sysfs(struct cpuidle_device *device);
+extern int cpuidle_add_state_sysfs(struct cpuidle_device_stats *dev_stats);
+extern void cpuidle_remove_state_sysfs(struct cpuidle_device_stats *dev_stats);
 extern int cpuidle_add_sysfs(struct sys_device *sysdev);
 extern void cpuidle_remove_sysfs(struct sys_device *sysdev);
 
drivers/cpuidle/driver.c +2 -22
index c235286..19b2000 100644
@@ -38,18 +38,13 @@ static struct cpuidle_driver *select_cpuidle_driver(void)
 
 static void set_current_cpuidle_driver(struct cpuidle_driver *drv)
 {
-	struct cpuidle_device *item = NULL;
 	if (drv == cpuidle_curr_driver)
 		return;
 
 	/* Unregister the previous drivers devices */
 	/* Do we need to take cpuidle_lock ? {un}register_device takes lock*/
 	if (cpuidle_curr_driver) {
-		list_for_each_entry(item, &cpuidle_detected_devices,
-			device_list) {
-			if (item->drv == cpuidle_curr_driver)
-				cpuidle_unregister_device(item);
-		}
+		cpuidle_unregister_device(cpuidle_curr_driver->dev);
 	}
 
 	cpuidle_curr_driver = drv;
@@ -57,12 +52,7 @@ static void set_current_cpuidle_driver(struct cpuidle_driver *drv)
 	if (drv == NULL)
 		return;
 	else {
-		/* Register the new driver devices */
-		list_for_each_entry(item, &cpuidle_detected_devices,
-			device_list) {
-			if (item->drv == drv)
-				cpuidle_register_device(item);
-		}
+		cpuidle_register_device(drv->dev);
 	}
 }
 
@@ -143,8 +133,6 @@ EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  */
 void cpuidle_unregister_driver(struct cpuidle_driver *drv)
 {
-	struct cpuidle_device *item = NULL;
-
 	if (arch_allow_unregister(drv))
 		return;
 
@@ -154,14 +142,6 @@ void cpuidle_unregister_driver(struct cpuidle_driver *drv)
 	list_del(&drv->driver_list);
 	set_current_cpuidle_driver(select_cpuidle_driver());
 
-	/* Delete all devices corresponding to this driver */
-	mutex_lock(&cpuidle_lock);
-	list_for_each_entry(item, &cpuidle_detected_devices, device_list) {
-		if (item->drv == drv)
-			list_del(&item->device_list);
-	}
-	mutex_unlock(&cpuidle_lock);
-
 	spin_unlock(&cpuidle_driver_lock);
 }
 
drivers/cpuidle/governor.c +5 -5
index 724c164..c4bf5f0 100644
@@ -43,7 +43,7 @@ static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
  */
 int cpuidle_switch_governor(struct cpuidle_governor *gov)
 {
-	struct cpuidle_device *dev;
+	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
 
 	if (gov == cpuidle_curr_governor)
 		return 0;
@@ -51,8 +51,8 @@ int cpuidle_switch_governor(struct cpuidle_governor *gov)
 	cpuidle_uninstall_idle_handler();
 
 	if (cpuidle_curr_governor) {
-		list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
-			cpuidle_disable_device(dev);
+		if (cpuidle_driver)
+			cpuidle_disable_device(cpuidle_driver->dev);
 		module_put(cpuidle_curr_governor->owner);
 	}
 
@@ -61,8 +61,8 @@ int cpuidle_switch_governor(struct cpuidle_governor *gov)
 	if (gov) {
 		if (!try_module_get(cpuidle_curr_governor->owner))
 			return -EINVAL;
-		list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
-			cpuidle_enable_device(dev);
+		if (cpuidle_driver)
+			cpuidle_enable_device(cpuidle_driver->dev);
 		cpuidle_install_idle_handler();
 		printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
 	}
drivers/cpuidle/governors/Makefile +1 -1
index 1b51272..a2afe56 100644
@@ -2,5 +2,5 @@
 # Makefile for cpuidle governors.
 #
 
-obj-$(CONFIG_CPU_IDLE_GOV_LADDER) += ladder.o
+obj-n += ladder.o
 obj-$(CONFIG_CPU_IDLE_GOV_MENU) += menu.o
drivers/cpuidle/governors/menu.c +11 -8
index f508690..8de7b76 100644
@@ -182,7 +182,8 @@ static inline int performance_multiplier(void)
 
 static DEFINE_PER_CPU(struct menu_device, menu_devices);
 
-static void menu_update(struct cpuidle_device *dev);
+static void menu_update(struct cpuidle_device *dev,
+			struct cpuidle_device_stats *dev_stats);
 
 /* This implements DIV_ROUND_CLOSEST but avoids 64 bit division */
 static u64 div_round64(u64 dividend, u32 divisor)
@@ -230,7 +231,8 @@ static void detect_repeating_patterns(struct menu_device *data)
  * menu_select - selects the next idle state to enter
  * @dev: the CPU
  */
-static int menu_select(struct cpuidle_device *dev)
+static int menu_select(struct cpuidle_device *dev,
+			struct cpuidle_device_stats *dev_stats)
 {
 	struct menu_device *data = &__get_cpu_var(menu_devices);
 	int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
@@ -239,7 +241,7 @@ static int menu_select(struct cpuidle_device *dev)
 	int multiplier;
 
 	if (data->needs_update) {
-		menu_update(dev);
+		menu_update(dev, dev_stats);
 		data->needs_update = 0;
 	}
 
@@ -312,7 +314,7 @@ static int menu_select(struct cpuidle_device *dev)
  * NOTE: it's important to be fast here because this operation will add to
  *       the overall exit latency.
  */
-static void menu_reflect(struct cpuidle_device *dev)
+static void menu_reflect(struct cpuidle_device_stats *dev_stats)
 {
 	struct menu_device *data = &__get_cpu_var(menu_devices);
 	data->needs_update = 1;
@@ -322,11 +324,12 @@ static void menu_reflect(struct cpuidle_device *dev)
  * menu_update - attempts to guess what happened after entry
  * @dev: the CPU
  */
-static void menu_update(struct cpuidle_device *dev)
+static void menu_update(struct cpuidle_device *dev,
+			struct cpuidle_device_stats *dev_stats)
 {
 	struct menu_device *data = &__get_cpu_var(menu_devices);
 	int last_idx = data->last_state_idx;
-	unsigned int last_idle_us = cpuidle_get_last_residency(dev);
+	unsigned int last_idle_us = cpuidle_get_last_residency(dev_stats);
 	struct cpuidle_state *target = &dev->states[last_idx];
 	unsigned int measured_us;
 	u64 new_factor;
@@ -383,9 +386,9 @@ static void menu_update(struct cpuidle_device *dev)
  * menu_enable_device - scans a CPU's states and does setup
  * @dev: the CPU
  */
-static int menu_enable_device(struct cpuidle_device *dev)
+static int menu_enable_device(struct cpuidle_device_stats *dev_stats)
 {
-	struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
+	struct menu_device *data = &per_cpu(menu_devices, dev_stats->cpu);
 
 	memset(data, 0, sizeof(struct menu_device));
 
drivers/cpuidle/sysfs.c +33 -30

166 lines not on this page · raw message

drivers/idle/default_driver.c +13 -27
index c8702b5..41ad63d 100644
@@ -275,28 +275,32 @@ static void c1e_idle(void)
 }
 
 static int poll_idle_wrapper(struct cpuidle_device *dev,
-		struct cpuidle_state *state)
+		struct cpuidle_state *state,
+		struct cpuidle_state_stats *state_stats)
 {
 	poll_idle();
 	return 0;
 }
 
 static int mwait_idle_wrapper(struct cpuidle_device *dev,
-		struct cpuidle_state *state)
+		struct cpuidle_state *state,
+		struct cpuidle_state_stats *state_stats)
 {
 	mwait_idle();
 	return 0;
 }
 
 static int c1e_idle_wrapper(struct cpuidle_device *dev,
-		struct cpuidle_state *state)
+		struct cpuidle_state *state,
+		struct cpuidle_state_stats *state_stats)
 {
 	c1e_idle();
 	return 0;
 }
 
 int default_idle_wrapper(struct cpuidle_device *dev,
-		struct cpuidle_state *state)
+		struct cpuidle_state *state,
+		struct cpuidle_state_stats *state_stats)
 {
 	default_idle();
 	return 0;
@@ -305,7 +309,6 @@ int default_idle_wrapper(struct cpuidle_device *dev,
 static struct cpuidle_state state_poll = {
 		.name = "POLL",
 		.desc = "POLL",
-		.driver_data = (void *) 0x00,
 		.flags = CPUIDLE_FLAG_TIME_VALID,
 		.exit_latency = 1,
 		.target_residency = 1,
@@ -315,7 +318,6 @@ static struct cpuidle_state state_poll = {
 static struct cpuidle_state state_mwait = {
 		.name = "C1",
 		.desc = "MWAIT No Hints",
-		.driver_data = (void *) 0x01,
 		.flags = CPUIDLE_FLAG_TIME_VALID,
 		.exit_latency = 1,
 		.target_residency = 1,
@@ -325,7 +327,6 @@ static struct cpuidle_state state_mwait = {
 static struct cpuidle_state state_c1e = {
 		.name = "C1E",
 		.desc = "C1E",
-		.driver_data = (void *) 0x02,
 		.flags = CPUIDLE_FLAG_TIME_VALID,
 		.exit_latency = 1,
 		.target_residency = 1,
@@ -335,7 +336,6 @@ static struct cpuidle_state state_c1e = {
 struct cpuidle_state state_default_idle = {
 		.name = "DEFAULT-IDLE",
 		.desc = "Default idle routine",
-		.driver_data = (void *) 0x03,
 		.flags = CPUIDLE_FLAG_TIME_VALID,
 		.exit_latency = 1,
 		.target_residency = 1,
@@ -416,34 +416,20 @@ static struct cpuidle_driver default_idle_driver = {
 	.priority = 100,
 };
 
-static int setup_cpuidle(int cpu)
+static int __init default_idle_init(void)
 {
-	struct cpuidle_device *dev = kzalloc(sizeof(struct cpuidle_device),
-					GFP_KERNEL);
+	int retval;
 	int count = CPUIDLE_DRIVER_STATE_START;
-	dev->cpu = cpu;
+	struct cpuidle_device *dev = kzalloc(sizeof(struct cpuidle_device),
+						GFP_KERNEL);
 	dev->drv = &default_idle_driver;
-
-	BUG_ON(opt_state == NULL);
 	dev->states[count] = *opt_state;
 	count++;
-
 	dev->state_count = count;
 
-	if (cpuidle_register_device(dev))
-		return -EIO;
-	return 0;
-}
-
-static int __init default_idle_init(void)
-{
-	int retval, i;
+	default_idle_driver.dev = dev;
 	retval = cpuidle_register_driver(&default_idle_driver);
 
-	for_each_online_cpu(i) {
-		setup_cpuidle(i);
-	}
-
 	return 0;
 }
 
reviewer
#6 lore raw
In reply to Trinabh Gupta
Quoted 25 lines · Trinabh Gupta

This patch series is an early RFC to discuss the feasibility of avoiding registering of idle states from each cpu.

The core change is to split the cpuidle_device structure into parts that can be global and parts that has to remain per-cpu. The per-cpu pieces are mostly generic statistics that can be independent of current running driver.

Motivation:
* Simplify the cpuidle subsystem framework and have registration/unregistration done by single cpu.

* Minimise the data structure that needs to be maintained for multiple cpuidle drivers

* Reference: https://lkml.org/lkml/2011/2/10/37

Advantages:
* Make the cpuidle framework simple for most use cases where C-States are symmetric. In case there are asymmetric C-States detected, fallback mechanism should be incorporated to maintain the system functional
https://lkml.org/lkml/2011/2/10/257
https://lkml.org/lkml/2011/2/10/37

Quoted 7 lines

* Non x86 archs that does not have asymmetric C-States like POWER, may not need the fallback mechanism and hence the framework will be simple for most use cases.

Disadvantages:
* Asymmetric C-States are part of x86 ACPI specification. Incorrect handling may functionally affect the system

I think this is a non-issue.

Quoted 3 lines

* Incorporating per-cpu masks for each state to allow/dis-allow global states on subset of CPUs may result in an implementation that is not better than current solution of having per-cpu states.

I don't think this is needed.

Quoted 10 lines

This patch series applies on top of the pm_idle cleanup patch
https://lkml.org/lkml/2011/3/22/150 (cpuidle: Cleanup pm_idle and include driver/cpuidle.c in-kernel)

This patch series is tested on x86 Nehalem system with multiple ACPI C-States.

This patch series has limitations of not handling multiple driver registration and switching between drivers on all CPUs mainly due to incomplete handling of per-cpu enable/disable and driver_data.

I think this series is more important than the feature of multiple driver/system support, and thus should come first.

thanks,
Len Brown, Intel Open Source Technology Center

reviewer
#2 lore raw

Re: [RFC PATCH V1 1/2] cpuidle: Data structure changes for global cpuidle device

In reply to Trinabh Gupta

I agree it is silly to allocate a cpuidle_device
for every cpu in the system as we do today.

Yes, splitting the counters out of cpuidle_device
is a necessary part of fixing that.

However, cpuidle_device.cpuidle_state[] is currently not per-driver, it is per-cpu, and it is writable.

In particular, the cpuidle_device->prepare() mechanism causes updates to the cpuidle_state[].flags, setting and clearing CPUIDLE_FLAG_IGNORE to tell the governor not to chose a state
on a per-cpu basis at run-time.

I don't like that mechanism.
I'd like to see it replaced, and when replaced,
cpuidle_state[] can be per system-wide driver.

I think the real problem that prepare() was trying to solve is that the driver today does not have the ability to over-rule the choice made by the governor. The driver may discover in the course of trying to satisfy the request of the governor that it needs to demote to a shallower state; or it may do its best to satisfy the governor's request, and the hardware may demote its request to a shallower state.

Unfortunately, when this happens, the driver dutifully returns the time spent in the state to cpuidle_idle_call(), who then updates the wrong last_residency, time, and usage counters.

Sure is ironic for the driver to allocate the data structures and then hand the timer to the uppper layer, just to have the upper layer update the wrong data structures...

Surely the driver enter routine should update the counters that the driver was obligated to allocate, and it should return the state actually entered (for tracing), rather than the time spent there.

The generic cpuidle code should simply handle where the counters live in the sysfs namespace, not updating the counters.

This needs to be addressed before cpuidle_device.cpuidle_state[] can be made one/system.

cheers,
Len Brown, Intel Open Source Technology Center

#7 lore raw
In reply to Len Brown
Quoted 31 lines · Len Brown
Quoted 29 lines · Trinabh Gupta

This patch series is an early RFC to discuss the feasibility of avoiding registering of idle states from each cpu.

The core change is to split the cpuidle_device structure into parts that can be global and parts that has to remain per-cpu. The per-cpu pieces are mostly generic statistics that can be independent of current running driver.

Motivation:
* Simplify the cpuidle subsystem framework and have registration/unregistration done by single cpu.

* Minimise the data structure that needs to be maintained for multiple cpuidle drivers

* Reference: https://lkml.org/lkml/2011/2/10/37

Advantages:
* Make the cpuidle framework simple for most use cases where C-States are symmetric. In case there are asymmetric C-States detected, fallback mechanism should be incorporated to maintain the system functional
https://lkml.org/lkml/2011/2/10/257
https://lkml.org/lkml/2011/2/10/37

* Non x86 archs that does not have asymmetric C-States like POWER, may not need the fallback mechanism and hence the framework will be simple for most use cases.

Quoted 5 lines
Quoted 3 lines

Disadvantages:
* Asymmetric C-States are part of x86 ACPI specification. Incorrect handling may functionally affect the system

I think this is a non-issue.

Hi Len,

Thanks for the confirmation. This gives us the right direction to proceed with the cleanup.

Quoted 13 lines
Quoted 3 lines

* Incorporating per-cpu masks for each state to allow/dis-allow global states on subset of CPUs may result in an implementation that is not better than current solution of having per-cpu states.

I don't think this is needed.

Quoted 7 lines

This patch series applies on top of the pm_idle cleanup patch
https://lkml.org/lkml/2011/3/22/150 (cpuidle: Cleanup pm_idle and include driver/cpuidle.c in-kernel)

This patch series is tested on x86 Nehalem system with multiple ACPI C-States.

Quoted 6 lines
Quoted 3 lines

This patch series has limitations of not handling multiple driver registration and switching between drivers on all CPUs mainly due to incomplete handling of per-cpu enable/disable and driver_data.

I think this series is more important than the feature of multiple driver/system support, and thus should come first.

Removing pm_idle() gets us to the situation where we have to support multiple registration to allow default_idle driver to register and do mwait. Your cleanup patch to x86 idle will make the transition much simple. Lets us have removal of pm_idle() and removal of per-cpu registration in the same patch series as the next step.

--Vaidy

#3 lore raw

Re: [RFC PATCH V1 1/2] cpuidle: Data structure changes for global cpuidle device

In reply to Len Brown
Quoted 15 lines · Len Brown

I agree it is silly to allocate a cpuidle_device
for every cpu in the system as we do today.

Yes, splitting the counters out of cpuidle_device
is a necessary part of fixing that.

However, cpuidle_device.cpuidle_state[] is currently not per-driver, it is per-cpu, and it is writable.

In particular, the cpuidle_device->prepare() mechanism causes updates to the cpuidle_state[].flags, setting and clearing CPUIDLE_FLAG_IGNORE to tell the governor not to chose a state
on a per-cpu basis at run-time.

Quoted 3 lines

I don't like that mechanism.
I'd like to see it replaced, and when replaced,
cpuidle_state[] can be per system-wide driver.

Thanks for the detailed review. I agree that we should rework handling of the cpuidle_state[].flags. However, is the prepare() mechanism used at all? Can we remove the option completely?

Quoted 11 lines

I think the real problem that prepare() was trying to solve is that the driver today does not have the ability to over-rule the choice made by the governor. The driver may discover in the course of trying to satisfy the request of the governor that it needs to demote to a shallower state; or it may do its best to satisfy the governor's request, and the hardware may demote its request to a shallower state.

Unfortunately, when this happens, the driver dutifully returns the time spent in the state to cpuidle_idle_call(), who then updates the wrong last_residency, time, and usage counters.

I did not get this scenario. Are you saying

target_state->enter(dev, target_state) can enter a different state than the one suggested by target_state?

I understand the hardware demotion part, but can we really detect the target 'demoted' state in that case? I guess not.

Quoted 8 lines

Sure is ironic for the driver to allocate the data structures and then hand the timer to the uppper layer, just to have the upper layer update the wrong data structures...

Surely the driver enter routine should update the counters that the driver was obligated to allocate, and it should return the state actually entered (for tracing), rather than the time spent there.

Can we do something like this:

last_state = target_state->enter(dev, target_state)

dev->last_state and dev->last_residency are updated inside target_state->enter()

The returned last_state is just for tracing, actual data is already updated in the cpuidle_dev structure and used for sysfs display.

Quoted 4 lines

The generic cpuidle code should simply handle where the counters live in the sysfs namespace, not updating the counters.
This needs to be addressed before cpuidle_device.cpuidle_state[] can be made one/system.

Agreed.

Thanks again for the recommendations.

--Vaidy

reviewer
#4 lore raw

Re: [RFC PATCH V1 1/2] cpuidle: Data structure changes for global cpuidle device

Quoted 8 lines
Quoted 8 lines

I think the real problem that prepare() was trying to solve is that the driver today does not have the ability to over-rule the choice made by the governor. The driver may discover in the course of trying to satisfy the request of the governor that it needs to demote to a shallower state; or it may do its best to satisfy the governor's request, and the hardware may demote its request to a shallower state.

Quoted 8 lines
Quoted 3 lines

Unfortunately, when this happens, the driver dutifully returns the time spent in the state to cpuidle_idle_call(), who then updates the wrong last_residency, time, and usage counters.

I did not get this scenario. Are you saying

target_state->enter(dev, target_state) can enter a different state than the one suggested by target_state?

Yes.

Quoted 2 lines

I understand the hardware demotion part, but can we really detect the target 'demoted' state in that case? I guess not.

In addition to HW demotion, SW may also notice at the last minute that some criteria for entering a state is not met, and need to demote in SW.

Effectively it is the same as HW demotion, except SW can see that it is going to happen ahead of time.

The example is MRST S0i3, where a number of dependencies on SOC device state must be met to enter the state, and those dependencies can change state at any time.

Quoted 11 lines
Quoted 8 lines

Sure is ironic for the driver to allocate the data structures and then hand the timer to the uppper layer, just to have the upper layer update the wrong data structures...

Surely the driver enter routine should update the counters that the driver was obligated to allocate, and it should return the state actually entered (for tracing), rather than the time spent there.

Can we do something like this:

Quoted 7 lines

last_state = target_state->enter(dev, target_state)

dev->last_state and dev->last_residency are updated inside target_state->enter()

The returned last_state is just for tracing, actual data is already updated in the cpuidle_dev structure and used for sysfs display.

dev->last_state and
dev->last_residency
seem like part of hacky support for run-time changing of c-states.

I think if we move the couter incrementing into the driver, then the upper layer shouldn't need to care about it. The driver will always have a better idea of what happened than cpuidle_idle_call().

Quoted 6 lines
Quoted 4 lines

The generic cpuidle code should simply handle where the counters live in the sysfs namespace, not updating the counters.
This needs to be addressed before cpuidle_device.cpuidle_state[] can be made one/system.

Agreed.

thanks,
Len Brown, Intel Open Source Technolgy Center