537 /* 538 * 'schedule()' is the scheduler function. It's a very simple and nice 539 * scheduler: it's not perfect, but certainly works for most things. 540 * 541 * The goto is "interesting". 542 * 543 * NOTE!! Task 0 is the 'idle' task, which gets called when no other 544 * tasks can run. It can not be killed, and it cannot sleep. The 'state' 545 * information in task[0] is never used. 546 */ 547 asmlinkage void schedule(void) 548 { 549 struct schedule_data * sched_data; 550 struct task_struct *prev, *next, *p; 551 struct list_head *tmp; 552 int this_cpu, c; 553 554 555 spin_lock_prefetch(&runqueue_lock); 556 557 BUG_ON(!current->active_mm); 558 need_resched_back: 559 prev = current; 560 this_cpu = prev->processor; 561 562 if (unlikely(in_interrupt())) { 563 printk("Scheduling in interrupt\n"); 564 BUG(); 565 } 566 567 release_kernel_lock(prev, this_cpu); 568 569 /* 570 * 'sched_data' is protected by the fact that we can run 571 * only one process per CPU. 572 */ 573 sched_data = & aligned_data[this_cpu].schedule_data; 574 575 spin_lock_irq(&runqueue_lock); 576 577 /* move an exhausted RR process to be last.. */ 578 if (unlikely(prev->policy == SCHED_RR)) 579 if (!prev->counter) { 580 prev->counter = NICE_TO_TICKS(prev->nice); 581 move_last_runqueue(prev); 582 } 583 584 switch (prev->state) { 585 case TASK_INTERRUPTIBLE: 586 if (signal_pending(prev)) { 587 prev->state = TASK_RUNNING; 588 break; 589 } 590 default: 591 del_from_runqueue(prev); 592 case TASK_RUNNING:; 593 } 594 prev->need_resched = 0; 595 596 /* 597 * this is the scheduler proper: 598 */ 599 600 repeat_schedule: 601 /* 602 * Default process to select.. 603 */ 604 next = idle_task(this_cpu); 605 c = -1000; 606 list_for_each(tmp, &runqueue_head) { 607 p = list_entry(tmp, struct task_struct, run_list); 608 if (can_schedule(p, this_cpu)) { 609 int weight = goodness(p, this_cpu, prev->active_mm); 610 if (weight > c) 611 c = weight, next = p; 612 } 613 } 614 615 /* Do we need to re-calculate counters? */ 616 if (unlikely(!c)) { 617 struct task_struct *p; 618 619 spin_unlock_irq(&runqueue_lock); 620 read_lock(&tasklist_lock); 621 for_each_task(p) 622 p->counter = (p->counter >> 1) + NICE_TO_TICKS(p->nice); 623 read_unlock(&tasklist_lock); 624 spin_lock_irq(&runqueue_lock); 625 goto repeat_schedule; 626 } 627 628 /* 629 * from this point on nothing can prevent us from 630 * switching to the next task, save this fact in 631 * sched_data. 632 */ 633 sched_data->curr = next; 634 task_set_cpu(next, this_cpu); 635 spin_unlock_irq(&runqueue_lock); 636 637 if (unlikely(prev == next)) { 638 /* We won't go through the normal tail, so do this by hand */ 639 prev->policy &= ~SCHED_YIELD; 640 goto same_process; 641 } 642 643 #ifdef CONFIG_SMP 644 /* 645 * maintain the per-process 'last schedule' value. 646 * (this has to be recalculated even if we reschedule to 647 * the same process) Currently this is only used on SMP, 648 * and it's approximate, so we do not have to maintain 649 * it while holding the runqueue spinlock. 650 */ 651 sched_data->last_schedule = get_cycles(); 652 653 /* 654 * We drop the scheduler lock early (it's a global spinlock), 655 * thus we have to lock the previous process from getting 656 * rescheduled during switch_to(). 657 */ 658 659 #endif /* CONFIG_SMP */ 660 661 kstat.context_swtch++; 662 /* 663 * there are 3 processes which are affected by a context switch: 664 * 665 * prev == .... ==> (last => next) 666 * 667 * It's the 'much more previous' 'prev' that is on next's stack, 668 * but prev is set to (the just run) 'last' process by switch_to(). 669 * This might sound slightly confusing but makes tons of sense. 670 */ 671 prepare_to_switch(); 672 { 673 struct mm_struct *mm = next->mm; 674 struct mm_struct *oldmm = prev->active_mm; 675 if (!mm) { 676 BUG_ON(next->active_mm); 677 next->active_mm = oldmm; 678 atomic_inc(&oldmm->mm_count); 679 enter_lazy_tlb(oldmm, next, this_cpu); 680 } else { 681 BUG_ON(next->active_mm != mm); 682 switch_mm(oldmm, mm, next, this_cpu); 683 } 684 685 if (!prev->mm) { 686 prev->active_mm = NULL; 687 mmdrop(oldmm); 688 } 689 } 690 691 /* 692 * This just switches the register state and the 693 * stack. 694 */ 695 switch_to(prev, next, prev); 696 __schedule_tail(prev); 697 698 same_process: 699 reacquire_kernel_lock(current); 700 if (current->need_resched) 701 goto need_resched_back; 702 return; 703 } 704