Skip to contentSkip to conversation

cpuidle: Handle TIF_NR_POLLING on behalf of polling idle states v2

PATCH Superseded Frederic Weisbecker 3 participants 10 messages Dec 6, 2024 – Dec 18, 2024

Subsystems (6)
x86 PowerPC ACPI Scheduler +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover cpuidle: Handle TIF_NR_POLLING on behalf of polling idle states v2

The TIF_NR_POLLING handling against TIF_NEED_RESCHED polling/monitoring idle states (mwait and also software polling) is a bit messy, with quite some wasted cycles spent on useless atomic operations. This tries to consolidate this state handling from the cpuidle core.

Changes since v1:

* Drop two x86 applied patches
* s/CPUIDLE_FLAG_POLLING_HARD/CPUIDLE_FLAG_MWAIT (Rafael)
* Fix confusion between polling and monitoring on comments (Rafael)
* Remove call_cpuidle_s2idle() (Rafael)

Frederic Weisbecker (3):
cpuidle: Remove unnecessary current_clr_polling_and_test() from
haltpoll
cpuidle: Remove call_cpuidle_s2idle()
cpuidle: Handle TIF_NR_POLLING on behalf of software polling idle
states

Peter Zijlstra (2):
cpuidle: Introduce CPUIDLE_FLAG_MWAIT
cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states

 arch/x86/include/asm/mwait.h       |  3 +-
 drivers/acpi/processor_idle.c      |  3 ++
 drivers/cpuidle/cpuidle-haltpoll.c |  3 --
 drivers/cpuidle/cpuidle-powernv.c  | 10 ------
 drivers/cpuidle/cpuidle-pseries.c  | 11 -------
 drivers/cpuidle/cpuidle.c          | 22 ++++++++++++-
 drivers/cpuidle/poll_state.c       | 30 +++++++----------
 drivers/idle/intel_idle.c          |  5 ++-
 include/linux/cpuidle.h            |  1 +
 include/linux/sched/idle.h         |  7 +++-
 kernel/sched/idle.c                | 53 +++++++++---------------------
 11 files changed, 63 insertions(+), 85 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/5 cpuidle: Remove unnecessary current_clr_polling_and_test() from haltpoll

When cpuidle drivers ->enter() callback are called, the TIF_NR_POLLING flag is cleared already and TIF_NEED_RESCHED checked by call_cpuidle().

Therefore calling current_clr_polling_and_test() is redundant here and further setting of TIF_NEED_RESCHED will result in an IPI and thus an idle loop exit. This call can be safely removed.

---
 drivers/cpuidle/cpuidle-haltpoll.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/cpuidle/cpuidle-haltpoll.c +0 -3
index bcd03e893a0a..07dd9f000273 100644
@@ -28,9 +28,6 @@ static enum cpuhp_state haltpoll_hp_state;
 static __cpuidle int default_enter_idle(struct cpuidle_device *dev,
 					struct cpuidle_driver *drv, int index)
 {
-	if (current_clr_polling_and_test())
-		return index;
-
 	arch_cpu_idle();
 	return index;
 }
Signature · 1 line
2.46.0
#2 lore raw

PATCH 2/5 cpuidle: Introduce CPUIDLE_FLAG_MWAIT

In reply to Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

Provide a way to tell the cpuidle core about states monitoring TIF_NEED_RESCHED on the hardware level, monitor/mwait users being the only examples in use.

This will allow cpuidle core to manage TIF_NR_POLLING on behalf of all kinds of TIF_NEED_RESCHED watching states while keeping a necessary distinction for the governors between software loops polling on TIF_NEED_RESCHED and hardware monitored writes to thread flags.

[fweisbec: _ Initialize flag from acpi_processor_setup_cstates() instead
of acpi_processor_setup_lpi_states(), as the latter seem to be about arm64...
_ Rename CPUIDLE_FLAG_NO_IPI to CPUIDLE_FLAG_MWAIT]

---
 drivers/acpi/processor_idle.c | 3 +++
 drivers/idle/intel_idle.c     | 5 ++++-
 include/linux/cpuidle.h       | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/acpi/processor_idle.c +3 -0
index 698897b29de2..66cb5536d91e 100644
@@ -806,6 +806,9 @@ static int acpi_processor_setup_cstates(struct acpi_processor *pr)
 		if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2)
 			drv->safe_state_index = count;
 
+		if (cx->entry_method == ACPI_CSTATE_FFH)
+			state->flags |= CPUIDLE_FLAG_MWAIT;
+
 		/*
 		 * Halt-induced C1 is not good for ->enter_s2idle, because it
 		 * re-enables interrupts on exit.  Moreover, C1 is generally not
drivers/idle/intel_idle.c +4 -1
index ac4d8faa3886..d52723fbeb04 100644
@@ -1787,7 +1787,8 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
 		if (cx->type > ACPI_STATE_C1)
 			state->target_residency *= 3;
 
-		state->flags = MWAIT2flg(cx->address);
+		state->flags = MWAIT2flg(cx->address) | CPUIDLE_FLAG_MWAIT;
+
 		if (cx->type > ACPI_STATE_C2)
 			state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
 
@@ -2072,6 +2073,8 @@ static bool __init intel_idle_verify_cstate(unsigned int mwait_hint)
 
 static void state_update_enter_method(struct cpuidle_state *state, int cstate)
 {
+	state->flags |= CPUIDLE_FLAG_MWAIT;
+
 	if (state->flags & CPUIDLE_FLAG_INIT_XSTATE) {
 		/*
 		 * Combining with XSTATE with IBRS or IRQ_ENABLE flags
include/linux/cpuidle.h +1 -0
index a9ee4fe55dcf..b8084617aa27 100644
@@ -85,6 +85,7 @@ struct cpuidle_state {
 #define CPUIDLE_FLAG_OFF		BIT(4) /* disable this state by default */
 #define CPUIDLE_FLAG_TLB_FLUSHED	BIT(5) /* idle-state flushes TLBs */
 #define CPUIDLE_FLAG_RCU_IDLE		BIT(6) /* idle-state takes care of RCU */
+#define CPUIDLE_FLAG_MWAIT		BIT(7) /* hardware need_resched() monitoring */
 
 struct cpuidle_device_kobj;
 struct cpuidle_state_kobj;
Signature · 1 line
2.46.0
maintainer
#3 lore raw

Re: [PATCH 2/5] cpuidle: Introduce CPUIDLE_FLAG_MWAIT

Quoted 11 lines · Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

Provide a way to tell the cpuidle core about states monitoring TIF_NEED_RESCHED on the hardware level, monitor/mwait users being the only examples in use.

This will allow cpuidle core to manage TIF_NR_POLLING on behalf of all kinds of TIF_NEED_RESCHED watching states while keeping a necessary distinction for the governors between software loops polling on TIF_NEED_RESCHED and hardware monitored writes to thread flags.

Quoted 6 lines

[fweisbec: _ Initialize flag from acpi_processor_setup_cstates() instead
of acpi_processor_setup_lpi_states(), as the latter seem to be about arm64...
_ Rename CPUIDLE_FLAG_NO_IPI to CPUIDLE_FLAG_MWAIT]

Quoted 59 lines
---
 drivers/acpi/processor_idle.c | 3 +++
 drivers/idle/intel_idle.c     | 5 ++++-
 include/linux/cpuidle.h       | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/acpi/processor_idle.c +3 -0
index 698897b29de2..66cb5536d91e 100644
@@ -806,6 +806,9 @@ static int acpi_processor_setup_cstates(struct acpi_processor *pr)
 		if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2)
 			drv->safe_state_index = count;
 
+		if (cx->entry_method == ACPI_CSTATE_FFH)
+			state->flags |= CPUIDLE_FLAG_MWAIT;
+
 		/*
 		 * Halt-induced C1 is not good for ->enter_s2idle, because it
 		 * re-enables interrupts on exit.  Moreover, C1 is generally not
drivers/idle/intel_idle.c +4 -1
index ac4d8faa3886..d52723fbeb04 100644
@@ -1787,7 +1787,8 @@ static void __init intel_idle_init_cstates_acpi(struct cpuidle_driver *drv)
 		if (cx->type > ACPI_STATE_C1)
 			state->target_residency *= 3;
 
-		state->flags = MWAIT2flg(cx->address);
+		state->flags = MWAIT2flg(cx->address) | CPUIDLE_FLAG_MWAIT;
+
 		if (cx->type > ACPI_STATE_C2)
 			state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
 
@@ -2072,6 +2073,8 @@ static bool __init intel_idle_verify_cstate(unsigned int mwait_hint)
 
 static void state_update_enter_method(struct cpuidle_state *state, int cstate)
 {
+	state->flags |= CPUIDLE_FLAG_MWAIT;
+
 	if (state->flags & CPUIDLE_FLAG_INIT_XSTATE) {
 		/*
 		 * Combining with XSTATE with IBRS or IRQ_ENABLE flags
include/linux/cpuidle.h +1 -0
index a9ee4fe55dcf..b8084617aa27 100644
@@ -85,6 +85,7 @@ struct cpuidle_state {
 #define CPUIDLE_FLAG_OFF		BIT(4) /* disable this state by default */
 #define CPUIDLE_FLAG_TLB_FLUSHED	BIT(5) /* idle-state flushes TLBs */
 #define CPUIDLE_FLAG_RCU_IDLE		BIT(6) /* idle-state takes care of RCU */
+#define CPUIDLE_FLAG_MWAIT		BIT(7) /* hardware need_resched() monitoring */
 
 struct cpuidle_device_kobj;
 struct cpuidle_state_kobj;
Signature · 1 line
2.46.0
#4 lore raw

PATCH 3/5 cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states

In reply to Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

The current handling of TIF_NR_POLLING is a bit of a maze:

1) TIF_NR_POLLING is set on idle entry (one atomic set)

2) Once cpuidle has selected an appropriate state and the tick is evaluated and then possibly stopped, TIF_NR_POLLING is cleared (one RmW operation)

2) The cpuidle state is then called with TIF_NR_POLLING cleared but if the state polls on (or monitors) need_resched() it sets again TIF_NR_POLLING before sleeping and clears it on wake-up. Summary:
another pair of set/clear

3) Set back TIF_NR_POLLING (one atomic set)

4) goto 2) if need_resched() is not set

All those costly atomic operations, fully ordered RmW for some of them, could be avoided if the cpuidle core knew in advance if the target state polls on (or monitors) need_resched(). If so, TIF_NR_POLLING could simply be set once upon entering the idle loop and cleared once after idle loop exit.

Start dealing with that with handling TIF_NR_POLLING on behalf of mwait based states.

[fweisbec: _ Handle broadcast properly
_ Ignore mwait_idle() as it can be used by default_idle_call()]

---
 arch/x86/include/asm/mwait.h |  3 +--
 drivers/cpuidle/cpuidle.c    | 22 +++++++++++++++++++-
 include/linux/sched/idle.h   |  7 ++++++-
 kernel/sched/idle.c          | 40 +++++++++++++-----------------------
 4 files changed, 42 insertions(+), 30 deletions(-)
arch/x86/include/asm/mwait.h +1 -2
index 920426d691ce..3634d00e5c37 100644
@@ -116,7 +116,7 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  */
 static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
 {
-	if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
+	if (static_cpu_has_bug(X86_BUG_MONITOR) || !need_resched()) {
 		if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
 			mb();
 			clflush((void *)&current_thread_info()->flags);
@@ -134,7 +134,6 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
 			}
 		}
 	}
-	current_clr_polling();
 }
 
 /*
drivers/cpuidle/cpuidle.c +21 -1
index 0835da449db8..46c0a2726f67 100644
@@ -217,10 +217,10 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 				 int index)
 {
 	int entered_state;
-
 	struct cpuidle_state *target_state = &drv->states[index];
 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
 	ktime_t time_start, time_end;
+	bool polling;
 
 	instrumentation_begin();
 
@@ -237,6 +237,23 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 		broadcast = false;
 	}
 
+	polling = target_state->flags & CPUIDLE_FLAG_MWAIT;
+
+	/*
+	 * If the target state doesn't poll on need_resched(), this is
+	 * the last check after which further TIF_NEED_RESCHED remote setting
+	 * will involve an IPI.
+	 */
+	if (!polling && current_clr_polling_and_test()) {
+		if (broadcast)
+			tick_broadcast_exit();
+		dev->last_residency_ns = 0;
+		local_irq_enable();
+		instrumentation_end();
+		return -EBUSY;
+	}
+
+
 	if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
 		leave_mm();
 
@@ -336,6 +353,9 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 		dev->states_usage[index].rejected++;
 	}
 
+	if (!polling)
+		__current_set_polling();
+
 	instrumentation_end();
 
 	return entered_state;
include/linux/sched/idle.h +6 -1
index e670ac282333..3e3482bfb028 100644
@@ -68,6 +68,8 @@ static __always_inline bool __must_check current_set_polling_and_test(void)
 
 static __always_inline bool __must_check current_clr_polling_and_test(void)
 {
+	bool ret;
+
 	__current_clr_polling();
 
 	/*
@@ -76,7 +78,10 @@ static __always_inline bool __must_check current_clr_polling_and_test(void)
 	 */
 	smp_mb__after_atomic();
 
-	return unlikely(tif_need_resched());
+	ret = unlikely(tif_need_resched());
+	if (ret)
+		__current_set_polling();
+	return ret;
 }
 
 #else
kernel/sched/idle.c +14 -26
index 621696269584..9eece3df1080 100644
@@ -114,12 +114,13 @@ void __cpuidle default_idle_call(void)
 		stop_critical_timings();
 
 		ct_cpuidle_enter();
-		arch_cpu_idle();
+		arch_cpu_idle(); // XXX assumes !polling
 		ct_cpuidle_exit();
 
 		start_critical_timings();
 		trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
 		cond_tick_broadcast_exit();
+		__current_set_polling();
 	}
 	local_irq_enable();
 	instrumentation_end();
@@ -128,31 +129,14 @@ void __cpuidle default_idle_call(void)
 static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
 			       struct cpuidle_device *dev)
 {
+	int ret;
+
 	if (current_clr_polling_and_test())
 		return -EBUSY;
 
-	return cpuidle_enter_s2idle(drv, dev);
-}
-
-static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
-		      int next_state)
-{
-	/*
-	 * The idle task must be scheduled, it is pointless to go to idle, just
-	 * update no idle residency and return.
-	 */
-	if (current_clr_polling_and_test()) {
-		dev->last_residency_ns = 0;
-		local_irq_enable();
-		return -EBUSY;
-	}
-
-	/*
-	 * 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
-	 */
-	return cpuidle_enter(drv, dev, next_state);
+	ret = cpuidle_enter_s2idle(drv, dev);
+	__current_set_polling();
+	return ret;
 }
 
 /**
@@ -213,7 +197,7 @@ static void cpuidle_idle_call(void)
 		tick_nohz_idle_stop_tick();
 
 		next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
-		call_cpuidle(drv, dev, next_state);
+		cpuidle_enter(drv, dev, next_state);
 	} else {
 		bool stop_tick = true;
 
@@ -227,7 +211,12 @@ static void cpuidle_idle_call(void)
 		else
 			tick_nohz_idle_retain_tick();
 
-		entered_state = call_cpuidle(drv, dev, next_state);
+		/*
+		 * 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);
 		/*
 		 * Give the governor an opportunity to reflect on the outcome
 		 */
@@ -235,7 +224,6 @@ static void cpuidle_idle_call(void)
 	}
 
 exit_idle:
-	__current_set_polling();
 
 	/*
 	 * It is up to the idle functions to re-enable local interrupts
Signature · 1 line
2.46.0
maintainer
#5 lore raw

Re: [PATCH 3/5] cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states

Quoted 25 lines · Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

The current handling of TIF_NR_POLLING is a bit of a maze:

1) TIF_NR_POLLING is set on idle entry (one atomic set)

2) Once cpuidle has selected an appropriate state and the tick is evaluated and then possibly stopped, TIF_NR_POLLING is cleared (one RmW operation)

2) The cpuidle state is then called with TIF_NR_POLLING cleared but if the state polls on (or monitors) need_resched() it sets again TIF_NR_POLLING before sleeping and clears it on wake-up. Summary:
another pair of set/clear

3) Set back TIF_NR_POLLING (one atomic set)

4) goto 2) if need_resched() is not set

All those costly atomic operations, fully ordered RmW for some of them, could be avoided if the cpuidle core knew in advance if the target state polls on (or monitors) need_resched(). If so, TIF_NR_POLLING could simply be set once upon entering the idle loop and cleared once after idle loop exit.

Quoted 7 lines

Start dealing with that with handling TIF_NR_POLLING on behalf of mwait based states.

[fweisbec: _ Handle broadcast properly
_ Ignore mwait_idle() as it can be used by default_idle_call()]

Quoted 194 lines
---
 arch/x86/include/asm/mwait.h |  3 +--
 drivers/cpuidle/cpuidle.c    | 22 +++++++++++++++++++-
 include/linux/sched/idle.h   |  7 ++++++-
 kernel/sched/idle.c          | 40 +++++++++++++-----------------------
 4 files changed, 42 insertions(+), 30 deletions(-)
arch/x86/include/asm/mwait.h +1 -2
index 920426d691ce..3634d00e5c37 100644
@@ -116,7 +116,7 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  */
 static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
 {
-	if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
+	if (static_cpu_has_bug(X86_BUG_MONITOR) || !need_resched()) {
 		if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
 			mb();
 			clflush((void *)&current_thread_info()->flags);
@@ -134,7 +134,6 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
 			}
 		}
 	}
-	current_clr_polling();
 }
 
 /*
drivers/cpuidle/cpuidle.c +21 -1
index 0835da449db8..46c0a2726f67 100644
@@ -217,10 +217,10 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 				 int index)
 {
 	int entered_state;
-
 	struct cpuidle_state *target_state = &drv->states[index];
 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
 	ktime_t time_start, time_end;
+	bool polling;
 
 	instrumentation_begin();
 
@@ -237,6 +237,23 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 		broadcast = false;
 	}
 
+	polling = target_state->flags & CPUIDLE_FLAG_MWAIT;
+
+	/*
+	 * If the target state doesn't poll on need_resched(), this is
+	 * the last check after which further TIF_NEED_RESCHED remote setting
+	 * will involve an IPI.
+	 */
+	if (!polling && current_clr_polling_and_test()) {
+		if (broadcast)
+			tick_broadcast_exit();
+		dev->last_residency_ns = 0;
+		local_irq_enable();
+		instrumentation_end();
+		return -EBUSY;
+	}
+
+
 	if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
 		leave_mm();
 
@@ -336,6 +353,9 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 		dev->states_usage[index].rejected++;
 	}
 
+	if (!polling)
+		__current_set_polling();
+
 	instrumentation_end();
 
 	return entered_state;
include/linux/sched/idle.h +6 -1
index e670ac282333..3e3482bfb028 100644
@@ -68,6 +68,8 @@ static __always_inline bool __must_check current_set_polling_and_test(void)
 
 static __always_inline bool __must_check current_clr_polling_and_test(void)
 {
+	bool ret;
+
 	__current_clr_polling();
 
 	/*
@@ -76,7 +78,10 @@ static __always_inline bool __must_check current_clr_polling_and_test(void)
 	 */
 	smp_mb__after_atomic();
 
-	return unlikely(tif_need_resched());
+	ret = unlikely(tif_need_resched());
+	if (ret)
+		__current_set_polling();
+	return ret;
 }
 
 #else
kernel/sched/idle.c +14 -26
index 621696269584..9eece3df1080 100644
@@ -114,12 +114,13 @@ void __cpuidle default_idle_call(void)
 		stop_critical_timings();
 
 		ct_cpuidle_enter();
-		arch_cpu_idle();
+		arch_cpu_idle(); // XXX assumes !polling
 		ct_cpuidle_exit();
 
 		start_critical_timings();
 		trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
 		cond_tick_broadcast_exit();
+		__current_set_polling();
 	}
 	local_irq_enable();
 	instrumentation_end();
@@ -128,31 +129,14 @@ void __cpuidle default_idle_call(void)
 static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
 			       struct cpuidle_device *dev)
 {
+	int ret;
+
 	if (current_clr_polling_and_test())
 		return -EBUSY;
 
-	return cpuidle_enter_s2idle(drv, dev);
-}
-
-static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
-		      int next_state)
-{
-	/*
-	 * The idle task must be scheduled, it is pointless to go to idle, just
-	 * update no idle residency and return.
-	 */
-	if (current_clr_polling_and_test()) {
-		dev->last_residency_ns = 0;
-		local_irq_enable();
-		return -EBUSY;
-	}
-
-	/*
-	 * 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
-	 */
-	return cpuidle_enter(drv, dev, next_state);
+	ret = cpuidle_enter_s2idle(drv, dev);
+	__current_set_polling();
+	return ret;
 }
 
 /**
@@ -213,7 +197,7 @@ static void cpuidle_idle_call(void)
 		tick_nohz_idle_stop_tick();
 
 		next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
-		call_cpuidle(drv, dev, next_state);
+		cpuidle_enter(drv, dev, next_state);
 	} else {
 		bool stop_tick = true;
 
@@ -227,7 +211,12 @@ static void cpuidle_idle_call(void)
 		else
 			tick_nohz_idle_retain_tick();
 
-		entered_state = call_cpuidle(drv, dev, next_state);
+		/*
+		 * 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);
 		/*
 		 * Give the governor an opportunity to reflect on the outcome
 		 */
@@ -235,7 +224,6 @@ static void cpuidle_idle_call(void)
 	}
 
 exit_idle:
-	__current_set_polling();
 
 	/*
 	 * It is up to the idle functions to re-enable local interrupts
Signature · 1 line
2.46.0
#6 lore raw

Re: [PATCH 3/5] cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states

In reply to Frederic Weisbecker
Quoted 46 lines · Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

The current handling of TIF_NR_POLLING is a bit of a maze:

1) TIF_NR_POLLING is set on idle entry (one atomic set)

2) Once cpuidle has selected an appropriate state and the tick is evaluated and then possibly stopped, TIF_NR_POLLING is cleared (one RmW operation)

2) The cpuidle state is then called with TIF_NR_POLLING cleared but if the state polls on (or monitors) need_resched() it sets again TIF_NR_POLLING before sleeping and clears it on wake-up. Summary:
another pair of set/clear

3) Set back TIF_NR_POLLING (one atomic set)

4) goto 2) if need_resched() is not set

All those costly atomic operations, fully ordered RmW for some of them, could be avoided if the cpuidle core knew in advance if the target state polls on (or monitors) need_resched(). If so, TIF_NR_POLLING could simply be set once upon entering the idle loop and cleared once after idle loop exit.

Start dealing with that with handling TIF_NR_POLLING on behalf of mwait based states.

[fweisbec: _ Handle broadcast properly
_ Ignore mwait_idle() as it can be used by default_idle_call()]

---
 arch/x86/include/asm/mwait.h |  3 +--
 drivers/cpuidle/cpuidle.c    | 22 +++++++++++++++++++-
 include/linux/sched/idle.h   |  7 ++++++-
 kernel/sched/idle.c          | 40 +++++++++++++-----------------------
 4 files changed, 42 insertions(+), 30 deletions(-)
arch/x86/include/asm/mwait.h +0 -0
index 920426d691ce..3634d00e5c37 100644

arch/x86/include/asm/mwait.h static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) · line 119

Quoted 5 lines
arch/x86/include/asm/mwait.h +1 -1
@@ -116,7 +116,7 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  */
 static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
 {
-       if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
+       if (static_cpu_has_bug(X86_BUG_MONITOR) || !need_resched()) {

I'm not sure how X86_BUG_MONITOR is going to work after the change.

As is, X86_BUG_MONITOR prevents current_set_polling_and_test() from being called, so __current_set_polling() will not be called and TIF_POLLING_NRFLAG will not be set, so an IPI is going to be used for CPU wakeup, which is what X86_BUG_MONITOR wants.

Preventing need_resched() from being called doesn't have this effect and the rest of the patch doesn't do anything about X86_BUG_MONITOR.

Is anything missing?

Quoted 176 lines
arch/x86/include/asm/mwait.h +0 -1
                if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
                        mb();
                        clflush((void *)&current_thread_info()->flags);
@@ -134,7 +134,6 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
                        }
                }
        }
-       current_clr_polling();
 }
 
 /*
drivers/cpuidle/cpuidle.c +21 -1
index 0835da449db8..46c0a2726f67 100644
@@ -217,10 +217,10 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
                                 int index)
 {
        int entered_state;
-
        struct cpuidle_state *target_state = &drv->states[index];
        bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
        ktime_t time_start, time_end;
+       bool polling;
 
        instrumentation_begin();
 
@@ -237,6 +237,23 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
                broadcast = false;
        }
 
+       polling = target_state->flags & CPUIDLE_FLAG_MWAIT;
+
+       /*
+        * If the target state doesn't poll on need_resched(), this is
+        * the last check after which further TIF_NEED_RESCHED remote setting
+        * will involve an IPI.
+        */
+       if (!polling && current_clr_polling_and_test()) {
+               if (broadcast)
+                       tick_broadcast_exit();
+               dev->last_residency_ns = 0;
+               local_irq_enable();
+               instrumentation_end();
+               return -EBUSY;
+       }
+
+
        if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
                leave_mm();
 
@@ -336,6 +353,9 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
                dev->states_usage[index].rejected++;
        }
 
+       if (!polling)
+               __current_set_polling();
+
        instrumentation_end();
 
        return entered_state;
include/linux/sched/idle.h +6 -1
index e670ac282333..3e3482bfb028 100644
@@ -68,6 +68,8 @@ static __always_inline bool __must_check current_set_polling_and_test(void)
 
 static __always_inline bool __must_check current_clr_polling_and_test(void)
 {
+       bool ret;
+
        __current_clr_polling();
 
        /*
@@ -76,7 +78,10 @@ static __always_inline bool __must_check current_clr_polling_and_test(void)
         */
        smp_mb__after_atomic();
 
-       return unlikely(tif_need_resched());
+       ret = unlikely(tif_need_resched());
+       if (ret)
+               __current_set_polling();
+       return ret;
 }
 
 #else
kernel/sched/idle.c +14 -27
index 621696269584..9eece3df1080 100644
@@ -114,12 +114,13 @@ void __cpuidle default_idle_call(void)
                stop_critical_timings();
 
                ct_cpuidle_enter();
-               arch_cpu_idle();
+               arch_cpu_idle(); // XXX assumes !polling
                ct_cpuidle_exit();
 
                start_critical_timings();
                trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
                cond_tick_broadcast_exit();
+               __current_set_polling();
        }
        local_irq_enable();
        instrumentation_end();
@@ -128,31 +129,14 @@ void __cpuidle default_idle_call(void)
 static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
                               struct cpuidle_device *dev)
 {
+       int ret;
+
        if (current_clr_polling_and_test())
                return -EBUSY;
 
-       return cpuidle_enter_s2idle(drv, dev);
-}
-
-static int call_cpuidle(struct cpuidle_driver *drv, struct cpuidle_device *dev,
-                     int next_state)
-{
-       /*
-        * The idle task must be scheduled, it is pointless to go to idle, just
-        * update no idle residency and return.
-        */
-       if (current_clr_polling_and_test()) {
-               dev->last_residency_ns = 0;
-               local_irq_enable();
-               return -EBUSY;
-       }
-
-       /*
-        * 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
-        */
-       return cpuidle_enter(drv, dev, next_state);
+       ret = cpuidle_enter_s2idle(drv, dev);
+       __current_set_polling();
+       return ret;
 }
 
 /**
@@ -213,7 +197,7 @@ static void cpuidle_idle_call(void)
                tick_nohz_idle_stop_tick();
 
                next_state = cpuidle_find_deepest_state(drv, dev, max_latency_ns);
-               call_cpuidle(drv, dev, next_state);
+               cpuidle_enter(drv, dev, next_state);
        } else {
                bool stop_tick = true;
 
@@ -227,7 +211,12 @@ static void cpuidle_idle_call(void)
                else
                        tick_nohz_idle_retain_tick();
 
-               entered_state = call_cpuidle(drv, dev, next_state);
+               /*
+                * 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);
                /*
                 * Give the governor an opportunity to reflect on the outcome
                 */
@@ -235,7 +224,6 @@ static void cpuidle_idle_call(void)
        }
 
 exit_idle:
-       __current_set_polling();
 
        /*
         * It is up to the idle functions to re-enable local interrupts
--

2.46.0

#7 lore raw

Re: [PATCH 3/5] cpuidle: Handle TIF_NR_POLLING on behalf of CPUIDLE_FLAG_MWAIT states

Quoted 60 lines · Rafael J. Wysocki
Quoted 46 lines · Frederic Weisbecker

From: Peter Zijlstra <[email protected]>

The current handling of TIF_NR_POLLING is a bit of a maze:

1) TIF_NR_POLLING is set on idle entry (one atomic set)

2) Once cpuidle has selected an appropriate state and the tick is evaluated and then possibly stopped, TIF_NR_POLLING is cleared (one RmW operation)

2) The cpuidle state is then called with TIF_NR_POLLING cleared but if the state polls on (or monitors) need_resched() it sets again TIF_NR_POLLING before sleeping and clears it on wake-up. Summary:
another pair of set/clear

3) Set back TIF_NR_POLLING (one atomic set)

4) goto 2) if need_resched() is not set

All those costly atomic operations, fully ordered RmW for some of them, could be avoided if the cpuidle core knew in advance if the target state polls on (or monitors) need_resched(). If so, TIF_NR_POLLING could simply be set once upon entering the idle loop and cleared once after idle loop exit.

Start dealing with that with handling TIF_NR_POLLING on behalf of mwait based states.

[fweisbec: _ Handle broadcast properly
_ Ignore mwait_idle() as it can be used by default_idle_call()]

---
 arch/x86/include/asm/mwait.h |  3 +--
 drivers/cpuidle/cpuidle.c    | 22 +++++++++++++++++++-
 include/linux/sched/idle.h   |  7 ++++++-
 kernel/sched/idle.c          | 40 +++++++++++++-----------------------
 4 files changed, 42 insertions(+), 30 deletions(-)
arch/x86/include/asm/mwait.h +0 -0
index 920426d691ce..3634d00e5c37 100644

arch/x86/include/asm/mwait.h static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) · line 119

Quoted 5 lines
arch/x86/include/asm/mwait.h +1 -1
@@ -116,7 +116,7 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  */
 static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
 {
-       if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
+       if (static_cpu_has_bug(X86_BUG_MONITOR) || !need_resched()) {

I'm not sure how X86_BUG_MONITOR is going to work after the change.

As is, X86_BUG_MONITOR prevents current_set_polling_and_test() from being called, so __current_set_polling() will not be called and TIF_POLLING_NRFLAG will not be set, so an IPI is going to be used for CPU wakeup, which is what X86_BUG_MONITOR wants.

Quoted 4 lines

Preventing need_resched() from being called doesn't have this effect and the rest of the patch doesn't do anything about X86_BUG_MONITOR.

Is anything missing?

I fear you're right, I overlooked that. Probably I should set CPUIDLE_FLAG_MWAIT only if !boot_cpu_has_bug(X86_BUG_MONITOR). Lemme see that.

Thanks.

#8 lore raw

PATCH 4/5 cpuidle: Remove call_cpuidle_s2idle()

In reply to Frederic Weisbecker

This middle call is unecessary, especially now that its counterpart call_cpuidle() has been removed.

---
 kernel/sched/idle.c | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)
kernel/sched/idle.c +6 -17
index 9eece3df1080..86b902eb24fe 100644
@@ -126,19 +126,6 @@ void __cpuidle default_idle_call(void)
 	instrumentation_end();
 }
 
-static int call_cpuidle_s2idle(struct cpuidle_driver *drv,
-			       struct cpuidle_device *dev)
-{
-	int ret;
-
-	if (current_clr_polling_and_test())
-		return -EBUSY;
-
-	ret = cpuidle_enter_s2idle(drv, dev);
-	__current_set_polling();
-	return ret;
-}
-
 /**
  * cpuidle_idle_call - the main idle function
  *
@@ -184,10 +171,12 @@ static void cpuidle_idle_call(void)
 		u64 max_latency_ns;
 
 		if (idle_should_enter_s2idle()) {
-
-			entered_state = call_cpuidle_s2idle(drv, dev);
-			if (entered_state > 0)
-				goto exit_idle;
+			if (!current_clr_polling_and_test()) {
+				entered_state = cpuidle_enter_s2idle(drv, dev);
+				__current_set_polling();
+				if (entered_state > 0)
+					goto exit_idle;
+			}
 
 			max_latency_ns = U64_MAX;
 		} else {
Signature · 1 line
2.46.0
#9 lore raw

PATCH 5/5 cpuidle: Handle TIF_NR_POLLING on behalf of software polling idle states

In reply to Frederic Weisbecker

Software polling idle states set again TIF_NR_POLLING and clear it upon exit. This involves error prone duplicated code and wasted cycles performing atomic operations, sometimes RmW fully ordered.

To avoid this, benefit instead from the same generic TIF_NR_POLLING handling that is currently in use for hardware monitoring states.

---
 drivers/cpuidle/cpuidle-powernv.c | 10 ----------
 drivers/cpuidle/cpuidle-pseries.c | 11 -----------
 drivers/cpuidle/cpuidle.c         |  2 +-
 drivers/cpuidle/poll_state.c      | 30 ++++++++++++------------------
 4 files changed, 13 insertions(+), 40 deletions(-)
drivers/cpuidle/cpuidle-powernv.c +0 -10
index 9ebedd972df0..1bf0d2234016 100644
@@ -71,8 +71,6 @@ static int snooze_loop(struct cpuidle_device *dev,
 {
 	u64 snooze_exit_time;
 
-	set_thread_flag(TIF_POLLING_NRFLAG);
-
 	local_irq_enable();
 
 	snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
@@ -81,21 +79,13 @@ static int snooze_loop(struct cpuidle_device *dev,
 	HMT_very_low();
 	while (!need_resched()) {
 		if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
-			/*
-			 * Task has not woken up but we are exiting the polling
-			 * loop anyway. Require a barrier after polling is
-			 * cleared to order subsequent test of need_resched().
-			 */
-			clear_thread_flag(TIF_POLLING_NRFLAG);
 			dev->poll_time_limit = true;
-			smp_mb();
 			break;
 		}
 	}
 
 	HMT_medium();
 	ppc64_runlatch_on();
-	clear_thread_flag(TIF_POLLING_NRFLAG);
 
 	local_irq_disable();
 
drivers/cpuidle/cpuidle-pseries.c +0 -11
index f68c65f1d023..704bb01d9e9e 100644
@@ -40,8 +40,6 @@ int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 {
 	u64 snooze_exit_time;
 
-	set_thread_flag(TIF_POLLING_NRFLAG);
-
 	pseries_idle_prolog();
 	raw_local_irq_enable();
 	snooze_exit_time = get_tb() + snooze_timeout;
@@ -51,21 +49,12 @@ int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 		HMT_low();
 		HMT_very_low();
 		if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
-			/*
-			 * Task has not woken up but we are exiting the polling
-			 * loop anyway. Require a barrier after polling is
-			 * cleared to order subsequent test of need_resched().
-			 */
 			dev->poll_time_limit = true;
-			clear_thread_flag(TIF_POLLING_NRFLAG);
-			smp_mb();
 			break;
 		}
 	}
 
 	HMT_medium();
-	clear_thread_flag(TIF_POLLING_NRFLAG);
-
 	raw_local_irq_disable();
 
 	pseries_idle_epilog();
drivers/cpuidle/cpuidle.c +1 -1
index 46c0a2726f67..fecc50c2860e 100644
@@ -237,7 +237,7 @@ noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
 		broadcast = false;
 	}
 
-	polling = target_state->flags & CPUIDLE_FLAG_MWAIT;
+	polling = target_state->flags & (CPUIDLE_FLAG_MWAIT | CPUIDLE_FLAG_POLLING);
 
 	/*
 	 * If the target state doesn't poll on need_resched(), this is
drivers/cpuidle/poll_state.c +12 -18
index 9b6d90a72601..d69936e2517e 100644
@@ -13,35 +13,29 @@
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int index)
 {
-	u64 time_start;
-
-	time_start = local_clock_noinstr();
+	u64 time_start = local_clock_noinstr();
+	unsigned int loop_count = 0;
+	u64 limit;
 
 	dev->poll_time_limit = false;
 
 	raw_local_irq_enable();
-	if (!current_set_polling_and_test()) {
-		unsigned int loop_count = 0;
-		u64 limit;
 
-		limit = cpuidle_poll_time(drv, dev);
+	limit = cpuidle_poll_time(drv, dev);
 
-		while (!need_resched()) {
-			cpu_relax();
-			if (loop_count++ < POLL_IDLE_RELAX_COUNT)
-				continue;
+	while (!need_resched()) {
+		cpu_relax();
+		if (loop_count++ < POLL_IDLE_RELAX_COUNT)
+			continue;
 
-			loop_count = 0;
-			if (local_clock_noinstr() - time_start > limit) {
-				dev->poll_time_limit = true;
-				break;
-			}
+		loop_count = 0;
+		if (local_clock_noinstr() - time_start > limit) {
+			dev->poll_time_limit = true;
+			break;
 		}
 	}
 	raw_local_irq_disable();
 
-	current_clr_polling();
-
 	return index;
 }
 
Signature · 1 line
2.46.0