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
21 #include <linux/module.h>
22 #include <linux/nmi.h>
23 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/highmem.h>
26 #include <linux/smp_lock.h>
27 #include <asm/mmu_context.h>
28 #include <linux/interrupt.h>
29 #include <linux/completion.h>
30 #include <linux/kernel_stat.h>
31 #include <linux/security.h>
32 #include <linux/notifier.h>
33 #include <linux/profile.h>
34 #include <linux/suspend.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/smp.h>
38 #include <linux/timer.h>
39 #include <linux/rcupdate.h>
40 #include <linux/cpu.h>
41 #include <linux/percpu.h>
42 #include <linux/kthread.h>
43 #include <linux/seq_file.h>
44 #include <linux/times.h>
45 #include <linux/vserver/sched.h>
46 #include <linux/vs_base.h>
47 #include <linux/vs_context.h>
48 #include <linux/vs_cvirt.h>
51 #include <asm/unistd.h>
54 #define cpu_to_node_mask(cpu) node_to_cpumask(cpu_to_node(cpu))
56 #define cpu_to_node_mask(cpu) (cpu_online_map)
59 /* used to soft spin in sched while dump is in progress */
60 unsigned long dump_oncpu;
61 EXPORT_SYMBOL(dump_oncpu);
64 * Convert user-nice values [ -20 ... 0 ... 19 ]
65 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
68 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
69 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
70 #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
73 * 'User priority' is the nice value converted to something we
74 * can work with better when scaling various scheduler parameters,
75 * it's a [ 0 ... 39 ] range.
77 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
78 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
79 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
82 * Some helpers for converting nanosecond timing to jiffy resolution
84 #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
85 #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
88 * These are the 'tuning knobs' of the scheduler:
90 * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
91 * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
92 * Timeslices get refilled after they expire.
94 #define MIN_TIMESLICE max(5 * HZ / 1000, 1)
95 #define DEF_TIMESLICE (100 * HZ / 1000)
96 #define ON_RUNQUEUE_WEIGHT 30
97 #define CHILD_PENALTY 95
98 #define PARENT_PENALTY 100
100 #define PRIO_BONUS_RATIO 25
101 #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100)
102 #define INTERACTIVE_DELTA 2
103 #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS)
104 #define STARVATION_LIMIT (MAX_SLEEP_AVG)
105 #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG))
106 #define CREDIT_LIMIT 100
109 * If a task is 'interactive' then we reinsert it in the active
110 * array after it has expired its current timeslice. (it will not
111 * continue to run immediately, it will still roundrobin with
112 * other interactive tasks.)
114 * This part scales the interactivity limit depending on niceness.
116 * We scale it linearly, offset by the INTERACTIVE_DELTA delta.
117 * Here are a few examples of different nice levels:
119 * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0]
120 * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0]
121 * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0]
122 * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0]
123 * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0]
125 * (the X axis represents the possible -5 ... 0 ... +5 dynamic
126 * priority range a task can explore, a value of '1' means the
127 * task is rated interactive.)
129 * Ie. nice +19 tasks can never get 'interactive' enough to be
130 * reinserted into the active array. And only heavily CPU-hog nice -20
131 * tasks will be expired. Default nice 0 tasks are somewhere between,
132 * it takes some effort for them to get interactive, but it's not
136 #define CURRENT_BONUS(p) \
137 (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \
141 #define TIMESLICE_GRANULARITY(p) (MIN_TIMESLICE * \
142 (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \
145 #define TIMESLICE_GRANULARITY(p) (MIN_TIMESLICE * \
146 (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)))
149 #define SCALE(v1,v1_max,v2_max) \
150 (v1) * (v2_max) / (v1_max)
153 (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA)
155 #define TASK_INTERACTIVE(p) \
156 ((p)->prio <= (p)->static_prio - DELTA(p))
158 #define INTERACTIVE_SLEEP(p) \
159 (JIFFIES_TO_NS(MAX_SLEEP_AVG * \
160 (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1))
162 #define HIGH_CREDIT(p) \
163 ((p)->interactive_credit > CREDIT_LIMIT)
165 #define LOW_CREDIT(p) \
166 ((p)->interactive_credit < -CREDIT_LIMIT)
168 #ifdef CONFIG_CKRM_CPU_SCHEDULE
170 * if belong to different class, compare class priority
171 * otherwise compare task priority
173 #define TASK_PREEMPTS_CURR(p, rq) \
174 ( ((p)->cpu_class != (rq)->curr->cpu_class) \
175 && ((rq)->curr != (rq)->idle) && ((p) != (rq)->idle )) \
176 ? class_preempts_curr((p),(rq)->curr) \
177 : ((p)->prio < (rq)->curr->prio)
179 #define TASK_PREEMPTS_CURR(p, rq) \
180 ((p)->prio < (rq)->curr->prio)
184 * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
185 * to time slice values: [800ms ... 100ms ... 5ms]
187 * The higher a thread's priority, the bigger timeslices
188 * it gets during one round of execution. But even the lowest
189 * priority thread gets MIN_TIMESLICE worth of execution time.
192 #define SCALE_PRIO(x, prio) \
193 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE)
195 unsigned int task_timeslice(task_t *p)
197 if (p->static_prio < NICE_TO_PRIO(0))
198 return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio);
200 return SCALE_PRIO(DEF_TIMESLICE, p->static_prio);
202 #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \
203 < (long long) (sd)->cache_hot_time)
216 * These are the runqueue data structures:
219 typedef struct runqueue runqueue_t;
220 #include <linux/ckrm_classqueue.h>
221 #include <linux/ckrm_sched.h>
224 * This is the main, per-CPU runqueue data structure.
226 * Locking rule: those places that want to lock multiple runqueues
227 * (such as the load balancing or the thread migration code), lock
228 * acquire operations must be ordered by ascending &runqueue.
234 * nr_running and cpu_load should be in the same cacheline because
235 * remote CPUs use both these fields when doing load calculation.
237 unsigned long nr_running;
239 unsigned long cpu_load;
241 unsigned long long nr_switches;
242 unsigned long expired_timestamp, nr_uninterruptible;
243 unsigned long long timestamp_last_tick;
245 struct mm_struct *prev_mm;
246 #ifdef CONFIG_CKRM_CPU_SCHEDULE
247 struct classqueue_struct classqueue;
248 ckrm_load_t ckrm_load;
250 prio_array_t *active, *expired, arrays[2];
252 int best_expired_prio;
256 struct sched_domain *sd;
258 /* For active balancing */
262 task_t *migration_thread;
263 struct list_head migration_queue;
266 #ifdef CONFIG_VSERVER_HARDCPU
267 struct list_head hold_queue;
271 #ifdef CONFIG_SCHEDSTATS
273 struct sched_info rq_sched_info;
275 /* sys_sched_yield() stats */
276 unsigned long yld_exp_empty;
277 unsigned long yld_act_empty;
278 unsigned long yld_both_empty;
279 unsigned long yld_cnt;
281 /* schedule() stats */
282 unsigned long sched_noswitch;
283 unsigned long sched_switch;
284 unsigned long sched_cnt;
285 unsigned long sched_goidle;
287 /* pull_task() stats */
288 unsigned long pt_gained[MAX_IDLE_TYPES];
289 unsigned long pt_lost[MAX_IDLE_TYPES];
291 /* active_load_balance() stats */
292 unsigned long alb_cnt;
293 unsigned long alb_lost;
294 unsigned long alb_gained;
295 unsigned long alb_failed;
297 /* try_to_wake_up() stats */
298 unsigned long ttwu_cnt;
299 unsigned long ttwu_attempts;
300 unsigned long ttwu_moved;
302 /* wake_up_new_task() stats */
303 unsigned long wunt_cnt;
304 unsigned long wunt_moved;
306 /* sched_migrate_task() stats */
307 unsigned long smt_cnt;
309 /* sched_balance_exec() stats */
310 unsigned long sbe_cnt;
314 static DEFINE_PER_CPU(struct runqueue, runqueues);
317 * sched-domains (multiprocessor balancing) declarations:
320 #define SCHED_LOAD_SCALE 128UL /* increase resolution of load */
322 #define SD_BALANCE_NEWIDLE 1 /* Balance when about to become idle */
323 #define SD_BALANCE_EXEC 2 /* Balance on exec */
324 #define SD_WAKE_IDLE 4 /* Wake to idle CPU on task wakeup */
325 #define SD_WAKE_AFFINE 8 /* Wake task to waking CPU */
326 #define SD_WAKE_BALANCE 16 /* Perform balancing at task wakeup */
327 #define SD_SHARE_CPUPOWER 32 /* Domain members share cpu power */
330 struct sched_group *next; /* Must be a circular list */
334 * CPU power of this group, SCHED_LOAD_SCALE being max power for a
335 * single CPU. This is read only (except for setup, hotplug CPU).
337 unsigned long cpu_power;
340 struct sched_domain {
341 /* These fields must be setup */
342 struct sched_domain *parent; /* top domain must be null terminated */
343 struct sched_group *groups; /* the balancing groups of the domain */
344 cpumask_t span; /* span of all CPUs in this domain */
345 unsigned long min_interval; /* Minimum balance interval ms */
346 unsigned long max_interval; /* Maximum balance interval ms */
347 unsigned int busy_factor; /* less balancing by factor if busy */
348 unsigned int imbalance_pct; /* No balance until over watermark */
349 unsigned long long cache_hot_time; /* Task considered cache hot (ns) */
350 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
351 unsigned int per_cpu_gain; /* CPU % gained by adding domain cpus */
352 int flags; /* See SD_* */
354 /* Runtime fields. */
355 unsigned long last_balance; /* init to jiffies. units in jiffies */
356 unsigned int balance_interval; /* initialise to 1. units in ms. */
357 unsigned int nr_balance_failed; /* initialise to 0 */
359 #ifdef CONFIG_SCHEDSTATS
360 /* load_balance() stats */
361 unsigned long lb_cnt[MAX_IDLE_TYPES];
362 unsigned long lb_failed[MAX_IDLE_TYPES];
363 unsigned long lb_imbalance[MAX_IDLE_TYPES];
364 unsigned long lb_nobusyg[MAX_IDLE_TYPES];
365 unsigned long lb_nobusyq[MAX_IDLE_TYPES];
367 /* sched_balance_exec() stats */
368 unsigned long sbe_attempts;
369 unsigned long sbe_pushed;
371 /* try_to_wake_up() stats */
372 unsigned long ttwu_wake_affine;
373 unsigned long ttwu_wake_balance;
377 #ifndef ARCH_HAS_SCHED_TUNE
378 #ifdef CONFIG_SCHED_SMT
379 #define ARCH_HAS_SCHED_WAKE_IDLE
380 /* Common values for SMT siblings */
381 #define SD_SIBLING_INIT (struct sched_domain) { \
382 .span = CPU_MASK_NONE, \
388 .imbalance_pct = 110, \
389 .cache_hot_time = 0, \
390 .cache_nice_tries = 0, \
391 .per_cpu_gain = 25, \
392 .flags = SD_BALANCE_NEWIDLE \
396 | SD_SHARE_CPUPOWER, \
397 .last_balance = jiffies, \
398 .balance_interval = 1, \
399 .nr_balance_failed = 0, \
403 /* Common values for CPUs */
404 #define SD_CPU_INIT (struct sched_domain) { \
405 .span = CPU_MASK_NONE, \
411 .imbalance_pct = 125, \
412 .cache_hot_time = cache_decay_ticks*1000000 ? : (5*1000000/2),\
413 .cache_nice_tries = 1, \
414 .per_cpu_gain = 100, \
415 .flags = SD_BALANCE_NEWIDLE \
419 .last_balance = jiffies, \
420 .balance_interval = 1, \
421 .nr_balance_failed = 0, \
424 /* Arch can override this macro in processor.h */
425 #if defined(CONFIG_NUMA) && !defined(SD_NODE_INIT)
426 #define SD_NODE_INIT (struct sched_domain) { \
427 .span = CPU_MASK_NONE, \
431 .max_interval = 32, \
433 .imbalance_pct = 125, \
434 .cache_hot_time = (10*1000000), \
435 .cache_nice_tries = 1, \
436 .per_cpu_gain = 100, \
437 .flags = SD_BALANCE_EXEC \
439 .last_balance = jiffies, \
440 .balance_interval = 1, \
441 .nr_balance_failed = 0, \
444 #endif /* ARCH_HAS_SCHED_TUNE */
448 #define for_each_domain(cpu, domain) \
449 for (domain = cpu_rq(cpu)->sd; domain; domain = domain->parent)
451 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
452 #define this_rq() (&__get_cpu_var(runqueues))
453 #define task_rq(p) cpu_rq(task_cpu(p))
454 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
457 * Default context-switch locking:
459 #ifndef prepare_arch_switch
460 # define prepare_arch_switch(rq, next) do { } while (0)
461 # define finish_arch_switch(rq, next) spin_unlock_irq(&(rq)->lock)
462 # define task_running(rq, p) ((rq)->curr == (p))
466 * task_rq_lock - lock the runqueue a given task resides on and disable
467 * interrupts. Note the ordering: we can safely lookup the task_rq without
468 * explicitly disabling preemption.
470 static runqueue_t *task_rq_lock(task_t *p, unsigned long *flags)
475 local_irq_save(*flags);
477 spin_lock(&rq->lock);
478 if (unlikely(rq != task_rq(p))) {
479 spin_unlock_irqrestore(&rq->lock, *flags);
480 goto repeat_lock_task;
485 static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags)
487 spin_unlock_irqrestore(&rq->lock, *flags);
490 #ifdef CONFIG_SCHEDSTATS
492 * bump this up when changing the output format or the meaning of an existing
493 * format, so that tools can adapt (or abort)
495 #define SCHEDSTAT_VERSION 10
497 static int show_schedstat(struct seq_file *seq, void *v)
500 enum idle_type itype;
502 seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION);
503 seq_printf(seq, "timestamp %lu\n", jiffies);
504 for_each_online_cpu(cpu) {
505 runqueue_t *rq = cpu_rq(cpu);
507 struct sched_domain *sd;
511 /* runqueue-specific stats */
513 "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
514 "%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
515 cpu, rq->yld_both_empty,
516 rq->yld_act_empty, rq->yld_exp_empty,
517 rq->yld_cnt, rq->sched_noswitch,
518 rq->sched_switch, rq->sched_cnt, rq->sched_goidle,
519 rq->alb_cnt, rq->alb_gained, rq->alb_lost,
521 rq->ttwu_cnt, rq->ttwu_moved, rq->ttwu_attempts,
522 rq->wunt_cnt, rq->wunt_moved,
523 rq->smt_cnt, rq->sbe_cnt, rq->rq_sched_info.cpu_time,
524 rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt);
526 for (itype = IDLE; itype < MAX_IDLE_TYPES; itype++)
527 seq_printf(seq, " %lu %lu", rq->pt_gained[itype],
529 seq_printf(seq, "\n");
532 /* domain-specific stats */
533 for_each_domain(cpu, sd) {
534 char mask_str[NR_CPUS];
536 cpumask_scnprintf(mask_str, NR_CPUS, sd->span);
537 seq_printf(seq, "domain%d %s", dcnt++, mask_str);
538 for (itype = IDLE; itype < MAX_IDLE_TYPES; itype++) {
539 seq_printf(seq, " %lu %lu %lu %lu %lu",
541 sd->lb_failed[itype],
542 sd->lb_imbalance[itype],
543 sd->lb_nobusyq[itype],
544 sd->lb_nobusyg[itype]);
546 seq_printf(seq, " %lu %lu %lu %lu\n",
547 sd->sbe_pushed, sd->sbe_attempts,
548 sd->ttwu_wake_affine, sd->ttwu_wake_balance);
555 static int schedstat_open(struct inode *inode, struct file *file)
557 unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32);
558 char *buf = kmalloc(size, GFP_KERNEL);
564 res = single_open(file, show_schedstat, NULL);
566 m = file->private_data;
574 struct file_operations proc_schedstat_operations = {
575 .open = schedstat_open,
578 .release = single_release,
581 # define schedstat_inc(rq, field) rq->field++;
582 # define schedstat_add(rq, field, amt) rq->field += amt;
583 #else /* !CONFIG_SCHEDSTATS */
584 # define schedstat_inc(rq, field) do { } while (0);
585 # define schedstat_add(rq, field, amt) do { } while (0);
589 * rq_lock - lock a given runqueue and disable interrupts.
591 static runqueue_t *this_rq_lock(void)
597 spin_lock(&rq->lock);
602 static inline void rq_unlock(runqueue_t *rq)
604 spin_unlock_irq(&rq->lock);
607 #ifdef CONFIG_SCHEDSTATS
609 * Called when a process is dequeued from the active array and given
610 * the cpu. We should note that with the exception of interactive
611 * tasks, the expired queue will become the active queue after the active
612 * queue is empty, without explicitly dequeuing and requeuing tasks in the
613 * expired queue. (Interactive tasks may be requeued directly to the
614 * active queue, thus delaying tasks in the expired queue from running;
615 * see scheduler_tick()).
617 * This function is only called from sched_info_arrive(), rather than
618 * dequeue_task(). Even though a task may be queued and dequeued multiple
619 * times as it is shuffled about, we're really interested in knowing how
620 * long it was from the *first* time it was queued to the time that it
623 static inline void sched_info_dequeued(task_t *t)
625 t->sched_info.last_queued = 0;
629 * Called when a task finally hits the cpu. We can now calculate how
630 * long it was waiting to run. We also note when it began so that we
631 * can keep stats on how long its timeslice is.
633 static inline void sched_info_arrive(task_t *t)
635 unsigned long now = jiffies, diff = 0;
636 struct runqueue *rq = task_rq(t);
638 if (t->sched_info.last_queued)
639 diff = now - t->sched_info.last_queued;
640 sched_info_dequeued(t);
641 t->sched_info.run_delay += diff;
642 t->sched_info.last_arrival = now;
643 t->sched_info.pcnt++;
648 rq->rq_sched_info.run_delay += diff;
649 rq->rq_sched_info.pcnt++;
653 * Called when a process is queued into either the active or expired
654 * array. The time is noted and later used to determine how long we
655 * had to wait for us to reach the cpu. Since the expired queue will
656 * become the active queue after active queue is empty, without dequeuing
657 * and requeuing any tasks, we are interested in queuing to either. It
658 * is unusual but not impossible for tasks to be dequeued and immediately
659 * requeued in the same or another array: this can happen in sched_yield(),
660 * set_user_nice(), and even load_balance() as it moves tasks from runqueue
663 * This function is only called from enqueue_task(), but also only updates
664 * the timestamp if it is already not set. It's assumed that
665 * sched_info_dequeued() will clear that stamp when appropriate.
667 static inline void sched_info_queued(task_t *t)
669 if (!t->sched_info.last_queued)
670 t->sched_info.last_queued = jiffies;
674 * Called when a process ceases being the active-running process, either
675 * voluntarily or involuntarily. Now we can calculate how long we ran.
677 static inline void sched_info_depart(task_t *t)
679 struct runqueue *rq = task_rq(t);
680 unsigned long diff = jiffies - t->sched_info.last_arrival;
682 t->sched_info.cpu_time += diff;
685 rq->rq_sched_info.cpu_time += diff;
689 * Called when tasks are switched involuntarily due, typically, to expiring
690 * their time slice. (This may also be called when switching to or from
691 * the idle task.) We are only called when prev != next.
693 static inline void sched_info_switch(task_t *prev, task_t *next)
695 struct runqueue *rq = task_rq(prev);
698 * prev now departs the cpu. It's not interesting to record
699 * stats about how efficient we were at scheduling the idle
702 if (prev != rq->idle)
703 sched_info_depart(prev);
705 if (next != rq->idle)
706 sched_info_arrive(next);
709 #define sched_info_queued(t) do { } while (0)
710 #define sched_info_switch(t, next) do { } while (0)
711 #endif /* CONFIG_SCHEDSTATS */
713 #ifdef CONFIG_CKRM_CPU_SCHEDULE
714 static inline ckrm_lrq_t *rq_get_next_class(struct runqueue *rq)
716 cq_node_t *node = classqueue_get_head(&rq->classqueue);
717 return ((node) ? class_list_entry(node) : NULL);
721 * return the cvt of the current running class
722 * if no current running class, return 0
723 * assume cpu is valid (cpu_online(cpu) == 1)
725 CVT_t get_local_cur_cvt(int cpu)
727 ckrm_lrq_t * lrq = rq_get_next_class(cpu_rq(cpu));
730 return lrq->local_cvt;
735 static inline struct task_struct * rq_get_next_task(struct runqueue* rq)
738 struct task_struct *next;
741 int cpu = smp_processor_id();
743 // it is guaranteed be the ( rq->nr_running > 0 ) check in
744 // schedule that a task will be found.
747 queue = rq_get_next_class(rq);
750 array = queue->active;
751 if (unlikely(!array->nr_active)) {
752 queue->active = queue->expired;
753 queue->expired = array;
754 queue->expired_timestamp = 0;
756 schedstat_inc(rq, sched_switch);
757 if (queue->active->nr_active)
758 set_top_priority(queue,
759 find_first_bit(queue->active->bitmap, MAX_PRIO));
761 classqueue_dequeue(queue->classqueue,
762 &queue->classqueue_linkobj);
763 cpu_demand_event(get_rq_local_stat(queue,cpu),CPU_DEMAND_DEQUEUE,0);
765 goto retry_next_class;
767 schedstat_inc(rq, sched_noswitch);
768 // BUG_ON(!array->nr_active);
770 idx = queue->top_priority;
771 // BUG_ON (idx == MAX_PRIO);
772 next = task_list_entry(array->queue[idx].next);
775 #else /*! CONFIG_CKRM_CPU_SCHEDULE*/
776 static inline struct task_struct * rq_get_next_task(struct runqueue* rq)
779 struct list_head *queue;
783 if (unlikely(!array->nr_active)) {
785 * Switch the active and expired arrays.
787 schedstat_inc(rq, sched_switch);
788 rq->active = rq->expired;
791 rq->expired_timestamp = 0;
792 rq->best_expired_prio = MAX_PRIO;
794 schedstat_inc(rq, sched_noswitch);
796 idx = sched_find_first_bit(array->bitmap);
797 queue = array->queue + idx;
798 return list_entry(queue->next, task_t, run_list);
801 static inline void class_enqueue_task(struct task_struct* p, prio_array_t *array) { }
802 static inline void class_dequeue_task(struct task_struct* p, prio_array_t *array) { }
803 static inline void init_cpu_classes(void) { }
804 #define rq_ckrm_load(rq) NULL
805 static inline void ckrm_sched_tick(int j,int this_cpu,void* name) {}
806 #endif /* CONFIG_CKRM_CPU_SCHEDULE */
809 * Adding/removing a task to/from a priority array:
811 static void dequeue_task(struct task_struct *p, prio_array_t *array)
814 list_del(&p->run_list);
815 if (list_empty(array->queue + p->prio))
816 __clear_bit(p->prio, array->bitmap);
817 class_dequeue_task(p,array);
820 static void enqueue_task(struct task_struct *p, prio_array_t *array)
822 sched_info_queued(p);
823 list_add_tail(&p->run_list, array->queue + p->prio);
824 __set_bit(p->prio, array->bitmap);
827 class_enqueue_task(p,array);
831 * Used by the migration code - we pull tasks from the head of the
832 * remote queue so we want these tasks to show up at the head of the
835 static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array)
837 list_add(&p->run_list, array->queue + p->prio);
838 __set_bit(p->prio, array->bitmap);
841 class_enqueue_task(p,array);
845 * effective_prio - return the priority that is based on the static
846 * priority but is modified by bonuses/penalties.
848 * We scale the actual sleep average [0 .... MAX_SLEEP_AVG]
849 * into the -5 ... 0 ... +5 bonus/penalty range.
851 * We use 25% of the full 0...39 priority range so that:
853 * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs.
854 * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks.
856 * Both properties are important to certain workloads.
858 static int effective_prio(task_t *p)
865 bonus = CURRENT_BONUS(p) - MAX_BONUS / 2;
867 prio = p->static_prio - bonus;
869 #ifdef CONFIG_VSERVER_HARDCPU
870 if (task_vx_flags(p, VXF_SCHED_PRIO, 0))
871 prio += effective_vavavoom(p, MAX_USER_PRIO);
874 if (prio < MAX_RT_PRIO)
876 if (prio > MAX_PRIO-1)
882 * __activate_task - move a task to the runqueue.
884 static inline void __activate_task(task_t *p, runqueue_t *rq)
886 enqueue_task(p, rq_active(p,rq));
891 * __activate_idle_task - move idle task to the _front_ of runqueue.
893 static inline void __activate_idle_task(task_t *p, runqueue_t *rq)
895 enqueue_task_head(p, rq_active(p,rq));
899 static void recalc_task_prio(task_t *p, unsigned long long now)
901 unsigned long long __sleep_time = now - p->timestamp;
902 unsigned long sleep_time;
904 if (__sleep_time > NS_MAX_SLEEP_AVG)
905 sleep_time = NS_MAX_SLEEP_AVG;
907 sleep_time = (unsigned long)__sleep_time;
909 if (likely(sleep_time > 0)) {
911 * User tasks that sleep a long time are categorised as
912 * idle and will get just interactive status to stay active &
913 * prevent them suddenly becoming cpu hogs and starving
916 if (p->mm && p->activated != -1 &&
917 sleep_time > INTERACTIVE_SLEEP(p)) {
918 p->sleep_avg = JIFFIES_TO_NS(MAX_SLEEP_AVG -
921 p->interactive_credit++;
924 * The lower the sleep avg a task has the more
925 * rapidly it will rise with sleep time.
927 sleep_time *= (MAX_BONUS - CURRENT_BONUS(p)) ? : 1;
930 * Tasks with low interactive_credit are limited to
931 * one timeslice worth of sleep avg bonus.
934 sleep_time > JIFFIES_TO_NS(task_timeslice(p)))
935 sleep_time = JIFFIES_TO_NS(task_timeslice(p));
938 * Non high_credit tasks waking from uninterruptible
939 * sleep are limited in their sleep_avg rise as they
940 * are likely to be cpu hogs waiting on I/O
942 if (p->activated == -1 && !HIGH_CREDIT(p) && p->mm) {
943 if (p->sleep_avg >= INTERACTIVE_SLEEP(p))
945 else if (p->sleep_avg + sleep_time >=
946 INTERACTIVE_SLEEP(p)) {
947 p->sleep_avg = INTERACTIVE_SLEEP(p);
953 * This code gives a bonus to interactive tasks.
955 * The boost works by updating the 'average sleep time'
956 * value here, based on ->timestamp. The more time a
957 * task spends sleeping, the higher the average gets -
958 * and the higher the priority boost gets as well.
960 p->sleep_avg += sleep_time;
962 if (p->sleep_avg > NS_MAX_SLEEP_AVG) {
963 p->sleep_avg = NS_MAX_SLEEP_AVG;
965 p->interactive_credit++;
970 p->prio = effective_prio(p);
974 * activate_task - move a task to the runqueue and do priority recalculation
976 * Update all the scheduling statistics stuff. (sleep average
977 * calculation, priority modifiers, etc.)
979 static void activate_task(task_t *p, runqueue_t *rq, int local)
981 unsigned long long now;
986 /* Compensate for drifting sched_clock */
987 runqueue_t *this_rq = this_rq();
988 now = (now - this_rq->timestamp_last_tick)
989 + rq->timestamp_last_tick;
993 recalc_task_prio(p, now);
996 * This checks to make sure it's not an uninterruptible task
997 * that is now waking up.
1001 * Tasks which were woken up by interrupts (ie. hw events)
1002 * are most likely of interactive nature. So we give them
1003 * the credit of extending their sleep time to the period
1004 * of time they spend on the runqueue, waiting for execution
1005 * on a CPU, first time around:
1011 * Normal first-time wakeups get a credit too for
1012 * on-runqueue time, but it will be weighted down:
1019 vx_activate_task(p);
1020 __activate_task(p, rq);
1024 * deactivate_task - remove a task from the runqueue.
1026 static void __deactivate_task(struct task_struct *p, runqueue_t *rq)
1029 if (p->state == TASK_UNINTERRUPTIBLE)
1030 rq->nr_uninterruptible++;
1031 dequeue_task(p, p->array);
1036 static void deactivate_task(struct task_struct *p, runqueue_t *rq)
1038 __deactivate_task(p, rq);
1039 vx_deactivate_task(p);
1043 * resched_task - mark a task 'to be rescheduled now'.
1045 * On UP this means the setting of the need_resched flag, on SMP it
1046 * might also involve a cross-CPU call to trigger the scheduler on
1050 static void resched_task(task_t *p)
1052 int need_resched, nrpolling;
1054 BUG_ON(!spin_is_locked(&task_rq(p)->lock));
1056 /* minimise the chance of sending an interrupt to poll_idle() */
1057 nrpolling = test_tsk_thread_flag(p,TIF_POLLING_NRFLAG);
1058 need_resched = test_and_set_tsk_thread_flag(p,TIF_NEED_RESCHED);
1059 nrpolling |= test_tsk_thread_flag(p,TIF_POLLING_NRFLAG);
1061 if (!need_resched && !nrpolling && (task_cpu(p) != smp_processor_id()))
1062 smp_send_reschedule(task_cpu(p));
1065 static inline void resched_task(task_t *p)
1067 set_tsk_need_resched(p);
1072 * task_curr - is this task currently executing on a CPU?
1073 * @p: the task in question.
1075 inline int task_curr(const task_t *p)
1077 return cpu_curr(task_cpu(p)) == p;
1087 struct list_head list;
1088 enum request_type type;
1090 /* For REQ_MOVE_TASK */
1094 /* For REQ_SET_DOMAIN */
1095 struct sched_domain *sd;
1097 struct completion done;
1101 * The task's runqueue lock must be held.
1102 * Returns true if you have to wait for migration thread.
1104 static int migrate_task(task_t *p, int dest_cpu, migration_req_t *req)
1106 runqueue_t *rq = task_rq(p);
1109 * If the task is not on a runqueue (and not running), then
1110 * it is sufficient to simply update the task's cpu field.
1112 if (!p->array && !task_running(rq, p)) {
1113 set_task_cpu(p, dest_cpu);
1117 init_completion(&req->done);
1118 req->type = REQ_MOVE_TASK;
1120 req->dest_cpu = dest_cpu;
1121 list_add(&req->list, &rq->migration_queue);
1126 * wait_task_inactive - wait for a thread to unschedule.
1128 * The caller must ensure that the task *will* unschedule sometime soon,
1129 * else this function might spin for a *long* time. This function can't
1130 * be called with interrupts off, or it may introduce deadlock with
1131 * smp_call_function() if an IPI is sent by the same process we are
1132 * waiting to become inactive.
1134 void wait_task_inactive(task_t * p)
1136 unsigned long flags;
1141 rq = task_rq_lock(p, &flags);
1142 /* Must be off runqueue entirely, not preempted. */
1143 if (unlikely(p->array)) {
1144 /* If it's preempted, we yield. It could be a while. */
1145 preempted = !task_running(rq, p);
1146 task_rq_unlock(rq, &flags);
1152 task_rq_unlock(rq, &flags);
1156 * kick_process - kick a running thread to enter/exit the kernel
1157 * @p: the to-be-kicked thread
1159 * Cause a process which is running on another CPU to enter
1160 * kernel-mode, without any delay. (to get signals handled.)
1162 void kick_process(task_t *p)
1168 if ((cpu != smp_processor_id()) && task_curr(p))
1169 smp_send_reschedule(cpu);
1174 * Return a low guess at the load of a migration-source cpu.
1176 * We want to under-estimate the load of migration sources, to
1177 * balance conservatively.
1179 static inline unsigned long source_load(int cpu)
1181 runqueue_t *rq = cpu_rq(cpu);
1182 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE;
1184 return min(rq->cpu_load, load_now);
1188 * Return a high guess at the load of a migration-target cpu
1190 static inline unsigned long target_load(int cpu)
1192 runqueue_t *rq = cpu_rq(cpu);
1193 unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE;
1195 return max(rq->cpu_load, load_now);
1201 * wake_idle() is useful especially on SMT architectures to wake a
1202 * task onto an idle sibling if we would otherwise wake it onto a
1205 * Returns the CPU we should wake onto.
1207 #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
1208 static int wake_idle(int cpu, task_t *p)
1211 runqueue_t *rq = cpu_rq(cpu);
1212 struct sched_domain *sd;
1219 if (!(sd->flags & SD_WAKE_IDLE))
1222 cpus_and(tmp, sd->span, p->cpus_allowed);
1224 for_each_cpu_mask(i, tmp) {
1232 static inline int wake_idle(int cpu, task_t *p)
1239 * try_to_wake_up - wake up a thread
1240 * @p: the to-be-woken-up thread
1241 * @state: the mask of task states that can be woken
1242 * @sync: do a synchronous wakeup?
1244 * Put it on the run-queue if it's not already there. The "current"
1245 * thread is always on the run-queue (except when the actual
1246 * re-schedule is in progress), and as such you're allowed to do
1247 * the simpler "current->state = TASK_RUNNING" to mark yourself
1248 * runnable without the overhead of this.
1250 * returns failure only if the task is already active.
1252 static int try_to_wake_up(task_t * p, unsigned int state, int sync)
1254 int cpu, this_cpu, success = 0;
1255 unsigned long flags;
1259 unsigned long load, this_load;
1260 struct sched_domain *sd;
1264 rq = task_rq_lock(p, &flags);
1265 schedstat_inc(rq, ttwu_cnt);
1266 old_state = p->state;
1267 if (!(old_state & state))
1274 this_cpu = smp_processor_id();
1277 if (unlikely(task_running(rq, p)))
1282 if (cpu == this_cpu || unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
1285 load = source_load(cpu);
1286 this_load = target_load(this_cpu);
1289 * If sync wakeup then subtract the (maximum possible) effect of
1290 * the currently running task from the load of the current CPU:
1293 this_load -= SCHED_LOAD_SCALE;
1295 /* Don't pull the task off an idle CPU to a busy one */
1296 if (load < SCHED_LOAD_SCALE/2 && this_load > SCHED_LOAD_SCALE/2)
1299 new_cpu = this_cpu; /* Wake to this CPU if we can */
1302 * Scan domains for affine wakeup and passive balancing
1305 for_each_domain(this_cpu, sd) {
1306 unsigned int imbalance;
1308 * Start passive balancing when half the imbalance_pct
1311 imbalance = sd->imbalance_pct + (sd->imbalance_pct - 100) / 2;
1313 if ((sd->flags & SD_WAKE_AFFINE) &&
1314 !task_hot(p, rq->timestamp_last_tick, sd)) {
1316 * This domain has SD_WAKE_AFFINE and p is cache cold
1319 if (cpu_isset(cpu, sd->span)) {
1320 schedstat_inc(sd, ttwu_wake_affine);
1323 } else if ((sd->flags & SD_WAKE_BALANCE) &&
1324 imbalance*this_load <= 100*load) {
1326 * This domain has SD_WAKE_BALANCE and there is
1329 if (cpu_isset(cpu, sd->span)) {
1330 schedstat_inc(sd, ttwu_wake_balance);
1336 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1338 schedstat_inc(rq, ttwu_attempts);
1339 new_cpu = wake_idle(new_cpu, p);
1340 if (new_cpu != cpu && cpu_isset(new_cpu, p->cpus_allowed)) {
1341 schedstat_inc(rq, ttwu_moved);
1342 set_task_cpu(p, new_cpu);
1343 task_rq_unlock(rq, &flags);
1344 /* might preempt at this point */
1345 rq = task_rq_lock(p, &flags);
1346 old_state = p->state;
1347 if (!(old_state & state))
1352 this_cpu = smp_processor_id();
1357 #endif /* CONFIG_SMP */
1358 if (old_state == TASK_UNINTERRUPTIBLE) {
1359 rq->nr_uninterruptible--;
1361 * Tasks on involuntary sleep don't earn
1362 * sleep_avg beyond just interactive state.
1368 * Sync wakeups (i.e. those types of wakeups where the waker
1369 * has indicated that it will leave the CPU in short order)
1370 * don't trigger a preemption, if the woken up task will run on
1371 * this cpu. (in this case the 'I will reschedule' promise of
1372 * the waker guarantees that the freshly woken up task is going
1373 * to be considered on this CPU.)
1375 activate_task(p, rq, cpu == this_cpu);
1376 if (!sync || cpu != this_cpu) {
1377 if (TASK_PREEMPTS_CURR(p, rq))
1378 resched_task(rq->curr);
1383 p->state = TASK_RUNNING;
1385 task_rq_unlock(rq, &flags);
1390 int fastcall wake_up_process(task_t * p)
1392 return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
1393 TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
1396 EXPORT_SYMBOL(wake_up_process);
1398 int fastcall wake_up_state(task_t *p, unsigned int state)
1400 return try_to_wake_up(p, state, 0);
1404 static int find_idlest_cpu(struct task_struct *p, int this_cpu,
1405 struct sched_domain *sd);
1409 * Perform scheduler related setup for a newly forked process p.
1410 * p is forked by current.
1412 void fastcall sched_fork(task_t *p)
1415 * We mark the process as running here, but have not actually
1416 * inserted it onto the runqueue yet. This guarantees that
1417 * nobody will actually run it, and a signal or other external
1418 * event cannot wake it up and insert it on the runqueue either.
1420 p->state = TASK_RUNNING;
1421 INIT_LIST_HEAD(&p->run_list);
1423 spin_lock_init(&p->switch_lock);
1424 #ifdef CONFIG_SCHEDSTATS
1425 memset(&p->sched_info, 0, sizeof(p->sched_info));
1427 #ifdef CONFIG_CKRM_CPU_SCHEDULE
1428 cpu_demand_event(&p->demand_stat,CPU_DEMAND_INIT,0);
1430 #ifdef CONFIG_PREEMPT
1432 * During context-switch we hold precisely one spinlock, which
1433 * schedule_tail drops. (in the common case it's this_rq()->lock,
1434 * but it also can be p->switch_lock.) So we compensate with a count
1435 * of 1. Also, we want to start with kernel preemption disabled.
1437 p->thread_info->preempt_count = 1;
1440 * Share the timeslice between parent and child, thus the
1441 * total amount of pending timeslices in the system doesn't change,
1442 * resulting in more scheduling fairness.
1444 local_irq_disable();
1445 p->time_slice = (current->time_slice + 1) >> 1;
1447 * The remainder of the first timeslice might be recovered by
1448 * the parent if the child exits early enough.
1450 p->first_time_slice = 1;
1451 current->time_slice >>= 1;
1452 p->timestamp = sched_clock();
1453 if (unlikely(!current->time_slice)) {
1455 * This case is rare, it happens when the parent has only
1456 * a single jiffy left from its timeslice. Taking the
1457 * runqueue lock is not a problem.
1459 current->time_slice = 1;
1461 scheduler_tick(0, 0);
1469 * wake_up_new_task - wake up a newly created task for the first time.
1471 * This function will do some initial scheduler statistics housekeeping
1472 * that must be done for every newly created context, then puts the task
1473 * on the runqueue and wakes it.
1475 void fastcall wake_up_new_task(task_t * p, unsigned long clone_flags)
1477 unsigned long flags;
1479 runqueue_t *rq, *this_rq;
1481 rq = task_rq_lock(p, &flags);
1483 this_cpu = smp_processor_id();
1485 BUG_ON(p->state != TASK_RUNNING);
1487 schedstat_inc(rq, wunt_cnt);
1489 * We decrease the sleep average of forking parents
1490 * and children as well, to keep max-interactive tasks
1491 * from forking tasks that are max-interactive. The parent
1492 * (current) is done further down, under its lock.
1494 p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) *
1495 CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
1497 p->interactive_credit = 0;
1499 p->prio = effective_prio(p);
1501 vx_activate_task(p);
1502 if (likely(cpu == this_cpu)) {
1503 if (!(clone_flags & CLONE_VM)) {
1505 * The VM isn't cloned, so we're in a good position to
1506 * do child-runs-first in anticipation of an exec. This
1507 * usually avoids a lot of COW overhead.
1509 if (unlikely(!current->array))
1510 __activate_task(p, rq);
1512 p->prio = current->prio;
1513 list_add_tail(&p->run_list, ¤t->run_list);
1514 p->array = current->array;
1515 p->array->nr_active++;
1517 class_enqueue_task(p,p->array);
1521 /* Run child last */
1522 __activate_task(p, rq);
1524 * We skip the following code due to cpu == this_cpu
1526 * task_rq_unlock(rq, &flags);
1527 * this_rq = task_rq_lock(current, &flags);
1531 this_rq = cpu_rq(this_cpu);
1534 * Not the local CPU - must adjust timestamp. This should
1535 * get optimised away in the !CONFIG_SMP case.
1537 p->timestamp = (p->timestamp - this_rq->timestamp_last_tick)
1538 + rq->timestamp_last_tick;
1539 __activate_task(p, rq);
1540 if (TASK_PREEMPTS_CURR(p, rq))
1541 resched_task(rq->curr);
1543 schedstat_inc(rq, wunt_moved);
1545 * Parent and child are on different CPUs, now get the
1546 * parent runqueue to update the parent's ->sleep_avg:
1548 task_rq_unlock(rq, &flags);
1549 this_rq = task_rq_lock(current, &flags);
1551 current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) *
1552 PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS);
1553 task_rq_unlock(this_rq, &flags);
1557 * Potentially available exiting-child timeslices are
1558 * retrieved here - this way the parent does not get
1559 * penalized for creating too many threads.
1561 * (this cannot be used to 'generate' timeslices
1562 * artificially, because any timeslice recovered here
1563 * was given away by the parent in the first place.)
1565 void fastcall sched_exit(task_t * p)
1567 unsigned long flags;
1571 * If the child was a (relative-) CPU hog then decrease
1572 * the sleep_avg of the parent as well.
1574 rq = task_rq_lock(p->parent, &flags);
1575 if (p->first_time_slice) {
1576 p->parent->time_slice += p->time_slice;
1577 if (unlikely(p->parent->time_slice > task_timeslice(p)))
1578 p->parent->time_slice = task_timeslice(p);
1580 if (p->sleep_avg < p->parent->sleep_avg)
1581 p->parent->sleep_avg = p->parent->sleep_avg /
1582 (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg /
1584 task_rq_unlock(rq, &flags);
1588 * finish_task_switch - clean up after a task-switch
1589 * @prev: the thread we just switched away from.
1591 * We enter this with the runqueue still locked, and finish_arch_switch()
1592 * will unlock it along with doing any other architecture-specific cleanup
1595 * Note that we may have delayed dropping an mm in context_switch(). If
1596 * so, we finish that here outside of the runqueue lock. (Doing it
1597 * with the lock held can cause deadlocks; see schedule() for
1600 static void finish_task_switch(task_t *prev)
1602 runqueue_t *rq = this_rq();
1603 struct mm_struct *mm = rq->prev_mm;
1604 unsigned long prev_task_flags;
1609 * A task struct has one reference for the use as "current".
1610 * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and
1611 * calls schedule one last time. The schedule call will never return,
1612 * and the scheduled task must drop that reference.
1613 * The test for EXIT_ZOMBIE must occur while the runqueue locks are
1614 * still held, otherwise prev could be scheduled on another cpu, die
1615 * there before we look at prev->state, and then the reference would
1617 * Manfred Spraul <manfred@colorfullife.com>
1619 prev_task_flags = prev->flags;
1620 finish_arch_switch(rq, prev);
1623 if (unlikely(prev_task_flags & PF_DEAD))
1624 put_task_struct(prev);
1628 * schedule_tail - first thing a freshly forked thread must call.
1629 * @prev: the thread we just switched away from.
1631 asmlinkage void schedule_tail(task_t *prev)
1633 finish_task_switch(prev);
1635 if (current->set_child_tid)
1636 put_user(current->pid, current->set_child_tid);
1640 * context_switch - switch to the new MM and the new
1641 * thread's register state.
1644 task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next)
1646 struct mm_struct *mm = next->mm;
1647 struct mm_struct *oldmm = prev->active_mm;
1649 if (unlikely(!mm)) {
1650 next->active_mm = oldmm;
1651 atomic_inc(&oldmm->mm_count);
1652 enter_lazy_tlb(oldmm, next);
1654 switch_mm(oldmm, mm, next);
1656 if (unlikely(!prev->mm)) {
1657 prev->active_mm = NULL;
1658 WARN_ON(rq->prev_mm);
1659 rq->prev_mm = oldmm;
1662 /* Here we just switch the register state and the stack. */
1663 switch_to(prev, next, prev);
1669 * nr_running, nr_uninterruptible and nr_context_switches:
1671 * externally visible scheduler statistics: current number of runnable
1672 * threads, current number of uninterruptible-sleeping threads, total
1673 * number of context switches performed since bootup.
1675 unsigned long nr_running(void)
1677 unsigned long i, sum = 0;
1679 for_each_online_cpu(i)
1680 sum += cpu_rq(i)->nr_running;
1685 unsigned long nr_uninterruptible(void)
1687 unsigned long i, sum = 0;
1690 sum += cpu_rq(i)->nr_uninterruptible;
1695 unsigned long long nr_context_switches(void)
1697 unsigned long long i, sum = 0;
1700 sum += cpu_rq(i)->nr_switches;
1705 unsigned long nr_iowait(void)
1707 unsigned long i, sum = 0;
1710 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1718 * double_rq_lock - safely lock two runqueues
1720 * Note this does not disable interrupts like task_rq_lock,
1721 * you need to do so manually before calling.
1723 static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2)
1726 spin_lock(&rq1->lock);
1729 spin_lock(&rq1->lock);
1730 spin_lock(&rq2->lock);
1732 spin_lock(&rq2->lock);
1733 spin_lock(&rq1->lock);
1739 * double_rq_unlock - safely unlock two runqueues
1741 * Note this does not restore interrupts like task_rq_unlock,
1742 * you need to do so manually after calling.
1744 static void double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2)
1746 spin_unlock(&rq1->lock);
1748 spin_unlock(&rq2->lock);
1752 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1754 static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest)
1756 if (unlikely(!spin_trylock(&busiest->lock))) {
1757 if (busiest < this_rq) {
1758 spin_unlock(&this_rq->lock);
1759 spin_lock(&busiest->lock);
1760 spin_lock(&this_rq->lock);
1762 spin_lock(&busiest->lock);
1767 * find_idlest_cpu - find the least busy runqueue.
1769 static int find_idlest_cpu(struct task_struct *p, int this_cpu,
1770 struct sched_domain *sd)
1772 unsigned long load, min_load, this_load;
1777 min_load = ULONG_MAX;
1779 cpus_and(mask, sd->span, p->cpus_allowed);
1781 for_each_cpu_mask(i, mask) {
1782 load = target_load(i);
1784 if (load < min_load) {
1788 /* break out early on an idle CPU: */
1794 /* add +1 to account for the new task */
1795 this_load = source_load(this_cpu) + SCHED_LOAD_SCALE;
1798 * Would with the addition of the new task to the
1799 * current CPU there be an imbalance between this
1800 * CPU and the idlest CPU?
1802 * Use half of the balancing threshold - new-context is
1803 * a good opportunity to balance.
1805 if (min_load*(100 + (sd->imbalance_pct-100)/2) < this_load*100)
1812 * If dest_cpu is allowed for this process, migrate the task to it.
1813 * This is accomplished by forcing the cpu_allowed mask to only
1814 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
1815 * the cpu_allowed mask is restored.
1817 static void sched_migrate_task(task_t *p, int dest_cpu)
1819 migration_req_t req;
1821 unsigned long flags;
1823 rq = task_rq_lock(p, &flags);
1824 if (!cpu_isset(dest_cpu, p->cpus_allowed)
1825 || unlikely(cpu_is_offline(dest_cpu)))
1828 schedstat_inc(rq, smt_cnt);
1829 /* force the process onto the specified CPU */
1830 if (migrate_task(p, dest_cpu, &req)) {
1831 /* Need to wait for migration thread (might exit: take ref). */
1832 struct task_struct *mt = rq->migration_thread;
1833 get_task_struct(mt);
1834 task_rq_unlock(rq, &flags);
1835 wake_up_process(mt);
1836 put_task_struct(mt);
1837 wait_for_completion(&req.done);
1841 task_rq_unlock(rq, &flags);
1845 * sched_exec(): find the highest-level, exec-balance-capable
1846 * domain and try to migrate the task to the least loaded CPU.
1848 * execve() is a valuable balancing opportunity, because at this point
1849 * the task has the smallest effective memory and cache footprint.
1851 void sched_exec(void)
1853 struct sched_domain *tmp, *sd = NULL;
1854 int new_cpu, this_cpu = get_cpu();
1856 schedstat_inc(this_rq(), sbe_cnt);
1857 /* Prefer the current CPU if there's only this task running */
1858 if (this_rq()->nr_running <= 1)
1861 for_each_domain(this_cpu, tmp)
1862 if (tmp->flags & SD_BALANCE_EXEC)
1866 schedstat_inc(sd, sbe_attempts);
1867 new_cpu = find_idlest_cpu(current, this_cpu, sd);
1868 if (new_cpu != this_cpu) {
1869 schedstat_inc(sd, sbe_pushed);
1871 sched_migrate_task(current, new_cpu);
1880 * pull_task - move a task from a remote runqueue to the local runqueue.
1881 * Both runqueues must be locked.
1884 void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p,
1885 runqueue_t *this_rq, prio_array_t *this_array, int this_cpu)
1887 dequeue_task(p, src_array);
1888 src_rq->nr_running--;
1889 set_task_cpu(p, this_cpu);
1890 this_rq->nr_running++;
1891 enqueue_task(p, this_array);
1892 p->timestamp = (p->timestamp - src_rq->timestamp_last_tick)
1893 + this_rq->timestamp_last_tick;
1895 * Note that idle threads have a prio of MAX_PRIO, for this test
1896 * to be always true for them.
1898 if (TASK_PREEMPTS_CURR(p, this_rq))
1899 resched_task(this_rq->curr);
1903 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
1906 int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu,
1907 struct sched_domain *sd, enum idle_type idle)
1910 * We do not migrate tasks that are:
1911 * 1) running (obviously), or
1912 * 2) cannot be migrated to this CPU due to cpus_allowed, or
1913 * 3) are cache-hot on their current CPU.
1915 if (task_running(rq, p))
1917 if (!cpu_isset(this_cpu, p->cpus_allowed))
1920 /* Aggressive migration if we've failed balancing */
1921 if (idle == NEWLY_IDLE ||
1922 sd->nr_balance_failed < sd->cache_nice_tries) {
1923 if (task_hot(p, rq->timestamp_last_tick, sd))
1930 #ifdef CONFIG_CKRM_CPU_SCHEDULE
1931 static inline int ckrm_preferred_task(task_t *tmp,long min, long max,
1932 int phase, enum idle_type idle)
1934 long pressure = task_load(tmp);
1939 if ((idle == NOT_IDLE) && ! phase && (pressure <= min))
1945 * move tasks for a specic local class
1946 * return number of tasks pulled
1948 static inline int ckrm_cls_move_tasks(ckrm_lrq_t* src_lrq,ckrm_lrq_t*dst_lrq,
1949 runqueue_t *this_rq,
1950 runqueue_t *busiest,
1951 struct sched_domain *sd,
1953 enum idle_type idle,
1954 long* pressure_imbalance)
1956 prio_array_t *array, *dst_array;
1957 struct list_head *head, *curr;
1962 long pressure_min, pressure_max;
1963 /*hzheng: magic : 90% balance is enough*/
1964 long balance_min = *pressure_imbalance / 10;
1966 * we don't want to migrate tasks that will reverse the balance
1967 * or the tasks that make too small difference
1969 #define CKRM_BALANCE_MAX_RATIO 100
1970 #define CKRM_BALANCE_MIN_RATIO 1
1974 * We first consider expired tasks. Those will likely not be
1975 * executed in the near future, and they are most likely to
1976 * be cache-cold, thus switching CPUs has the least effect
1979 if (src_lrq->expired->nr_active) {
1980 array = src_lrq->expired;
1981 dst_array = dst_lrq->expired;
1983 array = src_lrq->active;
1984 dst_array = dst_lrq->active;
1988 /* Start searching at priority 0: */
1992 idx = sched_find_first_bit(array->bitmap);
1994 idx = find_next_bit(array->bitmap, MAX_PRIO, idx);
1995 if (idx >= MAX_PRIO) {
1996 if (array == src_lrq->expired && src_lrq->active->nr_active) {
1997 array = src_lrq->active;
1998 dst_array = dst_lrq->active;
2001 if ((! phase) && (! pulled) && (idle != IDLE))
2002 goto start; //try again
2004 goto out; //finished search for this lrq
2007 head = array->queue + idx;
2010 tmp = list_entry(curr, task_t, run_list);
2014 if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle)) {
2021 pressure_min = *pressure_imbalance * CKRM_BALANCE_MIN_RATIO/100;
2022 pressure_max = *pressure_imbalance * CKRM_BALANCE_MAX_RATIO/100;
2024 * skip the tasks that will reverse the balance too much
2026 if (ckrm_preferred_task(tmp,pressure_min,pressure_max,phase,idle)) {
2027 *pressure_imbalance -= task_load(tmp);
2028 pull_task(busiest, array, tmp,
2029 this_rq, dst_array, this_cpu);
2032 if (*pressure_imbalance <= balance_min)
2044 static inline long ckrm_rq_imbalance(runqueue_t *this_rq,runqueue_t *dst_rq)
2048 * make sure after balance, imbalance' > - imbalance/2
2049 * we don't want the imbalance be reversed too much
2051 imbalance = pid_get_pressure(rq_ckrm_load(dst_rq),0)
2052 - pid_get_pressure(rq_ckrm_load(this_rq),1);
2058 * try to balance the two runqueues
2060 * Called with both runqueues locked.
2061 * if move_tasks is called, it will try to move at least one task over
2063 static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest,
2064 unsigned long max_nr_move, struct sched_domain *sd,
2065 enum idle_type idle)
2067 struct ckrm_cpu_class *clsptr,*vip_cls = NULL;
2068 ckrm_lrq_t* src_lrq,*dst_lrq;
2069 long pressure_imbalance, pressure_imbalance_old;
2070 int src_cpu = task_cpu(busiest->curr);
2071 struct list_head *list;
2075 imbalance = ckrm_rq_imbalance(this_rq,busiest);
2077 if ((idle == NOT_IDLE && imbalance <= 0) || busiest->nr_running <= 1)
2080 //try to find the vip class
2081 list_for_each_entry(clsptr,&active_cpu_classes,links) {
2082 src_lrq = get_ckrm_lrq(clsptr,src_cpu);
2084 if (! lrq_nr_running(src_lrq))
2087 if (! vip_cls || cpu_class_weight(vip_cls) < cpu_class_weight(clsptr) )
2094 * do search from the most significant class
2095 * hopefully, less tasks will be migrated this way
2104 src_lrq = get_ckrm_lrq(clsptr,src_cpu);
2105 if (! lrq_nr_running(src_lrq))
2108 dst_lrq = get_ckrm_lrq(clsptr,this_cpu);
2110 //how much pressure for this class should be transferred
2111 pressure_imbalance = src_lrq->lrq_load * imbalance/src_lrq->local_weight;
2112 if (pulled && ! pressure_imbalance)
2115 pressure_imbalance_old = pressure_imbalance;
2119 ckrm_cls_move_tasks(src_lrq,dst_lrq,
2123 &pressure_imbalance);
2126 * hzheng: 2 is another magic number
2127 * stop balancing if the imbalance is less than 25% of the orig
2129 if (pressure_imbalance <= (pressure_imbalance_old >> 2))
2133 imbalance *= pressure_imbalance / pressure_imbalance_old;
2136 list = clsptr->links.next;
2137 if (list == &active_cpu_classes)
2139 clsptr = list_entry(list, typeof(*clsptr), links);
2140 if (clsptr != vip_cls)
2147 * ckrm_check_balance - is load balancing necessary?
2148 * return 0 if load balancing is not necessary
2149 * otherwise return the average load of the system
2150 * also, update nr_group
2153 * no load balancing if it's load is over average
2154 * no load balancing if it's load is far more than the min
2156 * read the status of all the runqueues
2158 static unsigned long ckrm_check_balance(struct sched_domain *sd, int this_cpu,
2159 enum idle_type idle, int* nr_group)
2161 struct sched_group *group = sd->groups;
2162 unsigned long min_load, max_load, avg_load;
2163 unsigned long total_load, this_load, total_pwr;
2165 max_load = this_load = total_load = total_pwr = 0;
2166 min_load = 0xFFFFFFFF;
2175 /* Tally up the load of all CPUs in the group */
2176 cpus_and(tmp, group->cpumask, cpu_online_map);
2177 if (unlikely(cpus_empty(tmp)))
2181 local_group = cpu_isset(this_cpu, group->cpumask);
2183 for_each_cpu_mask(i, tmp) {
2184 load = pid_get_pressure(rq_ckrm_load(cpu_rq(i)),local_group);
2192 total_load += avg_load;
2193 total_pwr += group->cpu_power;
2195 /* Adjust by relative CPU power of the group */
2196 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
2199 this_load = avg_load;
2201 } else if (avg_load > max_load) {
2202 max_load = avg_load;
2204 if (avg_load < min_load) {
2205 min_load = avg_load;
2208 group = group->next;
2209 *nr_group = *nr_group + 1;
2210 } while (group != sd->groups);
2212 if (!max_load || this_load >= max_load)
2215 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2217 /* hzheng: debugging: 105 is a magic number
2218 * 100*max_load <= sd->imbalance_pct*this_load)
2219 * should use imbalance_pct instead
2221 if (this_load > avg_load
2222 || 100*max_load < 105*this_load
2223 || 100*min_load < 70*this_load
2233 * any group that has above average load is considered busy
2234 * find the busiest queue from any of busy group
2237 ckrm_find_busy_queue(struct sched_domain *sd, int this_cpu,
2238 unsigned long avg_load, enum idle_type idle,
2241 struct sched_group *group;
2242 runqueue_t * busiest=NULL;
2246 rand = get_ckrm_rand(nr_group);
2250 unsigned long load,total_load,max_load;
2253 runqueue_t * grp_busiest;
2255 cpus_and(tmp, group->cpumask, cpu_online_map);
2256 if (unlikely(cpus_empty(tmp)))
2257 goto find_nextgroup;
2262 for_each_cpu_mask(i, tmp) {
2263 load = pid_get_pressure(rq_ckrm_load(cpu_rq(i)),0);
2265 if (load > max_load) {
2267 grp_busiest = cpu_rq(i);
2271 total_load = (total_load * SCHED_LOAD_SCALE) / group->cpu_power;
2272 if (total_load > avg_load) {
2273 busiest = grp_busiest;
2274 if (nr_group >= rand)
2278 group = group->next;
2280 } while (group != sd->groups);
2286 * load_balance - pressure based load balancing algorithm used by ckrm
2288 static int ckrm_load_balance(int this_cpu, runqueue_t *this_rq,
2289 struct sched_domain *sd, enum idle_type idle)
2291 runqueue_t *busiest;
2292 unsigned long avg_load;
2293 int nr_moved,nr_group;
2295 avg_load = ckrm_check_balance(sd, this_cpu, idle, &nr_group);
2299 busiest = ckrm_find_busy_queue(sd,this_cpu,avg_load,idle,nr_group);
2303 * This should be "impossible", but since load
2304 * balancing is inherently racy and statistical,
2305 * it could happen in theory.
2307 if (unlikely(busiest == this_rq)) {
2313 if (busiest->nr_running > 1) {
2315 * Attempt to move tasks. If find_busiest_group has found
2316 * an imbalance but busiest->nr_running <= 1, the group is
2317 * still unbalanced. nr_moved simply stays zero, so it is
2318 * correctly treated as an imbalance.
2320 double_lock_balance(this_rq, busiest);
2321 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2323 spin_unlock(&busiest->lock);
2325 adjust_local_weight();
2330 sd->nr_balance_failed ++;
2332 sd->nr_balance_failed = 0;
2334 /* We were unbalanced, so reset the balancing interval */
2335 sd->balance_interval = sd->min_interval;
2340 /* tune up the balancing interval */
2341 if (sd->balance_interval < sd->max_interval)
2342 sd->balance_interval *= 2;
2348 * this_rq->lock is already held
2350 static inline int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2351 struct sched_domain *sd)
2354 read_lock(&class_list_lock);
2355 ret = ckrm_load_balance(this_cpu,this_rq,sd,NEWLY_IDLE);
2356 read_unlock(&class_list_lock);
2360 static inline int load_balance(int this_cpu, runqueue_t *this_rq,
2361 struct sched_domain *sd, enum idle_type idle)
2365 spin_lock(&this_rq->lock);
2366 read_lock(&class_list_lock);
2367 ret= ckrm_load_balance(this_cpu,this_rq,sd,NEWLY_IDLE);
2368 read_unlock(&class_list_lock);
2369 spin_unlock(&this_rq->lock);
2372 #else /*! CONFIG_CKRM_CPU_SCHEDULE */
2374 * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq,
2375 * as part of a balancing operation within "domain". Returns the number of
2378 * Called with both runqueues locked.
2380 static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest,
2381 unsigned long max_nr_move, struct sched_domain *sd,
2382 enum idle_type idle)
2384 prio_array_t *array, *dst_array;
2385 struct list_head *head, *curr;
2386 int idx, pulled = 0;
2389 if (max_nr_move <= 0 || busiest->nr_running <= 1)
2393 * We first consider expired tasks. Those will likely not be
2394 * executed in the near future, and they are most likely to
2395 * be cache-cold, thus switching CPUs has the least effect
2398 if (busiest->expired->nr_active) {
2399 array = busiest->expired;
2400 dst_array = this_rq->expired;
2402 array = busiest->active;
2403 dst_array = this_rq->active;
2407 /* Start searching at priority 0: */
2411 idx = sched_find_first_bit(array->bitmap);
2413 idx = find_next_bit(array->bitmap, MAX_PRIO, idx);
2414 if (idx >= MAX_PRIO) {
2415 if (array == busiest->expired && busiest->active->nr_active) {
2416 array = busiest->active;
2417 dst_array = this_rq->active;
2423 head = array->queue + idx;
2426 tmp = list_entry(curr, task_t, run_list);
2430 if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle)) {
2438 * Right now, this is the only place pull_task() is called,
2439 * so we can safely collect pull_task() stats here rather than
2440 * inside pull_task().
2442 schedstat_inc(this_rq, pt_gained[idle]);
2443 schedstat_inc(busiest, pt_lost[idle]);
2445 pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu);
2448 /* We only want to steal up to the prescribed number of tasks. */
2449 if (pulled < max_nr_move) {
2460 * find_busiest_group finds and returns the busiest CPU group within the
2461 * domain. It calculates and returns the number of tasks which should be
2462 * moved to restore balance via the imbalance parameter.
2464 static struct sched_group *
2465 find_busiest_group(struct sched_domain *sd, int this_cpu,
2466 unsigned long *imbalance, enum idle_type idle)
2468 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2469 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
2471 max_load = this_load = total_load = total_pwr = 0;
2478 local_group = cpu_isset(this_cpu, group->cpumask);
2480 /* Tally up the load of all CPUs in the group */
2483 for_each_cpu_mask(i, group->cpumask) {
2484 /* Bias balancing toward cpus of our domain */
2486 load = target_load(i);
2488 load = source_load(i);
2497 total_load += avg_load;
2498 total_pwr += group->cpu_power;
2500 /* Adjust by relative CPU power of the group */
2501 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
2504 this_load = avg_load;
2507 } else if (avg_load > max_load) {
2508 max_load = avg_load;
2512 group = group->next;
2513 } while (group != sd->groups);
2515 if (!busiest || this_load >= max_load)
2518 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2520 if (this_load >= avg_load ||
2521 100*max_load <= sd->imbalance_pct*this_load)
2525 * We're trying to get all the cpus to the average_load, so we don't
2526 * want to push ourselves above the average load, nor do we wish to
2527 * reduce the max loaded cpu below the average load, as either of these
2528 * actions would just result in more rebalancing later, and ping-pong
2529 * tasks around. Thus we look for the minimum possible imbalance.
2530 * Negative imbalances (*we* are more loaded than anyone else) will
2531 * be counted as no imbalance for these purposes -- we can't fix that
2532 * by pulling tasks to us. Be careful of negative numbers as they'll
2533 * appear as very large values with unsigned longs.
2535 *imbalance = min(max_load - avg_load, avg_load - this_load);
2537 /* How much load to actually move to equalise the imbalance */
2538 *imbalance = (*imbalance * min(busiest->cpu_power, this->cpu_power))
2541 if (*imbalance < SCHED_LOAD_SCALE - 1) {
2542 unsigned long pwr_now = 0, pwr_move = 0;
2545 if (max_load - this_load >= SCHED_LOAD_SCALE*2) {
2551 * OK, we don't have enough imbalance to justify moving tasks,
2552 * however we may be able to increase total CPU power used by
2556 pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load);
2557 pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load);
2558 pwr_now /= SCHED_LOAD_SCALE;
2560 /* Amount of load we'd subtract */
2561 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power;
2563 pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE,
2566 /* Amount of load we'd add */
2567 tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power;
2570 pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp);
2571 pwr_move /= SCHED_LOAD_SCALE;
2573 /* Move if we gain another 8th of a CPU worth of throughput */
2574 if (pwr_move < pwr_now + SCHED_LOAD_SCALE / 8)
2581 /* Get rid of the scaling factor, rounding down as we divide */
2582 *imbalance = (*imbalance + 1) / SCHED_LOAD_SCALE;
2587 if (busiest && (idle == NEWLY_IDLE ||
2588 (idle == IDLE && max_load > SCHED_LOAD_SCALE)) ) {
2598 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2600 static runqueue_t *find_busiest_queue(struct sched_group *group)
2602 unsigned long load, max_load = 0;
2603 runqueue_t *busiest = NULL;
2606 for_each_cpu_mask(i, group->cpumask) {
2607 load = source_load(i);
2609 if (load > max_load) {
2611 busiest = cpu_rq(i);
2619 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2620 * tasks if there is an imbalance.
2622 * Called with this_rq unlocked.
2624 static int load_balance(int this_cpu, runqueue_t *this_rq,
2625 struct sched_domain *sd, enum idle_type idle)
2627 struct sched_group *group;
2628 runqueue_t *busiest;
2629 unsigned long imbalance;
2632 spin_lock(&this_rq->lock);
2633 schedstat_inc(sd, lb_cnt[idle]);
2635 group = find_busiest_group(sd, this_cpu, &imbalance, idle);
2637 schedstat_inc(sd, lb_nobusyg[idle]);
2641 busiest = find_busiest_queue(group);
2643 schedstat_inc(sd, lb_nobusyq[idle]);
2648 * This should be "impossible", but since load
2649 * balancing is inherently racy and statistical,
2650 * it could happen in theory.
2652 if (unlikely(busiest == this_rq)) {
2657 schedstat_add(sd, lb_imbalance[idle], imbalance);
2660 if (busiest->nr_running > 1) {
2662 * Attempt to move tasks. If find_busiest_group has found
2663 * an imbalance but busiest->nr_running <= 1, the group is
2664 * still unbalanced. nr_moved simply stays zero, so it is
2665 * correctly treated as an imbalance.
2667 double_lock_balance(this_rq, busiest);
2668 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2669 imbalance, sd, idle);
2670 spin_unlock(&busiest->lock);
2672 spin_unlock(&this_rq->lock);
2675 schedstat_inc(sd, lb_failed[idle]);
2676 sd->nr_balance_failed++;
2678 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
2681 spin_lock(&busiest->lock);
2682 if (!busiest->active_balance) {
2683 busiest->active_balance = 1;
2684 busiest->push_cpu = this_cpu;
2687 spin_unlock(&busiest->lock);
2689 wake_up_process(busiest->migration_thread);
2692 * We've kicked active balancing, reset the failure
2695 sd->nr_balance_failed = sd->cache_nice_tries;
2698 sd->nr_balance_failed = 0;
2700 /* We were unbalanced, so reset the balancing interval */
2701 sd->balance_interval = sd->min_interval;
2706 spin_unlock(&this_rq->lock);
2708 /* tune up the balancing interval */
2709 if (sd->balance_interval < sd->max_interval)
2710 sd->balance_interval *= 2;
2716 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2717 * tasks if there is an imbalance.
2719 * Called from schedule when this_rq is about to become idle (NEWLY_IDLE).
2720 * this_rq is locked.
2722 static int load_balance_newidle(int this_cpu, runqueue_t *this_rq,
2723 struct sched_domain *sd)
2725 struct sched_group *group;
2726 runqueue_t *busiest = NULL;
2727 unsigned long imbalance;
2730 schedstat_inc(sd, lb_cnt[NEWLY_IDLE]);
2731 group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE);
2733 schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]);
2737 busiest = find_busiest_queue(group);
2738 if (!busiest || busiest == this_rq) {
2739 schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]);
2743 /* Attempt to move tasks */
2744 double_lock_balance(this_rq, busiest);
2746 schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance);
2747 nr_moved = move_tasks(this_rq, this_cpu, busiest,
2748 imbalance, sd, NEWLY_IDLE);
2750 schedstat_inc(sd, lb_failed[NEWLY_IDLE]);
2752 spin_unlock(&busiest->lock);
2757 #endif /* CONFIG_CKRM_CPU_SCHEDULE*/
2761 * idle_balance is called by schedule() if this_cpu is about to become
2762 * idle. Attempts to pull tasks from other CPUs.
2764 static inline void idle_balance(int this_cpu, runqueue_t *this_rq)
2766 struct sched_domain *sd;
2768 for_each_domain(this_cpu, sd) {
2769 if (sd->flags & SD_BALANCE_NEWIDLE) {
2770 if (load_balance_newidle(this_cpu, this_rq, sd)) {
2771 /* We've pulled tasks over so stop searching */
2779 * active_load_balance is run by migration threads. It pushes a running
2780 * task off the cpu. It can be required to correctly have at least 1 task
2781 * running on each physical CPU where possible, and not have a physical /
2782 * logical imbalance.
2784 * Called with busiest locked.
2786 static void active_load_balance(runqueue_t *busiest, int busiest_cpu)
2788 struct sched_domain *sd;
2789 struct sched_group *group, *busy_group;
2792 schedstat_inc(busiest, alb_cnt);
2793 if (busiest->nr_running <= 1)
2796 for_each_domain(busiest_cpu, sd)
2797 if (cpu_isset(busiest->push_cpu, sd->span))
2803 while (!cpu_isset(busiest_cpu, group->cpumask))
2804 group = group->next;
2812 if (group == busy_group)
2815 for_each_cpu_mask(i, group->cpumask) {
2821 rq = cpu_rq(push_cpu);
2824 * This condition is "impossible", but since load
2825 * balancing is inherently a bit racy and statistical,
2826 * it can trigger.. Reported by Bjorn Helgaas on a
2829 if (unlikely(busiest == rq))
2831 double_lock_balance(busiest, rq);
2832 if (move_tasks(rq, push_cpu, busiest, 1, sd, IDLE)) {
2833 schedstat_inc(busiest, alb_lost);
2834 schedstat_inc(rq, alb_gained);
2836 schedstat_inc(busiest, alb_failed);
2838 spin_unlock(&rq->lock);
2840 group = group->next;
2841 } while (group != sd->groups);
2845 * rebalance_tick will get called every timer tick, on every CPU.
2847 * It checks each scheduling domain to see if it is due to be balanced,
2848 * and initiates a balancing operation if so.
2850 * Balancing parameters are set up in arch_init_sched_domains.
2853 /* Don't have all balancing operations going off at once */
2854 #define CPU_OFFSET(cpu) (HZ * cpu / NR_CPUS)
2856 static void rebalance_tick(int this_cpu, runqueue_t *this_rq,
2857 enum idle_type idle)
2859 unsigned long old_load, this_load;
2860 unsigned long j = jiffies + CPU_OFFSET(this_cpu);
2861 struct sched_domain *sd;
2863 /* Update our load */
2864 old_load = this_rq->cpu_load;
2865 this_load = this_rq->nr_running * SCHED_LOAD_SCALE;
2867 * Round up the averaging division if load is increasing. This
2868 * prevents us from getting stuck on 9 if the load is 10, for
2871 if (this_load > old_load)
2873 this_rq->cpu_load = (old_load + this_load) / 2;
2875 for_each_domain(this_cpu, sd) {
2876 unsigned long interval = sd->balance_interval;
2879 interval *= sd->busy_factor;
2881 /* scale ms to jiffies */
2882 interval = msecs_to_jiffies(interval);
2883 if (unlikely(!interval))
2886 if (j - sd->last_balance >= interval) {
2887 if (load_balance(this_cpu, this_rq, sd, idle)) {
2888 /* We've pulled tasks over so no longer idle */
2891 sd->last_balance += interval;
2897 * on UP we do not need to balance between CPUs:
2899 static inline void rebalance_tick(int cpu, runqueue_t *rq, enum idle_type idle)
2902 static inline void idle_balance(int cpu, runqueue_t *rq)
2907 static inline int wake_priority_sleeper(runqueue_t *rq)
2910 #ifdef CONFIG_SCHED_SMT
2911 spin_lock(&rq->lock);
2913 * If an SMT sibling task has been put to sleep for priority
2914 * reasons reschedule the idle task to see if it can now run.
2916 if (rq->nr_running) {
2917 resched_task(rq->idle);
2920 spin_unlock(&rq->lock);
2925 DEFINE_PER_CPU(struct kernel_stat, kstat);
2926 EXPORT_PER_CPU_SYMBOL(kstat);
2929 * We place interactive tasks back into the active array, if possible.
2931 * To guarantee that this does not starve expired tasks we ignore the
2932 * interactivity of a task if the first expired task had to wait more
2933 * than a 'reasonable' amount of time. This deadline timeout is
2934 * load-dependent, as the frequency of array switched decreases with
2935 * increasing number of running tasks. We also ignore the interactivity
2936 * if a better static_prio task has expired:
2939 #ifndef CONFIG_CKRM_CPU_SCHEDULE
2940 #define EXPIRED_STARVING(rq) \
2941 ((STARVATION_LIMIT && ((rq)->expired_timestamp && \
2942 (jiffies - (rq)->expired_timestamp >= \
2943 STARVATION_LIMIT * ((rq)->nr_running) + 1))) || \
2944 ((rq)->curr->static_prio > (rq)->best_expired_prio))
2946 #define EXPIRED_STARVING(rq) \
2947 (STARVATION_LIMIT && ((rq)->expired_timestamp && \
2948 (jiffies - (rq)->expired_timestamp >= \
2949 STARVATION_LIMIT * (lrq_nr_running(rq)) + 1)))
2953 * This function gets called by the timer code, with HZ frequency.
2954 * We call it with interrupts disabled.
2956 * It also gets called by the fork code, when changing the parent's
2959 void scheduler_tick(int user_ticks, int sys_ticks)
2961 int cpu = smp_processor_id();
2962 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2963 runqueue_t *rq = this_rq();
2964 task_t *p = current;
2965 struct vx_info *vxi = p->vx_info;
2967 rq->timestamp_last_tick = sched_clock();
2969 if (rcu_pending(cpu))
2970 rcu_check_callbacks(cpu, user_ticks);
2974 vxi->sched.cpu[cpu].user_ticks += user_ticks;
2975 vxi->sched.cpu[cpu].sys_ticks += sys_ticks;
2978 /* note: this timer irq context must be accounted for as well */
2979 if (hardirq_count() - HARDIRQ_OFFSET) {
2980 cpustat->irq += sys_ticks;
2982 } else if (softirq_count()) {
2983 cpustat->softirq += sys_ticks;
2987 if (p == rq->idle) {
2988 if (atomic_read(&rq->nr_iowait) > 0)
2989 cpustat->iowait += sys_ticks;
2990 // vx_cpustat_acc(vxi, iowait, cpu, cpustat, sys_ticks);
2992 cpustat->idle += sys_ticks;
2993 // vx_cpustat_acc(vxi, idle, cpu, cpustat, sys_ticks);
2995 if (wake_priority_sleeper(rq))
2998 ckrm_sched_tick(jiffies,cpu,rq_ckrm_load(rq));
3000 #ifdef CONFIG_VSERVER_HARDCPU_IDLE
3001 if (!--rq->idle_tokens && !list_empty(&rq->hold_queue))
3004 rebalance_tick(cpu, rq, IDLE);
3007 if (TASK_NICE(p) > 0)
3008 cpustat->nice += user_ticks;
3010 cpustat->user += user_ticks;
3011 cpustat->system += sys_ticks;
3013 /* Task might have expired already, but not scheduled off yet */
3014 if (p->array != rq_active(p,rq)) {
3015 set_tsk_need_resched(p);
3018 spin_lock(&rq->lock);
3020 * The task was running during this tick - update the
3021 * time slice counter. Note: we do not update a thread's
3022 * priority until it either goes to sleep or uses up its
3023 * timeslice. This makes it possible for interactive tasks
3024 * to use up their timeslices at their highest priority levels.
3028 * RR tasks need a special form of timeslice management.
3029 * FIFO tasks have no timeslices.
3031 if ((p->policy == SCHED_RR) && !--p->time_slice) {
3032 p->time_slice = task_timeslice(p);
3033 p->first_time_slice = 0;
3034 set_tsk_need_resched(p);
3036 /* put it at the end of the queue: */
3037 dequeue_task(p, rq_active(p,rq));
3038 enqueue_task(p, rq_active(p,rq));
3042 if (vx_need_resched(p)) {
3043 #ifdef CONFIG_CKRM_CPU_SCHEDULE
3044 /* Hubertus ... we can abstract this out */
3045 ckrm_lrq_t* rq = get_task_lrq(p);
3047 dequeue_task(p, rq->active);
3048 set_tsk_need_resched(p);
3049 p->prio = effective_prio(p);
3050 p->time_slice = task_timeslice(p);
3051 p->first_time_slice = 0;
3053 if (!rq->expired_timestamp)
3054 rq->expired_timestamp = jiffies;
3055 if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) {
3056 enqueue_task(p, rq->expired);
3057 if (p->static_prio < this_rq()->best_expired_prio)
3058 this_rq()->best_expired_prio = p->static_prio;
3060 enqueue_task(p, rq->active);
3063 * Prevent a too long timeslice allowing a task to monopolize
3064 * the CPU. We do this by splitting up the timeslice into
3067 * Note: this does not mean the task's timeslices expire or
3068 * get lost in any way, they just might be preempted by
3069 * another task of equal priority. (one with higher
3070 * priority would have preempted this task already.) We
3071 * requeue this task to the end of the list on this priority
3072 * level, which is in essence a round-robin of tasks with
3075 * This only applies to tasks in the interactive
3076 * delta range with at least TIMESLICE_GRANULARITY to requeue.
3078 if (TASK_INTERACTIVE(p) && !((task_timeslice(p) -
3079 p->time_slice) % TIMESLICE_GRANULARITY(p)) &&
3080 (p->time_slice >= TIMESLICE_GRANULARITY(p)) &&
3081 (p->array == rq_active(p,rq))) {
3083 dequeue_task(p, rq_active(p,rq));
3084 set_tsk_need_resched(p);
3085 p->prio = effective_prio(p);
3086 enqueue_task(p, rq_active(p,rq));
3090 spin_unlock(&rq->lock);
3092 ckrm_sched_tick(jiffies,cpu,rq_ckrm_load(rq));
3093 rebalance_tick(cpu, rq, NOT_IDLE);
3096 #ifdef CONFIG_SCHED_SMT
3097 static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
3099 struct sched_domain *sd = this_rq->sd;
3100 cpumask_t sibling_map;
3103 if (!(sd->flags & SD_SHARE_CPUPOWER))
3106 #ifdef CONFIG_CKRM_CPU_SCHEDULE
3107 if (prev != rq->idle) {
3108 unsigned long long run = now - prev->timestamp;
3109 ckrm_lrq_t * lrq = get_task_lrq(prev);
3111 lrq->lrq_load -= task_load(prev);
3112 cpu_demand_event(&prev->demand_stat,CPU_DEMAND_DESCHEDULE,run);
3113 lrq->lrq_load += task_load(prev);
3115 cpu_demand_event(get_task_lrq_stat(prev),CPU_DEMAND_DESCHEDULE,run);
3116 update_local_cvt(prev, run);
3120 * Unlock the current runqueue because we have to lock in
3121 * CPU order to avoid deadlocks. Caller knows that we might
3122 * unlock. We keep IRQs disabled.
3124 spin_unlock(&this_rq->lock);
3126 sibling_map = sd->span;
3128 for_each_cpu_mask(i, sibling_map)
3129 spin_lock(&cpu_rq(i)->lock);
3131 * We clear this CPU from the mask. This both simplifies the
3132 * inner loop and keps this_rq locked when we exit:
3134 cpu_clear(this_cpu, sibling_map);
3136 for_each_cpu_mask(i, sibling_map) {
3137 runqueue_t *smt_rq = cpu_rq(i);
3140 * If an SMT sibling task is sleeping due to priority
3141 * reasons wake it up now.
3143 if (smt_rq->curr == smt_rq->idle && smt_rq->nr_running)
3144 resched_task(smt_rq->idle);
3147 for_each_cpu_mask(i, sibling_map)
3148 spin_unlock(&cpu_rq(i)->lock);
3150 * We exit with this_cpu's rq still held and IRQs
3155 static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
3157 struct sched_domain *sd = this_rq->sd;
3158 cpumask_t sibling_map;
3159 prio_array_t *array;
3163 if (!(sd->flags & SD_SHARE_CPUPOWER))
3167 * The same locking rules and details apply as for
3168 * wake_sleeping_dependent():
3170 spin_unlock(&this_rq->lock);
3171 sibling_map = sd->span;
3172 for_each_cpu_mask(i, sibling_map)
3173 spin_lock(&cpu_rq(i)->lock);
3174 cpu_clear(this_cpu, sibling_map);
3177 * Establish next task to be run - it might have gone away because
3178 * we released the runqueue lock above:
3180 if (!this_rq->nr_running)
3182 array = this_rq->active;
3183 if (!array->nr_active)
3184 array = this_rq->expired;
3185 BUG_ON(!array->nr_active);
3187 p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next,
3190 for_each_cpu_mask(i, sibling_map) {
3191 runqueue_t *smt_rq = cpu_rq(i);
3192 task_t *smt_curr = smt_rq->curr;
3195 * If a user task with lower static priority than the
3196 * running task on the SMT sibling is trying to schedule,
3197 * delay it till there is proportionately less timeslice
3198 * left of the sibling task to prevent a lower priority
3199 * task from using an unfair proportion of the
3200 * physical cpu's resources. -ck
3202 if (((smt_curr->time_slice * (100 - sd->per_cpu_gain) / 100) >
3203 task_timeslice(p) || rt_task(smt_curr)) &&
3204 p->mm && smt_curr->mm && !rt_task(p))
3208 * Reschedule a lower priority task on the SMT sibling,
3209 * or wake it up if it has been put to sleep for priority
3212 if ((((p->time_slice * (100 - sd->per_cpu_gain) / 100) >
3213 task_timeslice(smt_curr) || rt_task(p)) &&
3214 smt_curr->mm && p->mm && !rt_task(smt_curr)) ||
3215 (smt_curr == smt_rq->idle && smt_rq->nr_running))
3216 resched_task(smt_curr);
3219 for_each_cpu_mask(i, sibling_map)
3220 spin_unlock(&cpu_rq(i)->lock);
3224 static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq)
3228 static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq)
3235 * schedule() is the main scheduler function.
3237 asmlinkage void __sched schedule(void)
3240 task_t *prev, *next;
3242 prio_array_t *array;
3243 unsigned long long now;
3244 unsigned long run_time;
3246 #ifdef CONFIG_VSERVER_HARDCPU
3247 struct vx_info *vxi;
3252 * If crash dump is in progress, this other cpu's
3253 * need to wait until it completes.
3254 * NB: this code is optimized away for kernels without
3257 if (unlikely(dump_oncpu))
3258 goto dump_scheduling_disabled;
3261 //WARN_ON(system_state == SYSTEM_BOOTING);
3263 * Test if we are atomic. Since do_exit() needs to call into
3264 * schedule() atomically, we ignore that path for now.
3265 * Otherwise, whine if we are scheduling when we should not be.
3267 if (likely(!(current->exit_state & (EXIT_DEAD | EXIT_ZOMBIE)))) {
3268 if (unlikely(in_atomic())) {
3269 printk(KERN_ERR "bad: scheduling while atomic!\n");
3280 * The idle thread is not allowed to schedule!
3281 * Remove this check after it has been exercised a bit.
3283 if (unlikely(current == rq->idle) && current->state != TASK_RUNNING) {
3284 printk(KERN_ERR "bad: scheduling from the idle thread!\n");
3288 release_kernel_lock(prev);
3289 schedstat_inc(rq, sched_cnt);
3290 now = sched_clock();
3291 if (likely(now - prev->timestamp < NS_MAX_SLEEP_AVG))
3292 run_time = now - prev->timestamp;
3294 run_time = NS_MAX_SLEEP_AVG;
3297 * Tasks with interactive credits get charged less run_time
3298 * at high sleep_avg to delay them losing their interactive
3301 if (HIGH_CREDIT(prev))
3302 run_time /= (CURRENT_BONUS(prev) ? : 1);
3304 spin_lock_irq(&rq->lock);
3306 #ifdef CONFIG_CKRM_CPU_SCHEDULE
3307 if (prev != rq->idle) {
3308 unsigned long long run = now - prev->timestamp;
3309 ckrm_lrq_t * lrq = get_task_lrq(prev);
3311 lrq->lrq_load -= task_load(prev);
3312 cpu_demand_event(&prev->demand_stat,CPU_DEMAND_DESCHEDULE,run);
3313 lrq->lrq_load += task_load(prev);
3315 cpu_demand_event(get_task_lrq_stat(prev),CPU_DEMAND_DESCHEDULE,run);
3316 update_local_cvt(prev, run);
3320 if (unlikely(current->flags & PF_DEAD))
3321 current->state = EXIT_DEAD;
3323 * if entering off of a kernel preemption go straight
3324 * to picking the next task.
3326 switch_count = &prev->nivcsw;
3327 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3328 switch_count = &prev->nvcsw;
3329 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3330 unlikely(signal_pending(prev))))
3331 prev->state = TASK_RUNNING;
3333 deactivate_task(prev, rq);
3336 #ifdef CONFIG_VSERVER_HARDCPU
3337 if (!list_empty(&rq->hold_queue)) {
3338 struct list_head *l, *n;
3342 list_for_each_safe(l, n, &rq->hold_queue) {
3343 next = list_entry(l, task_t, run_list);
3344 if (vxi == next->vx_info)
3347 vxi = next->vx_info;
3348 ret = vx_tokens_recalc(vxi);
3349 // tokens = vx_tokens_avail(next);
3352 list_del(&next->run_list);
3353 next->state &= ~TASK_ONHOLD;
3356 array = rq->expired;
3357 next->prio = MAX_PRIO-1;
3358 enqueue_task(next, array);
3360 if (next->static_prio < rq->best_expired_prio)
3361 rq->best_expired_prio = next->static_prio;
3363 // printk("··· %8lu unhold %p [%d]\n", jiffies, next, next->prio);
3366 if ((ret < 0) && (maxidle < ret))
3370 rq->idle_tokens = -maxidle;
3375 cpu = smp_processor_id();
3376 if (unlikely(!rq->nr_running)) {
3378 idle_balance(cpu, rq);
3379 if (!rq->nr_running) {
3381 rq->expired_timestamp = 0;
3382 wake_sleeping_dependent(cpu, rq);
3384 * wake_sleeping_dependent() might have released
3385 * the runqueue, so break out if we got new
3388 if (!rq->nr_running)
3392 if (dependent_sleeper(cpu, rq)) {
3393 schedstat_inc(rq, sched_goidle);
3398 * dependent_sleeper() releases and reacquires the runqueue
3399 * lock, hence go into the idle loop if the rq went
3402 if (unlikely(!rq->nr_running))
3406 /* MEF: CKRM refactored code into rq_get_next_task(); make
3407 * sure that when upgrading changes are reflected into both
3408 * versions of the code.
3410 next = rq_get_next_task(rq);
3412 #ifdef CONFIG_VSERVER_HARDCPU
3413 vxi = next->vx_info;
3414 if (vx_info_flags(vxi, VXF_SCHED_PAUSE|VXF_SCHED_HARD, 0)) {
3415 int ret = vx_tokens_recalc(vxi);
3417 if (unlikely(ret <= 0)) {
3418 if (ret && (rq->idle_tokens > -ret))
3419 rq->idle_tokens = -ret;
3420 __deactivate_task(next, rq);
3421 recalc_task_prio(next, now);
3422 // a new one on hold
3424 next->state |= TASK_ONHOLD;
3425 list_add_tail(&next->run_list, &rq->hold_queue);
3426 //printk("··· %8lu hold %p [%d]\n", jiffies, next, next->prio);
3432 if (!rt_task(next) && next->activated > 0) {
3433 unsigned long long delta = now - next->timestamp;
3435 if (next->activated == 1)
3436 delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128;
3438 array = next->array;
3439 dequeue_task(next, array);
3440 recalc_task_prio(next, next->timestamp + delta);
3441 enqueue_task(next, array);
3443 next->activated = 0;
3446 clear_tsk_need_resched(prev);
3447 rcu_qsctr_inc(task_cpu(prev));
3449 prev->sleep_avg -= run_time;
3450 if ((long)prev->sleep_avg <= 0) {
3451 prev->sleep_avg = 0;
3452 if (!(HIGH_CREDIT(prev) || LOW_CREDIT(prev)))
3453 prev->interactive_credit--;
3455 prev->timestamp = prev->last_ran = now;
3457 sched_info_switch(prev, next);
3458 if (likely(prev != next)) {
3459 next->timestamp = now;
3464 prepare_arch_switch(rq, next);
3465 prev = context_switch(rq, prev, next);
3468 finish_task_switch(prev);
3470 spin_unlock_irq(&rq->lock);
3472 reacquire_kernel_lock(current);
3473 preempt_enable_no_resched();
3474 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3479 dump_scheduling_disabled:
3480 /* allow scheduling only if this is the dumping cpu */
3481 if (dump_oncpu != smp_processor_id()+1) {
3488 EXPORT_SYMBOL(schedule);
3489 #ifdef CONFIG_PREEMPT
3491 * this is is the entry point to schedule() from in-kernel preemption
3492 * off of preempt_enable. Kernel preemptions off return from interrupt
3493 * occur there and call schedule directly.
3495 asmlinkage void __sched preempt_schedule(void)
3497 struct thread_info *ti = current_thread_info();
3500 * If there is a non-zero preempt_count or interrupts are disabled,
3501 * we do not want to preempt the current task. Just return..
3503 if (unlikely(ti->preempt_count || irqs_disabled()))
3507 ti->preempt_count = PREEMPT_ACTIVE;
3509 ti->preempt_count = 0;
3511 /* we could miss a preemption opportunity between schedule and now */
3513 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3517 EXPORT_SYMBOL(preempt_schedule);
3518 #endif /* CONFIG_PREEMPT */
3520 int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, void *key)
3522 task_t *p = curr->task;
3523 return try_to_wake_up(p, mode, sync);
3526 EXPORT_SYMBOL(default_wake_function);
3529 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3530 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3531 * number) then we wake all the non-exclusive tasks and one exclusive task.
3533 * There are circumstances in which we can try to wake a task which has already
3534 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3535 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3537 static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
3538 int nr_exclusive, int sync, void *key)
3540 struct list_head *tmp, *next;
3542 list_for_each_safe(tmp, next, &q->task_list) {
3545 curr = list_entry(tmp, wait_queue_t, task_list);
3546 flags = curr->flags;
3547 if (curr->func(curr, mode, sync, key) &&
3548 (flags & WQ_FLAG_EXCLUSIVE) &&
3555 * __wake_up - wake up threads blocked on a waitqueue.
3557 * @mode: which threads
3558 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3560 void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
3561 int nr_exclusive, void *key)
3563 unsigned long flags;
3565 spin_lock_irqsave(&q->lock, flags);
3566 __wake_up_common(q, mode, nr_exclusive, 0, key);
3567 spin_unlock_irqrestore(&q->lock, flags);
3570 EXPORT_SYMBOL(__wake_up);
3573 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3575 void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3577 __wake_up_common(q, mode, 1, 0, NULL);
3581 * __wake_up - sync- wake up threads blocked on a waitqueue.
3583 * @mode: which threads
3584 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3586 * The sync wakeup differs that the waker knows that it will schedule
3587 * away soon, so while the target thread will be woken up, it will not
3588 * be migrated to another CPU - ie. the two threads are 'synchronized'
3589 * with each other. This can prevent needless bouncing between CPUs.
3591 * On UP it can prevent extra preemption.
3593 void fastcall __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
3595 unsigned long flags;
3601 if (unlikely(!nr_exclusive))
3604 spin_lock_irqsave(&q->lock, flags);
3605 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
3606 spin_unlock_irqrestore(&q->lock, flags);
3608 EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3610 void fastcall complete(struct completion *x)
3612 unsigned long flags;
3614 spin_lock_irqsave(&x->wait.lock, flags);
3616 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3618 spin_unlock_irqrestore(&x->wait.lock, flags);
3620 EXPORT_SYMBOL(complete);
3622 void fastcall complete_all(struct completion *x)
3624 unsigned long flags;
3626 spin_lock_irqsave(&x->wait.lock, flags);
3627 x->done += UINT_MAX/2;
3628 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3630 spin_unlock_irqrestore(&x->wait.lock, flags);
3632 EXPORT_SYMBOL(complete_all);
3634 void fastcall __sched wait_for_completion(struct completion *x)
3637 spin_lock_irq(&x->wait.lock);
3639 DECLARE_WAITQUEUE(wait, current);
3641 wait.flags |= WQ_FLAG_EXCLUSIVE;
3642 __add_wait_queue_tail(&x->wait, &wait);
3644 __set_current_state(TASK_UNINTERRUPTIBLE);
3645 spin_unlock_irq(&x->wait.lock);
3647 spin_lock_irq(&x->wait.lock);
3649 __remove_wait_queue(&x->wait, &wait);
3652 spin_unlock_irq(&x->wait.lock);
3654 EXPORT_SYMBOL(wait_for_completion);
3656 #define SLEEP_ON_VAR \
3657 unsigned long flags; \
3658 wait_queue_t wait; \
3659 init_waitqueue_entry(&wait, current);
3661 #define SLEEP_ON_HEAD \
3662 spin_lock_irqsave(&q->lock,flags); \
3663 __add_wait_queue(q, &wait); \
3664 spin_unlock(&q->lock);
3666 #define SLEEP_ON_TAIL \
3667 spin_lock_irq(&q->lock); \
3668 __remove_wait_queue(q, &wait); \
3669 spin_unlock_irqrestore(&q->lock, flags);
3671 #define SLEEP_ON_BKLCHECK \
3672 if (unlikely(!kernel_locked()) && \
3673 sleep_on_bkl_warnings < 10) { \
3674 sleep_on_bkl_warnings++; \
3678 static int sleep_on_bkl_warnings;
3680 void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q)
3686 current->state = TASK_INTERRUPTIBLE;
3693 EXPORT_SYMBOL(interruptible_sleep_on);
3695 long fastcall __sched interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
3701 current->state = TASK_INTERRUPTIBLE;
3704 timeout = schedule_timeout(timeout);
3710 EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3712 long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
3718 current->state = TASK_UNINTERRUPTIBLE;
3721 timeout = schedule_timeout(timeout);
3727 EXPORT_SYMBOL(sleep_on_timeout);
3729 void set_user_nice(task_t *p, long nice)
3731 unsigned long flags;
3732 prio_array_t *array;
3734 int old_prio, new_prio, delta;
3736 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3739 * We have to be careful, if called from sys_setpriority(),
3740 * the task might be in the middle of scheduling on another CPU.
3742 rq = task_rq_lock(p, &flags);
3744 * The RT priorities are set via setscheduler(), but we still
3745 * allow the 'normal' nice value to be set - but as expected
3746 * it wont have any effect on scheduling until the task is
3750 p->static_prio = NICE_TO_PRIO(nice);
3755 dequeue_task(p, array);
3758 new_prio = NICE_TO_PRIO(nice);
3759 delta = new_prio - old_prio;
3760 p->static_prio = NICE_TO_PRIO(nice);
3764 enqueue_task(p, array);
3766 * If the task increased its priority or is running and
3767 * lowered its priority, then reschedule its CPU:
3769 if (delta < 0 || (delta > 0 && task_running(rq, p)))
3770 resched_task(rq->curr);
3773 task_rq_unlock(rq, &flags);
3776 EXPORT_SYMBOL(set_user_nice);
3778 #ifdef __ARCH_WANT_SYS_NICE
3781 * sys_nice - change the priority of the current process.
3782 * @increment: priority increment
3784 * sys_setpriority is a more generic, but much slower function that
3785 * does similar things.
3787 asmlinkage long sys_nice(int increment)
3793 * Setpriority might change our priority at the same moment.
3794 * We don't have to worry. Conceptually one call occurs first
3795 * and we have a single winner.
3797 if (increment < 0) {
3798 if (vx_flags(VXF_IGNEG_NICE, 0))
3800 if (!capable(CAP_SYS_NICE))
3802 if (increment < -40)
3808 nice = PRIO_TO_NICE(current->static_prio) + increment;
3814 retval = security_task_setnice(current, nice);
3818 set_user_nice(current, nice);
3825 * task_prio - return the priority value of a given task.
3826 * @p: the task in question.
3828 * This is the priority value as seen by users in /proc.
3829 * RT tasks are offset by -200. Normal tasks are centered
3830 * around 0, value goes from -16 to +15.
3832 int task_prio(const task_t *p)
3834 return p->prio - MAX_RT_PRIO;
3838 * task_nice - return the nice value of a given task.
3839 * @p: the task in question.
3841 int task_nice(const task_t *p)
3843 return TASK_NICE(p);
3847 * idle_cpu - is a given cpu idle currently?
3848 * @cpu: the processor in question.
3850 int idle_cpu(int cpu)
3852 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
3855 EXPORT_SYMBOL_GPL(idle_cpu);
3858 * find_process_by_pid - find a process with a matching PID value.
3859 * @pid: the pid in question.
3861 static inline task_t *find_process_by_pid(pid_t pid)
3863 return pid ? find_task_by_pid(pid) : current;
3866 /* Actually do priority change: must hold rq lock. */
3867 static void __setscheduler(struct task_struct *p, int policy, int prio)
3871 p->rt_priority = prio;
3872 if (policy != SCHED_NORMAL)
3873 p->prio = MAX_USER_RT_PRIO-1 - p->rt_priority;
3875 p->prio = p->static_prio;
3879 * setscheduler - change the scheduling policy and/or RT priority of a thread.
3881 static int setscheduler(pid_t pid, int policy, struct sched_param __user *param)
3883 struct sched_param lp;
3884 int retval = -EINVAL;
3885 int oldprio, oldpolicy = -1;
3886 prio_array_t *array;
3887 unsigned long flags;
3891 if (!param || pid < 0)
3895 if (copy_from_user(&lp, param, sizeof(struct sched_param)))
3899 * We play safe to avoid deadlocks.
3901 read_lock_irq(&tasklist_lock);
3903 p = find_process_by_pid(pid);
3909 /* double check policy once rq lock held */
3911 policy = oldpolicy = p->policy;
3914 if (policy != SCHED_FIFO && policy != SCHED_RR &&
3915 policy != SCHED_NORMAL)
3918 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3921 * Valid priorities for SCHED_FIFO and SCHED_RR are
3922 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL is 0.
3925 if (lp.sched_priority < 0 || lp.sched_priority > MAX_USER_RT_PRIO-1)
3927 if ((policy == SCHED_NORMAL) != (lp.sched_priority == 0))
3931 if ((policy == SCHED_FIFO || policy == SCHED_RR) &&
3932 !capable(CAP_SYS_NICE))
3934 if ((current->euid != p->euid) && (current->euid != p->uid) &&
3935 !capable(CAP_SYS_NICE))
3938 retval = security_task_setscheduler(p, policy, &lp);
3943 * To be able to change p->policy safely, the apropriate
3944 * runqueue lock must be held.
3946 rq = task_rq_lock(p, &flags);
3947 /* recheck policy now with rq lock held */
3948 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3949 policy = oldpolicy = -1;
3950 task_rq_unlock(rq, &flags);
3955 deactivate_task(p, task_rq(p));
3958 __setscheduler(p, policy, lp.sched_priority);
3960 vx_activate_task(p);
3961 __activate_task(p, task_rq(p));
3963 * Reschedule if we are currently running on this runqueue and
3964 * our priority decreased, or if we are not currently running on
3965 * this runqueue and our priority is higher than the current's
3967 if (task_running(rq, p)) {
3968 if (p->prio > oldprio)
3969 resched_task(rq->curr);
3970 } else if (TASK_PREEMPTS_CURR(p, rq))
3971 resched_task(rq->curr);
3973 task_rq_unlock(rq, &flags);
3975 read_unlock_irq(&tasklist_lock);
3981 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
3982 * @pid: the pid in question.
3983 * @policy: new policy
3984 * @param: structure containing the new RT priority.
3986 asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
3987 struct sched_param __user *param)
3989 return setscheduler(pid, policy, param);
3993 * sys_sched_setparam - set/change the RT priority of a thread
3994 * @pid: the pid in question.
3995 * @param: structure containing the new RT priority.
3997 asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
3999 return setscheduler(pid, -1, param);
4003 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4004 * @pid: the pid in question.
4006 asmlinkage long sys_sched_getscheduler(pid_t pid)
4008 int retval = -EINVAL;
4015 read_lock(&tasklist_lock);
4016 p = find_process_by_pid(pid);
4018 retval = security_task_getscheduler(p);
4022 read_unlock(&tasklist_lock);
4029 * sys_sched_getscheduler - get the RT priority of a thread
4030 * @pid: the pid in question.
4031 * @param: structure containing the RT priority.
4033 asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4035 struct sched_param lp;
4036 int retval = -EINVAL;
4039 if (!param || pid < 0)
4042 read_lock(&tasklist_lock);
4043 p = find_process_by_pid(pid);
4048 retval = security_task_getscheduler(p);
4052 lp.sched_priority = p->rt_priority;
4053 read_unlock(&tasklist_lock);
4056 * This one might sleep, we cannot do it with a spinlock held ...
4058 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4064 read_unlock(&tasklist_lock);
4068 long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4074 read_lock(&tasklist_lock);
4076 p = find_process_by_pid(pid);
4078 read_unlock(&tasklist_lock);
4079 unlock_cpu_hotplug();
4084 * It is not safe to call set_cpus_allowed with the
4085 * tasklist_lock held. We will bump the task_struct's
4086 * usage count and then drop tasklist_lock.
4089 read_unlock(&tasklist_lock);
4092 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4093 !capable(CAP_SYS_NICE))
4096 retval = set_cpus_allowed(p, new_mask);
4100 unlock_cpu_hotplug();
4104 static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4105 cpumask_t *new_mask)
4107 if (len < sizeof(cpumask_t)) {
4108 memset(new_mask, 0, sizeof(cpumask_t));
4109 } else if (len > sizeof(cpumask_t)) {
4110 len = sizeof(cpumask_t);
4112 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4116 * sys_sched_setaffinity - set the cpu affinity of a process
4117 * @pid: pid of the process
4118 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4119 * @user_mask_ptr: user-space pointer to the new cpu mask
4121 asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4122 unsigned long __user *user_mask_ptr)
4127 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4131 return sched_setaffinity(pid, new_mask);
4135 * Represents all cpu's present in the system
4136 * In systems capable of hotplug, this map could dynamically grow
4137 * as new cpu's are detected in the system via any platform specific
4138 * method, such as ACPI for e.g.
4141 cpumask_t cpu_present_map;
4142 EXPORT_SYMBOL(cpu_present_map);
4145 cpumask_t cpu_online_map = CPU_MASK_ALL;
4146 cpumask_t cpu_possible_map = CPU_MASK_ALL;
4149 long sched_getaffinity(pid_t pid, cpumask_t *mask)
4155 read_lock(&tasklist_lock);
4158 p = find_process_by_pid(pid);
4163 cpus_and(*mask, p->cpus_allowed, cpu_possible_map);
4166 read_unlock(&tasklist_lock);
4167 unlock_cpu_hotplug();
4175 * sys_sched_getaffinity - get the cpu affinity of a process
4176 * @pid: pid of the process
4177 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4178 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4180 asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4181 unsigned long __user *user_mask_ptr)
4186 if (len < sizeof(cpumask_t))
4189 ret = sched_getaffinity(pid, &mask);
4193 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4196 return sizeof(cpumask_t);
4200 * sys_sched_yield - yield the current processor to other threads.
4202 * this function yields the current CPU by moving the calling thread
4203 * to the expired array. If there are no other threads running on this
4204 * CPU then this function will return.
4206 asmlinkage long sys_sched_yield(void)
4208 runqueue_t *rq = this_rq_lock();
4209 prio_array_t *array = current->array;
4210 prio_array_t *target = rq_expired(current,rq);
4212 schedstat_inc(rq, yld_cnt);
4214 * We implement yielding by moving the task into the expired
4217 * (special rule: RT tasks will just roundrobin in the active
4220 if (rt_task(current))
4221 target = rq_active(current,rq);
4223 #warning MEF need to fix up SCHEDSTATS code, but I hope this is fixed by the 2.6.10 CKRM patch
4224 #ifdef CONFIG_SCHEDSTATS
4225 if (current->array->nr_active == 1) {
4226 schedstat_inc(rq, yld_act_empty);
4227 if (!rq->expired->nr_active)
4228 schedstat_inc(rq, yld_both_empty);
4229 } else if (!rq->expired->nr_active)
4230 schedstat_inc(rq, yld_exp_empty);
4233 dequeue_task(current, array);
4234 enqueue_task(current, target);
4237 * Since we are going to call schedule() anyway, there's
4238 * no need to preempt or enable interrupts:
4240 _raw_spin_unlock(&rq->lock);
4241 preempt_enable_no_resched();
4248 void __sched __cond_resched(void)
4250 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4251 __might_sleep(__FILE__, __LINE__, 0);
4254 * The system_state check is somewhat ugly but we might be
4255 * called during early boot when we are not yet ready to reschedule.
4257 if (need_resched() && system_state >= SYSTEM_BOOTING_SCHEDULER_OK) {
4258 set_current_state(TASK_RUNNING);
4264 EXPORT_SYMBOL(__cond_resched);
4266 void __sched __cond_resched_lock(spinlock_t * lock)
4268 if (need_resched()) {
4269 _raw_spin_unlock(lock);
4270 preempt_enable_no_resched();
4271 set_current_state(TASK_RUNNING);
4277 EXPORT_SYMBOL(__cond_resched_lock);
4281 * yield - yield the current processor to other threads.
4283 * this is a shortcut for kernel-space yielding - it marks the
4284 * thread runnable and calls sys_sched_yield().
4286 void __sched yield(void)
4288 set_current_state(TASK_RUNNING);
4292 EXPORT_SYMBOL(yield);
4295 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4296 * that process accounting knows that this is a task in IO wait state.
4298 * But don't do that if it is a deliberate, throttling IO wait (this task
4299 * has set its backing_dev_info: the queue against which it should throttle)
4301 void __sched io_schedule(void)
4303 struct runqueue *rq = this_rq();
4305 atomic_inc(&rq->nr_iowait);
4307 atomic_dec(&rq->nr_iowait);
4310 EXPORT_SYMBOL(io_schedule);
4312 long __sched io_schedule_timeout(long timeout)
4314 struct runqueue *rq = this_rq();
4317 atomic_inc(&rq->nr_iowait);
4318 ret = schedule_timeout(timeout);
4319 atomic_dec(&rq->nr_iowait);
4324 * sys_sched_get_priority_max - return maximum RT priority.
4325 * @policy: scheduling class.
4327 * this syscall returns the maximum rt_priority that can be used
4328 * by a given scheduling class.
4330 asmlinkage long sys_sched_get_priority_max(int policy)
4337 ret = MAX_USER_RT_PRIO-1;
4347 * sys_sched_get_priority_min - return minimum RT priority.
4348 * @policy: scheduling class.
4350 * this syscall returns the minimum rt_priority that can be used
4351 * by a given scheduling class.
4353 asmlinkage long sys_sched_get_priority_min(int policy)
4369 * sys_sched_rr_get_interval - return the default timeslice of a process.
4370 * @pid: pid of the process.
4371 * @interval: userspace pointer to the timeslice value.
4373 * this syscall writes the default timeslice value of a given process
4374 * into the user-space timespec buffer. A value of '0' means infinity.
4377 long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
4379 int retval = -EINVAL;
4387 read_lock(&tasklist_lock);
4388 p = find_process_by_pid(pid);
4392 retval = security_task_getscheduler(p);
4396 jiffies_to_timespec(p->policy & SCHED_FIFO ?
4397 0 : task_timeslice(p), &t);
4398 read_unlock(&tasklist_lock);
4399 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
4403 read_unlock(&tasklist_lock);
4407 static inline struct task_struct *eldest_child(struct task_struct *p)
4409 if (list_empty(&p->children)) return NULL;
4410 return list_entry(p->children.next,struct task_struct,sibling);
4413 static inline struct task_struct *older_sibling(struct task_struct *p)
4415 if (p->sibling.prev==&p->parent->children) return NULL;
4416 return list_entry(p->sibling.prev,struct task_struct,sibling);
4419 static inline struct task_struct *younger_sibling(struct task_struct *p)
4421 if (p->sibling.next==&p->parent->children) return NULL;
4422 return list_entry(p->sibling.next,struct task_struct,sibling);
4425 static void show_task(task_t * p)
4429 unsigned long free = 0;
4430 static const char *stat_nam[] = { "R", "S", "D", "T", "t", "Z", "X" };
4432 printk("%-13.13s ", p->comm);
4433 state = p->state ? __ffs(p->state) + 1 : 0;
4434 if (state < ARRAY_SIZE(stat_nam))
4435 printk(stat_nam[state]);
4438 #if (BITS_PER_LONG == 32)
4439 if (state == TASK_RUNNING)
4440 printk(" running ");
4442 printk(" %08lX ", thread_saved_pc(p));
4444 if (state == TASK_RUNNING)
4445 printk(" running task ");
4447 printk(" %016lx ", thread_saved_pc(p));
4449 #ifdef CONFIG_DEBUG_STACK_USAGE
4451 unsigned long * n = (unsigned long *) (p->thread_info+1);
4454 free = (unsigned long) n - (unsigned long)(p->thread_info+1);
4457 printk("%5lu %5d %6d ", free, p->pid, p->parent->pid);
4458 if ((relative = eldest_child(p)))
4459 printk("%5d ", relative->pid);
4462 if ((relative = younger_sibling(p)))
4463 printk("%7d", relative->pid);
4466 if ((relative = older_sibling(p)))
4467 printk(" %5d", relative->pid);
4471 printk(" (L-TLB)\n");
4473 printk(" (NOTLB)\n");
4475 if (state != TASK_RUNNING)
4476 show_stack(p, NULL);
4479 void show_state(void)
4483 #if (BITS_PER_LONG == 32)
4486 printk(" task PC pid father child younger older\n");
4490 printk(" task PC pid father child younger older\n");
4492 read_lock(&tasklist_lock);
4493 do_each_thread(g, p) {
4495 * reset the NMI-timeout, listing all files on a slow
4496 * console might take alot of time:
4498 touch_nmi_watchdog();
4500 } while_each_thread(g, p);
4502 read_unlock(&tasklist_lock);
4505 void __devinit init_idle(task_t *idle, int cpu)
4507 runqueue_t *rq = cpu_rq(cpu);
4508 unsigned long flags;
4510 idle->sleep_avg = 0;
4511 idle->interactive_credit = 0;
4513 idle->prio = MAX_PRIO;
4514 idle->state = TASK_RUNNING;
4515 set_task_cpu(idle, cpu);
4517 #ifdef CONFIG_CKRM_CPU_SCHEDULE
4518 cpu_demand_event(&(idle->demand_stat),CPU_DEMAND_INIT,0);
4519 idle->cpu_class = get_default_cpu_class();
4523 spin_lock_irqsave(&rq->lock, flags);
4524 rq->curr = rq->idle = idle;
4525 set_tsk_need_resched(idle);
4526 spin_unlock_irqrestore(&rq->lock, flags);
4528 /* Set the preempt count _outside_ the spinlocks! */
4529 #ifdef CONFIG_PREEMPT
4530 idle->thread_info->preempt_count = (idle->lock_depth >= 0);
4532 idle->thread_info->preempt_count = 0;
4537 * In a system that switches off the HZ timer nohz_cpu_mask
4538 * indicates which cpus entered this state. This is used
4539 * in the rcu update to wait only for active cpus. For system
4540 * which do not switch off the HZ timer nohz_cpu_mask should
4541 * always be CPU_MASK_NONE.
4543 cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
4547 * This is how migration works:
4549 * 1) we queue a migration_req_t structure in the source CPU's
4550 * runqueue and wake up that CPU's migration thread.
4551 * 2) we down() the locked semaphore => thread blocks.
4552 * 3) migration thread wakes up (implicitly it forces the migrated
4553 * thread off the CPU)
4554 * 4) it gets the migration request and checks whether the migrated
4555 * task is still in the wrong runqueue.
4556 * 5) if it's in the wrong runqueue then the migration thread removes
4557 * it and puts it into the right queue.
4558 * 6) migration thread up()s the semaphore.
4559 * 7) we wake up and the migration is done.
4563 * Change a given task's CPU affinity. Migrate the thread to a
4564 * proper CPU and schedule it away if the CPU it's executing on
4565 * is removed from the allowed bitmask.
4567 * NOTE: the caller must have a valid reference to the task, the
4568 * task must not exit() & deallocate itself prematurely. The
4569 * call is not atomic; no spinlocks may be held.
4571 int set_cpus_allowed(task_t *p, cpumask_t new_mask)
4573 unsigned long flags;
4575 migration_req_t req;
4578 rq = task_rq_lock(p, &flags);
4579 if (!cpus_intersects(new_mask, cpu_online_map)) {
4584 p->cpus_allowed = new_mask;
4585 /* Can the task run on the task's current CPU? If so, we're done */
4586 if (cpu_isset(task_cpu(p), new_mask))
4589 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
4590 /* Need help from migration thread: drop lock and wait. */
4591 task_rq_unlock(rq, &flags);
4592 wake_up_process(rq->migration_thread);
4593 wait_for_completion(&req.done);
4594 tlb_migrate_finish(p->mm);
4598 task_rq_unlock(rq, &flags);
4602 EXPORT_SYMBOL_GPL(set_cpus_allowed);
4605 * Move (not current) task off this cpu, onto dest cpu. We're doing
4606 * this because either it can't run here any more (set_cpus_allowed()
4607 * away from this CPU, or CPU going down), or because we're
4608 * attempting to rebalance this task on exec (sched_exec).
4610 * So we race with normal scheduler movements, but that's OK, as long
4611 * as the task is no longer on this CPU.
4613 static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
4615 runqueue_t *rq_dest, *rq_src;
4617 if (unlikely(cpu_is_offline(dest_cpu)))
4620 rq_src = cpu_rq(src_cpu);
4621 rq_dest = cpu_rq(dest_cpu);
4623 double_rq_lock(rq_src, rq_dest);
4624 /* Already moved. */
4625 if (task_cpu(p) != src_cpu)
4627 /* Affinity changed (again). */
4628 if (!cpu_isset(dest_cpu, p->cpus_allowed))
4633 * Sync timestamp with rq_dest's before activating.
4634 * The same thing could be achieved by doing this step
4635 * afterwards, and pretending it was a local activate.
4636 * This way is cleaner and logically correct.
4638 p->timestamp = p->timestamp - rq_src->timestamp_last_tick
4639 + rq_dest->timestamp_last_tick;
4640 deactivate_task(p, rq_src);
4641 set_task_cpu(p, dest_cpu);
4642 activate_task(p, rq_dest, 0);
4643 if (TASK_PREEMPTS_CURR(p, rq_dest))
4644 resched_task(rq_dest->curr);
4646 set_task_cpu(p, dest_cpu);
4649 double_rq_unlock(rq_src, rq_dest);
4653 * migration_thread - this is a highprio system thread that performs
4654 * thread migration by bumping thread off CPU then 'pushing' onto
4657 static int migration_thread(void * data)
4660 int cpu = (long)data;
4663 BUG_ON(rq->migration_thread != current);
4665 set_current_state(TASK_INTERRUPTIBLE);
4666 while (!kthread_should_stop()) {
4667 struct list_head *head;
4668 migration_req_t *req;
4670 if (current->flags & PF_FREEZE)
4671 refrigerator(PF_FREEZE);
4673 spin_lock_irq(&rq->lock);
4675 if (cpu_is_offline(cpu)) {
4676 spin_unlock_irq(&rq->lock);
4680 if (rq->active_balance) {
4681 active_load_balance(rq, cpu);
4682 rq->active_balance = 0;
4685 head = &rq->migration_queue;
4687 if (list_empty(head)) {
4688 spin_unlock_irq(&rq->lock);
4690 set_current_state(TASK_INTERRUPTIBLE);
4693 req = list_entry(head->next, migration_req_t, list);
4694 list_del_init(head->next);
4696 if (req->type == REQ_MOVE_TASK) {
4697 spin_unlock(&rq->lock);
4698 __migrate_task(req->task, smp_processor_id(),
4701 } else if (req->type == REQ_SET_DOMAIN) {
4703 spin_unlock_irq(&rq->lock);
4705 spin_unlock_irq(&rq->lock);
4709 complete(&req->done);
4711 __set_current_state(TASK_RUNNING);
4715 /* Wait for kthread_stop */
4716 set_current_state(TASK_INTERRUPTIBLE);
4717 while (!kthread_should_stop()) {
4719 set_current_state(TASK_INTERRUPTIBLE);
4721 __set_current_state(TASK_RUNNING);
4725 #ifdef CONFIG_HOTPLUG_CPU
4726 /* Figure out where task on dead CPU should go, use force if neccessary. */
4727 static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk)
4733 mask = node_to_cpumask(cpu_to_node(dead_cpu));
4734 cpus_and(mask, mask, tsk->cpus_allowed);
4735 dest_cpu = any_online_cpu(mask);
4737 /* On any allowed CPU? */
4738 if (dest_cpu == NR_CPUS)
4739 dest_cpu = any_online_cpu(tsk->cpus_allowed);
4741 /* No more Mr. Nice Guy. */
4742 if (dest_cpu == NR_CPUS) {
4743 cpus_setall(tsk->cpus_allowed);
4744 dest_cpu = any_online_cpu(tsk->cpus_allowed);
4747 * Don't tell them about moving exiting tasks or
4748 * kernel threads (both mm NULL), since they never
4751 if (tsk->mm && printk_ratelimit())
4752 printk(KERN_INFO "process %d (%s) no "
4753 "longer affine to cpu%d\n",
4754 tsk->pid, tsk->comm, dead_cpu);
4756 __migrate_task(tsk, dead_cpu, dest_cpu);
4759 /* Run through task list and migrate tasks from the dead cpu. */
4760 static void migrate_live_tasks(int src_cpu)
4762 struct task_struct *tsk, *t;
4764 write_lock_irq(&tasklist_lock);
4766 do_each_thread(t, tsk) {
4770 if (task_cpu(tsk) == src_cpu)
4771 move_task_off_dead_cpu(src_cpu, tsk);
4772 } while_each_thread(t, tsk);
4774 write_unlock_irq(&tasklist_lock);
4777 /* Schedules idle task to be the next runnable task on current CPU.
4778 * It does so by boosting its priority to highest possible and adding it to
4779 * the _front_ of runqueue. Used by CPU offline code.
4781 void sched_idle_next(void)
4783 int cpu = smp_processor_id();
4784 runqueue_t *rq = this_rq();
4785 struct task_struct *p = rq->idle;
4786 unsigned long flags;
4788 /* cpu has to be offline */
4789 BUG_ON(cpu_online(cpu));
4791 /* Strictly not necessary since rest of the CPUs are stopped by now
4792 * and interrupts disabled on current cpu.
4794 spin_lock_irqsave(&rq->lock, flags);
4796 __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
4797 /* Add idle task to _front_ of it's priority queue */
4798 __activate_idle_task(p, rq);
4800 spin_unlock_irqrestore(&rq->lock, flags);
4803 static void migrate_dead(unsigned int dead_cpu, task_t *tsk)
4805 struct runqueue *rq = cpu_rq(dead_cpu);
4807 /* Must be exiting, otherwise would be on tasklist. */
4808 BUG_ON(tsk->exit_state != EXIT_ZOMBIE && tsk->exit_state != EXIT_DEAD);
4810 /* Cannot have done final schedule yet: would have vanished. */
4811 BUG_ON(tsk->flags & PF_DEAD);
4813 get_task_struct(tsk);
4816 * Drop lock around migration; if someone else moves it,
4817 * that's OK. No task can be added to this CPU, so iteration is
4820 spin_unlock_irq(&rq->lock);
4821 move_task_off_dead_cpu(dead_cpu, tsk);
4822 spin_lock_irq(&rq->lock);
4824 put_task_struct(tsk);
4827 /* release_task() removes task from tasklist, so we won't find dead tasks. */
4828 static void migrate_dead_tasks(unsigned int dead_cpu)
4831 struct runqueue *rq = cpu_rq(dead_cpu);
4833 for (arr = 0; arr < 2; arr++) {
4834 for (i = 0; i < MAX_PRIO; i++) {
4835 struct list_head *list = &rq->arrays[arr].queue[i];
4836 while (!list_empty(list))
4837 migrate_dead(dead_cpu,
4838 list_entry(list->next, task_t,
4843 #endif /* CONFIG_HOTPLUG_CPU */
4846 * migration_call - callback that gets triggered when a CPU is added.
4847 * Here we can start up the necessary migration thread for the new CPU.
4849 static int migration_call(struct notifier_block *nfb, unsigned long action,
4852 int cpu = (long)hcpu;
4853 struct task_struct *p;
4854 struct runqueue *rq;
4855 unsigned long flags;
4858 case CPU_UP_PREPARE:
4859 p = kthread_create(migration_thread, hcpu, "migration/%d",cpu);
4862 p->flags |= PF_NOFREEZE;
4863 kthread_bind(p, cpu);
4864 /* Must be high prio: stop_machine expects to yield to it. */
4865 rq = task_rq_lock(p, &flags);
4866 __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1);
4867 task_rq_unlock(rq, &flags);
4868 cpu_rq(cpu)->migration_thread = p;
4871 /* Strictly unneccessary, as first user will wake it. */
4872 wake_up_process(cpu_rq(cpu)->migration_thread);
4874 #ifdef CONFIG_HOTPLUG_CPU
4875 case CPU_UP_CANCELED:
4876 /* Unbind it from offline cpu so it can run. Fall thru. */
4877 kthread_bind(cpu_rq(cpu)->migration_thread,smp_processor_id());
4878 kthread_stop(cpu_rq(cpu)->migration_thread);
4879 cpu_rq(cpu)->migration_thread = NULL;
4882 migrate_live_tasks(cpu);
4884 kthread_stop(rq->migration_thread);
4885 rq->migration_thread = NULL;
4886 /* Idle task back to normal (off runqueue, low prio) */
4887 rq = task_rq_lock(rq->idle, &flags);
4888 deactivate_task(rq->idle, rq);
4889 rq->idle->static_prio = MAX_PRIO;
4890 __setscheduler(rq->idle, SCHED_NORMAL, 0);
4891 migrate_dead_tasks(cpu);
4892 task_rq_unlock(rq, &flags);
4893 BUG_ON(rq->nr_running != 0);
4895 /* No need to migrate the tasks: it was best-effort if
4896 * they didn't do lock_cpu_hotplug(). Just wake up
4897 * the requestors. */
4898 spin_lock_irq(&rq->lock);
4899 while (!list_empty(&rq->migration_queue)) {
4900 migration_req_t *req;
4901 req = list_entry(rq->migration_queue.next,
4902 migration_req_t, list);
4903 BUG_ON(req->type != REQ_MOVE_TASK);
4904 list_del_init(&req->list);
4905 complete(&req->done);
4907 spin_unlock_irq(&rq->lock);
4914 /* Register at highest priority so that task migration (migrate_all_tasks)
4915 * happens before everything else.
4917 static struct notifier_block __devinitdata migration_notifier = {
4918 .notifier_call = migration_call,
4922 int __init migration_init(void)
4924 void *cpu = (void *)(long)smp_processor_id();
4925 /* Start one for boot CPU. */
4926 migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
4927 migration_call(&migration_notifier, CPU_ONLINE, cpu);
4928 register_cpu_notifier(&migration_notifier);
4934 * The 'big kernel lock'
4936 * This spinlock is taken and released recursively by lock_kernel()
4937 * and unlock_kernel(). It is transparently dropped and reaquired
4938 * over schedule(). It is used to protect legacy code that hasn't
4939 * been migrated to a proper locking design yet.
4941 * Don't use in new code.
4943 * Note: spinlock debugging needs this even on !CONFIG_SMP.
4945 spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
4946 EXPORT_SYMBOL(kernel_flag);
4950 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
4951 * hold the hotplug lock.
4953 static void cpu_attach_domain(struct sched_domain *sd, int cpu)
4955 migration_req_t req;
4956 unsigned long flags;
4957 runqueue_t *rq = cpu_rq(cpu);
4960 spin_lock_irqsave(&rq->lock, flags);
4962 if (cpu == smp_processor_id() || !cpu_online(cpu)) {
4965 init_completion(&req.done);
4966 req.type = REQ_SET_DOMAIN;
4968 list_add(&req.list, &rq->migration_queue);
4972 spin_unlock_irqrestore(&rq->lock, flags);
4975 wake_up_process(rq->migration_thread);
4976 wait_for_completion(&req.done);
4981 * To enable disjoint top-level NUMA domains, define SD_NODES_PER_DOMAIN
4982 * in arch code. That defines the number of nearby nodes in a node's top
4983 * level scheduling domain.
4986 #ifdef SD_NODES_PER_DOMAIN
4988 * find_next_best_node - find the next node to include in a sched_domain
4989 * @node: node whose sched_domain we're building
4990 * @used_nodes: nodes already in the sched_domain
4992 * Find the next node to include in a given scheduling domain. Simply
4993 * finds the closest node not already in the @used_nodes map.
4995 * Should use nodemask_t.
4997 static int __devinit find_next_best_node(int node, unsigned long *used_nodes)
4999 int i, n, val, min_val, best_node = 0;
5003 for (i = 0; i < numnodes; i++) {
5004 /* Start at @node */
5005 n = (node + i) % numnodes;
5007 /* Skip already used nodes */
5008 if (test_bit(n, used_nodes))
5011 /* Simple min distance search */
5012 val = node_distance(node, i);
5014 if (val < min_val) {
5020 set_bit(best_node, used_nodes);
5025 * sched_domain_node_span - get a cpumask for a node's sched_domain
5026 * @node: node whose cpumask we're constructing
5027 * @size: number of nodes to include in this span
5029 * Given a node, construct a good cpumask for its sched_domain to span. It
5030 * should be one that prevents unnecessary balancing, but also spreads tasks
5033 static cpumask_t __devinit sched_domain_node_span(int node)
5037 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
5040 bitmap_zero(used_nodes, MAX_NUMNODES);
5042 for (i = 0; i < SD_NODES_PER_DOMAIN; i++) {
5043 int next_node = find_next_best_node(node, used_nodes);
5046 nodemask = node_to_cpumask(next_node);
5047 cpus_or(span, span, nodemask);
5052 #else /* SD_NODES_PER_DOMAIN */
5053 static cpumask_t __devinit sched_domain_node_span(int node)
5055 return cpu_possible_map;
5057 #endif /* SD_NODES_PER_DOMAIN */
5058 #endif /* CONFIG_NUMA */
5060 #ifdef CONFIG_SCHED_SMT
5061 static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
5062 static struct sched_group sched_group_cpus[NR_CPUS];
5063 static int __devinit cpu_to_cpu_group(int cpu)
5069 static DEFINE_PER_CPU(struct sched_domain, phys_domains);
5070 static struct sched_group sched_group_phys[NR_CPUS];
5071 static int __devinit cpu_to_phys_group(int cpu)
5073 #ifdef CONFIG_SCHED_SMT
5074 return first_cpu(cpu_sibling_map[cpu]);
5082 static DEFINE_PER_CPU(struct sched_domain, node_domains);
5083 static struct sched_group sched_group_nodes[MAX_NUMNODES];
5084 static int __devinit cpu_to_node_group(int cpu)
5086 return cpu_to_node(cpu);
5090 /* Groups for isolated scheduling domains */
5091 static struct sched_group sched_group_isolated[NR_CPUS];
5093 /* cpus with isolated domains */
5094 cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE;
5096 static int __devinit cpu_to_isolated_group(int cpu)
5101 /* Setup the mask of cpus configured for isolated domains */
5102 static int __init isolated_cpu_setup(char *str)
5104 int ints[NR_CPUS], i;
5106 str = get_options(str, ARRAY_SIZE(ints), ints);
5107 cpus_clear(cpu_isolated_map);
5108 for (i = 1; i <= ints[0]; i++)
5109 cpu_set(ints[i], cpu_isolated_map);
5113 __setup ("isolcpus=", isolated_cpu_setup);
5116 * init_sched_build_groups takes an array of groups, the cpumask we wish
5117 * to span, and a pointer to a function which identifies what group a CPU
5118 * belongs to. The return value of group_fn must be a valid index into the
5119 * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we
5120 * keep track of groups covered with a cpumask_t).
5122 * init_sched_build_groups will build a circular linked list of the groups
5123 * covered by the given span, and will set each group's ->cpumask correctly,
5124 * and ->cpu_power to 0.
5126 static void __devinit init_sched_build_groups(struct sched_group groups[],
5127 cpumask_t span, int (*group_fn)(int cpu))
5129 struct sched_group *first = NULL, *last = NULL;
5130 cpumask_t covered = CPU_MASK_NONE;
5133 for_each_cpu_mask(i, span) {
5134 int group = group_fn(i);
5135 struct sched_group *sg = &groups[group];
5138 if (cpu_isset(i, covered))
5141 sg->cpumask = CPU_MASK_NONE;
5144 for_each_cpu_mask(j, span) {
5145 if (group_fn(j) != group)
5148 cpu_set(j, covered);
5149 cpu_set(j, sg->cpumask);
5161 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
5163 static void __devinit arch_init_sched_domains(void)
5166 cpumask_t cpu_default_map;
5167 cpumask_t cpu_isolated_online_map;
5169 cpus_and(cpu_isolated_online_map, cpu_isolated_map, cpu_online_map);
5172 * Setup mask for cpus without special case scheduling requirements.
5173 * For now this just excludes isolated cpus, but could be used to
5174 * exclude other special cases in the future.
5176 cpus_complement(cpu_default_map, cpu_isolated_map);
5177 cpus_and(cpu_default_map, cpu_default_map, cpu_online_map);
5179 /* Set up domains */
5180 for_each_online_cpu(i) {
5182 struct sched_domain *sd = NULL, *p;
5183 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
5185 cpus_and(nodemask, nodemask, cpu_default_map);
5188 * Set up isolated domains.
5189 * Unlike those of other cpus, the domains and groups are
5190 * single level, and span a single cpu.
5192 if (cpu_isset(i, cpu_isolated_online_map)) {
5193 #ifdef CONFIG_SCHED_SMT
5194 sd = &per_cpu(cpu_domains, i);
5196 sd = &per_cpu(phys_domains, i);
5198 group = cpu_to_isolated_group(i);
5200 cpu_set(i, sd->span);
5201 sd->balance_interval = INT_MAX; /* Don't balance */
5202 sd->flags = 0; /* Avoid WAKE_ */
5203 sd->groups = &sched_group_isolated[group];
5204 printk(KERN_INFO "Setting up cpu %d isolated.\n", i);
5205 /* Single level, so continue with next cpu */
5210 sd = &per_cpu(node_domains, i);
5211 group = cpu_to_node_group(i);
5213 /* FIXME: should be multilevel, in arch code */
5214 sd->span = sched_domain_node_span(i);
5215 cpus_and(sd->span, sd->span, cpu_default_map);
5216 sd->groups = &sched_group_nodes[group];
5220 sd = &per_cpu(phys_domains, i);
5221 group = cpu_to_phys_group(i);
5223 sd->span = nodemask;
5225 sd->groups = &sched_group_phys[group];
5227 #ifdef CONFIG_SCHED_SMT
5229 sd = &per_cpu(cpu_domains, i);
5230 group = cpu_to_cpu_group(i);
5231 *sd = SD_SIBLING_INIT;
5232 sd->span = cpu_sibling_map[i];
5233 cpus_and(sd->span, sd->span, cpu_default_map);
5235 sd->groups = &sched_group_cpus[group];
5239 #ifdef CONFIG_SCHED_SMT
5240 /* Set up CPU (sibling) groups */
5241 for_each_online_cpu(i) {
5242 cpumask_t this_sibling_map = cpu_sibling_map[i];
5243 cpus_and(this_sibling_map, this_sibling_map, cpu_default_map);
5244 if (i != first_cpu(this_sibling_map))
5247 init_sched_build_groups(sched_group_cpus, this_sibling_map,
5252 /* Set up isolated groups */
5253 for_each_cpu_mask(i, cpu_isolated_online_map) {
5254 cpumask_t mask = cpumask_of_cpu(i);
5255 init_sched_build_groups(sched_group_isolated, mask,
5256 &cpu_to_isolated_group);
5259 /* Set up physical groups */
5260 for (i = 0; i < MAX_NUMNODES; i++) {
5261 cpumask_t nodemask = node_to_cpumask(i);
5263 cpus_and(nodemask, nodemask, cpu_default_map);
5264 if (cpus_empty(nodemask))
5267 init_sched_build_groups(sched_group_phys, nodemask,
5268 &cpu_to_phys_group);
5273 /* Set up node groups */
5274 init_sched_build_groups(sched_group_nodes, cpu_default_map,
5275 &cpu_to_node_group);
5279 /* Calculate CPU power for physical packages and nodes */
5280 for_each_cpu_mask(i, cpu_default_map) {
5282 struct sched_domain *sd;
5283 #ifdef CONFIG_SCHED_SMT
5284 sd = &per_cpu(cpu_domains, i);
5285 power = SCHED_LOAD_SCALE;
5286 sd->groups->cpu_power = power;
5289 sd = &per_cpu(phys_domains, i);
5290 power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE *
5291 (cpus_weight(sd->groups->cpumask)-1) / 10;
5292 sd->groups->cpu_power = power;
5296 if (i == first_cpu(sd->groups->cpumask)) {
5297 /* Only add "power" once for each physical package. */
5298 sd = &per_cpu(node_domains, i);
5299 sd->groups->cpu_power += power;
5304 /* Attach the domains */
5305 for_each_online_cpu(i) {
5306 struct sched_domain *sd;
5307 #ifdef CONFIG_SCHED_SMT
5308 sd = &per_cpu(cpu_domains, i);
5310 sd = &per_cpu(phys_domains, i);
5312 cpu_attach_domain(sd, i);
5317 #ifdef CONFIG_HOTPLUG_CPU
5318 static void __devinit arch_destroy_sched_domains(void)
5320 /* Do nothing: everything is statically allocated. */
5324 #undef SCHED_DOMAIN_DEBUG
5325 #ifdef SCHED_DOMAIN_DEBUG
5326 void sched_domain_debug(void)
5330 for_each_online_cpu(i) {
5331 runqueue_t *rq = cpu_rq(i);
5332 struct sched_domain *sd;
5337 printk(KERN_DEBUG "CPU%d:\n", i);
5342 struct sched_group *group = sd->groups;
5343 cpumask_t groupmask;
5345 cpumask_scnprintf(str, NR_CPUS, sd->span);
5346 cpus_clear(groupmask);
5349 for (j = 0; j < level + 1; j++)
5351 printk("domain %d: span %s\n", level, str);
5353 if (!cpu_isset(i, sd->span))
5354 printk(KERN_DEBUG "ERROR domain->span does not contain CPU%d\n", i);
5355 if (!cpu_isset(i, group->cpumask))
5356 printk(KERN_DEBUG "ERROR domain->groups does not contain CPU%d\n", i);
5357 if (!group->cpu_power)
5358 printk(KERN_DEBUG "ERROR domain->cpu_power not set\n");
5361 for (j = 0; j < level + 2; j++)
5366 printk(" ERROR: NULL");
5370 if (!cpus_weight(group->cpumask))
5371 printk(" ERROR empty group:");
5373 if (cpus_intersects(groupmask, group->cpumask))
5374 printk(" ERROR repeated CPUs:");
5376 cpus_or(groupmask, groupmask, group->cpumask);
5378 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5381 group = group->next;
5382 } while (group != sd->groups);
5385 if (!cpus_equal(sd->span, groupmask))
5386 printk(KERN_DEBUG "ERROR groups don't span domain->span\n");
5392 if (!cpus_subset(groupmask, sd->span))
5393 printk(KERN_DEBUG "ERROR parent span is not a superset of domain->span\n");
5400 #define sched_domain_debug() {}
5404 /* Initial dummy domain for early boot and for hotplug cpu */
5405 static __devinitdata struct sched_domain sched_domain_dummy;
5406 static __devinitdata struct sched_group sched_group_dummy;
5409 #ifdef CONFIG_HOTPLUG_CPU
5411 * Force a reinitialization of the sched domains hierarchy. The domains
5412 * and groups cannot be updated in place without racing with the balancing
5413 * code, so we temporarily attach all running cpus to a "dummy" domain
5414 * which will prevent rebalancing while the sched domains are recalculated.
5416 static int update_sched_domains(struct notifier_block *nfb,
5417 unsigned long action, void *hcpu)
5422 case CPU_UP_PREPARE:
5423 case CPU_DOWN_PREPARE:
5424 for_each_online_cpu(i)
5425 cpu_attach_domain(&sched_domain_dummy, i);
5426 arch_destroy_sched_domains();
5429 case CPU_UP_CANCELED:
5430 case CPU_DOWN_FAILED:
5434 * Fall through and re-initialise the domains.
5441 /* The hotplug lock is already held by cpu_up/cpu_down */
5442 arch_init_sched_domains();
5444 sched_domain_debug();
5450 void __init sched_init_smp(void)
5453 arch_init_sched_domains();
5454 sched_domain_debug();
5455 unlock_cpu_hotplug();
5456 /* XXX: Theoretical race here - CPU may be hotplugged now */
5457 hotcpu_notifier(update_sched_domains, 0);
5460 void __init sched_init_smp(void)
5463 #endif /* CONFIG_SMP */
5465 int in_sched_functions(unsigned long addr)
5467 /* Linker adds these: start and end of __sched functions */
5468 extern char __sched_text_start[], __sched_text_end[];
5469 return in_lock_functions(addr) ||
5470 (addr >= (unsigned long)__sched_text_start
5471 && addr < (unsigned long)__sched_text_end);
5474 void __init sched_init(void)
5480 /* Set up an initial dummy domain for early boot */
5482 memset(&sched_domain_dummy, 0, sizeof(struct sched_domain));
5483 sched_domain_dummy.span = CPU_MASK_ALL;
5484 sched_domain_dummy.groups = &sched_group_dummy;
5485 sched_domain_dummy.last_balance = jiffies;
5486 sched_domain_dummy.balance_interval = INT_MAX; /* Don't balance */
5487 sched_domain_dummy.busy_factor = 1;
5489 memset(&sched_group_dummy, 0, sizeof(struct sched_group));
5490 sched_group_dummy.cpumask = CPU_MASK_ALL;
5491 sched_group_dummy.next = &sched_group_dummy;
5492 sched_group_dummy.cpu_power = SCHED_LOAD_SCALE;
5497 for (i = 0; i < NR_CPUS; i++) {
5498 #ifndef CONFIG_CKRM_CPU_SCHEDULE
5500 prio_array_t *array;
5503 spin_lock_init(&rq->lock);
5505 for (j = 0; j < 2; j++) {
5506 array = rq->arrays + j;
5507 for (k = 0; k < MAX_PRIO; k++) {
5508 INIT_LIST_HEAD(array->queue + k);
5509 __clear_bit(k, array->bitmap);
5511 // delimiter for bitsearch
5512 __set_bit(MAX_PRIO, array->bitmap);
5515 rq->active = rq->arrays;
5516 rq->expired = rq->arrays + 1;
5517 rq->best_expired_prio = MAX_PRIO;
5521 spin_lock_init(&rq->lock);
5525 rq->sd = &sched_domain_dummy;
5527 #ifdef CONFIG_CKRM_CPU_SCHEDULE
5528 ckrm_load_init(rq_ckrm_load(rq));
5530 rq->active_balance = 0;
5532 rq->migration_thread = NULL;
5533 INIT_LIST_HEAD(&rq->migration_queue);
5535 #ifdef CONFIG_VSERVER_HARDCPU
5536 INIT_LIST_HEAD(&rq->hold_queue);
5538 atomic_set(&rq->nr_iowait, 0);
5543 * The boot idle thread does lazy MMU switching as well:
5545 atomic_inc(&init_mm.mm_count);
5546 enter_lazy_tlb(&init_mm, current);
5549 * Make us the idle thread. Technically, schedule() should not be
5550 * called from this thread, however somewhere below it might be,
5551 * but because we are the idle thread, we just pick up running again
5552 * when this runqueue becomes "idle".
5554 init_idle(current, smp_processor_id());
5557 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5558 void __might_sleep(char *file, int line, int atomic_depth)
5560 #if defined(in_atomic)
5561 static unsigned long prev_jiffy; /* ratelimiting */
5563 #ifndef CONFIG_PREEMPT
5566 if (((in_atomic() != atomic_depth) || irqs_disabled()) &&
5567 system_state == SYSTEM_RUNNING) {
5568 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
5570 prev_jiffy = jiffies;
5571 printk(KERN_ERR "Debug: sleeping function called from invalid"
5572 " context at %s:%d\n", file, line);
5573 printk("in_atomic():%d[expected: %d], irqs_disabled():%d\n",
5574 in_atomic(), atomic_depth, irqs_disabled());
5579 EXPORT_SYMBOL(__might_sleep);
5582 #ifdef CONFIG_CKRM_CPU_SCHEDULE
5584 * return the classqueue object of a certain processor
5586 struct classqueue_struct * get_cpu_classqueue(int cpu)
5588 return (& (cpu_rq(cpu)->classqueue) );
5592 * _ckrm_cpu_change_class - change the class of a task
5594 void _ckrm_cpu_change_class(task_t *tsk, struct ckrm_cpu_class *newcls)
5596 prio_array_t *array;
5597 struct runqueue *rq;
5598 unsigned long flags;
5600 rq = task_rq_lock(tsk,&flags);
5603 dequeue_task(tsk,array);
5604 tsk->cpu_class = newcls;
5605 enqueue_task(tsk,rq_active(tsk,rq));
5607 tsk->cpu_class = newcls;
5609 task_rq_unlock(rq,&flags);