improve SMP reschedule and idle routines
Subsystems (10)
PATCH 1/1 improve SMP reschedule and idle routines
OK, done a bit of work on all other architectures, and diffed to the latest -mm. Any chance you can put it in -mm, Andrew?
Also, while I was there, I thought I'd add the set_need_resched() thing to all the other architectures. I couldn't be bothered doing 2 patches, sorry.
So, this has been tested on i386, ia64, ppc64, sparc64, x86-64, however some of those (esp. ppc64) use different idle routines for different architectures. Needs testing.
David's ACK'ed the sparc64 changes, and Ingo's ACK'ed the rationale of the patch if not this exact one ;)
No other architectures even compile tested, but they've at least got something there now.
Quick audit of cpu_idle functions was interesting. I changed some, but they need review from maintainers, and some need more help. Is it worth sending this to linux-arch?
alpha - set TIF_POLLING_NRFLAG.
arm26 - how did it work before?! (fixed?)
h8300 - Is sleeping racy vs interrupts?
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.
m68knommu - "stop" if need_resched() is *SET*?!? (changed, is this right?)
- Is sleeping racy vs interrupts?
parisc - set TIF_POLLING_NRFLAG.
s390 - local irq disable before checking need_resched doesn't gain
anything (removed, OK?)
sh64 - Is sleeping racy vs interrupts?
sparc - IRQs need to be on here => changed local_irq_save to _disable.
- Changed idle loop so don't go to schedule() if pm_idle is NULL!
- set TIF_POLLING_NRFLAG for SMP.
- TODO: needs secondary CPUs to disable preempt
um - I'm too lazy to really look. Might be OK :P
xtensa - obviously not tested with preempt (hopefully fixed?)
Thanks,
Nick
Make some changes to the NEED_RESCHED and POLLING_NRFLAG to reduce confusion, and make their semantics rigid. Also have preempt explicitly disabled in idle routines. 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.
kernel/sched.c +14 -7
@@ -845,21 +845,28 @@ static void deactivate_task(struct task_#ifdef CONFIG_SMP static void resched_task (task_t *p) { -int need_resched, nrpolling; +int cpu;assert_spin_locked (&task_rq (p)->lock); -/* minimise the chance of sending an interrupt to poll_idle() */ - nrpolling =test_tsk_thread_flag (p,TIF_POLLING_NRFLAG ); - need_resched =test_and_set_tsk_thread_flag (p,TIF_NEED_RESCHED ); - nrpolling |=test_tsk_thread_flag (p,TIF_POLLING_NRFLAG ); +if (unlikely (test_tsk_thread_flag (p,TIF_NEED_RESCHED ))) +return ; + +set_tsk_thread_flag (p,TIF_NEED_RESCHED ); -if (!need_resched && !nrpolling && (task_cpu (p) !=smp_processor_id ())) -smp_send_reschedule (task_cpu (p)); + cpu =task_cpu (p); +if (cpu ==smp_processor_id ()) +return ; + +/* NEED_RESCHED must be visible before we test POLLING_NRFLAG */ +smp_mb (); +if (!test_tsk_thread_flag (p,TIF_POLLING_NRFLAG )) +smp_send_reschedule (cpu); }#else static inline void resched_task (task_t *p) { +assert_spin_locked (&task_rq (p)->lock);set_tsk_need_resched (p); }#endif
arch/i386/kernel/process.c +43 -54
132 lines not on this page · raw message
init/main.c +1 -1
@@ -382,7 +382,7 @@ static void noinline rest_init(void)kernel_thread (init,NULL ,CLONE_FS |CLONE_SIGHAND );numa_default_policy ();unlock_kernel (); -preempt_enable_no_resched (); +/* Don't re-enable preemption */ cpu_idle (); }
arch/i386/kernel/apm.c +13 -1
@@ -767,8 +767,20 @@ static int set_system_power_state(u_shorstatic int apm_do_idle (void ) {u32 eax; +u8 ret; +int idled =0 ; -if (apm_bios_call_simple (APM_FUNC_IDLE ,0 ,0 , &eax)) { +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); + } +set_thread_flag (TIF_POLLING_NRFLAG ); +if (!idled) +return 0 ; + +if (ret) {static unsigned long t;/* This always fails on some SMP boards running UP kernels.
drivers/acpi/processor_idle.c +18 -14
@@ -164,6 +164,14 @@ acpi_processor_power_activate (return ; } +static void acpi_safe_halt (void ) +{ +clear_thread_flag (TIF_POLLING_NRFLAG ); +smp_mb__after_clear_bit (); +while (!need_resched ()) +safe_halt (); +set_thread_flag (TIF_POLLING_NRFLAG ); +}static atomic_t c3_cpu_count;
@@ -176,7 +184,7 @@ static void acpi_processor_idle (void)int sleep_ticks =0 ;u32 t1, t2 =0 ; - pr = processors[_smp_processor_id ()]; + pr = processors[smp_processor_id ()];if (!pr)return ;
@@ -196,8 +204,13 @@ static void acpi_processor_idle (void) } cx = pr->power.state; -if (!cx) - goto easy_out; +if (!cx) { +if (pm_idle_save) +pm_idle_save (); +else +acpi_safe_halt (); +return ; + }/* * Check BM Activity
@@ -277,7 +290,8 @@ static void acpi_processor_idle (void)if (pm_idle_save)pm_idle_save ();else -safe_halt (); +acpi_safe_halt (); +/* * TBD: Can't get time duration while in C1, as resumes * go to an ISR rather than here. Need to instrument
@@ -407,16 +421,6 @@ end:*/ if (next_state != pr->power.state)acpi_processor_power_activate (pr, next_state); - -return ; - - easy_out: -/* do C1 instead of busy loop */ -if (pm_idle_save) -pm_idle_save (); -else -safe_halt (); -return ; }
arch/i386/kernel/smpboot.c +2 -0
@@ -477,6 +477,8 @@ set_cpu_sibling_map(int cpu)*/ static void __devinit start_secondary (void *unused) { +preempt_disable (); +/* * Dont put anything before smp_callin(), SMP * booting is too fragile that we want to limit the
arch/x86_64/kernel/process.c +38 -45
118 lines not on this page · raw message
arch/ppc64/kernel/idle.c +88 -96
272 lines not on this page · raw message
arch/ia64/kernel/process.c +20 -16
@@ -195,11 +195,16 @@ update_pal_halt_status(int status)void default_idle (void ) { -while (!need_resched ()) -if (can_do_pal_halt) +if (can_do_pal_halt) { +clear_thread_flag (TIF_POLLING_NRFLAG ); +smp_mb__after_clear_bit (); +while (!need_resched ())safe_halt (); -else +set_thread_flag (TIF_POLLING_NRFLAG ); + }else { +while (!need_resched ())cpu_relax (); + } }#ifdef CONFIG_HOTPLUG_CPU
@@ -261,18 +266,17 @@ void __attribute__((noreturn))cpu_idle (void) {void (*mark_idle)(int ) = ia64_mark_idle; - -set_tsk_need_resched (current); +int cpu =smp_processor_id (); +set_need_resched (); +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 ;
@@ -284,17 +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 (); } }
arch/ia64/kernel/smpboot.c +2 -0
@@ -393,6 +393,8 @@ smp_callin (void)int __devinit start_secondary (void *unused) { +preempt_disable (); +/* Early console may use I/O ports */ ia64_set_kr (IA64_KR_IO_BASE ,__pa (ia64_iobase));Dprintk ("start_secondary: starting CPU 0x%x\n" ,hard_smp_processor_id ());
arch/ppc64/kernel/smp.c +4 -1
@@ -561,7 +561,10 @@ int __devinit __cpu_up(unsigned int cpu)/* Activate a secondary processor. */ int __devinit start_secondary (void *unused) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + cpu =smp_processor_id ();atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/sparc64/kernel/smp.c +4 -12
@@ -144,6 +144,9 @@ void __init smp_callin(void)membar ("#LoadLoad" );cpu_set (cpuid, cpu_online_map); + +/* idle thread is expected to have preempt disabled */ +preempt_disable (); }void cpu_panic (void )
@@ -1167,20 +1170,9 @@ void __init smp_cpus_done(unsigned int m (bogosum/(5000/HZ))%100); } -/* This needn't do anything as we do not sleep the cpu -* inside of the idler task, so an interrupt is not needed -* to get a clean fast response. -* -* XXX Reverify this assumption... -DaveM -* -* Addendum: We do want it to do something for the signal -* delivery case, we detect that by just seeing -* if we are trying to send this to an idler or not. -*/ void smp_send_reschedule (int cpu) { -if (cpu_data (cpu).idle_volume ==0 ) -smp_receive_signal (cpu); +smp_receive_signal (cpu); }/* This is a nop because we capture all other cpus
arch/sparc64/kernel/process.c +18 -6
@@ -74,7 +74,9 @@ void cpu_idle(void)while (!need_resched ())barrier (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
@@ -83,21 +85,31 @@ void cpu_idle(void)/* * the idle loop on a UltraMultiPenguin... +* +* TIF_POLLING_NRFLAG is set because we do not sleep the cpu +* inside of the idler task, so an interrupt is not needed +* to get a clean fast response. +* +* XXX Reverify this assumption... -DaveM +* +* Addendum: We do want it to do something for the signal +* delivery case, we detect that by just seeing +* if we are trying to send this to an idler or not. */ -#define idle_me_harder () (cpu_data(smp_processor_id()).idle_volume += 1) -#define unidle_me () (cpu_data(smp_processor_id()).idle_volume = 0)void cpu_idle (void ) { +cpuinfo_sparc *cpuinfo = &local_cpu_data ();set_thread_flag (TIF_POLLING_NRFLAG ); +while (1 ) {if (need_resched ()) { -unidle_me (); -clear_thread_flag (TIF_POLLING_NRFLAG ); + cpuinfo->idle_volume =0 ; +preempt_enable_no_resched ();schedule (); -set_thread_flag (TIF_POLLING_NRFLAG ); +preempt_disable ();check_pgt_cache (); } -idle_me_harder (); + cpuinfo->idle_volume++;/* The store ordering is so that IRQ handlers on * other cpus see our increasing idleness for the buddy
arch/alpha/kernel/process.c +6 -7
@@ -43,22 +43,21 @@#include "proto.h" #include "pci_impl.h" -void default_idle (void ) -{ -barrier (); -} -void cpu_idle (void ) { +set_need_resched (); +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 (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/alpha/kernel/smp.c +5 -1
@@ -128,7 +128,11 @@ wait_boot_cpu_to_stop(int cpuid)void __init smp_callin (void ) { -int cpuid =hard_smp_processor_id (); +int cpuid; + +preempt_disable (); + + cpuid =hard_smp_processor_id ();if (cpu_test_and_set (cpuid, cpu_online_map)) {printk ("??, cpu 0x%x already present??\n" , cpuid);
arch/s390/kernel/smp.c +2 -0
@@ -528,6 +528,8 @@ extern void pfault_fini(void);int __devinit start_secondary (void *cpuvoid) { +preempt_disable (); +/* Setup the cpu */ cpu_init ();/* init per CPU timer */
arch/sparc/kernel/process.c +25 -11
@@ -72,6 +72,13 @@ struct thread_info *current_set[NR_CPUS]*/ void default_idle (void ) { +if (pm_idle) { +while (!need_resched ()) + (*pm_idle)(); + }else { +while (!need_resched ()) +cpu_relax (); + } }#ifndef CONFIG_SMP
@@ -83,6 +90,8 @@ void default_idle(void)*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ for (;;) {if (ARCH_SUN4C_SUN4 ) {
@@ -92,12 +101,11 @@ void cpu_idle(void)static unsigned long fps;unsigned long now;unsigned long faults; -unsigned long flags;extern unsigned long sun4c_kernel_faults;extern void sun4c_grow_kernel_ring (void ); -local_irq_save (flags); +local_irq_disable (); now = jiffies; count -= (now - last_jiffies); last_jiffies = now;
@@ -113,14 +121,14 @@ void cpu_idle(void)sun4c_grow_kernel_ring (); } } -local_irq_restore (flags); +local_irq_enable (); } -while ((!need_resched ()) && pm_idle) { - (*pm_idle)(); - } +default_idle (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
@@ -130,13 +138,19 @@ void cpu_idle(void)/* This is being executed in task 0 'user space'. */ void cpu_idle (void ) { + +set_need_resched (); +set_thread_flag (TIF_POLLING_NRFLAG ); +/* endless idle loop with no priority at all */ while (1 ) { -if (need_resched ()) { -schedule (); -check_pgt_cache (); - } -barrier ();/* or else gcc optimizes... */ +while (!need_resched ()) +cpu_relax (); + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); +check_pgt_cache (); } }
arch/ppc/kernel/idle.c +14 -7
@@ -50,8 +50,6 @@ void default_idle(void) }#endif } -if (need_resched ()) -schedule (); }/*
@@ -59,11 +57,20 @@ void default_idle(void)*/ void cpu_idle (void ) { -for (;;) -if (ppc_md.idle !=NULL ) - ppc_md.idle (); -else -default_idle (); +set_need_resched (); + +for (;;) { +while (need_resched ()) { +if (ppc_md.idle !=NULL ) + ppc_md.idle (); +else +default_idle (); + } + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }#if defined(CONFIG_SYSCTL ) && defined(CONFIG_6xx)
arch/m32r/kernel/process.c +4 -0
@@ -94,6 +94,8 @@ static void poll_idle (void)*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ()) {
@@ -104,7 +106,9 @@ void cpu_idle (void)idle (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/frv/kernel/process.c +7 -2
@@ -77,16 +77,21 @@ void (*idle)(void) = core_sleep_idle;*/ void cpu_idle (void ) { +int cpu =smp_processor_id (); +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ()) { - irq_stat[smp_processor_id ()].idle_timestamp = jiffies; + irq_stat[cpu].idle_timestamp = jiffies;if (!frv_dma_inprogress && idle)idle (); } - + +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/cris/kernel/process.c +4 -0
@@ -191,6 +191,8 @@ extern void default_idle(void);*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ()) {
@@ -201,7 +203,9 @@ void cpu_idle (void)idle (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/mips/kernel/smp.c +5 -1
@@ -83,7 +83,11 @@ extern ATTRIB_NORET void cpu_idle(void);*/ asmlinkage void start_secondary(void) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + + cpu =smp_processor_id ();cpu_probe ();cpu_report ();
arch/parisc/kernel/process.c +5 -0
@@ -88,11 +88,16 @@ void default_idle(void)*/ void cpu_idle (void ) { +set_need_resched (); +set_thread_flag (TIF_POLLING_NRFLAG ); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ())barrier (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
arch/ppc/kernel/smp.c +2 -0
@@ -326,6 +326,8 @@ int __devinit start_secondary(void *unus {int cpu; +preempt_disable (); +atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/sh/kernel/process.c +7 -9
@@ -51,28 +51,26 @@ void enable_hlt(void)EXPORT_SYMBOL (enable_hlt); -void default_idle (void ) +void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {if (hlt_counter) { -while (1 ) -if (need_resched ()) -break ; +while (!need_resched ()) +cpu_relax (); }else {while (!need_resched ())cpu_sleep (); } +preempt_disable_no_resched ();schedule (); +preempt_enable (); } } -void cpu_idle (void ) -{ -default_idle (); -} -void machine_restart (char * __unused) {/* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
arch/m68k/kernel/process.c +4 -0
@@ -98,11 +98,15 @@ void (*idle)(void) = default_idle;*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ())idle (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/mips/kernel/process.c +4 -0
@@ -53,12 +53,16 @@ void default_idle (void)*/ ATTRIB_NORET void cpu_idle(void) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ())if (cpu_wait) (*cpu_wait)(); +preempt_enable_no_resched ();schedule (); + preempt_disable() } }
arch/m68knommu/kernel/process.c +10 -6
@@ -45,11 +45,8 @@ asmlinkage void ret_from_fork(void);*/ void default_idle (void ) { -while (1 ) { -if (need_resched ()) - __asm__("stop #0x2000" : : :"cc" ); -schedule (); - } +while (!need_resched ()) + __asm__("stop #0x2000" : : :"cc" ); }void (*idle)(void ) = default_idle;
@@ -62,8 +59,15 @@ void (*idle)(void) = default_idle;*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ -idle (); +while (1 ) { +idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void machine_restart (char * __unused)
arch/sh/kernel/smp.c +5 -1
@@ -109,7 +109,11 @@ int __cpu_up(unsigned int cpu)int start_secondary (void *unused) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + + cpu =smp_processor_id ();atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/parisc/kernel/smp.c +2 -0
@@ -462,6 +462,8 @@ void __init smp_callin(void)void *istack;#endif +preempt_disable (); +smp_cpu_init (slave_id);#if 0 /* NOT WORKING YET - see entry.S */
arch/m32r/kernel/smpboot.c +1 -0
@@ -424,6 +424,7 @@ void __init smp_cpus_done(unsigned int m*==========================================================================*/ int __init start_secondary (void *unused) { +preempt_disable ();cpu_init ();smp_callin ();while (!cpu_isset (smp_processor_id (), smp_commenced_mask))
arch/s390/kernel/process.c +10 -8
@@ -101,11 +101,6 @@ void default_idle(void)int cpu, rc;local_irq_disable (); -if (need_resched ()) { -local_irq_enable (); -schedule (); -return ; - }/* CPU is going idle. */ cpu =smp_processor_id ();
@@ -121,7 +116,7 @@ void default_idle(void)__ctl_set_bit (8 ,15 );#ifdef CONFIG_HOTPLUG_CPU -if (cpu_is_offline (smp_processor_id ())) +if (cpu_is_offline (cpu))cpu_die ();#endif
@@ -161,8 +156,15 @@ void default_idle(void)void cpu_idle (void ) { -for (;;) -default_idle (); +set_need_resched (); + +for (;;) { +while (!need_resched ()) +default_idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void show_regs (struct pt_regs *regs)
arch/sh64/kernel/process.c +7 -11
@@ -307,23 +307,21 @@ __setup("hlt", hlt_setup);static inline void hlt (void ) { -if (hlt_counter) -return ; - __asm__ __volatile__ ("sleep" : : :"memory" ); }/* * The idle loop on a uniprocessor SH.. */ -void default_idle (void ) +void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ while (1 ) {if (hlt_counter) { -while (1 ) -if (need_resched ()) -break ; +while (!need_resched ()) +cpu_relax (); }else {local_irq_disable ();while (!need_resched ()) {
@@ -334,13 +332,11 @@ void default_idle(void) }local_irq_enable (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } -} -void cpu_idle (void ) -{ -default_idle (); }void machine_restart (char * __unused)
arch/arm26/kernel/process.c +7 -7
@@ -73,16 +73,16 @@ __setup("hlt", hlt_setup);*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ -preempt_disable ();while (1 ) { -while (!need_resched ()) { -local_irq_disable (); -if (!need_resched () && !hlt_counter) -local_irq_enable (); - } +while (!need_resched ()) +cpu_relax (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); } -schedule (); }static char reboot_mode ='h' ;
arch/arm/kernel/process.c +11 -6
@@ -84,10 +84,14 @@ EXPORT_SYMBOL(pm_power_off);*/ void default_idle (void ) { -local_irq_disable (); -if (!need_resched () && !hlt_counter) -arch_idle (); -local_irq_enable (); +if (hlt_counter) + cpu_relax() + else { +local_irq_disable (); +if (!need_resched ()) +arch_idle (); +local_irq_enable (); + } }/*
@@ -97,6 +101,7 @@ void default_idle(void)*/ void cpu_idle (void ) { +set_need_resched ();local_fiq_enable ();/* endless idle loop with no priority at all */
@@ -104,13 +109,13 @@ void cpu_idle(void)void (*idle)(void ) = pm_idle;if (!idle) idle = default_idle; -preempt_disable ();leds_event (led_idle_start);while (!need_resched ())idle ();leds_event (led_idle_end); -preempt_enable (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/h8300/kernel/process.c +14 -12
@@ -53,22 +53,17 @@ asmlinkage void ret_from_fork(void);#if !defined(CONFIG_H8300H_SIM ) && !defined(CONFIG_H8S_SIM )void default_idle (void ) { -while (1 ) { -if (need_resched ()) { -local_irq_enable (); - __asm__("sleep" ); -local_irq_disable (); - } -schedule (); +local_irq_disable (); +if (need_resched ()) { +local_irq_enable (); +/* XXX: race here! What if need_resched() gets set now? */ + __asm__("sleep" ); } }#else void default_idle (void ) { -while (1 ) { -if (need_resched ()) -schedule (); - } +cpu_relax (); }#endif void (*idle)(void ) = default_idle;
@@ -81,7 +76,14 @@ void (*idle)(void) = default_idle;*/ void cpu_idle (void ) { -idle (); +set_need_resched (); +while (1 ) { +while (!need_resched ()) +idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void machine_restart (char * __unused)
arch/xtensa/kernel/process.c +3 -1
@@ -91,13 +91,15 @@ coprocessor_info_t coprocessor_info[] =void cpu_idle (void ) {local_irq_enable (); +set_need_resched ();/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ())platform_idle (); -preempt_enable (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/v850/kernel/process.c +12 -6
@@ -36,11 +36,8 @@ extern void ret_from_fork (void);/* The idle loop. */ void default_idle (void ) { -while (1 ) { -while (!need_resched ()) - asm ("halt; nop; nop; nop; nop; nop" :::"cc" ); -schedule (); - } +while (!need_resched ()) + asm ("halt; nop; nop; nop; nop; nop" :::"cc" ); }void (*idle)(void ) = default_idle;
@@ -53,8 +50,17 @@ void (*idle)(void) = default_idle;*/ void cpu_idle (void ) { +set_need_resched (); +/* endless idle loop with no priority at all */ - (*idle) (); +while (1 ) { +while (!need_resched ()) + (*idle) (); + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }/*
Quoted 6 lines · Nick Piggin
OK, done a bit of work on all other architectures, and diffed to the latest -mm. Any chance you can put it in -mm, Andrew?
Also, while I was there, I thought I'd add the
set_need_resched()thing to all the other architectures. I couldn't be bothered doing 2 patches, sorry.
the need_resched changes are not needed meanwhile - we can do the first schedule() in rest_init() just fine. (See my earlier patch below.) So please keep the need_resched thing out of your patch.
----
The patch below should address this problem for all architectures, by doing an explicit schedule() in the init code before calling into cpu_idle(). It's a replacement for the following patch:
sched-remove-set_tsk_need_resched-from-init_idle.patch
Ingo
--
This patch tweaks idle thread setup semantics a bit: instead of setting NEED_RESCHED in init_idle(), we do an explicit schedule() before calling into cpu_idle().
This patch, while having no negative side-effects, enables wider use of cond_resched()s. (which might happen in the stock kernel too, but it's particulary important for voluntary-preempt)
kernel/sched.c renamed from kernel/sched.c.orig +8 -1
@@ -4163,6 +4163,14 @@ void show_state(void)read_unlock (&tasklist_lock); } +/** +* init_idle - set up an idle thread for a given CPU +* @idle: task in question +* @cpu: cpu the idle task belongs to +* +* NOTE: this function does not set the idle thread's NEED_RESCHED +* flag, to make booting more robust. +*/ void __devinit init_idle (task_t *idle,int cpu) {runqueue_t *rq =cpu_rq (cpu);
@@ -4180,7 +4188,6 @@ void __devinit init_idle(task_t *idle, i#if defined(CONFIG_SMP ) && defined(__ARCH_WANT_UNLOCKED_CTXSW) idle->oncpu =1 ;#endif -set_tsk_need_resched (idle);spin_unlock_irqrestore (&rq->lock, flags);/* Set the preempt count _outside_ the spinlocks! */
init/main.c renamed from init/main.c.orig +7 -0
@@ -383,6 +383,13 @@ static void noinline rest_init(void)numa_default_policy ();unlock_kernel ();preempt_enable_no_resched (); + +/* +* The boot idle thread must execute schedule() +* at least once to get things moving: +*/ +schedule (); +cpu_idle (); }
Quoted 11 lines · Ingo Molnar
Quoted 4 lines · Nick Piggin
OK, done a bit of work on all other architectures, and diffed to the latest -mm. Any chance you can put it in -mm, Andrew?
Also, while I was there, I thought I'd add the
set_need_resched()thing to all the other architectures. I couldn't be bothered doing 2 patches, sorry.the
need_reschedchanges are not needed meanwhile - we can do the firstschedule()inrest_init()just fine. (See my earlier patch below.) So please keep theneed_reschedthing out of your patch.
OK that's better. Sorry I didn't see your patch earlier.
I'll redo this patch. Coming up...
Send instant messages to your online friends http://au.messenger.yahoo.com
Ingo Molnar
Acked-by Nick PigginQuoted 8 lines · Nick Piggin
Quoted 4 lines
the
need_reschedchanges are not needed meanwhile - we can do the firstschedule()inrest_init()just fine. (See my earlier patch below.) So please keep theneed_reschedthing out of your patch.OK that's better. Sorry I didn't see your patch earlier.
I'll redo this patch. Coming up...
Andrew: please drop the following two patches:
sched-remove-set_tsk_need_resched-from-init_idle-v2.patch sched-remove-set_tsk_need_resched-from-init_idle-v2-ia64-fix.patch
and add the one below. The only followup patch (sched-voluntary-kernel-preemption.patch) should still apply cleanly. Nick's upcoming patch can then come afterwards.
----
This patch tweaks idle thread setup semantics a bit: instead of setting NEED_RESCHED in init_idle(), we do an explicit schedule() before calling into cpu_idle().
This patch, while having no negative side-effects, enables wider use of cond_resched()s. (which might happen in the stock kernel too, but it's particulary important for voluntary-preempt)
kernel/sched.c renamed from kernel/sched.c.orig +8 -1
@@ -4163,6 +4163,14 @@ void show_state(void)read_unlock (&tasklist_lock); } +/** +* init_idle - set up an idle thread for a given CPU +* @idle: task in question +* @cpu: cpu the idle task belongs to +* +* NOTE: this function does not set the idle thread's NEED_RESCHED +* flag, to make booting more robust. +*/ void __devinit init_idle (task_t *idle,int cpu) {runqueue_t *rq =cpu_rq (cpu);
@@ -4180,7 +4188,6 @@ void __devinit init_idle(task_t *idle, i#if defined(CONFIG_SMP ) && defined(__ARCH_WANT_UNLOCKED_CTXSW) idle->oncpu =1 ;#endif -set_tsk_need_resched (idle);spin_unlock_irqrestore (&rq->lock, flags);/* Set the preempt count _outside_ the spinlocks! */
init/main.c renamed from init/main.c.orig +7 -0
@@ -383,6 +383,13 @@ static void noinline rest_init(void)numa_default_policy ();unlock_kernel ();preempt_enable_no_resched (); + +/* +* The boot idle thread must execute schedule() +* at least once to get things moving: +*/ +schedule (); +cpu_idle (); }
Quoted 10 lines · Ingo Molnar
Quoted 4 lines · Nick Piggin
OK, done a bit of work on all other architectures, and diffed to the latest -mm. Any chance you can put it in -mm, Andrew?
Also, while I was there, I thought I'd add the
set_need_resched()thing to all the other architectures. I couldn't be bothered doing 2 patches, sorry.the
need_reschedchanges are not needed meanwhile - we can do the firstschedule()inrest_init()just fine. (See my earlier patch below.) So please keep theneed_reschedthing out of your patch.
The following patch is with your patch (and the ia64 fix) from -mm backed out, and the below patch applied.
Signature · 1 line
SUSE Labs, Novell Inc.
Make some changes to the NEED_RESCHED and POLLING_NRFLAG to reduce confusion, and make their semantics rigid. Also have preempt explicitly disabled in idle routines. 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.
kernel/sched.c +14 -7
@@ -845,21 +845,28 @@ static void deactivate_task(struct task_#ifdef CONFIG_SMP static void resched_task (task_t *p) { -int need_resched, nrpolling; +int cpu;assert_spin_locked (&task_rq (p)->lock); -/* minimise the chance of sending an interrupt to poll_idle() */ - nrpolling =test_tsk_thread_flag (p,TIF_POLLING_NRFLAG ); - need_resched =test_and_set_tsk_thread_flag (p,TIF_NEED_RESCHED ); - nrpolling |=test_tsk_thread_flag (p,TIF_POLLING_NRFLAG ); +if (unlikely (test_tsk_thread_flag (p,TIF_NEED_RESCHED ))) +return ; + +set_tsk_thread_flag (p,TIF_NEED_RESCHED ); -if (!need_resched && !nrpolling && (task_cpu (p) !=smp_processor_id ())) -smp_send_reschedule (task_cpu (p)); + cpu =task_cpu (p); +if (cpu ==smp_processor_id ()) +return ; + +/* NEED_RESCHED must be visible before we test POLLING_NRFLAG */ +smp_mb (); +if (!test_tsk_thread_flag (p,TIF_POLLING_NRFLAG )) +smp_send_reschedule (cpu); }#else static inline void resched_task (task_t *p) { +assert_spin_locked (&task_rq (p)->lock);set_tsk_need_resched (p); }#endif
arch/i386/kernel/process.c +42 -52
129 lines not on this page · raw message
init/main.c +2 -1
@@ -382,13 +382,14 @@ static void noinline rest_init(void)kernel_thread (init,NULL ,CLONE_FS |CLONE_SIGHAND );numa_default_policy ();unlock_kernel (); -preempt_enable_no_resched ();/* * The boot idle thread must execute schedule() * at least once to get things moving: */ +preempt_enable_no_resched ();schedule (); +preempt_disable ();cpu_idle (); }
arch/i386/kernel/apm.c +13 -1
@@ -767,8 +767,20 @@ static int set_system_power_state(u_shorstatic int apm_do_idle (void ) {u32 eax; +u8 ret; +int idled =0 ; -if (apm_bios_call_simple (APM_FUNC_IDLE ,0 ,0 , &eax)) { +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); + } +set_thread_flag (TIF_POLLING_NRFLAG ); +if (!idled) +return 0 ; + +if (ret) {static unsigned long t;/* This always fails on some SMP boards running UP kernels.
drivers/acpi/processor_idle.c +18 -14
@@ -164,6 +164,14 @@ acpi_processor_power_activate (return ; } +static void acpi_safe_halt (void ) +{ +clear_thread_flag (TIF_POLLING_NRFLAG ); +smp_mb__after_clear_bit (); +while (!need_resched ()) +safe_halt (); +set_thread_flag (TIF_POLLING_NRFLAG ); +}static atomic_t c3_cpu_count;
@@ -176,7 +184,7 @@ static void acpi_processor_idle (void)int sleep_ticks =0 ;u32 t1, t2 =0 ; - pr = processors[_smp_processor_id ()]; + pr = processors[smp_processor_id ()];if (!pr)return ;
@@ -196,8 +204,13 @@ static void acpi_processor_idle (void) } cx = pr->power.state; -if (!cx) - goto easy_out; +if (!cx) { +if (pm_idle_save) +pm_idle_save (); +else +acpi_safe_halt (); +return ; + }/* * Check BM Activity
@@ -277,7 +290,8 @@ static void acpi_processor_idle (void)if (pm_idle_save)pm_idle_save ();else -safe_halt (); +acpi_safe_halt (); +/* * TBD: Can't get time duration while in C1, as resumes * go to an ISR rather than here. Need to instrument
@@ -407,16 +421,6 @@ end:*/ if (next_state != pr->power.state)acpi_processor_power_activate (pr, next_state); - -return ; - - easy_out: -/* do C1 instead of busy loop */ -if (pm_idle_save) -pm_idle_save (); -else -safe_halt (); -return ; }
arch/i386/kernel/smpboot.c +2 -0
@@ -477,6 +477,8 @@ set_cpu_sibling_map(int cpu)*/ static void __devinit start_secondary (void *unused) { +preempt_disable (); +/* * Dont put anything before smp_callin(), SMP * booting is too fragile that we want to limit the
arch/x86_64/kernel/process.c +38 -44
116 lines not on this page · raw message
arch/ppc64/kernel/idle.c +88 -96
272 lines not on this page · raw message
arch/ia64/kernel/process.c +19 -14
@@ -195,11 +195,16 @@ update_pal_halt_status(int status)void default_idle (void ) { -while (!need_resched ()) -if (can_do_pal_halt) +if (can_do_pal_halt) { +clear_thread_flag (TIF_POLLING_NRFLAG ); +smp_mb__after_clear_bit (); +while (!need_resched ())safe_halt (); -else +set_thread_flag (TIF_POLLING_NRFLAG ); + }else { +while (!need_resched ())cpu_relax (); + } }#ifdef CONFIG_HOTPLUG_CPU
@@ -261,16 +266,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 ;
@@ -282,17 +287,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 (); } }
arch/ia64/kernel/smpboot.c +2 -0
@@ -393,6 +393,8 @@ smp_callin (void)int __devinit start_secondary (void *unused) { +preempt_disable (); +/* Early console may use I/O ports */ ia64_set_kr (IA64_KR_IO_BASE ,__pa (ia64_iobase));Dprintk ("start_secondary: starting CPU 0x%x\n" ,hard_smp_processor_id ());
arch/ppc64/kernel/smp.c +4 -1
@@ -561,7 +561,10 @@ int __devinit __cpu_up(unsigned int cpu)/* Activate a secondary processor. */ int __devinit start_secondary (void *unused) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + cpu =smp_processor_id ();atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/sparc64/kernel/smp.c +4 -12
@@ -144,6 +144,9 @@ void __init smp_callin(void)membar ("#LoadLoad" );cpu_set (cpuid, cpu_online_map); + +/* idle thread is expected to have preempt disabled */ +preempt_disable (); }void cpu_panic (void )
@@ -1167,20 +1170,9 @@ void __init smp_cpus_done(unsigned int m (bogosum/(5000/HZ))%100); } -/* This needn't do anything as we do not sleep the cpu -* inside of the idler task, so an interrupt is not needed -* to get a clean fast response. -* -* XXX Reverify this assumption... -DaveM -* -* Addendum: We do want it to do something for the signal -* delivery case, we detect that by just seeing -* if we are trying to send this to an idler or not. -*/ void smp_send_reschedule (int cpu) { -if (cpu_data (cpu).idle_volume ==0 ) -smp_receive_signal (cpu); +smp_receive_signal (cpu); }/* This is a nop because we capture all other cpus
arch/sparc64/kernel/process.c +18 -6
@@ -74,7 +74,9 @@ void cpu_idle(void)while (!need_resched ())barrier (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
@@ -83,21 +85,31 @@ void cpu_idle(void)/* * the idle loop on a UltraMultiPenguin... +* +* TIF_POLLING_NRFLAG is set because we do not sleep the cpu +* inside of the idler task, so an interrupt is not needed +* to get a clean fast response. +* +* XXX Reverify this assumption... -DaveM +* +* Addendum: We do want it to do something for the signal +* delivery case, we detect that by just seeing +* if we are trying to send this to an idler or not. */ -#define idle_me_harder () (cpu_data(smp_processor_id()).idle_volume += 1) -#define unidle_me () (cpu_data(smp_processor_id()).idle_volume = 0)void cpu_idle (void ) { +cpuinfo_sparc *cpuinfo = &local_cpu_data ();set_thread_flag (TIF_POLLING_NRFLAG ); +while (1 ) {if (need_resched ()) { -unidle_me (); -clear_thread_flag (TIF_POLLING_NRFLAG ); + cpuinfo->idle_volume =0 ; +preempt_enable_no_resched ();schedule (); -set_thread_flag (TIF_POLLING_NRFLAG ); +preempt_disable ();check_pgt_cache (); } -idle_me_harder (); + cpuinfo->idle_volume++;/* The store ordering is so that IRQ handlers on * other cpus see our increasing idleness for the buddy
arch/alpha/kernel/process.c +5 -7
@@ -43,22 +43,20 @@#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 (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/alpha/kernel/smp.c +5 -1
@@ -128,7 +128,11 @@ wait_boot_cpu_to_stop(int cpuid)void __init smp_callin (void ) { -int cpuid =hard_smp_processor_id (); +int cpuid; + +preempt_disable (); + + cpuid =hard_smp_processor_id ();if (cpu_test_and_set (cpuid, cpu_online_map)) {printk ("??, cpu 0x%x already present??\n" , cpuid);
arch/s390/kernel/smp.c +2 -0
@@ -528,6 +528,8 @@ extern void pfault_fini(void);int __devinit start_secondary (void *cpuvoid) { +preempt_disable (); +/* Setup the cpu */ cpu_init ();/* init per CPU timer */
arch/sparc/kernel/process.c +21 -11
@@ -72,6 +72,13 @@ struct thread_info *current_set[NR_CPUS]*/ void default_idle (void ) { +if (pm_idle) { +while (!need_resched ()) + (*pm_idle)(); + }else { +while (!need_resched ()) +cpu_relax (); + } }#ifndef CONFIG_SMP
@@ -92,12 +99,11 @@ void cpu_idle(void)static unsigned long fps;unsigned long now;unsigned long faults; -unsigned long flags;extern unsigned long sun4c_kernel_faults;extern void sun4c_grow_kernel_ring (void ); -local_irq_save (flags); +local_irq_disable (); now = jiffies; count -= (now - last_jiffies); last_jiffies = now;
@@ -113,14 +119,14 @@ void cpu_idle(void)sun4c_grow_kernel_ring (); } } -local_irq_restore (flags); +local_irq_enable (); } -while ((!need_resched ()) && pm_idle) { - (*pm_idle)(); - } +default_idle (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
@@ -130,13 +136,17 @@ void cpu_idle(void)/* This is being executed in task 0 'user space'. */ void cpu_idle (void ) { +set_thread_flag (TIF_POLLING_NRFLAG ); +/* endless idle loop with no priority at all */ while (1 ) { -if (need_resched ()) { -schedule (); -check_pgt_cache (); - } -barrier ();/* or else gcc optimizes... */ +while (!need_resched ()) +cpu_relax (); + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); +check_pgt_cache (); } }
arch/ppc/kernel/idle.c +12 -7
@@ -50,8 +50,6 @@ void default_idle(void) }#endif } -if (need_resched ()) -schedule (); }/*
@@ -59,11 +57,18 @@ void default_idle(void)*/ void cpu_idle (void ) { -for (;;) -if (ppc_md.idle !=NULL ) - ppc_md.idle (); -else -default_idle (); +for (;;) { +while (need_resched ()) { +if (ppc_md.idle !=NULL ) + ppc_md.idle (); +else +default_idle (); + } + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }#if defined(CONFIG_SYSCTL ) && defined(CONFIG_6xx)
arch/m32r/kernel/process.c +2 -0
@@ -104,7 +104,9 @@ void cpu_idle (void)idle (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/frv/kernel/process.c +6 -2
@@ -77,16 +77,20 @@ void (*idle)(void) = core_sleep_idle;*/ void cpu_idle (void ) { +int cpu =smp_processor_id (); +/* endless idle loop with no priority at all */ while (1 ) {while (!need_resched ()) { - irq_stat[smp_processor_id ()].idle_timestamp = jiffies; + irq_stat[cpu].idle_timestamp = jiffies;if (!frv_dma_inprogress && idle)idle (); } - + +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/cris/kernel/process.c +2 -0
@@ -201,7 +201,9 @@ void cpu_idle (void)idle (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/mips/kernel/smp.c +5 -1
@@ -83,7 +83,11 @@ extern ATTRIB_NORET void cpu_idle(void);*/ asmlinkage void start_secondary(void) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + + cpu =smp_processor_id ();cpu_probe ();cpu_report ();
arch/parisc/kernel/process.c +4 -0
@@ -88,11 +88,15 @@ 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 ())barrier (); +preempt_enable_no_resched ();schedule (); +preempt_disable ();check_pgt_cache (); } }
arch/ppc/kernel/smp.c +2 -0
@@ -326,6 +326,8 @@ int __devinit start_secondary(void *unus {int cpu; +preempt_disable (); +atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/sh/kernel/process.c +5 -9
@@ -51,28 +51,24 @@ void enable_hlt(void)EXPORT_SYMBOL (enable_hlt); -void default_idle (void ) +void cpu_idle (void ) {/* endless idle loop with no priority at all */ while (1 ) {if (hlt_counter) { -while (1 ) -if (need_resched ()) -break ; +while (!need_resched ()) +cpu_relax (); }else {while (!need_resched ())cpu_sleep (); } +preempt_disable_no_resched ();schedule (); +preempt_enable (); } } -void cpu_idle (void ) -{ -default_idle (); -} -void machine_restart (char * __unused) {/* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
arch/m68k/kernel/process.c +2 -0
@@ -102,7 +102,9 @@ void cpu_idle(void)while (1 ) {while (!need_resched ())idle (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/mips/kernel/process.c +2 -0
@@ -58,7 +58,9 @@ ATTRIB_NORET void cpu_idle(void)while (!need_resched ())if (cpu_wait) (*cpu_wait)(); +preempt_enable_no_resched ();schedule (); + preempt_disable() } }
arch/m68knommu/kernel/process.c +8 -6
@@ -45,11 +45,8 @@ asmlinkage void ret_from_fork(void);*/ void default_idle (void ) { -while (1 ) { -if (need_resched ()) - __asm__("stop #0x2000" : : :"cc" ); -schedule (); - } +while (!need_resched ()) + __asm__("stop #0x2000" : : :"cc" ); }void (*idle)(void ) = default_idle;
@@ -63,7 +60,12 @@ void (*idle)(void) = default_idle;void cpu_idle (void ) {/* endless idle loop with no priority at all */ -idle (); +while (1 ) { +idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void machine_restart (char * __unused)
arch/sh/kernel/smp.c +5 -1
@@ -109,7 +109,11 @@ int __cpu_up(unsigned int cpu)int start_secondary (void *unused) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu; + +preempt_disable (); + + cpu =smp_processor_id ();atomic_inc (&init_mm.mm_count); current->active_mm = &init_mm;
arch/parisc/kernel/smp.c +2 -0
@@ -462,6 +462,8 @@ void __init smp_callin(void)void *istack;#endif +preempt_disable (); +smp_cpu_init (slave_id);#if 0 /* NOT WORKING YET - see entry.S */
arch/m32r/kernel/smpboot.c +1 -0
@@ -424,6 +424,7 @@ void __init smp_cpus_done(unsigned int m*==========================================================================*/ int __init start_secondary (void *unused) { +preempt_disable ();cpu_init ();smp_callin ();while (!cpu_isset (smp_processor_id (), smp_commenced_mask))
arch/s390/kernel/process.c +8 -8
@@ -101,11 +101,6 @@ void default_idle(void)int cpu, rc;local_irq_disable (); -if (need_resched ()) { -local_irq_enable (); -schedule (); -return ; - }/* CPU is going idle. */ cpu =smp_processor_id ();
@@ -121,7 +116,7 @@ void default_idle(void)__ctl_set_bit (8 ,15 );#ifdef CONFIG_HOTPLUG_CPU -if (cpu_is_offline (smp_processor_id ())) +if (cpu_is_offline (cpu))cpu_die ();#endif
@@ -161,8 +156,13 @@ void default_idle(void)void cpu_idle (void ) { -for (;;) -default_idle (); +for (;;) { +while (!need_resched ()) +default_idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void show_regs (struct pt_regs *regs)
arch/sh64/kernel/process.c +5 -11
@@ -307,23 +307,19 @@ __setup("hlt", hlt_setup);static inline void hlt (void ) { -if (hlt_counter) -return ; - __asm__ __volatile__ ("sleep" : : :"memory" ); }/* * The idle loop on a uniprocessor SH.. */ -void default_idle (void ) +void cpu_idle (void ) {/* endless idle loop with no priority at all */ while (1 ) {if (hlt_counter) { -while (1 ) -if (need_resched ()) -break ; +while (!need_resched ()) +cpu_relax (); }else {local_irq_disable ();while (!need_resched ()) {
@@ -334,13 +330,11 @@ void default_idle(void) }local_irq_enable (); } +preempt_enable_no_resched ();schedule (); +preempt_disable (); } -} -void cpu_idle (void ) -{ -default_idle (); }void machine_restart (char * __unused)
arch/arm26/kernel/process.c +5 -7
@@ -74,15 +74,13 @@ __setup("hlt", hlt_setup);void cpu_idle (void ) {/* endless idle loop with no priority at all */ -preempt_disable ();while (1 ) { -while (!need_resched ()) { -local_irq_disable (); -if (!need_resched () && !hlt_counter) -local_irq_enable (); - } +while (!need_resched ()) +cpu_relax (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); } -schedule (); }static char reboot_mode ='h' ;
arch/arm/kernel/process.c +10 -6
@@ -84,10 +84,14 @@ EXPORT_SYMBOL(pm_power_off);*/ void default_idle (void ) { -local_irq_disable (); -if (!need_resched () && !hlt_counter) -arch_idle (); -local_irq_enable (); +if (hlt_counter) + cpu_relax() + else { +local_irq_disable (); +if (!need_resched ()) +arch_idle (); +local_irq_enable (); + } }/*
@@ -104,13 +108,13 @@ void cpu_idle(void)void (*idle)(void ) = pm_idle;if (!idle) idle = default_idle; -preempt_disable ();leds_event (led_idle_start);while (!need_resched ())idle ();leds_event (led_idle_end); -preempt_enable (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/h8300/kernel/process.c +13 -12
@@ -53,22 +53,17 @@ asmlinkage void ret_from_fork(void);#if !defined(CONFIG_H8300H_SIM ) && !defined(CONFIG_H8S_SIM )void default_idle (void ) { -while (1 ) { -if (need_resched ()) { -local_irq_enable (); - __asm__("sleep" ); -local_irq_disable (); - } -schedule (); +local_irq_disable (); +if (need_resched ()) { +local_irq_enable (); +/* XXX: race here! What if need_resched() gets set now? */ + __asm__("sleep" ); } }#else void default_idle (void ) { -while (1 ) { -if (need_resched ()) -schedule (); - } +cpu_relax (); }#endif void (*idle)(void ) = default_idle;
@@ -81,7 +76,13 @@ void (*idle)(void) = default_idle;*/ void cpu_idle (void ) { -idle (); +while (1 ) { +while (!need_resched ()) +idle (); +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }void machine_restart (char * __unused)
arch/xtensa/kernel/process.c +2 -1
@@ -96,8 +96,9 @@ void cpu_idle(void)while (1 ) {while (!need_resched ())platform_idle (); -preempt_enable (); +preempt_enable_no_resched ();schedule (); +preempt_disable (); } }
arch/v850/kernel/process.c +10 -6
@@ -36,11 +36,8 @@ extern void ret_from_fork (void);/* The idle loop. */ void default_idle (void ) { -while (1 ) { -while (!need_resched ()) - asm ("halt; nop; nop; nop; nop; nop" :::"cc" ); -schedule (); - } +while (!need_resched ()) + asm ("halt; nop; nop; nop; nop; nop" :::"cc" ); }void (*idle)(void ) = default_idle;
@@ -54,7 +51,14 @@ void (*idle)(void) = default_idle;void cpu_idle (void ) {/* endless idle loop with no priority at all */ - (*idle) (); +while (1 ) { +while (!need_resched ()) + (*idle) (); + +preempt_enable_no_resched (); +schedule (); +preempt_disable (); + } }/*
Ingo Molnar
Acked-byQuoted 4 lines · Nick Piggin
Make some changes to the
NEED_RESCHEDandPOLLING_NRFLAGto reduce confusion, and make their semantics rigid. Also have preempt explicitly disabled in idle routines. Improves efficiency ofresched_taskand somecpu_idleroutines.
Ingo
Quoted 5 lines · Nick Piggin
Make some changes to the
NEED_RESCHEDandPOLLING_NRFLAGto reduce confusion, and make their semantics rigid. Also have preempt explicitly disabled in idle routines. Improves efficiency ofresched_taskand somecpu_idleroutines.
This patch, with or without sched-resched-optimisation-fix.patch causes my x86_64 box to soil its pants.
I'll try to get -mm2 out the door - maybe there was some interaction with something else.
CPU: Trace cache: 12K uops, L1 D cache: 16K
CPU: L2 cache: 1024K
CPU: Physical Processor ID: 3
CPU1: Thermal monitoring enabled (TM1)
Intel(R) Xeon(TM) CPU 3.40GHz stepping 04
CPU 1: Syncing TSC to CPU 0.
Bo6tCng 2r sencor 2iz diTS6000 hspPf ff81a07ffiff 8 cyclesrsi mng Cr 923
tinofdelck p ing ad 2 ssaetediupr
/7PUipL6 cachs: 1ff4K100CPU: Ph8-11[1)ease U:teraee ca -e-------o
icIn tracizing CPU 0
.4PU:zTsaep iach04uoiigg enabled ciM1) rou e.. 680 . 1 oIntIP(R)lpjo1360) 29)
tC U:U hyLr Dn ache: 16K40)CPUCPU 2c cyn: n024KC
sCarte Thermal monitoring enabled (TM1)
Intel(R) Xeon(TM) CPU 3.40GHz stepping 04
APIC error on CPU3: 00(40)
CPU 3: Syncing TSC to CPU 0.
Kernel BUG at "kernel/sched.c":2805
invalid operand: 0000 [1] PREEMPT SMP
CPU 2
Modules linked in:
Pid: 0, comm: swapper Not tainted 2.6.12-rc5-mm2
RIP: 0010:[<ffffffff8012a97a>] <ffffffff8012a97a>{sub_preempt_count+22}
RSP: 0018:ffff81007ff7fef0 EFLAGS: 00010297
RAX: ffff81007ff7ffd8 RBX: ffffffff805d8180 RCX: 0000000000000000
RDX: 0000000000000000 RSI: 0000000000000246 RDI: 0000000000000001
RBP: ffff81007ff7fef0 R08: 00000000fffffff9 R09: 0000000000000002
R10: 00000000ffffffff R11: 0000000000000000 R12: 00000000000011d1
R13: ffff81007ff7ff18 R14: ffff81007ff7ff20 R15: 0000000000000001
FS: 0000000000000000(0000) GS:ffffffff805a3400(0000) knlGS:0000000000000000
CS: 0010 DS: 0018 ES: 0018 CR0: 000000008005003b
CR2: 0000000000000000 CR3: 0000000000101000 CR4: 00000000000006e0
Process swapper (pid: 0, threadinfo ffff81007ff7e000, task ffff81007ff740d0)
Stack: 0000000000000040 ffffffff8010beed ffffffffffffff67 ffffffff805b77c9
0000000000000246 0000000000000270 00000000000003af 0000000000000000
0000000000000000 0000000000000000
Call Trace:<ffffffff8010beed>{cpu_idle+94} <ffffffff805b77c9>{start_secondary+531}
Code: 0f 0b c4 7e 3d 80 ff ff ff ff f5 0a 81 ff fe 00 00 00 3e 77 RIP <ffffffff8012a97a>{sub_preempt_count+22} RSP <ffff81007ff7fef0>
<0>>ePnel payic -onoted nSC gi htCempte( tst iil 46 cyclesa ka
errPU 1: synlhs)
izeo tSo kuththrea0 ( ssartif -1
63)
Brought up 4 CPUs
Quoted 10 lines · Andrew Morton
Quoted 5 lines · Nick Piggin
Make some changes to the
NEED_RESCHEDandPOLLING_NRFLAGto reduce confusion, and make their semantics rigid. Also have preempt explicitly disabled in idle routines. Improves efficiency ofresched_taskand somecpu_idleroutines.This patch, with or without sched-resched-optimisation-fix.patch causes my
x86_64box to soil its pants.
Sorry about that. I probably have broken something since last testing x86-64. It looks like a simple mismatched preempt_ operation, so I'll try to get that fixed up shortly.
Signature · 5 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com