4 * Kernel scheduler and related syscalls
6 * Copyright (C) 1991-2002 Linus Torvalds
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
22 #include <linux/module.h>
23 #include <linux/nmi.h>
24 #include <linux/init.h>
25 #include <asm/uaccess.h>
26 #include <linux/highmem.h>
27 #include <linux/smp_lock.h>
28 #include <asm/mmu_context.h>
29 #include <linux/interrupt.h>
30 #include <linux/completion.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/security.h>
33 #include <linux/notifier.h>
34 #include <linux/profile.h>
35 #include <linux/suspend.h>
36 #include <linux/blkdev.h>
37 #include <linux/delay.h>
38 #include <linux/smp.h>
39 #include <linux/timer.h>
40 #include <linux/rcupdate.h>
41 #include <linux/cpu.h>
42 #include <linux/percpu.h>
43 #include <linux/kthread.h>
44 #include <linux/seq_file.h>
45 #include <linux/syscalls.h>
46 #include <linux/times.h>
49 #include <asm/unistd.h>
52 #define cpu_to_node_mask(cpu) node_to_cpumask(cpu_to_node(cpu))
54 #define cpu_to_node_mask(cpu) (cpu_online_map)
58 * Convert user-nice values [ -20 ... 0 ... 19 ]
59 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
62 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
63 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
64 #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
67 * 'User priority' is the nice value converted to something we
68 * can work with better when scaling various scheduler parameters,
69 * it's a [ 0 ... 39 ] range.
71 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
72 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
73 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
76 * Some helpers for converting nanosecond timing to jiffy resolution
78 #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
79 #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
82 * These are the 'tuning knobs' of the scheduler:
84 * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
85 * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
86 * Timeslices get refilled after they expire.
88 #define MIN_TIMESLICE max(5 * HZ / 1000, 1)
89 #define DEF_TIMESLICE (100 * HZ / 1000)
90 #define ON_RUNQUEUE_WEIGHT 30
91 #define CHILD_PENALTY 95
92 #define PARENT_PENALTY 100
94 #define PRIO_BONUS_RATIO 25
95 #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100)
96 #define INTERACTIVE_DELTA 2
97 #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS)
98 #define STARVATION_LIMIT (MAX_SLEEP_AVG)
99 #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG))
100 #define CREDIT_LIMIT 100
103 * If a task is 'interactive' then we reinsert it in the active
104 * array after it has expired its current timeslice. (it will not
105 * continue to run immediately, it will still roundrobin with
106 * other interactive tasks.)
108 * This part scales the interactivity limit depending on niceness.
110 * We scale it linearly, offset by the INTERACTIVE_DELTA delta.
111 * Here are a few examples of different nice levels:
113 * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0]
114 * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0]
115 * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0]
116 * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0]
117 * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0]
119 * (the X axis represents the possible -5 ... 0 ... +5 dynamic
120 * priority range a task can explore, a value of '1' means the
121 * task is rated interactive.)
123 * Ie. nice +19 tasks can never get 'interactive' enough to be
124 * reinserted into the active array. And only heavily CPU-hog nice -20
125 * tasks will be expired. Default nice 0 tasks are somewhere between,
126 * it takes some effort for them to get interactive, but it's not
130 #define CURRENT_BONUS(p) \
131 (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \
135 #define TIMESLICE_GRANULARITY(p) (MIN_TIMESLICE * \
136 (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \
139 #define TIMESLICE_GRANULARITY(p) (MIN_TIMESLICE * \
140 (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)))
143 #define SCALE(v1,v1_max,v2_max) \
144 (v1) * (v2_max) / (v1_max)
147 (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA)
149 #define TASK_INTERACTIVE(p) \
150 ((p)->prio <= (p)->static_prio - DELTA(p))
152 #define INTERACTIVE_SLEEP(p) \
153 (JIFFIES_TO_NS(MAX_SLEEP_AVG * \
154 (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1))
156 #define HIGH_CREDIT(p) \
157 ((p)->interactive_credit > CREDIT_LIMIT)
159 #define LOW_CREDIT(p) \
160 ((p)->interactive_credit < -CREDIT_LIMIT)
162 #define TASK_PREEMPTS_CURR(p, rq) \
163 ((p)->prio < (rq)->curr->prio)
166 * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
167 * to time slice values: [800ms ... 100ms ... 5ms]
169 * The higher a thread's priority, the bigger timeslices
170 * it gets during one round of execution. But even the lowest
171 * priority thread gets MIN_TIMESLICE worth of execution time.
174 #define SCALE_PRIO(x, prio) \
175 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE)
177 static unsigned int task_timeslice(task_t *p)
179 if (p->static_prio < NICE_TO_PRIO(0))
180 return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio);
182 return SCALE_PRIO(DEF_TIMESLICE, p->static_prio);
184 #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \
185 < (long long) (sd)->cache_hot_time)
188 * These are the runqueue data structures:
191 #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long))
193 typedef struct runqueue runqueue_t;
196 unsigned int nr_active;
197 unsigned long bitmap[BITMAP_SIZE];
198 struct list_head queue[MAX_PRIO];
202 * This is the main, per-CPU runqueue data structure.
204 * Locking rule: those places that want to lock multiple runqueues
205 * (such as the load balancing or the thread migration code), lock
206 * acquire operations must be ordered by ascending &runqueue.
212 * nr_running and cpu_load should be in the same cacheline because
213 * remote CPUs use both these fields when doing load calculation.
215 unsigned long nr_running;
217 unsigned long cpu_load;
219 unsigned long long nr_switches;
222 * This is part of a global counter where only the total sum
223 * over all CPUs matters. A task can increase this counter on
224 * one CPU and if it got migrated afterwards it may decrease
225 * it on another CPU. Always updated under the runqueue lock:
227 unsigned long nr_uninterruptible;
229 unsigned long expired_timestamp;
230 unsigned long long timestamp_last_tick;
232 struct mm_struct *prev_mm;
233 prio_array_t *active, *expired, arrays[2];
234 int best_expired_prio;
238 struct sched_domain *sd;
240 /* For active balancing */
244 task_t *migration_thread;
245 struct list_head migration_queue;
248 #ifdef CONFIG_SCHEDSTATS
250 struct sched_info rq_sched_info;
252 /* sys_sched_yield() stats */
253 unsigned long yld_exp_empty;
254 unsigned long yld_act_empty;
255 unsigned long yld_both_empty;
256 unsigned long yld_cnt;
258 /* schedule() stats */
259 unsigned long sched_noswitch;
260 unsigned long sched_switch;
261 unsigned long sched_cnt;
262 unsigned long sched_goidle;
264 /* pull_task() stats */
265 unsigned long pt_gained[MAX_IDLE_TYPES];
266 unsigned long pt_lost[MAX_IDLE_TYPES];
268 /* active_load_balance() stats */
269 unsigned long alb_cnt;
270 unsigned long alb_lost;
271 unsigned long alb_gained;
272 unsigned long alb_failed;
274 /* try_to_wake_up() stats */
275 unsigned long ttwu_cnt;
276 unsigned long ttwu_attempts;
277 unsigned long ttwu_moved;
279 /* wake_up_new_task() stats */
280 unsigned long wunt_cnt;
281 unsigned long wunt_moved;
283 /* sched_migrate_task() stats */
284 unsigned long smt_cnt;
286 /* sched_balance_exec() stats */
287 unsigned long sbe_cnt;
291 static DEFINE_PER_CPU(struct runqueue, runqueues);
293 #define for_each_domain(cpu, domain) \
294 for (domain = cpu_rq(cpu)->sd; domain; domain = domain->parent)
296 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
297 #define this_rq() (&__get_cpu_var(runqueues))
298 #define task_rq(p) cpu_rq(task_cpu(p))
299 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
302 * Default context-switch locking:
304 #ifndef prepare_arch_switch
305 # define prepare_arch_switch(rq, next) do { } while (0)
306 # define finish_arch_switch(rq, next) spin_unlock_irq(&(rq)->lock)
307 # define task_running(rq, p) ((rq)->curr == (p))
311 * task_rq_lock - lock the runqueue a given task resides on and disable
312 * interrupts. Note the ordering: we can safely lookup the task_rq without
313 * explicitly disabling preemption.
315 static runqueue_t *task_rq_lock(task_t *p, unsigned long *flags)
321 local_irq_save(*flags);
323 spin_lock(&rq->lock);
324 if (unlikely(rq != task_rq(p))) {
325 spin_unlock_irqrestore(&rq->lock, *flags);
326 goto repeat_lock_task;
331 static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags)
334 spin_unlock_irqrestore(&rq->lock, *flags);
337 #ifdef CONFIG_SCHEDSTATS
339 * bump this up when changing the output format or the meaning of an existing
340 * format, so that tools can adapt (or abort)
342 #define SCHEDSTAT_VERSION 10
344 static int show_schedstat(struct seq_file *seq, void *v)
347 enum idle_type itype;
349 seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
350 seq_printf(seq, "timestamp %lu\n", jiffies);
351 for_each_online_cpu(cpu) {
352 runqueue_t *rq = cpu_rq(cpu);
354 struct sched_domain *sd;
358 /* runqueue-specific stats */
360 "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
361 "%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
362 cpu, rq->yld_both_empty,
363 rq->yld_act_empty, rq->yld_exp_empty,
364 rq->yld_cnt, rq->sched_noswitch,
365 rq->sched_switch, rq->sched_cnt, rq->sched_goidle,
366 rq->alb_cnt, rq->alb_gained, rq->alb_lost,
368 rq->ttwu_cnt, rq->ttwu_moved, rq->ttwu_attempts,
369 rq->wunt_cnt, rq->wunt_moved,
370 rq->smt_cnt, rq->sbe_cnt, rq->rq_sched_info.cpu_time,
371 rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt);
373 for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES; itype++)
374 seq_printf(seq, " %lu %lu", rq->pt_gained[itype],
376 seq_printf(seq, "\n");
379 /* domain-specific stats */
380 for_each_domain(cpu, sd) {
381 char mask_str[NR_CPUS];
383 cpumask_scnprintf(mask_str, NR_CPUS, sd->span);
384 seq_printf(seq, "domain%d %s", dcnt++, mask_str);
385 for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES;
387 seq_printf(seq, " %lu %lu %lu %lu %lu",
389 sd->lb_failed[itype],
390 sd->lb_imbalance[itype],
391 sd->lb_nobusyq[itype],
392 sd->lb_nobusyg[itype]);
394 seq_printf(seq, " %lu %lu %lu %lu\n",
395 sd->sbe_pushed, sd->sbe_attempts,
396 sd->ttwu_wake_affine, sd->ttwu_wake_balance);
403 static int schedstat_open(struct inode *inode, struct file *file)
405 unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
406 char *buf = kmalloc(size, GFP_KERNEL);
412 res = single_open(file, show_schedstat, NULL);
414 m = file->private_data;
422 struct file_operations proc_schedstat_operations = {
423 .open = schedstat_open,
426 .release = single_release,
429 # define schedstat_inc(rq, field) rq->field++;
430 # define schedstat_add(rq, field, amt) rq->field += amt;
431 #else /* !CONFIG_SCHEDSTATS */
432 # define schedstat_inc(rq, field) do { } while (0);
433 # define schedstat_add(rq, field, amt) do { } while (0);
437 * rq_lock - lock a given runqueue and disable interrupts.
439 static runqueue_t *this_rq_lock(void)
446 spin_lock(&rq->lock);
451 static inline void rq_unlock(runqueue_t *rq)
454 spin_unlock_irq(&rq->lock);
457 #ifdef CONFIG_SCHEDSTATS
459 * Called when a process is dequeued from the active array and given
460 * the cpu. We should note that with the exception of interactive
461 * tasks, the expired queue will become the active queue after the active
462 * queue is empty, without explicitly dequeuing and requeuing tasks in the
463 * expired queue. (Interactive tasks may be requeued directly to the
464 * active queue, thus delaying tasks in the expired queue from running;
465 * see scheduler_tick()).
467 * This function is only called from sched_info_arrive(), rather than
468 * dequeue_task(). Even though a task may be queued and dequeued multiple
469 * times as it is shuffled about, we're really interested in knowing how
470 * long it was from the *first* time it was queued to the time that it
473 static inline void sched_info_dequeued(task_t *t)
475 t->sched_info.last_queued = 0;
479 * Called when a task finally hits the cpu. We can now calculate how
480 * long it was waiting to run. We also note when it began so that we
481 * can keep stats on how long its timeslice is.
483 static inline void sched_info_arrive(task_t *t)
485 unsigned long now = jiffies, diff = 0;
486 struct runqueue *rq = task_rq(t);
488 if (t->sched_info.last_queued)
489 diff = now - t->sched_info.last_queued;
490 sched_info_dequeued(t);
491 t->sched_info.run_delay += diff;
492 t->sched_info.last_arrival = now;
493 t->sched_info.pcnt++;
498 rq->rq_sched_info.run_delay += diff;
499 rq->rq_sched_info.pcnt++;
503 * Called when a process is queued into either the active or expired
504 * array. The time is noted and later used to determine how long we
505 * had to wait for us to reach the cpu. Since the expired queue will
506 * become the active queue after active queue is empty, without dequeuing
507 * and requeuing any tasks, we are interested in queuing to either. It
508 * is unusual but not impossible for tasks to be dequeued and immediately
509 * requeued in the same or another array: this can happen in sched_yield(),
510 * set_user_nice(), and even load_balance() as it moves tasks from runqueue
513 * This function is only called from enqueue_task(), but also only updates
514 * the timestamp if it is already not set. It's assumed that
515 * sched_info_dequeued() will clear that stamp when appropriate.
517 static inline void sched_info_queued(task_t *t)
519 if (!t->sched_info.last_queued)
520 t->sched_info.last_queued = jiffies;
524 * Called when a process ceases being the active-running process, either
525 * voluntarily or involuntarily. Now we can calculate how long we ran.
527 static inline void sched_info_depart(task_t *t)
529 struct runqueue *rq = task_rq(t);
530 unsigned long diff = jiffies - t->sched_info.last_arrival;
532 t->sched_info.cpu_time += diff;
535 rq->rq_sched_info.cpu_time += diff;
539 * Called when tasks are switched involuntarily due, typically, to expiring
540 * their time slice. (This may also be called when switching to or from
541 * the idle task.) We are only called when prev != next.
543 static inline void sched_info_switch(task_t *prev, task_t *next)
545 struct runqueue *rq = task_rq(prev);
548 * prev now departs the cpu. It's not interesting to record
549 * stats about how efficient we were at scheduling the idle
552 if (prev != rq->idle)
553 sched_info_depart(prev);
555 if (next != rq->idle)
556 sched_info_arrive(next);
559 #define sched_info_queued(t) do { } while (0)
560 #define sched_info_switch(t, next) do { } while (0)
561 #endif /* CONFIG_SCHEDSTATS */
564 * Adding/removing a task to/from a priority array:
566 static void dequeue_task(struct task_struct *p, prio_array_t *array)
569 list_del(&p->run_list);
570 if (list_empty(array->queue + p->prio))
571 __clear_bit(p->prio, array->bitmap);
574 static void enqueue_task(struct task_struct *p, prio_array_t *array)
576 sched_info_queued(p);
577 list_add_tail(&p->run_list, array->queue + p->prio);
578 __set_bit(p->prio, array->bitmap);
584 * Used by the migration code - we pull tasks from the head of the
585 * remote queue so we want these tasks to show up at the head of the
588 static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array)
590 list_add(&p->run_list, array->queue + p->prio);
591 __set_bit(p->prio, array->bitmap);
597 * effective_prio - return the priority that is based on the static
598 * priority but is modified by bonuses/penalties.
600 * We scale the actual sleep average [0 .... MAX_SLEEP_AVG]
601 * into the -5 ... 0 ... +5 bonus/penalty range.
603 * We use 25% of the full 0...39 priority range so that:
605 * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs.
606 * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks.
608 * Both properties are important to certain workloads.
610 static int effective_prio(task_t *p)
617 bonus = CURRENT_BONUS(p) - MAX_BONUS / 2;
619 prio = p->static_prio - bonus;
620 if (prio < MAX_RT_PRIO)
622 if (prio > MAX_PRIO-1)
628 * __activate_task - move a task to the runqueue.
630 static inline void __activate_task(task_t *p, runqueue_t *rq)
632 enqueue_task(p, rq->active);
637 * __activate_idle_task - move idle task to the _front_ of runqueue.
639 static inline void __activate_idle_task(task_t *p, runqueue_t *rq)
641 enqueue_task_head(p, rq->active);
645 static void recalc_task_prio(task_t *p, unsigned long long now)
647 unsigned long long __sleep_time = now - p->timestamp;
648 unsigned long sleep_time;
650 if (__sleep_time > NS_MAX_SLEEP_AVG)
651 sleep_time = NS_MAX_SLEEP_AVG;
653 sleep_time = (unsigned long)__sleep_time;
655 if (likely(sleep_time > 0)) {
657 * User tasks that sleep a long time are categorised as
658 * idle and will get just interactive status to stay active &
659 * prevent them suddenly becoming cpu hogs and starving
662 if (p->mm && p->activated != -1 &&
663 sleep_time > INTERACTIVE_SLEEP(p)) {
664 p->sleep_avg = JIFFIES_TO_NS(MAX_SLEEP_AVG -
667 p->interactive_credit++;
670 * The lower the sleep avg a task has the more
671 * rapidly it will rise with sleep time.
673 sleep_time *= (MAX_BONUS - CURRENT_BONUS(p)) ? : 1;
676 * Tasks with low interactive_credit are limited to
677 * one timeslice worth of sleep avg bonus.
680 sleep_time > JIFFIES_TO_NS(task_timeslice(p)))
681 sleep_time = JIFFIES_TO_NS(task_timeslice(p));
684 * Non high_credit tasks waking from uninterruptible
685 * sleep are limited in their sleep_avg rise as they
686 * are likely to be cpu hogs waiting on I/O
688 if (p->activated == -1 && !HIGH_CREDIT(p) && p->mm) {
689 if (p->sleep_avg >= INTERACTIVE_SLEEP(p))
691 else if (p->sleep_avg + sleep_time >=
692 INTERACTIVE_SLEEP(p)) {
693 p->sleep_avg = INTERACTIVE_SLEEP(p);
699 * This code gives a bonus to interactive tasks.
701 * The boost works by updating the 'average sleep time'
702 * value here, based on ->timestamp. The more time a
703 * task spends sleeping, the higher the average gets -
704 * and the higher the priority boost gets as well.
706 p->sleep_avg += sleep_time;
708 if (p->sleep_avg > NS_MAX_SLEEP_AVG) {
709 p->sleep_avg = NS_MAX_SLEEP_AVG;
711 p->interactive_credit++;
716 p->prio = effective_prio(p);
720 * activate_task - move a task to the runqueue and do priority recalculation
722 * Update all the scheduling statistics stuff. (sleep average
723 * calculation, priority modifiers, etc.)
725 static void activate_task(task_t *p, runqueue_t *rq, int local)
727 unsigned long long now;
732 /* Compensate for drifting sched_clock */
733 runqueue_t *this_rq = this_rq();
734 now = (now - this_rq->timestamp_last_tick)
735 + rq->timestamp_last_tick;
739 recalc_task_prio(p, now);
742 * This checks to make sure it's not an uninterruptible task
743 * that is now waking up.
747 * Tasks which were woken up by interrupts (ie. hw events)
748 * are most likely of interactive nature. So we give them
749 * the credit of extending their sleep time to the period
750 * of time they spend on the runqueue, waiting for execution
751 * on a CPU, first time around:
757 * Normal first-time wakeups get a credit too for
758 * on-runqueue time, but it will be weighted down:
765 __activate_task(p, rq);
769 * deactivate_task - remove a task from the runqueue.
771 static void deactivate_task(struct task_struct *p, runqueue_t *rq)
774 dequeue_task(p, p->array);
779 * resched_task - mark a task 'to be rescheduled now'.
781 * On UP this means the setting of the need_resched flag, on SMP it
782 * might also involve a cross-CPU call to trigger the scheduler on
786 static void resched_task(task_t *p)
788 int need_resched, nrpolling;
790 BUG_ON(!spin_is_locked(&task_rq(p)->lock));
792 /* minimise the chance of sending an interrupt to poll_idle() */
793 nrpolling = test_tsk_thread_flag(p,TIF_POLLING_NRFLAG);
794 need_resched = test_and_set_tsk_thread_flag(p,TIF_NEED_RESCHED);
795 nrpolling |= test_tsk_thread_flag(p,TIF_POLLING_NRFLAG);
797 if (!need_resched && !nrpolling && (task_cpu(p) != smp_processor_id()))
798 smp_send_reschedule(task_cpu(p));
801 static inline void resched_task(task_t *p)
803 set_tsk_need_resched(p);
808 * task_curr - is this task currently executing on a CPU?
809 * @p: the task in question.
811 inline int task_curr(const task_t *p)
813 return cpu_curr(task_cpu(p)) == p;
823 struct list_head list;
824 enum request_type type;
826 /* For REQ_MOVE_TASK */
830 /* For REQ_SET_DOMAIN */
831 struct sched_domain *sd;
833 struct completion done;
837 * The task's runqueue lock must be held.
838 * Returns true if you have to wait for migration thread.
840 static int migrate_task(task_t *p, int dest_cpu, migration_req_t *req)
842 runqueue_t *rq = task_rq(p);
845 * If the task is not on a runqueue (and not running), then
846 * it is sufficient to simply update the task's cpu field.
848 if (!p->array && !task_running(rq, p)) {
849 set_task_cpu(p, dest_cpu);
853 init_completion(&req->done);
854 req->type = REQ_MOVE_TASK;
856 req->dest_cpu = dest_cpu;
857 list_add(&req->list, &rq->migration_queue);
862 * wait_task_inactive - wait for a thread to unschedule.
864 * The caller must ensure that the task *will* unschedule sometime soon,
865 * else this function might spin for a *long* time. This function can't
866 * be called with interrupts off, or it may introduce deadlock with
867 * smp_call_function() if an IPI is sent by the same process we are
868 * waiting to become inactive.
870 void wait_task_inactive(task_t * p)
877 rq = task_rq_lock(p, &flags);
878 /* Must be off runqueue entirely, not preempted. */
879 if (unlikely(p->array)) {
880 /* If it's preempted, we yield. It could be a while. */
881 preempted = !task_running(rq, p);
882 task_rq_unlock(rq, &flags);
888 task_rq_unlock(rq, &flags);
892 * kick_process - kick a running thread to enter/exit the kernel
893 * @p: the to-be-kicked thread
895 * Cause a process which is running on another CPU to enter
896 * kernel-mode, without any delay. (to get signals handled.)
898 void kick_process(task_t *p)
904 if ((cpu != smp_processor_id()) && task_curr(p))
905 smp_send_reschedule(cpu);
910 * Return a low guess at the load of a migration-source cpu.
912 * We want to under-estimate the load of migration sources, to
913 * balance conservatively.
915 static inline unsigned long source_load(int cpu)
917 runqueue_t *rq = cpu_rq(cpu);
918 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE;
920 return min(rq->cpu_load, load_now);
924 * Return a high guess at the load of a migration-target cpu
926 static inline unsigned long target_load(int cpu)
928 runqueue_t *rq = cpu_rq(cpu);
929 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE;
931 return max(rq->cpu_load, load_now);
937 * wake_idle() is useful especially on SMT architectures to wake a
938 * task onto an idle sibling if we would otherwise wake it onto a
941 * Returns the CPU we should wake onto.
943 #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
944 static int wake_idle(int cpu, task_t *p)
947 runqueue_t *rq = cpu_rq(cpu);
948 struct sched_domain *sd;
955 if (!(sd->flags & SD_WAKE_IDLE))
958 cpus_and(tmp, sd->span, p->cpus_allowed);
960 for_each_cpu_mask(i, tmp) {
968 static inline int wake_idle(int cpu, task_t *p)
975 * try_to_wake_up - wake up a thread
976 * @p: the to-be-woken-up thread
977 * @state: the mask of task states that can be woken
978 * @sync: do a synchronous wakeup?
980 * Put it on the run-queue if it's not already there. The "current"
981 * thread is always on the run-queue (except when the actual
982 * re-schedule is in progress), and as such you're allowed to do
983 * the simpler "current->state = TASK_RUNNING" to mark yourself
984 * runnable without the overhead of this.
986 * returns failure only if the task is already active.
988 static int try_to_wake_up(task_t * p, unsigned int state, int sync)
990 int cpu, this_cpu, success = 0;
995 unsigned long load, this_load;
996 struct sched_domain *sd;
1000 rq = task_rq_lock(p, &flags);
1001 schedstat_inc(rq, ttwu_cnt);
1002 old_state = p->state;
1003 if (!(old_state & state))
1010 this_cpu = smp_processor_id();
1013 if (unlikely(task_running(rq, p)))
1018 if (cpu == this_cpu || unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
1021 load = source_load(cpu);
1022 this_load = target_load(this_cpu);
1025 * If sync wakeup then subtract the (maximum possible) effect of
1026 * the currently running task from the load of the current CPU:
1029 this_load -= SCHED_LOAD_SCALE;
1031 /* Don't pull the task off an idle CPU to a busy one */
1032 if (load < SCHED_LOAD_SCALE/2 && this_load > SCHED_LOAD_SCALE/2)
1035 new_cpu = this_cpu; /* Wake to this CPU if we can */
1038 * Scan domains for affine wakeup and passive balancing
1041 for_each_domain(this_cpu, sd) {
1042 unsigned int imbalance;
1044 * Start passive balancing when half the imbalance_pct
1047 imbalance = sd->imbalance_pct + (sd->imbalance_pct - 100) / 2;
1049 if ((sd->flags & SD_WAKE_AFFINE) &&
1050 !task_hot(p, rq->timestamp_last_tick, sd)) {
1052 * This domain has SD_WAKE_AFFINE and p is cache cold
1055 if (cpu_isset(cpu, sd->span)) {
1056 schedstat_inc(sd, ttwu_wake_affine);
1059 } else if ((sd->flags & SD_WAKE_BALANCE) &&
1060 imbalance*this_load <= 100*load) {
1062 * This domain has SD_WAKE_BALANCE and there is
1065 if (cpu_isset(cpu, sd->span)) {
1066 schedstat_inc(sd, ttwu_wake_balance);
1072 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1074 schedstat_inc(rq, ttwu_attempts);
1075 new_cpu = wake_idle(new_cpu, p);
1076 if (new_cpu != cpu && cpu_isset(new_cpu, p->cpus_allowed)) {
1077 schedstat_inc(rq, ttwu_moved);
1078 set_task_cpu(p, new_cpu);
1079 task_rq_unlock(rq, &flags);
1080 /* might preempt at this point */
1081 rq = task_rq_lock(p, &flags);
1082 old_state = p->state;
1083 if (!(old_state & state))
1088 this_cpu = smp_processor_id();
1093 #endif /* CONFIG_SMP */
1094 if (old_state == TASK_UNINTERRUPTIBLE) {
1095 rq->nr_uninterruptible--;
1097 * Tasks on involuntary sleep don't earn
1098 * sleep_avg beyond just interactive state.
1104 * Sync wakeups (i.e. those types of wakeups where the waker
1105 * has indicated that it will leave the CPU in short order)
1106 * don't trigger a preemption, if the woken up task will run on
1107 * this cpu. (in this case the 'I will reschedule' promise of
1108 * the waker guarantees that the freshly woken up task is going
1109 * to be considered on this CPU.)
1111 activate_task(p, rq, cpu == this_cpu);
1112 if (!sync || cpu != this_cpu) {
1113 if (TASK_PREEMPTS_CURR(p, rq))
1114 resched_task(rq->curr);
1119 p->state = TASK_RUNNING;
1121 task_rq_unlock(rq, &flags);
1126 int fastcall wake_up_process(task_t * p)
1128 return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
1129 TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
1132 EXPORT_SYMBOL(wake_up_process);
1134 int fastcall wake_up_state(task_t *p, unsigned int state)
1136 return try_to_wake_up(p, state, 0);
1140 static int find_idlest_cpu(struct task_struct *p, int this_cpu,
1141 struct sched_domain *sd);
1145 * Perform scheduler related setup for a newly forked process p.
1146 * p is forked by current.
1148 void fastcall sched_fork(task_t *p)
1151 * We mark the process as running here, but have not actually
1152 * inserted it onto the runqueue yet. This guarantees that
1153 * nobody will actually run it, and a signal or other external
1154 * event cannot wake it up and insert it on the runqueue either.
1156 p->state = TASK_RUNNING;
1157 INIT_LIST_HEAD(&p->run_list);
1159 spin_lock_init(&p->switch_lock);
1160 #ifdef CONFIG_SCHEDSTATS
1161 memset(&p->sched_info, 0, sizeof(p->sched_info));
1163 #ifdef CONFIG_PREEMPT
1165 * During context-switch we hold precisely one spinlock, which
1166 * schedule_tail drops. (in the common case it's this_rq()->lock,
1167 * but it also can be p->switch_lock.) So we compensate with a count
1168 * of 1. Also, we want to start with kernel preemption disabled.
1170 p->thread_info->preempt_count = 1;
1173 * Share the timeslice between parent and child, thus the
1174 * total amount of pending timeslices in the system doesn't change,
1175 * resulting in more scheduling fairness.
1177 local_irq_disable();
1178 p->time_slice = (current->time_slice + 1) >> 1;
1180 * The remainder of the first timeslice might be recovered by
1181 * the parent if the child exits early enough.
1183 p->first_time_slice = 1;
1184 current->time_slice >>= 1;
1185 p->timestamp = sched_clock();
1186 if (unlikely(!current->time_slice)) {
1188 * This case is rare, it happens when the parent has only
1189 * a single jiffy left from its timeslice. Taking the
1190 * runqueue lock is not a problem.
1192 current->time_slice = 1;
1194 scheduler_tick(0, 0);
1202 * wake_up_new_task - wake up a newly created task for the first time.
1204 * This function will do some initial scheduler statistics housekeeping
1205 * that must be done for every newly created context, then puts the task
1206 * on the runqueue and wakes it.
1208 void fastcall wake_up_new_task(task_t * p, unsigned long clone_flags)
1210 unsigned long flags;
1212 runqueue_t *rq, *this_rq;
1214 rq = task_rq_lock(p, &flags);
1216 this_cpu = smp_processor_id();
1218 BUG_ON(p->state != TASK_RUNNING);
1220 schedstat_inc(rq, wunt_cnt);
1222 * We decrease the sleep average of forking parents
1223 * and children as well, to keep max-interactive tasks
1224 * from forking tasks that are max-interactive. The parent
1225 * (current) is done further down, under its lock.
1227 p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) *
1228 CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
1230 p->interactive_credit = 0;
1232 p->prio = effective_prio(p);
1234 if (likely(cpu == this_cpu)) {
1235 if (!(clone_flags & CLONE_VM)) {
1237 * The VM isn't cloned, so we're in a good position to
1238 * do child-runs-first in anticipation of an exec. This
1239 * usually avoids a lot of COW overhead.
1241 if (unlikely(!current->array))
1242 __activate_task(p, rq);
1244 p->prio = current->prio;
1245 list_add_tail(&p->run_list, ¤t->run_list);
1246 p->array = current->array;
1247 p->array->nr_active++;
1252 /* Run child last */
1253 __activate_task(p, rq);
1255 * We skip the following code due to cpu == this_cpu
1257 * task_rq_unlock(rq, &flags);
1258 * this_rq = task_rq_lock(current, &flags);
1262 this_rq = cpu_rq(this_cpu);
1265 * Not the local CPU - must adjust timestamp. This should
1266 * get optimised away in the !CONFIG_SMP case.
1268 p->timestamp = (p->timestamp - this_rq->timestamp_last_tick)
1269 + rq->timestamp_last_tick;
1270 __activate_task(p, rq);
1271 if (TASK_PREEMPTS_CURR(p, rq))
1272 resched_task(rq->curr);
1274 schedstat_inc(rq, wunt_moved);
1276 * Parent and child are on different CPUs, now get the
1277 * parent runqueue to update the parent's ->sleep_avg:
1279 task_rq_unlock(rq, &flags);
1280 this_rq = task_rq_lock(current, &flags);
1282 current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) *
1283 PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
1284 task_rq_unlock(this_rq, &flags);
1288 * Potentially available exiting-child timeslices are
1289 * retrieved here - this way the parent does not get
1290 * penalized for creating too many threads.
1292 * (this cannot be used to 'generate' timeslices
1293 * artificially, because any timeslice recovered here
1294 * was given away by the parent in the first place.)
1296 void fastcall sched_exit(task_t * p)
1298 unsigned long flags;
1302 * If the child was a (relative-) CPU hog then decrease
1303 * the sleep_avg of the parent as well.
1305 rq = task_rq_lock(p->parent, &flags);
1306 if (p->first_time_slice) {
1307 p->parent->time_slice += p->time_slice;
1308 if (unlikely(p->parent->time_slice > task_timeslice(p)))
1309 p->parent->time_slice = task_timeslice(p);
1311 if (p->sleep_avg < p->parent->sleep_avg)
1312 p->parent->sleep_avg = p->parent->sleep_avg /
1313 (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg /
1315 task_rq_unlock(rq, &flags);
1319 * finish_task_switch - clean up after a task-switch
1320 * @prev: the thread we just switched away from.
1322 * We enter this with the runqueue still locked, and finish_arch_switch()
1323 * will unlock it along with doing any other architecture-specific cleanup
1326 * Note that we may have delayed dropping an mm in context_switch(). If
1327 * so, we finish that here outside of the runqueue lock. (Doing it
1328 * with the lock held can cause deadlocks; see schedule() for
1331 static void finish_task_switch(task_t *prev)
1332 __releases(rq->lock)
1334 runqueue_t *rq = this_rq();
1335 struct mm_struct *mm = rq->prev_mm;
1336 unsigned long prev_task_flags;
1341 * A task struct has one reference for the use as "current".
1342 * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and
1343 * calls schedule one last time. The schedule call will never return,
1344 * and the scheduled task must drop that reference.
1345 * The test for EXIT_ZOMBIE must occur while the runqueue locks are
1346 * still held, otherwise prev could be scheduled on another cpu, die
1347 * there before we look at prev->state, and then the reference would
1349 * Manfred Spraul <manfred@colorfullife.com>
1351 prev_task_flags = prev->flags;
1352 finish_arch_switch(rq, prev);
1355 if (unlikely(prev_task_flags & PF_DEAD))
1356 put_task_struct(prev);
1360 * schedule_tail - first thing a freshly forked thread must call.
1361 * @prev: the thread we just switched away from.
1363 asmlinkage void schedule_tail(task_t *prev)
1364 __releases(rq->lock)
1366 finish_task_switch(prev);
1368 if (current->set_child_tid)
1369 put_user(current->pid, current->set_child_tid);
1373 * context_switch - switch to the new MM and the new
1374 * thread's register state.
1377 task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next)
1379 struct mm_struct *mm = next->mm;
1380 struct mm_struct *oldmm = prev->active_mm;
1382 if (unlikely(!mm)) {
1383 next->active_mm = oldmm;
1384 atomic_inc(&oldmm->mm_count);
1385 enter_lazy_tlb(oldmm, next);
1387 switch_mm(oldmm, mm, next);
1389 if (unlikely(!prev->mm)) {
1390 prev->active_mm = NULL;
1391 WARN_ON(rq->prev_mm);
1392 rq->prev_mm = oldmm;
1395 /* Here we just switch the register state and the stack. */
1396 switch_to(prev, next, prev);
1402 * nr_running, nr_uninterruptible and nr_context_switches:
1404 * externally visible scheduler statistics: current number of runnable
1405 * threads, current number of uninterruptible-sleeping threads, total
1406 * number of context switches performed since bootup.
1408 unsigned long nr_running(void)
1410 unsigned long i, sum = 0;
1412 for_each_online_cpu(i)
1413 sum += cpu_rq(i)->nr_running;
1418 unsigned long nr_uninterruptible(void)
1420 unsigned long i, sum = 0;
1423 sum += cpu_rq(i)->nr_uninterruptible;
1426 * Since we read the counters lockless, it might be slightly
1427 * inaccurate. Do not allow it to go below zero though:
1429 if (unlikely((long)sum < 0))
1435 unsigned long long nr_context_switches(void)
1437 unsigned long long i, sum = 0;
1440 sum += cpu_rq(i)->nr_switches;
1445 unsigned long nr_iowait(void)
1447 unsigned long i, sum = 0;
1450 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1458 * double_rq_lock - safely lock two runqueues
1460 * Note this does not disable interrupts like task_rq_lock,
1461 * you need to do so manually before calling.
1463 static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2)
1464 __acquires(rq1->lock)
1465 __acquires(rq2->lock)
1468 spin_lock(&rq1->lock);
1469 __acquire(rq2->lock); /* Fake it out ;) */
1472 spin_lock(&rq1->lock);
1473 spin_lock(&rq2->lock);
1475 spin_lock(&rq2->lock);
1476 spin_lock(&rq1->lock);
1482 * double_rq_unlock - safely unlock two runqueues
1484 * Note this does not restore interrupts like task_rq_unlock,
1485 * you need to do so manually after calling.
1487 static void double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2)
1488 __releases(rq1->lock)
1489 __releases(rq2->lock)
1491 spin_unlock(&rq1->lock);
1493 spin_unlock(&rq2->lock);
1495 __release(rq2->lock);
1499 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1501 static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest)
1502 __releases(this_rq->lock)
1503 __acquires(busiest->lock)
1504 __acquires(this_rq->lock)
1506 if (unlikely(!spin_trylock(&busiest->lock))) {
1507 if (busiest < this_rq) {
1508 spin_unlock(&this_rq->lock);
1509 spin_lock(&busiest->lock);
1510 spin_lock(&this_rq->lock);
1512 spin_lock(&busiest->lock);
1517 * find_idlest_cpu - find the least busy runqueue.
1519 static int find_idlest_cpu(struct task_struct *p, int this_cpu,
1520 struct sched_domain *sd)
1522 unsigned long load, min_load, this_load;
1527 min_load = ULONG_MAX;
1529 cpus_and(mask, sd->span, p->cpus_allowed);
1531 for_each_cpu_mask(i, mask) {
1532 load = target_load(i);
1534 if (load < min_load) {
1538 /* break out early on an idle CPU: */
1544 /* add +1 to account for the new task */
1545 this_load = source_load(this_cpu) + SCHED_LOAD_SCALE;
1548 * Would with the addition of the new task to the
1549 * current CPU there be an imbalance between this
1550 * CPU and the idlest CPU?
1552 * Use half of the balancing threshold - new-context is
1553 * a good opportunity to balance.
1555 if (min_load*(100 + (sd->imbalance_pct-100)/2) < this_load*100)
1562 * If dest_cpu is allowed for this process, migrate the task to it.
1563 * This is accomplished by forcing the cpu_allowed mask to only
1564 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
1565 * the cpu_allowed mask is restored.
1567 static void sched_migrate_task(task_t *p, int dest_cpu)
1569 migration_req_t req;
1571 unsigned long flags;
1573 rq = task_rq_lock(p, &flags);
1574 if (!cpu_isset(dest_cpu, p->cpus_allowed)
1575 || unlikely(cpu_is_offline(dest_cpu)))
1578 schedstat_inc(rq, smt_cnt);
1579 /* force the process onto the specified CPU */
1580 if (migrate_task(p, dest_cpu, &req)) {
1581 /* Need to wait for migration thread (might exit: take ref). */
1582 struct task_struct *mt = rq->migration_thread;
1583 get_task_struct(mt);
1584 task_rq_unlock(rq, &flags);
1585 wake_up_process(mt);
1586 put_task_struct(mt);
1587 wait_for_completion(&req.done);
1591 task_rq_unlock(rq, &flags);
1595 * sched_exec(): find the highest-level, exec-balance-capable
1596 * domain and try to migrate the task to the least loaded CPU.
1598 * execve() is a valuable balancing opportunity, because at this point
1599 * the task has the smallest effective memory and cache footprint.
1601 void sched_exec(void)
1603 struct sched_domain *tmp, *sd = NULL;
1604 int new_cpu, this_cpu = get_cpu();
1606 schedstat_inc(this_rq(), sbe_cnt);
1607 /* Prefer the current CPU if there's only this task running */
1608 if (this_rq()->nr_running <= 1)
1611 for_each_domain(this_cpu, tmp)
1612 if (tmp->flags & SD_BALANCE_EXEC)
1616 schedstat_inc(sd, sbe_attempts);
1617 new_cpu = find_idlest_cpu(current, this_cpu, sd);
1618 if (new_cpu != this_cpu) {
1619 schedstat_inc(sd, sbe_pushed);
1621 sched_migrate_task(current, new_cpu);
1630 * pull_task - move a task from a remote runqueue to the local runqueue.
1631 * Both runqueues must be locked.
1634 void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p,
1635 runqueue_t *this_rq, prio_array_t *this_array, int this_cpu)
1637 dequeue_task(p, src_array);
1638 src_rq->nr_running--;
1639 set_task_cpu(p, this_cpu);
1640 this_rq->nr_running++;
1641 enqueue_task(p, this_array);
1642 p->timestamp = (p->timestamp - src_rq->timestamp_last_tick)
1643 + this_rq->timestamp_last_tick;
1645 * Note that idle threads have a prio of MAX_PRIO, for this test
1646 * to be always true for them.
1648 if (TASK_PREEMPTS_CURR(p, this_rq))
1649 resched_task(this_rq->curr);
1653 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
1656 int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu,
1657 struct sched_domain *sd, enum idle_type idle)
1660 * We do not migrate tasks that are:
1661 * 1) running (obviously), or
1662 * 2) cannot be migrated to this CPU due to cpus_allowed, or
1663 * 3) are cache-hot on their current CPU.
1665 if (task_running(rq, p))
1667 if (!cpu_isset(this_cpu, p->cpus_allowed))
1670 /* Aggressive migration if we've failed balancing */
1671 if (idle == NEWLY_IDLE ||
1672 sd->nr_balance_failed < sd->cache_nice_tries) {
1673 if (task_hot(p, rq->timestamp_last_tick, sd))
1681 * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq,
1682 * as part of a balancing operation within "domain". Returns the number of
1685 * Called with both runqueues locked.
1687 static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest,
1688 unsigned long max_nr_move, struct sched_domain *sd,
1689 enum idle_type idle)
1691 prio_array_t *array, *dst_array;
1692 struct list_head *head, *curr;
1693 int idx, pulled = 0;
1696 if (max_nr_move <= 0 || busiest->nr_running <= 1)
1700 * We first consider expired tasks. Those will likely not be
1701 * executed in the near future, and they are most likely to
1702 * be cache-cold, thus switching CPUs has the least effect
1705 if (busiest->expired->nr_active) {
1706 array = busiest->expired;
1707 dst_array = this_rq->expired;
1709 array = busiest->active;
1710 dst_array = this_rq->active;
1714 /* Start searching at priority 0: */
1718 idx = sched_find_first_bit(array->bitmap);
1720 idx = find_next_bit(array->bitmap, MAX_PRIO, idx);
1721 if (idx >= MAX_PRIO) {
1722 if (array == busiest->expired && busiest->active->nr_active) {
1723 array = busiest->active;
1724 dst_array = this_rq->active;
1730 head = array->queue + idx;
1733 tmp = list_entry(curr, task_t, run_list);
1737 if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle)) {
1745 * Right now, this is the only place pull_task() is called,
1746 * so we can safely collect pull_task() stats here rather than
1747 * inside pull_task().
1749 schedstat_inc(this_rq, pt_gained[idle]);
1750 schedstat_inc(busiest, pt_lost[idle]);
1752 pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu);
1755 /* We only want to steal up to the prescribed number of tasks. */
1756 if (pulled < max_nr_move) {
1767 * find_busiest_group finds and returns the busiest CPU group within the
1768 * domain. It calculates and returns the number of tasks which should be
1769 * moved to restore balance via the imbalance parameter.
1771 static struct sched_group *
1772 find_busiest_group(struct sched_domain *sd, int this_cpu,
1773 unsigned long *imbalance, enum idle_type idle)
1775 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
1776 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
1778 max_load = this_load = total_load = total_pwr = 0;
1785 local_group = cpu_isset(this_cpu, group->cpumask);
1787 /* Tally up the load of all CPUs in the group */
1790 for_each_cpu_mask(i, group->cpumask) {
1791 /* Bias balancing toward cpus of our domain */
1793 load = target_load(i);
1795 load = source_load(i);
1804 total_load += avg_load;
1805 total_pwr += group->cpu_power;
1807 /* Adjust by relative CPU power of the group */
1808 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
1811 this_load = avg_load;
1814 } else if (avg_load > max_load) {
1815 max_load = avg_load;
1819 group = group->next;
1820 } while (group != sd->groups);
1822 if (!busiest || this_load >= max_load)
1825 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
1827 if (this_load >= avg_load ||
1828 100*max_load <= sd->imbalance_pct*this_load)
1832 * We're trying to get all the cpus to the average_load, so we don't
1833 * want to push ourselves above the average load, nor do we wish to
1834 * reduce the max loaded cpu below the average load, as either of these
1835 * actions would just result in more rebalancing later, and ping-pong
1836 * tasks around. Thus we look for the minimum possible imbalance.
1837 * Negative imbalances (*we* are more loaded than anyone else) will
1838 * be counted as no imbalance for these purposes -- we can't fix that
1839 * by pulling tasks to us. Be careful of negative numbers as they'll
1840 * appear as very large values with unsigned longs.
1842 *imbalance = min(max_load - avg_load, avg_load - this_load);
1844 /* How much load to actually move to equalise the imbalance */
1845 *imbalance = (*imbalance * min(busiest->cpu_power, this->cpu_power))
1848 if (*imbalance < SCHED_LOAD_SCALE - 1) {
1849 unsigned long pwr_now = 0, pwr_move = 0;
1852 if (max_load - this_load >= SCHED_LOAD_SCALE*2) {
1858 * OK, we don't have enough imbalance to justify moving tasks,
1859 * however we may be able to increase total CPU power used by
1863 pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load);
1864 pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load);
1865 pwr_now /= SCHED_LOAD_SCALE;
1867 /* Amount of load we'd subtract */
1868 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power;
1870 pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE,
1873 /* Amount of load we'd add */
1874 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power;
1877 pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp);
1878 pwr_move /= SCHED_LOAD_SCALE;
1880 /* Move if we gain another 8th of a CPU worth of throughput */
1881 if (pwr_move < pwr_now + SCHED_LOAD_SCALE / 8)
1888 /* Get rid of the scaling factor, rounding down as we divide */
1889 *imbalance = (*imbalance + 1) / SCHED_LOAD_SCALE;
1894 if (busiest && (idle == NEWLY_IDLE ||
1895 (idle == SCHED_IDLE && max_load > SCHED_LOAD_SCALE)) ) {
1905 * find_busiest_queue - find the busiest runqueue among the cpus in group.
1907 static runqueue_t *find_busiest_queue(struct sched_group *group)
1909 unsigned long load, max_load = 0;
1910 runqueue_t *busiest = NULL;
1913 for_each_cpu_mask(i, group->cpumask) {
1914 load = source_load(i);
1916 if (load > max_load) {
1918 busiest = cpu_rq(i);
1926 * Check this_cpu to ensure it is balanced within domain. Attempt to move
1927 * tasks if there is an imbalance.
1929 * Called with this_rq unlocked.
1931 static int load_balance(int this_cpu, runqueue_t *this_rq,
1932 struct sched_domain *sd, enum idle_type idle)
1934 struct sched_group *group;
1935 runqueue_t *busiest;
1936 unsigned long imbalance;
1939 spin_lock(&this_rq->lock);
1940 schedstat_inc(sd, lb_cnt[idle]);
1942 group = find_busiest_group(sd, this_cpu, &imbalance, idle);
1944 schedstat_inc(sd, lb_nobusyg[idle]);
1948 busiest = find_busiest_queue(group);
1950 schedstat_inc(sd, lb_nobusyq[idle]);
1955 * This should be "impossible", but since load
1956 * balancing is inherently racy and statistical,
1957 * it could happen in theory.
1959 if (unlikely(busiest == this_rq)) {
1964 schedstat_add(sd, lb_imbalance[idle], imbalance);
1967 if (busiest->nr_running > 1) {
1969 * Attempt to move tasks. If find_busiest_group has found
1970 * an imbalance but busiest->nr_running <= 1, the group is
1971 * still unbalanced. nr_moved simply stays zero, so it is
1972 * correctly treated as an imbalance.
1974 double_lock_balance(this_rq, busiest);
1975 nr_moved = move_tasks(this_rq, this_cpu, busiest,
1976 imbalance, sd, idle);
1977 spin_unlock(&busiest->lock);
1979 spin_unlock(&this_rq->lock);
1982 schedstat_inc(sd, lb_failed[idle]);
1983 sd->nr_balance_failed++;
1985 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
1988 spin_lock(&busiest->lock);
1989 if (!busiest->active_balance) {
1990 busiest->active_balance = 1;
1991 busiest->push_cpu = this_cpu;
1994 spin_unlock(&busiest->lock);
1996 wake_up_process(busiest->migration_thread);
1999 * We've kicked active balancing, reset the failure
2002 sd->nr_balance_failed = sd->cache_nice_tries;
2006 * We were unbalanced, but unsuccessful in move_tasks(),
2007 * so bump the balance_interval to lessen the lock contention.
2009 if (sd->balance_interval < sd->max_interval)
2010 sd->balance_interval++;
2012 sd->nr_balance_failed = 0;
2014 /* We were unbalanced, so reset the balancing interval */
2015 sd->balance_interval = sd->min_interval;
2021 spin_unlock(&this_rq->lock);
2023 /* tune up the balancing interval */
2024 if (sd->balance_interval < sd->max_interval)
2025 sd->balance_interval *= 2;
2031 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2032 * tasks if there is an imbalance.
2034 * Called from schedule when this_rq is about to become idle (NEWLY_IDLE).
2035 * this_rq is locked.
2037 static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2038 struct sched_domain *sd)
2040 struct sched_group *group;
2041 runqueue_t *busiest = NULL;
2042 unsigned long imbalance;
2045 schedstat_inc(sd, lb_cnt[NEWLY_IDLE]);
2046 group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE);
2048 schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]);
2052 busiest = find_busiest_queue(group);
2053 if (!busiest || busiest == this_rq) {
2054 schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]);
2058 /* Attempt to move tasks */
2059 double_lock_balance(this_rq, busiest);
2061 schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance);
2062 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2063 imbalance, sd, NEWLY_IDLE);
2065 schedstat_inc(sd, lb_failed[NEWLY_IDLE]);
2067 spin_unlock(&busiest->lock);
2074 * idle_balance is called by schedule() if this_cpu is about to become
2075 * idle. Attempts to pull tasks from other CPUs.
2077 static inline void idle_balance(int this_cpu, runqueue_t *this_rq)
2079 struct sched_domain *sd;
2081 for_each_domain(this_cpu, sd) {
2082 if (sd->flags & SD_BALANCE_NEWIDLE) {
2083 if (load_balance_newidle(this_cpu, this_rq, sd)) {
2084 /* We've pulled tasks over so stop searching */
2091 #ifdef CONFIG_SCHED_SMT
2092 static int cpu_and_siblings_are_idle(int cpu)
2095 for_each_cpu_mask(sib, cpu_sibling_map[cpu]) {
2104 #define cpu_and_siblings_are_idle(A) idle_cpu(A)
2109 * active_load_balance is run by migration threads. It pushes running tasks
2110 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
2111 * running on each physical CPU where possible, and avoids physical /
2112 * logical imbalances.
2114 * Called with busiest_rq locked.
2116 static void active_load_balance(runqueue_t *busiest_rq, int busiest_cpu)
2118 struct sched_domain *sd;
2119 struct sched_group *cpu_group;
2120 cpumask_t visited_cpus;
2122 schedstat_inc(busiest_rq, alb_cnt);
2124 * Search for suitable CPUs to push tasks to in successively higher
2125 * domains with SD_LOAD_BALANCE set.
2127 visited_cpus = CPU_MASK_NONE;
2128 for_each_domain(busiest_cpu, sd) {
2129 if (!(sd->flags & SD_LOAD_BALANCE) || busiest_rq->nr_running <= 1)
2130 break; /* no more domains to search or no more tasks to move */
2132 cpu_group = sd->groups;
2133 do { /* sched_groups should either use list_heads or be merged into the domains structure */
2134 int cpu, target_cpu = -1;
2135 runqueue_t *target_rq;
2137 for_each_cpu_mask(cpu, cpu_group->cpumask) {
2138 if (cpu_isset(cpu, visited_cpus) || cpu == busiest_cpu ||
2139 !cpu_and_siblings_are_idle(cpu)) {
2140 cpu_set(cpu, visited_cpus);
2146 if (target_cpu == -1)
2147 goto next_group; /* failed to find a suitable target cpu in this domain */
2149 target_rq = cpu_rq(target_cpu);
2152 * This condition is "impossible", if it occurs we need to fix it
2153 * Reported by Bjorn Helgaas on a 128-cpu setup.
2155 BUG_ON(busiest_rq == target_rq);
2157 /* move a task from busiest_rq to target_rq */
2158 double_lock_balance(busiest_rq, target_rq);
2159 if (move_tasks(target_rq, target_cpu, busiest_rq, 1, sd, SCHED_IDLE)) {
2160 schedstat_inc(busiest_rq, alb_lost);
2161 schedstat_inc(target_rq, alb_gained);
2163 schedstat_inc(busiest_rq, alb_failed);
2165 spin_unlock(&target_rq->lock);
2167 cpu_group = cpu_group->next;
2168 } while (cpu_group != sd->groups && busiest_rq->nr_running > 1);
2173 * rebalance_tick will get called every timer tick, on every CPU.
2175 * It checks each scheduling domain to see if it is due to be balanced,
2176 * and initiates a balancing operation if so.
2178 * Balancing parameters are set up in arch_init_sched_domains.
2181 /* Don't have all balancing operations going off at once */
2182 #define CPU_OFFSET(cpu) (HZ * cpu / NR_CPUS)
2184 static void rebalance_tick(int this_cpu, runqueue_t *this_rq,
2185 enum idle_type idle)
2187 unsigned long old_load, this_load;
2188 unsigned long j = jiffies + CPU_OFFSET(this_cpu);
2189 struct sched_domain *sd;
2191 /* Update our load */
2192 old_load = this_rq->cpu_load;
2193 this_load = this_rq->nr_running * SCHED_LOAD_SCALE;
2195 * Round up the averaging division if load is increasing. This
2196 * prevents us from getting stuck on 9 if the load is 10, for
2199 if (this_load > old_load)
2201 this_rq->cpu_load = (old_load + this_load) / 2;
2203 for_each_domain(this_cpu, sd) {
2204 unsigned long interval;
2206 if (!(sd->flags & SD_LOAD_BALANCE))
2209 interval = sd->balance_interval;
2210 if (idle != SCHED_IDLE)
2211 interval *= sd->busy_factor;
2213 /* scale ms to jiffies */
2214 interval = msecs_to_jiffies(interval);
2215 if (unlikely(!interval))
2218 if (j - sd->last_balance >= interval) {
2219 if (load_balance(this_cpu, this_rq, sd, idle)) {
2220 /* We've pulled tasks over so no longer idle */
2223 sd->last_balance += interval;
2229 * on UP we do not need to balance between CPUs:
2231 static inline void rebalance_tick(int cpu, runqueue_t *rq, enum idle_type idle)
2234 static inline void idle_balance(int cpu, runqueue_t *rq)
2239 static inline int wake_priority_sleeper(runqueue_t *rq)
2242 #ifdef CONFIG_SCHED_SMT
2243 spin_lock(&rq->lock);
2245 * If an SMT sibling task has been put to sleep for priority
2246 * reasons reschedule the idle task to see if it can now run.
2248 if (rq->nr_running) {
2249 resched_task(rq->idle);
2252 spin_unlock(&rq->lock);
2257 DEFINE_PER_CPU(struct kernel_stat, kstat);
2259 EXPORT_PER_CPU_SYMBOL(kstat);
2262 * We place interactive tasks back into the active array, if possible.
2264 * To guarantee that this does not starve expired tasks we ignore the
2265 * interactivity of a task if the first expired task had to wait more
2266 * than a 'reasonable' amount of time. This deadline timeout is
2267 * load-dependent, as the frequency of array switched decreases with
2268 * increasing number of running tasks. We also ignore the interactivity
2269 * if a better static_prio task has expired:
2271 #define EXPIRED_STARVING(rq) \
2272 ((STARVATION_LIMIT && ((rq)->expired_timestamp && \
2273 (jiffies - (rq)->expired_timestamp >= \
2274 STARVATION_LIMIT * ((rq)->nr_running) + 1))) || \
2275 ((rq)->curr->static_prio > (rq)->best_expired_prio))
2278 * This function gets called by the timer code, with HZ frequency.
2279 * We call it with interrupts disabled.
2281 * It also gets called by the fork code, when changing the parent's
2284 void scheduler_tick(int user_ticks, int sys_ticks)
2286 int cpu = smp_processor_id();
2287 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2288 runqueue_t *rq = this_rq();
2289 task_t *p = current;
2291 rq->timestamp_last_tick = sched_clock();
2293 if (rcu_pending(cpu))
2294 rcu_check_callbacks(cpu, user_ticks);
2296 /* note: this timer irq context must be accounted for as well */
2297 if (hardirq_count() - HARDIRQ_OFFSET) {
2298 cpustat->irq += sys_ticks;
2300 } else if (softirq_count()) {
2301 cpustat->softirq += sys_ticks;
2305 if (p == rq->idle) {
2306 if (atomic_read(&rq->nr_iowait) > 0)
2307 cpustat->iowait += sys_ticks;
2309 cpustat->idle += sys_ticks;
2310 if (wake_priority_sleeper(rq))
2312 rebalance_tick(cpu, rq, SCHED_IDLE);
2315 if (TASK_NICE(p) > 0)
2316 cpustat->nice += user_ticks;
2318 cpustat->user += user_ticks;
2319 cpustat->system += sys_ticks;
2321 /* Task might have expired already, but not scheduled off yet */
2322 if (p->array != rq->active) {
2323 set_tsk_need_resched(p);
2326 spin_lock(&rq->lock);
2328 * The task was running during this tick - update the
2329 * time slice counter. Note: we do not update a thread's
2330 * priority until it either goes to sleep or uses up its
2331 * timeslice. This makes it possible for interactive tasks
2332 * to use up their timeslices at their highest priority levels.
2336 * RR tasks need a special form of timeslice management.
2337 * FIFO tasks have no timeslices.
2339 if ((p->policy == SCHED_RR) && !--p->time_slice) {
2340 p->time_slice = task_timeslice(p);
2341 p->first_time_slice = 0;
2342 set_tsk_need_resched(p);
2344 /* put it at the end of the queue: */
2345 dequeue_task(p, rq->active);
2346 enqueue_task(p, rq->active);
2350 if (!--p->time_slice) {
2351 dequeue_task(p, rq->active);
2352 set_tsk_need_resched(p);
2353 p->prio = effective_prio(p);
2354 p->time_slice = task_timeslice(p);
2355 p->first_time_slice = 0;
2357 if (!rq->expired_timestamp)
2358 rq->expired_timestamp = jiffies;
2359 if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) {
2360 enqueue_task(p, rq->expired);
2361 if (p->static_prio < rq->best_expired_prio)
2362 rq->best_expired_prio = p->static_prio;
2364 enqueue_task(p, rq->active);
2367 * Prevent a too long timeslice allowing a task to monopolize
2368 * the CPU. We do this by splitting up the timeslice into
2371 * Note: this does not mean the task's timeslices expire or
2372 * get lost in any way, they just might be preempted by
2373 * another task of equal priority. (one with higher
2374 * priority would have preempted this task already.) We
2375 * requeue this task to the end of the list on this priority
2376 * level, which is in essence a round-robin of tasks with
2379 * This only applies to tasks in the interactive
2380 * delta range with at least TIMESLICE_GRANULARITY to requeue.
2382 if (TASK_INTERACTIVE(p) && !((task_timeslice(p) -
2383 p->time_slice) % TIMESLICE_GRANULARITY(p)) &&
2384 (p->time_slice >= TIMESLICE_GRANULARITY(p)) &&
2385 (p->array == rq->active)) {
2387 dequeue_task(p, rq->active);
2388 set_tsk_need_resched(p);
2389 p->prio = effective_prio(p);
2390 enqueue_task(p, rq->active);
2394 spin_unlock(&rq->lock);
2396 rebalance_tick(cpu, rq, NOT_IDLE);
2399 #ifdef CONFIG_SCHED_SMT
2400 static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
2402 struct sched_domain *sd = this_rq->sd;
2403 cpumask_t sibling_map;
2406 if (!(sd->flags & SD_SHARE_CPUPOWER))
2410 * Unlock the current runqueue because we have to lock in
2411 * CPU order to avoid deadlocks. Caller knows that we might
2412 * unlock. We keep IRQs disabled.
2414 spin_unlock(&this_rq->lock);
2416 sibling_map = sd->span;
2418 for_each_cpu_mask(i, sibling_map)
2419 spin_lock(&cpu_rq(i)->lock);
2421 * We clear this CPU from the mask. This both simplifies the
2422 * inner loop and keps this_rq locked when we exit:
2424 cpu_clear(this_cpu, sibling_map);
2426 for_each_cpu_mask(i, sibling_map) {
2427 runqueue_t *smt_rq = cpu_rq(i);
2430 * If an SMT sibling task is sleeping due to priority
2431 * reasons wake it up now.
2433 if (smt_rq->curr == smt_rq->idle && smt_rq->nr_running)
2434 resched_task(smt_rq->idle);
2437 for_each_cpu_mask(i, sibling_map)
2438 spin_unlock(&cpu_rq(i)->lock);
2440 * We exit with this_cpu's rq still held and IRQs
2445 static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
2447 struct sched_domain *sd = this_rq->sd;
2448 cpumask_t sibling_map;
2449 prio_array_t *array;
2453 if (!(sd->flags & SD_SHARE_CPUPOWER))
2457 * The same locking rules and details apply as for
2458 * wake_sleeping_dependent():
2460 spin_unlock(&this_rq->lock);
2461 sibling_map = sd->span;
2462 for_each_cpu_mask(i, sibling_map)
2463 spin_lock(&cpu_rq(i)->lock);
2464 cpu_clear(this_cpu, sibling_map);
2467 * Establish next task to be run - it might have gone away because
2468 * we released the runqueue lock above:
2470 if (!this_rq->nr_running)
2472 array = this_rq->active;
2473 if (!array->nr_active)
2474 array = this_rq->expired;
2475 BUG_ON(!array->nr_active);
2477 p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next,
2480 for_each_cpu_mask(i, sibling_map) {
2481 runqueue_t *smt_rq = cpu_rq(i);
2482 task_t *smt_curr = smt_rq->curr;
2485 * If a user task with lower static priority than the
2486 * running task on the SMT sibling is trying to schedule,
2487 * delay it till there is proportionately less timeslice
2488 * left of the sibling task to prevent a lower priority
2489 * task from using an unfair proportion of the
2490 * physical cpu's resources. -ck
2492 if (((smt_curr->time_slice * (100 - sd->per_cpu_gain) / 100) >
2493 task_timeslice(p) || rt_task(smt_curr)) &&
2494 p->mm && smt_curr->mm && !rt_task(p))
2498 * Reschedule a lower priority task on the SMT sibling,
2499 * or wake it up if it has been put to sleep for priority
2502 if ((((p->time_slice * (100 - sd->per_cpu_gain) / 100) >
2503 task_timeslice(smt_curr) || rt_task(p)) &&
2504 smt_curr->mm && p->mm && !rt_task(smt_curr)) ||
2505 (smt_curr == smt_rq->idle && smt_rq->nr_running))
2506 resched_task(smt_curr);
2509 for_each_cpu_mask(i, sibling_map)
2510 spin_unlock(&cpu_rq(i)->lock);
2514 static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
2518 static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
2525 * schedule() is the main scheduler function.
2527 asmlinkage void __sched schedule(void)
2530 task_t *prev, *next;
2532 prio_array_t *array;
2533 struct list_head *queue;
2534 unsigned long long now;
2535 unsigned long run_time;
2539 * Test if we are atomic. Since do_exit() needs to call into
2540 * schedule() atomically, we ignore that path for now.
2541 * Otherwise, whine if we are scheduling when we should not be.
2543 if (likely(!(current->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)))) {
2544 if (unlikely(in_atomic())) {
2545 printk(KERN_ERR "scheduling while atomic: "
2547 current->comm, preempt_count(), current->pid);
2551 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2556 release_kernel_lock(prev);
2557 need_resched_nonpreemptible:
2561 * The idle thread is not allowed to schedule!
2562 * Remove this check after it has been exercised a bit.
2564 if (unlikely(current == rq->idle) && current->state != TASK_RUNNING) {
2565 printk(KERN_ERR "bad: scheduling from the idle thread!\n");
2569 schedstat_inc(rq, sched_cnt);
2570 now = sched_clock();
2571 if (likely(now - prev->timestamp < NS_MAX_SLEEP_AVG))
2572 run_time = now - prev->timestamp;
2574 run_time = NS_MAX_SLEEP_AVG;
2577 * Tasks with interactive credits get charged less run_time
2578 * at high sleep_avg to delay them losing their interactive
2581 if (HIGH_CREDIT(prev))
2582 run_time /= (CURRENT_BONUS(prev) ? : 1);
2584 spin_lock_irq(&rq->lock);
2586 if (unlikely(current->flags & PF_DEAD))
2587 current->state = EXIT_DEAD;
2589 * if entering off of a kernel preemption go straight
2590 * to picking the next task.
2592 switch_count = &prev->nivcsw;
2593 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
2594 switch_count = &prev->nvcsw;
2595 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
2596 unlikely(signal_pending(prev))))
2597 prev->state = TASK_RUNNING;
2599 if (prev->state == TASK_UNINTERRUPTIBLE)
2600 rq->nr_uninterruptible++;
2601 deactivate_task(prev, rq);
2605 cpu = smp_processor_id();
2606 if (unlikely(!rq->nr_running)) {
2608 idle_balance(cpu, rq);
2609 if (!rq->nr_running) {
2611 rq->expired_timestamp = 0;
2612 wake_sleeping_dependent(cpu, rq);
2614 * wake_sleeping_dependent() might have released
2615 * the runqueue, so break out if we got new
2618 if (!rq->nr_running)
2622 if (dependent_sleeper(cpu, rq)) {
2627 * dependent_sleeper() releases and reacquires the runqueue
2628 * lock, hence go into the idle loop if the rq went
2631 if (unlikely(!rq->nr_running))
2636 if (unlikely(!array->nr_active)) {
2638 * Switch the active and expired arrays.
2640 schedstat_inc(rq, sched_switch);
2641 rq->active = rq->expired;
2642 rq->expired = array;
2644 rq->expired_timestamp = 0;
2645 rq->best_expired_prio = MAX_PRIO;
2647 schedstat_inc(rq, sched_noswitch);
2649 idx = sched_find_first_bit(array->bitmap);
2650 queue = array->queue + idx;
2651 next = list_entry(queue->next, task_t, run_list);
2653 if (!rt_task(next) && next->activated > 0) {
2654 unsigned long long delta = now - next->timestamp;
2656 if (next->activated == 1)
2657 delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128;
2659 array = next->array;
2660 dequeue_task(next, array);
2661 recalc_task_prio(next, next->timestamp + delta);
2662 enqueue_task(next, array);
2664 next->activated = 0;
2666 if (next == rq->idle)
2667 schedstat_inc(rq, sched_goidle);
2669 clear_tsk_need_resched(prev);
2670 rcu_qsctr_inc(task_cpu(prev));
2672 prev->sleep_avg -= run_time;
2673 if ((long)prev->sleep_avg <= 0) {
2674 prev->sleep_avg = 0;
2675 if (!(HIGH_CREDIT(prev) || LOW_CREDIT(prev)))
2676 prev->interactive_credit--;
2678 prev->timestamp = prev->last_ran = now;
2680 sched_info_switch(prev, next);
2681 if (likely(prev != next)) {
2682 next->timestamp = now;
2687 prepare_arch_switch(rq, next);
2688 prev = context_switch(rq, prev, next);
2691 finish_task_switch(prev);
2693 spin_unlock_irq(&rq->lock);
2696 if (unlikely(reacquire_kernel_lock(prev) < 0))
2697 goto need_resched_nonpreemptible;
2698 preempt_enable_no_resched();
2699 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
2703 EXPORT_SYMBOL(schedule);
2705 #ifdef CONFIG_PREEMPT
2707 * this is is the entry point to schedule() from in-kernel preemption
2708 * off of preempt_enable. Kernel preemptions off return from interrupt
2709 * occur there and call schedule directly.
2711 asmlinkage void __sched preempt_schedule(void)
2713 struct thread_info *ti = current_thread_info();
2716 * If there is a non-zero preempt_count or interrupts are disabled,
2717 * we do not want to preempt the current task. Just return..
2719 if (unlikely(ti->preempt_count || irqs_disabled()))
2723 ti->preempt_count = PREEMPT_ACTIVE;
2725 ti->preempt_count = 0;
2727 /* we could miss a preemption opportunity between schedule and now */
2729 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
2733 EXPORT_SYMBOL(preempt_schedule);
2734 #endif /* CONFIG_PREEMPT */
2736 int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, void *key)
2738 task_t *p = curr->task;
2739 return try_to_wake_up(p, mode, sync);
2742 EXPORT_SYMBOL(default_wake_function);
2745 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
2746 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
2747 * number) then we wake all the non-exclusive tasks and one exclusive task.
2749 * There are circumstances in which we can try to wake a task which has already
2750 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
2751 * zero in this (rare) case, and we handle it by continuing to scan the queue.
2753 static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
2754 int nr_exclusive, int sync, void *key)
2756 struct list_head *tmp, *next;
2758 list_for_each_safe(tmp, next, &q->task_list) {
2761 curr = list_entry(tmp, wait_queue_t, task_list);
2762 flags = curr->flags;
2763 if (curr->func(curr, mode, sync, key) &&
2764 (flags & WQ_FLAG_EXCLUSIVE) &&
2771 * __wake_up - wake up threads blocked on a waitqueue.
2773 * @mode: which threads
2774 * @nr_exclusive: how many wake-one or wake-many threads to wake up
2776 void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
2777 int nr_exclusive, void *key)
2779 unsigned long flags;
2781 spin_lock_irqsave(&q->lock, flags);
2782 __wake_up_common(q, mode, nr_exclusive, 0, key);
2783 spin_unlock_irqrestore(&q->lock, flags);
2786 EXPORT_SYMBOL(__wake_up);
2789 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
2791 void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
2793 __wake_up_common(q, mode, 1, 0, NULL);
2797 * __wake_up - sync- wake up threads blocked on a waitqueue.
2799 * @mode: which threads
2800 * @nr_exclusive: how many wake-one or wake-many threads to wake up
2802 * The sync wakeup differs that the waker knows that it will schedule
2803 * away soon, so while the target thread will be woken up, it will not
2804 * be migrated to another CPU - ie. the two threads are 'synchronized'
2805 * with each other. This can prevent needless bouncing between CPUs.
2807 * On UP it can prevent extra preemption.
2809 void fastcall __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
2811 unsigned long flags;
2817 if (unlikely(!nr_exclusive))
2820 spin_lock_irqsave(&q->lock, flags);
2821 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
2822 spin_unlock_irqrestore(&q->lock, flags);
2824 EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
2826 void fastcall complete(struct completion *x)
2828 unsigned long flags;
2830 spin_lock_irqsave(&x->wait.lock, flags);
2832 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2834 spin_unlock_irqrestore(&x->wait.lock, flags);
2836 EXPORT_SYMBOL(complete);
2838 void fastcall complete_all(struct completion *x)
2840 unsigned long flags;
2842 spin_lock_irqsave(&x->wait.lock, flags);
2843 x->done += UINT_MAX/2;
2844 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2846 spin_unlock_irqrestore(&x->wait.lock, flags);
2848 EXPORT_SYMBOL(complete_all);
2850 void fastcall __sched wait_for_completion(struct completion *x)
2853 spin_lock_irq(&x->wait.lock);
2855 DECLARE_WAITQUEUE(wait, current);
2857 wait.flags |= WQ_FLAG_EXCLUSIVE;
2858 __add_wait_queue_tail(&x->wait, &wait);
2860 __set_current_state(TASK_UNINTERRUPTIBLE);
2861 spin_unlock_irq(&x->wait.lock);
2863 spin_lock_irq(&x->wait.lock);
2865 __remove_wait_queue(&x->wait, &wait);
2868 spin_unlock_irq(&x->wait.lock);
2870 EXPORT_SYMBOL(wait_for_completion);
2872 #define SLEEP_ON_VAR \
2873 unsigned long flags; \
2874 wait_queue_t wait; \
2875 init_waitqueue_entry(&wait, current);
2877 #define SLEEP_ON_HEAD \
2878 spin_lock_irqsave(&q->lock,flags); \
2879 __add_wait_queue(q, &wait); \
2880 spin_unlock(&q->lock);
2882 #define SLEEP_ON_TAIL \
2883 spin_lock_irq(&q->lock); \
2884 __remove_wait_queue(q, &wait); \
2885 spin_unlock_irqrestore(&q->lock, flags);
2887 void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q)
2891 current->state = TASK_INTERRUPTIBLE;
2898 EXPORT_SYMBOL(interruptible_sleep_on);
2900 long fastcall __sched interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
2904 current->state = TASK_INTERRUPTIBLE;
2907 timeout = schedule_timeout(timeout);
2913 EXPORT_SYMBOL(interruptible_sleep_on_timeout);
2915 void fastcall __sched sleep_on(wait_queue_head_t *q)
2919 current->state = TASK_UNINTERRUPTIBLE;
2926 EXPORT_SYMBOL(sleep_on);
2928 long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
2932 current->state = TASK_UNINTERRUPTIBLE;
2935 timeout = schedule_timeout(timeout);
2941 EXPORT_SYMBOL(sleep_on_timeout);
2943 void set_user_nice(task_t *p, long nice)
2945 unsigned long flags;
2946 prio_array_t *array;
2948 int old_prio, new_prio, delta;
2950 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
2953 * We have to be careful, if called from sys_setpriority(),
2954 * the task might be in the middle of scheduling on another CPU.
2956 rq = task_rq_lock(p, &flags);
2958 * The RT priorities are set via setscheduler(), but we still
2959 * allow the 'normal' nice value to be set - but as expected
2960 * it wont have any effect on scheduling until the task is
2964 p->static_prio = NICE_TO_PRIO(nice);
2969 dequeue_task(p, array);
2972 new_prio = NICE_TO_PRIO(nice);
2973 delta = new_prio - old_prio;
2974 p->static_prio = NICE_TO_PRIO(nice);
2978 enqueue_task(p, array);
2980 * If the task increased its priority or is running and
2981 * lowered its priority, then reschedule its CPU:
2983 if (delta < 0 || (delta > 0 && task_running(rq, p)))
2984 resched_task(rq->curr);
2987 task_rq_unlock(rq, &flags);
2990 EXPORT_SYMBOL(set_user_nice);
2992 #ifdef __ARCH_WANT_SYS_NICE
2995 * sys_nice - change the priority of the current process.
2996 * @increment: priority increment
2998 * sys_setpriority is a more generic, but much slower function that
2999 * does similar things.
3001 asmlinkage long sys_nice(int increment)
3007 * Setpriority might change our priority at the same moment.
3008 * We don't have to worry. Conceptually one call occurs first
3009 * and we have a single winner.
3011 if (increment < 0) {
3012 if (!capable(CAP_SYS_NICE))
3014 if (increment < -40)
3020 nice = PRIO_TO_NICE(current->static_prio) + increment;
3026 retval = security_task_setnice(current, nice);
3030 set_user_nice(current, nice);
3037 * task_prio - return the priority value of a given task.
3038 * @p: the task in question.
3040 * This is the priority value as seen by users in /proc.
3041 * RT tasks are offset by -200. Normal tasks are centered
3042 * around 0, value goes from -16 to +15.
3044 int task_prio(const task_t *p)
3046 return p->prio - MAX_RT_PRIO;
3050 * task_nice - return the nice value of a given task.
3051 * @p: the task in question.
3053 int task_nice(const task_t *p)
3055 return TASK_NICE(p);
3059 * idle_cpu - is a given cpu idle currently?
3060 * @cpu: the processor in question.
3062 int idle_cpu(int cpu)
3064 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
3067 EXPORT_SYMBOL_GPL(idle_cpu);
3070 * find_process_by_pid - find a process with a matching PID value.
3071 * @pid: the pid in question.
3073 static inline task_t *find_process_by_pid(pid_t pid)
3075 return pid ? find_task_by_pid(pid) : current;
3078 /* Actually do priority change: must hold rq lock. */
3079 static void __setscheduler(struct task_struct *p, int policy, int prio)
3083 p->rt_priority = prio;
3084 if (policy != SCHED_NORMAL)
3085 p->prio = MAX_USER_RT_PRIO-1 - p->rt_priority;
3087 p->prio = p->static_prio;
3091 * setscheduler - change the scheduling policy and/or RT priority of a thread.
3093 static int setscheduler(pid_t pid, int policy, struct sched_param __user *param)
3095 struct sched_param lp;
3096 int retval = -EINVAL;
3097 int oldprio, oldpolicy = -1;
3098 prio_array_t *array;
3099 unsigned long flags;
3103 if (!param || pid < 0)
3107 if (copy_from_user(&lp, param, sizeof(struct sched_param)))
3111 * We play safe to avoid deadlocks.
3113 read_lock_irq(&tasklist_lock);
3115 p = find_process_by_pid(pid);
3121 /* double check policy once rq lock held */
3123 policy = oldpolicy = p->policy;
3126 if (policy != SCHED_FIFO && policy != SCHED_RR &&
3127 policy != SCHED_NORMAL)
3131 * Valid priorities for SCHED_FIFO and SCHED_RR are
3132 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL is 0.
3135 if (lp.sched_priority < 0 || lp.sched_priority > MAX_USER_RT_PRIO-1)
3137 if ((policy == SCHED_NORMAL) != (lp.sched_priority == 0))
3141 if ((policy == SCHED_FIFO || policy == SCHED_RR) &&
3142 !capable(CAP_SYS_NICE))
3144 if ((current->euid != p->euid) && (current->euid != p->uid) &&
3145 !capable(CAP_SYS_NICE))
3148 retval = security_task_setscheduler(p, policy, &lp);
3152 * To be able to change p->policy safely, the apropriate
3153 * runqueue lock must be held.
3155 rq = task_rq_lock(p, &flags);
3156 /* recheck policy now with rq lock held */
3157 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3158 policy = oldpolicy = -1;
3159 task_rq_unlock(rq, &flags);
3164 deactivate_task(p, task_rq(p));
3167 __setscheduler(p, policy, lp.sched_priority);
3169 __activate_task(p, task_rq(p));
3171 * Reschedule if we are currently running on this runqueue and
3172 * our priority decreased, or if we are not currently running on
3173 * this runqueue and our priority is higher than the current's
3175 if (task_running(rq, p)) {
3176 if (p->prio > oldprio)
3177 resched_task(rq->curr);
3178 } else if (TASK_PREEMPTS_CURR(p, rq))
3179 resched_task(rq->curr);
3181 task_rq_unlock(rq, &flags);
3183 read_unlock_irq(&tasklist_lock);
3189 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
3190 * @pid: the pid in question.
3191 * @policy: new policy
3192 * @param: structure containing the new RT priority.
3194 asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
3195 struct sched_param __user *param)
3197 return setscheduler(pid, policy, param);
3201 * sys_sched_setparam - set/change the RT priority of a thread
3202 * @pid: the pid in question.
3203 * @param: structure containing the new RT priority.
3205 asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
3207 return setscheduler(pid, -1, param);
3211 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
3212 * @pid: the pid in question.
3214 asmlinkage long sys_sched_getscheduler(pid_t pid)
3216 int retval = -EINVAL;
3223 read_lock(&tasklist_lock);
3224 p = find_process_by_pid(pid);
3226 retval = security_task_getscheduler(p);
3230 read_unlock(&tasklist_lock);
3237 * sys_sched_getscheduler - get the RT priority of a thread
3238 * @pid: the pid in question.
3239 * @param: structure containing the RT priority.
3241 asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
3243 struct sched_param lp;
3244 int retval = -EINVAL;
3247 if (!param || pid < 0)
3250 read_lock(&tasklist_lock);
3251 p = find_process_by_pid(pid);
3256 retval = security_task_getscheduler(p);
3260 lp.sched_priority = p->rt_priority;
3261 read_unlock(&tasklist_lock);
3264 * This one might sleep, we cannot do it with a spinlock held ...
3266 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
3272 read_unlock(&tasklist_lock);
3276 long sched_setaffinity(pid_t pid, cpumask_t new_mask)
3282 read_lock(&tasklist_lock);
3284 p = find_process_by_pid(pid);
3286 read_unlock(&tasklist_lock);
3287 unlock_cpu_hotplug();
3292 * It is not safe to call set_cpus_allowed with the
3293 * tasklist_lock held. We will bump the task_struct's
3294 * usage count and then drop tasklist_lock.
3297 read_unlock(&tasklist_lock);
3300 if ((current->euid != p->euid) && (current->euid != p->uid) &&
3301 !capable(CAP_SYS_NICE))
3304 retval = set_cpus_allowed(p, new_mask);
3308 unlock_cpu_hotplug();
3312 static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
3313 cpumask_t *new_mask)
3315 if (len < sizeof(cpumask_t)) {
3316 memset(new_mask, 0, sizeof(cpumask_t));
3317 } else if (len > sizeof(cpumask_t)) {
3318 len = sizeof(cpumask_t);
3320 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
3324 * sys_sched_setaffinity - set the cpu affinity of a process
3325 * @pid: pid of the process
3326 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
3327 * @user_mask_ptr: user-space pointer to the new cpu mask
3329 asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
3330 unsigned long __user *user_mask_ptr)
3335 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
3339 return sched_setaffinity(pid, new_mask);
3343 * Represents all cpu's present in the system
3344 * In systems capable of hotplug, this map could dynamically grow
3345 * as new cpu's are detected in the system via any platform specific
3346 * method, such as ACPI for e.g.
3349 cpumask_t cpu_present_map;
3350 EXPORT_SYMBOL(cpu_present_map);
3353 cpumask_t cpu_online_map = CPU_MASK_ALL;
3354 cpumask_t cpu_possible_map = CPU_MASK_ALL;
3357 long sched_getaffinity(pid_t pid, cpumask_t *mask)
3363 read_lock(&tasklist_lock);
3366 p = find_process_by_pid(pid);
3371 cpus_and(*mask, p->cpus_allowed, cpu_possible_map);
3374 read_unlock(&tasklist_lock);
3375 unlock_cpu_hotplug();
3383 * sys_sched_getaffinity - get the cpu affinity of a process
3384 * @pid: pid of the process
3385 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
3386 * @user_mask_ptr: user-space pointer to hold the current cpu mask
3388 asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
3389 unsigned long __user *user_mask_ptr)
3394 if (len < sizeof(cpumask_t))
3397 ret = sched_getaffinity(pid, &mask);
3401 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
3404 return sizeof(cpumask_t);
3408 * sys_sched_yield - yield the current processor to other threads.
3410 * this function yields the current CPU by moving the calling thread
3411 * to the expired array. If there are no other threads running on this
3412 * CPU then this function will return.
3414 asmlinkage long sys_sched_yield(void)
3416 runqueue_t *rq = this_rq_lock();
3417 prio_array_t *array = current->array;
3418 prio_array_t *target = rq->expired;
3420 schedstat_inc(rq, yld_cnt);
3422 * We implement yielding by moving the task into the expired
3425 * (special rule: RT tasks will just roundrobin in the active
3428 if (rt_task(current))
3429 target = rq->active;
3431 if (current->array->nr_active == 1) {
3432 schedstat_inc(rq, yld_act_empty);
3433 if (!rq->expired->nr_active)
3434 schedstat_inc(rq, yld_both_empty);
3435 } else if (!rq->expired->nr_active)
3436 schedstat_inc(rq, yld_exp_empty);
3438 dequeue_task(current, array);
3439 enqueue_task(current, target);
3442 * Since we are going to call schedule() anyway, there's
3443 * no need to preempt or enable interrupts:
3445 __release(rq->lock);
3446 _raw_spin_unlock(&rq->lock);
3447 preempt_enable_no_resched();
3454 void __sched __cond_resched(void)
3456 set_current_state(TASK_RUNNING);
3460 EXPORT_SYMBOL(__cond_resched);
3463 * yield - yield the current processor to other threads.
3465 * this is a shortcut for kernel-space yielding - it marks the
3466 * thread runnable and calls sys_sched_yield().
3468 void __sched yield(void)
3470 set_current_state(TASK_RUNNING);
3474 EXPORT_SYMBOL(yield);
3477 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
3478 * that process accounting knows that this is a task in IO wait state.
3480 * But don't do that if it is a deliberate, throttling IO wait (this task
3481 * has set its backing_dev_info: the queue against which it should throttle)
3483 void __sched io_schedule(void)
3485 struct runqueue *rq = this_rq();
3487 atomic_inc(&rq->nr_iowait);
3489 atomic_dec(&rq->nr_iowait);
3492 EXPORT_SYMBOL(io_schedule);
3494 long __sched io_schedule_timeout(long timeout)
3496 struct runqueue *rq = this_rq();
3499 atomic_inc(&rq->nr_iowait);
3500 ret = schedule_timeout(timeout);
3501 atomic_dec(&rq->nr_iowait);
3506 * sys_sched_get_priority_max - return maximum RT priority.
3507 * @policy: scheduling class.
3509 * this syscall returns the maximum rt_priority that can be used
3510 * by a given scheduling class.
3512 asmlinkage long sys_sched_get_priority_max(int policy)
3519 ret = MAX_USER_RT_PRIO-1;
3529 * sys_sched_get_priority_min - return minimum RT priority.
3530 * @policy: scheduling class.
3532 * this syscall returns the minimum rt_priority that can be used
3533 * by a given scheduling class.
3535 asmlinkage long sys_sched_get_priority_min(int policy)
3551 * sys_sched_rr_get_interval - return the default timeslice of a process.
3552 * @pid: pid of the process.
3553 * @interval: userspace pointer to the timeslice value.
3555 * this syscall writes the default timeslice value of a given process
3556 * into the user-space timespec buffer. A value of '0' means infinity.
3559 long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
3561 int retval = -EINVAL;
3569 read_lock(&tasklist_lock);
3570 p = find_process_by_pid(pid);
3574 retval = security_task_getscheduler(p);
3578 jiffies_to_timespec(p->policy & SCHED_FIFO ?
3579 0 : task_timeslice(p), &t);
3580 read_unlock(&tasklist_lock);
3581 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
3585 read_unlock(&tasklist_lock);
3589 static inline struct task_struct *eldest_child(struct task_struct *p)
3591 if (list_empty(&p->children)) return NULL;
3592 return list_entry(p->children.next,struct task_struct,sibling);
3595 static inline struct task_struct *older_sibling(struct task_struct *p)
3597 if (p->sibling.prev==&p->parent->children) return NULL;
3598 return list_entry(p->sibling.prev,struct task_struct,sibling);
3601 static inline struct task_struct *younger_sibling(struct task_struct *p)
3603 if (p->sibling.next==&p->parent->children) return NULL;
3604 return list_entry(p->sibling.next,struct task_struct,sibling);
3607 static void show_task(task_t * p)
3611 unsigned long free = 0;
3612 static const char *stat_nam[] = { "R", "S", "D", "T", "t", "Z", "X" };
3614 printk("%-13.13s ", p->comm);
3615 state = p->state ? __ffs(p->state) + 1 : 0;
3616 if (state < ARRAY_SIZE(stat_nam))
3617 printk(stat_nam[state]);
3620 #if (BITS_PER_LONG == 32)
3621 if (state == TASK_RUNNING)
3622 printk(" running ");
3624 printk(" %08lX ", thread_saved_pc(p));
3626 if (state == TASK_RUNNING)
3627 printk(" running task ");
3629 printk(" %016lx ", thread_saved_pc(p));
3631 #ifdef CONFIG_DEBUG_STACK_USAGE
3633 unsigned long * n = (unsigned long *) (p->thread_info+1);
3636 free = (unsigned long) n - (unsigned long)(p->thread_info+1);
3639 printk("%5lu %5d %6d ", free, p->pid, p->parent->pid);
3640 if ((relative = eldest_child(p)))
3641 printk("%5d ", relative->pid);
3644 if ((relative = younger_sibling(p)))
3645 printk("%7d", relative->pid);
3648 if ((relative = older_sibling(p)))
3649 printk(" %5d", relative->pid);
3653 printk(" (L-TLB)\n");
3655 printk(" (NOTLB)\n");
3657 if (state != TASK_RUNNING)
3658 show_stack(p, NULL);
3661 void show_state(void)
3665 #if (BITS_PER_LONG == 32)
3668 printk(" task PC pid father child younger older\n");
3672 printk(" task PC pid father child younger older\n");
3674 read_lock(&tasklist_lock);
3675 do_each_thread(g, p) {
3677 * reset the NMI-timeout, listing all files on a slow
3678 * console might take alot of time:
3680 touch_nmi_watchdog();
3682 } while_each_thread(g, p);
3684 read_unlock(&tasklist_lock);
3687 void __devinit init_idle(task_t *idle, int cpu)
3689 runqueue_t *rq = cpu_rq(cpu);
3690 unsigned long flags;
3692 idle->sleep_avg = 0;
3693 idle->interactive_credit = 0;
3695 idle->prio = MAX_PRIO;
3696 idle->state = TASK_RUNNING;
3697 set_task_cpu(idle, cpu);
3699 spin_lock_irqsave(&rq->lock, flags);
3700 rq->curr = rq->idle = idle;
3701 set_tsk_need_resched(idle);
3702 spin_unlock_irqrestore(&rq->lock, flags);
3704 /* Set the preempt count _outside_ the spinlocks! */
3705 #ifdef CONFIG_PREEMPT
3706 idle->thread_info->preempt_count = (idle->lock_depth >= 0);
3708 idle->thread_info->preempt_count = 0;
3713 * In a system that switches off the HZ timer nohz_cpu_mask
3714 * indicates which cpus entered this state. This is used
3715 * in the rcu update to wait only for active cpus. For system
3716 * which do not switch off the HZ timer nohz_cpu_mask should
3717 * always be CPU_MASK_NONE.
3719 cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
3723 * This is how migration works:
3725 * 1) we queue a migration_req_t structure in the source CPU's
3726 * runqueue and wake up that CPU's migration thread.
3727 * 2) we down() the locked semaphore => thread blocks.
3728 * 3) migration thread wakes up (implicitly it forces the migrated
3729 * thread off the CPU)
3730 * 4) it gets the migration request and checks whether the migrated
3731 * task is still in the wrong runqueue.
3732 * 5) if it's in the wrong runqueue then the migration thread removes
3733 * it and puts it into the right queue.
3734 * 6) migration thread up()s the semaphore.
3735 * 7) we wake up and the migration is done.
3739 * Change a given task's CPU affinity. Migrate the thread to a
3740 * proper CPU and schedule it away if the CPU it's executing on
3741 * is removed from the allowed bitmask.
3743 * NOTE: the caller must have a valid reference to the task, the
3744 * task must not exit() & deallocate itself prematurely. The
3745 * call is not atomic; no spinlocks may be held.
3747 int set_cpus_allowed(task_t *p, cpumask_t new_mask)
3749 unsigned long flags;
3751 migration_req_t req;
3754 rq = task_rq_lock(p, &flags);
3755 if (!cpus_intersects(new_mask, cpu_online_map)) {
3760 p->cpus_allowed = new_mask;
3761 /* Can the task run on the task's current CPU? If so, we're done */
3762 if (cpu_isset(task_cpu(p), new_mask))
3765 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
3766 /* Need help from migration thread: drop lock and wait. */
3767 task_rq_unlock(rq, &flags);
3768 wake_up_process(rq->migration_thread);
3769 wait_for_completion(&req.done);
3770 tlb_migrate_finish(p->mm);
3774 task_rq_unlock(rq, &flags);
3778 EXPORT_SYMBOL_GPL(set_cpus_allowed);
3781 * Move (not current) task off this cpu, onto dest cpu. We're doing
3782 * this because either it can't run here any more (set_cpus_allowed()
3783 * away from this CPU, or CPU going down), or because we're
3784 * attempting to rebalance this task on exec (sched_exec).
3786 * So we race with normal scheduler movements, but that's OK, as long
3787 * as the task is no longer on this CPU.
3789 static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
3791 runqueue_t *rq_dest, *rq_src;
3793 if (unlikely(cpu_is_offline(dest_cpu)))
3796 rq_src = cpu_rq(src_cpu);
3797 rq_dest = cpu_rq(dest_cpu);
3799 double_rq_lock(rq_src, rq_dest);
3800 /* Already moved. */
3801 if (task_cpu(p) != src_cpu)
3803 /* Affinity changed (again). */
3804 if (!cpu_isset(dest_cpu, p->cpus_allowed))
3807 set_task_cpu(p, dest_cpu);
3810 * Sync timestamp with rq_dest's before activating.
3811 * The same thing could be achieved by doing this step
3812 * afterwards, and pretending it was a local activate.
3813 * This way is cleaner and logically correct.
3815 p->timestamp = p->timestamp - rq_src->timestamp_last_tick
3816 + rq_dest->timestamp_last_tick;
3817 deactivate_task(p, rq_src);
3818 activate_task(p, rq_dest, 0);
3819 if (TASK_PREEMPTS_CURR(p, rq_dest))
3820 resched_task(rq_dest->curr);
3824 double_rq_unlock(rq_src, rq_dest);
3828 * migration_thread - this is a highprio system thread that performs
3829 * thread migration by bumping thread off CPU then 'pushing' onto
3832 static int migration_thread(void * data)
3835 int cpu = (long)data;
3838 BUG_ON(rq->migration_thread != current);
3840 set_current_state(TASK_INTERRUPTIBLE);
3841 while (!kthread_should_stop()) {
3842 struct list_head *head;
3843 migration_req_t *req;
3845 if (current->flags & PF_FREEZE)
3846 refrigerator(PF_FREEZE);
3848 spin_lock_irq(&rq->lock);
3850 if (cpu_is_offline(cpu)) {
3851 spin_unlock_irq(&rq->lock);
3855 if (rq->active_balance) {
3856 active_load_balance(rq, cpu);
3857 rq->active_balance = 0;
3860 head = &rq->migration_queue;
3862 if (list_empty(head)) {
3863 spin_unlock_irq(&rq->lock);
3865 set_current_state(TASK_INTERRUPTIBLE);
3868 req = list_entry(head->next, migration_req_t, list);
3869 list_del_init(head->next);
3871 if (req->type == REQ_MOVE_TASK) {
3872 spin_unlock(&rq->lock);
3873 __migrate_task(req->task, smp_processor_id(),
3876 } else if (req->type == REQ_SET_DOMAIN) {
3878 spin_unlock_irq(&rq->lock);
3880 spin_unlock_irq(&rq->lock);
3884 complete(&req->done);
3886 __set_current_state(TASK_RUNNING);
3890 /* Wait for kthread_stop */
3891 set_current_state(TASK_INTERRUPTIBLE);
3892 while (!kthread_should_stop()) {
3894 set_current_state(TASK_INTERRUPTIBLE);
3896 __set_current_state(TASK_RUNNING);
3900 #ifdef CONFIG_HOTPLUG_CPU
3901 /* Figure out where task on dead CPU should go, use force if neccessary. */
3902 static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
3908 mask = node_to_cpumask(cpu_to_node(dead_cpu));
3909 cpus_and(mask, mask, tsk->cpus_allowed);
3910 dest_cpu = any_online_cpu(mask);
3912 /* On any allowed CPU? */
3913 if (dest_cpu == NR_CPUS)
3914 dest_cpu = any_online_cpu(tsk->cpus_allowed);
3916 /* No more Mr. Nice Guy. */
3917 if (dest_cpu == NR_CPUS) {
3918 cpus_setall(tsk->cpus_allowed);
3919 dest_cpu = any_online_cpu(tsk->cpus_allowed);
3922 * Don't tell them about moving exiting tasks or
3923 * kernel threads (both mm NULL), since they never
3926 if (tsk->mm && printk_ratelimit())
3927 printk(KERN_INFO "process %d (%s) no "
3928 "longer affine to cpu%d\n",
3929 tsk->pid, tsk->comm, dead_cpu);
3931 __migrate_task(tsk, dead_cpu, dest_cpu);
3935 * While a dead CPU has no uninterruptible tasks queued at this point,
3936 * it might still have a nonzero ->nr_uninterruptible counter, because
3937 * for performance reasons the counter is not stricly tracking tasks to
3938 * their home CPUs. So we just add the counter to another CPU's counter,
3939 * to keep the global sum constant after CPU-down:
3941 static void migrate_nr_uninterruptible(runqueue_t *rq_src)
3943 runqueue_t *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
3944 unsigned long flags;
3946 local_irq_save(flags);
3947 double_rq_lock(rq_src, rq_dest);
3948 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
3949 rq_src->nr_uninterruptible = 0;
3950 double_rq_unlock(rq_src, rq_dest);
3951 local_irq_restore(flags);
3954 /* Run through task list and migrate tasks from the dead cpu. */
3955 static void migrate_live_tasks(int src_cpu)
3957 struct task_struct *tsk, *t;
3959 write_lock_irq(&tasklist_lock);
3961 do_each_thread(t, tsk) {
3965 if (task_cpu(tsk) == src_cpu)
3966 move_task_off_dead_cpu(src_cpu, tsk);
3967 } while_each_thread(t, tsk);
3969 write_unlock_irq(&tasklist_lock);
3972 /* Schedules idle task to be the next runnable task on current CPU.
3973 * It does so by boosting its priority to highest possible and adding it to
3974 * the _front_ of runqueue. Used by CPU offline code.
3976 void sched_idle_next(void)
3978 int cpu = smp_processor_id();
3979 runqueue_t *rq = this_rq();
3980 struct task_struct *p = rq->idle;
3981 unsigned long flags;
3983 /* cpu has to be offline */
3984 BUG_ON(cpu_online(cpu));
3986 /* Strictly not necessary since rest of the CPUs are stopped by now
3987 * and interrupts disabled on current cpu.
3989 spin_lock_irqsave(&rq->lock, flags);
3991 __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
3992 /* Add idle task to _front_ of it's priority queue */
3993 __activate_idle_task(p, rq);
3995 spin_unlock_irqrestore(&rq->lock, flags);
3998 static void migrate_dead(unsigned int dead_cpu, task_t *tsk)
4000 struct runqueue *rq = cpu_rq(dead_cpu);
4002 /* Must be exiting, otherwise would be on tasklist. */
4003 BUG_ON(tsk->exit_state != EXIT_ZOMBIE && tsk->exit_state != EXIT_DEAD);
4005 /* Cannot have done final schedule yet: would have vanished. */
4006 BUG_ON(tsk->flags & PF_DEAD);
4008 get_task_struct(tsk);
4011 * Drop lock around migration; if someone else moves it,
4012 * that's OK. No task can be added to this CPU, so iteration is
4015 spin_unlock_irq(&rq->lock);
4016 move_task_off_dead_cpu(dead_cpu, tsk);
4017 spin_lock_irq(&rq->lock);
4019 put_task_struct(tsk);
4022 /* release_task() removes task from tasklist, so we won't find dead tasks. */
4023 static void migrate_dead_tasks(unsigned int dead_cpu)
4026 struct runqueue *rq = cpu_rq(dead_cpu);
4028 for (arr = 0; arr < 2; arr++) {
4029 for (i = 0; i < MAX_PRIO; i++) {
4030 struct list_head *list = &rq->arrays[arr].queue[i];
4031 while (!list_empty(list))
4032 migrate_dead(dead_cpu,
4033 list_entry(list->next, task_t,
4038 #endif /* CONFIG_HOTPLUG_CPU */
4041 * migration_call - callback that gets triggered when a CPU is added.
4042 * Here we can start up the necessary migration thread for the new CPU.
4044 static int migration_call(struct notifier_block *nfb, unsigned long action,
4047 int cpu = (long)hcpu;
4048 struct task_struct *p;
4049 struct runqueue *rq;
4050 unsigned long flags;
4053 case CPU_UP_PREPARE:
4054 p = kthread_create(migration_thread, hcpu, "migration/%d",cpu);
4057 p->flags |= PF_NOFREEZE;
4058 kthread_bind(p, cpu);
4059 /* Must be high prio: stop_machine expects to yield to it. */
4060 rq = task_rq_lock(p, &flags);
4061 __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
4062 task_rq_unlock(rq, &flags);
4063 cpu_rq(cpu)->migration_thread = p;
4066 /* Strictly unneccessary, as first user will wake it. */
4067 wake_up_process(cpu_rq(cpu)->migration_thread);
4069 #ifdef CONFIG_HOTPLUG_CPU
4070 case CPU_UP_CANCELED:
4071 /* Unbind it from offline cpu so it can run. Fall thru. */
4072 kthread_bind(cpu_rq(cpu)->migration_thread,smp_processor_id());
4073 kthread_stop(cpu_rq(cpu)->migration_thread);
4074 cpu_rq(cpu)->migration_thread = NULL;
4077 migrate_live_tasks(cpu);
4079 kthread_stop(rq->migration_thread);
4080 rq->migration_thread = NULL;
4081 /* Idle task back to normal (off runqueue, low prio) */
4082 rq = task_rq_lock(rq->idle, &flags);
4083 deactivate_task(rq->idle, rq);
4084 rq->idle->static_prio = MAX_PRIO;
4085 __setscheduler(rq->idle, SCHED_NORMAL, 0);
4086 migrate_dead_tasks(cpu);
4087 task_rq_unlock(rq, &flags);
4088 migrate_nr_uninterruptible(rq);
4089 BUG_ON(rq->nr_running != 0);
4091 /* No need to migrate the tasks: it was best-effort if
4092 * they didn't do lock_cpu_hotplug(). Just wake up
4093 * the requestors. */
4094 spin_lock_irq(&rq->lock);
4095 while (!list_empty(&rq->migration_queue)) {
4096 migration_req_t *req;
4097 req = list_entry(rq->migration_queue.next,
4098 migration_req_t, list);
4099 BUG_ON(req->type != REQ_MOVE_TASK);
4100 list_del_init(&req->list);
4101 complete(&req->done);
4103 spin_unlock_irq(&rq->lock);
4110 /* Register at highest priority so that task migration (migrate_all_tasks)
4111 * happens before everything else.
4113 static struct notifier_block __devinitdata migration_notifier = {
4114 .notifier_call = migration_call,
4118 int __init migration_init(void)
4120 void *cpu = (void *)(long)smp_processor_id();
4121 /* Start one for boot CPU. */
4122 migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
4123 migration_call(&migration_notifier, CPU_ONLINE, cpu);
4124 register_cpu_notifier(&migration_notifier);
4131 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
4132 * hold the hotplug lock.
4134 void __devinit cpu_attach_domain(struct sched_domain *sd, int cpu)
4136 migration_req_t req;
4137 unsigned long flags;
4138 runqueue_t *rq = cpu_rq(cpu);
4141 spin_lock_irqsave(&rq->lock, flags);
4143 if (cpu == smp_processor_id() || !cpu_online(cpu)) {
4146 init_completion(&req.done);
4147 req.type = REQ_SET_DOMAIN;
4149 list_add(&req.list, &rq->migration_queue);
4153 spin_unlock_irqrestore(&rq->lock, flags);
4156 wake_up_process(rq->migration_thread);
4157 wait_for_completion(&req.done);
4161 /* cpus with isolated domains */
4162 cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE;
4164 /* Setup the mask of cpus configured for isolated domains */
4165 static int __init isolated_cpu_setup(char *str)
4167 int ints[NR_CPUS], i;
4169 str = get_options(str, ARRAY_SIZE(ints), ints);
4170 cpus_clear(cpu_isolated_map);
4171 for (i = 1; i <= ints[0]; i++)
4172 cpu_set(ints[i], cpu_isolated_map);
4176 __setup ("isolcpus=", isolated_cpu_setup);
4179 * init_sched_build_groups takes an array of groups, the cpumask we wish
4180 * to span, and a pointer to a function which identifies what group a CPU
4181 * belongs to. The return value of group_fn must be a valid index into the
4182 * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we
4183 * keep track of groups covered with a cpumask_t).
4185 * init_sched_build_groups will build a circular linked list of the groups
4186 * covered by the given span, and will set each group's ->cpumask correctly,
4187 * and ->cpu_power to 0.
4189 void __devinit init_sched_build_groups(struct sched_group groups[],
4190 cpumask_t span, int (*group_fn)(int cpu))
4192 struct sched_group *first = NULL, *last = NULL;
4193 cpumask_t covered = CPU_MASK_NONE;
4196 for_each_cpu_mask(i, span) {
4197 int group = group_fn(i);
4198 struct sched_group *sg = &groups[group];
4201 if (cpu_isset(i, covered))
4204 sg->cpumask = CPU_MASK_NONE;
4207 for_each_cpu_mask(j, span) {
4208 if (group_fn(j) != group)
4211 cpu_set(j, covered);
4212 cpu_set(j, sg->cpumask);
4224 #ifdef ARCH_HAS_SCHED_DOMAIN
4225 extern void __devinit arch_init_sched_domains(void);
4226 extern void __devinit arch_destroy_sched_domains(void);
4228 #ifdef CONFIG_SCHED_SMT
4229 static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
4230 static struct sched_group sched_group_cpus[NR_CPUS];
4231 static int __devinit cpu_to_cpu_group(int cpu)
4237 static DEFINE_PER_CPU(struct sched_domain, phys_domains);
4238 static struct sched_group sched_group_phys[NR_CPUS];
4239 static int __devinit cpu_to_phys_group(int cpu)
4241 #ifdef CONFIG_SCHED_SMT
4242 return first_cpu(cpu_sibling_map[cpu]);
4250 static DEFINE_PER_CPU(struct sched_domain, node_domains);
4251 static struct sched_group sched_group_nodes[MAX_NUMNODES];
4252 static int __devinit cpu_to_node_group(int cpu)
4254 return cpu_to_node(cpu);
4258 #if defined(CONFIG_SCHED_SMT) && defined(CONFIG_NUMA)
4260 * The domains setup code relies on siblings not spanning
4261 * multiple nodes. Make sure the architecture has a proper
4264 static void check_sibling_maps(void)
4268 for_each_online_cpu(i) {
4269 for_each_cpu_mask(j, cpu_sibling_map[i]) {
4270 if (cpu_to_node(i) != cpu_to_node(j)) {
4271 printk(KERN_INFO "warning: CPU %d siblings map "
4272 "to different node - isolating "
4274 cpu_sibling_map[i] = cpumask_of_cpu(i);
4283 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
4285 static void __devinit arch_init_sched_domains(void)
4288 cpumask_t cpu_default_map;
4290 #if defined(CONFIG_SCHED_SMT) && defined(CONFIG_NUMA)
4291 check_sibling_maps();
4294 * Setup mask for cpus without special case scheduling requirements.
4295 * For now this just excludes isolated cpus, but could be used to
4296 * exclude other special cases in the future.
4298 cpus_complement(cpu_default_map, cpu_isolated_map);
4299 cpus_and(cpu_default_map, cpu_default_map, cpu_online_map);
4302 * Set up domains. Isolated domains just stay on the dummy domain.
4304 for_each_cpu_mask(i, cpu_default_map) {
4306 struct sched_domain *sd = NULL, *p;
4307 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
4309 cpus_and(nodemask, nodemask, cpu_default_map);
4312 sd = &per_cpu(node_domains, i);
4313 group = cpu_to_node_group(i);
4315 sd->span = cpu_default_map;
4316 sd->groups = &sched_group_nodes[group];
4320 sd = &per_cpu(phys_domains, i);
4321 group = cpu_to_phys_group(i);
4323 sd->span = nodemask;
4325 sd->groups = &sched_group_phys[group];
4327 #ifdef CONFIG_SCHED_SMT
4329 sd = &per_cpu(cpu_domains, i);
4330 group = cpu_to_cpu_group(i);
4331 *sd = SD_SIBLING_INIT;
4332 sd->span = cpu_sibling_map[i];
4333 cpus_and(sd->span, sd->span, cpu_default_map);
4335 sd->groups = &sched_group_cpus[group];
4339 #ifdef CONFIG_SCHED_SMT
4340 /* Set up CPU (sibling) groups */
4341 for_each_online_cpu(i) {
4342 cpumask_t this_sibling_map = cpu_sibling_map[i];
4343 cpus_and(this_sibling_map, this_sibling_map, cpu_default_map);
4344 if (i != first_cpu(this_sibling_map))
4347 init_sched_build_groups(sched_group_cpus, this_sibling_map,
4352 /* Set up physical groups */
4353 for (i = 0; i < MAX_NUMNODES; i++) {
4354 cpumask_t nodemask = node_to_cpumask(i);
4356 cpus_and(nodemask, nodemask, cpu_default_map);
4357 if (cpus_empty(nodemask))
4360 init_sched_build_groups(sched_group_phys, nodemask,
4361 &cpu_to_phys_group);
4365 /* Set up node groups */
4366 init_sched_build_groups(sched_group_nodes, cpu_default_map,
4367 &cpu_to_node_group);
4370 /* Calculate CPU power for physical packages and nodes */
4371 for_each_cpu_mask(i, cpu_default_map) {
4373 struct sched_domain *sd;
4374 #ifdef CONFIG_SCHED_SMT
4375 sd = &per_cpu(cpu_domains, i);
4376 power = SCHED_LOAD_SCALE;
4377 sd->groups->cpu_power = power;
4380 sd = &per_cpu(phys_domains, i);
4381 power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE *
4382 (cpus_weight(sd->groups->cpumask)-1) / 10;
4383 sd->groups->cpu_power = power;
4386 if (i == first_cpu(sd->groups->cpumask)) {
4387 /* Only add "power" once for each physical package. */
4388 sd = &per_cpu(node_domains, i);
4389 sd->groups->cpu_power += power;
4394 /* Attach the domains */
4395 for_each_online_cpu(i) {
4396 struct sched_domain *sd;
4397 #ifdef CONFIG_SCHED_SMT
4398 sd = &per_cpu(cpu_domains, i);
4400 sd = &per_cpu(phys_domains, i);
4402 cpu_attach_domain(sd, i);
4406 #ifdef CONFIG_HOTPLUG_CPU
4407 static void __devinit arch_destroy_sched_domains(void)
4409 /* Do nothing: everything is statically allocated. */
4413 #endif /* ARCH_HAS_SCHED_DOMAIN */
4415 #define SCHED_DOMAIN_DEBUG
4416 #ifdef SCHED_DOMAIN_DEBUG
4417 static void sched_domain_debug(void)
4421 for_each_online_cpu(i) {
4422 runqueue_t *rq = cpu_rq(i);
4423 struct sched_domain *sd;
4428 printk(KERN_DEBUG "CPU%d:\n", i);
4433 struct sched_group *group = sd->groups;
4434 cpumask_t groupmask;
4436 cpumask_scnprintf(str, NR_CPUS, sd->span);
4437 cpus_clear(groupmask);
4440 for (j = 0; j < level + 1; j++)
4442 printk("domain %d: ", level);
4444 if (!(sd->flags & SD_LOAD_BALANCE)) {
4445 printk("does not load-balance");
4447 printk(" ERROR !SD_LOAD_BALANCE domain has parent");
4452 printk("span %s\n", str);
4454 if (!cpu_isset(i, sd->span))
4455 printk(KERN_DEBUG "ERROR domain->span does not contain CPU%d\n", i);
4456 if (!cpu_isset(i, group->cpumask))
4457 printk(KERN_DEBUG "ERROR domain->groups does not contain CPU%d\n", i);
4460 for (j = 0; j < level + 2; j++)
4465 printk(" ERROR: NULL");
4469 if (!group->cpu_power)
4470 printk(KERN_DEBUG "ERROR group->cpu_power not set\n");
4472 if (!cpus_weight(group->cpumask))
4473 printk(" ERROR empty group:");
4475 if (cpus_intersects(groupmask, group->cpumask))
4476 printk(" ERROR repeated CPUs:");
4478 cpus_or(groupmask, groupmask, group->cpumask);
4480 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
4483 group = group->next;
4484 } while (group != sd->groups);
4487 if (!cpus_equal(sd->span, groupmask))
4488 printk(KERN_DEBUG "ERROR groups don't span domain->span\n");
4494 if (!cpus_subset(groupmask, sd->span))
4495 printk(KERN_DEBUG "ERROR parent span is not a superset of domain->span\n");
4502 #define sched_domain_debug() {}
4506 * Initial dummy domain for early boot and for hotplug cpu. Being static,
4507 * it is initialized to zero, so all balancing flags are cleared which is
4510 static struct sched_domain sched_domain_dummy;
4512 #ifdef CONFIG_HOTPLUG_CPU
4514 * Force a reinitialization of the sched domains hierarchy. The domains
4515 * and groups cannot be updated in place without racing with the balancing
4516 * code, so we temporarily attach all running cpus to a "dummy" domain
4517 * which will prevent rebalancing while the sched domains are recalculated.
4519 static int update_sched_domains(struct notifier_block *nfb,
4520 unsigned long action, void *hcpu)
4525 case CPU_UP_PREPARE:
4526 case CPU_DOWN_PREPARE:
4527 for_each_online_cpu(i)
4528 cpu_attach_domain(&sched_domain_dummy, i);
4529 arch_destroy_sched_domains();
4532 case CPU_UP_CANCELED:
4533 case CPU_DOWN_FAILED:
4537 * Fall through and re-initialise the domains.
4544 /* The hotplug lock is already held by cpu_up/cpu_down */
4545 arch_init_sched_domains();
4547 sched_domain_debug();
4553 void __init sched_init_smp(void)
4556 arch_init_sched_domains();
4557 sched_domain_debug();
4558 unlock_cpu_hotplug();
4559 /* XXX: Theoretical race here - CPU may be hotplugged now */
4560 hotcpu_notifier(update_sched_domains, 0);
4563 void __init sched_init_smp(void)
4566 #endif /* CONFIG_SMP */
4568 int in_sched_functions(unsigned long addr)
4570 /* Linker adds these: start and end of __sched functions */
4571 extern char __sched_text_start[], __sched_text_end[];
4572 return in_lock_functions(addr) ||
4573 (addr >= (unsigned long)__sched_text_start
4574 && addr < (unsigned long)__sched_text_end);
4577 void __init sched_init(void)
4582 for (i = 0; i < NR_CPUS; i++) {
4583 prio_array_t *array;
4586 spin_lock_init(&rq->lock);
4587 rq->active = rq->arrays;
4588 rq->expired = rq->arrays + 1;
4589 rq->best_expired_prio = MAX_PRIO;
4592 rq->sd = &sched_domain_dummy;
4594 rq->active_balance = 0;
4596 rq->migration_thread = NULL;
4597 INIT_LIST_HEAD(&rq->migration_queue);
4599 atomic_set(&rq->nr_iowait, 0);
4601 for (j = 0; j < 2; j++) {
4602 array = rq->arrays + j;
4603 for (k = 0; k < MAX_PRIO; k++) {
4604 INIT_LIST_HEAD(array->queue + k);
4605 __clear_bit(k, array->bitmap);
4607 // delimiter for bitsearch
4608 __set_bit(MAX_PRIO, array->bitmap);
4613 * The boot idle thread does lazy MMU switching as well:
4615 atomic_inc(&init_mm.mm_count);
4616 enter_lazy_tlb(&init_mm, current);
4619 * Make us the idle thread. Technically, schedule() should not be
4620 * called from this thread, however somewhere below it might be,
4621 * but because we are the idle thread, we just pick up running again
4622 * when this runqueue becomes "idle".
4624 init_idle(current, smp_processor_id());
4627 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4628 void __might_sleep(char *file, int line)
4630 #if defined(in_atomic)
4631 static unsigned long prev_jiffy; /* ratelimiting */
4633 if ((in_atomic() || irqs_disabled()) &&
4634 system_state == SYSTEM_RUNNING) {
4635 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
4637 prev_jiffy = jiffies;
4638 printk(KERN_ERR "Debug: sleeping function called from invalid"
4639 " context at %s:%d\n", file, line);
4640 printk("in_atomic():%d, irqs_disabled():%d\n",
4641 in_atomic(), irqs_disabled());
4646 EXPORT_SYMBOL(__might_sleep);
4649 #ifdef CONFIG_MAGIC_SYSRQ
4650 void normalize_rt_tasks(void)
4652 struct task_struct *p;
4653 prio_array_t *array;
4654 unsigned long flags;
4657 read_lock_irq(&tasklist_lock);
4658 for_each_process (p) {
4662 rq = task_rq_lock(p, &flags);
4666 deactivate_task(p, task_rq(p));
4667 __setscheduler(p, SCHED_NORMAL, 0);
4669 __activate_task(p, task_rq(p));
4670 resched_task(rq->curr);
4673 task_rq_unlock(rq, &flags);
4675 read_unlock_irq(&tasklist_lock);
4678 #endif /* CONFIG_MAGIC_SYSRQ */