Skip to contentSkip to conversation

cpuidle: Replace use_deepest_state flag by use_latency

RFC PATCH In review Daniel Lezcano 2 participants 6 messages Nov 13, 2019

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

RFC PATCH 1/3 cpuidle: Replace use_deepest_state flag by use_latency

We want to specify a latency constraint when choosing an idle state at play_idle time. Instead of duplicating the information in the structure or propagate the latency in the call stack, change the use_deepest_state by use_latency to introduce this constraint.

A zero latency constraint means "do not use the deepest idle state path" as the 'use_deepest_state' boolean was used in the cpuidle_idle_call.

---
 drivers/cpuidle/cpuidle.c | 6 +++---
 include/linux/cpuidle.h   | 6 +++---
 kernel/sched/idle.c       | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +3 -3
index 44ae39f2b47a..f68a6c9e8482 100644
@@ -100,19 +100,19 @@ static int find_deepest_state(struct cpuidle_driver *drv,
 
 /**
  * cpuidle_use_deepest_state - Set/clear governor override flag.
- * @enable: New value of the flag.
+ * @latency: A latency constraint
  *
  * Set/unset the current CPU to use the deepest idle state (override governors
  * going forward if set).
  */
-void cpuidle_use_deepest_state(bool enable)
+void cpuidle_use_latency(unsigned int latency)
 {
 	struct cpuidle_device *dev;
 
 	preempt_disable();
 	dev = cpuidle_get_device();
 	if (dev)
-		dev->use_deepest_state = enable;
+		dev->use_latency = latency;
 	preempt_enable();
 }
 
include/linux/cpuidle.h +3 -3
index d23a3b1ddcf6..32018704f4ea 100644
@@ -83,8 +83,8 @@ struct cpuidle_driver_kobj;
 struct cpuidle_device {
 	unsigned int		registered:1;
 	unsigned int		enabled:1;
-	unsigned int		use_deepest_state:1;
 	unsigned int		poll_time_limit:1;
+	unsigned int		use_latency;
 	unsigned int		cpu;
 	ktime_t			next_hrtimer;
 
@@ -210,7 +210,7 @@ extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 				      struct cpuidle_device *dev);
 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
 				struct cpuidle_device *dev);
-extern void cpuidle_use_deepest_state(bool enable);
+extern void cpuidle_use_latency(unsigned int latency);
 #else
 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 					     struct cpuidle_device *dev)
@@ -218,7 +218,7 @@ static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
 				       struct cpuidle_device *dev)
 {return -ENODEV; }
-static inline void cpuidle_use_deepest_state(bool enable)
+static inline void cpuidle_use_latency(unsigned int latency)
 {
 }
 #endif
kernel/sched/idle.c +3 -3
index 8dad5aa600ea..00e064d3dfe1 100644
@@ -165,7 +165,7 @@ static void cpuidle_idle_call(void)
 	 * until a proper wakeup interrupt happens.
 	 */
 
-	if (idle_should_enter_s2idle() || dev->use_deepest_state) {
+	if (idle_should_enter_s2idle() || dev->use_latency) {
 		if (idle_should_enter_s2idle()) {
 			rcu_idle_enter();
 
@@ -328,7 +328,7 @@ void play_idle(unsigned long duration_us)
 	rcu_sleep_check();
 	preempt_disable();
 	current->flags |= PF_IDLE;
-	cpuidle_use_deepest_state(true);
+	cpuidle_use_latency(1);
 
 	it.done = 0;
 	hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
@@ -339,7 +339,7 @@ void play_idle(unsigned long duration_us)
 	while (!READ_ONCE(it.done))
 		do_idle();
 
-	cpuidle_use_deepest_state(false);
+	cpuidle_use_latency(0);
 	current->flags &= ~PF_IDLE;
 
 	preempt_fold_need_resched();
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 2/3 sched: idle: Add a latency parameter to the play_idle function

By default the play_idle() function leads to the deepest idle state selection which is not necessarily the state we are interested in when forcing the CPU to go to idle.

Add a latency parameter to the play_idle() function, so the caller can use the constraint to allow a shallower state.

---
 drivers/powercap/idle_inject.c           | 2 +-
 drivers/thermal/intel/intel_powerclamp.c | 2 +-
 include/linux/cpu.h                      | 2 +-
 kernel/sched/idle.c                      | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/powercap/idle_inject.c +1 -1
index cd1270614cc6..6f2bfb172e61 100644
@@ -138,7 +138,7 @@ static void idle_inject_fn(unsigned int cpu)
 	 */
 	iit->should_run = 0;
 
-	play_idle(READ_ONCE(ii_dev->idle_duration_us));
+	play_idle(READ_ONCE(ii_dev->idle_duration_us), UINT_MAX);
 }
 
 /**
drivers/thermal/intel/intel_powerclamp.c +1 -1
index 53216dcbe173..dd1330d59176 100644
@@ -430,7 +430,7 @@ static void clamp_idle_injection_func(struct kthread_work *work)
 	if (should_skip)
 		goto balance;
 
-	play_idle(jiffies_to_usecs(w_data->duration_jiffies));
+	play_idle(jiffies_to_usecs(w_data->duration_jiffies), UINT_MAX);
 
 balance:
 	if (clamping && w_data->clamping && cpu_online(w_data->cpu))
include/linux/cpu.h +1 -1
index d0633ebdaa9c..241f558af17a 100644
@@ -179,7 +179,7 @@ void arch_cpu_idle_dead(void);
 int cpu_report_state(int cpu);
 int cpu_check_up_prepare(int cpu);
 void cpu_set_state_online(int cpu);
-void play_idle(unsigned long duration_us);
+void play_idle(unsigned long duration_us, unsigned int latency);
 
 #ifdef CONFIG_HOTPLUG_CPU
 bool cpu_wait_death(unsigned int cpu, int seconds);
kernel/sched/idle.c +2 -2
index 00e064d3dfe1..56a8b9d35cb9 100644
@@ -311,7 +311,7 @@ static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
 	return HRTIMER_NORESTART;
 }
 
-void play_idle(unsigned long duration_us)
+void play_idle(unsigned long duration_us, unsigned int latency)
 {
 	struct idle_timer it;
 
@@ -328,7 +328,7 @@ void play_idle(unsigned long duration_us)
 	rcu_sleep_check();
 	preempt_disable();
 	current->flags |= PF_IDLE;
-	cpuidle_use_latency(1);
+	cpuidle_use_latency(latency);
 
 	it.done = 0;
 	hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH RFC 2/3] sched: idle: Add a latency parameter to the play_idle function

Quoted 60 lines · Daniel Lezcano

By default the play_idle() function leads to the deepest idle state selection which is not necessarily the state we are interested in when forcing the CPU to go to idle.

Add a latency parameter to the play_idle() function, so the caller can use the constraint to allow a shallower state.

---
 drivers/powercap/idle_inject.c           | 2 +-
 drivers/thermal/intel/intel_powerclamp.c | 2 +-
 include/linux/cpu.h                      | 2 +-
 kernel/sched/idle.c                      | 4 ++--
 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/powercap/idle_inject.c +1 -1
index cd1270614cc6..6f2bfb172e61 100644
@@ -138,7 +138,7 @@ static void idle_inject_fn(unsigned int cpu)
         */
        iit->should_run = 0;
 
-       play_idle(READ_ONCE(ii_dev->idle_duration_us));
+       play_idle(READ_ONCE(ii_dev->idle_duration_us), UINT_MAX);
 }
 
 /**
drivers/thermal/intel/intel_powerclamp.c +1 -1
index 53216dcbe173..dd1330d59176 100644
@@ -430,7 +430,7 @@ static void clamp_idle_injection_func(struct kthread_work *work)
        if (should_skip)
                goto balance;
 
-       play_idle(jiffies_to_usecs(w_data->duration_jiffies));
+       play_idle(jiffies_to_usecs(w_data->duration_jiffies), UINT_MAX);
 
 balance:
        if (clamping && w_data->clamping && cpu_online(w_data->cpu))
include/linux/cpu.h +1 -1
index d0633ebdaa9c..241f558af17a 100644
@@ -179,7 +179,7 @@ void arch_cpu_idle_dead(void);
 int cpu_report_state(int cpu);
 int cpu_check_up_prepare(int cpu);
 void cpu_set_state_online(int cpu);
-void play_idle(unsigned long duration_us);
+void play_idle(unsigned long duration_us, unsigned int latency);
 
 #ifdef CONFIG_HOTPLUG_CPU
 bool cpu_wait_death(unsigned int cpu, int seconds);
kernel/sched/idle.c +0 -0
index 00e064d3dfe1..56a8b9d35cb9 100644

kernel/sched/idle.c static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer) · line 314

Quoted 5 lines
kernel/sched/idle.c +1 -1
@@ -311,7 +311,7 @@ static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)
        return HRTIMER_NORESTART;
 }
 
-void play_idle(unsigned long duration_us)
+void play_idle(unsigned long duration_us, unsigned int latency)

I'd rather rename this to something like play_idle_precise() and redefine play_idle() as a single-argument wrapper around it.

Maybe also change the first arg to ns while at it (and keep us in play_idle())?

Quoted 15 lines
kernel/sched/idle.c +1 -2
 {
        struct idle_timer it;
 
@@ -328,7 +328,7 @@ void play_idle(unsigned long duration_us)
        rcu_sleep_check();
        preempt_disable();
        current->flags |= PF_IDLE;
-       cpuidle_use_latency(1);
+       cpuidle_use_latency(latency);
 
        it.done = 0;
        hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
--

2.17.1

#3 lore raw

RFC PATCH 3/3 cpuidle: Use the latency to call find_deepest_idle_state

In reply to Daniel Lezcano

As the dev->use_latency is filled with the latency value when this function is called, use it as a parameter to the find_deepest_idle_state() function.

---
 drivers/cpuidle/cpuidle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +1 -1
index f68a6c9e8482..659d8b1ece6d 100644
@@ -124,7 +124,7 @@ void cpuidle_use_latency(unsigned int latency)
 int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 			       struct cpuidle_device *dev)
 {
-	return find_deepest_state(drv, dev, UINT_MAX, 0, false);
+	return find_deepest_state(drv, dev, dev->use_latency, 0, false);
 }
 
 #ifdef CONFIG_SUSPEND
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH RFC 3/3] cpuidle: Use the latency to call find_deepest_idle_state

Quoted 15 lines · Daniel Lezcano

As the dev->use_latency is filled with the latency value when this function is called, use it as a parameter to the find_deepest_idle_state() function.

---
 drivers/cpuidle/cpuidle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +0 -0
index f68a6c9e8482..659d8b1ece6d 100644

drivers/cpuidle/cpuidle.c void cpuidle_use_latency(unsigned int latency) · line 127

Quoted 5 lines
drivers/cpuidle/cpuidle.c +1 -1
@@ -124,7 +124,7 @@ void cpuidle_use_latency(unsigned int latency)
 int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
                               struct cpuidle_device *dev)
 {
-       return find_deepest_state(drv, dev, UINT_MAX, 0, false);
+       return find_deepest_state(drv, dev, dev->use_latency, 0, false);

This breaks the suspend-to-idle case when ->enter_s2idle is NULL for all of the enabled states, because the latency limit for forced idle should not be applied then.

Maybe pass the latency limit to cpuidle_find_deepest_state() and rearrange the code in cpuidle_idle_call() to use U64_MAX in the s2idle case.

BTW, note that find_deepest_state() will take a u64 latency limit in ns now (and going forward).

Quoted 4 lines
drivers/cpuidle/cpuidle.c +0 -1
 }
 
 #ifdef CONFIG_SUSPEND
--
#5 lore raw
In reply to Daniel Lezcano
Quoted 22 lines · Daniel Lezcano

We want to specify a latency constraint when choosing an idle state at play_idle time. Instead of duplicating the information in the structure or propagate the latency in the call stack, change the use_deepest_state by use_latency to introduce this constraint.

A zero latency constraint means "do not use the deepest idle state path" as the 'use_deepest_state' boolean was used in the cpuidle_idle_call.

---
 drivers/cpuidle/cpuidle.c | 6 +++---
 include/linux/cpuidle.h   | 6 +++---
 kernel/sched/idle.c       | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +0 -0
index 44ae39f2b47a..f68a6c9e8482 100644

drivers/cpuidle/cpuidle.c static int find_deepest_state(struct cpuidle_driver *drv, · line 103

Quoted 5 lines
drivers/cpuidle/cpuidle.c +1 -1
@@ -100,19 +100,19 @@ static int find_deepest_state(struct cpuidle_driver *drv,
 
 /**
  * cpuidle_use_deepest_state - Set/clear governor override flag.
- * @enable: New value of the flag.
+ * @latency: A latency constraint

I would call this latency_limit.

Maybe even latency_limit_ns (or us, whatever is more suitable), to make it clear which unit of time is used here.

drivers/cpuidle/cpuidle.c static int find_deepest_state(struct cpuidle_driver *drv, · line 106

Quoted 3 lines
drivers/cpuidle/cpuidle.c +0 -0
  *
  * Set/unset the current CPU to use the deepest idle state (override governors
  * going forward if set).

I would update the comment too, something like:

"Set/unset the current CPU to use the deepest idle state with the exit latency within @latency_limit"

drivers/cpuidle/cpuidle.c static int find_deepest_state(struct cpuidle_driver *drv, · line 108

Quoted 3 lines
drivers/cpuidle/cpuidle.c +1 -1
  */
-void cpuidle_use_deepest_state(bool enable)
+void cpuidle_use_latency(unsigned int latency)

I wouldn't change the name of the function (because why really?).

Quoted 16 lines
drivers/cpuidle/cpuidle.c +1 -1
 {
        struct cpuidle_device *dev;
 
        preempt_disable();
        dev = cpuidle_get_device();
        if (dev)
-               dev->use_deepest_state = enable;
+               dev->use_latency = latency;
        preempt_enable();
 }
 
include/linux/cpuidle.h +0 -0
index d23a3b1ddcf6..32018704f4ea 100644

include/linux/cpuidle.h struct cpuidle_driver_kobj; · line 87

Quoted 6 lines
include/linux/cpuidle.h +1 -1
@@ -83,8 +83,8 @@ struct cpuidle_driver_kobj;
 struct cpuidle_device {
        unsigned int            registered:1;
        unsigned int            enabled:1;
-       unsigned int            use_deepest_state:1;
        unsigned int            poll_time_limit:1;
+       unsigned int            use_latency;

And maybe call this forced_idle_latency_limit or similar?

The idea being that when it is set, idle is forced (i.e. no governors), but there is a latency limit for the state to use.

Quoted 53 lines
include/linux/cpuidle.h +2 -2
        unsigned int            cpu;
        ktime_t                 next_hrtimer;
 
@@ -210,7 +210,7 @@ extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
                                      struct cpuidle_device *dev);
 extern int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
                                struct cpuidle_device *dev);
-extern void cpuidle_use_deepest_state(bool enable);
+extern void cpuidle_use_latency(unsigned int latency);
 #else
 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
                                             struct cpuidle_device *dev)
@@ -218,7 +218,7 @@ static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 static inline int cpuidle_enter_s2idle(struct cpuidle_driver *drv,
                                       struct cpuidle_device *dev)
 {return -ENODEV; }
-static inline void cpuidle_use_deepest_state(bool enable)
+static inline void cpuidle_use_latency(unsigned int latency)
 {
 }
 #endif
kernel/sched/idle.c +3 -4
index 8dad5aa600ea..00e064d3dfe1 100644
@@ -165,7 +165,7 @@ static void cpuidle_idle_call(void)
         * until a proper wakeup interrupt happens.
         */
 
-       if (idle_should_enter_s2idle() || dev->use_deepest_state) {
+       if (idle_should_enter_s2idle() || dev->use_latency) {
                if (idle_should_enter_s2idle()) {
                        rcu_idle_enter();
 
@@ -328,7 +328,7 @@ void play_idle(unsigned long duration_us)
        rcu_sleep_check();
        preempt_disable();
        current->flags |= PF_IDLE;
-       cpuidle_use_deepest_state(true);
+       cpuidle_use_latency(1);
 
        it.done = 0;
        hrtimer_init_on_stack(&it.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
@@ -339,7 +339,7 @@ void play_idle(unsigned long duration_us)
        while (!READ_ONCE(it.done))
                do_idle();
 
-       cpuidle_use_deepest_state(false);
+       cpuidle_use_latency(0);
        current->flags &= ~PF_IDLE;
 
        preempt_fold_need_resched();
--