Skip to contentSkip to conversation

idle/cpuidle: Split cpuidle_idle_call main function into smaller functions

PATCH Superseded Daniel Lezcano 3 participants 15 messages Feb 11, 2014 – Feb 12, 2014

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 idle/cpuidle: Split cpuidle_idle_call main function into smaller functions

In order to allow better integration between the cpuidle framework and the scheduler, reducing the distance between these two sub-components will facilitate this integration by moving part of the cpuidle code in the idle task file and, because idle.c is in the sched directory, we have access to the scheduler's private structures.

This patch splits the cpuidle_idle_call main entry function into 3 calls to a newly added API:
1. select the idle state
2. enter the idle state
3. reflect the idle state

The cpuidle_idle_call calls these three functions to implement the main idle entry function.

---
 drivers/cpuidle/cpuidle.c |  102 ++++++++++++++++++++++++++++++++-------------
 include/linux/cpuidle.h   |   14 +++++++
 2 files changed, 87 insertions(+), 29 deletions(-)
drivers/cpuidle/cpuidle.c +73 -29
index a55e68f..172ab6a 100644
@@ -108,6 +108,71 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 }
 
 /**
+ * cpuidle_select - ask the cpuidle framework to choose an idle state
+ *
+ * @drv: the cpuidle driver
+ * @dev: the cpuidle device
+ *
+ * Returns the index of the idle state. On error it returns:
+ * -NODEV : the cpuidle framework is not available
+ * -EBUSY : the cpuidle framework is not initialized
+ */
+int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
+{
+	if (off || !initialized)
+		return -ENODEV;
+
+	if (!drv || !dev || !dev->enabled)
+		return -EBUSY;
+
+	return cpuidle_curr_governor->select(drv, dev);
+}
+
+/**
+ * cpuidle_enter - enter into the specified idle state
+ *
+ * @drv:   the cpuidle driver tied with the cpu
+ * @dev:   the cpuidle device
+ * @index: the index in the idle state table
+ *
+ * Returns the index in the idle state, < 0 in case of error.
+ * The error code depends on the backend driver
+ */
+int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+		  int index)
+{
+	int entered_state;
+	bool broadcast = !!(drv->states[index].flags & CPUIDLE_FLAG_TIMER_STOP);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
+
+	if (cpuidle_state_is_coupled(dev, drv, index))
+		entered_state = cpuidle_enter_state_coupled(dev, drv, index);
+	else
+		entered_state = cpuidle_enter_state(dev, drv, index);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
+
+	return entered_state;
+}
+
+/**
+ * cpuidle_reflect - tell the underlying governor what was the state
+ * we were in
+ *
+ * @dev  : the cpuidle device
+ * @index: the index in the idle state table
+ *
+ */
+void cpuidle_reflect(struct cpuidle_device *dev, int index)
+{
+	if (cpuidle_curr_governor->reflect)
+		cpuidle_curr_governor->reflect(dev, index);
+}
+
+/**
  * cpuidle_idle_call - the main idle loop
  *
  * NOTE: no locks or semaphores should be used here
@@ -116,51 +181,30 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 int cpuidle_idle_call(void)
 {
 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
-	struct cpuidle_driver *drv;
+	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
-	bool broadcast;
-
-	if (off || !initialized)
-		return -ENODEV;
-
-	/* check if the device is ready */
-	if (!dev || !dev->enabled)
-		return -EBUSY;
-
-	drv = cpuidle_get_cpu_driver(dev);
 
 	/* ask the governor for the next state */
-	next_state = cpuidle_curr_governor->select(drv, dev);
+	next_state = cpuidle_select(drv, dev);
+	if (next_state < 0)
+		return next_state;
+
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
-		if (cpuidle_curr_governor->reflect)
-			cpuidle_curr_governor->reflect(dev, next_state);
+		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
 		return 0;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
 
-	broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
-
-	if (cpuidle_state_is_coupled(dev, drv, next_state))
-		entered_state = cpuidle_enter_state_coupled(dev, drv,
-							    next_state);
-	else
-		entered_state = cpuidle_enter_state(dev, drv, next_state);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
+	entered_state = cpuidle_enter(drv, dev, next_state);
 
 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
 
 	/* give the governor an opportunity to reflect on the outcome */
-	if (cpuidle_curr_governor->reflect)
-		cpuidle_curr_governor->reflect(dev, entered_state);
+	cpuidle_reflect(dev, entered_state);
 
 	return 0;
 }
include/linux/cpuidle.h +14 -0
index 50fcbb0..bf06f37 100644
@@ -119,6 +119,13 @@ struct cpuidle_driver {
 
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
+
+extern int cpuidle_select(struct cpuidle_driver *drv,
+			  struct cpuidle_device *dev);
+extern int cpuidle_enter(struct cpuidle_driver *drv,
+			 struct cpuidle_device *dev, int index);
+extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
+
 extern int cpuidle_idle_call(void);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 extern struct cpuidle_driver *cpuidle_get_driver(void);
@@ -141,6 +148,13 @@ extern int cpuidle_play_dead(void);
 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
 #else
 static inline void disable_cpuidle(void) { }
+static inline int cpuidle_select(struct cpuidle_driver *drv,
+				 struct cpuidle_device *dev)
+{return -ENODEV; }
+static inline int cpuidle_enter(struct cpuidle_driver *drv,
+				struct cpuidle_device *dev, int index)
+{return -ENODEV; }
+static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 2/5 cpuidle/idle: Move the cpuidle_idle_call function to idle.c

The cpuidle_idle_call does nothing more than calling the three individuals function and is no longer used by any arch specific code but only in the cpuidle framework code.

We can move this function into the idle task code to ensure better proximity to the scheduler code.

---
 drivers/cpuidle/cpuidle.c |   37 -------------------------------------
 include/linux/cpuidle.h   |    2 --
 kernel/sched/idle.c       |   44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 39 deletions(-)
drivers/cpuidle/cpuidle.c +0 -37
index 172ab6a..ae3078b 100644
@@ -173,43 +173,6 @@ void cpuidle_reflect(struct cpuidle_device *dev, int index)
 }
 
 /**
- * cpuidle_idle_call - the main idle loop
- *
- * NOTE: no locks or semaphores should be used here
- * return non-zero on failure
- */
-int cpuidle_idle_call(void)
-{
-	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
-	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
-	int next_state, entered_state;
-
-	/* ask the governor for the next state */
-	next_state = cpuidle_select(drv, dev);
-	if (next_state < 0)
-		return next_state;
-
-	if (need_resched()) {
-		dev->last_residency = 0;
-		/* give the governor an opportunity to reflect on the outcome */
-		cpuidle_reflect(dev, next_state);
-		local_irq_enable();
-		return 0;
-	}
-
-	trace_cpu_idle_rcuidle(next_state, dev->cpu);
-
-	entered_state = cpuidle_enter(drv, dev, next_state);
-
-	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
-
-	/* give the governor an opportunity to reflect on the outcome */
-	cpuidle_reflect(dev, entered_state);
-
-	return 0;
-}
-
-/**
  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  */
 void cpuidle_install_idle_handler(void)
include/linux/cpuidle.h +0 -2
index bf06f37..0befaff 100644
@@ -126,7 +126,6 @@ extern int cpuidle_enter(struct cpuidle_driver *drv,
 			 struct cpuidle_device *dev, int index);
 extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
 
-extern int cpuidle_idle_call(void);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 extern struct cpuidle_driver *cpuidle_get_driver(void);
 extern struct cpuidle_driver *cpuidle_driver_ref(void);
@@ -155,7 +154,6 @@ static inline int cpuidle_enter(struct cpuidle_driver *drv,
 				struct cpuidle_device *dev, int index)
 {return -ENODEV; }
 static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
-static inline int cpuidle_idle_call(void) { 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; }
kernel/sched/idle.c +44 -0

50 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 2/5] cpuidle/idle: Move the cpuidle_idle_call function to idle.c

Hi Daniel,

Quoted 6 lines · Daniel Lezcano

The cpuidle_idle_call does nothing more than calling the three individuals function and is no longer used by any arch specific code but only in the cpuidle framework code.

We can move this function into the idle task code to ensure better proximity to the scheduler code.

So my understanding of this patchset is that by moving cpuidle_idle_call() under kernel/sched, we now have a way of calling into the cpuidle governor and the cpuidle driver with additional parameters like cpu_load(), idle_stamp etc.. so that we can expect the governor and driver to take better decisions about entry and exit into idle states. Is this the advantage we hope to begin with?

Thanks

Regards
Preeti U Murthy

Quoted 4 lines

---

#3 lore raw

Re: [PATCH 2/5] cpuidle/idle: Move the cpuidle_idle_call function to idle.c

Quoted 10 lines · Preeti U Murthy

Hi Daniel,

Quoted 6 lines · Daniel Lezcano

The cpuidle_idle_call does nothing more than calling the three individuals function and is no longer used by any arch specific code but only in the cpuidle framework code.

We can move this function into the idle task code to ensure better proximity to the scheduler code.

Quoted 6 lines

So my understanding of this patchset is that by moving cpuidle_idle_call() under kernel/sched, we now have a way of calling into the cpuidle governor and the cpuidle driver with additional parameters like cpu_load(), idle_stamp etc.. so that we can expect the governor and driver to take better decisions about entry and exit into idle states. Is this the advantage we hope to begin with?

Yes, right.

Signature · 5 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
#4 lore raw

PATCH 3/5 idle: Reorganize the idle loop

In reply to Daniel Lezcano

Now that we have the main cpuidle function in idle.c, move some code from the idle mainloop to this function for the sake of clarity.

That removes if then else indentation difficult to follow when looking at the code. This patch does not the change the current behavior.

---
 include/linux/cpuidle.h |    2 ++
 kernel/sched/idle.c     |   39 ++++++++++++++++++++-------------------
 2 files changed, 22 insertions(+), 19 deletions(-)
include/linux/cpuidle.h +2 -0
index 0befaff..a8d5bd3 100644
@@ -175,6 +175,8 @@ static inline int cpuidle_enable_device(struct cpuidle_device *dev)
 {return -ENODEV; }
 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
+static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
+	struct cpuidle_device *dev) {return NULL; }
 #endif
 
 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
kernel/sched/idle.c +20 -19
index 6963822..8b10891 100644
@@ -63,7 +63,6 @@ void __weak arch_cpu_idle(void)
 	local_irq_enable();
 }
 
-#ifdef CONFIG_CPU_IDLE
 /**
  * cpuidle_idle_call - the main idle function
  *
@@ -76,17 +75,26 @@ static int cpuidle_idle_call(void)
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
 
-	/* ask the governor for the next state */
+	stop_critical_timings();
+	rcu_idle_enter();
+
+	/* Ask the governor for the next state, this call can fail for
+	 * different reasons: cpuidle is not enabled or an idle state
+	 * fulfilling the constraints was not found. In this case, we fall
+	 * back to the default idle function
+	 */
 	next_state = cpuidle_select(drv, dev);
-	if (next_state < 0)
-		return next_state;
+	if (next_state < 0) {
+		arch_cpu_idle();
+		goto out;
+	}
 
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
 		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
-		return 0;
+		goto out;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
@@ -97,15 +105,15 @@ static int cpuidle_idle_call(void)
 
 	/* give the governor an opportunity to reflect on the outcome */
 	cpuidle_reflect(dev, entered_state);
+out:
+	if (WARN_ON_ONCE(irqs_disabled()))
+		local_irq_enable();
+
+	rcu_idle_exit();
+	start_critical_timings();
 
 	return 0;
 }
-#else
-static inline int cpuidle_idle_call(void)
-{
-	return -ENODEV;
-}
-#endif
 
 /*
  * Generic idle loop implementation
@@ -138,14 +146,7 @@ static void cpu_idle_loop(void)
 				cpu_idle_poll();
 			} else {
 				if (!current_clr_polling_and_test()) {
-					stop_critical_timings();
-					rcu_idle_enter();
-					if (cpuidle_idle_call())
-						arch_cpu_idle();
-					if (WARN_ON_ONCE(irqs_disabled()))
-						local_irq_enable();
-					rcu_idle_exit();
-					start_critical_timings();
+					cpuidle_idle_call();
 				} else {
 					local_irq_enable();
 				}
Signature · 1 line
1.7.9.5
Acked-by on v1 3/5
#5 lore raw

Re: [PATCH 3/5] idle: Reorganize the idle loop

Quoted 5 lines · Daniel Lezcano

Now that we have the main cpuidle function in idle.c, move some code from the idle mainloop to this function for the sake of clarity.

That removes if then else indentation difficult to follow when looking at the code. This patch does not the change the current behavior.

s/the change/change/

Quoted 34 lines
---
 include/linux/cpuidle.h |    2 ++
 kernel/sched/idle.c     |   39 ++++++++++++++++++++-------------------
 2 files changed, 22 insertions(+), 19 deletions(-)
include/linux/cpuidle.h +2 -0
index 0befaff..a8d5bd3 100644
@@ -175,6 +175,8 @@ static inline int cpuidle_enable_device(struct cpuidle_device *dev)
 {return -ENODEV; }
 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
+static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
+	struct cpuidle_device *dev) {return NULL; }
 #endif
 
 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
kernel/sched/idle.c +0 -1
index 6963822..8b10891 100644
@@ -63,7 +63,6 @@ void __weak arch_cpu_idle(void)
 	local_irq_enable();
 }
 
-#ifdef CONFIG_CPU_IDLE
 /**
  * cpuidle_idle_call - the main idle function
  *
@@ -76,17 +75,26 @@ static int cpuidle_idle_call(void)
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;

kernel/sched/idle.c static int cpuidle_idle_call(void) · line 85

Quoted 10 lines
kernel/sched/idle.c +8 -1
 
-	/* ask the governor for the next state */
+	stop_critical_timings();
+	rcu_idle_enter();
+
+	/* Ask the governor for the next state, this call can fail for
+	 * different reasons: cpuidle is not enabled or an idle state
+	 * fulfilling the constraints was not found. In this case, we fall
+	 * back to the default idle function
+	 */

Preferred style for multi-line comment blocks is:

	/*
	 * First line here, leaving the opening line empty.
	 * Next line.
	 */

Otherwise...

Quoted 59 lines
kernel/sched/idle.c +12 -17
 	next_state = cpuidle_select(drv, dev);
-	if (next_state < 0)
-		return next_state;
+	if (next_state < 0) {
+		arch_cpu_idle();
+		goto out;
+	}
 
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
 		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
-		return 0;
+		goto out;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
@@ -97,15 +105,15 @@ static int cpuidle_idle_call(void)
 
 	/* give the governor an opportunity to reflect on the outcome */
 	cpuidle_reflect(dev, entered_state);
+out:
+	if (WARN_ON_ONCE(irqs_disabled()))
+		local_irq_enable();
+
+	rcu_idle_exit();
+	start_critical_timings();
 
 	return 0;
 }
-#else
-static inline int cpuidle_idle_call(void)
-{
-	return -ENODEV;
-}
-#endif
 
 /*
  * Generic idle loop implementation
@@ -138,14 +146,7 @@ static void cpu_idle_loop(void)
 				cpu_idle_poll();
 			} else {
 				if (!current_clr_polling_and_test()) {
-					stop_critical_timings();
-					rcu_idle_enter();
-					if (cpuidle_idle_call())
-						arch_cpu_idle();
-					if (WARN_ON_ONCE(irqs_disabled()))
-						local_irq_enable();
-					rcu_idle_exit();
-					start_critical_timings();
+					cpuidle_idle_call();
 				} else {
 					local_irq_enable();
 				}
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH 3/5] idle: Reorganize the idle loop

In reply to Daniel Lezcano

Hi Daniel,

Find below a couple of comments.

Quoted 96 lines · Daniel Lezcano

Now that we have the main cpuidle function in idle.c, move some code from the idle mainloop to this function for the sake of clarity.

That removes if then else indentation difficult to follow when looking at the code. This patch does not the change the current behavior.

---
 include/linux/cpuidle.h |    2 ++
 kernel/sched/idle.c     |   39 ++++++++++++++++++++-------------------
 2 files changed, 22 insertions(+), 19 deletions(-)
include/linux/cpuidle.h +2 -0
index 0befaff..a8d5bd3 100644
@@ -175,6 +175,8 @@ static inline int cpuidle_enable_device(struct cpuidle_device *dev)
 {return -ENODEV; }
 static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
+static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
+	struct cpuidle_device *dev) {return NULL; }
 #endif
 
 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
kernel/sched/idle.c +19 -13
index 6963822..8b10891 100644
@@ -63,7 +63,6 @@ void __weak arch_cpu_idle(void)
 	local_irq_enable();
 }
 
-#ifdef CONFIG_CPU_IDLE
 /**
  * cpuidle_idle_call - the main idle function
  *
@@ -76,17 +75,26 @@ static int cpuidle_idle_call(void)
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
 
-	/* ask the governor for the next state */
+	stop_critical_timings();
+	rcu_idle_enter();
+
+	/* Ask the governor for the next state, this call can fail for
+	 * different reasons: cpuidle is not enabled or an idle state
+	 * fulfilling the constraints was not found. In this case, we fall
+	 * back to the default idle function
+	 */
 	next_state = cpuidle_select(drv, dev);
-	if (next_state < 0)
-		return next_state;
+	if (next_state < 0) {
+		arch_cpu_idle();
+		goto out;
+	}
 
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
 		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
-		return 0;
+		goto out;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
@@ -97,15 +105,15 @@ static int cpuidle_idle_call(void)
 
 	/* give the governor an opportunity to reflect on the outcome */
 	cpuidle_reflect(dev, entered_state);
+out:
+	if (WARN_ON_ONCE(irqs_disabled()))
+		local_irq_enable();
+
+	rcu_idle_exit();
+	start_critical_timings();
 
 	return 0;
 }
-#else
-static inline int cpuidle_idle_call(void)
-{
-	return -ENODEV;
-}
-#endif
 
 /*
  * Generic idle loop implementation
@@ -138,14 +146,7 @@ static void cpu_idle_loop(void)
 				cpu_idle_poll();
 			} else {
 				if (!current_clr_polling_and_test()) {
-					stop_critical_timings();
-					rcu_idle_enter();

kernel/sched/idle.c static void cpu_idle_loop(void) · line 152

Quoted 10 lines
kernel/sched/idle.c +1 -6
-					if (cpuidle_idle_call())
-						arch_cpu_idle();
-					if (WARN_ON_ONCE(irqs_disabled()))
-						local_irq_enable();
-					rcu_idle_exit();
-					start_critical_timings();
+					cpuidle_idle_call();
 				} else {
 					local_irq_enable();
 				}

Is this really necessary? It seems better to let the cpuidle_idle_loop() to handle the cpuidle specific tasks and the generic idle loop to handle the peripheral functions like stop_critical_timings(), rcu_idle_enter() and their counterparts. I am unable to see what we are gaining by doing this.

Besides, cpuidle_idle_call() is expected to just call into the cpuidle governor and driver. What I would have expected this patchset to do is simply move this call under kernel/sched like you have done in your first two patches so as to gain the benefit of using the parameters that the scheduler keeps track of to make better cpuidle state entry decisions.

But going one step too far by moving the other idle handling functions into it would result in some confusion and add more code than it is meant to handle. This will avoid having to add comments in the cpuidle_idle_call() function as currently being done in Patch[5/5], to clarify what each function is meant to do.

So IMO, Patches[1/5] and [2/5] by themselves are sufficient to increase the proximity between scheduler and cpuidle.

Thanks

Regards
Preeti U Murthy

Quoted 1 line
#7 lore raw

Re: [PATCH 3/5] idle: Reorganize the idle loop

Quoted 124 lines · Preeti U Murthy

Hi Daniel,

Find below a couple of comments.

Quoted 96 lines · Daniel Lezcano

Now that we have the main cpuidle function in idle.c, move some code from the idle mainloop to this function for the sake of clarity.

That removes if then else indentation difficult to follow when looking at the code. This patch does not the change the current behavior.

---
  include/linux/cpuidle.h |    2 ++
  kernel/sched/idle.c     |   39 ++++++++++++++++++++-------------------
  2 files changed, 22 insertions(+), 19 deletions(-)
include/linux/cpuidle.h +2 -0
index 0befaff..a8d5bd3 100644
@@ -175,6 +175,8 @@ static inline int cpuidle_enable_device(struct cpuidle_device *dev)
  {return -ENODEV; }
  static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
  static inline int cpuidle_play_dead(void) {return -ENODEV; }
+static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
+	struct cpuidle_device *dev) {return NULL; }
  #endif
 
  #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
kernel/sched/idle.c +19 -13
index 6963822..8b10891 100644
@@ -63,7 +63,6 @@ void __weak arch_cpu_idle(void)
  	local_irq_enable();
  }
 
-#ifdef CONFIG_CPU_IDLE
  /**
   * cpuidle_idle_call - the main idle function
   *
@@ -76,17 +75,26 @@ static int cpuidle_idle_call(void)
  	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  	int next_state, entered_state;
 
-	/* ask the governor for the next state */
+	stop_critical_timings();
+	rcu_idle_enter();
+
+	/* Ask the governor for the next state, this call can fail for
+	 * different reasons: cpuidle is not enabled or an idle state
+	 * fulfilling the constraints was not found. In this case, we fall
+	 * back to the default idle function
+	 */
  	next_state = cpuidle_select(drv, dev);
-	if (next_state < 0)
-		return next_state;
+	if (next_state < 0) {
+		arch_cpu_idle();
+		goto out;
+	}
 
  	if (need_resched()) {
  		dev->last_residency = 0;
  		/* give the governor an opportunity to reflect on the outcome */
  		cpuidle_reflect(dev, next_state);
  		local_irq_enable();
-		return 0;
+		goto out;
  	}
 
  	trace_cpu_idle_rcuidle(next_state, dev->cpu);
@@ -97,15 +105,15 @@ static int cpuidle_idle_call(void)
 
  	/* give the governor an opportunity to reflect on the outcome */
  	cpuidle_reflect(dev, entered_state);
+out:
+	if (WARN_ON_ONCE(irqs_disabled()))
+		local_irq_enable();
+
+	rcu_idle_exit();
+	start_critical_timings();
 
  	return 0;
  }
-#else
-static inline int cpuidle_idle_call(void)
-{
-	return -ENODEV;
-}
-#endif
 
  /*
   * Generic idle loop implementation
@@ -138,14 +146,7 @@ static void cpu_idle_loop(void)
  				cpu_idle_poll();
  			} else {
  				if (!current_clr_polling_and_test()) {
-					stop_critical_timings();
-					rcu_idle_enter();

kernel/sched/idle.c static void cpu_idle_loop(void) · line 152

Quoted 10 lines
kernel/sched/idle.c +1 -6
-					if (cpuidle_idle_call())
-						arch_cpu_idle();
-					if (WARN_ON_ONCE(irqs_disabled()))
-						local_irq_enable();
-					rcu_idle_exit();
-					start_critical_timings();
+					cpuidle_idle_call();
  				} else {
  					local_irq_enable();
  				}

Is this really necessary? It seems better to let the cpuidle_idle_loop() to handle the cpuidle specific tasks and the generic idle loop to handle the peripheral functions like stop_critical_timings(), rcu_idle_enter() and their counterparts. I am unable to see what we are gaining by doing this.

Besides, cpuidle_idle_call() is expected to just call into the cpuidle governor and driver. What I would have expected this patchset to do is simply move this call under kernel/sched like you have done in your first two patches so as to gain the benefit of using the parameters that the scheduler keeps track of to make better cpuidle state entry decisions.

Quoted 8 lines

But going one step too far by moving the other idle handling functions into it would result in some confusion and add more code than it is meant to handle. This will avoid having to add comments in the cpuidle_idle_call() function as currently being done in Patch[5/5], to clarify what each function is meant to do.

So IMO, Patches[1/5] and [2/5] by themselves are sufficient to increase the proximity between scheduler and cpuidle.

May be patches[1/5] and [2/5] are enough to increase the proximity but the next 3 patches do clearly simplify the code readability.

From my POV, the conditions in the idle loop are much more confusing than making a linear code in cpuidle_idle_call.

What I agree on is the confusion around the cpuidle_idle_call function name which does not reflect what it does after these patches and maybe we should rename 'cpuidle_idle_call' to 'idle()' (or whatever).

Signature · 5 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
#8 lore raw

PATCH 4/5 idle: Move idle conditions in cpuidle_idle main function

In reply to Daniel Lezcano

This patch moves the condition before entering idle into the cpuidle main function located in idle.c. That simplify the idle mainloop functions and increase the readibility of the conditions to enter truly idle.

This patch is code reorganization and does not change the behavior of the function.

---
 kernel/sched/idle.c |   40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)
kernel/sched/idle.c +21 -19
index 8b10891..72b5926 100644
@@ -75,6 +75,23 @@ static int cpuidle_idle_call(void)
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
 
+	/*
+	 * In poll mode we reenable interrupts and spin.
+	 *
+	 * Also if we detected in the wakeup from idle path that the
+	 * tick broadcast device expired for us, we don't want to go
+	 * deep idle as we know that the IPI is going to arrive right
+	 * away
+	 */
+	if (cpu_idle_force_poll || tick_check_broadcast_expired())
+		return cpu_idle_poll();
+
+	if (current_clr_polling_and_test()) {
+		local_irq_enable();
+		__current_set_polling();
+		return 0;
+	}
+
 	stop_critical_timings();
 	rcu_idle_enter();
 
@@ -112,6 +129,8 @@ out:
 	rcu_idle_exit();
 	start_critical_timings();
 
+	__current_set_polling();
+
 	return 0;
 }
 
@@ -133,25 +152,8 @@ static void cpu_idle_loop(void)
 			local_irq_disable();
 			arch_cpu_idle_enter();
 
-			/*
-			 * In poll mode we reenable interrupts and spin.
-			 *
-			 * Also if we detected in the wakeup from idle
-			 * path that the tick broadcast device expired
-			 * for us, we don't want to go deep idle as we
-			 * know that the IPI is going to arrive right
-			 * away
-			 */
-			if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
-				cpu_idle_poll();
-			} else {
-				if (!current_clr_polling_and_test()) {
-					cpuidle_idle_call();
-				} else {
-					local_irq_enable();
-				}
-				__current_set_polling();
-			}
+			cpuidle_idle_call();
+
 			arch_cpu_idle_exit();
 			/*
 			 * We need to test and propagate the TIF_NEED_RESCHED
Signature · 1 line
1.7.9.5
#9 lore raw

PATCH 5/5 idle: Add more comments to the code

In reply to Daniel Lezcano

The idle main function is a complex and a critical function. Added more comments to the code.

---
 kernel/sched/idle.c |   37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
kernel/sched/idle.c +33 -4
index 72b5926..36ff1a7 100644
@@ -86,19 +86,34 @@ static int cpuidle_idle_call(void)
 	if (cpu_idle_force_poll || tick_check_broadcast_expired())
 		return cpu_idle_poll();
 
+	/*
+	 * Check if the idle task must rescheduled. If it is the case,
+	 * exit the function after re-enabling the local irq and set
+	 * again the polling flag
+	 */
 	if (current_clr_polling_and_test()) {
 		local_irq_enable();
 		__current_set_polling();
 		return 0;
 	}
 
+	/*
+	 * During the idle period, stop measuring the disabled irqs
+	 * critical sections latencies
+	 */
 	stop_critical_timings();
+
+	/*
+	 * Tell the RCU framework we are entering an idle section,
+	 * so no more rcu read side critical sections and one more
+	 * step to the grace period
+	 */
 	rcu_idle_enter();
 
-	/* Ask the governor for the next state, this call can fail for
-	 * different reasons: cpuidle is not enabled or an idle state
-	 * fulfilling the constraints was not found. In this case, we fall
-	 * back to the default idle function
+	/*
+	 * Ask the governor to choose an idle state it thinks it is
+	 * convenient to go to. There is *always* a convenient idle
+	 * state but the call could fail if cpuidle is not enabled
 	 */
 	next_state = cpuidle_select(drv, dev);
 	if (next_state < 0) {
@@ -106,6 +121,10 @@ static int cpuidle_idle_call(void)
 		goto out;
 	}
 
+	/*
+	 * The idle task must be scheduled, it is pointless to go to idle,
+	 * just update no idle residency and get out of this function
+	 */
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
@@ -116,6 +135,12 @@ static int cpuidle_idle_call(void)
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
 
+	/*
+	 * Enter the idle state previously returned by the governor
+	 * decision. This function will block until an interrupt
+	 * occurs and will take care of re-enabling the local
+	 * interrupts
+	 */
 	entered_state = cpuidle_enter(drv, dev, next_state);
 
 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
@@ -152,6 +177,10 @@ static void cpu_idle_loop(void)
 			local_irq_disable();
 			arch_cpu_idle_enter();
 
+			/*
+			 * It is up to the underlying functions to
+			 * enable the local interrupts again
+			 */
 			cpuidle_idle_call();
 
 			arch_cpu_idle_exit();
Signature · 1 line
1.7.9.5
Acked-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] idle: Add more comments to the code

Quoted 4 lines · Daniel Lezcano

The idle main function is a complex and a critical function. Added more comments to the code.

Few questions below. In any case,:

Quoted 9 lines
---
 kernel/sched/idle.c |   37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
kernel/sched/idle.c +0 -0
index 72b5926..36ff1a7 100644

kernel/sched/idle.c static int cpuidle_idle_call(void) · line 90

Quoted 5 lines
kernel/sched/idle.c +2 -0
@@ -86,19 +86,34 @@ static int cpuidle_idle_call(void)
 	if (cpu_idle_force_poll || tick_check_broadcast_expired())
 		return cpu_idle_poll();
 
+	/*
+	 * Check if the idle task must rescheduled. If it is the case,

s/must/must be/

Quoted 34 lines
kernel/sched/idle.c +17 -4
+	 * exit the function after re-enabling the local irq and set
+	 * again the polling flag
+	 */
 	if (current_clr_polling_and_test()) {
 		local_irq_enable();
 		__current_set_polling();
 		return 0;
 	}
 
+	/*
+	 * During the idle period, stop measuring the disabled irqs
+	 * critical sections latencies
+	 */
 	stop_critical_timings();
+
+	/*
+	 * Tell the RCU framework we are entering an idle section,
+	 * so no more rcu read side critical sections and one more
+	 * step to the grace period
+	 */
 	rcu_idle_enter();
 
-	/* Ask the governor for the next state, this call can fail for
-	 * different reasons: cpuidle is not enabled or an idle state
-	 * fulfilling the constraints was not found. In this case, we fall
-	 * back to the default idle function
+	/*
+	 * Ask the governor to choose an idle state it thinks it is
+	 * convenient to go to. There is *always* a convenient idle
+	 * state but the call could fail if cpuidle is not enabled
 	 */
 	next_state = cpuidle_select(drv, dev);
 	if (next_state < 0) {

kernel/sched/idle.c static int cpuidle_idle_call(void) · line 130

Quoted 10 lines
kernel/sched/idle.c +4 -0
@@ -106,6 +121,10 @@ static int cpuidle_idle_call(void)
 		goto out;
 	}
 
+	/*
+	 * The idle task must be scheduled, it is pointless to go to idle,
+	 * just update no idle residency and get out of this function
+	 */
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */

Is this if block really necessary? We already have need_resched() being monitored in the outer loop. Are cpuidle_select() or rcu_idle_enter() likely to spend a significant amount of time justifying a recheck here?

Nicolas

#11 lore raw

Re: [PATCH 5/5] idle: Add more comments to the code

Quoted 10 lines · Nicolas Pitre
Quoted 4 lines · Daniel Lezcano

The idle main function is a complex and a critical function. Added more comments to the code.

Few questions below. In any case,:

Thanks for the review Nico !

Answer below.

Quoted 54 lines
Quoted 9 lines
---
  kernel/sched/idle.c |   37 +++++++++++++++++++++++++++++++++----
  1 file changed, 33 insertions(+), 4 deletions(-)
kernel/sched/idle.c +0 -0
index 72b5926..36ff1a7 100644

kernel/sched/idle.c static int cpuidle_idle_call(void) · line 90

Quoted 5 lines
kernel/sched/idle.c +2 -0
@@ -86,19 +86,34 @@ static int cpuidle_idle_call(void)
  	if (cpu_idle_force_poll || tick_check_broadcast_expired())
  		return cpu_idle_poll();
 
+	/*
+	 * Check if the idle task must rescheduled. If it is the case,

s/must/must be/

Quoted 37 lines
kernel/sched/idle.c +17 -4
+	 * exit the function after re-enabling the local irq and set
+	 * again the polling flag
+	 */
  	if (current_clr_polling_and_test()) {
  		local_irq_enable();
  		__current_set_polling();
  		return 0;
  	}
 
+	/*
+	 * During the idle period, stop measuring the disabled irqs
+	 * critical sections latencies
+	 */
  	stop_critical_timings();
+
+	/*
+	 * Tell the RCU framework we are entering an idle section,
+	 * so no more rcu read side critical sections and one more
+	 * step to the grace period
+	 */
  	rcu_idle_enter();
 
-	/* Ask the governor for the next state, this call can fail for
-	 * different reasons: cpuidle is not enabled or an idle state
-	 * fulfilling the constraints was not found. In this case, we fall
-	 * back to the default idle function
+	/*
+	 * Ask the governor to choose an idle state it thinks it is
+	 * convenient to go to. There is *always* a convenient idle
+	 * state but the call could fail if cpuidle is not enabled
  	 */
  	next_state = cpuidle_select(drv, dev);
  	if (next_state < 0) {
@@ -106,6 +121,10 @@ static int cpuidle_idle_call(void)
  		goto out;
  	}
 
Quoted 11 lines

kernel/sched/idle.c static int cpuidle_idle_call(void) · line 130

Quoted 7 lines
kernel/sched/idle.c +4 -0
+	/*
+	 * The idle task must be scheduled, it is pointless to go to idle,
+	 * just update no idle residency and get out of this function
+	 */
  	if (need_resched()) {
  		dev->last_residency = 0;
  		/* give the governor an opportunity to reflect on the outcome */

Is this if block really necessary? We already have need_resched() being monitored in the outer loop. Are cpuidle_select() or rcu_idle_enter() likely to spend a significant amount of time justifying a recheck here?

That's a question I have been always asking myself.

The cpuidle_select function could spend some time for:

1. reflecting the idle time for the statistics of the previous idle period. This processing is post-poned when exiting an idle state via the 'need_update' field in the cpuidle structure. I guess, this is because it can take a while and we want to exit asap to reduce the wakeup latency.

2. there are some processing to choose the idle state.

I don't know what is the rational here to use need_resched at this place except to 'abort' an idle state arbitrarily after some experimentation for better reactivity. I am wondering if the multiple need_resched() we find in the call stack for some idle states makes really sense and doesn't denote a lack of control of what is happening in the idle path vs system activity or a lack of confidence in the idle duration prediction.

Signature · 5 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
Acked-by on v1 1/5
#12 lore raw
In reply to Daniel Lezcano
Quoted 12 lines · Daniel Lezcano

In order to allow better integration between the cpuidle framework and the scheduler, reducing the distance between these two sub-components will facilitate this integration by moving part of the cpuidle code in the idle task file and, because idle.c is in the sched directory, we have access to the scheduler's private structures.

This patch splits the cpuidle_idle_call main entry function into 3 calls to a newly added API:
1. select the idle state
2. enter the idle state
3. reflect the idle state

Quoted 4 lines

The cpuidle_idle_call calls these three functions to implement the main idle entry function.

Quoted 176 lines
---
 drivers/cpuidle/cpuidle.c |  102 ++++++++++++++++++++++++++++++++-------------
 include/linux/cpuidle.h   |   14 +++++++
 2 files changed, 87 insertions(+), 29 deletions(-)
drivers/cpuidle/cpuidle.c +73 -29
index a55e68f..172ab6a 100644
@@ -108,6 +108,71 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 }
 
 /**
+ * cpuidle_select - ask the cpuidle framework to choose an idle state
+ *
+ * @drv: the cpuidle driver
+ * @dev: the cpuidle device
+ *
+ * Returns the index of the idle state. On error it returns:
+ * -NODEV : the cpuidle framework is not available
+ * -EBUSY : the cpuidle framework is not initialized
+ */
+int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
+{
+	if (off || !initialized)
+		return -ENODEV;
+
+	if (!drv || !dev || !dev->enabled)
+		return -EBUSY;
+
+	return cpuidle_curr_governor->select(drv, dev);
+}
+
+/**
+ * cpuidle_enter - enter into the specified idle state
+ *
+ * @drv:   the cpuidle driver tied with the cpu
+ * @dev:   the cpuidle device
+ * @index: the index in the idle state table
+ *
+ * Returns the index in the idle state, < 0 in case of error.
+ * The error code depends on the backend driver
+ */
+int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+		  int index)
+{
+	int entered_state;
+	bool broadcast = !!(drv->states[index].flags & CPUIDLE_FLAG_TIMER_STOP);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
+
+	if (cpuidle_state_is_coupled(dev, drv, index))
+		entered_state = cpuidle_enter_state_coupled(dev, drv, index);
+	else
+		entered_state = cpuidle_enter_state(dev, drv, index);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
+
+	return entered_state;
+}
+
+/**
+ * cpuidle_reflect - tell the underlying governor what was the state
+ * we were in
+ *
+ * @dev  : the cpuidle device
+ * @index: the index in the idle state table
+ *
+ */
+void cpuidle_reflect(struct cpuidle_device *dev, int index)
+{
+	if (cpuidle_curr_governor->reflect)
+		cpuidle_curr_governor->reflect(dev, index);
+}
+
+/**
  * cpuidle_idle_call - the main idle loop
  *
  * NOTE: no locks or semaphores should be used here
@@ -116,51 +181,30 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 int cpuidle_idle_call(void)
 {
 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
-	struct cpuidle_driver *drv;
+	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
-	bool broadcast;
-
-	if (off || !initialized)
-		return -ENODEV;
-
-	/* check if the device is ready */
-	if (!dev || !dev->enabled)
-		return -EBUSY;
-
-	drv = cpuidle_get_cpu_driver(dev);
 
 	/* ask the governor for the next state */
-	next_state = cpuidle_curr_governor->select(drv, dev);
+	next_state = cpuidle_select(drv, dev);
+	if (next_state < 0)
+		return next_state;
+
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
-		if (cpuidle_curr_governor->reflect)
-			cpuidle_curr_governor->reflect(dev, next_state);
+		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
 		return 0;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
 
-	broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
-
-	if (cpuidle_state_is_coupled(dev, drv, next_state))
-		entered_state = cpuidle_enter_state_coupled(dev, drv,
-							    next_state);
-	else
-		entered_state = cpuidle_enter_state(dev, drv, next_state);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
+	entered_state = cpuidle_enter(drv, dev, next_state);
 
 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
 
 	/* give the governor an opportunity to reflect on the outcome */
-	if (cpuidle_curr_governor->reflect)
-		cpuidle_curr_governor->reflect(dev, entered_state);
+	cpuidle_reflect(dev, entered_state);
 
 	return 0;
 }
include/linux/cpuidle.h +14 -0
index 50fcbb0..bf06f37 100644
@@ -119,6 +119,13 @@ struct cpuidle_driver {
 
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
+
+extern int cpuidle_select(struct cpuidle_driver *drv,
+			  struct cpuidle_device *dev);
+extern int cpuidle_enter(struct cpuidle_driver *drv,
+			 struct cpuidle_device *dev, int index);
+extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
+
 extern int cpuidle_idle_call(void);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 extern struct cpuidle_driver *cpuidle_get_driver(void);
@@ -141,6 +148,13 @@ extern int cpuidle_play_dead(void);
 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
 #else
 static inline void disable_cpuidle(void) { }
+static inline int cpuidle_select(struct cpuidle_driver *drv,
+				 struct cpuidle_device *dev)
+{return -ENODEV; }
+static inline int cpuidle_enter(struct cpuidle_driver *drv,
+				struct cpuidle_device *dev, int index)
+{return -ENODEV; }
+static inline void cpuidle_reflect(struct cpuidle_device *dev, int index) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
Signature · 1 line
1.7.9.5
#13 lore raw
In reply to Daniel Lezcano

Hi Daniel,

Quoted 35 lines · Daniel Lezcano

In order to allow better integration between the cpuidle framework and the scheduler, reducing the distance between these two sub-components will facilitate this integration by moving part of the cpuidle code in the idle task file and, because idle.c is in the sched directory, we have access to the scheduler's private structures.

This patch splits the cpuidle_idle_call main entry function into 3 calls to a newly added API:
1. select the idle state
2. enter the idle state
3. reflect the idle state

The cpuidle_idle_call calls these three functions to implement the main idle entry function.

---
 drivers/cpuidle/cpuidle.c |  102 ++++++++++++++++++++++++++++++++-------------
 include/linux/cpuidle.h   |   14 +++++++
 2 files changed, 87 insertions(+), 29 deletions(-)
drivers/cpuidle/cpuidle.c +6 -0
index a55e68f..172ab6a 100644
@@ -108,6 +108,71 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 }
 
 /**
+ * cpuidle_select - ask the cpuidle framework to choose an idle state
+ *
+ * @drv: the cpuidle driver
+ * @dev: the cpuidle device
+ *
+ * Returns the index of the idle state. On error it returns:

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 126

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+ * -NODEV : the cpuidle framework is not available
+ * -EBUSY : the cpuidle framework is not initialized
+ */
+int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
+{
+	if (off || !initialized)
+		return -ENODEV;
+
+	if (!drv || !dev || !dev->enabled)
+		return -EBUSY;

I would suggest moving the above two conditions under another function, cpuidle_enabled() maybe? The reason is, cpuidle_select() indicates that, it is invoked to select an idle state. While you are expecting this function to return an idle state, it seems counter-intuitive to return a ENODEV/EBUSY. This function is expected to be a call into the governor specific code and the same function should not be used to verify if cpuidle is enabled/not IMHO.

Quoted 20 lines
drivers/cpuidle/cpuidle.c +20 -0
+
+	return cpuidle_curr_governor->select(drv, dev);
+}
+
+/**
+ * cpuidle_enter - enter into the specified idle state
+ *
+ * @drv:   the cpuidle driver tied with the cpu
+ * @dev:   the cpuidle device
+ * @index: the index in the idle state table
+ *
+ * Returns the index in the idle state, < 0 in case of error.
+ * The error code depends on the backend driver
+ */
+int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+		  int index)
+{
+	int entered_state;
+	bool broadcast = !!(drv->states[index].flags & CPUIDLE_FLAG_TIMER_STOP);
+

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 156

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
+
+	if (cpuidle_state_is_coupled(dev, drv, index))
+		entered_state = cpuidle_enter_state_coupled(dev, drv, index);
+	else
+		entered_state = cpuidle_enter_state(dev, drv, index);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);

The tip tree, timers/core branch has the patch,
tick: Introduce hrtimer based broadcast. In the problem scenario that this patchset is addressing, the call to broadcast framework may return an error indicating that the idle state in question cannot be entered into. I wanted to bring it to your notice, so that early on you can take care of this. You will need to add code below in the invocation of cpuidle_enter() to verify if the idle state was entered into or not. If it was not, then you will need to skip tracing and reflecting of the idle state and directly exit the cpuidle loop with a failed status.

Quoted 72 lines
drivers/cpuidle/cpuidle.c +26 -27
+
+	return entered_state;
+}
+
+/**
+ * cpuidle_reflect - tell the underlying governor what was the state
+ * we were in
+ *
+ * @dev  : the cpuidle device
+ * @index: the index in the idle state table
+ *
+ */
+void cpuidle_reflect(struct cpuidle_device *dev, int index)
+{
+	if (cpuidle_curr_governor->reflect)
+		cpuidle_curr_governor->reflect(dev, index);
+}
+
+/**
  * cpuidle_idle_call - the main idle loop
  *
  * NOTE: no locks or semaphores should be used here
@@ -116,51 +181,30 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 int cpuidle_idle_call(void)
 {
 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
-	struct cpuidle_driver *drv;
+	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
 	int next_state, entered_state;
-	bool broadcast;
-
-	if (off || !initialized)
-		return -ENODEV;
-
-	/* check if the device is ready */
-	if (!dev || !dev->enabled)
-		return -EBUSY;
-
-	drv = cpuidle_get_cpu_driver(dev);
 
 	/* ask the governor for the next state */
-	next_state = cpuidle_curr_governor->select(drv, dev);
+	next_state = cpuidle_select(drv, dev);
+	if (next_state < 0)
+		return next_state;
+
 	if (need_resched()) {
 		dev->last_residency = 0;
 		/* give the governor an opportunity to reflect on the outcome */
-		if (cpuidle_curr_governor->reflect)
-			cpuidle_curr_governor->reflect(dev, next_state);
+		cpuidle_reflect(dev, next_state);
 		local_irq_enable();
 		return 0;
 	}
 
 	trace_cpu_idle_rcuidle(next_state, dev->cpu);
 
-	broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
-
-	if (cpuidle_state_is_coupled(dev, drv, next_state))
-		entered_state = cpuidle_enter_state_coupled(dev, drv,
-							    next_state);
-	else
-		entered_state = cpuidle_enter_state(dev, drv, next_state);
-
-	if (broadcast)
-		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
+	entered_state = cpuidle_enter(drv, dev, next_state);

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 210

Quoted 10 lines
drivers/cpuidle/cpuidle.c +1 -2
 
 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
 
 	/* give the governor an opportunity to reflect on the outcome */
-	if (cpuidle_curr_governor->reflect)
-		cpuidle_curr_governor->reflect(dev, entered_state);
+	cpuidle_reflect(dev, entered_state);
 
 	return 0;
 }

Thanks

Regards
Preeti U Murthy

#14 lore raw
Quoted 49 lines · Preeti U Murthy

Hi Daniel,

Quoted 35 lines · Daniel Lezcano

In order to allow better integration between the cpuidle framework and the scheduler, reducing the distance between these two sub-components will facilitate this integration by moving part of the cpuidle code in the idle task file and, because idle.c is in the sched directory, we have access to the scheduler's private structures.

This patch splits the cpuidle_idle_call main entry function into 3 calls to a newly added API:
1. select the idle state
2. enter the idle state
3. reflect the idle state

The cpuidle_idle_call calls these three functions to implement the main idle entry function.

---
  drivers/cpuidle/cpuidle.c |  102 ++++++++++++++++++++++++++++++++-------------
  include/linux/cpuidle.h   |   14 +++++++
  2 files changed, 87 insertions(+), 29 deletions(-)
drivers/cpuidle/cpuidle.c +6 -0
index a55e68f..172ab6a 100644
@@ -108,6 +108,71 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
  }
 
  /**
+ * cpuidle_select - ask the cpuidle framework to choose an idle state
+ *
+ * @drv: the cpuidle driver
+ * @dev: the cpuidle device
+ *
+ * Returns the index of the idle state. On error it returns:

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 126

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+ * -NODEV : the cpuidle framework is not available
+ * -EBUSY : the cpuidle framework is not initialized
+ */
+int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
+{
+	if (off || !initialized)
+		return -ENODEV;
+
+	if (!drv || !dev || !dev->enabled)
+		return -EBUSY;
Quoted 7 lines

I would suggest moving the above two conditions under another function, cpuidle_enabled() maybe? The reason is, cpuidle_select() indicates that, it is invoked to select an idle state. While you are expecting this function to return an idle state, it seems counter-intuitive to return a ENODEV/EBUSY. This function is expected to be a call into the governor specific code and the same function should not be used to verify if cpuidle is enabled/not IMHO.

Yes, I fully agree. I will fix that.

Quoted 32 lines
Quoted 20 lines
drivers/cpuidle/cpuidle.c +20 -0
+
+	return cpuidle_curr_governor->select(drv, dev);
+}
+
+/**
+ * cpuidle_enter - enter into the specified idle state
+ *
+ * @drv:   the cpuidle driver tied with the cpu
+ * @dev:   the cpuidle device
+ * @index: the index in the idle state table
+ *
+ * Returns the index in the idle state, < 0 in case of error.
+ * The error code depends on the backend driver
+ */
+int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
+		  int index)
+{
+	int entered_state;
+	bool broadcast = !!(drv->states[index].flags & CPUIDLE_FLAG_TIMER_STOP);
+

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 156

Quoted 10 lines
drivers/cpuidle/cpuidle.c +10 -0
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
+
+	if (cpuidle_state_is_coupled(dev, drv, index))
+		entered_state = cpuidle_enter_state_coupled(dev, drv, index);
+	else
+		entered_state = cpuidle_enter_state(dev, drv, index);
+
+	if (broadcast)
+		clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);

The tip tree, timers/core branch has the patch,

Quoted 8 lines

tick: Introduce hrtimer based broadcast. In the problem scenario that this patchset is addressing, the call to broadcast framework may return an error indicating that the idle state in question cannot be entered into. I wanted to bring it to your notice, so that early on you can take care of this. You will need to add code below in the invocation of cpuidle_enter() to verify if the idle state was entered into or not. If it was not, then you will need to skip tracing and reflecting of the idle state and directly exit the cpuidle loop with a failed status.

Ok.

Thanks !

Signature · 5 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