Commit ea811747 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

sched, idle: Fix the idle polling state logic

Mike reported that commit 7d1a9417 ("x86: Use generic idle loop")
regressed several workloads and caused excessive reschedule
interrupts.

The patch in question failed to notice that the x86 code had an
inverted sense of the polling state versus the new generic code (x86:
default polling, generic: default !polling).

Fix the two prominent x86 mwait based idle drivers and introduce a few
new generic polling helpers (fixing the wrong smp_mb__after_clear_bit
usage).

Also switch the idle routines to using tif_need_resched() which is an
immediate TIF_NEED_RESCHED test as opposed to need_resched which will
end up being slightly different.
Reported-by: default avatarMike Galbraith <bitbucket@online.de>
Signed-off-by: default avatarPeter Zijlstra <peterz@infradead.org>
Cc: lenb@kernel.org
Cc: tglx@linutronix.de
Link: http://lkml.kernel.org/n/tip-nc03imb0etuefmzybzj7sprf@git.kernel.orgSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 31503986
...@@ -119,17 +119,10 @@ static struct dmi_system_id processor_power_dmi_table[] = { ...@@ -119,17 +119,10 @@ static struct dmi_system_id processor_power_dmi_table[] = {
*/ */
static void acpi_safe_halt(void) static void acpi_safe_halt(void)
{ {
current_thread_info()->status &= ~TS_POLLING; if (!tif_need_resched()) {
/*
* TS_POLLING-cleared state must be visible before we
* test NEED_RESCHED:
*/
smp_mb();
if (!need_resched()) {
safe_halt(); safe_halt();
local_irq_disable(); local_irq_disable();
} }
current_thread_info()->status |= TS_POLLING;
} }
#ifdef ARCH_APICTIMER_STOPS_ON_C3 #ifdef ARCH_APICTIMER_STOPS_ON_C3
...@@ -737,6 +730,11 @@ static int acpi_idle_enter_c1(struct cpuidle_device *dev, ...@@ -737,6 +730,11 @@ static int acpi_idle_enter_c1(struct cpuidle_device *dev,
if (unlikely(!pr)) if (unlikely(!pr))
return -EINVAL; return -EINVAL;
if (cx->entry_method == ACPI_CSTATE_FFH) {
if (current_set_polling_and_test())
return -EINVAL;
}
lapic_timer_state_broadcast(pr, cx, 1); lapic_timer_state_broadcast(pr, cx, 1);
acpi_idle_do_entry(cx); acpi_idle_do_entry(cx);
...@@ -790,19 +788,10 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev, ...@@ -790,19 +788,10 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
if (unlikely(!pr)) if (unlikely(!pr))
return -EINVAL; return -EINVAL;
if (cx->entry_method != ACPI_CSTATE_FFH) { if (cx->entry_method == ACPI_CSTATE_FFH) {
current_thread_info()->status &= ~TS_POLLING; if (current_set_polling_and_test())
/*
* TS_POLLING-cleared state must be visible before we test
* NEED_RESCHED:
*/
smp_mb();
if (unlikely(need_resched())) {
current_thread_info()->status |= TS_POLLING;
return -EINVAL; return -EINVAL;
} }
}
/* /*
* Must be done before busmaster disable as we might need to * Must be done before busmaster disable as we might need to
...@@ -819,9 +808,6 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev, ...@@ -819,9 +808,6 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
sched_clock_idle_wakeup_event(0); sched_clock_idle_wakeup_event(0);
if (cx->entry_method != ACPI_CSTATE_FFH)
current_thread_info()->status |= TS_POLLING;
lapic_timer_state_broadcast(pr, cx, 0); lapic_timer_state_broadcast(pr, cx, 0);
return index; return index;
} }
...@@ -858,19 +844,10 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev, ...@@ -858,19 +844,10 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
} }
} }
if (cx->entry_method != ACPI_CSTATE_FFH) { if (cx->entry_method == ACPI_CSTATE_FFH) {
current_thread_info()->status &= ~TS_POLLING; if (current_set_polling_and_test())
/*
* TS_POLLING-cleared state must be visible before we test
* NEED_RESCHED:
*/
smp_mb();
if (unlikely(need_resched())) {
current_thread_info()->status |= TS_POLLING;
return -EINVAL; return -EINVAL;
} }
}
acpi_unlazy_tlb(smp_processor_id()); acpi_unlazy_tlb(smp_processor_id());
...@@ -915,9 +892,6 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev, ...@@ -915,9 +892,6 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
sched_clock_idle_wakeup_event(0); sched_clock_idle_wakeup_event(0);
if (cx->entry_method != ACPI_CSTATE_FFH)
current_thread_info()->status |= TS_POLLING;
lapic_timer_state_broadcast(pr, cx, 0); lapic_timer_state_broadcast(pr, cx, 0);
return index; return index;
} }
......
...@@ -359,7 +359,7 @@ static int intel_idle(struct cpuidle_device *dev, ...@@ -359,7 +359,7 @@ static int intel_idle(struct cpuidle_device *dev,
if (!(lapic_timer_reliable_states & (1 << (cstate)))) if (!(lapic_timer_reliable_states & (1 << (cstate))))
clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
if (!need_resched()) { if (!current_set_polling_and_test()) {
__monitor((void *)&current_thread_info()->flags, 0, 0); __monitor((void *)&current_thread_info()->flags, 0, 0);
smp_mb(); smp_mb();
......
...@@ -2479,34 +2479,98 @@ static inline int tsk_is_polling(struct task_struct *p) ...@@ -2479,34 +2479,98 @@ static inline int tsk_is_polling(struct task_struct *p)
{ {
return task_thread_info(p)->status & TS_POLLING; return task_thread_info(p)->status & TS_POLLING;
} }
static inline void current_set_polling(void) static inline void __current_set_polling(void)
{ {
current_thread_info()->status |= TS_POLLING; current_thread_info()->status |= TS_POLLING;
} }
static inline void current_clr_polling(void) static inline bool __must_check current_set_polling_and_test(void)
{
__current_set_polling();
/*
* Polling state must be visible before we test NEED_RESCHED,
* paired by resched_task()
*/
smp_mb();
return unlikely(tif_need_resched());
}
static inline void __current_clr_polling(void)
{ {
current_thread_info()->status &= ~TS_POLLING; current_thread_info()->status &= ~TS_POLLING;
smp_mb__after_clear_bit(); }
static inline bool __must_check current_clr_polling_and_test(void)
{
__current_clr_polling();
/*
* Polling state must be visible before we test NEED_RESCHED,
* paired by resched_task()
*/
smp_mb();
return unlikely(tif_need_resched());
} }
#elif defined(TIF_POLLING_NRFLAG) #elif defined(TIF_POLLING_NRFLAG)
static inline int tsk_is_polling(struct task_struct *p) static inline int tsk_is_polling(struct task_struct *p)
{ {
return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG); return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
} }
static inline void current_set_polling(void)
static inline void __current_set_polling(void)
{ {
set_thread_flag(TIF_POLLING_NRFLAG); set_thread_flag(TIF_POLLING_NRFLAG);
} }
static inline void current_clr_polling(void) static inline bool __must_check current_set_polling_and_test(void)
{
__current_set_polling();
/*
* Polling state must be visible before we test NEED_RESCHED,
* paired by resched_task()
*
* XXX: assumes set/clear bit are identical barrier wise.
*/
smp_mb__after_clear_bit();
return unlikely(tif_need_resched());
}
static inline void __current_clr_polling(void)
{ {
clear_thread_flag(TIF_POLLING_NRFLAG); clear_thread_flag(TIF_POLLING_NRFLAG);
} }
static inline bool __must_check current_clr_polling_and_test(void)
{
__current_clr_polling();
/*
* Polling state must be visible before we test NEED_RESCHED,
* paired by resched_task()
*/
smp_mb__after_clear_bit();
return unlikely(tif_need_resched());
}
#else #else
static inline int tsk_is_polling(struct task_struct *p) { return 0; } static inline int tsk_is_polling(struct task_struct *p) { return 0; }
static inline void current_set_polling(void) { } static inline void __current_set_polling(void) { }
static inline void current_clr_polling(void) { } static inline void __current_clr_polling(void) { }
static inline bool __must_check current_set_polling_and_test(void)
{
return unlikely(tif_need_resched());
}
static inline bool __must_check current_clr_polling_and_test(void)
{
return unlikely(tif_need_resched());
}
#endif #endif
/* /*
......
...@@ -118,6 +118,8 @@ static inline __deprecated void set_need_resched(void) ...@@ -118,6 +118,8 @@ static inline __deprecated void set_need_resched(void)
*/ */
} }
#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
#if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
/* /*
* An arch can define its own version of set_restore_sigmask() to get the * An arch can define its own version of set_restore_sigmask() to get the
......
...@@ -44,7 +44,7 @@ static inline int cpu_idle_poll(void) ...@@ -44,7 +44,7 @@ static inline int cpu_idle_poll(void)
rcu_idle_enter(); rcu_idle_enter();
trace_cpu_idle_rcuidle(0, smp_processor_id()); trace_cpu_idle_rcuidle(0, smp_processor_id());
local_irq_enable(); local_irq_enable();
while (!need_resched()) while (!tif_need_resched())
cpu_relax(); cpu_relax();
trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
rcu_idle_exit(); rcu_idle_exit();
...@@ -92,8 +92,7 @@ static void cpu_idle_loop(void) ...@@ -92,8 +92,7 @@ static void cpu_idle_loop(void)
if (cpu_idle_force_poll || tick_check_broadcast_expired()) { if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
cpu_idle_poll(); cpu_idle_poll();
} else { } else {
current_clr_polling(); if (!current_clr_polling_and_test()) {
if (!need_resched()) {
stop_critical_timings(); stop_critical_timings();
rcu_idle_enter(); rcu_idle_enter();
arch_cpu_idle(); arch_cpu_idle();
...@@ -103,7 +102,7 @@ static void cpu_idle_loop(void) ...@@ -103,7 +102,7 @@ static void cpu_idle_loop(void)
} else { } else {
local_irq_enable(); local_irq_enable();
} }
current_set_polling(); __current_set_polling();
} }
arch_cpu_idle_exit(); arch_cpu_idle_exit();
} }
...@@ -129,7 +128,7 @@ void cpu_startup_entry(enum cpuhp_state state) ...@@ -129,7 +128,7 @@ void cpu_startup_entry(enum cpuhp_state state)
*/ */
boot_init_stack_canary(); boot_init_stack_canary();
#endif #endif
current_set_polling(); __current_set_polling();
arch_cpu_idle_prepare(); arch_cpu_idle_prepare();
cpu_idle_loop(); cpu_idle_loop();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment