Skip to content

Commit fc97fc2

Browse files
Peter Zijlstragregkh
authored andcommitted
sched, idle: Fix the idle polling state logic
commit ea81174 upstream. Mike reported that commit 7d1a941 ("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: Mike Galbraith <bitbucket@online.de> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Cc: lenb@kernel.org Cc: tglx@linutronix.de Link: http://lkml.kernel.org/n/tip-nc03imb0etuefmzybzj7sprf@git.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 19dbd3f commit fc97fc2

6 files changed

Lines changed: 91 additions & 52 deletions

File tree

‎arch/x86/kernel/process.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ static void amd_e400_idle(void)
391391
* The switch back from broadcast mode needs to be
392392
* called with interrupts disabled.
393393
*/
394-
local_irq_disable();
395-
clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
396-
local_irq_enable();
394+
local_irq_disable();
395+
clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
396+
local_irq_enable();
397397
} else
398398
default_idle();
399399
}

‎drivers/acpi/processor_idle.c‎

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,10 @@ static struct dmi_system_id processor_power_dmi_table[] = {
119119
*/
120120
static void acpi_safe_halt(void)
121121
{
122-
current_thread_info()->status &= ~TS_POLLING;
123-
/*
124-
* TS_POLLING-cleared state must be visible before we
125-
* test NEED_RESCHED:
126-
*/
127-
smp_mb();
128-
if (!need_resched()) {
122+
if (!tif_need_resched()) {
129123
safe_halt();
130124
local_irq_disable();
131125
}
132-
current_thread_info()->status |= TS_POLLING;
133126
}
134127

135128
#ifdef ARCH_APICTIMER_STOPS_ON_C3
@@ -737,6 +730,11 @@ static int acpi_idle_enter_c1(struct cpuidle_device *dev,
737730
if (unlikely(!pr))
738731
return -EINVAL;
739732

733+
if (cx->entry_method == ACPI_CSTATE_FFH) {
734+
if (current_set_polling_and_test())
735+
return -EINVAL;
736+
}
737+
740738
lapic_timer_state_broadcast(pr, cx, 1);
741739
acpi_idle_do_entry(cx);
742740

@@ -790,18 +788,9 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
790788
if (unlikely(!pr))
791789
return -EINVAL;
792790

793-
if (cx->entry_method != ACPI_CSTATE_FFH) {
794-
current_thread_info()->status &= ~TS_POLLING;
795-
/*
796-
* TS_POLLING-cleared state must be visible before we test
797-
* NEED_RESCHED:
798-
*/
799-
smp_mb();
800-
801-
if (unlikely(need_resched())) {
802-
current_thread_info()->status |= TS_POLLING;
791+
if (cx->entry_method == ACPI_CSTATE_FFH) {
792+
if (current_set_polling_and_test())
803793
return -EINVAL;
804-
}
805794
}
806795

807796
/*
@@ -819,9 +808,6 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
819808

820809
sched_clock_idle_wakeup_event(0);
821810

822-
if (cx->entry_method != ACPI_CSTATE_FFH)
823-
current_thread_info()->status |= TS_POLLING;
824-
825811
lapic_timer_state_broadcast(pr, cx, 0);
826812
return index;
827813
}
@@ -858,18 +844,9 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
858844
}
859845
}
860846

861-
if (cx->entry_method != ACPI_CSTATE_FFH) {
862-
current_thread_info()->status &= ~TS_POLLING;
863-
/*
864-
* TS_POLLING-cleared state must be visible before we test
865-
* NEED_RESCHED:
866-
*/
867-
smp_mb();
868-
869-
if (unlikely(need_resched())) {
870-
current_thread_info()->status |= TS_POLLING;
847+
if (cx->entry_method == ACPI_CSTATE_FFH) {
848+
if (current_set_polling_and_test())
871849
return -EINVAL;
872-
}
873850
}
874851

875852
acpi_unlazy_tlb(smp_processor_id());
@@ -915,9 +892,6 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
915892

916893
sched_clock_idle_wakeup_event(0);
917894

918-
if (cx->entry_method != ACPI_CSTATE_FFH)
919-
current_thread_info()->status |= TS_POLLING;
920-
921895
lapic_timer_state_broadcast(pr, cx, 0);
922896
return index;
923897
}

‎drivers/idle/intel_idle.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static int intel_idle(struct cpuidle_device *dev,
359359
if (!(lapic_timer_reliable_states & (1 << (cstate))))
360360
clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
361361

362-
if (!need_resched()) {
362+
if (!current_set_polling_and_test()) {
363363

364364
__monitor((void *)&current_thread_info()->flags, 0, 0);
365365
smp_mb();

‎include/linux/sched.h‎

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,34 +2474,98 @@ static inline int tsk_is_polling(struct task_struct *p)
24742474
{
24752475
return task_thread_info(p)->status & TS_POLLING;
24762476
}
2477-
static inline void current_set_polling(void)
2477+
static inline void __current_set_polling(void)
24782478
{
24792479
current_thread_info()->status |= TS_POLLING;
24802480
}
24812481

2482-
static inline void current_clr_polling(void)
2482+
static inline bool __must_check current_set_polling_and_test(void)
2483+
{
2484+
__current_set_polling();
2485+
2486+
/*
2487+
* Polling state must be visible before we test NEED_RESCHED,
2488+
* paired by resched_task()
2489+
*/
2490+
smp_mb();
2491+
2492+
return unlikely(tif_need_resched());
2493+
}
2494+
2495+
static inline void __current_clr_polling(void)
24832496
{
24842497
current_thread_info()->status &= ~TS_POLLING;
2485-
smp_mb__after_clear_bit();
2498+
}
2499+
2500+
static inline bool __must_check current_clr_polling_and_test(void)
2501+
{
2502+
__current_clr_polling();
2503+
2504+
/*
2505+
* Polling state must be visible before we test NEED_RESCHED,
2506+
* paired by resched_task()
2507+
*/
2508+
smp_mb();
2509+
2510+
return unlikely(tif_need_resched());
24862511
}
24872512
#elif defined(TIF_POLLING_NRFLAG)
24882513
static inline int tsk_is_polling(struct task_struct *p)
24892514
{
24902515
return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
24912516
}
2492-
static inline void current_set_polling(void)
2517+
2518+
static inline void __current_set_polling(void)
24932519
{
24942520
set_thread_flag(TIF_POLLING_NRFLAG);
24952521
}
24962522

2497-
static inline void current_clr_polling(void)
2523+
static inline bool __must_check current_set_polling_and_test(void)
2524+
{
2525+
__current_set_polling();
2526+
2527+
/*
2528+
* Polling state must be visible before we test NEED_RESCHED,
2529+
* paired by resched_task()
2530+
*
2531+
* XXX: assumes set/clear bit are identical barrier wise.
2532+
*/
2533+
smp_mb__after_clear_bit();
2534+
2535+
return unlikely(tif_need_resched());
2536+
}
2537+
2538+
static inline void __current_clr_polling(void)
24982539
{
24992540
clear_thread_flag(TIF_POLLING_NRFLAG);
25002541
}
2542+
2543+
static inline bool __must_check current_clr_polling_and_test(void)
2544+
{
2545+
__current_clr_polling();
2546+
2547+
/*
2548+
* Polling state must be visible before we test NEED_RESCHED,
2549+
* paired by resched_task()
2550+
*/
2551+
smp_mb__after_clear_bit();
2552+
2553+
return unlikely(tif_need_resched());
2554+
}
2555+
25012556
#else
25022557
static inline int tsk_is_polling(struct task_struct *p) { return 0; }
2503-
static inline void current_set_polling(void) { }
2504-
static inline void current_clr_polling(void) { }
2558+
static inline void __current_set_polling(void) { }
2559+
static inline void __current_clr_polling(void) { }
2560+
2561+
static inline bool __must_check current_set_polling_and_test(void)
2562+
{
2563+
return unlikely(tif_need_resched());
2564+
}
2565+
static inline bool __must_check current_clr_polling_and_test(void)
2566+
{
2567+
return unlikely(tif_need_resched());
2568+
}
25052569
#endif
25062570

25072571
/*

‎include/linux/thread_info.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
107107
#define set_need_resched() set_thread_flag(TIF_NEED_RESCHED)
108108
#define clear_need_resched() clear_thread_flag(TIF_NEED_RESCHED)
109109

110+
#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
111+
110112
#if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
111113
/*
112114
* An arch can define its own version of set_restore_sigmask() to get the

‎kernel/cpu/idle.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static inline int cpu_idle_poll(void)
4444
rcu_idle_enter();
4545
trace_cpu_idle_rcuidle(0, smp_processor_id());
4646
local_irq_enable();
47-
while (!need_resched())
47+
while (!tif_need_resched())
4848
cpu_relax();
4949
trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
5050
rcu_idle_exit();
@@ -92,8 +92,7 @@ static void cpu_idle_loop(void)
9292
if (cpu_idle_force_poll || tick_check_broadcast_expired()) {
9393
cpu_idle_poll();
9494
} else {
95-
current_clr_polling();
96-
if (!need_resched()) {
95+
if (!current_clr_polling_and_test()) {
9796
stop_critical_timings();
9897
rcu_idle_enter();
9998
arch_cpu_idle();
@@ -103,7 +102,7 @@ static void cpu_idle_loop(void)
103102
} else {
104103
local_irq_enable();
105104
}
106-
current_set_polling();
105+
__current_set_polling();
107106
}
108107
arch_cpu_idle_exit();
109108
}
@@ -129,7 +128,7 @@ void cpu_startup_entry(enum cpuhp_state state)
129128
*/
130129
boot_init_stack_canary();
131130
#endif
132-
current_set_polling();
131+
__current_set_polling();
133132
arch_cpu_idle_prepare();
134133
cpu_idle_loop();
135134
}

0 commit comments

Comments
 (0)