Skip to contentSkip to conversation

Stop sched tick in idle injection task

PATCH In review Jacob Pan 2 participants 4 messages Nov 29, 2016 – Dec 16, 2016

Subsystems (5)
lore
Share & tools
lore

Jacob Pan

author
#0 lore raw

v5 cover Stop sched tick in idle injection task

Changelog:
v5: - Fix compile error in cpuidle patch reported by 0-day.
- Reverse patch order for correct dependency.

v4: - Misc comments from Ingo are addressed, including style fix,
timeout handling, fork().
- Dropped powerclamp patch from this set, move to its own series.

v3: - Rearrange idle.c change based on Rafael's suggestion.

v2:
- Moved duration timer from powerclamp driver to play_idle()
- Unexport cpuidle_use_deepest_state
- Indentation fix

Idle injection drivers today use RT threads to run idle loop. There are efficiency and accounting issues with the current intel_powerclamp.c and acpi_pad.c. A while ago, I posted CFS based idle injection patch trying to address them:
https://lkml.org/lkml/2015/11/13/576

Peter proposed another approach with the introduction of a PF_IDLE flag. This patchset is based on his original posting:
https://lkml.org/lkml/2014/6/4/56

These patches apply on top of the kworker and cpu hotplug state machine changes made to Intel powerclamp driver.
https://lkml.org/lkml/2016/10/17/362

Similar changes to ACPI PAD driver is developed along with other enhancements. It will be posted after this patchset is accepted.

Jacob Pan (1):
cpuidle: allow setting deepest idle

Peter Zijlstra (1):
idle: add support for tasks that inject idle

 drivers/cpuidle/cpuidle.c |  13 +++-
 include/linux/cpu.h       |   2 +
 include/linux/cpuidle.h   |   7 +-
 include/linux/sched.h     |   3 +-
 kernel/fork.c             |   2 +-
 kernel/sched/core.c       |   1 +
 kernel/sched/idle.c       | 175 +++++++++++++++++++++++++++++-----------------
 7 files changed, 133 insertions(+), 70 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v5 1/2 cpuidle: allow setting deepest idle

When idle injection is used to cap power, we need to override governor's choice of idle states. This patch allows caller to select the deepest idle state on a CPU therefore achieve the maximum potential power saving.

---
 drivers/cpuidle/cpuidle.c | 13 ++++++++++++-
 include/linux/cpuidle.h   |  7 ++++++-
 kernel/sched/idle.c       | 13 ++++++++-----
 3 files changed, 26 insertions(+), 7 deletions(-)
drivers/cpuidle/cpuidle.c +12 -1
index c73207a..afc005b 100644
@@ -97,7 +97,17 @@ static int find_deepest_state(struct cpuidle_driver *drv,
 	return ret;
 }
 
-#ifdef CONFIG_SUSPEND
+/* Set the current cpu to use the deepest idle state, override governors */
+void cpuidle_use_deepest_state(bool enable)
+{
+	struct cpuidle_device *dev;
+
+	preempt_disable();
+	dev = cpuidle_get_device();
+	dev->use_deepest_state = enable;
+	preempt_enable();
+}
+
 /**
  * cpuidle_find_deepest_state - Find the deepest available idle state.
  * @drv: cpuidle driver for the given CPU.
@@ -109,6 +119,7 @@ int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 	return find_deepest_state(drv, dev, UINT_MAX, 0, false);
 }
 
+#ifdef CONFIG_SUSPEND
 static void enter_freeze_proper(struct cpuidle_driver *drv,
 				struct cpuidle_device *dev, int index)
 {
include/linux/cpuidle.h +6 -1
index bb31373..63bf385 100644
@@ -74,6 +74,7 @@ struct cpuidle_state {
 struct cpuidle_device {
 	unsigned int		registered:1;
 	unsigned int		enabled:1;
+	unsigned int		use_deepest_state:1;
 	unsigned int		cpu;
 
 	int			last_residency;
@@ -192,11 +193,12 @@ static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
 #endif
 
-#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_SUSPEND)
+#ifdef CONFIG_CPU_IDLE
 extern int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 				      struct cpuidle_device *dev);
 extern int cpuidle_enter_freeze(struct cpuidle_driver *drv,
 				struct cpuidle_device *dev);
+extern void cpuidle_use_deepest_state(bool enable);
 #else
 static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 					     struct cpuidle_device *dev)
@@ -204,6 +206,9 @@ static inline int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
 static inline int cpuidle_enter_freeze(struct cpuidle_driver *drv,
 				       struct cpuidle_device *dev)
 {return -ENODEV; }
+static inline void cpuidle_use_deepest_state(bool enable)
+{
+}
 #endif
 
 /* kernel/sched/idle.c */
kernel/sched/idle.c +8 -5
index 1d8718d..513e4df 100644
@@ -164,11 +164,14 @@ static void cpuidle_idle_call(void)
 	 * timekeeping to prevent timer interrupts from kicking us out of idle
 	 * until a proper wakeup interrupt happens.
 	 */
-	if (idle_should_freeze()) {
-		entered_state = cpuidle_enter_freeze(drv, dev);
-		if (entered_state > 0) {
-			local_irq_enable();
-			goto exit_idle;
+
+	if (idle_should_freeze() || dev->use_deepest_state) {
+		if (idle_should_freeze()) {
+			entered_state = cpuidle_enter_freeze(drv, dev);
+			if (entered_state > 0) {
+				local_irq_enable();
+				goto exit_idle;
+			}
 		}
 
 		next_state = cpuidle_find_deepest_state(drv, dev);
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v5 2/2 idle: add support for tasks that inject idle

In reply to Jacob Pan

From: Peter Zijlstra <[email protected]>

Idle injection drivers such as Intel powerclamp and ACPI PAD drivers use realtime tasks to take control of CPU then inject idle. There are two issues with this approach:

1. Low efficiency: injected idle task is treated as busy so sched ticks do not stop during injected idle period, the result of these unwanted wakeups can be ~20% loss in power savings.

2. Idle accounting: injected idle time is presented to user as busy.

This patch addresses the issues by introducing a new PF_IDLE flag which allows any given task to be treated as idle task while the flag is set. Therefore, idle injection tasks can run through the normal flow of NOHZ idle enter/exit to get the correct accounting as well as tick stop when possible.

The implication is that idle task is then no longer limited to PID == 0.

---
 include/linux/cpu.h   |   2 +
 include/linux/sched.h |   3 +-
 kernel/fork.c         |   2 +-
 kernel/sched/core.c   |   1 +
 kernel/sched/idle.c   | 162 +++++++++++++++++++++++++++++++-------------------
 5 files changed, 107 insertions(+), 63 deletions(-)
include/linux/cpu.h +2 -0
index b886dc1..ac0efae 100644
@@ -245,6 +245,8 @@ static inline void enable_nonboot_cpus(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_ms);
+
 #ifdef CONFIG_HOTPLUG_CPU
 bool cpu_wait_death(unsigned int cpu, int seconds);
 bool cpu_report_death(void);
include/linux/sched.h +2 -1
index e9c009d..a3d338e 100644
@@ -2254,6 +2254,7 @@ static inline cputime_t task_gtime(struct task_struct *t)
 /*
  * Per process flags
  */
+#define PF_IDLE		0x00000002	/* I am an IDLE thread */
 #define PF_EXITING	0x00000004	/* getting shut down */
 #define PF_EXITPIDONE	0x00000008	/* pi exit done on shut down */
 #define PF_VCPU		0x00000010	/* I'm a virtual CPU */
@@ -2611,7 +2612,7 @@ extern int sched_setattr(struct task_struct *,
  */
 static inline bool is_idle_task(const struct task_struct *p)
 {
-	return p->pid == 0;
+	return !!(p->flags & PF_IDLE);
 }
 extern struct task_struct *curr_task(int cpu);
 extern void ia64_set_curr_task(int cpu, struct task_struct *p);
kernel/fork.c +1 -1
index 997ac1d..a8eb821 100644
@@ -1540,7 +1540,7 @@ static __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_count;
 
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
-	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
+	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE);
 	p->flags |= PF_FORKNOEXEC;
 	INIT_LIST_HEAD(&p->children);
 	INIT_LIST_HEAD(&p->sibling);
kernel/sched/core.c +1 -0
index 154fd68..c95fbcd 100644
@@ -5279,6 +5279,7 @@ void init_idle(struct task_struct *idle, int cpu)
 	__sched_fork(0, idle);
 	idle->state = TASK_RUNNING;
 	idle->se.exec_start = sched_clock();
+	idle->flags |= PF_IDLE;
 
 	kasan_unpoison_task_stack(idle);
 
kernel/sched/idle.c +101 -61

188 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#3 lore raw
In reply to Jacob Pan
Quoted 5 lines · Jacob Pan

Jacob Pan (1):
cpuidle: allow setting deepest idle

Peter Zijlstra (1):
idle: add support for tasks that inject idle

Rafael, OK if I carry that first patch so that the combined stuff actually compiles and such?

Jacob, I assume you'll follow up with patches converting those drivers?