Skip to content

Commit

Permalink
[PATCH] sched: resched and cpu_idle rework
Browse files Browse the repository at this point in the history
Make some changes to the NEED_RESCHED and POLLING_NRFLAG to reduce
confusion, and make their semantics rigid.  Improves efficiency of
resched_task and some cpu_idle routines.

* In resched_task:
- TIF_NEED_RESCHED is only cleared with the task's runqueue lock held,
  and as we hold it during resched_task, then there is no need for an
  atomic test and set there. The only other time this should be set is
  when the task's quantum expires, in the timer interrupt - this is
  protected against because the rq lock is irq-safe.

- If TIF_NEED_RESCHED is set, then we don't need to do anything. It
  won't get unset until the task get's schedule()d off.

- If we are running on the same CPU as the task we resched, then set
  TIF_NEED_RESCHED and no further action is required.

- If we are running on another CPU, and TIF_POLLING_NRFLAG is *not* set
  after TIF_NEED_RESCHED has been set, then we need to send an IPI.

Using these rules, we are able to remove the test and set operation in
resched_task, and make clear the previously vague semantics of
POLLING_NRFLAG.

* In idle routines:
- Enter cpu_idle with preempt disabled. When the need_resched() condition
  becomes true, explicitly call schedule(). This makes things a bit clearer
  (IMO), but haven't updated all architectures yet.

- Many do a test and clear of TIF_NEED_RESCHED for some reason. According
  to the resched_task rules, this isn't needed (and actually breaks the
  assumption that TIF_NEED_RESCHED is only cleared with the runqueue lock
  held). So remove that. Generally one less locked memory op when switching
  to the idle thread.

- Many idle routines clear TIF_POLLING_NRFLAG, and only set it in the inner
  most polling idle loops. The above resched_task semantics allow it to be
  set until before the last time need_resched() is checked before going into
  a halt requiring interrupt wakeup.

  Many idle routines simply never enter such a halt, and so POLLING_NRFLAG
  can be always left set, completely eliminating resched IPIs when rescheduling
  the idle task.

  POLLING_NRFLAG width can be increased, to reduce the chance of resched IPIs.

Signed-off-by: Nick Piggin <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Con Kolivas <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Nick Piggin authored and Linus Torvalds committed Nov 9, 2005
1 parent 5bfb5d6 commit 64c7c8f
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 220 deletions.
89 changes: 89 additions & 0 deletions Documentation/sched-arch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
CPU Scheduler implementation hints for architecture specific code

Nick Piggin, 2005

Context switch
==============
1. Runqueue locking
By default, the switch_to arch function is called with the runqueue
locked. This is usually not a problem unless switch_to may need to
take the runqueue lock. This is usually due to a wake up operation in
the context switch. See include/asm-ia64/system.h for an example.

To request the scheduler call switch_to with the runqueue unlocked,
you must `#define __ARCH_WANT_UNLOCKED_CTXSW` in a header file
(typically the one where switch_to is defined).

Unlocked context switches introduce only a very minor performance
penalty to the core scheduler implementation in the CONFIG_SMP case.

2. Interrupt status
By default, the switch_to arch function is called with interrupts
disabled. Interrupts may be enabled over the call if it is likely to
introduce a significant interrupt latency by adding the line
`#define __ARCH_WANT_INTERRUPTS_ON_CTXSW` in the same place as for
unlocked context switches. This define also implies
`__ARCH_WANT_UNLOCKED_CTXSW`. See include/asm-arm/system.h for an
example.


CPU idle
========
Your cpu_idle routines need to obey the following rules:

1. Preempt should now disabled over idle routines. Should only
be enabled to call schedule() then disabled again.

2. need_resched/TIF_NEED_RESCHED is only ever set, and will never
be cleared until the running task has called schedule(). Idle
threads need only ever query need_resched, and may never set or
clear it.

3. When cpu_idle finds (need_resched() == 'true'), it should call
schedule(). It should not call schedule() otherwise.

4. The only time interrupts need to be disabled when checking
need_resched is if we are about to sleep the processor until
the next interrupt (this doesn't provide any protection of
need_resched, it prevents losing an interrupt).

4a. Common problem with this type of sleep appears to be:
local_irq_disable();
if (!need_resched()) {
local_irq_enable();
*** resched interrupt arrives here ***
__asm__("sleep until next interrupt");
}

5. TIF_POLLING_NRFLAG can be set by idle routines that do not
need an interrupt to wake them up when need_resched goes high.
In other words, they must be periodically polling need_resched,
although it may be reasonable to do some background work or enter
a low CPU priority.

5a. If TIF_POLLING_NRFLAG is set, and we do decide to enter
an interrupt sleep, it needs to be cleared then a memory
barrier issued (followed by a test of need_resched with
interrupts disabled, as explained in 3).

arch/i386/kernel/process.c has examples of both polling and
sleeping idle functions.


Possible arch/ problems
=======================

Possible arch problems I found (and either tried to fix or didn't):

h8300 - Is such sleeping racy vs interrupts? (See #4a).
The H8/300 manual I found indicates yes, however disabling IRQs
over the sleep mean only NMIs can wake it up, so can't fix easily
without doing spin waiting.

ia64 - is safe_halt call racy vs interrupts? (does it sleep?) (See #4a)

sh64 - Is sleeping racy vs interrupts? (See #4a)

sparc - IRQs on at this point(?), change local_irq_save to _disable.
- TODO: needs secondary CPUs to disable preempt (See #1)

10 changes: 3 additions & 7 deletions arch/alpha/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,17 @@
#include "proto.h"
#include "pci_impl.h"

void default_idle(void)
{
barrier();
}

void
cpu_idle(void)
{
set_thread_flag(TIF_POLLING_NRFLAG);

while (1) {
void (*idle)(void) = default_idle;
/* FIXME -- EV6 and LCA45 know how to power down
the CPU. */

while (!need_resched())
idle();
cpu_relax();
schedule();
}
}
Expand Down
14 changes: 9 additions & 5 deletions arch/arm/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ EXPORT_SYMBOL(pm_power_off);
*/
void default_idle(void)
{
local_irq_disable();
if (!need_resched() && !hlt_counter) {
timer_dyn_reprogram();
arch_idle();
if (hlt_counter)
cpu_relax();
else {
local_irq_disable();
if (!need_resched()) {
timer_dyn_reprogram();
arch_idle();
}
local_irq_enable();
}
local_irq_enable();
}

/*
Expand Down
20 changes: 19 additions & 1 deletion arch/i386/kernel/apm.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,26 @@ static int set_system_power_state(u_short state)
static int apm_do_idle(void)
{
u32 eax;
u8 ret = 0;
int idled = 0;
int polling;

polling = test_thread_flag(TIF_POLLING_NRFLAG);
if (polling) {
clear_thread_flag(TIF_POLLING_NRFLAG);
smp_mb__after_clear_bit();
}
if (!need_resched()) {
idled = 1;
ret = apm_bios_call_simple(APM_FUNC_IDLE, 0, 0, &eax);
}
if (polling)
set_thread_flag(TIF_POLLING_NRFLAG);

if (!idled)
return 0;

if (apm_bios_call_simple(APM_FUNC_IDLE, 0, 0, &eax)) {
if (ret) {
static unsigned long t;

/* This always fails on some SMP boards running UP kernels.
Expand Down
64 changes: 28 additions & 36 deletions arch/i386/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,22 @@ EXPORT_SYMBOL(enable_hlt);
*/
void default_idle(void)
{
local_irq_enable();

if (!hlt_counter && boot_cpu_data.hlt_works_ok) {
local_irq_disable();
if (!need_resched())
safe_halt();
else
local_irq_enable();
clear_thread_flag(TIF_POLLING_NRFLAG);
smp_mb__after_clear_bit();
while (!need_resched()) {
local_irq_disable();
if (!need_resched())
safe_halt();
else
local_irq_enable();
}
set_thread_flag(TIF_POLLING_NRFLAG);
} else {
cpu_relax();
while (!need_resched())
cpu_relax();
}
}
#ifdef CONFIG_APM_MODULE
Expand All @@ -120,29 +128,14 @@ EXPORT_SYMBOL(default_idle);
*/
static void poll_idle (void)
{
int oldval;

local_irq_enable();

/*
* Deal with another CPU just having chosen a thread to
* run here:
*/
oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);

if (!oldval) {
set_thread_flag(TIF_POLLING_NRFLAG);
asm volatile(
"2:"
"testl %0, %1;"
"rep; nop;"
"je 2b;"
: : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags));

clear_thread_flag(TIF_POLLING_NRFLAG);
} else {
set_need_resched();
}
asm volatile(
"2:"
"testl %0, %1;"
"rep; nop;"
"je 2b;"
: : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags));
}

#ifdef CONFIG_HOTPLUG_CPU
Expand Down Expand Up @@ -181,6 +174,8 @@ void cpu_idle(void)
{
int cpu = smp_processor_id();

set_thread_flag(TIF_POLLING_NRFLAG);

/* endless idle loop with no priority at all */
while (1) {
while (!need_resched()) {
Expand Down Expand Up @@ -246,15 +241,12 @@ static void mwait_idle(void)
{
local_irq_enable();

if (!need_resched()) {
set_thread_flag(TIF_POLLING_NRFLAG);
do {
__monitor((void *)&current_thread_info()->flags, 0, 0);
if (need_resched())
break;
__mwait(0, 0);
} while (!need_resched());
clear_thread_flag(TIF_POLLING_NRFLAG);
while (!need_resched()) {
__monitor((void *)&current_thread_info()->flags, 0, 0);
smp_mb();
if (need_resched())
break;
__mwait(0, 0);
}
}

Expand Down
32 changes: 17 additions & 15 deletions arch/ia64/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ void
default_idle (void)
{
local_irq_enable();
while (!need_resched())
if (can_do_pal_halt)
safe_halt();
else
while (!need_resched()) {
if (can_do_pal_halt) {
local_irq_disable();
if (!need_resched())
safe_halt();
local_irq_enable();
} else
cpu_relax();
}
}

#ifdef CONFIG_HOTPLUG_CPU
Expand Down Expand Up @@ -263,16 +267,16 @@ void __attribute__((noreturn))
cpu_idle (void)
{
void (*mark_idle)(int) = ia64_mark_idle;
int cpu = smp_processor_id();
set_thread_flag(TIF_POLLING_NRFLAG);

/* endless idle loop with no priority at all */
while (1) {
if (!need_resched()) {
void (*idle)(void);
#ifdef CONFIG_SMP
if (!need_resched())
min_xtp();
#endif
while (!need_resched()) {
void (*idle)(void);

if (__get_cpu_var(cpu_idle_state))
__get_cpu_var(cpu_idle_state) = 0;

Expand All @@ -284,19 +288,17 @@ cpu_idle (void)
if (!idle)
idle = default_idle;
(*idle)();
}

if (mark_idle)
(*mark_idle)(0);

if (mark_idle)
(*mark_idle)(0);
#ifdef CONFIG_SMP
normal_xtp();
normal_xtp();
#endif
}
preempt_enable_no_resched();
schedule();
preempt_disable();
check_pgt_cache();
if (cpu_is_offline(smp_processor_id()))
if (cpu_is_offline(cpu))
play_dead();
}
}
Expand Down
2 changes: 2 additions & 0 deletions arch/parisc/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ void default_idle(void)
*/
void cpu_idle(void)
{
set_thread_flag(TIF_POLLING_NRFLAG);

/* endless idle loop with no priority at all */
while (1) {
while (!need_resched())
Expand Down
10 changes: 2 additions & 8 deletions arch/powerpc/platforms/iseries/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,10 @@ static void iseries_shared_idle(void)
static void iseries_dedicated_idle(void)
{
long oldval;
set_thread_flag(TIF_POLLING_NRFLAG);

while (1) {
oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);

if (!oldval) {
set_thread_flag(TIF_POLLING_NRFLAG);

if (!need_resched()) {
while (!need_resched()) {
ppc64_runlatch_off();
HMT_low();
Expand All @@ -722,9 +719,6 @@ static void iseries_dedicated_idle(void)
}

HMT_medium();
clear_thread_flag(TIF_POLLING_NRFLAG);
} else {
set_need_resched();
}

ppc64_runlatch_on();
Expand Down
Loading

0 comments on commit 64c7c8f

Please sign in to comment.