1 This patch adds support for bfs v230, modified for diff size reduction
3 --- a/Documentation/sysctl/kernel.txt
4 +++ b/Documentation/sysctl/kernel.txt
5 @@ -27,6 +27,7 @@ show up in /proc/sys/kernel:
10 - java-appletviewer [ binfmt_java, obsolete ]
11 - java-interpreter [ binfmt_java, obsolete ]
12 - kstack_depth_to_print [ X86 only ]
13 @@ -49,6 +50,7 @@ show up in /proc/sys/kernel:
15 - real-root-dev ==> Documentation/initrd.txt
16 - reboot-cmd [ SPARC only ]
21 @@ -171,6 +173,16 @@ Default value is "/sbin/hotplug".
23 ==============================================================
27 +This sets the percentage cpu that the unprivileged SCHED_ISO tasks can
28 +run effectively at realtime priority, averaged over a rolling five
29 +seconds over the -whole- system, meaning all cpus.
31 +Set to 70 (percent) by default.
33 +==============================================================
37 This flag controls the L2 cache of G3 processor boards. If
38 @@ -333,6 +345,19 @@ rebooting. ???
40 ==============================================================
42 +rr_interval: (BFS only)
44 +This is the smallest duration that any cpu process scheduling unit
45 +will run for. Increasing this value can increase throughput of cpu
46 +bound tasks substantially but at the expense of increased latencies
47 +overall. This value is in milliseconds and the default value chosen
48 +depends on the number of cpus available at scheduler initialisation
51 +Valid values are from 1-5000.
53 +==============================================================
57 The file rtsig-max can be used to tune the maximum number
58 --- a/include/linux/init_task.h
59 +++ b/include/linux/init_task.h
60 @@ -116,9 +116,10 @@ extern struct cred init_cred;
61 .usage = ATOMIC_INIT(2), \
62 .flags = PF_KTHREAD, \
64 - .prio = MAX_PRIO-20, \
65 + .prio = NORMAL_PRIO, \
66 .static_prio = MAX_PRIO-20, \
67 - .normal_prio = MAX_PRIO-20, \
68 + .normal_prio = NORMAL_PRIO, \
70 .policy = SCHED_NORMAL, \
71 .cpus_allowed = CPU_MASK_ALL, \
73 --- a/include/linux/sched.h
74 +++ b/include/linux/sched.h
79 -/* SCHED_ISO: reserved but not implemented yet */
83 +#define SCHED_MAX (SCHED_IDLE)
84 +#define SCHED_RANGE(policy) ((policy) <= SCHED_MAX)
89 @@ -1090,10 +1093,13 @@ struct sched_entity {
90 struct load_weight load; /* for load-balancing */
91 struct rb_node run_node;
92 struct list_head group_node;
93 +#ifdef CONFIG_SCHED_CFS
99 +#ifdef CONFIG_SCHED_CFS
101 u64 prev_sum_exec_runtime;
103 @@ -1145,6 +1151,7 @@ struct sched_entity {
104 /* rq "owned" by this entity/group: */
110 struct sched_rt_entity {
111 @@ -1172,17 +1179,19 @@ struct task_struct {
113 int lock_depth; /* BKL lock depth */
116 -#ifdef __ARCH_WANT_UNLOCKED_CTXSW
121 int prio, static_prio, normal_prio;
122 unsigned int rt_priority;
123 const struct sched_class *sched_class;
124 struct sched_entity se;
125 struct sched_rt_entity rt;
126 + unsigned long deadline;
127 +#ifdef CONFIG_SCHED_BFS
128 + int load_weight; /* for niceness load balancing purposes */
129 + int first_time_slice;
130 + unsigned long long timestamp, last_ran;
131 + unsigned long utime_pc, stime_pc;
134 #ifdef CONFIG_PREEMPT_NOTIFIERS
135 /* list of struct preempt_notifier: */
136 @@ -1205,6 +1214,9 @@ struct task_struct {
139 cpumask_t cpus_allowed;
140 +#ifdef CONFIG_HOTPLUG_CPU
141 + cpumask_t unplugged_mask;
144 #ifdef CONFIG_PREEMPT_RCU
145 int rcu_read_lock_nesting;
146 @@ -1497,11 +1509,19 @@ struct task_struct {
147 * priority to a value higher than any user task. Note:
148 * MAX_RT_PRIO must not be smaller than MAX_USER_RT_PRIO.
151 +#define PRIO_RANGE (40)
152 #define MAX_USER_RT_PRIO 100
153 #define MAX_RT_PRIO MAX_USER_RT_PRIO
155 +#ifdef CONFIG_SCHED_BFS
156 +#define MAX_PRIO (MAX_RT_PRIO + PRIO_RANGE)
157 +#define ISO_PRIO (MAX_RT_PRIO)
158 +#define NORMAL_PRIO (MAX_RT_PRIO + 1)
159 +#define IDLE_PRIO (MAX_RT_PRIO + 2)
160 +#define PRIO_LIMIT ((IDLE_PRIO) + 1)
162 #define MAX_PRIO (MAX_RT_PRIO + 40)
163 +#define NORMAL_PRIO (MAX_RT_PRIO - 20)
165 #define DEFAULT_PRIO (MAX_RT_PRIO + 20)
167 static inline int rt_prio(int prio)
168 @@ -1785,7 +1805,7 @@ task_sched_runtime(struct task_struct *t
169 extern unsigned long long thread_group_sched_runtime(struct task_struct *task);
171 /* sched_exec is called by processes performing an exec */
173 +#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_CFS)
174 extern void sched_exec(void);
176 #define sched_exec() {}
179 @@ -451,9 +451,22 @@ config LOG_BUF_SHIFT
180 config HAVE_UNSTABLE_SCHED_CLOCK
196 bool "Group CPU scheduler"
197 depends on EXPERIMENTAL
198 + depends on SCHED_CFS
201 This feature lets CPU scheduler recognize task groups and control CPU
202 @@ -504,6 +517,7 @@ endchoice
205 boolean "Control Group support"
206 + depends on SCHED_CFS
208 This option adds support for grouping sets of processes together, for
209 use with process control subsystems such as Cpusets, CFS, memory
210 --- a/kernel/Makefile
211 +++ b/kernel/Makefile
213 # Makefile for the linux kernel.
216 -obj-y = sched.o fork.o exec_domain.o panic.o printk.o \
217 +obj-y = $(if $(CONFIG_SCHED_CFS),sched.o,sched_bfs.o) fork.o exec_domain.o panic.o printk.o \
218 cpu.o exit.o itimer.o time.o softirq.o resource.o \
219 sysctl.o capability.o ptrace.o timer.o user.o \
220 signal.o sys.o kmod.o workqueue.o pid.o \
221 @@ -108,6 +108,7 @@ ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER
222 # I turn this off for IA-64 only. Andreas Schwab says it's also needed on m68k
223 # to get a correct value for the wait-channel (WCHAN in ps). --davidm
224 CFLAGS_sched.o := $(PROFILING) -fno-omit-frame-pointer
225 +CFLAGS_sched_bfs.o := $(PROFILING) -fno-omit-frame-pointer
228 $(obj)/configs.o: $(obj)/config_data.h
229 --- a/kernel/kthread.c
230 +++ b/kernel/kthread.c
232 #include <linux/mutex.h>
233 #include <trace/events/sched.h>
235 +#ifdef CONFIG_SCHED_BFS
236 +#define KTHREAD_NICE_LEVEL (0)
238 #define KTHREAD_NICE_LEVEL (-5)
241 static DEFINE_SPINLOCK(kthread_create_lock);
242 static LIST_HEAD(kthread_create_list);
244 +++ b/kernel/sched_bfs.c
247 + * kernel/sched_bfs.c, was sched.c
249 + * Kernel scheduler and related syscalls
251 + * Copyright (C) 1991-2002 Linus Torvalds
253 + * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
254 + * make semaphores SMP safe
255 + * 1998-11-19 Implemented schedule_timeout() and related stuff
256 + * by Andrea Arcangeli
257 + * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
258 + * hybrid priority-list and round-robin design with
259 + * an array-switch method of distributing timeslices
260 + * and per-CPU runqueues. Cleanups and useful suggestions
261 + * by Davide Libenzi, preemptible kernel bits by Robert Love.
262 + * 2003-09-03 Interactivity tuning by Con Kolivas.
263 + * 2004-04-02 Scheduler domains code by Nick Piggin
264 + * 2007-04-15 Work begun on replacing all interactivity tuning with a
265 + * fair scheduling design by Con Kolivas.
266 + * 2007-05-05 Load balancing (smp-nice) and other improvements
267 + * by Peter Williams
268 + * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
269 + * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
270 + * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
271 + * Thomas Gleixner, Mike Kravetz
272 + * now Brainfuck deadline scheduling policy by Con Kolivas deletes
273 + * a whole lot of those previous things.
276 +#include <linux/mm.h>
277 +#include <linux/module.h>
278 +#include <linux/nmi.h>
279 +#include <linux/init.h>
280 +#include <asm/uaccess.h>
281 +#include <linux/highmem.h>
282 +#include <linux/smp_lock.h>
283 +#include <asm/mmu_context.h>
284 +#include <linux/interrupt.h>
285 +#include <linux/capability.h>
286 +#include <linux/completion.h>
287 +#include <linux/kernel_stat.h>
288 +#include <linux/debug_locks.h>
289 +#include <linux/perf_counter.h>
290 +#include <linux/security.h>
291 +#include <linux/notifier.h>
292 +#include <linux/profile.h>
293 +#include <linux/freezer.h>
294 +#include <linux/vmalloc.h>
295 +#include <linux/blkdev.h>
296 +#include <linux/delay.h>
297 +#include <linux/smp.h>
298 +#include <linux/threads.h>
299 +#include <linux/timer.h>
300 +#include <linux/rcupdate.h>
301 +#include <linux/cpu.h>
302 +#include <linux/cpuset.h>
303 +#include <linux/cpumask.h>
304 +#include <linux/percpu.h>
305 +#include <linux/kthread.h>
306 +#include <linux/proc_fs.h>
307 +#include <linux/seq_file.h>
308 +#include <linux/syscalls.h>
309 +#include <linux/times.h>
310 +#include <linux/tsacct_kern.h>
311 +#include <linux/kprobes.h>
312 +#include <linux/delayacct.h>
313 +#include <linux/reciprocal_div.h>
314 +#include <linux/log2.h>
315 +#include <linux/bootmem.h>
316 +#include <linux/ftrace.h>
318 +#include <asm/tlb.h>
319 +#include <asm/unistd.h>
321 +#define CREATE_TRACE_POINTS
322 +#include <trace/events/sched.h>
324 +#define rt_prio(prio) unlikely((prio) < MAX_RT_PRIO)
325 +#define rt_task(p) rt_prio((p)->prio)
326 +#define rt_queue(rq) rt_prio((rq)->rq_prio)
327 +#define batch_task(p) (unlikely((p)->policy == SCHED_BATCH))
328 +#define is_rt_policy(policy) ((policy) == SCHED_FIFO || \
329 + (policy) == SCHED_RR)
330 +#define has_rt_policy(p) unlikely(is_rt_policy((p)->policy))
331 +#define idleprio_task(p) unlikely((p)->policy == SCHED_IDLE)
332 +#define iso_task(p) unlikely((p)->policy == SCHED_ISO)
333 +#define iso_queue(rq) unlikely((rq)->rq_policy == SCHED_ISO)
334 +#define ISO_PERIOD ((5 * HZ * num_online_cpus()) + 1)
337 + * Convert user-nice values [ -20 ... 0 ... 19 ]
338 + * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
341 +#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
342 +#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
343 +#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
346 + * 'User priority' is the nice value converted to something we
347 + * can work with better when scaling various scheduler parameters,
348 + * it's a [ 0 ... 39 ] range.
350 +#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
351 +#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
352 +#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
353 +#define SCHED_PRIO(p) ((p)+MAX_RT_PRIO)
355 +/* Some helpers for converting to/from various scales.*/
356 +#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
357 +#define MS_TO_NS(TIME) ((TIME) * 1000000)
358 +#define MS_TO_US(TIME) ((TIME) * 1000)
362 + * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
363 + * Since cpu_power is a 'constant', we can use a reciprocal divide.
365 +static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
367 + return reciprocal_divide(load, sg->reciprocal_cpu_power);
371 + * Each time a sched group cpu_power is changed,
372 + * we must compute its reciprocal value
374 +static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
376 + sg->__cpu_power += val;
377 + sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
382 + * This is the time all tasks within the same priority round robin.
383 + * Value is in ms and set to a minimum of 6ms. Scales with number of cpus.
384 + * Tunable via /proc interface.
386 +int rr_interval __read_mostly = 6;
389 + * sched_iso_cpu - sysctl which determines the cpu percentage SCHED_ISO tasks
390 + * are allowed to run five seconds as real time tasks. This is the total over
393 +int sched_iso_cpu __read_mostly = 70;
395 +int prio_ratios[PRIO_RANGE] __read_mostly;
397 +static inline unsigned long timeslice(void)
399 + return MS_TO_US(rr_interval);
404 + unsigned long nr_running;
405 + unsigned long nr_uninterruptible;
406 + unsigned long long nr_switches;
407 + struct list_head queue[PRIO_LIMIT];
408 + DECLARE_BITMAP(prio_bitmap, PRIO_LIMIT + 1);
409 + unsigned long iso_ticks;
410 + unsigned short iso_refractory;
412 + unsigned long qnr; /* queued not running */
413 + cpumask_t cpu_idle_map;
417 +static struct global_rq grq;
420 + * This is the main, per-CPU runqueue data structure.
421 + * All this is protected by the global_rq lock.
426 + unsigned char in_nohz_recently;
430 + struct task_struct *curr, *idle;
431 + struct mm_struct *prev_mm;
432 + struct list_head queue; /* Place to store currently running task */
434 + /* Stored data about rq->curr to work outside grq lock */
435 + unsigned long rq_deadline;
436 + unsigned int rq_policy;
440 + /* Accurate timekeeping data */
441 + u64 timekeep_clock;
442 + unsigned long user_pc, nice_pc, irq_pc, softirq_pc, system_pc,
443 + iowait_pc, idle_pc;
444 + atomic_t nr_iowait;
446 + int cpu; /* cpu of this runqueue */
450 + struct root_domain *rd;
451 + struct sched_domain *sd;
453 + struct list_head migration_queue;
457 +#ifdef CONFIG_SCHEDSTATS
459 + /* latency stats */
460 + struct sched_info rq_sched_info;
461 + unsigned long long rq_cpu_time;
462 + /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
464 + /* sys_sched_yield() stats */
465 + unsigned int yld_count;
467 + /* schedule() stats */
468 + unsigned int sched_switch;
469 + unsigned int sched_count;
470 + unsigned int sched_goidle;
472 + /* try_to_wake_up() stats */
473 + unsigned int ttwu_count;
474 + unsigned int ttwu_local;
477 + unsigned int bkl_count;
481 +static DEFINE_PER_CPU(struct rq, runqueues) ____cacheline_aligned_in_smp;
482 +static DEFINE_MUTEX(sched_hotcpu_mutex);
487 + * We add the notion of a root-domain which will be used to define per-domain
488 + * variables. Each exclusive cpuset essentially defines an island domain by
489 + * fully partitioning the member cpus from any other cpuset. Whenever a new
490 + * exclusive cpuset is created, we also create and attach a new root-domain
494 +struct root_domain {
496 + cpumask_var_t span;
497 + cpumask_var_t online;
500 + * The "RT overload" flag: it gets set if a CPU has more than
501 + * one runnable RT task.
503 + cpumask_var_t rto_mask;
504 + atomic_t rto_count;
505 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
507 + * Preferred wake up cpu nominated by sched_mc balance that will be
508 + * used when most cpus are idle in the system indicating overall very
509 + * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
511 + unsigned int sched_mc_preferred_wakeup_cpu;
516 + * By default the system creates a single root-domain with all cpus as
517 + * members (mimicking the global state we have today).
519 +static struct root_domain def_root_domain;
523 +static inline int cpu_of(struct rq *rq)
533 + * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
534 + * See detach_destroy_domains: synchronize_sched for details.
536 + * The domain tree of any CPU may only be accessed from within
537 + * preempt-disabled sections.
539 +#define for_each_domain(cpu, __sd) \
540 + for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
542 +#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
543 +#define this_rq() (&__get_cpu_var(runqueues))
544 +#define task_rq(p) cpu_rq(task_cpu(p))
545 +#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
547 +#include "sched_stats.h"
549 +#ifndef prepare_arch_switch
550 +# define prepare_arch_switch(next) do { } while (0)
552 +#ifndef finish_arch_switch
553 +# define finish_arch_switch(prev) do { } while (0)
556 +inline void update_rq_clock(struct rq *rq)
558 + rq->clock = sched_clock_cpu(cpu_of(rq));
561 +static inline int task_running(struct task_struct *p)
563 + return (!!p->oncpu);
566 +static inline void grq_lock(void)
567 + __acquires(grq.lock)
570 + spin_lock(&grq.lock);
573 +static inline void grq_unlock(void)
574 + __releases(grq.lock)
576 + spin_unlock(&grq.lock);
579 +static inline void grq_lock_irq(void)
580 + __acquires(grq.lock)
583 + spin_lock_irq(&grq.lock);
586 +static inline void time_lock_grq(struct rq *rq)
587 + __acquires(grq.lock)
590 + update_rq_clock(rq);
593 +static inline void grq_unlock_irq(void)
594 + __releases(grq.lock)
596 + spin_unlock_irq(&grq.lock);
599 +static inline void grq_lock_irqsave(unsigned long *flags)
600 + __acquires(grq.lock)
603 + spin_lock_irqsave(&grq.lock, *flags);
606 +static inline void grq_unlock_irqrestore(unsigned long *flags)
607 + __releases(grq.lock)
609 + spin_unlock_irqrestore(&grq.lock, *flags);
612 +static inline struct rq
613 +*task_grq_lock(struct task_struct *p, unsigned long *flags)
614 + __acquires(grq.lock)
616 + grq_lock_irqsave(flags);
620 +static inline struct rq
621 +*time_task_grq_lock(struct task_struct *p, unsigned long *flags)
622 + __acquires(grq.lock)
624 + struct rq *rq = task_grq_lock(p, flags);
625 + update_rq_clock(rq);
629 +static inline void task_grq_unlock(unsigned long *flags)
630 + __releases(grq.lock)
632 + grq_unlock_irqrestore(flags);
636 + * runqueue_is_locked
638 + * Returns true if the global runqueue is locked.
639 + * This interface allows printk to be called with the runqueue lock
640 + * held and know whether or not it is OK to wake up the klogd.
642 +int runqueue_is_locked(void)
644 + return spin_is_locked(&grq.lock);
647 +void task_rq_unlock_wait(struct task_struct *p)
648 + __releases(grq.lock)
650 + smp_mb(); /* spin-unlock-wait is not a full memory barrier */
651 + spin_unlock_wait(&grq.lock);
654 +static inline void time_grq_lock(struct rq *rq, unsigned long *flags)
655 + __acquires(grq.lock)
657 + spin_lock_irqsave(&grq.lock, *flags);
658 + update_rq_clock(rq);
661 +static inline struct rq *__task_grq_lock(struct task_struct *p)
662 + __acquires(grq.lock)
668 +static inline void __task_grq_unlock(void)
669 + __releases(grq.lock)
674 +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
675 +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
679 +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
681 +#ifdef CONFIG_DEBUG_SPINLOCK
682 + /* this is a valid case when another task releases the spinlock */
683 + grq.lock.owner = current;
686 + * If we are tracking spinlock dependencies then we have to
687 + * fix up the runqueue lock - which gets 'carried over' from
688 + * prev into current:
690 + spin_acquire(&grq.lock.dep_map, 0, 0, _THIS_IP_);
695 +#else /* __ARCH_WANT_UNLOCKED_CTXSW */
697 +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
699 +#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
706 +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
709 +#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
710 + local_irq_enable();
713 +#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
716 + * A task that is queued will be on the grq run list.
717 + * A task that is not running or queued will not be on the grq run list.
718 + * A task that is currently running will have ->oncpu set and be queued
719 + * temporarily in its own rq queue.
720 + * A task that is running and no longer queued will be seen only on
721 + * context switch exit.
724 +static inline int task_queued(struct task_struct *p)
726 + return (!list_empty(&p->rt.run_list));
729 +static inline int task_queued_only(struct task_struct *p)
731 + return (!list_empty(&p->rt.run_list) && !task_running(p));
735 + * Removing from the global runqueue. Enter with grq locked.
737 +static void dequeue_task(struct task_struct *p)
739 + list_del_init(&p->rt.run_list);
740 + if (list_empty(grq.queue + p->prio))
741 + __clear_bit(p->prio, grq.prio_bitmap);
744 +static inline void reset_first_time_slice(struct task_struct *p)
746 + if (unlikely(p->first_time_slice))
747 + p->first_time_slice = 0;
750 +static int idleprio_suitable(struct task_struct *p)
752 + return (!freezing(p) && !signal_pending(p) &&
753 + !(task_contributes_to_load(p)) && !(p->flags & (PF_EXITING)));
756 +static int isoprio_suitable(void)
758 + return !grq.iso_refractory;
762 + * Adding to the global runqueue. Enter with grq locked.
764 +static void enqueue_task(struct task_struct *p)
767 + /* Check it hasn't gotten rt from PI */
768 + if ((idleprio_task(p) && idleprio_suitable(p)) ||
769 + (iso_task(p) && isoprio_suitable()))
770 + p->prio = p->normal_prio;
772 + p->prio = NORMAL_PRIO;
774 + __set_bit(p->prio, grq.prio_bitmap);
775 + list_add_tail(&p->rt.run_list, grq.queue + p->prio);
776 + sched_info_queued(p);
779 +/* Only idle task does this as a real time task*/
780 +static inline void enqueue_task_head(struct task_struct *p)
782 + __set_bit(p->prio, grq.prio_bitmap);
783 + list_add(&p->rt.run_list, grq.queue + p->prio);
784 + sched_info_queued(p);
787 +static inline void requeue_task(struct task_struct *p)
789 + sched_info_queued(p);
792 +static inline int pratio(struct task_struct *p)
794 + return prio_ratios[TASK_USER_PRIO(p)];
798 + * task_timeslice - all tasks of all priorities get the exact same timeslice
799 + * length. CPU distribution is handled by giving different deadlines to
800 + * tasks of different priorities.
802 +static inline int task_timeslice(struct task_struct *p)
804 + return (rr_interval * pratio(p) / 100);
808 +static inline void inc_qnr(void)
813 +static inline void dec_qnr(void)
818 +static inline int queued_notrunning(void)
823 +static inline void inc_qnr(void)
827 +static inline void dec_qnr(void)
831 +static inline int queued_notrunning(void)
833 + return grq.nr_running;
838 + * activate_idle_task - move idle task to the _front_ of runqueue.
840 +static inline void activate_idle_task(struct task_struct *p)
842 + enqueue_task_head(p);
847 +static inline int normal_prio(struct task_struct *p)
849 + if (has_rt_policy(p))
850 + return MAX_RT_PRIO - 1 - p->rt_priority;
851 + if (idleprio_task(p))
855 + return NORMAL_PRIO;
859 + * Calculate the current priority, i.e. the priority
860 + * taken into account by the scheduler. This value might
861 + * be boosted by RT tasks as it will be RT if the task got
862 + * RT-boosted. If not then it returns p->normal_prio.
864 +static int effective_prio(struct task_struct *p)
866 + p->normal_prio = normal_prio(p);
868 + * If we are RT tasks or we were boosted to RT priority,
869 + * keep the priority unchanged. Otherwise, update priority
870 + * to the normal priority:
872 + if (!rt_prio(p->prio))
873 + return p->normal_prio;
878 + * activate_task - move a task to the runqueue. Enter with grq locked. The rq
879 + * doesn't really matter but gives us the local clock.
881 +static void activate_task(struct task_struct *p, struct rq *rq)
883 + u64 now = rq->clock;
886 + * Sleep time is in units of nanosecs, so shift by 20 to get a
887 + * milliseconds-range estimation of the amount of time that the task
890 + if (unlikely(prof_on == SLEEP_PROFILING)) {
891 + if (p->state == TASK_UNINTERRUPTIBLE)
892 + profile_hits(SLEEP_PROFILING, (void *)get_wchan(p),
893 + (now - p->timestamp) >> 20);
896 + p->prio = effective_prio(p);
897 + p->timestamp = now;
898 + if (task_contributes_to_load(p))
899 + grq.nr_uninterruptible--;
906 + * deactivate_task - If it's running, it's not on the grq and we can just
907 + * decrement the nr_running.
909 +static inline void deactivate_task(struct task_struct *p)
911 + if (task_contributes_to_load(p))
912 + grq.nr_uninterruptible++;
917 +void set_task_cpu(struct task_struct *p, unsigned int cpu)
919 + trace_sched_migrate_task(p, cpu);
921 + * After ->cpu is set up to a new value, task_grq_lock(p, ...) can be
922 + * successfuly executed on another CPU. We must ensure that updates of
923 + * per-task data have been completed by this moment.
926 + task_thread_info(p)->cpu = cpu;
931 + * Move a task off the global queue and take it to a cpu for it will
932 + * become the running task.
934 +static inline void take_task(struct rq *rq, struct task_struct *p)
936 + set_task_cpu(p, rq->cpu);
938 + list_add(&p->rt.run_list, &rq->queue);
943 + * Returns a descheduling task to the grq runqueue unless it is being
946 +static inline void return_task(struct task_struct *p, int deactivate)
948 + list_del_init(&p->rt.run_list);
950 + deactivate_task(p);
958 + * resched_task - mark a task 'to be rescheduled now'.
960 + * On UP this means the setting of the need_resched flag, on SMP it
961 + * might also involve a cross-CPU call to trigger the scheduler on
966 +#ifndef tsk_is_polling
967 +#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
970 +static void resched_task(struct task_struct *p)
974 + assert_spin_locked(&grq.lock);
976 + if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
979 + set_tsk_thread_flag(p, TIF_NEED_RESCHED);
982 + if (cpu == smp_processor_id())
985 + /* NEED_RESCHED must be visible before we test polling */
987 + if (!tsk_is_polling(p))
988 + smp_send_reschedule(cpu);
992 +static inline void resched_task(struct task_struct *p)
994 + assert_spin_locked(&grq.lock);
995 + set_tsk_need_resched(p);
1000 + * task_curr - is this task currently executing on a CPU?
1001 + * @p: the task in question.
1003 +inline int task_curr(const struct task_struct *p)
1005 + return cpu_curr(task_cpu(p)) == p;
1009 +struct migration_req {
1010 + struct list_head list;
1012 + struct task_struct *task;
1015 + struct completion done;
1019 + * wait_task_context_switch - wait for a thread to complete at least one
1022 + * @p must not be current.
1024 +void wait_task_context_switch(struct task_struct *p)
1026 + unsigned long nvcsw, nivcsw, flags;
1031 + nivcsw = p->nivcsw;
1034 + * The runqueue is assigned before the actual context
1035 + * switch. We need to take the runqueue lock.
1037 + * We could check initially without the lock but it is
1038 + * very likely that we need to take the lock in every
1041 + rq = task_grq_lock(p, &flags);
1042 + running = task_running(p);
1043 + task_grq_unlock(&flags);
1045 + if (likely(!running))
1048 + * The switch count is incremented before the actual
1049 + * context switch. We thus wait for two switches to be
1050 + * sure at least one completed.
1052 + if ((p->nvcsw - nvcsw) > 1)
1054 + if ((p->nivcsw - nivcsw) > 1)
1062 + * wait_task_inactive - wait for a thread to unschedule.
1064 + * If @match_state is nonzero, it's the @p->state value just checked and
1065 + * not expected to change. If it changes, i.e. @p might have woken up,
1066 + * then return zero. When we succeed in waiting for @p to be off its CPU,
1067 + * we return a positive number (its total switch count). If a second call
1068 + * a short while later returns the same number, the caller can be sure that
1069 + * @p has remained unscheduled the whole time.
1071 + * The caller must ensure that the task *will* unschedule sometime soon,
1072 + * else this function might spin for a *long* time. This function can't
1073 + * be called with interrupts off, or it may introduce deadlock with
1074 + * smp_call_function() if an IPI is sent by the same process we are
1075 + * waiting to become inactive.
1077 +unsigned long wait_task_inactive(struct task_struct *p, long match_state)
1079 + unsigned long flags;
1080 + int running, on_rq;
1081 + unsigned long ncsw;
1086 + * We do the initial early heuristics without holding
1087 + * any task-queue locks at all. We'll only try to get
1088 + * the runqueue lock when things look like they will
1094 + * If the task is actively running on another CPU
1095 + * still, just relax and busy-wait without holding
1098 + * NOTE! Since we don't hold any locks, it's not
1099 + * even sure that "rq" stays as the right runqueue!
1100 + * But we don't care, since this will
1101 + * return false if the runqueue has changed and p
1102 + * is actually now running somewhere else!
1104 + while (task_running(p) && p == rq->curr) {
1105 + if (match_state && unlikely(p->state != match_state))
1111 + * Ok, time to look more closely! We need the grq
1112 + * lock now, to be *sure*. If we're wrong, we'll
1113 + * just go back and repeat.
1115 + rq = task_grq_lock(p, &flags);
1116 + trace_sched_wait_task(rq, p);
1117 + running = task_running(p);
1118 + on_rq = task_queued(p);
1120 + if (!match_state || p->state == match_state)
1121 + ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
1122 + task_grq_unlock(&flags);
1125 + * If it changed from the expected state, bail out now.
1127 + if (unlikely(!ncsw))
1131 + * Was it really running after all now that we
1132 + * checked with the proper locks actually held?
1134 + * Oops. Go back and try again..
1136 + if (unlikely(running)) {
1142 + * It's not enough that it's not actively running,
1143 + * it must be off the runqueue _entirely_, and not
1146 + * So if it was still runnable (but just not actively
1147 + * running right now), it's preempted, and we should
1148 + * yield - it could be a while.
1150 + if (unlikely(on_rq)) {
1151 + schedule_timeout_uninterruptible(1);
1156 + * Ahh, all good. It wasn't running, and it wasn't
1157 + * runnable, which means that it will never become
1158 + * running in the future either. We're all done!
1167 + * kick_process - kick a running thread to enter/exit the kernel
1168 + * @p: the to-be-kicked thread
1170 + * Cause a process which is running on another CPU to enter
1171 + * kernel-mode, without any delay. (to get signals handled.)
1173 + * NOTE: this function doesnt have to take the runqueue lock,
1174 + * because all it wants to ensure is that the remote task enters
1175 + * the kernel. If the IPI races and the task has been migrated
1176 + * to another CPU then no harm is done and the purpose has been
1177 + * achieved as well.
1179 +void kick_process(struct task_struct *p)
1183 + preempt_disable();
1184 + cpu = task_cpu(p);
1185 + if ((cpu != smp_processor_id()) && task_curr(p))
1186 + smp_send_reschedule(cpu);
1189 +EXPORT_SYMBOL_GPL(kick_process);
1192 +#define rq_idle(rq) ((rq)->rq_prio == PRIO_LIMIT)
1195 + * RT tasks preempt purely on priority. SCHED_NORMAL tasks preempt on the
1196 + * basis of earlier deadlines. SCHED_BATCH and SCHED_IDLE don't preempt,
1197 + * they cooperatively multitask.
1199 +static inline int task_preempts_curr(struct task_struct *p, struct rq *rq)
1203 + if (p->prio < rq->rq_prio)
1205 + else if (p->policy == SCHED_NORMAL && (p->prio == rq->rq_prio &&
1206 + time_before(p->deadline, rq->rq_deadline)))
1212 + * Wake up *any* suitable cpu to schedule this task.
1214 +static void try_preempt(struct task_struct *p)
1216 + struct rq *highest_prio_rq, *this_rq;
1217 + unsigned long latest_deadline, cpu;
1221 + /* Try the task's previous rq first and as a fallback */
1222 + this_rq = task_rq(p);
1224 + if (cpu_isset(this_rq->cpu, p->cpus_allowed)) {
1225 + highest_prio_rq = this_rq;
1226 + /* If this_rq is idle, use that. */
1227 + if (rq_idle(this_rq))
1230 + highest_prio_rq = cpu_rq(any_online_cpu(p->cpus_allowed));
1231 + latest_deadline = this_rq->rq_deadline;
1232 + highest_prio = this_rq->rq_prio;
1234 + cpus_and(tmp, cpu_online_map, p->cpus_allowed);
1236 + for_each_cpu_mask(cpu, tmp) {
1242 + if (rq_idle(rq)) {
1243 + /* found an idle rq, use that one */
1244 + highest_prio_rq = rq;
1248 + rq_prio = rq->rq_prio;
1249 + if (rq_prio > highest_prio ||
1250 + (rq_prio == highest_prio &&
1251 + time_after(rq->rq_deadline, latest_deadline))) {
1252 + highest_prio = rq_prio;
1253 + latest_deadline = rq->rq_deadline;
1254 + highest_prio_rq = rq;
1258 + if (!task_preempts_curr(p, highest_prio_rq))
1261 + resched_task(highest_prio_rq->curr);
1266 + * task_oncpu_function_call - call a function on the cpu on which a task runs
1267 + * @p: the task to evaluate
1268 + * @func: the function to be called
1269 + * @info: the function call argument
1271 + * Calls the function @func when the task is currently running. This might
1272 + * be on the current CPU, which just calls the function directly
1274 +void task_oncpu_function_call(struct task_struct *p,
1275 + void (*func) (void *info), void *info)
1279 + preempt_disable();
1280 + cpu = task_cpu(p);
1282 + smp_call_function_single(cpu, func, info, 1);
1287 +static int suitable_idle_cpus(struct task_struct *p)
1289 + return (cpus_intersects(p->cpus_allowed, grq.cpu_idle_map));
1292 +static int suitable_idle_cpus(struct task_struct *p)
1299 + * try_to_wake_up - wake up a thread
1300 + * @p: the to-be-woken-up thread
1301 + * @state: the mask of task states that can be woken
1302 + * @sync: do a synchronous wakeup?
1304 + * Put it on the run-queue if it's not already there. The "current"
1305 + * thread is always on the run-queue (except when the actual
1306 + * re-schedule is in progress), and as such you're allowed to do
1307 + * the simpler "current->state = TASK_RUNNING" to mark yourself
1308 + * runnable without the overhead of this.
1310 + * returns failure only if the task is already active.
1312 +static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
1314 + unsigned long flags;
1319 + rq = time_task_grq_lock(p, &flags);
1320 + old_state = p->state;
1321 + if (!(old_state & state))
1325 + * Note this catches tasks that are running and queued, but returns
1326 + * false during the context switch when they're running and no
1329 + if (task_queued(p))
1332 + activate_task(p, rq);
1334 + * Sync wakeups (i.e. those types of wakeups where the waker
1335 + * has indicated that it will leave the CPU in short order)
1336 + * don't trigger a preemption if there are no idle cpus,
1337 + * instead waiting for current to deschedule.
1339 + if (!sync || (sync && suitable_idle_cpus(p)))
1344 + trace_sched_wakeup(rq, p, success);
1345 + p->state = TASK_RUNNING;
1347 + task_grq_unlock(&flags);
1352 + * wake_up_process - Wake up a specific process
1353 + * @p: The process to be woken up.
1355 + * Attempt to wake up the nominated process and move it to the set of runnable
1356 + * processes. Returns 1 if the process was woken up, 0 if it was already
1359 + * It may be assumed that this function implies a write memory barrier before
1360 + * changing the task state if and only if any tasks are woken up.
1362 +int wake_up_process(struct task_struct *p)
1364 + return try_to_wake_up(p, TASK_ALL, 0);
1366 +EXPORT_SYMBOL(wake_up_process);
1368 +int wake_up_state(struct task_struct *p, unsigned int state)
1370 + return try_to_wake_up(p, state, 0);
1374 + * Perform scheduler related setup for a newly forked process p.
1375 + * p is forked by current.
1377 +void sched_fork(struct task_struct *p, int clone_flags)
1379 + int cpu = get_cpu();
1382 +#ifdef CONFIG_PREEMPT_NOTIFIERS
1383 + INIT_HLIST_HEAD(&p->preempt_notifiers);
1386 + * We mark the process as running here, but have not actually
1387 + * inserted it onto the runqueue yet. This guarantees that
1388 + * nobody will actually run it, and a signal or other external
1389 + * event cannot wake it up and insert it on the runqueue either.
1391 + p->state = TASK_RUNNING;
1392 + set_task_cpu(p, cpu);
1394 + /* Should be reset in fork.c but done here for ease of bfs patching */
1395 + p->se.sum_exec_runtime = p->stime_pc = p->utime_pc = 0;
1398 + * Make sure we do not leak PI boosting priority to the child:
1400 + p->prio = current->normal_prio;
1402 + INIT_LIST_HEAD(&p->rt.run_list);
1403 +#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
1404 + if (unlikely(sched_info_on()))
1405 + memset(&p->sched_info, 0, sizeof(p->sched_info));
1410 +#ifdef CONFIG_PREEMPT
1411 + /* Want to start with kernel preemption disabled. */
1412 + task_thread_info(p)->preempt_count = 1;
1414 + if (unlikely(p->policy == SCHED_FIFO))
1417 + * Share the timeslice between parent and child, thus the
1418 + * total amount of pending timeslices in the system doesn't change,
1419 + * resulting in more scheduling fairness. If it's negative, it won't
1420 + * matter since that's the same as being 0. current's time_slice is
1421 + * actually in rq_time_slice when it's running.
1423 + local_irq_disable();
1424 + rq = task_rq(current);
1425 + if (likely(rq->rq_time_slice > 0)) {
1426 + rq->rq_time_slice /= 2;
1428 + * The remainder of the first timeslice might be recovered by
1429 + * the parent if the child exits early enough.
1431 + p->first_time_slice = 1;
1433 + p->rt.time_slice = rq->rq_time_slice;
1434 + local_irq_enable();
1440 + * wake_up_new_task - wake up a newly created task for the first time.
1442 + * This function will do some initial scheduler statistics housekeeping
1443 + * that must be done for every newly created context, then puts the task
1444 + * on the runqueue and wakes it.
1446 +void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
1448 + struct task_struct *parent;
1449 + unsigned long flags;
1452 + rq = time_task_grq_lock(p, &flags); ;
1453 + parent = p->parent;
1454 + BUG_ON(p->state != TASK_RUNNING);
1455 + set_task_cpu(p, task_cpu(parent));
1457 + activate_task(p, rq);
1458 + trace_sched_wakeup_new(rq, p, 1);
1459 + if (!(clone_flags & CLONE_VM) && rq->curr == parent &&
1460 + !suitable_idle_cpus(p)) {
1462 + * The VM isn't cloned, so we're in a good position to
1463 + * do child-runs-first in anticipation of an exec. This
1464 + * usually avoids a lot of COW overhead.
1466 + resched_task(parent);
1469 + task_grq_unlock(&flags);
1473 + * Potentially available exiting-child timeslices are
1474 + * retrieved here - this way the parent does not get
1475 + * penalized for creating too many threads.
1477 + * (this cannot be used to 'generate' timeslices
1478 + * artificially, because any timeslice recovered here
1479 + * was given away by the parent in the first place.)
1481 +void sched_exit(struct task_struct *p)
1483 + struct task_struct *parent;
1484 + unsigned long flags;
1487 + if (p->first_time_slice) {
1488 + parent = p->parent;
1489 + rq = task_grq_lock(parent, &flags);
1490 + parent->rt.time_slice += p->rt.time_slice;
1491 + if (unlikely(parent->rt.time_slice > timeslice()))
1492 + parent->rt.time_slice = timeslice();
1493 + task_grq_unlock(&flags);
1497 +#ifdef CONFIG_PREEMPT_NOTIFIERS
1500 + * preempt_notifier_register - tell me when current is being preempted & rescheduled
1501 + * @notifier: notifier struct to register
1503 +void preempt_notifier_register(struct preempt_notifier *notifier)
1505 + hlist_add_head(¬ifier->link, ¤t->preempt_notifiers);
1507 +EXPORT_SYMBOL_GPL(preempt_notifier_register);
1510 + * preempt_notifier_unregister - no longer interested in preemption notifications
1511 + * @notifier: notifier struct to unregister
1513 + * This is safe to call from within a preemption notifier.
1515 +void preempt_notifier_unregister(struct preempt_notifier *notifier)
1517 + hlist_del(¬ifier->link);
1519 +EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
1521 +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1523 + struct preempt_notifier *notifier;
1524 + struct hlist_node *node;
1526 + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1527 + notifier->ops->sched_in(notifier, raw_smp_processor_id());
1531 +fire_sched_out_preempt_notifiers(struct task_struct *curr,
1532 + struct task_struct *next)
1534 + struct preempt_notifier *notifier;
1535 + struct hlist_node *node;
1537 + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1538 + notifier->ops->sched_out(notifier, next);
1541 +#else /* !CONFIG_PREEMPT_NOTIFIERS */
1543 +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1548 +fire_sched_out_preempt_notifiers(struct task_struct *curr,
1549 + struct task_struct *next)
1553 +#endif /* CONFIG_PREEMPT_NOTIFIERS */
1556 + * prepare_task_switch - prepare to switch tasks
1557 + * @rq: the runqueue preparing to switch
1558 + * @next: the task we are going to switch to.
1560 + * This is called with the rq lock held and interrupts off. It must
1561 + * be paired with a subsequent finish_task_switch after the context
1564 + * prepare_task_switch sets up locking and calls architecture specific
1568 +prepare_task_switch(struct rq *rq, struct task_struct *prev,
1569 + struct task_struct *next)
1571 + fire_sched_out_preempt_notifiers(prev, next);
1572 + prepare_lock_switch(rq, next);
1573 + prepare_arch_switch(next);
1577 + * finish_task_switch - clean up after a task-switch
1578 + * @rq: runqueue associated with task-switch
1579 + * @prev: the thread we just switched away from.
1581 + * finish_task_switch must be called after the context switch, paired
1582 + * with a prepare_task_switch call before the context switch.
1583 + * finish_task_switch will reconcile locking set up by prepare_task_switch,
1584 + * and do any other architecture-specific cleanup actions.
1586 + * Note that we may have delayed dropping an mm in context_switch(). If
1587 + * so, we finish that here outside of the runqueue lock. (Doing it
1588 + * with the lock held can cause deadlocks; see schedule() for
1591 +static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
1592 + __releases(grq.lock)
1594 + struct mm_struct *mm = rq->prev_mm;
1597 + rq->prev_mm = NULL;
1600 + * A task struct has one reference for the use as "current".
1601 + * If a task dies, then it sets TASK_DEAD in tsk->state and calls
1602 + * schedule one last time. The schedule call will never return, and
1603 + * the scheduled task must drop that reference.
1604 + * The test for TASK_DEAD must occur while the runqueue locks are
1605 + * still held, otherwise prev could be scheduled on another cpu, die
1606 + * there before we look at prev->state, and then the reference would
1607 + * be dropped twice.
1608 + * Manfred Spraul <manfred@colorfullife.com>
1610 + prev_state = prev->state;
1611 + finish_arch_switch(prev);
1612 + perf_counter_task_sched_in(current, cpu_of(rq));
1613 + finish_lock_switch(rq, prev);
1615 + fire_sched_in_preempt_notifiers(current);
1618 + if (unlikely(prev_state == TASK_DEAD)) {
1620 + * Remove function-return probe instances associated with this
1621 + * task and put them back on the free list.
1623 + kprobe_flush_task(prev);
1624 + put_task_struct(prev);
1629 + * schedule_tail - first thing a freshly forked thread must call.
1630 + * @prev: the thread we just switched away from.
1632 +asmlinkage void schedule_tail(struct task_struct *prev)
1633 + __releases(grq.lock)
1635 + struct rq *rq = this_rq();
1637 + finish_task_switch(rq, prev);
1638 +#ifdef __ARCH_WANT_UNLOCKED_CTXSW
1639 + /* In this case, finish_task_switch does not reenable preemption */
1642 + if (current->set_child_tid)
1643 + put_user(current->pid, current->set_child_tid);
1647 + * context_switch - switch to the new MM and the new
1648 + * thread's register state.
1651 +context_switch(struct rq *rq, struct task_struct *prev,
1652 + struct task_struct *next)
1654 + struct mm_struct *mm, *oldmm;
1656 + prepare_task_switch(rq, prev, next);
1657 + trace_sched_switch(rq, prev, next);
1659 + oldmm = prev->active_mm;
1661 + * For paravirt, this is coupled with an exit in switch_to to
1662 + * combine the page table reload and the switch backend into
1665 + arch_start_context_switch(prev);
1667 + if (unlikely(!mm)) {
1668 + next->active_mm = oldmm;
1669 + atomic_inc(&oldmm->mm_count);
1670 + enter_lazy_tlb(oldmm, next);
1672 + switch_mm(oldmm, mm, next);
1674 + if (unlikely(!prev->mm)) {
1675 + prev->active_mm = NULL;
1676 + rq->prev_mm = oldmm;
1679 + * Since the runqueue lock will be released by the next
1680 + * task (which is an invalid locking op but in the case
1681 + * of the scheduler it's an obvious special-case), so we
1682 + * do an early lockdep release here:
1684 +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
1685 + spin_release(&grq.lock.dep_map, 1, _THIS_IP_);
1688 + /* Here we just switch the register state and the stack. */
1689 + switch_to(prev, next, prev);
1693 + * this_rq must be evaluated again because prev may have moved
1694 + * CPUs since it called schedule(), thus the 'rq' on its stack
1695 + * frame will be invalid.
1697 + finish_task_switch(this_rq(), prev);
1701 + * nr_running, nr_uninterruptible and nr_context_switches:
1703 + * externally visible scheduler statistics: current number of runnable
1704 + * threads, current number of uninterruptible-sleeping threads, total
1705 + * number of context switches performed since bootup. All are measured
1706 + * without grabbing the grq lock but the occasional inaccurate result
1707 + * doesn't matter so long as it's positive.
1709 +unsigned long nr_running(void)
1711 + long nr = grq.nr_running;
1713 + if (unlikely(nr < 0))
1715 + return (unsigned long)nr;
1718 +unsigned long nr_uninterruptible(void)
1720 + unsigned long nu = grq.nr_uninterruptible;
1722 + if (unlikely(nu < 0))
1727 +unsigned long long nr_context_switches(void)
1729 + long long ns = grq.nr_switches;
1731 + /* This is of course impossible */
1732 + if (unlikely(ns < 0))
1734 + return (long long)ns;
1737 +unsigned long nr_iowait(void)
1739 + unsigned long i, sum = 0;
1741 + for_each_possible_cpu(i)
1742 + sum += atomic_read(&cpu_rq(i)->nr_iowait);
1747 +unsigned long nr_active(void)
1749 + return nr_running() + nr_uninterruptible();
1752 +/* Variables and functions for calc_load */
1753 +static unsigned long calc_load_update;
1754 +unsigned long avenrun[3];
1755 +EXPORT_SYMBOL(avenrun);
1758 + * get_avenrun - get the load average array
1759 + * @loads: pointer to dest load array
1760 + * @offset: offset to add
1761 + * @shift: shift count to shift the result left
1763 + * These values are estimates at best, so no need for locking.
1765 +void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
1767 + loads[0] = (avenrun[0] + offset) << shift;
1768 + loads[1] = (avenrun[1] + offset) << shift;
1769 + loads[2] = (avenrun[2] + offset) << shift;
1772 +static unsigned long
1773 +calc_load(unsigned long load, unsigned long exp, unsigned long active)
1776 + load += active * (FIXED_1 - exp);
1777 + return load >> FSHIFT;
1781 + * calc_load - update the avenrun load estimates every LOAD_FREQ seconds.
1783 +void calc_global_load(void)
1787 + if (time_before(jiffies, calc_load_update))
1789 + active = nr_active() * FIXED_1;
1791 + avenrun[0] = calc_load(avenrun[0], EXP_1, active);
1792 + avenrun[1] = calc_load(avenrun[1], EXP_5, active);
1793 + avenrun[2] = calc_load(avenrun[2], EXP_15, active);
1795 + calc_load_update = jiffies + LOAD_FREQ;
1798 +DEFINE_PER_CPU(struct kernel_stat, kstat);
1800 +EXPORT_PER_CPU_SYMBOL(kstat);
1803 + * On each tick, see what percentage of that tick was attributed to each
1804 + * component and add the percentage to the _pc values. Once a _pc value has
1805 + * accumulated one tick's worth, account for that. This means the total
1806 + * percentage of load components will always be 100 per tick.
1808 +static void pc_idle_time(struct rq *rq, unsigned long pc)
1810 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
1811 + cputime64_t tmp = cputime_to_cputime64(jiffies_to_cputime(1));
1813 + if (atomic_read(&rq->nr_iowait) > 0) {
1814 + rq->iowait_pc += pc;
1815 + if (rq->iowait_pc >= 100) {
1816 + rq->iowait_pc %= 100;
1817 + cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
1820 + rq->idle_pc += pc;
1821 + if (rq->idle_pc >= 100) {
1822 + rq->idle_pc %= 100;
1823 + cpustat->idle = cputime64_add(cpustat->idle, tmp);
1829 +pc_system_time(struct rq *rq, struct task_struct *p, int hardirq_offset,
1830 + unsigned long pc, unsigned long ns)
1832 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
1833 + cputime_t one_jiffy = jiffies_to_cputime(1);
1834 + cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
1835 + cputime64_t tmp = cputime_to_cputime64(one_jiffy);
1837 + p->stime_pc += pc;
1838 + if (p->stime_pc >= 100) {
1839 + p->stime_pc -= 100;
1840 + p->stime = cputime_add(p->stime, one_jiffy);
1841 + p->stimescaled = cputime_add(p->stimescaled, one_jiffy_scaled);
1842 + account_group_system_time(p, one_jiffy);
1843 + acct_update_integrals(p);
1845 + p->se.sum_exec_runtime += ns;
1847 + if (hardirq_count() - hardirq_offset)
1849 + else if (softirq_count()) {
1850 + rq->softirq_pc += pc;
1851 + if (rq->softirq_pc >= 100) {
1852 + rq->softirq_pc %= 100;
1853 + cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
1856 + rq->system_pc += pc;
1857 + if (rq->system_pc >= 100) {
1858 + rq->system_pc %= 100;
1859 + cpustat->system = cputime64_add(cpustat->system, tmp);
1864 +static void pc_user_time(struct rq *rq, struct task_struct *p,
1865 + unsigned long pc, unsigned long ns)
1867 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
1868 + cputime_t one_jiffy = jiffies_to_cputime(1);
1869 + cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
1870 + cputime64_t tmp = cputime_to_cputime64(one_jiffy);
1872 + p->utime_pc += pc;
1873 + if (p->utime_pc >= 100) {
1874 + p->utime_pc -= 100;
1875 + p->utime = cputime_add(p->utime, one_jiffy);
1876 + p->utimescaled = cputime_add(p->utimescaled, one_jiffy_scaled);
1877 + account_group_user_time(p, one_jiffy);
1878 + acct_update_integrals(p);
1880 + p->se.sum_exec_runtime += ns;
1882 + if (TASK_NICE(p) > 0 || idleprio_task(p)) {
1883 + rq->nice_pc += pc;
1884 + if (rq->nice_pc >= 100) {
1885 + rq->nice_pc %= 100;
1886 + cpustat->nice = cputime64_add(cpustat->nice, tmp);
1889 + rq->user_pc += pc;
1890 + if (rq->user_pc >= 100) {
1891 + rq->user_pc %= 100;
1892 + cpustat->user = cputime64_add(cpustat->user, tmp);
1897 +/* Convert nanoseconds to percentage of one tick. */
1898 +#define NS_TO_PC(NS) (NS * 100 / JIFFIES_TO_NS(1))
1901 + * This is called on clock ticks and on context switches.
1902 + * Bank in p->se.sum_exec_runtime the ns elapsed since the last tick or switch.
1903 + * CPU scheduler quota accounting is also performed here in microseconds.
1904 + * The value returned from sched_clock() occasionally gives bogus values so
1905 + * some sanity checking is required. Time is supposed to be banked all the
1906 + * time so default to half a tick to make up for when sched_clock reverts
1907 + * to just returning jiffies, and for hardware that can't do tsc.
1910 +update_cpu_clock(struct rq *rq, struct task_struct *p, int tick)
1912 + long time_diff = rq->clock - p->last_ran;
1913 + long account_ns = rq->clock - rq->timekeep_clock;
1914 + struct task_struct *idle = rq->idle;
1915 + unsigned long account_pc;
1918 + * There should be less than or equal to one jiffy worth, and not
1919 + * negative/overflow. time_diff is only used for internal scheduler
1920 + * time_slice accounting.
1922 + if (time_diff <= 0)
1923 + time_diff = JIFFIES_TO_NS(1) / 2;
1924 + else if (time_diff > JIFFIES_TO_NS(1))
1925 + time_diff = JIFFIES_TO_NS(1);
1927 + if (unlikely(account_ns < 0))
1930 + account_pc = NS_TO_PC(account_ns);
1933 + int user_tick = user_mode(get_irq_regs());
1935 + /* Accurate tick timekeeping */
1937 + pc_user_time(rq, p, account_pc, account_ns);
1938 + else if (p != idle || (irq_count() != HARDIRQ_OFFSET))
1939 + pc_system_time(rq, p, HARDIRQ_OFFSET,
1940 + account_pc, account_ns);
1942 + pc_idle_time(rq, account_pc);
1944 + /* Accurate subtick timekeeping */
1946 + pc_idle_time(rq, account_pc);
1948 + pc_user_time(rq, p, account_pc, account_ns);
1951 + /* time_slice accounting is done in usecs to avoid overflow on 32bit */
1952 + if (rq->rq_policy != SCHED_FIFO && p != idle)
1953 + rq->rq_time_slice -= time_diff / 1000;
1954 + p->last_ran = rq->timekeep_clock = rq->clock;
1958 + * Return any ns on the sched_clock that have not yet been accounted in
1959 + * @p in case that task is currently running.
1961 + * Called with task_grq_lock() held on @rq.
1963 +static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
1967 + if (p == rq->curr) {
1968 + update_rq_clock(rq);
1969 + ns = rq->clock - p->last_ran;
1977 +unsigned long long task_delta_exec(struct task_struct *p)
1979 + unsigned long flags;
1983 + rq = task_grq_lock(p, &flags);
1984 + ns = do_task_delta_exec(p, rq);
1985 + task_grq_unlock(&flags);
1991 + * Return accounted runtime for the task.
1992 + * In case the task is currently running, return the runtime plus current's
1993 + * pending runtime that have not been accounted yet.
1995 +unsigned long long task_sched_runtime(struct task_struct *p)
1997 + unsigned long flags;
2001 + rq = task_grq_lock(p, &flags);
2002 + ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
2003 + task_grq_unlock(&flags);
2009 + * Return sum_exec_runtime for the thread group.
2010 + * In case the task is currently running, return the sum plus current's
2011 + * pending runtime that have not been accounted yet.
2013 + * Note that the thread group might have other running tasks as well,
2014 + * so the return value not includes other pending runtime that other
2015 + * running tasks might have.
2017 +unsigned long long thread_group_sched_runtime(struct task_struct *p)
2019 + struct task_cputime totals;
2020 + unsigned long flags;
2024 + rq = task_grq_lock(p, &flags);
2025 + thread_group_cputime(p, &totals);
2026 + ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
2027 + task_grq_unlock(&flags);
2032 +/* Compatibility crap for removal */
2033 +void account_user_time(struct task_struct *p, cputime_t cputime,
2034 + cputime_t cputime_scaled)
2038 +void account_idle_time(cputime_t cputime)
2043 + * Account guest cpu time to a process.
2044 + * @p: the process that the cpu time gets accounted to
2045 + * @cputime: the cpu time spent in virtual machine since the last update
2046 + * @cputime_scaled: cputime scaled by cpu frequency
2048 +static void account_guest_time(struct task_struct *p, cputime_t cputime,
2049 + cputime_t cputime_scaled)
2052 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2054 + tmp = cputime_to_cputime64(cputime);
2056 + /* Add guest time to process. */
2057 + p->utime = cputime_add(p->utime, cputime);
2058 + p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
2059 + account_group_user_time(p, cputime);
2060 + p->gtime = cputime_add(p->gtime, cputime);
2062 + /* Add guest time to cpustat. */
2063 + cpustat->user = cputime64_add(cpustat->user, tmp);
2064 + cpustat->guest = cputime64_add(cpustat->guest, tmp);
2068 + * Account system cpu time to a process.
2069 + * @p: the process that the cpu time gets accounted to
2070 + * @hardirq_offset: the offset to subtract from hardirq_count()
2071 + * @cputime: the cpu time spent in kernel space since the last update
2072 + * @cputime_scaled: cputime scaled by cpu frequency
2073 + * This is for guest only now.
2075 +void account_system_time(struct task_struct *p, int hardirq_offset,
2076 + cputime_t cputime, cputime_t cputime_scaled)
2079 + if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
2080 + account_guest_time(p, cputime, cputime_scaled);
2084 + * Account for involuntary wait time.
2085 + * @steal: the cpu time spent in involuntary wait
2087 +void account_steal_time(cputime_t cputime)
2089 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2090 + cputime64_t cputime64 = cputime_to_cputime64(cputime);
2092 + cpustat->steal = cputime64_add(cpustat->steal, cputime64);
2096 + * Account for idle time.
2097 + * @cputime: the cpu time spent in idle wait
2099 +static void account_idle_times(cputime_t cputime)
2101 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2102 + cputime64_t cputime64 = cputime_to_cputime64(cputime);
2103 + struct rq *rq = this_rq();
2105 + if (atomic_read(&rq->nr_iowait) > 0)
2106 + cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
2108 + cpustat->idle = cputime64_add(cpustat->idle, cputime64);
2111 +#ifndef CONFIG_VIRT_CPU_ACCOUNTING
2113 +void account_process_tick(struct task_struct *p, int user_tick)
2118 + * Account multiple ticks of steal time.
2119 + * @p: the process from which the cpu time has been stolen
2120 + * @ticks: number of stolen ticks
2122 +void account_steal_ticks(unsigned long ticks)
2124 + account_steal_time(jiffies_to_cputime(ticks));
2128 + * Account multiple ticks of idle time.
2129 + * @ticks: number of stolen ticks
2131 +void account_idle_ticks(unsigned long ticks)
2133 + account_idle_times(jiffies_to_cputime(ticks));
2138 + * Functions to test for when SCHED_ISO tasks have used their allocated
2139 + * quota as real time scheduling and convert them back to SCHED_NORMAL.
2140 + * Where possible, the data is tested lockless, to avoid grabbing grq_lock
2141 + * because the occasional inaccurate result won't matter. However the
2142 + * data is only ever modified under lock.
2144 +static void set_iso_refractory(void)
2147 + grq.iso_refractory = 1;
2151 +static void clear_iso_refractory(void)
2154 + grq.iso_refractory = 0;
2159 + * Test if SCHED_ISO tasks have run longer than their alloted period as RT
2160 + * tasks and set the refractory flag if necessary. There is 10% hysteresis
2161 + * for unsetting the flag.
2163 +static unsigned int test_ret_isorefractory(struct rq *rq)
2165 + if (likely(!grq.iso_refractory)) {
2166 + if (grq.iso_ticks / ISO_PERIOD > sched_iso_cpu)
2167 + set_iso_refractory();
2169 + if (grq.iso_ticks / ISO_PERIOD < (sched_iso_cpu * 90 / 100))
2170 + clear_iso_refractory();
2172 + return grq.iso_refractory;
2175 +static void iso_tick(void)
2178 + grq.iso_ticks += 100;
2182 +/* No SCHED_ISO task was running so decrease rq->iso_ticks */
2183 +static inline void no_iso_tick(void)
2185 + if (grq.iso_ticks) {
2187 + grq.iso_ticks = grq.iso_ticks * (ISO_PERIOD - 1) / ISO_PERIOD;
2192 +static int rq_running_iso(struct rq *rq)
2194 + return rq->rq_prio == ISO_PRIO;
2197 +/* This manages tasks that have run out of timeslice during a scheduler_tick */
2198 +static void task_running_tick(struct rq *rq)
2200 + struct task_struct *p;
2203 + * If a SCHED_ISO task is running we increment the iso_ticks. In
2204 + * order to prevent SCHED_ISO tasks from causing starvation in the
2205 + * presence of true RT tasks we account those as iso_ticks as well.
2207 + if ((rt_queue(rq) || (iso_queue(rq) && !grq.iso_refractory))) {
2208 + if (grq.iso_ticks <= (ISO_PERIOD * 100) - 100)
2213 + if (iso_queue(rq)) {
2214 + if (unlikely(test_ret_isorefractory(rq))) {
2215 + if (rq_running_iso(rq)) {
2217 + * SCHED_ISO task is running as RT and limit
2218 + * has been hit. Force it to reschedule as
2219 + * SCHED_NORMAL by zeroing its time_slice
2221 + rq->rq_time_slice = 0;
2226 + /* SCHED_FIFO tasks never run out of timeslice. */
2227 + if (rq_idle(rq) || rq->rq_time_slice > 0 || rq->rq_policy == SCHED_FIFO)
2230 + /* p->rt.time_slice <= 0. We only modify task_struct under grq lock */
2233 + if (likely(task_running(p))) {
2235 + set_tsk_need_resched(p);
2240 +void wake_up_idle_cpu(int cpu);
2243 + * This function gets called by the timer code, with HZ frequency.
2244 + * We call it with interrupts disabled. The data modified is all
2245 + * local to struct rq so we don't need to grab grq lock.
2247 +void scheduler_tick(void)
2249 + int cpu = smp_processor_id();
2250 + struct rq *rq = cpu_rq(cpu);
2252 + sched_clock_tick();
2253 + update_rq_clock(rq);
2254 + update_cpu_clock(rq, rq->curr, 1);
2256 + task_running_tick(rq);
2259 + if (unlikely(queued_notrunning()))
2260 + set_tsk_need_resched(rq->idle);
2264 +notrace unsigned long get_parent_ip(unsigned long addr)
2266 + if (in_lock_functions(addr)) {
2267 + addr = CALLER_ADDR2;
2268 + if (in_lock_functions(addr))
2269 + addr = CALLER_ADDR3;
2274 +#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2275 + defined(CONFIG_PREEMPT_TRACER))
2276 +void __kprobes add_preempt_count(int val)
2278 +#ifdef CONFIG_DEBUG_PREEMPT
2282 + if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2285 + preempt_count() += val;
2286 +#ifdef CONFIG_DEBUG_PREEMPT
2288 + * Spinlock count overflowing soon?
2290 + DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2291 + PREEMPT_MASK - 10);
2293 + if (preempt_count() == val)
2294 + trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
2296 +EXPORT_SYMBOL(add_preempt_count);
2298 +void __kprobes sub_preempt_count(int val)
2300 +#ifdef CONFIG_DEBUG_PREEMPT
2304 + if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
2307 + * Is the spinlock portion underflowing?
2309 + if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2310 + !(preempt_count() & PREEMPT_MASK)))
2314 + if (preempt_count() == val)
2315 + trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
2316 + preempt_count() -= val;
2318 +EXPORT_SYMBOL(sub_preempt_count);
2322 + * Deadline is "now" in jiffies + (offset by priority). Setting the deadline
2323 + * is the key to everything. It distributes cpu fairly amongst tasks of the
2324 + * same nice value, it proportions cpu according to nice level, it means the
2325 + * task that last woke up the longest ago has the earliest deadline, thus
2326 + * ensuring that interactive tasks get low latency on wake up.
2328 +static inline int prio_deadline_diff(struct task_struct *p)
2330 + return (pratio(p) * rr_interval * HZ / 1000 / 100) ? : 1;
2333 +static inline int longest_deadline(void)
2335 + return (prio_ratios[39] * rr_interval * HZ / 1000 / 100);
2339 + * SCHED_IDLE tasks still have a deadline set, but offset by to nice +19.
2340 + * This allows nice levels to work between IDLEPRIO tasks and gives a
2341 + * deadline longer than nice +19 for when they're scheduled as SCHED_NORMAL
2344 +static inline void time_slice_expired(struct task_struct *p)
2346 + reset_first_time_slice(p);
2347 + p->rt.time_slice = timeslice();
2348 + p->deadline = jiffies + prio_deadline_diff(p);
2349 + if (idleprio_task(p))
2350 + p->deadline += longest_deadline();
2353 +static inline void check_deadline(struct task_struct *p)
2355 + if (p->rt.time_slice <= 0)
2356 + time_slice_expired(p);
2360 + * O(n) lookup of all tasks in the global runqueue. The real brainfuck
2361 + * of lock contention and O(n). It's not really O(n) as only the queued,
2362 + * but not running tasks are scanned, and is O(n) queued in the worst case
2363 + * scenario only because the right task can be found before scanning all of
2365 + * Tasks are selected in this order:
2366 + * Real time tasks are selected purely by their static priority and in the
2367 + * order they were queued, so the lowest value idx, and the first queued task
2368 + * of that priority value is chosen.
2369 + * If no real time tasks are found, the SCHED_ISO priority is checked, and
2370 + * all SCHED_ISO tasks have the same priority value, so they're selected by
2371 + * the earliest deadline value.
2372 + * If no SCHED_ISO tasks are found, SCHED_NORMAL tasks are selected by the
2373 + * earliest deadline.
2374 + * Finally if no SCHED_NORMAL tasks are found, SCHED_IDLEPRIO tasks are
2375 + * selected by the earliest deadline.
2377 +static inline struct
2378 +task_struct *earliest_deadline_task(struct rq *rq, struct task_struct *idle)
2380 + unsigned long dl, earliest_deadline = 0; /* Initialise to silence compiler */
2381 + struct task_struct *p, *edt;
2382 + unsigned int cpu = rq->cpu;
2383 + struct list_head *queue;
2388 + idx = find_next_bit(grq.prio_bitmap, PRIO_LIMIT, idx);
2389 + if (idx >= PRIO_LIMIT)
2391 + queue = &grq.queue[idx];
2392 + list_for_each_entry(p, queue, rt.run_list) {
2393 + /* Make sure cpu affinity is ok */
2394 + if (!cpu_isset(cpu, p->cpus_allowed))
2396 + if (idx < MAX_RT_PRIO) {
2397 + /* We found an rt task */
2403 + * No rt task, select the earliest deadline task now.
2404 + * On the 1st run the 2nd condition is never used, so
2405 + * there is no need to initialise earliest_deadline
2406 + * before. Normalise all old deadlines to now.
2408 + if (time_before(p->deadline, jiffies))
2413 + if (edt == idle ||
2414 + time_before(dl, earliest_deadline)) {
2415 + earliest_deadline = dl;
2419 + if (edt == idle) {
2420 + if (++idx < PRIO_LIMIT)
2425 + take_task(rq, edt);
2431 +static inline void set_cpuidle_map(unsigned long cpu)
2433 + cpu_set(cpu, grq.cpu_idle_map);
2436 +static inline void clear_cpuidle_map(unsigned long cpu)
2438 + cpu_clear(cpu, grq.cpu_idle_map);
2441 +#else /* CONFIG_SMP */
2442 +static inline void set_cpuidle_map(unsigned long cpu)
2446 +static inline void clear_cpuidle_map(unsigned long cpu)
2449 +#endif /* !CONFIG_SMP */
2452 + * Print scheduling while atomic bug:
2454 +static noinline void __schedule_bug(struct task_struct *prev)
2456 + struct pt_regs *regs = get_irq_regs();
2458 + printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
2459 + prev->comm, prev->pid, preempt_count());
2461 + debug_show_held_locks(prev);
2463 + if (irqs_disabled())
2464 + print_irqtrace_events(prev);
2473 + * Various schedule()-time debugging checks and statistics:
2475 +static inline void schedule_debug(struct task_struct *prev)
2478 + * Test if we are atomic. Since do_exit() needs to call into
2479 + * schedule() atomically, we ignore that path for now.
2480 + * Otherwise, whine if we are scheduling when we should not be.
2482 + if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
2483 + __schedule_bug(prev);
2485 + profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2487 + schedstat_inc(this_rq(), sched_count);
2488 +#ifdef CONFIG_SCHEDSTATS
2489 + if (unlikely(prev->lock_depth >= 0)) {
2490 + schedstat_inc(this_rq(), bkl_count);
2491 + schedstat_inc(prev, sched_info.bkl_count);
2497 + * schedule() is the main scheduler function.
2499 +asmlinkage void __sched __schedule(void)
2501 + struct task_struct *prev, *next, *idle;
2502 + int deactivate = 0, cpu;
2503 + long *switch_count;
2507 + cpu = smp_processor_id();
2509 + rcu_qsctr_inc(cpu);
2511 + switch_count = &prev->nivcsw;
2513 + release_kernel_lock(prev);
2514 +need_resched_nonpreemptible:
2516 + schedule_debug(prev);
2519 + * The idle thread is not allowed to schedule!
2520 + * Remove this check after it has been exercised a bit.
2522 + if (unlikely(prev == idle) && prev->state != TASK_RUNNING) {
2523 + printk(KERN_ERR "bad: scheduling from the idle thread!\n");
2528 + update_rq_clock(rq);
2530 + update_cpu_clock(rq, prev, 0);
2532 + clear_tsk_need_resched(prev);
2534 + if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
2535 + if (unlikely(signal_pending_state(prev->state, prev)))
2536 + prev->state = TASK_RUNNING;
2539 + switch_count = &prev->nvcsw;
2542 + if (prev != idle) {
2543 + /* Update all the information stored on struct rq */
2544 + prev->rt.time_slice = rq->rq_time_slice;
2545 + prev->deadline = rq->rq_deadline;
2546 + check_deadline(prev);
2547 + return_task(prev, deactivate);
2550 + if (likely(queued_notrunning())) {
2551 + next = earliest_deadline_task(rq, idle);
2554 + schedstat_inc(rq, sched_goidle);
2557 + if (next == rq->idle)
2558 + set_cpuidle_map(cpu);
2560 + clear_cpuidle_map(cpu);
2563 + prefetch_stack(next);
2565 + prev->timestamp = prev->last_ran = now;
2567 + if (likely(prev != next)) {
2568 + rq->rq_time_slice = next->rt.time_slice;
2569 + rq->rq_deadline = next->deadline;
2570 + rq->rq_prio = next->prio;
2572 + sched_info_switch(prev, next);
2573 + grq.nr_switches++;
2579 + context_switch(rq, prev, next); /* unlocks the rq */
2581 + * the context switch might have flipped the stack from under
2582 + * us, hence refresh the local variables.
2584 + cpu = smp_processor_id();
2589 + if (unlikely(reacquire_kernel_lock(current) < 0))
2590 + goto need_resched_nonpreemptible;
2593 +asmlinkage void __sched schedule(void)
2596 + preempt_disable();
2598 + preempt_enable_no_resched();
2599 + if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
2600 + goto need_resched;
2602 +EXPORT_SYMBOL(schedule);
2605 +int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
2611 +#ifdef CONFIG_PREEMPT
2613 + * this is the entry point to schedule() from in-kernel preemption
2614 + * off of preempt_enable. Kernel preemptions off return from interrupt
2615 + * occur there and call schedule directly.
2617 +asmlinkage void __sched preempt_schedule(void)
2619 + struct thread_info *ti = current_thread_info();
2622 + * If there is a non-zero preempt_count or interrupts are disabled,
2623 + * we do not want to preempt the current task. Just return..
2625 + if (likely(ti->preempt_count || irqs_disabled()))
2629 + add_preempt_count(PREEMPT_ACTIVE);
2631 + sub_preempt_count(PREEMPT_ACTIVE);
2634 + * Check again in case we missed a preemption opportunity
2635 + * between schedule and now.
2638 + } while (need_resched());
2640 +EXPORT_SYMBOL(preempt_schedule);
2643 + * this is the entry point to schedule() from kernel preemption
2644 + * off of irq context.
2645 + * Note, that this is called and return with irqs disabled. This will
2646 + * protect us against recursive calling from irq.
2648 +asmlinkage void __sched preempt_schedule_irq(void)
2650 + struct thread_info *ti = current_thread_info();
2652 + /* Catch callers which need to be fixed */
2653 + BUG_ON(ti->preempt_count || !irqs_disabled());
2656 + add_preempt_count(PREEMPT_ACTIVE);
2657 + local_irq_enable();
2659 + local_irq_disable();
2660 + sub_preempt_count(PREEMPT_ACTIVE);
2663 + * Check again in case we missed a preemption opportunity
2664 + * between schedule and now.
2667 + } while (need_resched());
2670 +#endif /* CONFIG_PREEMPT */
2672 +int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
2675 + return try_to_wake_up(curr->private, mode, sync);
2677 +EXPORT_SYMBOL(default_wake_function);
2680 + * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
2681 + * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
2682 + * number) then we wake all the non-exclusive tasks and one exclusive task.
2684 + * There are circumstances in which we can try to wake a task which has already
2685 + * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
2686 + * zero in this (rare) case, and we handle it by continuing to scan the queue.
2688 +void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
2689 + int nr_exclusive, int sync, void *key)
2691 + struct list_head *tmp, *next;
2693 + list_for_each_safe(tmp, next, &q->task_list) {
2694 + wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
2695 + unsigned flags = curr->flags;
2697 + if (curr->func(curr, mode, sync, key) &&
2698 + (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
2704 + * __wake_up - wake up threads blocked on a waitqueue.
2705 + * @q: the waitqueue
2706 + * @mode: which threads
2707 + * @nr_exclusive: how many wake-one or wake-many threads to wake up
2708 + * @key: is directly passed to the wakeup function
2710 + * It may be assumed that this function implies a write memory barrier before
2711 + * changing the task state if and only if any tasks are woken up.
2713 +void __wake_up(wait_queue_head_t *q, unsigned int mode,
2714 + int nr_exclusive, void *key)
2716 + unsigned long flags;
2718 + spin_lock_irqsave(&q->lock, flags);
2719 + __wake_up_common(q, mode, nr_exclusive, 0, key);
2720 + spin_unlock_irqrestore(&q->lock, flags);
2722 +EXPORT_SYMBOL(__wake_up);
2725 + * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
2727 +void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
2729 + __wake_up_common(q, mode, 1, 0, NULL);
2732 +void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
2734 + __wake_up_common(q, mode, 1, 0, key);
2738 + * __wake_up_sync_key - wake up threads blocked on a waitqueue.
2739 + * @q: the waitqueue
2740 + * @mode: which threads
2741 + * @nr_exclusive: how many wake-one or wake-many threads to wake up
2742 + * @key: opaque value to be passed to wakeup targets
2744 + * The sync wakeup differs that the waker knows that it will schedule
2745 + * away soon, so while the target thread will be woken up, it will not
2746 + * be migrated to another CPU - ie. the two threads are 'synchronized'
2747 + * with each other. This can prevent needless bouncing between CPUs.
2749 + * On UP it can prevent extra preemption.
2751 + * It may be assumed that this function implies a write memory barrier before
2752 + * changing the task state if and only if any tasks are woken up.
2754 +void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
2755 + int nr_exclusive, void *key)
2757 + unsigned long flags;
2763 + if (unlikely(!nr_exclusive))
2766 + spin_lock_irqsave(&q->lock, flags);
2767 + __wake_up_common(q, mode, nr_exclusive, sync, key);
2768 + spin_unlock_irqrestore(&q->lock, flags);
2770 +EXPORT_SYMBOL_GPL(__wake_up_sync_key);
2773 + * __wake_up_sync - wake up threads blocked on a waitqueue.
2774 + * @q: the waitqueue
2775 + * @mode: which threads
2776 + * @nr_exclusive: how many wake-one or wake-many threads to wake up
2778 + * The sync wakeup differs that the waker knows that it will schedule
2779 + * away soon, so while the target thread will be woken up, it will not
2780 + * be migrated to another CPU - ie. the two threads are 'synchronized'
2781 + * with each other. This can prevent needless bouncing between CPUs.
2783 + * On UP it can prevent extra preemption.
2785 +void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
2787 + unsigned long flags;
2793 + if (unlikely(!nr_exclusive))
2796 + spin_lock_irqsave(&q->lock, flags);
2797 + __wake_up_common(q, mode, nr_exclusive, sync, NULL);
2798 + spin_unlock_irqrestore(&q->lock, flags);
2800 +EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
2803 + * complete: - signals a single thread waiting on this completion
2804 + * @x: holds the state of this particular completion
2806 + * This will wake up a single thread waiting on this completion. Threads will be
2807 + * awakened in the same order in which they were queued.
2809 + * See also complete_all(), wait_for_completion() and related routines.
2811 + * It may be assumed that this function implies a write memory barrier before
2812 + * changing the task state if and only if any tasks are woken up.
2814 +void complete(struct completion *x)
2816 + unsigned long flags;
2818 + spin_lock_irqsave(&x->wait.lock, flags);
2820 + __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
2821 + spin_unlock_irqrestore(&x->wait.lock, flags);
2823 +EXPORT_SYMBOL(complete);
2826 + * complete_all: - signals all threads waiting on this completion
2827 + * @x: holds the state of this particular completion
2829 + * This will wake up all threads waiting on this particular completion event.
2831 + * It may be assumed that this function implies a write memory barrier before
2832 + * changing the task state if and only if any tasks are woken up.
2834 +void complete_all(struct completion *x)
2836 + unsigned long flags;
2838 + spin_lock_irqsave(&x->wait.lock, flags);
2839 + x->done += UINT_MAX/2;
2840 + __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
2841 + spin_unlock_irqrestore(&x->wait.lock, flags);
2843 +EXPORT_SYMBOL(complete_all);
2845 +static inline long __sched
2846 +do_wait_for_common(struct completion *x, long timeout, int state)
2849 + DECLARE_WAITQUEUE(wait, current);
2851 + wait.flags |= WQ_FLAG_EXCLUSIVE;
2852 + __add_wait_queue_tail(&x->wait, &wait);
2854 + if (signal_pending_state(state, current)) {
2855 + timeout = -ERESTARTSYS;
2858 + __set_current_state(state);
2859 + spin_unlock_irq(&x->wait.lock);
2860 + timeout = schedule_timeout(timeout);
2861 + spin_lock_irq(&x->wait.lock);
2862 + } while (!x->done && timeout);
2863 + __remove_wait_queue(&x->wait, &wait);
2868 + return timeout ?: 1;
2871 +static long __sched
2872 +wait_for_common(struct completion *x, long timeout, int state)
2876 + spin_lock_irq(&x->wait.lock);
2877 + timeout = do_wait_for_common(x, timeout, state);
2878 + spin_unlock_irq(&x->wait.lock);
2883 + * wait_for_completion: - waits for completion of a task
2884 + * @x: holds the state of this particular completion
2886 + * This waits to be signaled for completion of a specific task. It is NOT
2887 + * interruptible and there is no timeout.
2889 + * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
2890 + * and interrupt capability. Also see complete().
2892 +void __sched wait_for_completion(struct completion *x)
2894 + wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
2896 +EXPORT_SYMBOL(wait_for_completion);
2899 + * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
2900 + * @x: holds the state of this particular completion
2901 + * @timeout: timeout value in jiffies
2903 + * This waits for either a completion of a specific task to be signaled or for a
2904 + * specified timeout to expire. The timeout is in jiffies. It is not
2907 +unsigned long __sched
2908 +wait_for_completion_timeout(struct completion *x, unsigned long timeout)
2910 + return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
2912 +EXPORT_SYMBOL(wait_for_completion_timeout);
2915 + * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
2916 + * @x: holds the state of this particular completion
2918 + * This waits for completion of a specific task to be signaled. It is
2921 +int __sched wait_for_completion_interruptible(struct completion *x)
2923 + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
2924 + if (t == -ERESTARTSYS)
2928 +EXPORT_SYMBOL(wait_for_completion_interruptible);
2931 + * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
2932 + * @x: holds the state of this particular completion
2933 + * @timeout: timeout value in jiffies
2935 + * This waits for either a completion of a specific task to be signaled or for a
2936 + * specified timeout to expire. It is interruptible. The timeout is in jiffies.
2938 +unsigned long __sched
2939 +wait_for_completion_interruptible_timeout(struct completion *x,
2940 + unsigned long timeout)
2942 + return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
2944 +EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
2947 + * wait_for_completion_killable: - waits for completion of a task (killable)
2948 + * @x: holds the state of this particular completion
2950 + * This waits to be signaled for completion of a specific task. It can be
2951 + * interrupted by a kill signal.
2953 +int __sched wait_for_completion_killable(struct completion *x)
2955 + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
2956 + if (t == -ERESTARTSYS)
2960 +EXPORT_SYMBOL(wait_for_completion_killable);
2963 + * try_wait_for_completion - try to decrement a completion without blocking
2964 + * @x: completion structure
2966 + * Returns: 0 if a decrement cannot be done without blocking
2967 + * 1 if a decrement succeeded.
2969 + * If a completion is being used as a counting completion,
2970 + * attempt to decrement the counter without blocking. This
2971 + * enables us to avoid waiting if the resource the completion
2972 + * is protecting is not available.
2974 +bool try_wait_for_completion(struct completion *x)
2978 + spin_lock_irq(&x->wait.lock);
2983 + spin_unlock_irq(&x->wait.lock);
2986 +EXPORT_SYMBOL(try_wait_for_completion);
2989 + * completion_done - Test to see if a completion has any waiters
2990 + * @x: completion structure
2992 + * Returns: 0 if there are waiters (wait_for_completion() in progress)
2993 + * 1 if there are no waiters.
2996 +bool completion_done(struct completion *x)
3000 + spin_lock_irq(&x->wait.lock);
3003 + spin_unlock_irq(&x->wait.lock);
3006 +EXPORT_SYMBOL(completion_done);
3008 +static long __sched
3009 +sleep_on_common(wait_queue_head_t *q, int state, long timeout)
3011 + unsigned long flags;
3012 + wait_queue_t wait;
3014 + init_waitqueue_entry(&wait, current);
3016 + __set_current_state(state);
3018 + spin_lock_irqsave(&q->lock, flags);
3019 + __add_wait_queue(q, &wait);
3020 + spin_unlock(&q->lock);
3021 + timeout = schedule_timeout(timeout);
3022 + spin_lock_irq(&q->lock);
3023 + __remove_wait_queue(q, &wait);
3024 + spin_unlock_irqrestore(&q->lock, flags);
3029 +void __sched interruptible_sleep_on(wait_queue_head_t *q)
3031 + sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
3033 +EXPORT_SYMBOL(interruptible_sleep_on);
3036 +interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
3038 + return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
3040 +EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3042 +void __sched sleep_on(wait_queue_head_t *q)
3044 + sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
3046 +EXPORT_SYMBOL(sleep_on);
3048 +long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
3050 + return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
3052 +EXPORT_SYMBOL(sleep_on_timeout);
3054 +#ifdef CONFIG_RT_MUTEXES
3057 + * rt_mutex_setprio - set the current priority of a task
3059 + * @prio: prio value (kernel-internal form)
3061 + * This function changes the 'effective' priority of a task. It does
3062 + * not touch ->normal_prio like __setscheduler().
3064 + * Used by the rt_mutex code to implement priority inheritance logic.
3066 +void rt_mutex_setprio(struct task_struct *p, int prio)
3068 + unsigned long flags;
3069 + int queued, oldprio;
3072 + BUG_ON(prio < 0 || prio > MAX_PRIO);
3074 + rq = time_task_grq_lock(p, &flags);
3076 + oldprio = p->prio;
3077 + queued = task_queued_only(p);
3081 + if (task_running(p) && prio > oldprio)
3088 + task_grq_unlock(&flags);
3094 + * Adjust the deadline for when the priority is to change, before it's
3097 +static void adjust_deadline(struct task_struct *p, int new_prio)
3099 + p->deadline += (prio_ratios[USER_PRIO(new_prio)] - pratio(p)) *
3100 + rr_interval * HZ / 1000 / 100;
3103 +void set_user_nice(struct task_struct *p, long nice)
3105 + int queued, new_static;
3106 + unsigned long flags;
3109 + if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3111 + new_static = NICE_TO_PRIO(nice);
3113 + * We have to be careful, if called from sys_setpriority(),
3114 + * the task might be in the middle of scheduling on another CPU.
3116 + rq = time_task_grq_lock(p, &flags);
3118 + * The RT priorities are set via sched_setscheduler(), but we still
3119 + * allow the 'normal' nice value to be set - but as expected
3120 + * it wont have any effect on scheduling until the task is
3121 + * not SCHED_NORMAL/SCHED_BATCH:
3123 + if (has_rt_policy(p)) {
3124 + p->static_prio = new_static;
3127 + queued = task_queued_only(p);
3129 + * If p is actually running, we don't need to do anything when
3130 + * changing the priority because the grq is unaffected.
3135 + adjust_deadline(p, new_static);
3136 + p->static_prio = new_static;
3137 + p->prio = effective_prio(p);
3144 + /* Just resched the task, schedule() will know what to do. */
3145 + if (task_running(p))
3148 + task_grq_unlock(&flags);
3150 +EXPORT_SYMBOL(set_user_nice);
3153 + * can_nice - check if a task can reduce its nice value
3155 + * @nice: nice value
3157 +int can_nice(const struct task_struct *p, const int nice)
3159 + /* convert nice value [19,-20] to rlimit style value [1,40] */
3160 + int nice_rlim = 20 - nice;
3162 + return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
3163 + capable(CAP_SYS_NICE));
3166 +#ifdef __ARCH_WANT_SYS_NICE
3169 + * sys_nice - change the priority of the current process.
3170 + * @increment: priority increment
3172 + * sys_setpriority is a more generic, but much slower function that
3173 + * does similar things.
3175 +SYSCALL_DEFINE1(nice, int, increment)
3177 + long nice, retval;
3180 + * Setpriority might change our priority at the same moment.
3181 + * We don't have to worry. Conceptually one call occurs first
3182 + * and we have a single winner.
3184 + if (increment < -40)
3186 + if (increment > 40)
3189 + nice = TASK_NICE(current) + increment;
3195 + if (increment < 0 && !can_nice(current, nice))
3198 + retval = security_task_setnice(current, nice);
3202 + set_user_nice(current, nice);
3209 + * task_prio - return the priority value of a given task.
3210 + * @p: the task in question.
3212 + * This is the priority value as seen by users in /proc.
3213 + * RT tasks are offset by -100. Normal tasks are centered
3214 + * around 1, value goes from 0 (SCHED_ISO) up to 82 (nice +19
3217 +int task_prio(const struct task_struct *p)
3219 + int delta, prio = p->prio - MAX_RT_PRIO;
3221 + /* rt tasks and iso tasks */
3225 + delta = (p->deadline - jiffies) * 40 / longest_deadline();
3226 + if (delta > 0 && delta <= 80)
3233 + * task_nice - return the nice value of a given task.
3234 + * @p: the task in question.
3236 +int task_nice(const struct task_struct *p)
3238 + return TASK_NICE(p);
3240 +EXPORT_SYMBOL_GPL(task_nice);
3243 + * idle_cpu - is a given cpu idle currently?
3244 + * @cpu: the processor in question.
3246 +int idle_cpu(int cpu)
3248 + return cpu_curr(cpu) == cpu_rq(cpu)->idle;
3252 + * idle_task - return the idle task for a given cpu.
3253 + * @cpu: the processor in question.
3255 +struct task_struct *idle_task(int cpu)
3257 + return cpu_rq(cpu)->idle;
3261 + * find_process_by_pid - find a process with a matching PID value.
3262 + * @pid: the pid in question.
3264 +static inline struct task_struct *find_process_by_pid(pid_t pid)
3266 + return pid ? find_task_by_vpid(pid) : current;
3269 +/* Actually do priority change: must hold grq lock. */
3270 +static void __setscheduler(struct task_struct *p, int policy, int prio)
3272 + BUG_ON(task_queued_only(p));
3274 + p->policy = policy;
3275 + p->rt_priority = prio;
3276 + p->normal_prio = normal_prio(p);
3277 + /* we are holding p->pi_lock already */
3278 + p->prio = rt_mutex_getprio(p);
3280 + * Reschedule if running. schedule() will know if it can continue
3283 + if (task_running(p))
3288 + * check the target process has a UID that matches the current process's
3290 +static bool check_same_owner(struct task_struct *p)
3292 + const struct cred *cred = current_cred(), *pcred;
3296 + pcred = __task_cred(p);
3297 + match = (cred->euid == pcred->euid ||
3298 + cred->euid == pcred->uid);
3299 + rcu_read_unlock();
3303 +static int __sched_setscheduler(struct task_struct *p, int policy,
3304 + struct sched_param *param, bool user)
3306 + struct sched_param zero_param = { .sched_priority = 0 };
3307 + int queued, retval, oldprio, oldpolicy = -1;
3308 + unsigned long flags, rlim_rtprio = 0;
3311 + /* may grab non-irq protected spin_locks */
3312 + BUG_ON(in_interrupt());
3314 + if (is_rt_policy(policy) && !capable(CAP_SYS_NICE)) {
3315 + unsigned long lflags;
3317 + if (!lock_task_sighand(p, &lflags))
3319 + rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
3320 + unlock_task_sighand(p, &lflags);
3324 + * If the caller requested an RT policy without having the
3325 + * necessary rights, we downgrade the policy to SCHED_ISO.
3326 + * We also set the parameter to zero to pass the checks.
3328 + policy = SCHED_ISO;
3329 + param = &zero_param;
3332 + /* double check policy once rq lock held */
3334 + policy = oldpolicy = p->policy;
3335 + else if (!SCHED_RANGE(policy))
3338 + * Valid priorities for SCHED_FIFO and SCHED_RR are
3339 + * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL and
3340 + * SCHED_BATCH is 0.
3342 + if (param->sched_priority < 0 ||
3343 + (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
3344 + (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
3346 + if (is_rt_policy(policy) != (param->sched_priority != 0))
3350 + * Allow unprivileged RT tasks to decrease priority:
3352 + if (user && !capable(CAP_SYS_NICE)) {
3353 + if (is_rt_policy(policy)) {
3354 + /* can't set/change the rt policy */
3355 + if (policy != p->policy && !rlim_rtprio)
3358 + /* can't increase priority */
3359 + if (param->sched_priority > p->rt_priority &&
3360 + param->sched_priority > rlim_rtprio)
3363 + switch (p->policy) {
3365 + * Can only downgrade policies but not back to
3369 + if (policy == SCHED_ISO)
3371 + if (policy == SCHED_NORMAL)
3375 + if (policy == SCHED_BATCH)
3377 + if (policy != SCHED_IDLE)
3381 + if (policy == SCHED_IDLE)
3389 + /* can't change other user's priorities */
3390 + if (!check_same_owner(p))
3394 + retval = security_task_setscheduler(p, policy, param);
3398 + * make sure no PI-waiters arrive (or leave) while we are
3399 + * changing the priority of the task:
3401 + spin_lock_irqsave(&p->pi_lock, flags);
3403 + * To be able to change p->policy safely, the apropriate
3404 + * runqueue lock must be held.
3406 + rq = __task_grq_lock(p);
3407 + /* recheck policy now with rq lock held */
3408 + if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3409 + __task_grq_unlock();
3410 + spin_unlock_irqrestore(&p->pi_lock, flags);
3411 + policy = oldpolicy = -1;
3414 + update_rq_clock(rq);
3415 + queued = task_queued_only(p);
3418 + oldprio = p->prio;
3419 + __setscheduler(p, policy, param->sched_priority);
3424 + __task_grq_unlock();
3425 + spin_unlock_irqrestore(&p->pi_lock, flags);
3427 + rt_mutex_adjust_pi(p);
3433 + * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
3434 + * @p: the task in question.
3435 + * @policy: new policy.
3436 + * @param: structure containing the new RT priority.
3438 + * NOTE that the task may be already dead.
3440 +int sched_setscheduler(struct task_struct *p, int policy,
3441 + struct sched_param *param)
3443 + return __sched_setscheduler(p, policy, param, true);
3446 +EXPORT_SYMBOL_GPL(sched_setscheduler);
3449 + * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
3450 + * @p: the task in question.
3451 + * @policy: new policy.
3452 + * @param: structure containing the new RT priority.
3454 + * Just like sched_setscheduler, only don't bother checking if the
3455 + * current context has permission. For example, this is needed in
3456 + * stop_machine(): we create temporary high priority worker threads,
3457 + * but our caller might not have that capability.
3459 +int sched_setscheduler_nocheck(struct task_struct *p, int policy,
3460 + struct sched_param *param)
3462 + return __sched_setscheduler(p, policy, param, false);
3466 +do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
3468 + struct sched_param lparam;
3469 + struct task_struct *p;
3472 + if (!param || pid < 0)
3474 + if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
3479 + p = find_process_by_pid(pid);
3481 + retval = sched_setscheduler(p, policy, &lparam);
3482 + rcu_read_unlock();
3488 + * sys_sched_setscheduler - set/change the scheduler policy and RT priority
3489 + * @pid: the pid in question.
3490 + * @policy: new policy.
3491 + * @param: structure containing the new RT priority.
3493 +asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
3494 + struct sched_param __user *param)
3496 + /* negative values for policy are not valid */
3500 + return do_sched_setscheduler(pid, policy, param);
3504 + * sys_sched_setparam - set/change the RT priority of a thread
3505 + * @pid: the pid in question.
3506 + * @param: structure containing the new RT priority.
3508 +SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
3510 + return do_sched_setscheduler(pid, -1, param);
3514 + * sys_sched_getscheduler - get the policy (scheduling class) of a thread
3515 + * @pid: the pid in question.
3517 +SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
3519 + struct task_struct *p;
3520 + int retval = -EINVAL;
3523 + goto out_nounlock;
3526 + read_lock(&tasklist_lock);
3527 + p = find_process_by_pid(pid);
3529 + retval = security_task_getscheduler(p);
3531 + retval = p->policy;
3533 + read_unlock(&tasklist_lock);
3540 + * sys_sched_getscheduler - get the RT priority of a thread
3541 + * @pid: the pid in question.
3542 + * @param: structure containing the RT priority.
3544 +SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
3546 + struct sched_param lp;
3547 + struct task_struct *p;
3548 + int retval = -EINVAL;
3550 + if (!param || pid < 0)
3551 + goto out_nounlock;
3553 + read_lock(&tasklist_lock);
3554 + p = find_process_by_pid(pid);
3559 + retval = security_task_getscheduler(p);
3563 + lp.sched_priority = p->rt_priority;
3564 + read_unlock(&tasklist_lock);
3567 + * This one might sleep, we cannot do it with a spinlock held ...
3569 + retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
3575 + read_unlock(&tasklist_lock);
3579 +long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
3581 + cpumask_var_t cpus_allowed, new_mask;
3582 + struct task_struct *p;
3585 + get_online_cpus();
3586 + read_lock(&tasklist_lock);
3588 + p = find_process_by_pid(pid);
3590 + read_unlock(&tasklist_lock);
3591 + put_online_cpus();
3596 + * It is not safe to call set_cpus_allowed with the
3597 + * tasklist_lock held. We will bump the task_struct's
3598 + * usage count and then drop tasklist_lock.
3600 + get_task_struct(p);
3601 + read_unlock(&tasklist_lock);
3603 + if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
3605 + goto out_put_task;
3607 + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
3609 + goto out_free_cpus_allowed;
3612 + if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
3615 + retval = security_task_setscheduler(p, 0, NULL);
3619 + cpuset_cpus_allowed(p, cpus_allowed);
3620 + cpumask_and(new_mask, in_mask, cpus_allowed);
3622 + retval = set_cpus_allowed_ptr(p, new_mask);
3625 + cpuset_cpus_allowed(p, cpus_allowed);
3626 + if (!cpumask_subset(new_mask, cpus_allowed)) {
3628 + * We must have raced with a concurrent cpuset
3629 + * update. Just reset the cpus_allowed to the
3630 + * cpuset's cpus_allowed
3632 + cpumask_copy(new_mask, cpus_allowed);
3637 + free_cpumask_var(new_mask);
3638 +out_free_cpus_allowed:
3639 + free_cpumask_var(cpus_allowed);
3641 + put_task_struct(p);
3642 + put_online_cpus();
3646 +static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
3647 + cpumask_t *new_mask)
3649 + if (len < sizeof(cpumask_t)) {
3650 + memset(new_mask, 0, sizeof(cpumask_t));
3651 + } else if (len > sizeof(cpumask_t)) {
3652 + len = sizeof(cpumask_t);
3654 + return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
3659 + * sys_sched_setaffinity - set the cpu affinity of a process
3660 + * @pid: pid of the process
3661 + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
3662 + * @user_mask_ptr: user-space pointer to the new cpu mask
3664 +SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
3665 + unsigned long __user *, user_mask_ptr)
3667 + cpumask_var_t new_mask;
3670 + if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
3673 + retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
3675 + retval = sched_setaffinity(pid, new_mask);
3676 + free_cpumask_var(new_mask);
3680 +long sched_getaffinity(pid_t pid, cpumask_t *mask)
3682 + struct task_struct *p;
3685 + mutex_lock(&sched_hotcpu_mutex);
3686 + read_lock(&tasklist_lock);
3689 + p = find_process_by_pid(pid);
3693 + retval = security_task_getscheduler(p);
3697 + cpus_and(*mask, p->cpus_allowed, cpu_online_map);
3700 + read_unlock(&tasklist_lock);
3701 + mutex_unlock(&sched_hotcpu_mutex);
3709 + * sys_sched_getaffinity - get the cpu affinity of a process
3710 + * @pid: pid of the process
3711 + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
3712 + * @user_mask_ptr: user-space pointer to hold the current cpu mask
3714 +SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
3715 + unsigned long __user *, user_mask_ptr)
3718 + cpumask_var_t mask;
3720 + if (len < cpumask_size())
3723 + if (!alloc_cpumask_var(&mask, GFP_KERNEL))
3726 + ret = sched_getaffinity(pid, mask);
3728 + if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
3731 + ret = cpumask_size();
3733 + free_cpumask_var(mask);
3739 + * sys_sched_yield - yield the current processor to other threads.
3741 + * This function yields the current CPU to other tasks. It does this by
3742 + * refilling the timeslice, resetting the deadline and scheduling away.
3744 +SYSCALL_DEFINE0(sched_yield)
3746 + struct task_struct *p;
3750 + schedstat_inc(this_rq(), yld_count);
3751 + update_rq_clock(task_rq(p));
3752 + time_slice_expired(p);
3756 + * Since we are going to call schedule() anyway, there's
3757 + * no need to preempt or enable interrupts:
3759 + __release(grq.lock);
3760 + spin_release(&grq.lock.dep_map, 1, _THIS_IP_);
3761 + _raw_spin_unlock(&grq.lock);
3762 + preempt_enable_no_resched();
3769 +static inline int should_resched(void)
3771 + return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
3774 +static void __cond_resched(void)
3776 +#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
3777 + __might_sleep(__FILE__, __LINE__);
3780 + * The BKS might be reacquired before we have dropped
3781 + * PREEMPT_ACTIVE, which could trigger a second
3782 + * cond_resched() call.
3785 + add_preempt_count(PREEMPT_ACTIVE);
3787 + sub_preempt_count(PREEMPT_ACTIVE);
3788 + } while (need_resched());
3791 +int __sched _cond_resched(void)
3793 + if (should_resched()) {
3799 +EXPORT_SYMBOL(_cond_resched);
3802 + * cond_resched_lock() - if a reschedule is pending, drop the given lock,
3803 + * call schedule, and on return reacquire the lock.
3805 + * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
3806 + * operations here to prevent schedule() from being called twice (once via
3807 + * spin_unlock(), once by hand).
3809 +int cond_resched_lock(spinlock_t *lock)
3811 + int resched = should_resched();
3814 + if (spin_needbreak(lock) || resched) {
3815 + spin_unlock(lock);
3825 +EXPORT_SYMBOL(cond_resched_lock);
3827 +int __sched cond_resched_softirq(void)
3829 + BUG_ON(!in_softirq());
3831 + if (should_resched()) {
3832 + local_bh_enable();
3834 + local_bh_disable();
3839 +EXPORT_SYMBOL(cond_resched_softirq);
3842 + * yield - yield the current processor to other threads.
3844 + * This is a shortcut for kernel-space yielding - it marks the
3845 + * thread runnable and calls sys_sched_yield().
3847 +void __sched yield(void)
3849 + set_current_state(TASK_RUNNING);
3850 + sys_sched_yield();
3852 +EXPORT_SYMBOL(yield);
3855 + * This task is about to go to sleep on IO. Increment rq->nr_iowait so
3856 + * that process accounting knows that this is a task in IO wait state.
3858 + * But don't do that if it is a deliberate, throttling IO wait (this task
3859 + * has set its backing_dev_info: the queue against which it should throttle)
3861 +void __sched io_schedule(void)
3863 + struct rq *rq = &__raw_get_cpu_var(runqueues);
3865 + delayacct_blkio_start();
3866 + atomic_inc(&rq->nr_iowait);
3868 + atomic_dec(&rq->nr_iowait);
3869 + delayacct_blkio_end();
3871 +EXPORT_SYMBOL(io_schedule);
3873 +long __sched io_schedule_timeout(long timeout)
3875 + struct rq *rq = &__raw_get_cpu_var(runqueues);
3878 + delayacct_blkio_start();
3879 + atomic_inc(&rq->nr_iowait);
3880 + ret = schedule_timeout(timeout);
3881 + atomic_dec(&rq->nr_iowait);
3882 + delayacct_blkio_end();
3887 + * sys_sched_get_priority_max - return maximum RT priority.
3888 + * @policy: scheduling class.
3890 + * this syscall returns the maximum rt_priority that can be used
3891 + * by a given scheduling class.
3893 +SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
3895 + int ret = -EINVAL;
3900 + ret = MAX_USER_RT_PRIO-1;
3902 + case SCHED_NORMAL:
3913 + * sys_sched_get_priority_min - return minimum RT priority.
3914 + * @policy: scheduling class.
3916 + * this syscall returns the minimum rt_priority that can be used
3917 + * by a given scheduling class.
3919 +SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
3921 + int ret = -EINVAL;
3928 + case SCHED_NORMAL:
3939 + * sys_sched_rr_get_interval - return the default timeslice of a process.
3940 + * @pid: pid of the process.
3941 + * @interval: userspace pointer to the timeslice value.
3943 + * this syscall writes the default timeslice value of a given process
3944 + * into the user-space timespec buffer. A value of '0' means infinity.
3946 +SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
3947 + struct timespec __user *, interval)
3949 + struct task_struct *p;
3950 + int retval = -EINVAL;
3951 + struct timespec t;
3954 + goto out_nounlock;
3957 + read_lock(&tasklist_lock);
3958 + p = find_process_by_pid(pid);
3962 + retval = security_task_getscheduler(p);
3966 + t = ns_to_timespec(p->policy == SCHED_FIFO ? 0 :
3967 + MS_TO_NS(task_timeslice(p)));
3968 + read_unlock(&tasklist_lock);
3969 + retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
3973 + read_unlock(&tasklist_lock);
3977 +static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
3979 +void sched_show_task(struct task_struct *p)
3981 + unsigned long free = 0;
3984 + state = p->state ? __ffs(p->state) + 1 : 0;
3985 + printk(KERN_INFO "%-13.13s %c", p->comm,
3986 + state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
3987 +#if BITS_PER_LONG == 32
3988 + if (state == TASK_RUNNING)
3989 + printk(KERN_CONT " running ");
3991 + printk(KERN_CONT " %08lx ", thread_saved_pc(p));
3993 + if (state == TASK_RUNNING)
3994 + printk(KERN_CONT " running task ");
3996 + printk(KERN_CONT " %016lx ", thread_saved_pc(p));
3998 +#ifdef CONFIG_DEBUG_STACK_USAGE
3999 + free = stack_not_used(p);
4001 + printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
4002 + task_pid_nr(p), task_pid_nr(p->real_parent),
4003 + (unsigned long)task_thread_info(p)->flags);
4005 + show_stack(p, NULL);
4008 +void show_state_filter(unsigned long state_filter)
4010 + struct task_struct *g, *p;
4012 +#if BITS_PER_LONG == 32
4014 + " task PC stack pid father\n");
4017 + " task PC stack pid father\n");
4019 + read_lock(&tasklist_lock);
4020 + do_each_thread(g, p) {
4022 + * reset the NMI-timeout, listing all files on a slow
4023 + * console might take alot of time:
4025 + touch_nmi_watchdog();
4026 + if (!state_filter || (p->state & state_filter))
4027 + sched_show_task(p);
4028 + } while_each_thread(g, p);
4030 + touch_all_softlockup_watchdogs();
4032 + read_unlock(&tasklist_lock);
4034 + * Only show locks if all tasks are dumped:
4036 + if (state_filter == -1)
4037 + debug_show_all_locks();
4041 + * init_idle - set up an idle thread for a given CPU
4042 + * @idle: task in question
4043 + * @cpu: cpu the idle task belongs to
4045 + * NOTE: this function does not set the idle thread's NEED_RESCHED
4046 + * flag, to make booting more robust.
4048 +void __cpuinit init_idle(struct task_struct *idle, int cpu)
4050 + struct rq *rq = cpu_rq(cpu);
4051 + unsigned long flags;
4053 + time_grq_lock(rq, &flags);
4054 + idle->timestamp = idle->last_ran = rq->clock;
4055 + idle->state = TASK_RUNNING;
4056 + /* Setting prio to illegal value shouldn't matter when never queued */
4057 + idle->prio = rq->rq_prio = PRIO_LIMIT;
4058 + rq->rq_deadline = idle->deadline;
4059 + rq->rq_policy = idle->policy;
4060 + rq->rq_time_slice = idle->rt.time_slice;
4061 + idle->cpus_allowed = cpumask_of_cpu(cpu);
4062 + set_task_cpu(idle, cpu);
4063 + rq->curr = rq->idle = idle;
4065 + set_cpuidle_map(cpu);
4066 +#ifdef CONFIG_HOTPLUG_CPU
4067 + idle->unplugged_mask = CPU_MASK_NONE;
4069 + grq_unlock_irqrestore(&flags);
4071 + /* Set the preempt count _outside_ the spinlocks! */
4072 +#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
4073 + task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
4075 + task_thread_info(idle)->preempt_count = 0;
4077 + ftrace_graph_init_task(idle);
4081 + * In a system that switches off the HZ timer nohz_cpu_mask
4082 + * indicates which cpus entered this state. This is used
4083 + * in the rcu update to wait only for active cpus. For system
4084 + * which do not switch off the HZ timer nohz_cpu_mask should
4085 + * always be CPU_BITS_NONE.
4087 +cpumask_var_t nohz_cpu_mask;
4090 +#ifdef CONFIG_NO_HZ
4092 + atomic_t load_balancer;
4093 + cpumask_var_t cpu_mask;
4094 + cpumask_var_t ilb_grp_nohz_mask;
4095 +} nohz ____cacheline_aligned = {
4096 + .load_balancer = ATOMIC_INIT(-1),
4099 +int get_nohz_load_balancer(void)
4101 + return atomic_read(&nohz.load_balancer);
4105 + * This routine will try to nominate the ilb (idle load balancing)
4106 + * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4107 + * load balancing on behalf of all those cpus. If all the cpus in the system
4108 + * go into this tickless mode, then there will be no ilb owner (as there is
4109 + * no need for one) and all the cpus will sleep till the next wakeup event
4112 + * For the ilb owner, tick is not stopped. And this tick will be used
4113 + * for idle load balancing. ilb owner will still be part of
4116 + * While stopping the tick, this cpu will become the ilb owner if there
4117 + * is no other owner. And will be the owner till that cpu becomes busy
4118 + * or if all cpus in the system stop their ticks at which point
4119 + * there is no need for ilb owner.
4121 + * When the ilb owner becomes busy, it nominates another owner, during the
4122 + * next busy scheduler_tick()
4124 +int select_nohz_load_balancer(int stop_tick)
4126 + int cpu = smp_processor_id();
4129 + cpu_rq(cpu)->in_nohz_recently = 1;
4131 + if (!cpu_active(cpu)) {
4132 + if (atomic_read(&nohz.load_balancer) != cpu)
4136 + * If we are going offline and still the leader,
4139 + if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4145 + cpumask_set_cpu(cpu, nohz.cpu_mask);
4147 + /* time for ilb owner also to sleep */
4148 + if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
4149 + if (atomic_read(&nohz.load_balancer) == cpu)
4150 + atomic_set(&nohz.load_balancer, -1);
4154 + if (atomic_read(&nohz.load_balancer) == -1) {
4155 + /* make me the ilb owner */
4156 + if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4158 + } else if (atomic_read(&nohz.load_balancer) == cpu)
4161 + if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
4164 + cpumask_clear_cpu(cpu, nohz.cpu_mask);
4166 + if (atomic_read(&nohz.load_balancer) == cpu)
4167 + if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4174 + * When add_timer_on() enqueues a timer into the timer wheel of an
4175 + * idle CPU then this timer might expire before the next timer event
4176 + * which is scheduled to wake up that CPU. In case of a completely
4177 + * idle system the next event might even be infinite time into the
4178 + * future. wake_up_idle_cpu() ensures that the CPU is woken up and
4179 + * leaves the inner idle loop so the newly added timer is taken into
4180 + * account when the CPU goes back to idle and evaluates the timer
4181 + * wheel for the next timer event.
4183 +void wake_up_idle_cpu(int cpu)
4185 + struct task_struct *idle;
4188 + if (cpu == smp_processor_id())
4195 + * This is safe, as this function is called with the timer
4196 + * wheel base lock of (cpu) held. When the CPU is on the way
4197 + * to idle and has not yet set rq->curr to idle then it will
4198 + * be serialized on the timer wheel base lock and take the new
4199 + * timer into account automatically.
4201 + if (unlikely(rq->curr != idle))
4205 + * We can set TIF_RESCHED on the idle task of the other CPU
4206 + * lockless. The worst case is that the other CPU runs the
4207 + * idle task through an additional NOOP schedule()
4209 + set_tsk_need_resched(idle);
4211 + /* NEED_RESCHED must be visible before we test polling */
4213 + if (!tsk_is_polling(idle))
4214 + smp_send_reschedule(cpu);
4217 +#endif /* CONFIG_NO_HZ */
4220 + * Change a given task's CPU affinity. Migrate the thread to a
4221 + * proper CPU and schedule it away if the CPU it's executing on
4222 + * is removed from the allowed bitmask.
4224 + * NOTE: the caller must have a valid reference to the task, the
4225 + * task must not exit() & deallocate itself prematurely. The
4226 + * call is not atomic; no spinlocks may be held.
4228 +int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
4230 + unsigned long flags;
4236 + rq = task_grq_lock(p, &flags);
4237 + if (!cpumask_intersects(new_mask, cpu_online_mask)) {
4242 + if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
4243 + !cpumask_equal(&p->cpus_allowed, new_mask))) {
4248 + queued = task_queued_only(p);
4250 + cpumask_copy(&p->cpus_allowed, new_mask);
4251 + p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
4253 + /* Can the task run on the task's current CPU? If so, we're done */
4254 + if (cpumask_test_cpu(task_cpu(p), new_mask))
4257 + /* Reschedule the task, schedule() will know if it can keep running */
4258 + if (task_running(p))
4261 + set_task_cpu(p, cpumask_any_and(cpu_online_mask, new_mask));
4266 + task_grq_unlock(&flags);
4268 + /* This might be a flaky way of changing cpus! */
4273 +EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
4275 +#ifdef CONFIG_HOTPLUG_CPU
4276 +/* Schedules idle task to be the next runnable task on current CPU.
4277 + * It does so by boosting its priority to highest possible.
4278 + * Used by CPU offline code.
4280 +void sched_idle_next(void)
4282 + int this_cpu = smp_processor_id();
4283 + struct rq *rq = cpu_rq(this_cpu);
4284 + struct task_struct *idle = rq->idle;
4285 + unsigned long flags;
4287 + /* cpu has to be offline */
4288 + BUG_ON(cpu_online(this_cpu));
4291 + * Strictly not necessary since rest of the CPUs are stopped by now
4292 + * and interrupts disabled on the current cpu.
4294 + time_grq_lock(rq, &flags);
4296 + __setscheduler(idle, SCHED_FIFO, MAX_RT_PRIO - 1);
4298 + activate_idle_task(idle);
4299 + set_tsk_need_resched(rq->curr);
4301 + grq_unlock_irqrestore(&flags);
4305 + * Ensures that the idle task is using init_mm right before its cpu goes
4308 +void idle_task_exit(void)
4310 + struct mm_struct *mm = current->active_mm;
4312 + BUG_ON(cpu_online(smp_processor_id()));
4314 + if (mm != &init_mm)
4315 + switch_mm(mm, &init_mm, current);
4319 +#endif /* CONFIG_HOTPLUG_CPU */
4321 +#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
4323 +static struct ctl_table sd_ctl_dir[] = {
4325 + .procname = "sched_domain",
4331 +static struct ctl_table sd_ctl_root[] = {
4333 + .ctl_name = CTL_KERN,
4334 + .procname = "kernel",
4336 + .child = sd_ctl_dir,
4341 +static struct ctl_table *sd_alloc_ctl_entry(int n)
4343 + struct ctl_table *entry =
4344 + kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
4349 +static void sd_free_ctl_entry(struct ctl_table **tablep)
4351 + struct ctl_table *entry;
4354 + * In the intermediate directories, both the child directory and
4355 + * procname are dynamically allocated and could fail but the mode
4356 + * will always be set. In the lowest directory the names are
4357 + * static strings and all have proc handlers.
4359 + for (entry = *tablep; entry->mode; entry++) {
4361 + sd_free_ctl_entry(&entry->child);
4362 + if (entry->proc_handler == NULL)
4363 + kfree(entry->procname);
4371 +set_table_entry(struct ctl_table *entry,
4372 + const char *procname, void *data, int maxlen,
4373 + mode_t mode, proc_handler *proc_handler)
4375 + entry->procname = procname;
4376 + entry->data = data;
4377 + entry->maxlen = maxlen;
4378 + entry->mode = mode;
4379 + entry->proc_handler = proc_handler;
4382 +static struct ctl_table *
4383 +sd_alloc_ctl_domain_table(struct sched_domain *sd)
4385 + struct ctl_table *table = sd_alloc_ctl_entry(13);
4387 + if (table == NULL)
4390 + set_table_entry(&table[0], "min_interval", &sd->min_interval,
4391 + sizeof(long), 0644, proc_doulongvec_minmax);
4392 + set_table_entry(&table[1], "max_interval", &sd->max_interval,
4393 + sizeof(long), 0644, proc_doulongvec_minmax);
4394 + set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
4395 + sizeof(int), 0644, proc_dointvec_minmax);
4396 + set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
4397 + sizeof(int), 0644, proc_dointvec_minmax);
4398 + set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
4399 + sizeof(int), 0644, proc_dointvec_minmax);
4400 + set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
4401 + sizeof(int), 0644, proc_dointvec_minmax);
4402 + set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
4403 + sizeof(int), 0644, proc_dointvec_minmax);
4404 + set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
4405 + sizeof(int), 0644, proc_dointvec_minmax);
4406 + set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
4407 + sizeof(int), 0644, proc_dointvec_minmax);
4408 + set_table_entry(&table[9], "cache_nice_tries",
4409 + &sd->cache_nice_tries,
4410 + sizeof(int), 0644, proc_dointvec_minmax);
4411 + set_table_entry(&table[10], "flags", &sd->flags,
4412 + sizeof(int), 0644, proc_dointvec_minmax);
4413 + set_table_entry(&table[11], "name", sd->name,
4414 + CORENAME_MAX_SIZE, 0444, proc_dostring);
4415 + /* &table[12] is terminator */
4420 +static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
4422 + struct ctl_table *entry, *table;
4423 + struct sched_domain *sd;
4424 + int domain_num = 0, i;
4427 + for_each_domain(cpu, sd)
4429 + entry = table = sd_alloc_ctl_entry(domain_num + 1);
4430 + if (table == NULL)
4434 + for_each_domain(cpu, sd) {
4435 + snprintf(buf, 32, "domain%d", i);
4436 + entry->procname = kstrdup(buf, GFP_KERNEL);
4437 + entry->mode = 0555;
4438 + entry->child = sd_alloc_ctl_domain_table(sd);
4445 +static struct ctl_table_header *sd_sysctl_header;
4446 +static void register_sched_domain_sysctl(void)
4448 + int i, cpu_num = num_online_cpus();
4449 + struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
4452 + WARN_ON(sd_ctl_dir[0].child);
4453 + sd_ctl_dir[0].child = entry;
4455 + if (entry == NULL)
4458 + for_each_online_cpu(i) {
4459 + snprintf(buf, 32, "cpu%d", i);
4460 + entry->procname = kstrdup(buf, GFP_KERNEL);
4461 + entry->mode = 0555;
4462 + entry->child = sd_alloc_ctl_cpu_table(i);
4466 + WARN_ON(sd_sysctl_header);
4467 + sd_sysctl_header = register_sysctl_table(sd_ctl_root);
4470 +/* may be called multiple times per register */
4471 +static void unregister_sched_domain_sysctl(void)
4473 + if (sd_sysctl_header)
4474 + unregister_sysctl_table(sd_sysctl_header);
4475 + sd_sysctl_header = NULL;
4476 + if (sd_ctl_dir[0].child)
4477 + sd_free_ctl_entry(&sd_ctl_dir[0].child);
4480 +static void register_sched_domain_sysctl(void)
4483 +static void unregister_sched_domain_sysctl(void)
4488 +static void set_rq_online(struct rq *rq)
4490 + if (!rq->online) {
4491 + cpumask_set_cpu(rq->cpu, rq->rd->online);
4496 +static void set_rq_offline(struct rq *rq)
4499 + cpumask_clear_cpu(rq->cpu, rq->rd->online);
4504 +#ifdef CONFIG_HOTPLUG_CPU
4506 + * This cpu is going down, so walk over the tasklist and find tasks that can
4507 + * only run on this cpu and remove their affinity. Store their value in
4508 + * unplugged_mask so it can be restored once their correct cpu is online. No
4509 + * need to do anything special since they'll just move on next reschedule if
4510 + * they're running.
4512 +static void remove_cpu(unsigned long cpu)
4514 + struct task_struct *p, *t;
4516 + read_lock(&tasklist_lock);
4518 + do_each_thread(t, p) {
4519 + cpumask_t cpus_remaining;
4521 + cpus_and(cpus_remaining, p->cpus_allowed, cpu_online_map);
4522 + cpu_clear(cpu, cpus_remaining);
4523 + if (cpus_empty(cpus_remaining)) {
4524 + p->unplugged_mask = p->cpus_allowed;
4525 + p->cpus_allowed = cpu_possible_map;
4527 + } while_each_thread(t, p);
4529 + read_unlock(&tasklist_lock);
4533 + * This cpu is coming up so add it to the cpus_allowed.
4535 +static void add_cpu(unsigned long cpu)
4537 + struct task_struct *p, *t;
4539 + read_lock(&tasklist_lock);
4541 + do_each_thread(t, p) {
4542 + /* Have we taken all the cpus from the unplugged_mask back */
4543 + if (cpus_empty(p->unplugged_mask))
4546 + /* Was this cpu in the unplugged_mask mask */
4547 + if (cpu_isset(cpu, p->unplugged_mask)) {
4548 + cpu_set(cpu, p->cpus_allowed);
4549 + if (cpus_subset(p->unplugged_mask, p->cpus_allowed)) {
4551 + * Have we set more than the unplugged_mask?
4552 + * If so, that means we have remnants set from
4553 + * the unplug/plug cycle and need to remove
4554 + * them. Then clear the unplugged_mask as we've
4555 + * set all the cpus back.
4557 + p->cpus_allowed = p->unplugged_mask;
4558 + cpus_clear(p->unplugged_mask);
4561 + } while_each_thread(t, p);
4563 + read_unlock(&tasklist_lock);
4566 +static void add_cpu(unsigned long cpu)
4572 + * migration_call - callback that gets triggered when a CPU is added.
4574 +static int __cpuinit
4575 +migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
4577 + int cpu = (long)hcpu;
4578 + unsigned long flags;
4583 + case CPU_UP_PREPARE:
4584 + case CPU_UP_PREPARE_FROZEN:
4588 + case CPU_ONLINE_FROZEN:
4589 + /* Update our root-domain */
4591 + grq_lock_irqsave(&flags);
4593 + BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
4595 + set_rq_online(rq);
4598 + grq_unlock_irqrestore(&flags);
4601 +#ifdef CONFIG_HOTPLUG_CPU
4602 + case CPU_UP_CANCELED:
4603 + case CPU_UP_CANCELED_FROZEN:
4607 + case CPU_DEAD_FROZEN:
4608 + cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
4610 + /* Idle task back to normal (off runqueue, low prio) */
4613 + deactivate_task(rq->idle);
4614 + rq->idle->static_prio = MAX_PRIO;
4615 + __setscheduler(rq->idle, SCHED_NORMAL, 0);
4616 + rq->idle->prio = PRIO_LIMIT;
4617 + update_rq_clock(rq);
4623 + case CPU_DYING_FROZEN:
4625 + grq_lock_irqsave(&flags);
4627 + BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
4628 + set_rq_offline(rq);
4630 + grq_unlock_irqrestore(&flags);
4638 + * Register at high priority so that task migration (migrate_all_tasks)
4639 + * happens before everything else. This has to be lower priority than
4640 + * the notifier in the perf_counter subsystem, though.
4642 +static struct notifier_block __cpuinitdata migration_notifier = {
4643 + .notifier_call = migration_call,
4647 +int __init migration_init(void)
4649 + void *cpu = (void *)(long)smp_processor_id();
4652 + /* Start one for the boot CPU: */
4653 + err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
4654 + BUG_ON(err == NOTIFY_BAD);
4655 + migration_call(&migration_notifier, CPU_ONLINE, cpu);
4656 + register_cpu_notifier(&migration_notifier);
4660 +early_initcall(migration_init);
4664 + * sched_domains_mutex serializes calls to arch_init_sched_domains,
4665 + * detach_destroy_domains and partition_sched_domains.
4667 +static DEFINE_MUTEX(sched_domains_mutex);
4671 +#ifdef CONFIG_SCHED_DEBUG
4673 +static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
4674 + struct cpumask *groupmask)
4676 + struct sched_group *group = sd->groups;
4679 + cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
4680 + cpumask_clear(groupmask);
4682 + printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
4684 + if (!(sd->flags & SD_LOAD_BALANCE)) {
4685 + printk("does not load-balance\n");
4687 + printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
4692 + printk(KERN_CONT "span %s level %s\n", str, sd->name);
4694 + if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
4695 + printk(KERN_ERR "ERROR: domain->span does not contain "
4698 + if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
4699 + printk(KERN_ERR "ERROR: domain->groups does not contain"
4703 + printk(KERN_DEBUG "%*s groups:", level + 1, "");
4707 + printk(KERN_ERR "ERROR: group is NULL\n");
4711 + if (!group->__cpu_power) {
4712 + printk(KERN_CONT "\n");
4713 + printk(KERN_ERR "ERROR: domain->cpu_power not "
4718 + if (!cpumask_weight(sched_group_cpus(group))) {
4719 + printk(KERN_CONT "\n");
4720 + printk(KERN_ERR "ERROR: empty group\n");
4724 + if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
4725 + printk(KERN_CONT "\n");
4726 + printk(KERN_ERR "ERROR: repeated CPUs\n");
4730 + cpumask_or(groupmask, groupmask, sched_group_cpus(group));
4732 + cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
4734 + printk(KERN_CONT " %s", str);
4735 + if (group->__cpu_power != SCHED_LOAD_SCALE) {
4736 + printk(KERN_CONT " (__cpu_power = %d)",
4737 + group->__cpu_power);
4740 + group = group->next;
4741 + } while (group != sd->groups);
4742 + printk(KERN_CONT "\n");
4744 + if (!cpumask_equal(sched_domain_span(sd), groupmask))
4745 + printk(KERN_ERR "ERROR: groups don't span domain->span\n");
4748 + !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
4749 + printk(KERN_ERR "ERROR: parent span is not a superset "
4750 + "of domain->span\n");
4754 +static void sched_domain_debug(struct sched_domain *sd, int cpu)
4756 + cpumask_var_t groupmask;
4760 + printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
4764 + printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
4766 + if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
4767 + printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
4772 + if (sched_domain_debug_one(sd, cpu, level, groupmask))
4779 + free_cpumask_var(groupmask);
4781 +#else /* !CONFIG_SCHED_DEBUG */
4782 +# define sched_domain_debug(sd, cpu) do { } while (0)
4783 +#endif /* CONFIG_SCHED_DEBUG */
4785 +static int sd_degenerate(struct sched_domain *sd)
4787 + if (cpumask_weight(sched_domain_span(sd)) == 1)
4790 + /* Following flags need at least 2 groups */
4791 + if (sd->flags & (SD_LOAD_BALANCE |
4792 + SD_BALANCE_NEWIDLE |
4795 + SD_SHARE_CPUPOWER |
4796 + SD_SHARE_PKG_RESOURCES)) {
4797 + if (sd->groups != sd->groups->next)
4801 + /* Following flags don't use groups */
4802 + if (sd->flags & (SD_WAKE_IDLE |
4811 +sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
4813 + unsigned long cflags = sd->flags, pflags = parent->flags;
4815 + if (sd_degenerate(parent))
4818 + if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
4821 + /* Does parent contain flags not in child? */
4822 + /* WAKE_BALANCE is a subset of WAKE_AFFINE */
4823 + if (cflags & SD_WAKE_AFFINE)
4824 + pflags &= ~SD_WAKE_BALANCE;
4825 + /* Flags needing groups don't count if only 1 group in parent */
4826 + if (parent->groups == parent->groups->next) {
4827 + pflags &= ~(SD_LOAD_BALANCE |
4828 + SD_BALANCE_NEWIDLE |
4831 + SD_SHARE_CPUPOWER |
4832 + SD_SHARE_PKG_RESOURCES);
4833 + if (nr_node_ids == 1)
4834 + pflags &= ~SD_SERIALIZE;
4836 + if (~cflags & pflags)
4842 +static void free_rootdomain(struct root_domain *rd)
4844 + free_cpumask_var(rd->rto_mask);
4845 + free_cpumask_var(rd->online);
4846 + free_cpumask_var(rd->span);
4850 +static void rq_attach_root(struct rq *rq, struct root_domain *rd)
4852 + struct root_domain *old_rd = NULL;
4853 + unsigned long flags;
4855 + grq_lock_irqsave(&flags);
4860 + if (cpumask_test_cpu(rq->cpu, old_rd->online))
4861 + set_rq_offline(rq);
4863 + cpumask_clear_cpu(rq->cpu, old_rd->span);
4866 + * If we dont want to free the old_rt yet then
4867 + * set old_rd to NULL to skip the freeing later
4868 + * in this function:
4870 + if (!atomic_dec_and_test(&old_rd->refcount))
4874 + atomic_inc(&rd->refcount);
4877 + cpumask_set_cpu(rq->cpu, rd->span);
4878 + if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
4879 + set_rq_online(rq);
4881 + grq_unlock_irqrestore(&flags);
4884 + free_rootdomain(old_rd);
4887 +static int init_rootdomain(struct root_domain *rd, bool bootmem)
4889 + gfp_t gfp = GFP_KERNEL;
4891 + memset(rd, 0, sizeof(*rd));
4896 + if (!alloc_cpumask_var(&rd->span, gfp))
4898 + if (!alloc_cpumask_var(&rd->online, gfp))
4900 + if (!alloc_cpumask_var(&rd->rto_mask, gfp))
4906 + free_cpumask_var(rd->online);
4908 + free_cpumask_var(rd->span);
4913 +static void init_defrootdomain(void)
4915 + init_rootdomain(&def_root_domain, true);
4917 + atomic_set(&def_root_domain.refcount, 1);
4920 +static struct root_domain *alloc_rootdomain(void)
4922 + struct root_domain *rd;
4924 + rd = kmalloc(sizeof(*rd), GFP_KERNEL);
4928 + if (init_rootdomain(rd, false) != 0) {
4937 + * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
4938 + * hold the hotplug lock.
4941 +cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
4943 + struct rq *rq = cpu_rq(cpu);
4944 + struct sched_domain *tmp;
4946 + /* Remove the sched domains which do not contribute to scheduling. */
4947 + for (tmp = sd; tmp; ) {
4948 + struct sched_domain *parent = tmp->parent;
4952 + if (sd_parent_degenerate(tmp, parent)) {
4953 + tmp->parent = parent->parent;
4954 + if (parent->parent)
4955 + parent->parent->child = tmp;
4957 + tmp = tmp->parent;
4960 + if (sd && sd_degenerate(sd)) {
4966 + sched_domain_debug(sd, cpu);
4968 + rq_attach_root(rq, rd);
4969 + rcu_assign_pointer(rq->sd, sd);
4972 +/* cpus with isolated domains */
4973 +static cpumask_var_t cpu_isolated_map;
4975 +/* Setup the mask of cpus configured for isolated domains */
4976 +static int __init isolated_cpu_setup(char *str)
4978 + cpulist_parse(str, cpu_isolated_map);
4982 +__setup("isolcpus=", isolated_cpu_setup);
4985 + * init_sched_build_groups takes the cpumask we wish to span, and a pointer
4986 + * to a function which identifies what group(along with sched group) a CPU
4987 + * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
4988 + * (due to the fact that we keep track of groups covered with a struct cpumask).
4990 + * init_sched_build_groups will build a circular linked list of the groups
4991 + * covered by the given span, and will set each group's ->cpumask correctly,
4992 + * and ->cpu_power to 0.
4995 +init_sched_build_groups(const struct cpumask *span,
4996 + const struct cpumask *cpu_map,
4997 + int (*group_fn)(int cpu, const struct cpumask *cpu_map,
4998 + struct sched_group **sg,
4999 + struct cpumask *tmpmask),
5000 + struct cpumask *covered, struct cpumask *tmpmask)
5002 + struct sched_group *first = NULL, *last = NULL;
5005 + cpumask_clear(covered);
5007 + for_each_cpu(i, span) {
5008 + struct sched_group *sg;
5009 + int group = group_fn(i, cpu_map, &sg, tmpmask);
5012 + if (cpumask_test_cpu(i, covered))
5015 + cpumask_clear(sched_group_cpus(sg));
5016 + sg->__cpu_power = 0;
5018 + for_each_cpu(j, span) {
5019 + if (group_fn(j, cpu_map, NULL, tmpmask) != group)
5022 + cpumask_set_cpu(j, covered);
5023 + cpumask_set_cpu(j, sched_group_cpus(sg));
5031 + last->next = first;
5034 +#define SD_NODES_PER_DOMAIN 16
5039 + * find_next_best_node - find the next node to include in a sched_domain
5040 + * @node: node whose sched_domain we're building
5041 + * @used_nodes: nodes already in the sched_domain
5043 + * Find the next node to include in a given scheduling domain. Simply
5044 + * finds the closest node not already in the @used_nodes map.
5046 + * Should use nodemask_t.
5048 +static int find_next_best_node(int node, nodemask_t *used_nodes)
5050 + int i, n, val, min_val, best_node = 0;
5052 + min_val = INT_MAX;
5054 + for (i = 0; i < nr_node_ids; i++) {
5055 + /* Start at @node */
5056 + n = (node + i) % nr_node_ids;
5058 + if (!nr_cpus_node(n))
5061 + /* Skip already used nodes */
5062 + if (node_isset(n, *used_nodes))
5065 + /* Simple min distance search */
5066 + val = node_distance(node, n);
5068 + if (val < min_val) {
5074 + node_set(best_node, *used_nodes);
5079 + * sched_domain_node_span - get a cpumask for a node's sched_domain
5080 + * @node: node whose cpumask we're constructing
5081 + * @span: resulting cpumask
5083 + * Given a node, construct a good cpumask for its sched_domain to span. It
5084 + * should be one that prevents unnecessary balancing, but also spreads tasks
5087 +static void sched_domain_node_span(int node, struct cpumask *span)
5089 + nodemask_t used_nodes;
5092 + cpumask_clear(span);
5093 + nodes_clear(used_nodes);
5095 + cpumask_or(span, span, cpumask_of_node(node));
5096 + node_set(node, used_nodes);
5098 + for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
5099 + int next_node = find_next_best_node(node, &used_nodes);
5101 + cpumask_or(span, span, cpumask_of_node(next_node));
5104 +#endif /* CONFIG_NUMA */
5106 +int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
5109 + * The cpus mask in sched_group and sched_domain hangs off the end.
5111 + * ( See the the comments in include/linux/sched.h:struct sched_group
5112 + * and struct sched_domain. )
5114 +struct static_sched_group {
5115 + struct sched_group sg;
5116 + DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
5119 +struct static_sched_domain {
5120 + struct sched_domain sd;
5121 + DECLARE_BITMAP(span, CONFIG_NR_CPUS);
5125 + * SMT sched-domains:
5127 +#ifdef CONFIG_SCHED_SMT
5128 +static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
5129 +static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
5132 +cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
5133 + struct sched_group **sg, struct cpumask *unused)
5136 + *sg = &per_cpu(sched_group_cpus, cpu).sg;
5139 +#endif /* CONFIG_SCHED_SMT */
5142 + * multi-core sched-domains:
5144 +#ifdef CONFIG_SCHED_MC
5145 +static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
5146 +static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
5147 +#endif /* CONFIG_SCHED_MC */
5149 +#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
5151 +cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
5152 + struct sched_group **sg, struct cpumask *mask)
5156 + cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
5157 + group = cpumask_first(mask);
5159 + *sg = &per_cpu(sched_group_core, group).sg;
5162 +#elif defined(CONFIG_SCHED_MC)
5164 +cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
5165 + struct sched_group **sg, struct cpumask *unused)
5168 + *sg = &per_cpu(sched_group_core, cpu).sg;
5173 +static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
5174 +static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
5177 +cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
5178 + struct sched_group **sg, struct cpumask *mask)
5181 +#ifdef CONFIG_SCHED_MC
5182 + cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
5183 + group = cpumask_first(mask);
5184 +#elif defined(CONFIG_SCHED_SMT)
5185 + cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
5186 + group = cpumask_first(mask);
5191 + *sg = &per_cpu(sched_group_phys, group).sg;
5196 + * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
5197 + * @group: The group whose first cpu is to be returned.
5199 +static inline unsigned int group_first_cpu(struct sched_group *group)
5201 + return cpumask_first(sched_group_cpus(group));
5206 + * The init_sched_build_groups can't handle what we want to do with node
5207 + * groups, so roll our own. Now each node has its own list of groups which
5208 + * gets dynamically allocated.
5210 +static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
5211 +static struct sched_group ***sched_group_nodes_bycpu;
5213 +static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
5214 +static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
5216 +static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
5217 + struct sched_group **sg,
5218 + struct cpumask *nodemask)
5222 + cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
5223 + group = cpumask_first(nodemask);
5226 + *sg = &per_cpu(sched_group_allnodes, group).sg;
5230 +static void init_numa_sched_groups_power(struct sched_group *group_head)
5232 + struct sched_group *sg = group_head;
5238 + for_each_cpu(j, sched_group_cpus(sg)) {
5239 + struct sched_domain *sd;
5241 + sd = &per_cpu(phys_domains, j).sd;
5242 + if (j != group_first_cpu(sd->groups)) {
5244 + * Only add "power" once for each
5245 + * physical package.
5250 + sg_inc_cpu_power(sg, sd->groups->__cpu_power);
5253 + } while (sg != group_head);
5255 +#endif /* CONFIG_NUMA */
5258 +/* Free memory allocated for various sched_group structures */
5259 +static void free_sched_groups(const struct cpumask *cpu_map,
5260 + struct cpumask *nodemask)
5264 + for_each_cpu(cpu, cpu_map) {
5265 + struct sched_group **sched_group_nodes
5266 + = sched_group_nodes_bycpu[cpu];
5268 + if (!sched_group_nodes)
5271 + for (i = 0; i < nr_node_ids; i++) {
5272 + struct sched_group *oldsg, *sg = sched_group_nodes[i];
5274 + cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
5275 + if (cpumask_empty(nodemask))
5285 + if (oldsg != sched_group_nodes[i])
5288 + kfree(sched_group_nodes);
5289 + sched_group_nodes_bycpu[cpu] = NULL;
5292 +#else /* !CONFIG_NUMA */
5293 +static void free_sched_groups(const struct cpumask *cpu_map,
5294 + struct cpumask *nodemask)
5297 +#endif /* CONFIG_NUMA */
5300 + * Initialize sched groups cpu_power.
5302 + * cpu_power indicates the capacity of sched group, which is used while
5303 + * distributing the load between different sched groups in a sched domain.
5304 + * Typically cpu_power for all the groups in a sched domain will be same unless
5305 + * there are asymmetries in the topology. If there are asymmetries, group
5306 + * having more cpu_power will pickup more load compared to the group having
5309 + * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
5310 + * the maximum number of tasks a group can handle in the presence of other idle
5311 + * or lightly loaded groups in the same sched domain.
5313 +static void init_sched_groups_power(int cpu, struct sched_domain *sd)
5315 + struct sched_domain *child;
5316 + struct sched_group *group;
5318 + WARN_ON(!sd || !sd->groups);
5320 + if (cpu != group_first_cpu(sd->groups))
5323 + child = sd->child;
5325 + sd->groups->__cpu_power = 0;
5328 + * For perf policy, if the groups in child domain share resources
5329 + * (for example cores sharing some portions of the cache hierarchy
5330 + * or SMT), then set this domain groups cpu_power such that each group
5331 + * can handle only one task, when there are other idle groups in the
5332 + * same sched domain.
5334 + if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
5336 + (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
5337 + sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
5342 + * add cpu_power of each child group to this groups cpu_power
5344 + group = child->groups;
5346 + sg_inc_cpu_power(sd->groups, group->__cpu_power);
5347 + group = group->next;
5348 + } while (group != child->groups);
5352 + * Initializers for schedule domains
5353 + * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
5356 +#ifdef CONFIG_SCHED_DEBUG
5357 +# define SD_INIT_NAME(sd, type) sd->name = #type
5359 +# define SD_INIT_NAME(sd, type) do { } while (0)
5362 +#define SD_INIT(sd, type) sd_init_##type(sd)
5364 +#define SD_INIT_FUNC(type) \
5365 +static noinline void sd_init_##type(struct sched_domain *sd) \
5367 + memset(sd, 0, sizeof(*sd)); \
5368 + *sd = SD_##type##_INIT; \
5369 + sd->level = SD_LV_##type; \
5370 + SD_INIT_NAME(sd, type); \
5375 + SD_INIT_FUNC(ALLNODES)
5376 + SD_INIT_FUNC(NODE)
5378 +#ifdef CONFIG_SCHED_SMT
5379 + SD_INIT_FUNC(SIBLING)
5381 +#ifdef CONFIG_SCHED_MC
5385 +static int default_relax_domain_level = -1;
5387 +static int __init setup_relax_domain_level(char *str)
5389 + unsigned long val;
5391 + val = simple_strtoul(str, NULL, 0);
5392 + if (val < SD_LV_MAX)
5393 + default_relax_domain_level = val;
5397 +__setup("relax_domain_level=", setup_relax_domain_level);
5399 +static void set_domain_attribute(struct sched_domain *sd,
5400 + struct sched_domain_attr *attr)
5404 + if (!attr || attr->relax_domain_level < 0) {
5405 + if (default_relax_domain_level < 0)
5408 + request = default_relax_domain_level;
5410 + request = attr->relax_domain_level;
5411 + if (request < sd->level) {
5412 + /* turn off idle balance on this domain */
5413 + sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
5415 + /* turn on idle balance on this domain */
5416 + sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
5421 + * Build sched domains for a given set of cpus and attach the sched domains
5422 + * to the individual cpus
5424 +static int __build_sched_domains(const struct cpumask *cpu_map,
5425 + struct sched_domain_attr *attr)
5427 + int i, err = -ENOMEM;
5428 + struct root_domain *rd;
5429 + cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
5432 + cpumask_var_t domainspan, covered, notcovered;
5433 + struct sched_group **sched_group_nodes = NULL;
5434 + int sd_allnodes = 0;
5436 + if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
5438 + if (!alloc_cpumask_var(&covered, GFP_KERNEL))
5439 + goto free_domainspan;
5440 + if (!alloc_cpumask_var(¬covered, GFP_KERNEL))
5441 + goto free_covered;
5444 + if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
5445 + goto free_notcovered;
5446 + if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
5447 + goto free_nodemask;
5448 + if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
5449 + goto free_this_sibling_map;
5450 + if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
5451 + goto free_this_core_map;
5452 + if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
5453 + goto free_send_covered;
5457 + * Allocate the per-node list of sched groups
5459 + sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
5461 + if (!sched_group_nodes) {
5462 + printk(KERN_WARNING "Can not alloc sched group node list\n");
5463 + goto free_tmpmask;
5467 + rd = alloc_rootdomain();
5469 + printk(KERN_WARNING "Cannot alloc root domain\n");
5470 + goto free_sched_groups;
5474 + sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
5478 + * Set up domains for cpus specified by the cpu_map.
5480 + for_each_cpu(i, cpu_map) {
5481 + struct sched_domain *sd = NULL, *p;
5483 + cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
5486 + if (cpumask_weight(cpu_map) >
5487 + SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
5488 + sd = &per_cpu(allnodes_domains, i).sd;
5489 + SD_INIT(sd, ALLNODES);
5490 + set_domain_attribute(sd, attr);
5491 + cpumask_copy(sched_domain_span(sd), cpu_map);
5492 + cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
5498 + sd = &per_cpu(node_domains, i).sd;
5499 + SD_INIT(sd, NODE);
5500 + set_domain_attribute(sd, attr);
5501 + sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
5505 + cpumask_and(sched_domain_span(sd),
5506 + sched_domain_span(sd), cpu_map);
5510 + sd = &per_cpu(phys_domains, i).sd;
5512 + set_domain_attribute(sd, attr);
5513 + cpumask_copy(sched_domain_span(sd), nodemask);
5517 + cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
5519 +#ifdef CONFIG_SCHED_MC
5521 + sd = &per_cpu(core_domains, i).sd;
5523 + set_domain_attribute(sd, attr);
5524 + cpumask_and(sched_domain_span(sd), cpu_map,
5525 + cpu_coregroup_mask(i));
5528 + cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
5531 +#ifdef CONFIG_SCHED_SMT
5533 + sd = &per_cpu(cpu_domains, i).sd;
5534 + SD_INIT(sd, SIBLING);
5535 + set_domain_attribute(sd, attr);
5536 + cpumask_and(sched_domain_span(sd),
5537 + topology_thread_cpumask(i), cpu_map);
5540 + cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
5544 +#ifdef CONFIG_SCHED_SMT
5545 + /* Set up CPU (sibling) groups */
5546 + for_each_cpu(i, cpu_map) {
5547 + cpumask_and(this_sibling_map,
5548 + topology_thread_cpumask(i), cpu_map);
5549 + if (i != cpumask_first(this_sibling_map))
5552 + init_sched_build_groups(this_sibling_map, cpu_map,
5553 + &cpu_to_cpu_group,
5554 + send_covered, tmpmask);
5558 +#ifdef CONFIG_SCHED_MC
5559 + /* Set up multi-core groups */
5560 + for_each_cpu(i, cpu_map) {
5561 + cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
5562 + if (i != cpumask_first(this_core_map))
5565 + init_sched_build_groups(this_core_map, cpu_map,
5566 + &cpu_to_core_group,
5567 + send_covered, tmpmask);
5571 + /* Set up physical groups */
5572 + for (i = 0; i < nr_node_ids; i++) {
5573 + cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
5574 + if (cpumask_empty(nodemask))
5577 + init_sched_build_groups(nodemask, cpu_map,
5578 + &cpu_to_phys_group,
5579 + send_covered, tmpmask);
5583 + /* Set up node groups */
5584 + if (sd_allnodes) {
5585 + init_sched_build_groups(cpu_map, cpu_map,
5586 + &cpu_to_allnodes_group,
5587 + send_covered, tmpmask);
5590 + for (i = 0; i < nr_node_ids; i++) {
5591 + /* Set up node groups */
5592 + struct sched_group *sg, *prev;
5595 + cpumask_clear(covered);
5596 + cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
5597 + if (cpumask_empty(nodemask)) {
5598 + sched_group_nodes[i] = NULL;
5602 + sched_domain_node_span(i, domainspan);
5603 + cpumask_and(domainspan, domainspan, cpu_map);
5605 + sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
5608 + printk(KERN_WARNING "Can not alloc domain group for "
5612 + sched_group_nodes[i] = sg;
5613 + for_each_cpu(j, nodemask) {
5614 + struct sched_domain *sd;
5616 + sd = &per_cpu(node_domains, j).sd;
5619 + sg->__cpu_power = 0;
5620 + cpumask_copy(sched_group_cpus(sg), nodemask);
5622 + cpumask_or(covered, covered, nodemask);
5625 + for (j = 0; j < nr_node_ids; j++) {
5626 + int n = (i + j) % nr_node_ids;
5628 + cpumask_complement(notcovered, covered);
5629 + cpumask_and(tmpmask, notcovered, cpu_map);
5630 + cpumask_and(tmpmask, tmpmask, domainspan);
5631 + if (cpumask_empty(tmpmask))
5634 + cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
5635 + if (cpumask_empty(tmpmask))
5638 + sg = kmalloc_node(sizeof(struct sched_group) +
5642 + printk(KERN_WARNING
5643 + "Can not alloc domain group for node %d\n", j);
5646 + sg->__cpu_power = 0;
5647 + cpumask_copy(sched_group_cpus(sg), tmpmask);
5648 + sg->next = prev->next;
5649 + cpumask_or(covered, covered, tmpmask);
5656 + /* Calculate CPU power for physical packages and nodes */
5657 +#ifdef CONFIG_SCHED_SMT
5658 + for_each_cpu(i, cpu_map) {
5659 + struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
5661 + init_sched_groups_power(i, sd);
5664 +#ifdef CONFIG_SCHED_MC
5665 + for_each_cpu(i, cpu_map) {
5666 + struct sched_domain *sd = &per_cpu(core_domains, i).sd;
5668 + init_sched_groups_power(i, sd);
5672 + for_each_cpu(i, cpu_map) {
5673 + struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
5675 + init_sched_groups_power(i, sd);
5679 + for (i = 0; i < nr_node_ids; i++)
5680 + init_numa_sched_groups_power(sched_group_nodes[i]);
5682 + if (sd_allnodes) {
5683 + struct sched_group *sg;
5685 + cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
5687 + init_numa_sched_groups_power(sg);
5691 + /* Attach the domains */
5692 + for_each_cpu(i, cpu_map) {
5693 + struct sched_domain *sd;
5694 +#ifdef CONFIG_SCHED_SMT
5695 + sd = &per_cpu(cpu_domains, i).sd;
5696 +#elif defined(CONFIG_SCHED_MC)
5697 + sd = &per_cpu(core_domains, i).sd;
5699 + sd = &per_cpu(phys_domains, i).sd;
5701 + cpu_attach_domain(sd, rd, i);
5707 + free_cpumask_var(tmpmask);
5709 + free_cpumask_var(send_covered);
5710 +free_this_core_map:
5711 + free_cpumask_var(this_core_map);
5712 +free_this_sibling_map:
5713 + free_cpumask_var(this_sibling_map);
5715 + free_cpumask_var(nodemask);
5718 + free_cpumask_var(notcovered);
5720 + free_cpumask_var(covered);
5722 + free_cpumask_var(domainspan);
5729 + kfree(sched_group_nodes);
5731 + goto free_tmpmask;
5735 + free_sched_groups(cpu_map, tmpmask);
5736 + free_rootdomain(rd);
5737 + goto free_tmpmask;
5741 +static int build_sched_domains(const struct cpumask *cpu_map)
5743 + return __build_sched_domains(cpu_map, NULL);
5746 +static struct cpumask *doms_cur; /* current sched domains */
5747 +static int ndoms_cur; /* number of sched domains in 'doms_cur' */
5748 +static struct sched_domain_attr *dattr_cur;
5749 + /* attribues of custom domains in 'doms_cur' */
5752 + * Special case: If a kmalloc of a doms_cur partition (array of
5753 + * cpumask) fails, then fallback to a single sched domain,
5754 + * as determined by the single cpumask fallback_doms.
5756 +static cpumask_var_t fallback_doms;
5759 + * arch_update_cpu_topology lets virtualized architectures update the
5760 + * cpu core maps. It is supposed to return 1 if the topology changed
5761 + * or 0 if it stayed the same.
5763 +int __attribute__((weak)) arch_update_cpu_topology(void)
5769 + * Set up scheduler domains and groups. Callers must hold the hotplug lock.
5770 + * For now this just excludes isolated cpus, but could be used to
5771 + * exclude other special cases in the future.
5773 +static int arch_init_sched_domains(const struct cpumask *cpu_map)
5777 + arch_update_cpu_topology();
5779 + doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
5781 + doms_cur = fallback_doms;
5782 + cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
5784 + err = build_sched_domains(doms_cur);
5785 + register_sched_domain_sysctl();
5790 +static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
5791 + struct cpumask *tmpmask)
5793 + free_sched_groups(cpu_map, tmpmask);
5797 + * Detach sched domains from a group of cpus specified in cpu_map
5798 + * These cpus will now be attached to the NULL domain
5800 +static void detach_destroy_domains(const struct cpumask *cpu_map)
5802 + /* Save because hotplug lock held. */
5803 + static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
5806 + for_each_cpu(i, cpu_map)
5807 + cpu_attach_domain(NULL, &def_root_domain, i);
5808 + synchronize_sched();
5809 + arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
5812 +/* handle null as "default" */
5813 +static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
5814 + struct sched_domain_attr *new, int idx_new)
5816 + struct sched_domain_attr tmp;
5822 + tmp = SD_ATTR_INIT;
5823 + return !memcmp(cur ? (cur + idx_cur) : &tmp,
5824 + new ? (new + idx_new) : &tmp,
5825 + sizeof(struct sched_domain_attr));
5829 + * Partition sched domains as specified by the 'ndoms_new'
5830 + * cpumasks in the array doms_new[] of cpumasks. This compares
5831 + * doms_new[] to the current sched domain partitioning, doms_cur[].
5832 + * It destroys each deleted domain and builds each new domain.
5834 + * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
5835 + * The masks don't intersect (don't overlap.) We should setup one
5836 + * sched domain for each mask. CPUs not in any of the cpumasks will
5837 + * not be load balanced. If the same cpumask appears both in the
5838 + * current 'doms_cur' domains and in the new 'doms_new', we can leave
5841 + * The passed in 'doms_new' should be kmalloc'd. This routine takes
5842 + * ownership of it and will kfree it when done with it. If the caller
5843 + * failed the kmalloc call, then it can pass in doms_new == NULL &&
5844 + * ndoms_new == 1, and partition_sched_domains() will fallback to
5845 + * the single partition 'fallback_doms', it also forces the domains
5848 + * If doms_new == NULL it will be replaced with cpu_online_mask.
5849 + * ndoms_new == 0 is a special case for destroying existing domains,
5850 + * and it will not create the default domain.
5852 + * Call with hotplug lock held
5854 +/* FIXME: Change to struct cpumask *doms_new[] */
5855 +void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
5856 + struct sched_domain_attr *dattr_new)
5861 + mutex_lock(&sched_domains_mutex);
5863 + /* always unregister in case we don't destroy any domains */
5864 + unregister_sched_domain_sysctl();
5866 + /* Let architecture update cpu core mappings. */
5867 + new_topology = arch_update_cpu_topology();
5869 + n = doms_new ? ndoms_new : 0;
5871 + /* Destroy deleted domains */
5872 + for (i = 0; i < ndoms_cur; i++) {
5873 + for (j = 0; j < n && !new_topology; j++) {
5874 + if (cpumask_equal(&doms_cur[i], &doms_new[j])
5875 + && dattrs_equal(dattr_cur, i, dattr_new, j))
5878 + /* no match - a current sched domain not in new doms_new[] */
5879 + detach_destroy_domains(doms_cur + i);
5884 + if (doms_new == NULL) {
5886 + doms_new = fallback_doms;
5887 + cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
5888 + WARN_ON_ONCE(dattr_new);
5891 + /* Build new domains */
5892 + for (i = 0; i < ndoms_new; i++) {
5893 + for (j = 0; j < ndoms_cur && !new_topology; j++) {
5894 + if (cpumask_equal(&doms_new[i], &doms_cur[j])
5895 + && dattrs_equal(dattr_new, i, dattr_cur, j))
5898 + /* no match - add a new doms_new */
5899 + __build_sched_domains(doms_new + i,
5900 + dattr_new ? dattr_new + i : NULL);
5905 + /* Remember the new sched domains */
5906 + if (doms_cur != fallback_doms)
5908 + kfree(dattr_cur); /* kfree(NULL) is safe */
5909 + doms_cur = doms_new;
5910 + dattr_cur = dattr_new;
5911 + ndoms_cur = ndoms_new;
5913 + register_sched_domain_sysctl();
5915 + mutex_unlock(&sched_domains_mutex);
5918 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
5919 +static void arch_reinit_sched_domains(void)
5921 + get_online_cpus();
5923 + /* Destroy domains first to force the rebuild */
5924 + partition_sched_domains(0, NULL, NULL);
5926 + rebuild_sched_domains();
5927 + put_online_cpus();
5930 +static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
5932 + unsigned int level = 0;
5934 + if (sscanf(buf, "%u", &level) != 1)
5938 + * level is always be positive so don't check for
5939 + * level < POWERSAVINGS_BALANCE_NONE which is 0
5940 + * What happens on 0 or 1 byte write,
5941 + * need to check for count as well?
5944 + if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
5948 + sched_smt_power_savings = level;
5950 + sched_mc_power_savings = level;
5952 + arch_reinit_sched_domains();
5957 +#ifdef CONFIG_SCHED_MC
5958 +static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
5961 + return sprintf(page, "%u\n", sched_mc_power_savings);
5963 +static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
5964 + const char *buf, size_t count)
5966 + return sched_power_savings_store(buf, count, 0);
5968 +static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
5969 + sched_mc_power_savings_show,
5970 + sched_mc_power_savings_store);
5973 +#ifdef CONFIG_SCHED_SMT
5974 +static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
5977 + return sprintf(page, "%u\n", sched_smt_power_savings);
5979 +static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
5980 + const char *buf, size_t count)
5982 + return sched_power_savings_store(buf, count, 1);
5984 +static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
5985 + sched_smt_power_savings_show,
5986 + sched_smt_power_savings_store);
5989 +int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
5993 +#ifdef CONFIG_SCHED_SMT
5994 + if (smt_capable())
5995 + err = sysfs_create_file(&cls->kset.kobj,
5996 + &attr_sched_smt_power_savings.attr);
5998 +#ifdef CONFIG_SCHED_MC
5999 + if (!err && mc_capable())
6000 + err = sysfs_create_file(&cls->kset.kobj,
6001 + &attr_sched_mc_power_savings.attr);
6005 +#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
6007 +#ifndef CONFIG_CPUSETS
6009 + * Add online and remove offline CPUs from the scheduler domains.
6010 + * When cpusets are enabled they take over this function.
6012 +static int update_sched_domains(struct notifier_block *nfb,
6013 + unsigned long action, void *hcpu)
6017 + case CPU_ONLINE_FROZEN:
6019 + case CPU_DEAD_FROZEN:
6020 + partition_sched_domains(1, NULL, NULL);
6024 + return NOTIFY_DONE;
6029 +static int update_runtime(struct notifier_block *nfb,
6030 + unsigned long action, void *hcpu)
6033 + case CPU_DOWN_PREPARE:
6034 + case CPU_DOWN_PREPARE_FROZEN:
6037 + case CPU_DOWN_FAILED:
6038 + case CPU_DOWN_FAILED_FROZEN:
6040 + case CPU_ONLINE_FROZEN:
6044 + return NOTIFY_DONE;
6048 +void __init sched_init_smp(void)
6050 + cpumask_var_t non_isolated_cpus;
6052 + alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
6054 +#if defined(CONFIG_NUMA)
6055 + sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
6057 + BUG_ON(sched_group_nodes_bycpu == NULL);
6059 + get_online_cpus();
6060 + mutex_lock(&sched_domains_mutex);
6061 + arch_init_sched_domains(cpu_online_mask);
6062 + cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
6063 + if (cpumask_empty(non_isolated_cpus))
6064 + cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
6065 + mutex_unlock(&sched_domains_mutex);
6066 + put_online_cpus();
6068 +#ifndef CONFIG_CPUSETS
6069 + /* XXX: Theoretical race here - CPU may be hotplugged now */
6070 + hotcpu_notifier(update_sched_domains, 0);
6073 + /* RT runtime code needs to handle some hotplug events */
6074 + hotcpu_notifier(update_runtime, 0);
6076 + /* Move init over to a non-isolated CPU */
6077 + if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
6079 + free_cpumask_var(non_isolated_cpus);
6081 + alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
6084 + * Assume that every added cpu gives us slightly less overall latency
6085 + * allowing us to increase the base rr_interval, but in a non linear
6088 + rr_interval *= 1 + ilog2(num_online_cpus());
6091 +void __init sched_init_smp(void)
6094 +#endif /* CONFIG_SMP */
6096 +unsigned int sysctl_timer_migration = 1;
6098 +int in_sched_functions(unsigned long addr)
6100 + return in_lock_functions(addr) ||
6101 + (addr >= (unsigned long)__sched_text_start
6102 + && addr < (unsigned long)__sched_text_end);
6105 +void __init sched_init(void)
6108 + int highest_cpu = 0;
6110 + prio_ratios[0] = 100;
6111 + for (i = 1 ; i < PRIO_RANGE ; i++)
6112 + prio_ratios[i] = prio_ratios[i - 1] * 11 / 10;
6115 + init_defrootdomain();
6116 + cpus_clear(grq.cpu_idle_map);
6118 + spin_lock_init(&grq.lock);
6119 + for_each_possible_cpu(i) {
6123 + INIT_LIST_HEAD(&rq->queue);
6124 + rq->rq_deadline = 0;
6127 + rq->user_pc = rq->nice_pc = rq->softirq_pc = rq->system_pc =
6128 + rq->iowait_pc = rq->idle_pc = 0;
6133 + INIT_LIST_HEAD(&rq->migration_queue);
6134 + rq_attach_root(rq, &def_root_domain);
6136 + atomic_set(&rq->nr_iowait, 0);
6139 + grq.iso_ticks = grq.nr_running = grq.nr_uninterruptible = 0;
6140 + for (i = 0; i < PRIO_LIMIT; i++)
6141 + INIT_LIST_HEAD(grq.queue + i);
6142 + bitmap_zero(grq.prio_bitmap, PRIO_LIMIT);
6143 + /* delimiter for bitsearch */
6144 + __set_bit(PRIO_LIMIT, grq.prio_bitmap);
6147 + nr_cpu_ids = highest_cpu + 1;
6150 +#ifdef CONFIG_PREEMPT_NOTIFIERS
6151 + INIT_HLIST_HEAD(&init_task.preempt_notifiers);
6154 +#ifdef CONFIG_RT_MUTEXES
6155 + plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
6159 + * The boot idle thread does lazy MMU switching as well:
6161 + atomic_inc(&init_mm.mm_count);
6162 + enter_lazy_tlb(&init_mm, current);
6165 + * Make us the idle thread. Technically, schedule() should not be
6166 + * called from this thread, however somewhere below it might be,
6167 + * but because we are the idle thread, we just pick up running again
6168 + * when this runqueue becomes "idle".
6170 + init_idle(current, smp_processor_id());
6172 + /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
6173 + alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
6175 +#ifdef CONFIG_NO_HZ
6176 + alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
6177 + alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
6179 + alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
6181 + perf_counter_init();
6184 +#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6185 +void __might_sleep(char *file, int line)
6188 + static unsigned long prev_jiffy; /* ratelimiting */
6190 + if ((in_atomic() || irqs_disabled()) &&
6191 + system_state == SYSTEM_RUNNING && !oops_in_progress) {
6192 + if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6194 + prev_jiffy = jiffies;
6195 + printk(KERN_ERR "BUG: sleeping function called from invalid"
6196 + " context at %s:%d\n", file, line);
6197 + printk("in_atomic():%d, irqs_disabled():%d\n",
6198 + in_atomic(), irqs_disabled());
6199 + debug_show_held_locks(current);
6200 + if (irqs_disabled())
6201 + print_irqtrace_events(current);
6206 +EXPORT_SYMBOL(__might_sleep);
6209 +#ifdef CONFIG_MAGIC_SYSRQ
6210 +void normalize_rt_tasks(void)
6212 + struct task_struct *g, *p;
6213 + unsigned long flags;
6217 + read_lock_irq(&tasklist_lock);
6219 + do_each_thread(g, p) {
6220 + if (!rt_task(p) && !iso_task(p))
6223 + spin_lock_irqsave(&p->pi_lock, flags);
6224 + rq = __task_grq_lock(p);
6225 + update_rq_clock(rq);
6227 + queued = task_queued_only(p);
6230 + __setscheduler(p, SCHED_NORMAL, 0);
6231 + if (task_running(p))
6238 + __task_grq_unlock();
6239 + spin_unlock_irqrestore(&p->pi_lock, flags);
6240 + } while_each_thread(g, p);
6242 + read_unlock_irq(&tasklist_lock);
6244 +#endif /* CONFIG_MAGIC_SYSRQ */
6248 + * These functions are only useful for the IA64 MCA handling.
6250 + * They can only be called when the whole system has been
6251 + * stopped - every CPU needs to be quiescent, and no scheduling
6252 + * activity can take place. Using them for anything else would
6253 + * be a serious bug, and as a result, they aren't even visible
6254 + * under any other configuration.
6258 + * curr_task - return the current task for a given cpu.
6259 + * @cpu: the processor in question.
6261 + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6263 +struct task_struct *curr_task(int cpu)
6265 + return cpu_curr(cpu);
6269 + * set_curr_task - set the current task for a given cpu.
6270 + * @cpu: the processor in question.
6271 + * @p: the task pointer to set.
6273 + * Description: This function must only be used when non-maskable interrupts
6274 + * are serviced on a separate stack. It allows the architecture to switch the
6275 + * notion of the current task on a cpu in a non-blocking manner. This function
6276 + * must be called with all CPU's synchronized, and interrupts disabled, the
6277 + * and caller must save the original value of the current task (see
6278 + * curr_task() above) and restore that value before reenabling interrupts and
6279 + * re-starting the system.
6281 + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6283 +void set_curr_task(int cpu, struct task_struct *p)
6285 + cpu_curr(cpu) = p;
6291 + * Use precise platform statistics if available:
6293 +#ifdef CONFIG_VIRT_CPU_ACCOUNTING
6294 +cputime_t task_utime(struct task_struct *p)
6299 +cputime_t task_stime(struct task_struct *p)
6304 +cputime_t task_utime(struct task_struct *p)
6306 + clock_t utime = cputime_to_clock_t(p->utime),
6307 + total = utime + cputime_to_clock_t(p->stime);
6310 + temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
6314 + do_div(temp, total);
6316 + utime = (clock_t)temp;
6318 + p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
6319 + return p->prev_utime;
6322 +cputime_t task_stime(struct task_struct *p)
6326 + stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
6327 + cputime_to_clock_t(task_utime(p));
6330 + p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
6332 + return p->prev_stime;
6336 +inline cputime_t task_gtime(struct task_struct *p)
6341 +void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6344 +#ifdef CONFIG_SCHED_DEBUG
6345 +void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
6348 +void proc_sched_set_task(struct task_struct *p)
6351 --- a/kernel/sysctl.c
6352 +++ b/kernel/sysctl.c
6353 @@ -86,6 +86,8 @@ extern int percpu_pagelist_fraction;
6354 extern int compat_log;
6355 extern int latencytop_enabled;
6356 extern int sysctl_nr_open_min, sysctl_nr_open_max;
6357 +extern int rr_interval;
6358 +extern int sched_iso_cpu;
6360 extern int sysctl_nr_trim_pages;
6362 @@ -103,7 +105,8 @@ static int zero;
6363 static int __maybe_unused one = 1;
6364 static int __maybe_unused two = 2;
6365 static unsigned long one_ul = 1;
6366 -static int one_hundred = 100;
6367 +static int __read_mostly one_hundred = 100;
6368 +static int __maybe_unused __read_mostly five_thousand = 5000;
6370 /* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
6371 static unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
6372 @@ -238,7 +241,7 @@ static struct ctl_table root_table[] = {
6376 -#ifdef CONFIG_SCHED_DEBUG
6377 +#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SCHED_CFS)
6378 static int min_sched_granularity_ns = 100000; /* 100 usecs */
6379 static int max_sched_granularity_ns = NSEC_PER_SEC; /* 1 second */
6380 static int min_wakeup_granularity_ns; /* 0 usecs */
6381 @@ -246,7 +249,7 @@ static int max_wakeup_granularity_ns = N
6384 static struct ctl_table kern_table[] = {
6385 -#ifdef CONFIG_SCHED_DEBUG
6386 +#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SCHED_CFS)
6388 .ctl_name = CTL_UNNUMBERED,
6389 .procname = "sched_min_granularity_ns",
6390 @@ -342,6 +345,7 @@ static struct ctl_table kern_table[] = {
6394 +#ifdef CONFIG_SCHED_CFS
6396 .ctl_name = CTL_UNNUMBERED,
6397 .procname = "sched_rt_period_us",
6398 @@ -366,6 +370,7 @@ static struct ctl_table kern_table[] = {
6400 .proc_handler = &proc_dointvec,
6403 #ifdef CONFIG_PROVE_LOCKING
6405 .ctl_name = CTL_UNNUMBERED,
6406 @@ -798,6 +803,30 @@ static struct ctl_table kern_table[] = {
6407 .proc_handler = &proc_dointvec,
6410 +#ifdef CONFIG_SCHED_BFS
6412 + .ctl_name = CTL_UNNUMBERED,
6413 + .procname = "rr_interval",
6414 + .data = &rr_interval,
6415 + .maxlen = sizeof (int),
6417 + .proc_handler = &proc_dointvec_minmax,
6418 + .strategy = &sysctl_intvec,
6420 + .extra2 = &five_thousand,
6423 + .ctl_name = CTL_UNNUMBERED,
6424 + .procname = "iso_cpu",
6425 + .data = &sched_iso_cpu,
6426 + .maxlen = sizeof (int),
6428 + .proc_handler = &proc_dointvec_minmax,
6429 + .strategy = &sysctl_intvec,
6431 + .extra2 = &one_hundred,
6434 #if defined(CONFIG_S390) && defined(CONFIG_SMP)
6436 .ctl_name = KERN_SPIN_RETRY,
6437 --- a/kernel/workqueue.c
6438 +++ b/kernel/workqueue.c
6439 @@ -317,7 +317,9 @@ static int worker_thread(void *__cwq)
6440 if (cwq->wq->freezeable)
6443 +#ifdef CONFIG_SCHED_CFS
6444 set_user_nice(current, -5);
6448 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);