diff mbox series

[RFC,1/3] cpuidle: Replace use_deepest_state flag by use_latency

Message ID 20191113185419.13305-1-daniel.lezcano@linaro.org
State New
Headers show
Series [RFC,1/3] cpuidle: Replace use_deepest_state flag by use_latency | expand

Commit Message

Daniel Lezcano Nov. 13, 2019, 6:54 p.m. UTC
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.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

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

-- 
2.17.1

Comments

Rafael J. Wysocki Nov. 13, 2019, 9:59 p.m. UTC | #1
On Wed, Nov 13, 2019 at 7:54 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>

> 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.

>

> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

> ---

>  drivers/cpuidle/cpuidle.c | 6 +++---

>  include/linux/cpuidle.h   | 6 +++---

>  kernel/sched/idle.c       | 6 +++---

>  3 files changed, 9 insertions(+), 9 deletions(-)

>

> diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c

> index 44ae39f2b47a..f68a6c9e8482 100644

> --- a/drivers/cpuidle/cpuidle.c

> +++ b/drivers/cpuidle/cpuidle.c

> @@ -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.

>   *

>   * 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"

>   */

> -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?).

>  {

>         struct cpuidle_device *dev;

>

>         preempt_disable();

>         dev = cpuidle_get_device();

>         if (dev)

> -               dev->use_deepest_state = enable;

> +               dev->use_latency = latency;

>         preempt_enable();

>  }

>

> diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h

> index d23a3b1ddcf6..32018704f4ea 100644

> --- a/include/linux/cpuidle.h

> +++ b/include/linux/cpuidle.h

> @@ -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.

>         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

> diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c

> index 8dad5aa600ea..00e064d3dfe1 100644

> --- a/kernel/sched/idle.c

> +++ b/kernel/sched/idle.c

> @@ -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();

> --
Rafael J. Wysocki Nov. 13, 2019, 10:08 p.m. UTC | #2
On Wed, Nov 13, 2019 at 7:54 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>

> 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.

>

> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

> ---

>  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(-)

>

> diff --git a/drivers/powercap/idle_inject.c b/drivers/powercap/idle_inject.c

> index cd1270614cc6..6f2bfb172e61 100644

> --- a/drivers/powercap/idle_inject.c

> +++ b/drivers/powercap/idle_inject.c

> @@ -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);

>  }

>

>  /**

> diff --git a/drivers/thermal/intel/intel_powerclamp.c b/drivers/thermal/intel/intel_powerclamp.c

> index 53216dcbe173..dd1330d59176 100644

> --- a/drivers/thermal/intel/intel_powerclamp.c

> +++ b/drivers/thermal/intel/intel_powerclamp.c

> @@ -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))

> diff --git a/include/linux/cpu.h b/include/linux/cpu.h

> index d0633ebdaa9c..241f558af17a 100644

> --- a/include/linux/cpu.h

> +++ b/include/linux/cpu.h

> @@ -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);

> diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c

> index 00e064d3dfe1..56a8b9d35cb9 100644

> --- a/kernel/sched/idle.c

> +++ b/kernel/sched/idle.c

> @@ -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())?

>  {

>         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

>
Rafael J. Wysocki Nov. 13, 2019, 10:19 p.m. UTC | #3
On Wed, Nov 13, 2019 at 7:54 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>

> 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.

>

> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

> ---

>  drivers/cpuidle/cpuidle.c | 2 +-

>  1 file changed, 1 insertion(+), 1 deletion(-)

>

> diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c

> index f68a6c9e8482..659d8b1ece6d 100644

> --- a/drivers/cpuidle/cpuidle.c

> +++ b/drivers/cpuidle/cpuidle.c

> @@ -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).

>  }

>

>  #ifdef CONFIG_SUSPEND

> --
diff mbox series

Patch

diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c
index 44ae39f2b47a..f68a6c9e8482 100644
--- a/drivers/cpuidle/cpuidle.c
+++ b/drivers/cpuidle/cpuidle.c
@@ -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();
 }
 
diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h
index d23a3b1ddcf6..32018704f4ea 100644
--- a/include/linux/cpuidle.h
+++ b/include/linux/cpuidle.h
@@ -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
diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index 8dad5aa600ea..00e064d3dfe1 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -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();