1 diff -Nurb linux-2.6.27-590/arch/Kconfig linux-2.6.27-591/arch/Kconfig
2 --- linux-2.6.27-590/arch/Kconfig 2010-01-29 16:29:46.000000000 -0500
3 +++ linux-2.6.27-591/arch/Kconfig 2010-01-29 16:30:22.000000000 -0500
9 + bool "Chopstix (PlanetLab)"
10 + depends on MODULES && OPROFILE
12 + Chopstix allows you to monitor various events by summarizing them
13 + in lossy data structures and transferring these data structures
14 + into user space. If in doubt, say "N".
22 depends on KALLSYMS && MODULES
23 diff -Nurb linux-2.6.27-590/arch/x86/kernel/asm-offsets_32.c linux-2.6.27-591/arch/x86/kernel/asm-offsets_32.c
24 --- linux-2.6.27-590/arch/x86/kernel/asm-offsets_32.c 2008-10-09 18:13:53.000000000 -0400
25 +++ linux-2.6.27-591/arch/x86/kernel/asm-offsets_32.c 2010-01-29 16:45:48.000000000 -0500
27 #include <linux/signal.h>
28 #include <linux/personality.h>
29 #include <linux/suspend.h>
30 +#include <linux/arrays.h>
31 #include <linux/kbuild.h>
32 #include <asm/ucontext.h>
35 #include <linux/lguest.h>
36 #include "../../../drivers/lguest/lg.h"
39 +#define STACKOFFSET(sym, str, mem) \
40 + DEFINE(sym, offsetof(struct str, mem)-sizeof(struct str));
42 /* workaround for a warning with -Wmissing-prototypes */
47 + unsigned long dcookie;
49 + unsigned int number;
54 OFFSET(IA32_SIGCONTEXT_ax, sigcontext, ax);
56 OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);
59 + STACKOFFSET(TASK_thread, task_struct, thread);
60 + STACKOFFSET(THREAD_esp, thread_struct, sp);
61 + STACKOFFSET(EVENT_event_data, event, event_data);
62 + STACKOFFSET(EVENT_task, event, task);
63 + STACKOFFSET(EVENT_event_type, event, event_type);
64 + STACKOFFSET(SPEC_number, event_spec, number);
65 + DEFINE(EVENT_SIZE, sizeof(struct event));
66 + DEFINE(SPEC_SIZE, sizeof(struct event_spec));
67 + DEFINE(SPEC_EVENT_SIZE, sizeof(struct event_spec)+sizeof(struct event));
69 OFFSET(TI_task, thread_info, task);
70 OFFSET(TI_exec_domain, thread_info, exec_domain);
71 OFFSET(TI_flags, thread_info, flags);
72 diff -Nurb linux-2.6.27-590/arch/x86/kernel/entry_32.S linux-2.6.27-591/arch/x86/kernel/entry_32.S
73 --- linux-2.6.27-590/arch/x86/kernel/entry_32.S 2008-10-09 18:13:53.000000000 -0400
74 +++ linux-2.6.27-591/arch/x86/kernel/entry_32.S 2010-01-29 16:30:22.000000000 -0500
76 cmpl $(nr_syscalls), %eax
79 + /* Move Chopstix syscall probe here */
80 + /* Save and clobber: eax, ecx, ebp */
85 + subl $SPEC_EVENT_SIZE, %esp
86 + movl rec_event, %ecx
89 + # struct event is first, just below %ebp
90 + movl %eax, (SPEC_number-EVENT_SIZE)(%ebp)
91 + leal -SPEC_EVENT_SIZE(%ebp), %eax
92 + movl %eax, EVENT_event_data(%ebp)
93 + movl $6, EVENT_event_type(%ebp)
94 + movl rec_event, %edx
96 + leal -EVENT_SIZE(%ebp), %eax
100 + addl $SPEC_EVENT_SIZE, %esp
106 call *sys_call_table(,%eax,4)
107 movl %eax,PT_EAX(%esp) # store the return value
109 diff -Nurb linux-2.6.27-590/arch/x86/mm/fault.c linux-2.6.27-591/arch/x86/mm/fault.c
110 --- linux-2.6.27-590/arch/x86/mm/fault.c 2010-01-29 16:29:46.000000000 -0500
111 +++ linux-2.6.27-591/arch/x86/mm/fault.c 2010-01-29 16:30:22.000000000 -0500
117 +extern void (*rec_event)(void *,unsigned int);
120 + unsigned long dcookie;
122 + unsigned char reason;
127 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
128 diff -Nurb linux-2.6.27-590/drivers/oprofile/cpu_buffer.c linux-2.6.27-591/drivers/oprofile/cpu_buffer.c
129 --- linux-2.6.27-590/drivers/oprofile/cpu_buffer.c 2008-10-09 18:13:53.000000000 -0400
130 +++ linux-2.6.27-591/drivers/oprofile/cpu_buffer.c 2010-01-29 16:30:22.000000000 -0500
132 #include <linux/oprofile.h>
133 #include <linux/vmalloc.h>
134 #include <linux/errno.h>
135 +#include <linux/arrays.h>
137 #include "event_buffer.h"
138 #include "cpu_buffer.h"
143 +#ifdef CONFIG_CHOPSTIX
147 + unsigned long dcookie;
151 +extern void (*rec_event)(void *,unsigned int);
155 add_sample(struct oprofile_cpu_buffer * cpu_buf,
156 unsigned long pc, unsigned long event)
159 entry->event = event;
160 increment_head(cpu_buf);
167 int is_kernel = !user_mode(regs);
168 unsigned long pc = profile_pc(regs);
171 +#ifdef CONFIG_CHOPSTIX
174 + struct event_spec espec;
175 + esig.task = current;
178 + esig.event_data=&espec;
179 + esig.event_type=event; /* index in the event array currently set up */
180 + /* make sure the counters are loaded in the order we want them to show up*/
181 + (*rec_event)(&esig, 1);
184 oprofile_add_ext_sample(pc, regs, event, is_kernel);
187 + oprofile_add_ext_sample(pc, regs, event, is_kernel);
193 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
194 diff -Nurb linux-2.6.27-590/fs/bio.c linux-2.6.27-591/fs/bio.c
195 --- linux-2.6.27-590/fs/bio.c 2008-10-09 18:13:53.000000000 -0400
196 +++ linux-2.6.27-591/fs/bio.c 2010-01-29 16:30:22.000000000 -0500
198 #include <linux/workqueue.h>
199 #include <linux/blktrace_api.h>
200 #include <scsi/sg.h> /* for struct sg_iovec */
201 +#include <linux/arrays.h>
203 static struct kmem_cache *bio_slab __read_mostly;
211 * fs_bio_set is the bio_set containing bio and iovec memory pools used by
212 * IO code that does not need private memory pools.
213 @@ -1171,6 +1173,14 @@
219 + unsigned long dcookie;
221 + unsigned char reason;
224 +extern void (*rec_event)(void *,unsigned int);
226 * bio_endio - end I/O on a bio
228 @@ -1192,6 +1202,24 @@
229 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
232 +#ifdef CONFIG_CHOPSTIX
234 + struct event event;
235 + struct event_spec espec;
238 + espec.reason = 1;/*response */
240 + eip = bio->bi_end_io;
241 + event.event_data=&espec;
243 + event.event_type=3;
244 + /* index in the event array currently set up */
245 + /* make sure the counters are loaded in the order we want them to show up*/
246 + (*rec_event)(&event, bytes_done);
251 bio->bi_end_io(bio, error);
253 diff -Nurb linux-2.6.27-590/fs/exec.c linux-2.6.27-591/fs/exec.c
254 --- linux-2.6.27-590/fs/exec.c 2010-01-29 16:29:48.000000000 -0500
255 +++ linux-2.6.27-591/fs/exec.c 2010-01-29 16:45:48.000000000 -0500
257 #include <linux/fdtable.h>
258 #include <linux/mm.h>
259 #include <linux/stat.h>
260 +#include <linux/dcookies.h>
261 #include <linux/fcntl.h>
262 #include <linux/smp_lock.h>
263 #include <linux/swap.h>
268 + #ifdef CONFIG_CHOPSTIX
269 + unsigned long cookie;
270 + extern void (*rec_event)(void *, unsigned int);
271 + if (rec_event && !nd.dentry->d_cookie)
272 + get_dcookie(nd.dentry, nd.mnt, &cookie);
278 diff -Nurb linux-2.6.27-590/include/linux/arrays.h linux-2.6.27-591/include/linux/arrays.h
279 --- linux-2.6.27-590/include/linux/arrays.h 1969-12-31 19:00:00.000000000 -0500
280 +++ linux-2.6.27-591/include/linux/arrays.h 2010-01-29 16:30:22.000000000 -0500
282 +#ifndef __ARRAYS_H__
283 +#define __ARRAYS_H__
284 +#include <linux/list.h>
286 +#define SAMPLING_METHOD_DEFAULT 0
287 +#define SAMPLING_METHOD_LOG 1
289 +/* Every probe has an array handler */
291 +/* XXX - Optimize this structure */
293 +extern void (*rec_event)(void *,unsigned int);
294 +struct array_handler {
295 + struct list_head link;
296 + unsigned int (*hash_func)(void *);
297 + unsigned int (*sampling_func)(void *,int,void *);
298 + unsigned short size;
299 + unsigned int threshold;
300 + unsigned char **expcount;
301 + unsigned int sampling_method;
302 + unsigned int **arrays;
303 + unsigned int arraysize;
304 + unsigned int num_samples[2];
305 + void **epoch_samples; /* size-sized lists of samples */
306 + unsigned int (*serialize)(void *, void *);
307 + unsigned char code[5];
311 + struct list_head link;
313 + unsigned int count;
314 + unsigned int event_type;
315 + struct task_struct *task;
318 diff -Nurb linux-2.6.27-590/include/linux/sched.h.rej linux-2.6.27-591/include/linux/sched.h.rej
319 --- linux-2.6.27-590/include/linux/sched.h.rej 1969-12-31 19:00:00.000000000 -0500
320 +++ linux-2.6.27-591/include/linux/sched.h.rej 2010-01-29 16:30:22.000000000 -0500
325 + unsigned long sleep_avg;
326 + unsigned long long timestamp, last_ran;
327 + unsigned long long sched_time; /* sched_clock time spent running */
328 + enum sleep_type sleep_type;
332 + unsigned long sleep_avg;
333 + unsigned long long timestamp, last_ran;
334 ++ #ifdef CONFIG_CHOPSTIX
335 ++ unsigned long last_interrupted, last_ran_j;
338 + unsigned long long sched_time; /* sched_clock time spent running */
339 + enum sleep_type sleep_type;
341 diff -Nurb linux-2.6.27-590/kernel/sched.c linux-2.6.27-591/kernel/sched.c
342 --- linux-2.6.27-590/kernel/sched.c 2010-01-29 16:29:48.000000000 -0500
343 +++ linux-2.6.27-591/kernel/sched.c 2010-01-29 17:30:42.000000000 -0500
345 * 1998-11-19 Implemented schedule_timeout() and related stuff
346 * by Andrea Arcangeli
347 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
348 - * hybrid priority-list and round-robin design with
349 + * hybrid priority-list and round-robin deventn with
350 * an array-switch method of distributing timeslices
351 * and per-CPU runqueues. Cleanups and useful suggestions
352 * by Davide Libenzi, preemptible kernel bits by Robert Love.
355 #include "sched_cpupri.h"
357 +#define INTERRUPTIBLE -1
361 * Convert user-nice values [ -20 ... 0 ... 19 ]
362 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
363 @@ -4428,6 +4431,11 @@
367 +#ifdef CONFIG_CHOPSTIX
368 +void (*rec_event)(void *,unsigned int) = NULL;
369 +EXPORT_SYMBOL(rec_event);
373 * schedule() is the main scheduler function.
375 @@ -5369,6 +5377,7 @@
377 read_unlock(&tasklist_lock);
381 if ((current->euid != p->euid) && (current->euid != p->uid) &&
382 !capable(CAP_SYS_NICE))
383 diff -Nurb linux-2.6.27-590/kernel/sched.c.orig linux-2.6.27-591/kernel/sched.c.orig
384 --- linux-2.6.27-590/kernel/sched.c.orig 1969-12-31 19:00:00.000000000 -0500
385 +++ linux-2.6.27-591/kernel/sched.c.orig 2010-01-29 16:30:22.000000000 -0500
390 + * Kernel scheduler and related syscalls
392 + * Copyright (C) 1991-2002 Linus Torvalds
394 + * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
395 + * make semaphores SMP safe
396 + * 1998-11-19 Implemented schedule_timeout() and related stuff
397 + * by Andrea Arcangeli
398 + * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
399 + * hybrid priority-list and round-robin deventn with
400 + * an array-switch method of distributing timeslices
401 + * and per-CPU runqueues. Cleanups and useful suggestions
402 + * by Davide Libenzi, preemptible kernel bits by Robert Love.
403 + * 2003-09-03 Interactivity tuning by Con Kolivas.
404 + * 2004-04-02 Scheduler domains code by Nick Piggin
405 + * 2007-04-15 Work begun on replacing all interactivity tuning with a
406 + * fair scheduling design by Con Kolivas.
407 + * 2007-05-05 Load balancing (smp-nice) and other improvements
408 + * by Peter Williams
409 + * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
410 + * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
411 + * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
412 + * Thomas Gleixner, Mike Kravetz
415 +#include <linux/mm.h>
416 +#include <linux/module.h>
417 +#include <linux/nmi.h>
418 +#include <linux/init.h>
419 +#include <linux/uaccess.h>
420 +#include <linux/highmem.h>
421 +#include <linux/smp_lock.h>
422 +#include <asm/mmu_context.h>
423 +#include <linux/interrupt.h>
424 +#include <linux/capability.h>
425 +#include <linux/completion.h>
426 +#include <linux/kernel_stat.h>
427 +#include <linux/debug_locks.h>
428 +#include <linux/security.h>
429 +#include <linux/notifier.h>
430 +#include <linux/profile.h>
431 +#include <linux/freezer.h>
432 +#include <linux/vmalloc.h>
433 +#include <linux/blkdev.h>
434 +#include <linux/delay.h>
435 +#include <linux/pid_namespace.h>
436 +#include <linux/smp.h>
437 +#include <linux/threads.h>
438 +#include <linux/timer.h>
439 +#include <linux/rcupdate.h>
440 +#include <linux/cpu.h>
441 +#include <linux/cpuset.h>
442 +#include <linux/percpu.h>
443 +#include <linux/kthread.h>
444 +#include <linux/seq_file.h>
445 +#include <linux/sysctl.h>
446 +#include <linux/syscalls.h>
447 +#include <linux/times.h>
448 +#include <linux/tsacct_kern.h>
449 +#include <linux/kprobes.h>
450 +#include <linux/delayacct.h>
451 +#include <linux/reciprocal_div.h>
452 +#include <linux/unistd.h>
453 +#include <linux/pagemap.h>
454 +#include <linux/hrtimer.h>
455 +#include <linux/tick.h>
456 +#include <linux/bootmem.h>
457 +#include <linux/debugfs.h>
458 +#include <linux/ctype.h>
459 +#include <linux/ftrace.h>
460 +#include <linux/vs_sched.h>
461 +#include <linux/vs_cvirt.h>
463 +#include <asm/tlb.h>
464 +#include <asm/irq_regs.h>
466 +#include "sched_cpupri.h"
468 +#define INTERRUPTIBLE -1
472 + * Convert user-nice values [ -20 ... 0 ... 19 ]
473 + * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
476 +#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
477 +#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
478 +#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
481 + * 'User priority' is the nice value converted to something we
482 + * can work with better when scaling various scheduler parameters,
483 + * it's a [ 0 ... 39 ] range.
485 +#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
486 +#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
487 +#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
490 + * Helpers for converting nanosecond timing to jiffy resolution
492 +#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
494 +#define NICE_0_LOAD SCHED_LOAD_SCALE
495 +#define NICE_0_SHIFT SCHED_LOAD_SHIFT
498 + * These are the 'tuning knobs' of the scheduler:
500 + * default timeslice is 100 msecs (used only for SCHED_RR tasks).
501 + * Timeslices get refilled after they expire.
503 +#define DEF_TIMESLICE (100 * HZ / 1000)
506 + * single value that denotes runtime == period, ie unlimited time.
508 +#define RUNTIME_INF ((u64)~0ULL)
512 + * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
513 + * Since cpu_power is a 'constant', we can use a reciprocal divide.
515 +static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
517 + return reciprocal_divide(load, sg->reciprocal_cpu_power);
521 + * Each time a sched group cpu_power is changed,
522 + * we must compute its reciprocal value
524 +static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
526 + sg->__cpu_power += val;
527 + sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
531 +static inline int rt_policy(int policy)
533 + if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
538 +static inline int task_has_rt_policy(struct task_struct *p)
540 + return rt_policy(p->policy);
544 + * This is the priority-queue data structure of the RT scheduling class:
546 +struct rt_prio_array {
547 + DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
548 + struct list_head queue[MAX_RT_PRIO];
551 +struct rt_bandwidth {
552 + /* nests inside the rq lock: */
553 + spinlock_t rt_runtime_lock;
556 + struct hrtimer rt_period_timer;
559 +static struct rt_bandwidth def_rt_bandwidth;
561 +static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
563 +static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
565 + struct rt_bandwidth *rt_b =
566 + container_of(timer, struct rt_bandwidth, rt_period_timer);
572 + now = hrtimer_cb_get_time(timer);
573 + overrun = hrtimer_forward(timer, now, rt_b->rt_period);
578 + idle = do_sched_rt_period_timer(rt_b, overrun);
581 + return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
585 +void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
587 + rt_b->rt_period = ns_to_ktime(period);
588 + rt_b->rt_runtime = runtime;
590 + spin_lock_init(&rt_b->rt_runtime_lock);
592 + hrtimer_init(&rt_b->rt_period_timer,
593 + CLOCK_MONOTONIC, HRTIMER_MODE_REL);
594 + rt_b->rt_period_timer.function = sched_rt_period_timer;
595 + rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
598 +static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
602 + if (rt_b->rt_runtime == RUNTIME_INF)
605 + if (hrtimer_active(&rt_b->rt_period_timer))
608 + spin_lock(&rt_b->rt_runtime_lock);
610 + if (hrtimer_active(&rt_b->rt_period_timer))
613 + now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
614 + hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
615 + hrtimer_start(&rt_b->rt_period_timer,
616 + rt_b->rt_period_timer.expires,
619 + spin_unlock(&rt_b->rt_runtime_lock);
622 +#ifdef CONFIG_RT_GROUP_SCHED
623 +static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
625 + hrtimer_cancel(&rt_b->rt_period_timer);
630 + * sched_domains_mutex serializes calls to arch_init_sched_domains,
631 + * detach_destroy_domains and partition_sched_domains.
633 +static DEFINE_MUTEX(sched_domains_mutex);
635 +#ifdef CONFIG_GROUP_SCHED
637 +#include <linux/cgroup.h>
641 +static LIST_HEAD(task_groups);
643 +/* task group related information */
645 +#ifdef CONFIG_CGROUP_SCHED
646 + struct cgroup_subsys_state css;
649 +#ifdef CONFIG_FAIR_GROUP_SCHED
650 + /* schedulable entities of this group on each cpu */
651 + struct sched_entity **se;
652 + /* runqueue "owned" by this group on each cpu */
653 + struct cfs_rq **cfs_rq;
654 + unsigned long shares;
657 +#ifdef CONFIG_RT_GROUP_SCHED
658 + struct sched_rt_entity **rt_se;
659 + struct rt_rq **rt_rq;
661 + struct rt_bandwidth rt_bandwidth;
664 + struct rcu_head rcu;
665 + struct list_head list;
667 + struct task_group *parent;
668 + struct list_head siblings;
669 + struct list_head children;
672 +#ifdef CONFIG_USER_SCHED
676 + * Every UID task group (including init_task_group aka UID-0) will
677 + * be a child to this group.
679 +struct task_group root_task_group;
681 +#ifdef CONFIG_FAIR_GROUP_SCHED
682 +/* Default task group's sched entity on each cpu */
683 +static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
684 +/* Default task group's cfs_rq on each cpu */
685 +static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
686 +#endif /* CONFIG_FAIR_GROUP_SCHED */
688 +#ifdef CONFIG_RT_GROUP_SCHED
689 +static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
690 +static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
691 +#endif /* CONFIG_RT_GROUP_SCHED */
692 +#else /* !CONFIG_FAIR_GROUP_SCHED */
693 +#define root_task_group init_task_group
694 +#endif /* CONFIG_FAIR_GROUP_SCHED */
696 +/* task_group_lock serializes add/remove of task groups and also changes to
697 + * a task group's cpu shares.
699 +static DEFINE_SPINLOCK(task_group_lock);
701 +#ifdef CONFIG_FAIR_GROUP_SCHED
702 +#ifdef CONFIG_USER_SCHED
703 +# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
704 +#else /* !CONFIG_USER_SCHED */
705 +# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
706 +#endif /* CONFIG_USER_SCHED */
709 + * A weight of 0 or 1 can cause arithmetics problems.
710 + * A weight of a cfs_rq is the sum of weights of which entities
711 + * are queued on this cfs_rq, so a weight of a entity should not be
712 + * too large, so as the shares value of a task group.
713 + * (The default weight is 1024 - so there's no practical
714 + * limitation from this.)
716 +#define MIN_SHARES 2
717 +#define MAX_SHARES (1UL << 18)
719 +static int init_task_group_load = INIT_TASK_GROUP_LOAD;
722 +/* Default task group.
723 + * Every task in system belong to this group at bootup.
725 +struct task_group init_task_group;
727 +/* return group to which a task belongs */
728 +static inline struct task_group *task_group(struct task_struct *p)
730 + struct task_group *tg;
732 +#ifdef CONFIG_USER_SCHED
734 +#elif defined(CONFIG_CGROUP_SCHED)
735 + tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
736 + struct task_group, css);
738 + tg = &init_task_group;
743 +/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
744 +static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
746 +#ifdef CONFIG_FAIR_GROUP_SCHED
747 + p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
748 + p->se.parent = task_group(p)->se[cpu];
751 +#ifdef CONFIG_RT_GROUP_SCHED
752 + p->rt.rt_rq = task_group(p)->rt_rq[cpu];
753 + p->rt.parent = task_group(p)->rt_se[cpu];
759 +static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
760 +static inline struct task_group *task_group(struct task_struct *p)
765 +#endif /* CONFIG_GROUP_SCHED */
767 +/* CFS-related fields in a runqueue */
769 + struct load_weight load;
770 + unsigned long nr_running;
776 + struct rb_root tasks_timeline;
777 + struct rb_node *rb_leftmost;
779 + struct list_head tasks;
780 + struct list_head *balance_iterator;
783 + * 'curr' points to currently running entity on this cfs_rq.
784 + * It is set to NULL otherwise (i.e when none are currently running).
786 + struct sched_entity *curr, *next;
788 + unsigned long nr_spread_over;
790 +#ifdef CONFIG_FAIR_GROUP_SCHED
791 + struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
794 + * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
795 + * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
796 + * (like users, containers etc.)
798 + * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
799 + * list is used during load balance.
801 + struct list_head leaf_cfs_rq_list;
802 + struct task_group *tg; /* group that "owns" this runqueue */
806 + * the part of load.weight contributed by tasks
808 + unsigned long task_weight;
811 + * h_load = weight * f(tg)
813 + * Where f(tg) is the recursive weight fraction assigned to
816 + unsigned long h_load;
819 + * this cpu's part of tg->shares
821 + unsigned long shares;
824 + * load.weight at the time we set shares
826 + unsigned long rq_weight;
831 +/* Real-Time classes' related field in a runqueue: */
833 + struct rt_prio_array active;
834 + unsigned long rt_nr_running;
835 +#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
836 + int highest_prio; /* highest queued rt task prio */
839 + unsigned long rt_nr_migratory;
845 + /* Nests inside the rq lock: */
846 + spinlock_t rt_runtime_lock;
848 +#ifdef CONFIG_RT_GROUP_SCHED
849 + unsigned long rt_nr_boosted;
852 + struct list_head leaf_rt_rq_list;
853 + struct task_group *tg;
854 + struct sched_rt_entity *rt_se;
861 + * We add the notion of a root-domain which will be used to define per-domain
862 + * variables. Each exclusive cpuset essentially defines an island domain by
863 + * fully partitioning the member cpus from any other cpuset. Whenever a new
864 + * exclusive cpuset is created, we also create and attach a new root-domain
868 +struct root_domain {
874 + * The "RT overload" flag: it gets set if a CPU has more than
875 + * one runnable RT task.
877 + cpumask_t rto_mask;
878 + atomic_t rto_count;
880 + struct cpupri cpupri;
885 + * By default the system creates a single root-domain with all cpus as
886 + * members (mimicking the global state we have today).
888 +static struct root_domain def_root_domain;
891 + unsigned long norm_time;
892 + unsigned long idle_time;
893 +#ifdef CONFIG_VSERVER_IDLETIME
896 +#ifdef CONFIG_VSERVER_HARDCPU
897 + struct list_head hold_queue;
898 + unsigned long nr_onhold;
903 + * This is the main, per-CPU runqueue data structure.
905 + * Locking rule: those places that want to lock multiple runqueues
906 + * (such as the load balancing or the thread migration code), lock
907 + * acquire operations must be ordered by ascending &runqueue.
910 + /* runqueue lock: */
914 + * nr_running and cpu_load should be in the same cacheline because
915 + * remote CPUs use both these fields when doing load calculation.
917 + unsigned long nr_running;
918 + #define CPU_LOAD_IDX_MAX 5
919 + unsigned long cpu_load[CPU_LOAD_IDX_MAX];
920 + unsigned char idle_at_tick;
922 + unsigned long last_tick_seen;
923 + unsigned char in_nohz_recently;
925 + /* capture load from *all* tasks on this cpu: */
926 + struct load_weight load;
927 + unsigned long nr_load_updates;
933 +#ifdef CONFIG_FAIR_GROUP_SCHED
934 + /* list of leaf cfs_rq on this cpu: */
935 + struct list_head leaf_cfs_rq_list;
937 +#ifdef CONFIG_RT_GROUP_SCHED
938 + struct list_head leaf_rt_rq_list;
942 + * This is part of a global counter where only the total sum
943 + * over all CPUs matters. A task can increase this counter on
944 + * one CPU and if it got migrated afterwards it may decrease
945 + * it on another CPU. Always updated under the runqueue lock:
947 + unsigned long nr_uninterruptible;
949 + struct task_struct *curr, *idle;
950 + unsigned long next_balance;
951 + struct mm_struct *prev_mm;
955 + atomic_t nr_iowait;
958 + struct root_domain *rd;
959 + struct sched_domain *sd;
961 + /* For active balancing */
962 + int active_balance;
964 + /* cpu of this runqueue: */
968 + unsigned long avg_load_per_task;
970 + struct task_struct *migration_thread;
971 + struct list_head migration_queue;
974 +#ifdef CONFIG_SCHED_HRTICK
976 + int hrtick_csd_pending;
977 + struct call_single_data hrtick_csd;
979 + struct hrtimer hrtick_timer;
982 +#ifdef CONFIG_SCHEDSTATS
983 + /* latency stats */
984 + struct sched_info rq_sched_info;
986 + /* sys_sched_yield() stats */
987 + unsigned int yld_exp_empty;
988 + unsigned int yld_act_empty;
989 + unsigned int yld_both_empty;
990 + unsigned int yld_count;
992 + /* schedule() stats */
993 + unsigned int sched_switch;
994 + unsigned int sched_count;
995 + unsigned int sched_goidle;
997 + /* try_to_wake_up() stats */
998 + unsigned int ttwu_count;
999 + unsigned int ttwu_local;
1002 + unsigned int bkl_count;
1006 +static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1008 +static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
1010 + rq->curr->sched_class->check_preempt_curr(rq, p);
1013 +static inline int cpu_of(struct rq *rq)
1023 + * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1024 + * See detach_destroy_domains: synchronize_sched for details.
1026 + * The domain tree of any CPU may only be accessed from within
1027 + * preempt-disabled sections.
1029 +#define for_each_domain(cpu, __sd) \
1030 + for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
1032 +#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
1033 +#define this_rq() (&__get_cpu_var(runqueues))
1034 +#define task_rq(p) cpu_rq(task_cpu(p))
1035 +#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
1037 +static inline void update_rq_clock(struct rq *rq)
1039 + rq->clock = sched_clock_cpu(cpu_of(rq));
1043 + * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1045 +#ifdef CONFIG_SCHED_DEBUG
1046 +# define const_debug __read_mostly
1048 +# define const_debug static const
1052 + * runqueue_is_locked
1054 + * Returns true if the current cpu runqueue is locked.
1055 + * This interface allows printk to be called with the runqueue lock
1056 + * held and know whether or not it is OK to wake up the klogd.
1058 +int runqueue_is_locked(void)
1060 + int cpu = get_cpu();
1061 + struct rq *rq = cpu_rq(cpu);
1064 + ret = spin_is_locked(&rq->lock);
1070 + * Debugging: various feature bits
1073 +#define SCHED_FEAT(name, enabled) \
1074 + __SCHED_FEAT_##name ,
1077 +#include "sched_features.h"
1082 +#define SCHED_FEAT(name, enabled) \
1083 + (1UL << __SCHED_FEAT_##name) * enabled |
1085 +const_debug unsigned int sysctl_sched_features =
1086 +#include "sched_features.h"
1091 +#ifdef CONFIG_SCHED_DEBUG
1092 +#define SCHED_FEAT(name, enabled) \
1095 +static __read_mostly char *sched_feat_names[] = {
1096 +#include "sched_features.h"
1102 +static int sched_feat_open(struct inode *inode, struct file *filp)
1104 + filp->private_data = inode->i_private;
1109 +sched_feat_read(struct file *filp, char __user *ubuf,
1110 + size_t cnt, loff_t *ppos)
1117 + for (i = 0; sched_feat_names[i]; i++) {
1118 + len += strlen(sched_feat_names[i]);
1122 + buf = kmalloc(len + 2, GFP_KERNEL);
1126 + for (i = 0; sched_feat_names[i]; i++) {
1127 + if (sysctl_sched_features & (1UL << i))
1128 + r += sprintf(buf + r, "%s ", sched_feat_names[i]);
1130 + r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
1133 + r += sprintf(buf + r, "\n");
1134 + WARN_ON(r >= len + 2);
1136 + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1144 +sched_feat_write(struct file *filp, const char __user *ubuf,
1145 + size_t cnt, loff_t *ppos)
1155 + if (copy_from_user(&buf, ubuf, cnt))
1160 + if (strncmp(buf, "NO_", 3) == 0) {
1165 + for (i = 0; sched_feat_names[i]; i++) {
1166 + int len = strlen(sched_feat_names[i]);
1168 + if (strncmp(cmp, sched_feat_names[i], len) == 0) {
1170 + sysctl_sched_features &= ~(1UL << i);
1172 + sysctl_sched_features |= (1UL << i);
1177 + if (!sched_feat_names[i])
1180 + filp->f_pos += cnt;
1185 +static struct file_operations sched_feat_fops = {
1186 + .open = sched_feat_open,
1187 + .read = sched_feat_read,
1188 + .write = sched_feat_write,
1191 +static __init int sched_init_debug(void)
1193 + debugfs_create_file("sched_features", 0644, NULL, NULL,
1194 + &sched_feat_fops);
1198 +late_initcall(sched_init_debug);
1202 +#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1205 + * Number of tasks to iterate in a single balance run.
1206 + * Limited because this is done with IRQs disabled.
1208 +const_debug unsigned int sysctl_sched_nr_migrate = 32;
1211 + * ratelimit for updating the group shares.
1214 +unsigned int sysctl_sched_shares_ratelimit = 250000;
1217 + * period over which we measure -rt task cpu usage in us.
1220 +unsigned int sysctl_sched_rt_period = 1000000;
1222 +static __read_mostly int scheduler_running;
1225 + * part of the period that we allow rt tasks to run in us.
1228 +int sysctl_sched_rt_runtime = 950000;
1230 +static inline u64 global_rt_period(void)
1232 + return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1235 +static inline u64 global_rt_runtime(void)
1237 + if (sysctl_sched_rt_runtime < 0)
1238 + return RUNTIME_INF;
1240 + return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1243 +#ifndef prepare_arch_switch
1244 +# define prepare_arch_switch(next) do { } while (0)
1246 +#ifndef finish_arch_switch
1247 +# define finish_arch_switch(prev) do { } while (0)
1250 +static inline int task_current(struct rq *rq, struct task_struct *p)
1252 + return rq->curr == p;
1255 +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
1256 +static inline int task_running(struct rq *rq, struct task_struct *p)
1258 + return task_current(rq, p);
1261 +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1265 +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1267 +#ifdef CONFIG_DEBUG_SPINLOCK
1268 + /* this is a valid case when another task releases the spinlock */
1269 + rq->lock.owner = current;
1272 + * If we are tracking spinlock dependencies then we have to
1273 + * fix up the runqueue lock - which gets 'carried over' from
1274 + * prev into current:
1276 + spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1278 + spin_unlock_irq(&rq->lock);
1281 +#else /* __ARCH_WANT_UNLOCKED_CTXSW */
1282 +static inline int task_running(struct rq *rq, struct task_struct *p)
1287 + return task_current(rq, p);
1291 +static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1295 + * We can optimise this out completely for !SMP, because the
1296 + * SMP rebalancing from interrupt is the only thing that cares
1301 +#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1302 + spin_unlock_irq(&rq->lock);
1304 + spin_unlock(&rq->lock);
1308 +static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1312 + * After ->oncpu is cleared, the task can be moved to a different CPU.
1313 + * We must ensure this doesn't happen until the switch is completely
1319 +#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1320 + local_irq_enable();
1323 +#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
1326 + * __task_rq_lock - lock the runqueue a given task resides on.
1327 + * Must be called interrupts disabled.
1329 +static inline struct rq *__task_rq_lock(struct task_struct *p)
1330 + __acquires(rq->lock)
1333 + struct rq *rq = task_rq(p);
1334 + spin_lock(&rq->lock);
1335 + if (likely(rq == task_rq(p)))
1337 + spin_unlock(&rq->lock);
1342 + * task_rq_lock - lock the runqueue a given task resides on and disable
1343 + * interrupts. Note the ordering: we can safely lookup the task_rq without
1344 + * explicitly disabling preemption.
1346 +static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
1347 + __acquires(rq->lock)
1352 + local_irq_save(*flags);
1354 + spin_lock(&rq->lock);
1355 + if (likely(rq == task_rq(p)))
1357 + spin_unlock_irqrestore(&rq->lock, *flags);
1361 +static void __task_rq_unlock(struct rq *rq)
1362 + __releases(rq->lock)
1364 + spin_unlock(&rq->lock);
1367 +static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
1368 + __releases(rq->lock)
1370 + spin_unlock_irqrestore(&rq->lock, *flags);
1374 + * this_rq_lock - lock this runqueue and disable interrupts.
1376 +static struct rq *this_rq_lock(void)
1377 + __acquires(rq->lock)
1381 + local_irq_disable();
1383 + spin_lock(&rq->lock);
1388 +#ifdef CONFIG_SCHED_HRTICK
1390 + * Use HR-timers to deliver accurate preemption points.
1392 + * Its all a bit involved since we cannot program an hrt while holding the
1393 + * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1394 + * reschedule event.
1396 + * When we get rescheduled we reprogram the hrtick_timer outside of the
1401 + * Use hrtick when:
1402 + * - enabled by features
1403 + * - hrtimer is actually high res
1405 +static inline int hrtick_enabled(struct rq *rq)
1407 + if (!sched_feat(HRTICK))
1409 + if (!cpu_active(cpu_of(rq)))
1411 + return hrtimer_is_hres_active(&rq->hrtick_timer);
1414 +static void hrtick_clear(struct rq *rq)
1416 + if (hrtimer_active(&rq->hrtick_timer))
1417 + hrtimer_cancel(&rq->hrtick_timer);
1421 + * High-resolution timer tick.
1422 + * Runs from hardirq context with interrupts disabled.
1424 +static enum hrtimer_restart hrtick(struct hrtimer *timer)
1426 + struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1428 + WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1430 + spin_lock(&rq->lock);
1431 + update_rq_clock(rq);
1432 + rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1433 + spin_unlock(&rq->lock);
1435 + return HRTIMER_NORESTART;
1440 + * called from hardirq (IPI) context
1442 +static void __hrtick_start(void *arg)
1444 + struct rq *rq = arg;
1446 + spin_lock(&rq->lock);
1447 + hrtimer_restart(&rq->hrtick_timer);
1448 + rq->hrtick_csd_pending = 0;
1449 + spin_unlock(&rq->lock);
1453 + * Called to set the hrtick timer state.
1455 + * called with rq->lock held and irqs disabled
1457 +static void hrtick_start(struct rq *rq, u64 delay)
1459 + struct hrtimer *timer = &rq->hrtick_timer;
1460 + ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
1462 + timer->expires = time;
1464 + if (rq == this_rq()) {
1465 + hrtimer_restart(timer);
1466 + } else if (!rq->hrtick_csd_pending) {
1467 + __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
1468 + rq->hrtick_csd_pending = 1;
1473 +hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1475 + int cpu = (int)(long)hcpu;
1478 + case CPU_UP_CANCELED:
1479 + case CPU_UP_CANCELED_FROZEN:
1480 + case CPU_DOWN_PREPARE:
1481 + case CPU_DOWN_PREPARE_FROZEN:
1483 + case CPU_DEAD_FROZEN:
1484 + hrtick_clear(cpu_rq(cpu));
1488 + return NOTIFY_DONE;
1491 +static __init void init_hrtick(void)
1493 + hotcpu_notifier(hotplug_hrtick, 0);
1497 + * Called to set the hrtick timer state.
1499 + * called with rq->lock held and irqs disabled
1501 +static void hrtick_start(struct rq *rq, u64 delay)
1503 + hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1506 +static void init_hrtick(void)
1509 +#endif /* CONFIG_SMP */
1511 +static void init_rq_hrtick(struct rq *rq)
1514 + rq->hrtick_csd_pending = 0;
1516 + rq->hrtick_csd.flags = 0;
1517 + rq->hrtick_csd.func = __hrtick_start;
1518 + rq->hrtick_csd.info = rq;
1521 + hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1522 + rq->hrtick_timer.function = hrtick;
1523 + rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
1526 +static inline void hrtick_clear(struct rq *rq)
1530 +static inline void init_rq_hrtick(struct rq *rq)
1534 +static inline void init_hrtick(void)
1540 + * resched_task - mark a task 'to be rescheduled now'.
1542 + * On UP this means the setting of the need_resched flag, on SMP it
1543 + * might also involve a cross-CPU call to trigger the scheduler on
1548 +#ifndef tsk_is_polling
1549 +#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1552 +static void resched_task(struct task_struct *p)
1556 + assert_spin_locked(&task_rq(p)->lock);
1558 + if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
1561 + set_tsk_thread_flag(p, TIF_NEED_RESCHED);
1563 + cpu = task_cpu(p);
1564 + if (cpu == smp_processor_id())
1567 + /* NEED_RESCHED must be visible before we test polling */
1569 + if (!tsk_is_polling(p))
1570 + smp_send_reschedule(cpu);
1573 +static void resched_cpu(int cpu)
1575 + struct rq *rq = cpu_rq(cpu);
1576 + unsigned long flags;
1578 + if (!spin_trylock_irqsave(&rq->lock, flags))
1580 + resched_task(cpu_curr(cpu));
1581 + spin_unlock_irqrestore(&rq->lock, flags);
1584 +#ifdef CONFIG_NO_HZ
1586 + * When add_timer_on() enqueues a timer into the timer wheel of an
1587 + * idle CPU then this timer might expire before the next timer event
1588 + * which is scheduled to wake up that CPU. In case of a completely
1589 + * idle system the next event might even be infinite time into the
1590 + * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1591 + * leaves the inner idle loop so the newly added timer is taken into
1592 + * account when the CPU goes back to idle and evaluates the timer
1593 + * wheel for the next timer event.
1595 +void wake_up_idle_cpu(int cpu)
1597 + struct rq *rq = cpu_rq(cpu);
1599 + if (cpu == smp_processor_id())
1603 + * This is safe, as this function is called with the timer
1604 + * wheel base lock of (cpu) held. When the CPU is on the way
1605 + * to idle and has not yet set rq->curr to idle then it will
1606 + * be serialized on the timer wheel base lock and take the new
1607 + * timer into account automatically.
1609 + if (rq->curr != rq->idle)
1613 + * We can set TIF_RESCHED on the idle task of the other CPU
1614 + * lockless. The worst case is that the other CPU runs the
1615 + * idle task through an additional NOOP schedule()
1617 + set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1619 + /* NEED_RESCHED must be visible before we test polling */
1621 + if (!tsk_is_polling(rq->idle))
1622 + smp_send_reschedule(cpu);
1624 +#endif /* CONFIG_NO_HZ */
1626 +#else /* !CONFIG_SMP */
1627 +static void resched_task(struct task_struct *p)
1629 + assert_spin_locked(&task_rq(p)->lock);
1630 + set_tsk_need_resched(p);
1632 +#endif /* CONFIG_SMP */
1634 +#if BITS_PER_LONG == 32
1635 +# define WMULT_CONST (~0UL)
1637 +# define WMULT_CONST (1UL << 32)
1640 +#define WMULT_SHIFT 32
1643 + * Shift right and round:
1645 +#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
1648 + * delta *= weight / lw
1650 +static unsigned long
1651 +calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1652 + struct load_weight *lw)
1656 + if (!lw->inv_weight) {
1657 + if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1658 + lw->inv_weight = 1;
1660 + lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1664 + tmp = (u64)delta_exec * weight;
1666 + * Check whether we'd overflow the 64-bit multiplication:
1668 + if (unlikely(tmp > WMULT_CONST))
1669 + tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
1672 + tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
1674 + return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
1677 +static inline void update_load_add(struct load_weight *lw, unsigned long inc)
1679 + lw->weight += inc;
1680 + lw->inv_weight = 0;
1683 +static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
1685 + lw->weight -= dec;
1686 + lw->inv_weight = 0;
1690 + * To aid in avoiding the subversion of "niceness" due to uneven distribution
1691 + * of tasks with abnormal "nice" values across CPUs the contribution that
1692 + * each task makes to its run queue's load is weighted according to its
1693 + * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1694 + * scaled version of the new time slice allocation that they receive on time
1695 + * slice expiry etc.
1698 +#define WEIGHT_IDLEPRIO 2
1699 +#define WMULT_IDLEPRIO (1 << 31)
1702 + * Nice levels are multiplicative, with a gentle 10% change for every
1703 + * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1704 + * nice 1, it will get ~10% less CPU time than another CPU-bound task
1705 + * that remained on nice 0.
1707 + * The "10% effect" is relative and cumulative: from _any_ nice level,
1708 + * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1709 + * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1710 + * If a task goes up by ~10% and another task goes down by ~10% then
1711 + * the relative distance between them is ~25%.)
1713 +static const int prio_to_weight[40] = {
1714 + /* -20 */ 88761, 71755, 56483, 46273, 36291,
1715 + /* -15 */ 29154, 23254, 18705, 14949, 11916,
1716 + /* -10 */ 9548, 7620, 6100, 4904, 3906,
1717 + /* -5 */ 3121, 2501, 1991, 1586, 1277,
1718 + /* 0 */ 1024, 820, 655, 526, 423,
1719 + /* 5 */ 335, 272, 215, 172, 137,
1720 + /* 10 */ 110, 87, 70, 56, 45,
1721 + /* 15 */ 36, 29, 23, 18, 15,
1725 + * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1727 + * In cases where the weight does not change often, we can use the
1728 + * precalculated inverse to speed up arithmetics by turning divisions
1729 + * into multiplications:
1731 +static const u32 prio_to_wmult[40] = {
1732 + /* -20 */ 48388, 59856, 76040, 92818, 118348,
1733 + /* -15 */ 147320, 184698, 229616, 287308, 360437,
1734 + /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1735 + /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1736 + /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1737 + /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1738 + /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1739 + /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1742 +static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1745 + * runqueue iterator, to support SMP load-balancing between different
1746 + * scheduling classes, without having to expose their internal data
1747 + * structures to the load-balancing proper:
1749 +struct rq_iterator {
1751 + struct task_struct *(*start)(void *);
1752 + struct task_struct *(*next)(void *);
1756 +static unsigned long
1757 +balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1758 + unsigned long max_load_move, struct sched_domain *sd,
1759 + enum cpu_idle_type idle, int *all_pinned,
1760 + int *this_best_prio, struct rq_iterator *iterator);
1763 +iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1764 + struct sched_domain *sd, enum cpu_idle_type idle,
1765 + struct rq_iterator *iterator);
1768 +#ifdef CONFIG_CGROUP_CPUACCT
1769 +static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1771 +static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1774 +static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1776 + update_load_add(&rq->load, load);
1779 +static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1781 + update_load_sub(&rq->load, load);
1785 +static unsigned long source_load(int cpu, int type);
1786 +static unsigned long target_load(int cpu, int type);
1787 +static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1789 +static unsigned long cpu_avg_load_per_task(int cpu)
1791 + struct rq *rq = cpu_rq(cpu);
1793 + if (rq->nr_running)
1794 + rq->avg_load_per_task = rq->load.weight / rq->nr_running;
1796 + return rq->avg_load_per_task;
1799 +#ifdef CONFIG_FAIR_GROUP_SCHED
1801 +typedef void (*tg_visitor)(struct task_group *, int, struct sched_domain *);
1804 + * Iterate the full tree, calling @down when first entering a node and @up when
1805 + * leaving it for the final time.
1808 +walk_tg_tree(tg_visitor down, tg_visitor up, int cpu, struct sched_domain *sd)
1810 + struct task_group *parent, *child;
1813 + parent = &root_task_group;
1815 + (*down)(parent, cpu, sd);
1816 + list_for_each_entry_rcu(child, &parent->children, siblings) {
1823 + (*up)(parent, cpu, sd);
1826 + parent = parent->parent;
1829 + rcu_read_unlock();
1832 +static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1835 + * Calculate and set the cpu's group shares.
1838 +__update_group_shares_cpu(struct task_group *tg, int cpu,
1839 + unsigned long sd_shares, unsigned long sd_rq_weight)
1842 + unsigned long shares;
1843 + unsigned long rq_weight;
1848 + rq_weight = tg->cfs_rq[cpu]->load.weight;
1851 + * If there are currently no tasks on the cpu pretend there is one of
1852 + * average load so that when a new task gets to run here it will not
1853 + * get delayed by group starvation.
1857 + rq_weight = NICE_0_LOAD;
1860 + if (unlikely(rq_weight > sd_rq_weight))
1861 + rq_weight = sd_rq_weight;
1864 + * \Sum shares * rq_weight
1865 + * shares = -----------------------
1869 + shares = (sd_shares * rq_weight) / (sd_rq_weight + 1);
1872 + * record the actual number of shares, not the boosted amount.
1874 + tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1875 + tg->cfs_rq[cpu]->rq_weight = rq_weight;
1877 + if (shares < MIN_SHARES)
1878 + shares = MIN_SHARES;
1879 + else if (shares > MAX_SHARES)
1880 + shares = MAX_SHARES;
1882 + __set_se_shares(tg->se[cpu], shares);
1886 + * Re-compute the task group their per cpu shares over the given domain.
1887 + * This needs to be done in a bottom-up fashion because the rq weight of a
1888 + * parent group depends on the shares of its child groups.
1891 +tg_shares_up(struct task_group *tg, int cpu, struct sched_domain *sd)
1893 + unsigned long rq_weight = 0;
1894 + unsigned long shares = 0;
1897 + for_each_cpu_mask(i, sd->span) {
1898 + rq_weight += tg->cfs_rq[i]->load.weight;
1899 + shares += tg->cfs_rq[i]->shares;
1902 + if ((!shares && rq_weight) || shares > tg->shares)
1903 + shares = tg->shares;
1905 + if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1906 + shares = tg->shares;
1909 + rq_weight = cpus_weight(sd->span) * NICE_0_LOAD;
1911 + for_each_cpu_mask(i, sd->span) {
1912 + struct rq *rq = cpu_rq(i);
1913 + unsigned long flags;
1915 + spin_lock_irqsave(&rq->lock, flags);
1916 + __update_group_shares_cpu(tg, i, shares, rq_weight);
1917 + spin_unlock_irqrestore(&rq->lock, flags);
1922 + * Compute the cpu's hierarchical load factor for each task group.
1923 + * This needs to be done in a top-down fashion because the load of a child
1924 + * group is a fraction of its parents load.
1927 +tg_load_down(struct task_group *tg, int cpu, struct sched_domain *sd)
1929 + unsigned long load;
1931 + if (!tg->parent) {
1932 + load = cpu_rq(cpu)->load.weight;
1934 + load = tg->parent->cfs_rq[cpu]->h_load;
1935 + load *= tg->cfs_rq[cpu]->shares;
1936 + load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1939 + tg->cfs_rq[cpu]->h_load = load;
1943 +tg_nop(struct task_group *tg, int cpu, struct sched_domain *sd)
1947 +static void update_shares(struct sched_domain *sd)
1949 + u64 now = cpu_clock(raw_smp_processor_id());
1950 + s64 elapsed = now - sd->last_update;
1952 + if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1953 + sd->last_update = now;
1954 + walk_tg_tree(tg_nop, tg_shares_up, 0, sd);
1958 +static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1960 + spin_unlock(&rq->lock);
1961 + update_shares(sd);
1962 + spin_lock(&rq->lock);
1965 +static void update_h_load(int cpu)
1967 + walk_tg_tree(tg_load_down, tg_nop, cpu, NULL);
1972 +static inline void update_shares(struct sched_domain *sd)
1976 +static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1984 +#ifdef CONFIG_FAIR_GROUP_SCHED
1985 +static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1988 + cfs_rq->shares = shares;
1993 +#include "sched_stats.h"
1994 +#include "sched_idletask.c"
1995 +#include "sched_fair.c"
1996 +#include "sched_rt.c"
1997 +#ifdef CONFIG_SCHED_DEBUG
1998 +# include "sched_debug.c"
2001 +#define sched_class_highest (&rt_sched_class)
2002 +#define for_each_class(class) \
2003 + for (class = sched_class_highest; class; class = class->next)
2005 +static void inc_nr_running(struct rq *rq)
2010 +static void dec_nr_running(struct rq *rq)
2015 +static void set_load_weight(struct task_struct *p)
2017 + if (task_has_rt_policy(p)) {
2018 + p->se.load.weight = prio_to_weight[0] * 2;
2019 + p->se.load.inv_weight = prio_to_wmult[0] >> 1;
2024 + * SCHED_IDLE tasks get minimal weight:
2026 + if (p->policy == SCHED_IDLE) {
2027 + p->se.load.weight = WEIGHT_IDLEPRIO;
2028 + p->se.load.inv_weight = WMULT_IDLEPRIO;
2032 + p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
2033 + p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
2036 +static void update_avg(u64 *avg, u64 sample)
2038 + s64 diff = sample - *avg;
2039 + *avg += diff >> 3;
2042 +static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
2044 + // BUG_ON(p->state & TASK_ONHOLD);
2045 + sched_info_queued(p);
2046 + p->sched_class->enqueue_task(rq, p, wakeup);
2050 +static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
2052 + if (sleep && p->se.last_wakeup) {
2053 + update_avg(&p->se.avg_overlap,
2054 + p->se.sum_exec_runtime - p->se.last_wakeup);
2055 + p->se.last_wakeup = 0;
2058 + sched_info_dequeued(p);
2059 + p->sched_class->dequeue_task(rq, p, sleep);
2064 + * __normal_prio - return the priority that is based on the static prio
2066 +static inline int __normal_prio(struct task_struct *p)
2068 + return p->static_prio;
2072 + * Calculate the expected normal priority: i.e. priority
2073 + * without taking RT-inheritance into account. Might be
2074 + * boosted by interactivity modifiers. Changes upon fork,
2075 + * setprio syscalls, and whenever the interactivity
2076 + * estimator recalculates.
2078 +static inline int normal_prio(struct task_struct *p)
2082 + if (task_has_rt_policy(p))
2083 + prio = MAX_RT_PRIO-1 - p->rt_priority;
2085 + prio = __normal_prio(p);
2090 + * Calculate the current priority, i.e. the priority
2091 + * taken into account by the scheduler. This value might
2092 + * be boosted by RT tasks, or might be boosted by
2093 + * interactivity modifiers. Will be RT if the task got
2094 + * RT-boosted. If not then it returns p->normal_prio.
2096 +static int effective_prio(struct task_struct *p)
2098 + p->normal_prio = normal_prio(p);
2100 + * If we are RT tasks or we were boosted to RT priority,
2101 + * keep the priority unchanged. Otherwise, update priority
2102 + * to the normal priority:
2104 + if (!rt_prio(p->prio))
2105 + return p->normal_prio;
2110 + * activate_task - move a task to the runqueue.
2112 +static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
2114 + if (task_contributes_to_load(p))
2115 + rq->nr_uninterruptible--;
2117 + enqueue_task(rq, p, wakeup);
2118 + inc_nr_running(rq);
2122 + * deactivate_task - remove a task from the runqueue.
2124 +static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
2126 + if (task_contributes_to_load(p))
2127 + rq->nr_uninterruptible++;
2129 + dequeue_task(rq, p, sleep);
2130 + dec_nr_running(rq);
2134 + * task_curr - is this task currently executing on a CPU?
2135 + * @p: the task in question.
2137 +inline int task_curr(const struct task_struct *p)
2139 + return cpu_curr(task_cpu(p)) == p;
2142 +static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2144 + set_task_rq(p, cpu);
2147 + * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2148 + * successfuly executed on another CPU. We must ensure that updates of
2149 + * per-task data have been completed by this moment.
2152 + task_thread_info(p)->cpu = cpu;
2156 +static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2157 + const struct sched_class *prev_class,
2158 + int oldprio, int running)
2160 + if (prev_class != p->sched_class) {
2161 + if (prev_class->switched_from)
2162 + prev_class->switched_from(rq, p, running);
2163 + p->sched_class->switched_to(rq, p, running);
2165 + p->sched_class->prio_changed(rq, p, oldprio, running);
2170 +/* Used instead of source_load when we know the type == 0 */
2171 +static unsigned long weighted_cpuload(const int cpu)
2173 + return cpu_rq(cpu)->load.weight;
2177 + * Is this task likely cache-hot:
2180 +task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2185 + * Buddy candidates are cache hot:
2187 + if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
2190 + if (p->sched_class != &fair_sched_class)
2193 + if (sysctl_sched_migration_cost == -1)
2195 + if (sysctl_sched_migration_cost == 0)
2198 + delta = now - p->se.exec_start;
2200 + return delta < (s64)sysctl_sched_migration_cost;
2204 +void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2206 + int old_cpu = task_cpu(p);
2207 + struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
2208 + struct cfs_rq *old_cfsrq = task_cfs_rq(p),
2209 + *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
2212 + clock_offset = old_rq->clock - new_rq->clock;
2214 +#ifdef CONFIG_SCHEDSTATS
2215 + if (p->se.wait_start)
2216 + p->se.wait_start -= clock_offset;
2217 + if (p->se.sleep_start)
2218 + p->se.sleep_start -= clock_offset;
2219 + if (p->se.block_start)
2220 + p->se.block_start -= clock_offset;
2221 + if (old_cpu != new_cpu) {
2222 + schedstat_inc(p, se.nr_migrations);
2223 + if (task_hot(p, old_rq->clock, NULL))
2224 + schedstat_inc(p, se.nr_forced2_migrations);
2227 + p->se.vruntime -= old_cfsrq->min_vruntime -
2228 + new_cfsrq->min_vruntime;
2230 + __set_task_cpu(p, new_cpu);
2233 +struct migration_req {
2234 + struct list_head list;
2236 + struct task_struct *task;
2239 + struct completion done;
2242 +#include "sched_mon.h"
2246 + * The task's runqueue lock must be held.
2247 + * Returns true if you have to wait for migration thread.
2250 +migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2252 + struct rq *rq = task_rq(p);
2254 + vxm_migrate_task(p, rq, dest_cpu);
2256 + * If the task is not on a runqueue (and not running), then
2257 + * it is sufficient to simply update the task's cpu field.
2259 + if (!p->se.on_rq && !task_running(rq, p)) {
2260 + set_task_cpu(p, dest_cpu);
2264 + init_completion(&req->done);
2266 + req->dest_cpu = dest_cpu;
2267 + list_add(&req->list, &rq->migration_queue);
2273 + * wait_task_inactive - wait for a thread to unschedule.
2275 + * If @match_state is nonzero, it's the @p->state value just checked and
2276 + * not expected to change. If it changes, i.e. @p might have woken up,
2277 + * then return zero. When we succeed in waiting for @p to be off its CPU,
2278 + * we return a positive number (its total switch count). If a second call
2279 + * a short while later returns the same number, the caller can be sure that
2280 + * @p has remained unscheduled the whole time.
2282 + * The caller must ensure that the task *will* unschedule sometime soon,
2283 + * else this function might spin for a *long* time. This function can't
2284 + * be called with interrupts off, or it may introduce deadlock with
2285 + * smp_call_function() if an IPI is sent by the same process we are
2286 + * waiting to become inactive.
2288 +unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2290 + unsigned long flags;
2291 + int running, on_rq;
2292 + unsigned long ncsw;
2297 + * We do the initial early heuristics without holding
2298 + * any task-queue locks at all. We'll only try to get
2299 + * the runqueue lock when things look like they will
2305 + * If the task is actively running on another CPU
2306 + * still, just relax and busy-wait without holding
2309 + * NOTE! Since we don't hold any locks, it's not
2310 + * even sure that "rq" stays as the right runqueue!
2311 + * But we don't care, since "task_running()" will
2312 + * return false if the runqueue has changed and p
2313 + * is actually now running somewhere else!
2315 + while (task_running(rq, p)) {
2316 + if (match_state && unlikely(p->state != match_state))
2322 + * Ok, time to look more closely! We need the rq
2323 + * lock now, to be *sure*. If we're wrong, we'll
2324 + * just go back and repeat.
2326 + rq = task_rq_lock(p, &flags);
2327 + running = task_running(rq, p);
2328 + on_rq = p->se.on_rq;
2330 + if (!match_state || p->state == match_state) {
2331 + ncsw = p->nivcsw + p->nvcsw;
2332 + if (unlikely(!ncsw))
2335 + task_rq_unlock(rq, &flags);
2338 + * If it changed from the expected state, bail out now.
2340 + if (unlikely(!ncsw))
2344 + * Was it really running after all now that we
2345 + * checked with the proper locks actually held?
2347 + * Oops. Go back and try again..
2349 + if (unlikely(running)) {
2355 + * It's not enough that it's not actively running,
2356 + * it must be off the runqueue _entirely_, and not
2359 + * So if it wa still runnable (but just not actively
2360 + * running right now), it's preempted, and we should
2361 + * yield - it could be a while.
2363 + if (unlikely(on_rq)) {
2364 + schedule_timeout_uninterruptible(1);
2369 + * Ahh, all good. It wasn't running, and it wasn't
2370 + * runnable, which means that it will never become
2371 + * running in the future either. We're all done!
2380 + * kick_process - kick a running thread to enter/exit the kernel
2381 + * @p: the to-be-kicked thread
2383 + * Cause a process which is running on another CPU to enter
2384 + * kernel-mode, without any delay. (to get signals handled.)
2386 + * NOTE: this function doesnt have to take the runqueue lock,
2387 + * because all it wants to ensure is that the remote task enters
2388 + * the kernel. If the IPI races and the task has been migrated
2389 + * to another CPU then no harm is done and the purpose has been
2390 + * achieved as well.
2392 +void kick_process(struct task_struct *p)
2396 + preempt_disable();
2397 + cpu = task_cpu(p);
2398 + if ((cpu != smp_processor_id()) && task_curr(p))
2399 + smp_send_reschedule(cpu);
2404 + * Return a low guess at the load of a migration-source cpu weighted
2405 + * according to the scheduling class and "nice" value.
2407 + * We want to under-estimate the load of migration sources, to
2408 + * balance conservatively.
2410 +static unsigned long source_load(int cpu, int type)
2412 + struct rq *rq = cpu_rq(cpu);
2413 + unsigned long total = weighted_cpuload(cpu);
2415 + if (type == 0 || !sched_feat(LB_BIAS))
2418 + return min(rq->cpu_load[type-1], total);
2422 + * Return a high guess at the load of a migration-target cpu weighted
2423 + * according to the scheduling class and "nice" value.
2425 +static unsigned long target_load(int cpu, int type)
2427 + struct rq *rq = cpu_rq(cpu);
2428 + unsigned long total = weighted_cpuload(cpu);
2430 + if (type == 0 || !sched_feat(LB_BIAS))
2433 + return max(rq->cpu_load[type-1], total);
2437 + * find_idlest_group finds and returns the least busy CPU group within the
2440 +static struct sched_group *
2441 +find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2443 + struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2444 + unsigned long min_load = ULONG_MAX, this_load = 0;
2445 + int load_idx = sd->forkexec_idx;
2446 + int imbalance = 100 + (sd->imbalance_pct-100)/2;
2449 + unsigned long load, avg_load;
2453 + /* Skip over this group if it has no CPUs allowed */
2454 + if (!cpus_intersects(group->cpumask, p->cpus_allowed))
2457 + local_group = cpu_isset(this_cpu, group->cpumask);
2459 + /* Tally up the load of all CPUs in the group */
2462 + for_each_cpu_mask_nr(i, group->cpumask) {
2463 + /* Bias balancing toward cpus of our domain */
2465 + load = source_load(i, load_idx);
2467 + load = target_load(i, load_idx);
2472 + /* Adjust by relative CPU power of the group */
2473 + avg_load = sg_div_cpu_power(group,
2474 + avg_load * SCHED_LOAD_SCALE);
2476 + if (local_group) {
2477 + this_load = avg_load;
2479 + } else if (avg_load < min_load) {
2480 + min_load = avg_load;
2483 + } while (group = group->next, group != sd->groups);
2485 + if (!idlest || 100*this_load < imbalance*min_load)
2491 + * find_idlest_cpu - find the idlest cpu among the cpus in group.
2494 +find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2497 + unsigned long load, min_load = ULONG_MAX;
2501 + /* Traverse only the allowed CPUs */
2502 + cpus_and(*tmp, group->cpumask, p->cpus_allowed);
2504 + for_each_cpu_mask_nr(i, *tmp) {
2505 + load = weighted_cpuload(i);
2507 + if (load < min_load || (load == min_load && i == this_cpu)) {
2517 + * sched_balance_self: balance the current task (running on cpu) in domains
2518 + * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2519 + * SD_BALANCE_EXEC.
2521 + * Balance, ie. select the least loaded group.
2523 + * Returns the target CPU number, or the same CPU if no balancing is needed.
2525 + * preempt must be disabled.
2527 +static int sched_balance_self(int cpu, int flag)
2529 + struct task_struct *t = current;
2530 + struct sched_domain *tmp, *sd = NULL;
2532 + for_each_domain(cpu, tmp) {
2534 + * If power savings logic is enabled for a domain, stop there.
2536 + if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2538 + if (tmp->flags & flag)
2543 + update_shares(sd);
2546 + cpumask_t span, tmpmask;
2547 + struct sched_group *group;
2548 + int new_cpu, weight;
2550 + if (!(sd->flags & flag)) {
2556 + group = find_idlest_group(sd, t, cpu);
2562 + new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
2563 + if (new_cpu == -1 || new_cpu == cpu) {
2564 + /* Now try balancing at a lower domain level of cpu */
2569 + /* Now try balancing at a lower domain level of new_cpu */
2572 + weight = cpus_weight(span);
2573 + for_each_domain(cpu, tmp) {
2574 + if (weight <= cpus_weight(tmp->span))
2576 + if (tmp->flags & flag)
2579 + /* while loop will break here if sd == NULL */
2585 +#endif /* CONFIG_SMP */
2588 + * try_to_wake_up - wake up a thread
2589 + * @p: the to-be-woken-up thread
2590 + * @state: the mask of task states that can be woken
2591 + * @sync: do a synchronous wakeup?
2593 + * Put it on the run-queue if it's not already there. The "current"
2594 + * thread is always on the run-queue (except when the actual
2595 + * re-schedule is in progress), and as such you're allowed to do
2596 + * the simpler "current->state = TASK_RUNNING" to mark yourself
2597 + * runnable without the overhead of this.
2599 + * returns failure only if the task is already active.
2601 +static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
2603 + int cpu, orig_cpu, this_cpu, success = 0;
2604 + unsigned long flags;
2608 + if (!sched_feat(SYNC_WAKEUPS))
2612 + if (sched_feat(LB_WAKEUP_UPDATE)) {
2613 + struct sched_domain *sd;
2615 + this_cpu = raw_smp_processor_id();
2616 + cpu = task_cpu(p);
2618 + for_each_domain(this_cpu, sd) {
2619 + if (cpu_isset(cpu, sd->span)) {
2620 + update_shares(sd);
2628 + rq = task_rq_lock(p, &flags);
2629 + old_state = p->state;
2630 + if (!(old_state & state))
2636 + cpu = task_cpu(p);
2638 + this_cpu = smp_processor_id();
2641 + if (unlikely(task_running(rq, p)))
2642 + goto out_activate;
2644 + cpu = p->sched_class->select_task_rq(p, sync);
2645 + if (cpu != orig_cpu) {
2646 + set_task_cpu(p, cpu);
2647 + task_rq_unlock(rq, &flags);
2648 + /* might preempt at this point */
2649 + rq = task_rq_lock(p, &flags);
2650 + old_state = p->state;
2652 + /* we need to unhold suspended tasks
2653 + if (old_state & TASK_ONHOLD) {
2654 + vx_unhold_task(p, rq);
2655 + old_state = p->state;
2657 + if (!(old_state & state))
2662 + this_cpu = smp_processor_id();
2663 + cpu = task_cpu(p);
2666 +#ifdef CONFIG_SCHEDSTATS
2667 + schedstat_inc(rq, ttwu_count);
2668 + if (cpu == this_cpu)
2669 + schedstat_inc(rq, ttwu_local);
2671 + struct sched_domain *sd;
2672 + for_each_domain(this_cpu, sd) {
2673 + if (cpu_isset(cpu, sd->span)) {
2674 + schedstat_inc(sd, ttwu_wake_remote);
2679 +#endif /* CONFIG_SCHEDSTATS */
2682 +#endif /* CONFIG_SMP */
2683 + schedstat_inc(p, se.nr_wakeups);
2685 + schedstat_inc(p, se.nr_wakeups_sync);
2686 + if (orig_cpu != cpu)
2687 + schedstat_inc(p, se.nr_wakeups_migrate);
2688 + if (cpu == this_cpu)
2689 + schedstat_inc(p, se.nr_wakeups_local);
2691 + schedstat_inc(p, se.nr_wakeups_remote);
2692 + update_rq_clock(rq);
2693 + activate_task(rq, p, 1);
2697 + trace_mark(kernel_sched_wakeup,
2698 + "pid %d state %ld ## rq %p task %p rq->curr %p",
2699 + p->pid, p->state, rq, p, rq->curr);
2700 + check_preempt_curr(rq, p);
2702 + p->state = TASK_RUNNING;
2704 + if (p->sched_class->task_wake_up)
2705 + p->sched_class->task_wake_up(rq, p);
2708 + current->se.last_wakeup = current->se.sum_exec_runtime;
2710 + task_rq_unlock(rq, &flags);
2715 +int wake_up_process(struct task_struct *p)
2717 + return try_to_wake_up(p, TASK_ALL, 0);
2719 +EXPORT_SYMBOL(wake_up_process);
2721 +int wake_up_state(struct task_struct *p, unsigned int state)
2723 + return try_to_wake_up(p, state, 0);
2727 + * Perform scheduler related setup for a newly forked process p.
2728 + * p is forked by current.
2730 + * __sched_fork() is basic setup used by init_idle() too:
2732 +static void __sched_fork(struct task_struct *p)
2734 + p->se.exec_start = 0;
2735 + p->se.sum_exec_runtime = 0;
2736 + p->se.prev_sum_exec_runtime = 0;
2737 + p->se.last_wakeup = 0;
2738 + p->se.avg_overlap = 0;
2740 +#ifdef CONFIG_SCHEDSTATS
2741 + p->se.wait_start = 0;
2742 + p->se.sum_sleep_runtime = 0;
2743 + p->se.sleep_start = 0;
2744 + p->se.block_start = 0;
2745 + p->se.sleep_max = 0;
2746 + p->se.block_max = 0;
2747 + p->se.exec_max = 0;
2748 + p->se.slice_max = 0;
2749 + p->se.wait_max = 0;
2752 + INIT_LIST_HEAD(&p->rt.run_list);
2754 + INIT_LIST_HEAD(&p->se.group_node);
2756 +#ifdef CONFIG_PREEMPT_NOTIFIERS
2757 + INIT_HLIST_HEAD(&p->preempt_notifiers);
2761 + * We mark the process as running here, but have not actually
2762 + * inserted it onto the runqueue yet. This guarantees that
2763 + * nobody will actually run it, and a signal or other external
2764 + * event cannot wake it up and insert it on the runqueue either.
2766 + p->state = TASK_RUNNING;
2770 + * fork()/clone()-time setup:
2772 +void sched_fork(struct task_struct *p, int clone_flags)
2774 + int cpu = get_cpu();
2779 + cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2781 + set_task_cpu(p, cpu);
2784 + * Make sure we do not leak PI boosting priority to the child:
2786 + p->prio = current->normal_prio;
2787 + if (!rt_prio(p->prio))
2788 + p->sched_class = &fair_sched_class;
2790 +#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
2791 + if (likely(sched_info_on()))
2792 + memset(&p->sched_info, 0, sizeof(p->sched_info));
2794 +#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
2797 +#ifdef CONFIG_PREEMPT
2798 + /* Want to start with kernel preemption disabled. */
2799 + task_thread_info(p)->preempt_count = 1;
2805 + * wake_up_new_task - wake up a newly created task for the first time.
2807 + * This function will do some initial scheduler statistics housekeeping
2808 + * that must be done for every newly created context, then puts the task
2809 + * on the runqueue and wakes it.
2811 +void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2813 + unsigned long flags;
2816 + rq = task_rq_lock(p, &flags);
2817 + BUG_ON(p->state != TASK_RUNNING);
2818 + update_rq_clock(rq);
2820 + p->prio = effective_prio(p);
2822 + if (!p->sched_class->task_new || !current->se.on_rq) {
2823 + activate_task(rq, p, 0);
2826 + * Let the scheduling class do new task startup
2827 + * management (if any):
2829 + p->sched_class->task_new(rq, p);
2830 + inc_nr_running(rq);
2832 + trace_mark(kernel_sched_wakeup_new,
2833 + "pid %d state %ld ## rq %p task %p rq->curr %p",
2834 + p->pid, p->state, rq, p, rq->curr);
2835 + check_preempt_curr(rq, p);
2837 + if (p->sched_class->task_wake_up)
2838 + p->sched_class->task_wake_up(rq, p);
2840 + task_rq_unlock(rq, &flags);
2843 +#ifdef CONFIG_PREEMPT_NOTIFIERS
2846 + * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2847 + * @notifier: notifier struct to register
2849 +void preempt_notifier_register(struct preempt_notifier *notifier)
2851 + hlist_add_head(¬ifier->link, ¤t->preempt_notifiers);
2853 +EXPORT_SYMBOL_GPL(preempt_notifier_register);
2856 + * preempt_notifier_unregister - no longer interested in preemption notifications
2857 + * @notifier: notifier struct to unregister
2859 + * This is safe to call from within a preemption notifier.
2861 +void preempt_notifier_unregister(struct preempt_notifier *notifier)
2863 + hlist_del(¬ifier->link);
2865 +EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2867 +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2869 + struct preempt_notifier *notifier;
2870 + struct hlist_node *node;
2872 + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2873 + notifier->ops->sched_in(notifier, raw_smp_processor_id());
2877 +fire_sched_out_preempt_notifiers(struct task_struct *curr,
2878 + struct task_struct *next)
2880 + struct preempt_notifier *notifier;
2881 + struct hlist_node *node;
2883 + hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2884 + notifier->ops->sched_out(notifier, next);
2887 +#else /* !CONFIG_PREEMPT_NOTIFIERS */
2889 +static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2894 +fire_sched_out_preempt_notifiers(struct task_struct *curr,
2895 + struct task_struct *next)
2899 +#endif /* CONFIG_PREEMPT_NOTIFIERS */
2902 + * prepare_task_switch - prepare to switch tasks
2903 + * @rq: the runqueue preparing to switch
2904 + * @prev: the current task that is being switched out
2905 + * @next: the task we are going to switch to.
2907 + * This is called with the rq lock held and interrupts off. It must
2908 + * be paired with a subsequent finish_task_switch after the context
2911 + * prepare_task_switch sets up locking and calls architecture specific
2915 +prepare_task_switch(struct rq *rq, struct task_struct *prev,
2916 + struct task_struct *next)
2918 + fire_sched_out_preempt_notifiers(prev, next);
2919 + prepare_lock_switch(rq, next);
2920 + prepare_arch_switch(next);
2924 + * finish_task_switch - clean up after a task-switch
2925 + * @rq: runqueue associated with task-switch
2926 + * @prev: the thread we just switched away from.
2928 + * finish_task_switch must be called after the context switch, paired
2929 + * with a prepare_task_switch call before the context switch.
2930 + * finish_task_switch will reconcile locking set up by prepare_task_switch,
2931 + * and do any other architecture-specific cleanup actions.
2933 + * Note that we may have delayed dropping an mm in context_switch(). If
2934 + * so, we finish that here outside of the runqueue lock. (Doing it
2935 + * with the lock held can cause deadlocks; see schedule() for
2938 +static void finish_task_switch(struct rq *rq, struct task_struct *prev)
2939 + __releases(rq->lock)
2941 + struct mm_struct *mm = rq->prev_mm;
2944 + rq->prev_mm = NULL;
2947 + * A task struct has one reference for the use as "current".
2948 + * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2949 + * schedule one last time. The schedule call will never return, and
2950 + * the scheduled task must drop that reference.
2951 + * The test for TASK_DEAD must occur while the runqueue locks are
2952 + * still held, otherwise prev could be scheduled on another cpu, die
2953 + * there before we look at prev->state, and then the reference would
2954 + * be dropped twice.
2955 + * Manfred Spraul <manfred@colorfullife.com>
2957 + prev_state = prev->state;
2958 + finish_arch_switch(prev);
2959 + finish_lock_switch(rq, prev);
2961 + if (current->sched_class->post_schedule)
2962 + current->sched_class->post_schedule(rq);
2965 + fire_sched_in_preempt_notifiers(current);
2968 + if (unlikely(prev_state == TASK_DEAD)) {
2970 + * Remove function-return probe instances associated with this
2971 + * task and put them back on the free list.
2973 + kprobe_flush_task(prev);
2974 + put_task_struct(prev);
2979 + * schedule_tail - first thing a freshly forked thread must call.
2980 + * @prev: the thread we just switched away from.
2982 +asmlinkage void schedule_tail(struct task_struct *prev)
2983 + __releases(rq->lock)
2985 + struct rq *rq = this_rq();
2987 + finish_task_switch(rq, prev);
2988 +#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2989 + /* In this case, finish_task_switch does not reenable preemption */
2992 + if (current->set_child_tid)
2993 + put_user(task_pid_vnr(current), current->set_child_tid);
2997 + * context_switch - switch to the new MM and the new
2998 + * thread's register state.
3001 +context_switch(struct rq *rq, struct task_struct *prev,
3002 + struct task_struct *next)
3004 + struct mm_struct *mm, *oldmm;
3006 + prepare_task_switch(rq, prev, next);
3007 + trace_mark(kernel_sched_schedule,
3008 + "prev_pid %d next_pid %d prev_state %ld "
3009 + "## rq %p prev %p next %p",
3010 + prev->pid, next->pid, prev->state,
3013 + oldmm = prev->active_mm;
3015 + * For paravirt, this is coupled with an exit in switch_to to
3016 + * combine the page table reload and the switch backend into
3019 + arch_enter_lazy_cpu_mode();
3021 + if (unlikely(!mm)) {
3022 + next->active_mm = oldmm;
3023 + atomic_inc(&oldmm->mm_count);
3024 + enter_lazy_tlb(oldmm, next);
3026 + switch_mm(oldmm, mm, next);
3028 + if (unlikely(!prev->mm)) {
3029 + prev->active_mm = NULL;
3030 + rq->prev_mm = oldmm;
3033 + * Since the runqueue lock will be released by the next
3034 + * task (which is an invalid locking op but in the case
3035 + * of the scheduler it's an obvious special-case), so we
3036 + * do an early lockdep release here:
3038 +#ifndef __ARCH_WANT_UNLOCKED_CTXSW
3039 + spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
3042 + /* Here we just switch the register state and the stack. */
3043 + switch_to(prev, next, prev);
3047 + * this_rq must be evaluated again because prev may have moved
3048 + * CPUs since it called schedule(), thus the 'rq' on its stack
3049 + * frame will be invalid.
3051 + finish_task_switch(this_rq(), prev);
3055 + * nr_running, nr_uninterruptible and nr_context_switches:
3057 + * externally visible scheduler statistics: current number of runnable
3058 + * threads, current number of uninterruptible-sleeping threads, total
3059 + * number of context switches performed since bootup.
3061 +unsigned long nr_running(void)
3063 + unsigned long i, sum = 0;
3065 + for_each_online_cpu(i)
3066 + sum += cpu_rq(i)->nr_running;
3071 +unsigned long nr_uninterruptible(void)
3073 + unsigned long i, sum = 0;
3075 + for_each_possible_cpu(i)
3076 + sum += cpu_rq(i)->nr_uninterruptible;
3079 + * Since we read the counters lockless, it might be slightly
3080 + * inaccurate. Do not allow it to go below zero though:
3082 + if (unlikely((long)sum < 0))
3088 +unsigned long long nr_context_switches(void)
3091 + unsigned long long sum = 0;
3093 + for_each_possible_cpu(i)
3094 + sum += cpu_rq(i)->nr_switches;
3099 +unsigned long nr_iowait(void)
3101 + unsigned long i, sum = 0;
3103 + for_each_possible_cpu(i)
3104 + sum += atomic_read(&cpu_rq(i)->nr_iowait);
3109 +unsigned long nr_active(void)
3111 + unsigned long i, running = 0, uninterruptible = 0;
3113 + for_each_online_cpu(i) {
3114 + running += cpu_rq(i)->nr_running;
3115 + uninterruptible += cpu_rq(i)->nr_uninterruptible;
3118 + if (unlikely((long)uninterruptible < 0))
3119 + uninterruptible = 0;
3121 + return running + uninterruptible;
3125 + * Update rq->cpu_load[] statistics. This function is usually called every
3126 + * scheduler tick (TICK_NSEC).
3128 +static void update_cpu_load(struct rq *this_rq)
3130 + unsigned long this_load = this_rq->load.weight;
3133 + this_rq->nr_load_updates++;
3135 + /* Update our load: */
3136 + for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3137 + unsigned long old_load, new_load;
3139 + /* scale is effectively 1 << i now, and >> i divides by scale */
3141 + old_load = this_rq->cpu_load[i];
3142 + new_load = this_load;
3144 + * Round up the averaging division if load is increasing. This
3145 + * prevents us from getting stuck on 9 if the load is 10, for
3148 + if (new_load > old_load)
3149 + new_load += scale-1;
3150 + this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3157 + * double_rq_lock - safely lock two runqueues
3159 + * Note this does not disable interrupts like task_rq_lock,
3160 + * you need to do so manually before calling.
3162 +static void double_rq_lock(struct rq *rq1, struct rq *rq2)
3163 + __acquires(rq1->lock)
3164 + __acquires(rq2->lock)
3166 + BUG_ON(!irqs_disabled());
3168 + spin_lock(&rq1->lock);
3169 + __acquire(rq2->lock); /* Fake it out ;) */
3172 + spin_lock(&rq1->lock);
3173 + spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
3175 + spin_lock(&rq2->lock);
3176 + spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
3179 + update_rq_clock(rq1);
3180 + update_rq_clock(rq2);
3184 + * double_rq_unlock - safely unlock two runqueues
3186 + * Note this does not restore interrupts like task_rq_unlock,
3187 + * you need to do so manually after calling.
3189 +static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3190 + __releases(rq1->lock)
3191 + __releases(rq2->lock)
3193 + spin_unlock(&rq1->lock);
3195 + spin_unlock(&rq2->lock);
3197 + __release(rq2->lock);
3201 + * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3203 +static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
3204 + __releases(this_rq->lock)
3205 + __acquires(busiest->lock)
3206 + __acquires(this_rq->lock)
3210 + if (unlikely(!irqs_disabled())) {
3211 + /* printk() doesn't work good under rq->lock */
3212 + spin_unlock(&this_rq->lock);
3215 + if (unlikely(!spin_trylock(&busiest->lock))) {
3216 + if (busiest < this_rq) {
3217 + spin_unlock(&this_rq->lock);
3218 + spin_lock(&busiest->lock);
3219 + spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
3222 + spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
3227 +static void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
3228 + __releases(busiest->lock)
3230 + spin_unlock(&busiest->lock);
3231 + lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
3235 + * If dest_cpu is allowed for this process, migrate the task to it.
3236 + * This is accomplished by forcing the cpu_allowed mask to only
3237 + * allow dest_cpu, which will force the cpu onto dest_cpu. Then
3238 + * the cpu_allowed mask is restored.
3240 +static void sched_migrate_task(struct task_struct *p, int dest_cpu)
3242 + struct migration_req req;
3243 + unsigned long flags;
3246 + rq = task_rq_lock(p, &flags);
3247 + if (!cpu_isset(dest_cpu, p->cpus_allowed)
3248 + || unlikely(!cpu_active(dest_cpu)))
3251 + /* force the process onto the specified CPU */
3252 + if (migrate_task(p, dest_cpu, &req)) {
3253 + /* Need to wait for migration thread (might exit: take ref). */
3254 + struct task_struct *mt = rq->migration_thread;
3256 + get_task_struct(mt);
3257 + task_rq_unlock(rq, &flags);
3258 + wake_up_process(mt);
3259 + put_task_struct(mt);
3260 + wait_for_completion(&req.done);
3265 + task_rq_unlock(rq, &flags);
3269 + * sched_exec - execve() is a valuable balancing opportunity, because at
3270 + * this point the task has the smallest effective memory and cache footprint.
3272 +void sched_exec(void)
3274 + int new_cpu, this_cpu = get_cpu();
3275 + new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
3277 + if (new_cpu != this_cpu)
3278 + sched_migrate_task(current, new_cpu);
3282 + * pull_task - move a task from a remote runqueue to the local runqueue.
3283 + * Both runqueues must be locked.
3285 +static void pull_task(struct rq *src_rq, struct task_struct *p,
3286 + struct rq *this_rq, int this_cpu)
3288 + deactivate_task(src_rq, p, 0);
3289 + set_task_cpu(p, this_cpu);
3290 + activate_task(this_rq, p, 0);
3292 + * Note that idle threads have a prio of MAX_PRIO, for this test
3293 + * to be always true for them.
3295 + check_preempt_curr(this_rq, p);
3299 + * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3302 +int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
3303 + struct sched_domain *sd, enum cpu_idle_type idle,
3307 + * We do not migrate tasks that are:
3308 + * 1) running (obviously), or
3309 + * 2) cannot be migrated to this CPU due to cpus_allowed, or
3310 + * 3) are cache-hot on their current CPU.
3312 + if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3313 + schedstat_inc(p, se.nr_failed_migrations_affine);
3318 + if (task_running(rq, p)) {
3319 + schedstat_inc(p, se.nr_failed_migrations_running);
3324 + * Aggressive migration if:
3325 + * 1) task is cache cold, or
3326 + * 2) too many balance attempts have failed.
3329 + if (!task_hot(p, rq->clock, sd) ||
3330 + sd->nr_balance_failed > sd->cache_nice_tries) {
3331 +#ifdef CONFIG_SCHEDSTATS
3332 + if (task_hot(p, rq->clock, sd)) {
3333 + schedstat_inc(sd, lb_hot_gained[idle]);
3334 + schedstat_inc(p, se.nr_forced_migrations);
3340 + if (task_hot(p, rq->clock, sd)) {
3341 + schedstat_inc(p, se.nr_failed_migrations_hot);
3347 +static unsigned long
3348 +balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3349 + unsigned long max_load_move, struct sched_domain *sd,
3350 + enum cpu_idle_type idle, int *all_pinned,
3351 + int *this_best_prio, struct rq_iterator *iterator)
3353 + int loops = 0, pulled = 0, pinned = 0;
3354 + struct task_struct *p;
3355 + long rem_load_move = max_load_move;
3357 + if (max_load_move == 0)
3363 + * Start the load-balancing iterator:
3365 + p = iterator->start(iterator->arg);
3367 + if (!p || loops++ > sysctl_sched_nr_migrate)
3370 + if ((p->se.load.weight >> 1) > rem_load_move ||
3371 + !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3372 + p = iterator->next(iterator->arg);
3376 + pull_task(busiest, p, this_rq, this_cpu);
3378 + rem_load_move -= p->se.load.weight;
3381 + * We only want to steal up to the prescribed amount of weighted load.
3383 + if (rem_load_move > 0) {
3384 + if (p->prio < *this_best_prio)
3385 + *this_best_prio = p->prio;
3386 + p = iterator->next(iterator->arg);
3391 + * Right now, this is one of only two places pull_task() is called,
3392 + * so we can safely collect pull_task() stats here rather than
3393 + * inside pull_task().
3395 + schedstat_add(sd, lb_gained[idle], pulled);
3398 + *all_pinned = pinned;
3400 + return max_load_move - rem_load_move;
3404 + * move_tasks tries to move up to max_load_move weighted load from busiest to
3405 + * this_rq, as part of a balancing operation within domain "sd".
3406 + * Returns 1 if successful and 0 otherwise.
3408 + * Called with both runqueues locked.
3410 +static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3411 + unsigned long max_load_move,
3412 + struct sched_domain *sd, enum cpu_idle_type idle,
3415 + const struct sched_class *class = sched_class_highest;
3416 + unsigned long total_load_moved = 0;
3417 + int this_best_prio = this_rq->curr->prio;
3420 + total_load_moved +=
3421 + class->load_balance(this_rq, this_cpu, busiest,
3422 + max_load_move - total_load_moved,
3423 + sd, idle, all_pinned, &this_best_prio);
3424 + class = class->next;
3426 + if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3429 + } while (class && max_load_move > total_load_moved);
3431 + return total_load_moved > 0;
3435 +iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3436 + struct sched_domain *sd, enum cpu_idle_type idle,
3437 + struct rq_iterator *iterator)
3439 + struct task_struct *p = iterator->start(iterator->arg);
3443 + if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3444 + pull_task(busiest, p, this_rq, this_cpu);
3446 + * Right now, this is only the second place pull_task()
3447 + * is called, so we can safely collect pull_task()
3448 + * stats here rather than inside pull_task().
3450 + schedstat_inc(sd, lb_gained[idle]);
3454 + p = iterator->next(iterator->arg);
3461 + * move_one_task tries to move exactly one task from busiest to this_rq, as
3462 + * part of active balancing operations within "domain".
3463 + * Returns 1 if successful and 0 otherwise.
3465 + * Called with both runqueues locked.
3467 +static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3468 + struct sched_domain *sd, enum cpu_idle_type idle)
3470 + const struct sched_class *class;
3472 + for (class = sched_class_highest; class; class = class->next)
3473 + if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
3480 + * find_busiest_group finds and returns the busiest CPU group within the
3481 + * domain. It calculates and returns the amount of weighted load which
3482 + * should be moved to restore balance via the imbalance parameter.
3484 +static struct sched_group *
3485 +find_busiest_group(struct sched_domain *sd, int this_cpu,
3486 + unsigned long *imbalance, enum cpu_idle_type idle,
3487 + int *sd_idle, const cpumask_t *cpus, int *balance)
3489 + struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3490 + unsigned long max_load, avg_load, total_load, this_load, total_pwr;
3491 + unsigned long max_pull;
3492 + unsigned long busiest_load_per_task, busiest_nr_running;
3493 + unsigned long this_load_per_task, this_nr_running;
3494 + int load_idx, group_imb = 0;
3495 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3496 + int power_savings_balance = 1;
3497 + unsigned long leader_nr_running = 0, min_load_per_task = 0;
3498 + unsigned long min_nr_running = ULONG_MAX;
3499 + struct sched_group *group_min = NULL, *group_leader = NULL;
3502 + max_load = this_load = total_load = total_pwr = 0;
3503 + busiest_load_per_task = busiest_nr_running = 0;
3504 + this_load_per_task = this_nr_running = 0;
3506 + if (idle == CPU_NOT_IDLE)
3507 + load_idx = sd->busy_idx;
3508 + else if (idle == CPU_NEWLY_IDLE)
3509 + load_idx = sd->newidle_idx;
3511 + load_idx = sd->idle_idx;
3514 + unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
3517 + int __group_imb = 0;
3518 + unsigned int balance_cpu = -1, first_idle_cpu = 0;
3519 + unsigned long sum_nr_running, sum_weighted_load;
3520 + unsigned long sum_avg_load_per_task;
3521 + unsigned long avg_load_per_task;
3523 + local_group = cpu_isset(this_cpu, group->cpumask);
3526 + balance_cpu = first_cpu(group->cpumask);
3528 + /* Tally up the load of all CPUs in the group */
3529 + sum_weighted_load = sum_nr_running = avg_load = 0;
3530 + sum_avg_load_per_task = avg_load_per_task = 0;
3533 + min_cpu_load = ~0UL;
3535 + for_each_cpu_mask_nr(i, group->cpumask) {
3538 + if (!cpu_isset(i, *cpus))
3543 + if (*sd_idle && rq->nr_running)
3546 + /* Bias balancing toward cpus of our domain */
3547 + if (local_group) {
3548 + if (idle_cpu(i) && !first_idle_cpu) {
3549 + first_idle_cpu = 1;
3553 + load = target_load(i, load_idx);
3555 + load = source_load(i, load_idx);
3556 + if (load > max_cpu_load)
3557 + max_cpu_load = load;
3558 + if (min_cpu_load > load)
3559 + min_cpu_load = load;
3563 + sum_nr_running += rq->nr_running;
3564 + sum_weighted_load += weighted_cpuload(i);
3566 + sum_avg_load_per_task += cpu_avg_load_per_task(i);
3570 + * First idle cpu or the first cpu(busiest) in this sched group
3571 + * is eligible for doing load balancing at this and above
3572 + * domains. In the newly idle case, we will allow all the cpu's
3573 + * to do the newly idle load balance.
3575 + if (idle != CPU_NEWLY_IDLE && local_group &&
3576 + balance_cpu != this_cpu && balance) {
3581 + total_load += avg_load;
3582 + total_pwr += group->__cpu_power;
3584 + /* Adjust by relative CPU power of the group */
3585 + avg_load = sg_div_cpu_power(group,
3586 + avg_load * SCHED_LOAD_SCALE);
3590 + * Consider the group unbalanced when the imbalance is larger
3591 + * than the average weight of two tasks.
3593 + * APZ: with cgroup the avg task weight can vary wildly and
3594 + * might not be a suitable number - should we keep a
3595 + * normalized nr_running number somewhere that negates
3598 + avg_load_per_task = sg_div_cpu_power(group,
3599 + sum_avg_load_per_task * SCHED_LOAD_SCALE);
3601 + if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
3604 + group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
3606 + if (local_group) {
3607 + this_load = avg_load;
3609 + this_nr_running = sum_nr_running;
3610 + this_load_per_task = sum_weighted_load;
3611 + } else if (avg_load > max_load &&
3612 + (sum_nr_running > group_capacity || __group_imb)) {
3613 + max_load = avg_load;
3615 + busiest_nr_running = sum_nr_running;
3616 + busiest_load_per_task = sum_weighted_load;
3617 + group_imb = __group_imb;
3620 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3622 + * Busy processors will not participate in power savings
3625 + if (idle == CPU_NOT_IDLE ||
3626 + !(sd->flags & SD_POWERSAVINGS_BALANCE))
3630 + * If the local group is idle or completely loaded
3631 + * no need to do power savings balance at this domain
3633 + if (local_group && (this_nr_running >= group_capacity ||
3634 + !this_nr_running))
3635 + power_savings_balance = 0;
3638 + * If a group is already running at full capacity or idle,
3639 + * don't include that group in power savings calculations
3641 + if (!power_savings_balance || sum_nr_running >= group_capacity
3642 + || !sum_nr_running)
3646 + * Calculate the group which has the least non-idle load.
3647 + * This is the group from where we need to pick up the load
3648 + * for saving power
3650 + if ((sum_nr_running < min_nr_running) ||
3651 + (sum_nr_running == min_nr_running &&
3652 + first_cpu(group->cpumask) <
3653 + first_cpu(group_min->cpumask))) {
3654 + group_min = group;
3655 + min_nr_running = sum_nr_running;
3656 + min_load_per_task = sum_weighted_load /
3661 + * Calculate the group which is almost near its
3662 + * capacity but still has some space to pick up some load
3663 + * from other group and save more power
3665 + if (sum_nr_running <= group_capacity - 1) {
3666 + if (sum_nr_running > leader_nr_running ||
3667 + (sum_nr_running == leader_nr_running &&
3668 + first_cpu(group->cpumask) >
3669 + first_cpu(group_leader->cpumask))) {
3670 + group_leader = group;
3671 + leader_nr_running = sum_nr_running;
3676 + group = group->next;
3677 + } while (group != sd->groups);
3679 + if (!busiest || this_load >= max_load || busiest_nr_running == 0)
3680 + goto out_balanced;
3682 + avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3684 + if (this_load >= avg_load ||
3685 + 100*max_load <= sd->imbalance_pct*this_load)
3686 + goto out_balanced;
3688 + busiest_load_per_task /= busiest_nr_running;
3690 + busiest_load_per_task = min(busiest_load_per_task, avg_load);
3693 + * We're trying to get all the cpus to the average_load, so we don't
3694 + * want to push ourselves above the average load, nor do we wish to
3695 + * reduce the max loaded cpu below the average load, as either of these
3696 + * actions would just result in more rebalancing later, and ping-pong
3697 + * tasks around. Thus we look for the minimum possible imbalance.
3698 + * Negative imbalances (*we* are more loaded than anyone else) will
3699 + * be counted as no imbalance for these purposes -- we can't fix that
3700 + * by pulling tasks to us. Be careful of negative numbers as they'll
3701 + * appear as very large values with unsigned longs.
3703 + if (max_load <= busiest_load_per_task)
3704 + goto out_balanced;
3707 + * In the presence of smp nice balancing, certain scenarios can have
3708 + * max load less than avg load(as we skip the groups at or below
3709 + * its cpu_power, while calculating max_load..)
3711 + if (max_load < avg_load) {
3713 + goto small_imbalance;
3716 + /* Don't want to pull so many tasks that a group would go idle */
3717 + max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
3719 + /* How much load to actually move to equalise the imbalance */
3720 + *imbalance = min(max_pull * busiest->__cpu_power,
3721 + (avg_load - this_load) * this->__cpu_power)
3722 + / SCHED_LOAD_SCALE;
3725 + * if *imbalance is less than the average load per runnable task
3726 + * there is no gaurantee that any tasks will be moved so we'll have
3727 + * a think about bumping its value to force at least one task to be
3730 + if (*imbalance < busiest_load_per_task) {
3731 + unsigned long tmp, pwr_now, pwr_move;
3732 + unsigned int imbn;
3735 + pwr_move = pwr_now = 0;
3737 + if (this_nr_running) {
3738 + this_load_per_task /= this_nr_running;
3739 + if (busiest_load_per_task > this_load_per_task)
3742 + this_load_per_task = cpu_avg_load_per_task(this_cpu);
3744 + if (max_load - this_load + 2*busiest_load_per_task >=
3745 + busiest_load_per_task * imbn) {
3746 + *imbalance = busiest_load_per_task;
3751 + * OK, we don't have enough imbalance to justify moving tasks,
3752 + * however we may be able to increase total CPU power used by
3756 + pwr_now += busiest->__cpu_power *
3757 + min(busiest_load_per_task, max_load);
3758 + pwr_now += this->__cpu_power *
3759 + min(this_load_per_task, this_load);
3760 + pwr_now /= SCHED_LOAD_SCALE;
3762 + /* Amount of load we'd subtract */
3763 + tmp = sg_div_cpu_power(busiest,
3764 + busiest_load_per_task * SCHED_LOAD_SCALE);
3765 + if (max_load > tmp)
3766 + pwr_move += busiest->__cpu_power *
3767 + min(busiest_load_per_task, max_load - tmp);
3769 + /* Amount of load we'd add */
3770 + if (max_load * busiest->__cpu_power <
3771 + busiest_load_per_task * SCHED_LOAD_SCALE)
3772 + tmp = sg_div_cpu_power(this,
3773 + max_load * busiest->__cpu_power);
3775 + tmp = sg_div_cpu_power(this,
3776 + busiest_load_per_task * SCHED_LOAD_SCALE);
3777 + pwr_move += this->__cpu_power *
3778 + min(this_load_per_task, this_load + tmp);
3779 + pwr_move /= SCHED_LOAD_SCALE;
3781 + /* Move if we gain throughput */
3782 + if (pwr_move > pwr_now)
3783 + *imbalance = busiest_load_per_task;
3789 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3790 + if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3793 + if (this == group_leader && group_leader != group_min) {
3794 + *imbalance = min_load_per_task;
3804 + * find_busiest_queue - find the busiest runqueue among the cpus in group.
3807 +find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
3808 + unsigned long imbalance, const cpumask_t *cpus)
3810 + struct rq *busiest = NULL, *rq;
3811 + unsigned long max_load = 0;
3814 + for_each_cpu_mask_nr(i, group->cpumask) {
3817 + if (!cpu_isset(i, *cpus))
3821 + wl = weighted_cpuload(i);
3823 + if (rq->nr_running == 1 && wl > imbalance)
3826 + if (wl > max_load) {
3836 + * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3837 + * so long as it is large enough.
3839 +#define MAX_PINNED_INTERVAL 512
3842 + * Check this_cpu to ensure it is balanced within domain. Attempt to move
3843 + * tasks if there is an imbalance.
3845 +static int load_balance(int this_cpu, struct rq *this_rq,
3846 + struct sched_domain *sd, enum cpu_idle_type idle,
3847 + int *balance, cpumask_t *cpus)
3849 + int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
3850 + struct sched_group *group;
3851 + unsigned long imbalance;
3852 + struct rq *busiest;
3853 + unsigned long flags;
3855 + cpus_setall(*cpus);
3858 + * When power savings policy is enabled for the parent domain, idle
3859 + * sibling can pick up load irrespective of busy siblings. In this case,
3860 + * let the state of idle sibling percolate up as CPU_IDLE, instead of
3861 + * portraying it as CPU_NOT_IDLE.
3863 + if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
3864 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3867 + schedstat_inc(sd, lb_count[idle]);
3870 + update_shares(sd);
3871 + group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
3874 + if (*balance == 0)
3875 + goto out_balanced;
3878 + schedstat_inc(sd, lb_nobusyg[idle]);
3879 + goto out_balanced;
3882 + busiest = find_busiest_queue(group, idle, imbalance, cpus);
3884 + schedstat_inc(sd, lb_nobusyq[idle]);
3885 + goto out_balanced;
3888 + BUG_ON(busiest == this_rq);
3890 + schedstat_add(sd, lb_imbalance[idle], imbalance);
3893 + if (busiest->nr_running > 1) {
3895 + * Attempt to move tasks. If find_busiest_group has found
3896 + * an imbalance but busiest->nr_running <= 1, the group is
3897 + * still unbalanced. ld_moved simply stays zero, so it is
3898 + * correctly treated as an imbalance.
3900 + local_irq_save(flags);
3901 + double_rq_lock(this_rq, busiest);
3902 + ld_moved = move_tasks(this_rq, this_cpu, busiest,
3903 + imbalance, sd, idle, &all_pinned);
3904 + double_rq_unlock(this_rq, busiest);
3905 + local_irq_restore(flags);
3908 + * some other cpu did the load balance for us.
3910 + if (ld_moved && this_cpu != smp_processor_id())
3911 + resched_cpu(this_cpu);
3913 + /* All tasks on this runqueue were pinned by CPU affinity */
3914 + if (unlikely(all_pinned)) {
3915 + cpu_clear(cpu_of(busiest), *cpus);
3916 + if (!cpus_empty(*cpus))
3918 + goto out_balanced;
3923 + schedstat_inc(sd, lb_failed[idle]);
3924 + sd->nr_balance_failed++;
3926 + if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
3928 + spin_lock_irqsave(&busiest->lock, flags);
3930 + /* don't kick the migration_thread, if the curr
3931 + * task on busiest cpu can't be moved to this_cpu
3933 + if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
3934 + spin_unlock_irqrestore(&busiest->lock, flags);
3936 + goto out_one_pinned;
3939 + if (!busiest->active_balance) {
3940 + busiest->active_balance = 1;
3941 + busiest->push_cpu = this_cpu;
3942 + active_balance = 1;
3944 + spin_unlock_irqrestore(&busiest->lock, flags);
3945 + if (active_balance)
3946 + wake_up_process(busiest->migration_thread);
3949 + * We've kicked active balancing, reset the failure
3952 + sd->nr_balance_failed = sd->cache_nice_tries+1;
3955 + sd->nr_balance_failed = 0;
3957 + if (likely(!active_balance)) {
3958 + /* We were unbalanced, so reset the balancing interval */
3959 + sd->balance_interval = sd->min_interval;
3962 + * If we've begun active balancing, start to back off. This
3963 + * case may not be covered by the all_pinned logic if there
3964 + * is only 1 task on the busy runqueue (because we don't call
3967 + if (sd->balance_interval < sd->max_interval)
3968 + sd->balance_interval *= 2;
3971 + if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3972 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3978 + schedstat_inc(sd, lb_balanced[idle]);
3980 + sd->nr_balance_failed = 0;
3983 + /* tune up the balancing interval */
3984 + if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3985 + (sd->balance_interval < sd->max_interval))
3986 + sd->balance_interval *= 2;
3988 + if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3989 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
3995 + update_shares(sd);
4000 + * Check this_cpu to ensure it is balanced within domain. Attempt to move
4001 + * tasks if there is an imbalance.
4003 + * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
4004 + * this_rq is locked.
4007 +load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
4010 + struct sched_group *group;
4011 + struct rq *busiest = NULL;
4012 + unsigned long imbalance;
4015 + int all_pinned = 0;
4017 + cpus_setall(*cpus);
4020 + * When power savings policy is enabled for the parent domain, idle
4021 + * sibling can pick up load irrespective of busy siblings. In this case,
4022 + * let the state of idle sibling percolate up as IDLE, instead of
4023 + * portraying it as CPU_NOT_IDLE.
4025 + if (sd->flags & SD_SHARE_CPUPOWER &&
4026 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4029 + schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
4031 + update_shares_locked(this_rq, sd);
4032 + group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
4033 + &sd_idle, cpus, NULL);
4035 + schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
4036 + goto out_balanced;
4039 + busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
4041 + schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
4042 + goto out_balanced;
4045 + BUG_ON(busiest == this_rq);
4047 + schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
4050 + if (busiest->nr_running > 1) {
4051 + /* Attempt to move tasks */
4052 + double_lock_balance(this_rq, busiest);
4053 + /* this_rq->clock is already updated */
4054 + update_rq_clock(busiest);
4055 + ld_moved = move_tasks(this_rq, this_cpu, busiest,
4056 + imbalance, sd, CPU_NEWLY_IDLE,
4058 + double_unlock_balance(this_rq, busiest);
4060 + if (unlikely(all_pinned)) {
4061 + cpu_clear(cpu_of(busiest), *cpus);
4062 + if (!cpus_empty(*cpus))
4068 + schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
4069 + if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4070 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4073 + sd->nr_balance_failed = 0;
4075 + update_shares_locked(this_rq, sd);
4079 + schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
4080 + if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4081 + !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
4083 + sd->nr_balance_failed = 0;
4089 + * idle_balance is called by schedule() if this_cpu is about to become
4090 + * idle. Attempts to pull tasks from other CPUs.
4092 +static void idle_balance(int this_cpu, struct rq *this_rq)
4094 + struct sched_domain *sd;
4095 + int pulled_task = -1;
4096 + unsigned long next_balance = jiffies + HZ;
4097 + cpumask_t tmpmask;
4099 + for_each_domain(this_cpu, sd) {
4100 + unsigned long interval;
4102 + if (!(sd->flags & SD_LOAD_BALANCE))
4105 + if (sd->flags & SD_BALANCE_NEWIDLE)
4106 + /* If we've pulled tasks over stop searching: */
4107 + pulled_task = load_balance_newidle(this_cpu, this_rq,
4110 + interval = msecs_to_jiffies(sd->balance_interval);
4111 + if (time_after(next_balance, sd->last_balance + interval))
4112 + next_balance = sd->last_balance + interval;
4116 + if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
4118 + * We are going idle. next_balance may be set based on
4119 + * a busy processor. So reset next_balance.
4121 + this_rq->next_balance = next_balance;
4126 + * active_load_balance is run by migration threads. It pushes running tasks
4127 + * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4128 + * running on each physical CPU where possible, and avoids physical /
4129 + * logical imbalances.
4131 + * Called with busiest_rq locked.
4133 +static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
4135 + int target_cpu = busiest_rq->push_cpu;
4136 + struct sched_domain *sd;
4137 + struct rq *target_rq;
4139 + /* Is there any task to move? */
4140 + if (busiest_rq->nr_running <= 1)
4143 + target_rq = cpu_rq(target_cpu);
4146 + * This condition is "impossible", if it occurs
4147 + * we need to fix it. Originally reported by
4148 + * Bjorn Helgaas on a 128-cpu setup.
4150 + BUG_ON(busiest_rq == target_rq);
4152 + /* move a task from busiest_rq to target_rq */
4153 + double_lock_balance(busiest_rq, target_rq);
4154 + update_rq_clock(busiest_rq);
4155 + update_rq_clock(target_rq);
4157 + /* Search for an sd spanning us and the target CPU. */
4158 + for_each_domain(target_cpu, sd) {
4159 + if ((sd->flags & SD_LOAD_BALANCE) &&
4160 + cpu_isset(busiest_cpu, sd->span))
4165 + schedstat_inc(sd, alb_count);
4167 + if (move_one_task(target_rq, target_cpu, busiest_rq,
4169 + schedstat_inc(sd, alb_pushed);
4171 + schedstat_inc(sd, alb_failed);
4173 + double_unlock_balance(busiest_rq, target_rq);
4176 +#ifdef CONFIG_NO_HZ
4178 + atomic_t load_balancer;
4179 + cpumask_t cpu_mask;
4180 +} nohz ____cacheline_aligned = {
4181 + .load_balancer = ATOMIC_INIT(-1),
4182 + .cpu_mask = CPU_MASK_NONE,
4186 + * This routine will try to nominate the ilb (idle load balancing)
4187 + * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4188 + * load balancing on behalf of all those cpus. If all the cpus in the system
4189 + * go into this tickless mode, then there will be no ilb owner (as there is
4190 + * no need for one) and all the cpus will sleep till the next wakeup event
4193 + * For the ilb owner, tick is not stopped. And this tick will be used
4194 + * for idle load balancing. ilb owner will still be part of
4197 + * While stopping the tick, this cpu will become the ilb owner if there
4198 + * is no other owner. And will be the owner till that cpu becomes busy
4199 + * or if all cpus in the system stop their ticks at which point
4200 + * there is no need for ilb owner.
4202 + * When the ilb owner becomes busy, it nominates another owner, during the
4203 + * next busy scheduler_tick()
4205 +int select_nohz_load_balancer(int stop_tick)
4207 + int cpu = smp_processor_id();
4210 + cpu_set(cpu, nohz.cpu_mask);
4211 + cpu_rq(cpu)->in_nohz_recently = 1;
4214 + * If we are going offline and still the leader, give up!
4216 + if (!cpu_active(cpu) &&
4217 + atomic_read(&nohz.load_balancer) == cpu) {
4218 + if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4223 + /* time for ilb owner also to sleep */
4224 + if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4225 + if (atomic_read(&nohz.load_balancer) == cpu)
4226 + atomic_set(&nohz.load_balancer, -1);
4230 + if (atomic_read(&nohz.load_balancer) == -1) {
4231 + /* make me the ilb owner */
4232 + if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4234 + } else if (atomic_read(&nohz.load_balancer) == cpu)
4237 + if (!cpu_isset(cpu, nohz.cpu_mask))
4240 + cpu_clear(cpu, nohz.cpu_mask);
4242 + if (atomic_read(&nohz.load_balancer) == cpu)
4243 + if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4250 +static DEFINE_SPINLOCK(balancing);
4253 + * It checks each scheduling domain to see if it is due to be balanced,
4254 + * and initiates a balancing operation if so.
4256 + * Balancing parameters are set up in arch_init_sched_domains.
4258 +static void rebalance_domains(int cpu, enum cpu_idle_type idle)
4261 + struct rq *rq = cpu_rq(cpu);
4262 + unsigned long interval;
4263 + struct sched_domain *sd;
4264 + /* Earliest time when we have to do rebalance again */
4265 + unsigned long next_balance = jiffies + 60*HZ;
4266 + int update_next_balance = 0;
4267 + int need_serialize;
4270 + for_each_domain(cpu, sd) {
4271 + if (!(sd->flags & SD_LOAD_BALANCE))
4274 + interval = sd->balance_interval;
4275 + if (idle != CPU_IDLE)
4276 + interval *= sd->busy_factor;
4278 + /* scale ms to jiffies */
4279 + interval = msecs_to_jiffies(interval);
4280 + if (unlikely(!interval))
4282 + if (interval > HZ*NR_CPUS/10)
4283 + interval = HZ*NR_CPUS/10;
4285 + need_serialize = sd->flags & SD_SERIALIZE;
4287 + if (need_serialize) {
4288 + if (!spin_trylock(&balancing))
4292 + if (time_after_eq(jiffies, sd->last_balance + interval)) {
4293 + if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
4295 + * We've pulled tasks over so either we're no
4296 + * longer idle, or one of our SMT siblings is
4299 + idle = CPU_NOT_IDLE;
4301 + sd->last_balance = jiffies;
4303 + if (need_serialize)
4304 + spin_unlock(&balancing);
4306 + if (time_after(next_balance, sd->last_balance + interval)) {
4307 + next_balance = sd->last_balance + interval;
4308 + update_next_balance = 1;
4312 + * Stop the load balance at this level. There is another
4313 + * CPU in our sched group which is doing load balancing more
4321 + * next_balance will be updated only when there is a need.
4322 + * When the cpu is attached to null domain for ex, it will not be
4325 + if (likely(update_next_balance))
4326 + rq->next_balance = next_balance;
4330 + * run_rebalance_domains is triggered when needed from the scheduler tick.
4331 + * In CONFIG_NO_HZ case, the idle load balance owner will do the
4332 + * rebalancing for all the cpus for whom scheduler ticks are stopped.
4334 +static void run_rebalance_domains(struct softirq_action *h)
4336 + int this_cpu = smp_processor_id();
4337 + struct rq *this_rq = cpu_rq(this_cpu);
4338 + enum cpu_idle_type idle = this_rq->idle_at_tick ?
4339 + CPU_IDLE : CPU_NOT_IDLE;
4341 + rebalance_domains(this_cpu, idle);
4343 +#ifdef CONFIG_NO_HZ
4345 + * If this cpu is the owner for idle load balancing, then do the
4346 + * balancing on behalf of the other idle cpus whose ticks are
4349 + if (this_rq->idle_at_tick &&
4350 + atomic_read(&nohz.load_balancer) == this_cpu) {
4351 + cpumask_t cpus = nohz.cpu_mask;
4355 + cpu_clear(this_cpu, cpus);
4356 + for_each_cpu_mask_nr(balance_cpu, cpus) {
4358 + * If this cpu gets work to do, stop the load balancing
4359 + * work being done for other cpus. Next load
4360 + * balancing owner will pick it up.
4362 + if (need_resched())
4365 + rebalance_domains(balance_cpu, CPU_IDLE);
4367 + rq = cpu_rq(balance_cpu);
4368 + if (time_after(this_rq->next_balance, rq->next_balance))
4369 + this_rq->next_balance = rq->next_balance;
4376 + * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4378 + * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4379 + * idle load balancing owner or decide to stop the periodic load balancing,
4380 + * if the whole system is idle.
4382 +static inline void trigger_load_balance(struct rq *rq, int cpu)
4384 +#ifdef CONFIG_NO_HZ
4386 + * If we were in the nohz mode recently and busy at the current
4387 + * scheduler tick, then check if we need to nominate new idle
4390 + if (rq->in_nohz_recently && !rq->idle_at_tick) {
4391 + rq->in_nohz_recently = 0;
4393 + if (atomic_read(&nohz.load_balancer) == cpu) {
4394 + cpu_clear(cpu, nohz.cpu_mask);
4395 + atomic_set(&nohz.load_balancer, -1);
4398 + if (atomic_read(&nohz.load_balancer) == -1) {
4400 + * simple selection for now: Nominate the
4401 + * first cpu in the nohz list to be the next
4404 + * TBD: Traverse the sched domains and nominate
4405 + * the nearest cpu in the nohz.cpu_mask.
4407 + int ilb = first_cpu(nohz.cpu_mask);
4409 + if (ilb < nr_cpu_ids)
4415 + * If this cpu is idle and doing idle load balancing for all the
4416 + * cpus with ticks stopped, is it time for that to stop?
4418 + if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4419 + cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4425 + * If this cpu is idle and the idle load balancing is done by
4426 + * someone else, then no need raise the SCHED_SOFTIRQ
4428 + if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4429 + cpu_isset(cpu, nohz.cpu_mask))
4432 + if (time_after_eq(jiffies, rq->next_balance))
4433 + raise_softirq(SCHED_SOFTIRQ);
4436 +#else /* CONFIG_SMP */
4439 + * on UP we do not need to balance between CPUs:
4441 +static inline void idle_balance(int cpu, struct rq *rq)
4447 +DEFINE_PER_CPU(struct kernel_stat, kstat);
4449 +EXPORT_PER_CPU_SYMBOL(kstat);
4452 + * Return p->sum_exec_runtime plus any more ns on the sched_clock
4453 + * that have not yet been banked in case the task is currently running.
4455 +unsigned long long task_sched_runtime(struct task_struct *p)
4457 + unsigned long flags;
4458 + u64 ns, delta_exec;
4461 + rq = task_rq_lock(p, &flags);
4462 + ns = p->se.sum_exec_runtime;
4463 + if (task_current(rq, p)) {
4464 + update_rq_clock(rq);
4465 + delta_exec = rq->clock - p->se.exec_start;
4466 + if ((s64)delta_exec > 0)
4469 + task_rq_unlock(rq, &flags);
4475 + * Account user cpu time to a process.
4476 + * @p: the process that the cpu time gets accounted to
4477 + * @cputime: the cpu time spent in user space since the last update
4479 +void account_user_time(struct task_struct *p, cputime_t cputime)
4481 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4482 + struct vx_info *vxi = p->vx_info; /* p is _always_ current */
4484 + int nice = (TASK_NICE(p) > 0);
4486 + p->utime = cputime_add(p->utime, cputime);
4487 + vx_account_user(vxi, cputime, nice);
4489 + /* Add user time to cpustat. */
4490 + tmp = cputime_to_cputime64(cputime);
4492 + cpustat->nice = cputime64_add(cpustat->nice, tmp);
4494 + cpustat->user = cputime64_add(cpustat->user, tmp);
4495 + /* Account for user time used */
4496 + acct_update_integrals(p);
4500 + * Account guest cpu time to a process.
4501 + * @p: the process that the cpu time gets accounted to
4502 + * @cputime: the cpu time spent in virtual machine since the last update
4504 +static void account_guest_time(struct task_struct *p, cputime_t cputime)
4507 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4509 + tmp = cputime_to_cputime64(cputime);
4511 + p->utime = cputime_add(p->utime, cputime);
4512 + p->gtime = cputime_add(p->gtime, cputime);
4514 + cpustat->user = cputime64_add(cpustat->user, tmp);
4515 + cpustat->guest = cputime64_add(cpustat->guest, tmp);
4519 + * Account scaled user cpu time to a process.
4520 + * @p: the process that the cpu time gets accounted to
4521 + * @cputime: the cpu time spent in user space since the last update
4523 +void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4525 + p->utimescaled = cputime_add(p->utimescaled, cputime);
4529 + * Account system cpu time to a process.
4530 + * @p: the process that the cpu time gets accounted to
4531 + * @hardirq_offset: the offset to subtract from hardirq_count()
4532 + * @cputime: the cpu time spent in kernel space since the last update
4534 +void account_system_time(struct task_struct *p, int hardirq_offset,
4535 + cputime_t cputime)
4537 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4538 + struct vx_info *vxi = p->vx_info; /* p is _always_ current */
4539 + struct rq *rq = this_rq();
4542 + if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4543 + account_guest_time(p, cputime);
4547 + p->stime = cputime_add(p->stime, cputime);
4548 + vx_account_system(vxi, cputime, (p == rq->idle));
4550 + /* Add system time to cpustat. */
4551 + tmp = cputime_to_cputime64(cputime);
4552 + if (hardirq_count() - hardirq_offset)
4553 + cpustat->irq = cputime64_add(cpustat->irq, tmp);
4554 + else if (softirq_count())
4555 + cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
4556 + else if (p != rq->idle)
4557 + cpustat->system = cputime64_add(cpustat->system, tmp);
4558 + else if (atomic_read(&rq->nr_iowait) > 0)
4559 + cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4561 + cpustat->idle = cputime64_add(cpustat->idle, tmp);
4562 + /* Account for system time used */
4563 + acct_update_integrals(p);
4567 + * Account scaled system cpu time to a process.
4568 + * @p: the process that the cpu time gets accounted to
4569 + * @hardirq_offset: the offset to subtract from hardirq_count()
4570 + * @cputime: the cpu time spent in kernel space since the last update
4572 +void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4574 + p->stimescaled = cputime_add(p->stimescaled, cputime);
4578 + * Account for involuntary wait time.
4579 + * @p: the process from which the cpu time has been stolen
4580 + * @steal: the cpu time spent in involuntary wait
4582 +void account_steal_time(struct task_struct *p, cputime_t steal)
4584 + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4585 + cputime64_t tmp = cputime_to_cputime64(steal);
4586 + struct rq *rq = this_rq();
4588 + if (p == rq->idle) {
4589 + p->stime = cputime_add(p->stime, steal);
4590 + if (atomic_read(&rq->nr_iowait) > 0)
4591 + cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4593 + cpustat->idle = cputime64_add(cpustat->idle, tmp);
4595 + cpustat->steal = cputime64_add(cpustat->steal, tmp);
4599 + * Use precise platform statistics if available:
4601 +#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4602 +cputime_t task_utime(struct task_struct *p)
4607 +cputime_t task_stime(struct task_struct *p)
4612 +cputime_t task_utime(struct task_struct *p)
4614 + clock_t utime = cputime_to_clock_t(p->utime),
4615 + total = utime + cputime_to_clock_t(p->stime);
4619 + * Use CFS's precise accounting:
4621 + temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4625 + do_div(temp, total);
4627 + utime = (clock_t)temp;
4629 + p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4630 + return p->prev_utime;
4633 +cputime_t task_stime(struct task_struct *p)
4638 + * Use CFS's precise accounting. (we subtract utime from
4639 + * the total, to make sure the total observed by userspace
4640 + * grows monotonically - apps rely on that):
4642 + stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4643 + cputime_to_clock_t(task_utime(p));
4646 + p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4648 + return p->prev_stime;
4652 +inline cputime_t task_gtime(struct task_struct *p)
4658 + * This function gets called by the timer code, with HZ frequency.
4659 + * We call it with interrupts disabled.
4661 + * It also gets called by the fork code, when changing the parent's
4664 +void scheduler_tick(void)
4666 + int cpu = smp_processor_id();
4667 + struct rq *rq = cpu_rq(cpu);
4668 + struct task_struct *curr = rq->curr;
4670 + sched_clock_tick();
4672 + spin_lock(&rq->lock);
4673 + update_rq_clock(rq);
4674 + update_cpu_load(rq);
4675 + curr->sched_class->task_tick(rq, curr, 0);
4676 + spin_unlock(&rq->lock);
4679 + rq->idle_at_tick = idle_cpu(cpu);
4680 + trigger_load_balance(rq, cpu);
4684 +#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4685 + defined(CONFIG_PREEMPT_TRACER))
4687 +static inline unsigned long get_parent_ip(unsigned long addr)
4689 + if (in_lock_functions(addr)) {
4690 + addr = CALLER_ADDR2;
4691 + if (in_lock_functions(addr))
4692 + addr = CALLER_ADDR3;
4697 +void __kprobes add_preempt_count(int val)
4699 +#ifdef CONFIG_DEBUG_PREEMPT
4703 + if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4706 + preempt_count() += val;
4707 +#ifdef CONFIG_DEBUG_PREEMPT
4709 + * Spinlock count overflowing soon?
4711 + DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4712 + PREEMPT_MASK - 10);
4714 + if (preempt_count() == val)
4715 + trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
4717 +EXPORT_SYMBOL(add_preempt_count);
4719 +void __kprobes sub_preempt_count(int val)
4721 +#ifdef CONFIG_DEBUG_PREEMPT
4725 + if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4728 + * Is the spinlock portion underflowing?
4730 + if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4731 + !(preempt_count() & PREEMPT_MASK)))
4735 + if (preempt_count() == val)
4736 + trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
4737 + preempt_count() -= val;
4739 +EXPORT_SYMBOL(sub_preempt_count);
4744 + * Print scheduling while atomic bug:
4746 +static noinline void __schedule_bug(struct task_struct *prev)
4748 + struct pt_regs *regs = get_irq_regs();
4750 + printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4751 + prev->comm, prev->pid, preempt_count());
4753 + debug_show_held_locks(prev);
4755 + if (irqs_disabled())
4756 + print_irqtrace_events(prev);
4765 + * Various schedule()-time debugging checks and statistics:
4767 +static inline void schedule_debug(struct task_struct *prev)
4770 + * Test if we are atomic. Since do_exit() needs to call into
4771 + * schedule() atomically, we ignore that path for now.
4772 + * Otherwise, whine if we are scheduling when we should not be.
4774 + if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
4775 + __schedule_bug(prev);
4777 + profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4779 + schedstat_inc(this_rq(), sched_count);
4780 +#ifdef CONFIG_SCHEDSTATS
4781 + if (unlikely(prev->lock_depth >= 0)) {
4782 + schedstat_inc(this_rq(), bkl_count);
4783 + schedstat_inc(prev, sched_info.bkl_count);
4789 + * Pick up the highest-prio task:
4791 +static inline struct task_struct *
4792 +pick_next_task(struct rq *rq, struct task_struct *prev)
4794 + const struct sched_class *class;
4795 + struct task_struct *p;
4798 + * Optimization: we know that if all tasks are in
4799 + * the fair class we can call that function directly:
4801 + if (likely(rq->nr_running == rq->cfs.nr_running)) {
4802 + p = fair_sched_class.pick_next_task(rq);
4807 + class = sched_class_highest;
4809 + p = class->pick_next_task(rq);
4813 + * Will never be NULL as the idle class always
4814 + * returns a non-NULL p:
4816 + class = class->next;
4821 + * schedule() is the main scheduler function.
4823 +asmlinkage void __sched schedule(void)
4825 + struct task_struct *prev, *next;
4826 + unsigned long *switch_count;
4831 + preempt_disable();
4832 + cpu = smp_processor_id();
4834 + rcu_qsctr_inc(cpu);
4836 + switch_count = &prev->nivcsw;
4838 + release_kernel_lock(prev);
4839 +need_resched_nonpreemptible:
4841 + schedule_debug(prev);
4843 + if (sched_feat(HRTICK))
4847 + * Do the rq-clock update outside the rq lock:
4849 + local_irq_disable();
4850 + update_rq_clock(rq);
4851 + spin_lock(&rq->lock);
4852 + clear_tsk_need_resched(prev);
4854 + if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4855 + if (unlikely(signal_pending_state(prev->state, prev)))
4856 + prev->state = TASK_RUNNING;
4858 + deactivate_task(rq, prev, 1);
4859 + switch_count = &prev->nvcsw;
4863 + if (prev->sched_class->pre_schedule)
4864 + prev->sched_class->pre_schedule(rq, prev);
4867 + if (unlikely(!rq->nr_running))
4868 + idle_balance(cpu, rq);
4870 + prev->sched_class->put_prev_task(rq, prev);
4871 + next = pick_next_task(rq, prev);
4873 + if (likely(prev != next)) {
4874 + sched_info_switch(prev, next);
4876 + rq->nr_switches++;
4880 + context_switch(rq, prev, next); /* unlocks the rq */
4882 + * the context switch might have flipped the stack from under
4883 + * us, hence refresh the local variables.
4885 + cpu = smp_processor_id();
4888 + spin_unlock_irq(&rq->lock);
4890 + if (unlikely(reacquire_kernel_lock(current) < 0))
4891 + goto need_resched_nonpreemptible;
4893 + preempt_enable_no_resched();
4894 + if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4895 + goto need_resched;
4897 +EXPORT_SYMBOL(schedule);
4899 +#ifdef CONFIG_PREEMPT
4901 + * this is the entry point to schedule() from in-kernel preemption
4902 + * off of preempt_enable. Kernel preemptions off return from interrupt
4903 + * occur there and call schedule directly.
4905 +asmlinkage void __sched preempt_schedule(void)
4907 + struct thread_info *ti = current_thread_info();
4910 + * If there is a non-zero preempt_count or interrupts are disabled,
4911 + * we do not want to preempt the current task. Just return..
4913 + if (likely(ti->preempt_count || irqs_disabled()))
4917 + add_preempt_count(PREEMPT_ACTIVE);
4919 + sub_preempt_count(PREEMPT_ACTIVE);
4922 + * Check again in case we missed a preemption opportunity
4923 + * between schedule and now.
4926 + } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
4928 +EXPORT_SYMBOL(preempt_schedule);
4931 + * this is the entry point to schedule() from kernel preemption
4932 + * off of irq context.
4933 + * Note, that this is called and return with irqs disabled. This will
4934 + * protect us against recursive calling from irq.
4936 +asmlinkage void __sched preempt_schedule_irq(void)
4938 + struct thread_info *ti = current_thread_info();
4940 + /* Catch callers which need to be fixed */
4941 + BUG_ON(ti->preempt_count || !irqs_disabled());
4944 + add_preempt_count(PREEMPT_ACTIVE);
4945 + local_irq_enable();
4947 + local_irq_disable();
4948 + sub_preempt_count(PREEMPT_ACTIVE);
4951 + * Check again in case we missed a preemption opportunity
4952 + * between schedule and now.
4955 + } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
4958 +#endif /* CONFIG_PREEMPT */
4960 +int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4963 + return try_to_wake_up(curr->private, mode, sync);
4965 +EXPORT_SYMBOL(default_wake_function);
4968 + * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4969 + * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
4970 + * number) then we wake all the non-exclusive tasks and one exclusive task.
4972 + * There are circumstances in which we can try to wake a task which has already
4973 + * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
4974 + * zero in this (rare) case, and we handle it by continuing to scan the queue.
4976 +static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4977 + int nr_exclusive, int sync, void *key)
4979 + wait_queue_t *curr, *next;
4981 + list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
4982 + unsigned flags = curr->flags;
4984 + if (curr->func(curr, mode, sync, key) &&
4985 + (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
4991 + * __wake_up - wake up threads blocked on a waitqueue.
4992 + * @q: the waitqueue
4993 + * @mode: which threads
4994 + * @nr_exclusive: how many wake-one or wake-many threads to wake up
4995 + * @key: is directly passed to the wakeup function
4997 +void __wake_up(wait_queue_head_t *q, unsigned int mode,
4998 + int nr_exclusive, void *key)
5000 + unsigned long flags;
5002 + spin_lock_irqsave(&q->lock, flags);
5003 + __wake_up_common(q, mode, nr_exclusive, 0, key);
5004 + spin_unlock_irqrestore(&q->lock, flags);
5006 +EXPORT_SYMBOL(__wake_up);
5009 + * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
5011 +void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
5013 + __wake_up_common(q, mode, 1, 0, NULL);
5017 + * __wake_up_sync - wake up threads blocked on a waitqueue.
5018 + * @q: the waitqueue
5019 + * @mode: which threads
5020 + * @nr_exclusive: how many wake-one or wake-many threads to wake up
5022 + * The sync wakeup differs that the waker knows that it will schedule
5023 + * away soon, so while the target thread will be woken up, it will not
5024 + * be migrated to another CPU - ie. the two threads are 'synchronized'
5025 + * with each other. This can prevent needless bouncing between CPUs.
5027 + * On UP it can prevent extra preemption.
5030 +__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
5032 + unsigned long flags;
5038 + if (unlikely(!nr_exclusive))
5041 + spin_lock_irqsave(&q->lock, flags);
5042 + __wake_up_common(q, mode, nr_exclusive, sync, NULL);
5043 + spin_unlock_irqrestore(&q->lock, flags);
5045 +EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
5047 +void complete(struct completion *x)
5049 + unsigned long flags;
5051 + spin_lock_irqsave(&x->wait.lock, flags);
5053 + __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
5054 + spin_unlock_irqrestore(&x->wait.lock, flags);
5056 +EXPORT_SYMBOL(complete);
5058 +void complete_all(struct completion *x)
5060 + unsigned long flags;
5062 + spin_lock_irqsave(&x->wait.lock, flags);
5063 + x->done += UINT_MAX/2;
5064 + __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
5065 + spin_unlock_irqrestore(&x->wait.lock, flags);
5067 +EXPORT_SYMBOL(complete_all);
5069 +static inline long __sched
5070 +do_wait_for_common(struct completion *x, long timeout, int state)
5073 + DECLARE_WAITQUEUE(wait, current);
5075 + wait.flags |= WQ_FLAG_EXCLUSIVE;
5076 + __add_wait_queue_tail(&x->wait, &wait);
5078 + if ((state == TASK_INTERRUPTIBLE &&
5079 + signal_pending(current)) ||
5080 + (state == TASK_KILLABLE &&
5081 + fatal_signal_pending(current))) {
5082 + timeout = -ERESTARTSYS;
5085 + __set_current_state(state);
5086 + spin_unlock_irq(&x->wait.lock);
5087 + timeout = schedule_timeout(timeout);
5088 + spin_lock_irq(&x->wait.lock);
5089 + } while (!x->done && timeout);
5090 + __remove_wait_queue(&x->wait, &wait);
5095 + return timeout ?: 1;
5098 +static long __sched
5099 +wait_for_common(struct completion *x, long timeout, int state)
5103 + spin_lock_irq(&x->wait.lock);
5104 + timeout = do_wait_for_common(x, timeout, state);
5105 + spin_unlock_irq(&x->wait.lock);
5109 +void __sched wait_for_completion(struct completion *x)
5111 + wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
5113 +EXPORT_SYMBOL(wait_for_completion);
5115 +unsigned long __sched
5116 +wait_for_completion_timeout(struct completion *x, unsigned long timeout)
5118 + return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
5120 +EXPORT_SYMBOL(wait_for_completion_timeout);
5122 +int __sched wait_for_completion_interruptible(struct completion *x)
5124 + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
5125 + if (t == -ERESTARTSYS)
5129 +EXPORT_SYMBOL(wait_for_completion_interruptible);
5131 +unsigned long __sched
5132 +wait_for_completion_interruptible_timeout(struct completion *x,
5133 + unsigned long timeout)
5135 + return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
5137 +EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
5139 +int __sched wait_for_completion_killable(struct completion *x)
5141 + long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
5142 + if (t == -ERESTARTSYS)
5146 +EXPORT_SYMBOL(wait_for_completion_killable);
5149 + * try_wait_for_completion - try to decrement a completion without blocking
5150 + * @x: completion structure
5152 + * Returns: 0 if a decrement cannot be done without blocking
5153 + * 1 if a decrement succeeded.
5155 + * If a completion is being used as a counting completion,
5156 + * attempt to decrement the counter without blocking. This
5157 + * enables us to avoid waiting if the resource the completion
5158 + * is protecting is not available.
5160 +bool try_wait_for_completion(struct completion *x)
5164 + spin_lock_irq(&x->wait.lock);
5169 + spin_unlock_irq(&x->wait.lock);
5172 +EXPORT_SYMBOL(try_wait_for_completion);
5175 + * completion_done - Test to see if a completion has any waiters
5176 + * @x: completion structure
5178 + * Returns: 0 if there are waiters (wait_for_completion() in progress)
5179 + * 1 if there are no waiters.
5182 +bool completion_done(struct completion *x)
5186 + spin_lock_irq(&x->wait.lock);
5189 + spin_unlock_irq(&x->wait.lock);
5192 +EXPORT_SYMBOL(completion_done);
5194 +static long __sched
5195 +sleep_on_common(wait_queue_head_t *q, int state, long timeout)
5197 + unsigned long flags;
5198 + wait_queue_t wait;
5200 + init_waitqueue_entry(&wait, current);
5202 + __set_current_state(state);
5204 + spin_lock_irqsave(&q->lock, flags);
5205 + __add_wait_queue(q, &wait);
5206 + spin_unlock(&q->lock);
5207 + timeout = schedule_timeout(timeout);
5208 + spin_lock_irq(&q->lock);
5209 + __remove_wait_queue(q, &wait);
5210 + spin_unlock_irqrestore(&q->lock, flags);
5215 +void __sched interruptible_sleep_on(wait_queue_head_t *q)
5217 + sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
5219 +EXPORT_SYMBOL(interruptible_sleep_on);
5222 +interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
5224 + return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
5226 +EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5228 +void __sched sleep_on(wait_queue_head_t *q)
5230 + sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
5232 +EXPORT_SYMBOL(sleep_on);
5234 +long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
5236 + return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
5238 +EXPORT_SYMBOL(sleep_on_timeout);
5240 +#ifdef CONFIG_RT_MUTEXES
5243 + * rt_mutex_setprio - set the current priority of a task
5245 + * @prio: prio value (kernel-internal form)
5247 + * This function changes the 'effective' priority of a task. It does
5248 + * not touch ->normal_prio like __setscheduler().
5250 + * Used by the rt_mutex code to implement priority inheritance logic.
5252 +void rt_mutex_setprio(struct task_struct *p, int prio)
5254 + unsigned long flags;
5255 + int oldprio, on_rq, running;
5257 + const struct sched_class *prev_class = p->sched_class;
5259 + BUG_ON(prio < 0 || prio > MAX_PRIO);
5261 + rq = task_rq_lock(p, &flags);
5262 + update_rq_clock(rq);
5264 + oldprio = p->prio;
5265 + on_rq = p->se.on_rq;
5266 + running = task_current(rq, p);
5268 + dequeue_task(rq, p, 0);
5270 + p->sched_class->put_prev_task(rq, p);
5272 + if (rt_prio(prio))
5273 + p->sched_class = &rt_sched_class;
5275 + p->sched_class = &fair_sched_class;
5280 + p->sched_class->set_curr_task(rq);
5282 + enqueue_task(rq, p, 0);
5284 + check_class_changed(rq, p, prev_class, oldprio, running);
5286 + task_rq_unlock(rq, &flags);
5291 +void set_user_nice(struct task_struct *p, long nice)
5293 + int old_prio, delta, on_rq;
5294 + unsigned long flags;
5297 + if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5300 + * We have to be careful, if called from sys_setpriority(),
5301 + * the task might be in the middle of scheduling on another CPU.
5303 + rq = task_rq_lock(p, &flags);
5304 + update_rq_clock(rq);
5306 + * The RT priorities are set via sched_setscheduler(), but we still
5307 + * allow the 'normal' nice value to be set - but as expected
5308 + * it wont have any effect on scheduling until the task is
5309 + * SCHED_FIFO/SCHED_RR:
5311 + if (task_has_rt_policy(p)) {
5312 + p->static_prio = NICE_TO_PRIO(nice);
5315 + on_rq = p->se.on_rq;
5317 + dequeue_task(rq, p, 0);
5319 + p->static_prio = NICE_TO_PRIO(nice);
5320 + set_load_weight(p);
5321 + old_prio = p->prio;
5322 + p->prio = effective_prio(p);
5323 + delta = p->prio - old_prio;
5326 + enqueue_task(rq, p, 0);
5328 + * If the task increased its priority or is running and
5329 + * lowered its priority, then reschedule its CPU:
5331 + if (delta < 0 || (delta > 0 && task_running(rq, p)))
5332 + resched_task(rq->curr);
5335 + task_rq_unlock(rq, &flags);
5337 +EXPORT_SYMBOL(set_user_nice);
5340 + * can_nice - check if a task can reduce its nice value
5342 + * @nice: nice value
5344 +int can_nice(const struct task_struct *p, const int nice)
5346 + /* convert nice value [19,-20] to rlimit style value [1,40] */
5347 + int nice_rlim = 20 - nice;
5349 + return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5350 + capable(CAP_SYS_NICE));
5353 +#ifdef __ARCH_WANT_SYS_NICE
5356 + * sys_nice - change the priority of the current process.
5357 + * @increment: priority increment
5359 + * sys_setpriority is a more generic, but much slower function that
5360 + * does similar things.
5362 +SYSCALL_DEFINE1(nice, int, increment)
5364 + long nice, retval;
5367 + * Setpriority might change our priority at the same moment.
5368 + * We don't have to worry. Conceptually one call occurs first
5369 + * and we have a single winner.
5371 + if (increment < -40)
5373 + if (increment > 40)
5376 + nice = PRIO_TO_NICE(current->static_prio) + increment;
5382 + if (increment < 0 && !can_nice(current, nice))
5383 + return vx_flags(VXF_IGNEG_NICE, 0) ? 0 : -EPERM;
5385 + retval = security_task_setnice(current, nice);
5389 + set_user_nice(current, nice);
5396 + * task_prio - return the priority value of a given task.
5397 + * @p: the task in question.
5399 + * This is the priority value as seen by users in /proc.
5400 + * RT tasks are offset by -200. Normal tasks are centered
5401 + * around 0, value goes from -16 to +15.
5403 +int task_prio(const struct task_struct *p)
5405 + return p->prio - MAX_RT_PRIO;
5409 + * task_nice - return the nice value of a given task.
5410 + * @p: the task in question.
5412 +int task_nice(const struct task_struct *p)
5414 + return TASK_NICE(p);
5416 +EXPORT_SYMBOL(task_nice);
5419 + * idle_cpu - is a given cpu idle currently?
5420 + * @cpu: the processor in question.
5422 +int idle_cpu(int cpu)
5424 + return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5428 + * idle_task - return the idle task for a given cpu.
5429 + * @cpu: the processor in question.
5431 +struct task_struct *idle_task(int cpu)
5433 + return cpu_rq(cpu)->idle;
5437 + * find_process_by_pid - find a process with a matching PID value.
5438 + * @pid: the pid in question.
5440 +static struct task_struct *find_process_by_pid(pid_t pid)
5442 + return pid ? find_task_by_vpid(pid) : current;
5445 +/* Actually do priority change: must hold rq lock. */
5447 +__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
5449 + BUG_ON(p->se.on_rq);
5451 + p->policy = policy;
5452 + switch (p->policy) {
5453 + case SCHED_NORMAL:
5456 + p->sched_class = &fair_sched_class;
5460 + p->sched_class = &rt_sched_class;
5464 + p->rt_priority = prio;
5465 + p->normal_prio = normal_prio(p);
5466 + /* we are holding p->pi_lock already */
5467 + p->prio = rt_mutex_getprio(p);
5468 + set_load_weight(p);
5471 +static int __sched_setscheduler(struct task_struct *p, int policy,
5472 + struct sched_param *param, bool user)
5474 + int retval, oldprio, oldpolicy = -1, on_rq, running;
5475 + unsigned long flags;
5476 + const struct sched_class *prev_class = p->sched_class;
5479 + /* may grab non-irq protected spin_locks */
5480 + BUG_ON(in_interrupt());
5482 + /* double check policy once rq lock held */
5484 + policy = oldpolicy = p->policy;
5485 + else if (policy != SCHED_FIFO && policy != SCHED_RR &&
5486 + policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5487 + policy != SCHED_IDLE)
5490 + * Valid priorities for SCHED_FIFO and SCHED_RR are
5491 + * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5492 + * SCHED_BATCH and SCHED_IDLE is 0.
5494 + if (param->sched_priority < 0 ||
5495 + (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
5496 + (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
5498 + if (rt_policy(policy) != (param->sched_priority != 0))
5502 + * Allow unprivileged RT tasks to decrease priority:
5504 + if (user && !capable(CAP_SYS_NICE)) {
5505 + if (rt_policy(policy)) {
5506 + unsigned long rlim_rtprio;
5508 + if (!lock_task_sighand(p, &flags))
5510 + rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5511 + unlock_task_sighand(p, &flags);
5513 + /* can't set/change the rt policy */
5514 + if (policy != p->policy && !rlim_rtprio)
5517 + /* can't increase priority */
5518 + if (param->sched_priority > p->rt_priority &&
5519 + param->sched_priority > rlim_rtprio)
5523 + * Like positive nice levels, dont allow tasks to
5524 + * move out of SCHED_IDLE either:
5526 + if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5529 + /* can't change other user's priorities */
5530 + if ((current->euid != p->euid) &&
5531 + (current->euid != p->uid))
5536 +#ifdef CONFIG_RT_GROUP_SCHED
5538 + * Do not allow realtime tasks into groups that have no runtime
5541 + if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
5545 + retval = security_task_setscheduler(p, policy, param);
5551 + * make sure no PI-waiters arrive (or leave) while we are
5552 + * changing the priority of the task:
5554 + spin_lock_irqsave(&p->pi_lock, flags);
5556 + * To be able to change p->policy safely, the apropriate
5557 + * runqueue lock must be held.
5559 + rq = __task_rq_lock(p);
5560 + /* recheck policy now with rq lock held */
5561 + if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5562 + policy = oldpolicy = -1;
5563 + __task_rq_unlock(rq);
5564 + spin_unlock_irqrestore(&p->pi_lock, flags);
5567 + update_rq_clock(rq);
5568 + on_rq = p->se.on_rq;
5569 + running = task_current(rq, p);
5571 + deactivate_task(rq, p, 0);
5573 + p->sched_class->put_prev_task(rq, p);
5575 + oldprio = p->prio;
5576 + __setscheduler(rq, p, policy, param->sched_priority);
5579 + p->sched_class->set_curr_task(rq);
5581 + activate_task(rq, p, 0);
5583 + check_class_changed(rq, p, prev_class, oldprio, running);
5585 + __task_rq_unlock(rq);
5586 + spin_unlock_irqrestore(&p->pi_lock, flags);
5588 + rt_mutex_adjust_pi(p);
5594 + * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5595 + * @p: the task in question.
5596 + * @policy: new policy.
5597 + * @param: structure containing the new RT priority.
5599 + * NOTE that the task may be already dead.
5601 +int sched_setscheduler(struct task_struct *p, int policy,
5602 + struct sched_param *param)
5604 + return __sched_setscheduler(p, policy, param, true);
5606 +EXPORT_SYMBOL_GPL(sched_setscheduler);
5609 + * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5610 + * @p: the task in question.
5611 + * @policy: new policy.
5612 + * @param: structure containing the new RT priority.
5614 + * Just like sched_setscheduler, only don't bother checking if the
5615 + * current context has permission. For example, this is needed in
5616 + * stop_machine(): we create temporary high priority worker threads,
5617 + * but our caller might not have that capability.
5619 +int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5620 + struct sched_param *param)
5622 + return __sched_setscheduler(p, policy, param, false);
5626 +do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
5628 + struct sched_param lparam;
5629 + struct task_struct *p;
5632 + if (!param || pid < 0)
5634 + if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5639 + p = find_process_by_pid(pid);
5641 + retval = sched_setscheduler(p, policy, &lparam);
5642 + rcu_read_unlock();
5648 + * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5649 + * @pid: the pid in question.
5650 + * @policy: new policy.
5651 + * @param: structure containing the new RT priority.
5653 +SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
5654 + struct sched_param __user *, param)
5656 + /* negative values for policy are not valid */
5660 + return do_sched_setscheduler(pid, policy, param);
5664 + * sys_sched_setparam - set/change the RT priority of a thread
5665 + * @pid: the pid in question.
5666 + * @param: structure containing the new RT priority.
5668 +SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
5670 + return do_sched_setscheduler(pid, -1, param);
5674 + * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5675 + * @pid: the pid in question.
5677 +SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
5679 + struct task_struct *p;
5686 + read_lock(&tasklist_lock);
5687 + p = find_process_by_pid(pid);
5689 + retval = security_task_getscheduler(p);
5691 + retval = p->policy;
5693 + read_unlock(&tasklist_lock);
5698 + * sys_sched_getscheduler - get the RT priority of a thread
5699 + * @pid: the pid in question.
5700 + * @param: structure containing the RT priority.
5702 +SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
5704 + struct sched_param lp;
5705 + struct task_struct *p;
5708 + if (!param || pid < 0)
5711 + read_lock(&tasklist_lock);
5712 + p = find_process_by_pid(pid);
5717 + retval = security_task_getscheduler(p);
5721 + lp.sched_priority = p->rt_priority;
5722 + read_unlock(&tasklist_lock);
5725 + * This one might sleep, we cannot do it with a spinlock held ...
5727 + retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5732 + read_unlock(&tasklist_lock);
5736 +long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
5738 + cpumask_t cpus_allowed;
5739 + cpumask_t new_mask = *in_mask;
5740 + struct task_struct *p;
5743 + get_online_cpus();
5744 + read_lock(&tasklist_lock);
5746 + p = find_process_by_pid(pid);
5748 + read_unlock(&tasklist_lock);
5749 + put_online_cpus();
5754 + * It is not safe to call set_cpus_allowed with the
5755 + * tasklist_lock held. We will bump the task_struct's
5756 + * usage count and then drop tasklist_lock.
5758 + get_task_struct(p);
5759 + read_unlock(&tasklist_lock);
5763 + if ((current->euid != p->euid) && (current->euid != p->uid) &&
5764 + !capable(CAP_SYS_NICE))
5767 + retval = security_task_setscheduler(p, 0, NULL);
5771 + cpuset_cpus_allowed(p, &cpus_allowed);
5772 + cpus_and(new_mask, new_mask, cpus_allowed);
5774 + retval = set_cpus_allowed_ptr(p, &new_mask);
5777 + cpuset_cpus_allowed(p, &cpus_allowed);
5778 + if (!cpus_subset(new_mask, cpus_allowed)) {
5780 + * We must have raced with a concurrent cpuset
5781 + * update. Just reset the cpus_allowed to the
5782 + * cpuset's cpus_allowed
5784 + new_mask = cpus_allowed;
5789 + put_task_struct(p);
5790 + put_online_cpus();
5794 +static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5795 + cpumask_t *new_mask)
5797 + if (len < sizeof(cpumask_t)) {
5798 + memset(new_mask, 0, sizeof(cpumask_t));
5799 + } else if (len > sizeof(cpumask_t)) {
5800 + len = sizeof(cpumask_t);
5802 + return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5806 + * sys_sched_setaffinity - set the cpu affinity of a process
5807 + * @pid: pid of the process
5808 + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5809 + * @user_mask_ptr: user-space pointer to the new cpu mask
5811 +SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
5812 + unsigned long __user *, user_mask_ptr)
5814 + cpumask_t new_mask;
5817 + retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5821 + return sched_setaffinity(pid, &new_mask);
5824 +long sched_getaffinity(pid_t pid, cpumask_t *mask)
5826 + struct task_struct *p;
5829 + get_online_cpus();
5830 + read_lock(&tasklist_lock);
5833 + p = find_process_by_pid(pid);
5837 + retval = security_task_getscheduler(p);
5841 + cpus_and(*mask, p->cpus_allowed, cpu_online_map);
5844 + read_unlock(&tasklist_lock);
5845 + put_online_cpus();
5851 + * sys_sched_getaffinity - get the cpu affinity of a process
5852 + * @pid: pid of the process
5853 + * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5854 + * @user_mask_ptr: user-space pointer to hold the current cpu mask
5856 +SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
5857 + unsigned long __user *, user_mask_ptr)
5862 + if (len < sizeof(cpumask_t))
5865 + ret = sched_getaffinity(pid, &mask);
5869 + if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5872 + return sizeof(cpumask_t);
5876 + * sys_sched_yield - yield the current processor to other threads.
5878 + * This function yields the current CPU to other tasks. If there are no
5879 + * other threads running on this CPU then this function will return.
5881 +SYSCALL_DEFINE0(sched_yield)
5883 + struct rq *rq = this_rq_lock();
5885 + schedstat_inc(rq, yld_count);
5886 + current->sched_class->yield_task(rq);
5889 + * Since we are going to call schedule() anyway, there's
5890 + * no need to preempt or enable interrupts:
5892 + __release(rq->lock);
5893 + spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
5894 + _raw_spin_unlock(&rq->lock);
5895 + preempt_enable_no_resched();
5902 +static void __cond_resched(void)
5904 +#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5905 + __might_sleep(__FILE__, __LINE__);
5908 + * The BKS might be reacquired before we have dropped
5909 + * PREEMPT_ACTIVE, which could trigger a second
5910 + * cond_resched() call.
5913 + add_preempt_count(PREEMPT_ACTIVE);
5915 + sub_preempt_count(PREEMPT_ACTIVE);
5916 + } while (need_resched());
5919 +int __sched _cond_resched(void)
5921 + if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5922 + system_state == SYSTEM_RUNNING) {
5928 +EXPORT_SYMBOL(_cond_resched);
5931 + * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5932 + * call schedule, and on return reacquire the lock.
5934 + * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
5935 + * operations here to prevent schedule() from being called twice (once via
5936 + * spin_unlock(), once by hand).
5938 +int cond_resched_lock(spinlock_t *lock)
5940 + int resched = need_resched() && system_state == SYSTEM_RUNNING;
5943 + if (spin_needbreak(lock) || resched) {
5944 + spin_unlock(lock);
5945 + if (resched && need_resched())
5954 +EXPORT_SYMBOL(cond_resched_lock);
5956 +int __sched cond_resched_softirq(void)
5958 + BUG_ON(!in_softirq());
5960 + if (need_resched() && system_state == SYSTEM_RUNNING) {
5961 + local_bh_enable();
5963 + local_bh_disable();
5968 +EXPORT_SYMBOL(cond_resched_softirq);
5971 + * yield - yield the current processor to other threads.
5973 + * This is a shortcut for kernel-space yielding - it marks the
5974 + * thread runnable and calls sys_sched_yield().
5976 +void __sched yield(void)
5978 + set_current_state(TASK_RUNNING);
5979 + sys_sched_yield();
5981 +EXPORT_SYMBOL(yield);
5984 + * This task is about to go to sleep on IO. Increment rq->nr_iowait so
5985 + * that process accounting knows that this is a task in IO wait state.
5987 + * But don't do that if it is a deliberate, throttling IO wait (this task
5988 + * has set its backing_dev_info: the queue against which it should throttle)
5990 +void __sched io_schedule(void)
5992 + struct rq *rq = &__raw_get_cpu_var(runqueues);
5994 + delayacct_blkio_start();
5995 + atomic_inc(&rq->nr_iowait);
5997 + atomic_dec(&rq->nr_iowait);
5998 + delayacct_blkio_end();
6000 +EXPORT_SYMBOL(io_schedule);
6002 +long __sched io_schedule_timeout(long timeout)
6004 + struct rq *rq = &__raw_get_cpu_var(runqueues);
6007 + delayacct_blkio_start();
6008 + atomic_inc(&rq->nr_iowait);
6009 + ret = schedule_timeout(timeout);
6010 + atomic_dec(&rq->nr_iowait);
6011 + delayacct_blkio_end();
6016 + * sys_sched_get_priority_max - return maximum RT priority.
6017 + * @policy: scheduling class.
6019 + * this syscall returns the maximum rt_priority that can be used
6020 + * by a given scheduling class.
6022 +SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
6024 + int ret = -EINVAL;
6029 + ret = MAX_USER_RT_PRIO-1;
6031 + case SCHED_NORMAL:
6041 + * sys_sched_get_priority_min - return minimum RT priority.
6042 + * @policy: scheduling class.
6044 + * this syscall returns the minimum rt_priority that can be used
6045 + * by a given scheduling class.
6047 +SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
6049 + int ret = -EINVAL;
6056 + case SCHED_NORMAL:
6065 + * sys_sched_rr_get_interval - return the default timeslice of a process.
6066 + * @pid: pid of the process.
6067 + * @interval: userspace pointer to the timeslice value.
6069 + * this syscall writes the default timeslice value of a given process
6070 + * into the user-space timespec buffer. A value of '0' means infinity.
6072 +SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
6073 + struct timespec __user *, interval)
6075 + struct task_struct *p;
6076 + unsigned int time_slice;
6078 + struct timespec t;
6084 + read_lock(&tasklist_lock);
6085 + p = find_process_by_pid(pid);
6089 + retval = security_task_getscheduler(p);
6094 + * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
6095 + * tasks that are on an otherwise idle runqueue:
6098 + if (p->policy == SCHED_RR) {
6099 + time_slice = DEF_TIMESLICE;
6100 + } else if (p->policy != SCHED_FIFO) {
6101 + struct sched_entity *se = &p->se;
6102 + unsigned long flags;
6105 + rq = task_rq_lock(p, &flags);
6106 + if (rq->cfs.load.weight)
6107 + time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
6108 + task_rq_unlock(rq, &flags);
6110 + read_unlock(&tasklist_lock);
6111 + jiffies_to_timespec(time_slice, &t);
6112 + retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
6116 + read_unlock(&tasklist_lock);
6120 +static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
6122 +void sched_show_task(struct task_struct *p)
6124 + unsigned long free = 0;
6127 + state = p->state ? __ffs(p->state) + 1 : 0;
6128 + printk(KERN_INFO "%-13.13s %c", p->comm,
6129 + state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
6130 +#if BITS_PER_LONG == 32
6131 + if (state == TASK_RUNNING)
6132 + printk(KERN_CONT " running ");
6134 + printk(KERN_CONT " %08lx ", thread_saved_pc(p));
6136 + if (state == TASK_RUNNING)
6137 + printk(KERN_CONT " running task ");
6139 + printk(KERN_CONT " %016lx ", thread_saved_pc(p));
6141 +#ifdef CONFIG_DEBUG_STACK_USAGE
6143 + unsigned long *n = end_of_stack(p);
6146 + free = (unsigned long)n - (unsigned long)end_of_stack(p);
6149 + printk(KERN_CONT "%5lu %5d %6d\n", free,
6150 + task_pid_nr(p), task_pid_nr(p->real_parent));
6152 + show_stack(p, NULL);
6155 +void show_state_filter(unsigned long state_filter)
6157 + struct task_struct *g, *p;
6159 +#if BITS_PER_LONG == 32
6161 + " task PC stack pid father\n");
6164 + " task PC stack pid father\n");
6166 + read_lock(&tasklist_lock);
6167 + do_each_thread(g, p) {
6169 + * reset the NMI-timeout, listing all files on a slow
6170 + * console might take alot of time:
6172 + touch_nmi_watchdog();
6173 + if (!state_filter || (p->state & state_filter))
6174 + sched_show_task(p);
6175 + } while_each_thread(g, p);
6177 + touch_all_softlockup_watchdogs();
6179 +#ifdef CONFIG_SCHED_DEBUG
6180 + sysrq_sched_debug_show();
6182 + read_unlock(&tasklist_lock);
6184 + * Only show locks if all tasks are dumped:
6186 + if (state_filter == -1)
6187 + debug_show_all_locks();
6190 +void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6192 + idle->sched_class = &idle_sched_class;
6196 + * init_idle - set up an idle thread for a given CPU
6197 + * @idle: task in question
6198 + * @cpu: cpu the idle task belongs to
6200 + * NOTE: this function does not set the idle thread's NEED_RESCHED
6201 + * flag, to make booting more robust.
6203 +void __cpuinit init_idle(struct task_struct *idle, int cpu)
6205 + struct rq *rq = cpu_rq(cpu);
6206 + unsigned long flags;
6208 + __sched_fork(idle);
6209 + idle->se.exec_start = sched_clock();
6211 + idle->prio = idle->normal_prio = MAX_PRIO;
6212 + idle->cpus_allowed = cpumask_of_cpu(cpu);
6213 + __set_task_cpu(idle, cpu);
6215 + spin_lock_irqsave(&rq->lock, flags);
6216 + rq->curr = rq->idle = idle;
6217 +#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6220 + spin_unlock_irqrestore(&rq->lock, flags);
6222 + /* Set the preempt count _outside_ the spinlocks! */
6223 +#if defined(CONFIG_PREEMPT)
6224 + task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
6226 + task_thread_info(idle)->preempt_count = 0;
6229 + * The idle tasks have their own, simple scheduling class:
6231 + idle->sched_class = &idle_sched_class;
6235 + * In a system that switches off the HZ timer nohz_cpu_mask
6236 + * indicates which cpus entered this state. This is used
6237 + * in the rcu update to wait only for active cpus. For system
6238 + * which do not switch off the HZ timer nohz_cpu_mask should
6239 + * always be CPU_MASK_NONE.
6241 +cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
6244 + * Increase the granularity value when there are more CPUs,
6245 + * because with more CPUs the 'effective latency' as visible
6246 + * to users decreases. But the relationship is not linear,
6247 + * so pick a second-best guess by going with the log2 of the
6250 + * This idea comes from the SD scheduler of Con Kolivas:
6252 +static inline void sched_init_granularity(void)
6254 + unsigned int factor = 1 + ilog2(num_online_cpus());
6255 + const unsigned long limit = 200000000;
6257 + sysctl_sched_min_granularity *= factor;
6258 + if (sysctl_sched_min_granularity > limit)
6259 + sysctl_sched_min_granularity = limit;
6261 + sysctl_sched_latency *= factor;
6262 + if (sysctl_sched_latency > limit)
6263 + sysctl_sched_latency = limit;
6265 + sysctl_sched_wakeup_granularity *= factor;
6267 + sysctl_sched_shares_ratelimit *= factor;
6272 + * This is how migration works:
6274 + * 1) we queue a struct migration_req structure in the source CPU's
6275 + * runqueue and wake up that CPU's migration thread.
6276 + * 2) we down() the locked semaphore => thread blocks.
6277 + * 3) migration thread wakes up (implicitly it forces the migrated
6278 + * thread off the CPU)
6279 + * 4) it gets the migration request and checks whether the migrated
6280 + * task is still in the wrong runqueue.
6281 + * 5) if it's in the wrong runqueue then the migration thread removes
6282 + * it and puts it into the right queue.
6283 + * 6) migration thread up()s the semaphore.
6284 + * 7) we wake up and the migration is done.
6288 + * Change a given task's CPU affinity. Migrate the thread to a
6289 + * proper CPU and schedule it away if the CPU it's executing on
6290 + * is removed from the allowed bitmask.
6292 + * NOTE: the caller must have a valid reference to the task, the
6293 + * task must not exit() & deallocate itself prematurely. The
6294 + * call is not atomic; no spinlocks may be held.
6296 +int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
6298 + struct migration_req req;
6299 + unsigned long flags;
6303 + rq = task_rq_lock(p, &flags);
6304 + if (!cpus_intersects(*new_mask, cpu_online_map)) {
6309 + if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
6310 + !cpus_equal(p->cpus_allowed, *new_mask))) {
6315 + if (p->sched_class->set_cpus_allowed)
6316 + p->sched_class->set_cpus_allowed(p, new_mask);
6318 + p->cpus_allowed = *new_mask;
6319 + p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
6322 + /* Can the task run on the task's current CPU? If so, we're done */
6323 + if (cpu_isset(task_cpu(p), *new_mask))
6326 + if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
6327 + /* Need help from migration thread: drop lock and wait. */
6328 + task_rq_unlock(rq, &flags);
6329 + wake_up_process(rq->migration_thread);
6330 + wait_for_completion(&req.done);
6331 + tlb_migrate_finish(p->mm);
6335 + task_rq_unlock(rq, &flags);
6339 +EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
6342 + * Move (not current) task off this cpu, onto dest cpu. We're doing
6343 + * this because either it can't run here any more (set_cpus_allowed()
6344 + * away from this CPU, or CPU going down), or because we're
6345 + * attempting to rebalance this task on exec (sched_exec).
6347 + * So we race with normal scheduler movements, but that's OK, as long
6348 + * as the task is no longer on this CPU.
6350 + * Returns non-zero if task was successfully migrated.
6352 +static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
6354 + struct rq *rq_dest, *rq_src;
6355 + int ret = 0, on_rq;
6357 + if (unlikely(!cpu_active(dest_cpu)))
6360 + rq_src = cpu_rq(src_cpu);
6361 + rq_dest = cpu_rq(dest_cpu);
6363 + double_rq_lock(rq_src, rq_dest);
6364 + /* Already moved. */
6365 + if (task_cpu(p) != src_cpu)
6367 + /* Affinity changed (again). */
6368 + if (!cpu_isset(dest_cpu, p->cpus_allowed))
6371 + on_rq = p->se.on_rq;
6373 + deactivate_task(rq_src, p, 0);
6375 + set_task_cpu(p, dest_cpu);
6377 + activate_task(rq_dest, p, 0);
6378 + check_preempt_curr(rq_dest, p);
6383 + double_rq_unlock(rq_src, rq_dest);
6388 + * migration_thread - this is a highprio system thread that performs
6389 + * thread migration by bumping thread off CPU then 'pushing' onto
6390 + * another runqueue.
6392 +static int migration_thread(void *data)
6394 + int cpu = (long)data;
6398 + BUG_ON(rq->migration_thread != current);
6400 + set_current_state(TASK_INTERRUPTIBLE);
6401 + while (!kthread_should_stop()) {
6402 + struct migration_req *req;
6403 + struct list_head *head;
6405 + spin_lock_irq(&rq->lock);
6407 + if (cpu_is_offline(cpu)) {
6408 + spin_unlock_irq(&rq->lock);
6412 + if (rq->active_balance) {
6413 + active_load_balance(rq, cpu);
6414 + rq->active_balance = 0;
6417 + head = &rq->migration_queue;
6419 + if (list_empty(head)) {
6420 + spin_unlock_irq(&rq->lock);
6422 + set_current_state(TASK_INTERRUPTIBLE);
6425 + req = list_entry(head->next, struct migration_req, list);
6426 + list_del_init(head->next);
6428 + spin_unlock(&rq->lock);
6429 + __migrate_task(req->task, cpu, req->dest_cpu);
6430 + local_irq_enable();
6432 + complete(&req->done);
6434 + __set_current_state(TASK_RUNNING);
6438 + /* Wait for kthread_stop */
6439 + set_current_state(TASK_INTERRUPTIBLE);
6440 + while (!kthread_should_stop()) {
6442 + set_current_state(TASK_INTERRUPTIBLE);
6444 + __set_current_state(TASK_RUNNING);
6448 +#ifdef CONFIG_HOTPLUG_CPU
6450 +static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6454 + local_irq_disable();
6455 + ret = __migrate_task(p, src_cpu, dest_cpu);
6456 + local_irq_enable();
6461 + * Figure out where task on dead CPU should go, use force if necessary.
6462 + * NOTE: interrupts should be disabled by the caller
6464 +static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
6466 + unsigned long flags;
6472 + /* On same node? */
6473 + mask = node_to_cpumask(cpu_to_node(dead_cpu));
6474 + cpus_and(mask, mask, p->cpus_allowed);
6475 + dest_cpu = any_online_cpu(mask);
6477 + /* On any allowed CPU? */
6478 + if (dest_cpu >= nr_cpu_ids)
6479 + dest_cpu = any_online_cpu(p->cpus_allowed);
6481 + /* No more Mr. Nice Guy. */
6482 + if (dest_cpu >= nr_cpu_ids) {
6483 + cpumask_t cpus_allowed;
6485 + cpuset_cpus_allowed_locked(p, &cpus_allowed);
6487 + * Try to stay on the same cpuset, where the
6488 + * current cpuset may be a subset of all cpus.
6489 + * The cpuset_cpus_allowed_locked() variant of
6490 + * cpuset_cpus_allowed() will not block. It must be
6491 + * called within calls to cpuset_lock/cpuset_unlock.
6493 + rq = task_rq_lock(p, &flags);
6494 + p->cpus_allowed = cpus_allowed;
6495 + dest_cpu = any_online_cpu(p->cpus_allowed);
6496 + task_rq_unlock(rq, &flags);
6499 + * Don't tell them about moving exiting tasks or
6500 + * kernel threads (both mm NULL), since they never
6503 + if (p->mm && printk_ratelimit()) {
6504 + printk(KERN_INFO "process %d (%s) no "
6505 + "longer affine to cpu%d\n",
6506 + task_pid_nr(p), p->comm, dead_cpu);
6509 + } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
6513 + * While a dead CPU has no uninterruptible tasks queued at this point,
6514 + * it might still have a nonzero ->nr_uninterruptible counter, because
6515 + * for performance reasons the counter is not stricly tracking tasks to
6516 + * their home CPUs. So we just add the counter to another CPU's counter,
6517 + * to keep the global sum constant after CPU-down:
6519 +static void migrate_nr_uninterruptible(struct rq *rq_src)
6521 + struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
6522 + unsigned long flags;
6524 + local_irq_save(flags);
6525 + double_rq_lock(rq_src, rq_dest);
6526 + rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6527 + rq_src->nr_uninterruptible = 0;
6528 + double_rq_unlock(rq_src, rq_dest);
6529 + local_irq_restore(flags);
6532 +/* Run through task list and migrate tasks from the dead cpu. */
6533 +static void migrate_live_tasks(int src_cpu)
6535 + struct task_struct *p, *t;
6537 + read_lock(&tasklist_lock);
6539 + do_each_thread(t, p) {
6543 + if (task_cpu(p) == src_cpu)
6544 + move_task_off_dead_cpu(src_cpu, p);
6545 + } while_each_thread(t, p);
6547 + read_unlock(&tasklist_lock);
6551 + * Schedules idle task to be the next runnable task on current CPU.
6552 + * It does so by boosting its priority to highest possible.
6553 + * Used by CPU offline code.
6555 +void sched_idle_next(void)
6557 + int this_cpu = smp_processor_id();
6558 + struct rq *rq = cpu_rq(this_cpu);
6559 + struct task_struct *p = rq->idle;
6560 + unsigned long flags;
6562 + /* cpu has to be offline */
6563 + BUG_ON(cpu_online(this_cpu));
6566 + * Strictly not necessary since rest of the CPUs are stopped by now
6567 + * and interrupts disabled on the current cpu.
6569 + spin_lock_irqsave(&rq->lock, flags);
6571 + __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
6573 + update_rq_clock(rq);
6574 + activate_task(rq, p, 0);
6576 + spin_unlock_irqrestore(&rq->lock, flags);
6580 + * Ensures that the idle task is using init_mm right before its cpu goes
6583 +void idle_task_exit(void)
6585 + struct mm_struct *mm = current->active_mm;
6587 + BUG_ON(cpu_online(smp_processor_id()));
6589 + if (mm != &init_mm)
6590 + switch_mm(mm, &init_mm, current);
6594 +/* called under rq->lock with disabled interrupts */
6595 +static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
6597 + struct rq *rq = cpu_rq(dead_cpu);
6599 + /* Must be exiting, otherwise would be on tasklist. */
6600 + BUG_ON(!p->exit_state);
6602 + /* Cannot have done final schedule yet: would have vanished. */
6603 + BUG_ON(p->state == TASK_DEAD);
6605 + get_task_struct(p);
6608 + * Drop lock around migration; if someone else moves it,
6609 + * that's OK. No task can be added to this CPU, so iteration is
6612 + spin_unlock_irq(&rq->lock);
6613 + move_task_off_dead_cpu(dead_cpu, p);
6614 + spin_lock_irq(&rq->lock);
6616 + put_task_struct(p);
6619 +/* release_task() removes task from tasklist, so we won't find dead tasks. */
6620 +static void migrate_dead_tasks(unsigned int dead_cpu)
6622 + struct rq *rq = cpu_rq(dead_cpu);
6623 + struct task_struct *next;
6626 + if (!rq->nr_running)
6628 + update_rq_clock(rq);
6629 + next = pick_next_task(rq, rq->curr);
6632 + next->sched_class->put_prev_task(rq, next);
6633 + migrate_dead(dead_cpu, next);
6637 +#endif /* CONFIG_HOTPLUG_CPU */
6639 +#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6641 +static struct ctl_table sd_ctl_dir[] = {
6643 + .procname = "sched_domain",
6649 +static struct ctl_table sd_ctl_root[] = {
6651 + .ctl_name = CTL_KERN,
6652 + .procname = "kernel",
6654 + .child = sd_ctl_dir,
6659 +static struct ctl_table *sd_alloc_ctl_entry(int n)
6661 + struct ctl_table *entry =
6662 + kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
6667 +static void sd_free_ctl_entry(struct ctl_table **tablep)
6669 + struct ctl_table *entry;
6672 + * In the intermediate directories, both the child directory and
6673 + * procname are dynamically allocated and could fail but the mode
6674 + * will always be set. In the lowest directory the names are
6675 + * static strings and all have proc handlers.
6677 + for (entry = *tablep; entry->mode; entry++) {
6679 + sd_free_ctl_entry(&entry->child);
6680 + if (entry->proc_handler == NULL)
6681 + kfree(entry->procname);
6689 +set_table_entry(struct ctl_table *entry,
6690 + const char *procname, void *data, int maxlen,
6691 + mode_t mode, proc_handler *proc_handler)
6693 + entry->procname = procname;
6694 + entry->data = data;
6695 + entry->maxlen = maxlen;
6696 + entry->mode = mode;
6697 + entry->proc_handler = proc_handler;
6700 +static struct ctl_table *
6701 +sd_alloc_ctl_domain_table(struct sched_domain *sd)
6703 + struct ctl_table *table = sd_alloc_ctl_entry(12);
6705 + if (table == NULL)
6708 + set_table_entry(&table[0], "min_interval", &sd->min_interval,
6709 + sizeof(long), 0644, proc_doulongvec_minmax);
6710 + set_table_entry(&table[1], "max_interval", &sd->max_interval,
6711 + sizeof(long), 0644, proc_doulongvec_minmax);
6712 + set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
6713 + sizeof(int), 0644, proc_dointvec_minmax);
6714 + set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
6715 + sizeof(int), 0644, proc_dointvec_minmax);
6716 + set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
6717 + sizeof(int), 0644, proc_dointvec_minmax);
6718 + set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
6719 + sizeof(int), 0644, proc_dointvec_minmax);
6720 + set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
6721 + sizeof(int), 0644, proc_dointvec_minmax);
6722 + set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
6723 + sizeof(int), 0644, proc_dointvec_minmax);
6724 + set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
6725 + sizeof(int), 0644, proc_dointvec_minmax);
6726 + set_table_entry(&table[9], "cache_nice_tries",
6727 + &sd->cache_nice_tries,
6728 + sizeof(int), 0644, proc_dointvec_minmax);
6729 + set_table_entry(&table[10], "flags", &sd->flags,
6730 + sizeof(int), 0644, proc_dointvec_minmax);
6731 + /* &table[11] is terminator */
6736 +static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
6738 + struct ctl_table *entry, *table;
6739 + struct sched_domain *sd;
6740 + int domain_num = 0, i;
6743 + for_each_domain(cpu, sd)
6745 + entry = table = sd_alloc_ctl_entry(domain_num + 1);
6746 + if (table == NULL)
6750 + for_each_domain(cpu, sd) {
6751 + snprintf(buf, 32, "domain%d", i);
6752 + entry->procname = kstrdup(buf, GFP_KERNEL);
6753 + entry->mode = 0555;
6754 + entry->child = sd_alloc_ctl_domain_table(sd);
6761 +static struct ctl_table_header *sd_sysctl_header;
6762 +static void register_sched_domain_sysctl(void)
6764 + int i, cpu_num = num_online_cpus();
6765 + struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6768 + WARN_ON(sd_ctl_dir[0].child);
6769 + sd_ctl_dir[0].child = entry;
6771 + if (entry == NULL)
6774 + for_each_online_cpu(i) {
6775 + snprintf(buf, 32, "cpu%d", i);
6776 + entry->procname = kstrdup(buf, GFP_KERNEL);
6777 + entry->mode = 0555;
6778 + entry->child = sd_alloc_ctl_cpu_table(i);
6782 + WARN_ON(sd_sysctl_header);
6783 + sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6786 +/* may be called multiple times per register */
6787 +static void unregister_sched_domain_sysctl(void)
6789 + if (sd_sysctl_header)
6790 + unregister_sysctl_table(sd_sysctl_header);
6791 + sd_sysctl_header = NULL;
6792 + if (sd_ctl_dir[0].child)
6793 + sd_free_ctl_entry(&sd_ctl_dir[0].child);
6796 +static void register_sched_domain_sysctl(void)
6799 +static void unregister_sched_domain_sysctl(void)
6804 +static void set_rq_online(struct rq *rq)
6806 + if (!rq->online) {
6807 + const struct sched_class *class;
6809 + cpu_set(rq->cpu, rq->rd->online);
6812 + for_each_class(class) {
6813 + if (class->rq_online)
6814 + class->rq_online(rq);
6819 +static void set_rq_offline(struct rq *rq)
6822 + const struct sched_class *class;
6824 + for_each_class(class) {
6825 + if (class->rq_offline)
6826 + class->rq_offline(rq);
6829 + cpu_clear(rq->cpu, rq->rd->online);
6835 + * migration_call - callback that gets triggered when a CPU is added.
6836 + * Here we can start up the necessary migration thread for the new CPU.
6838 +static int __cpuinit
6839 +migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
6841 + struct task_struct *p;
6842 + int cpu = (long)hcpu;
6843 + unsigned long flags;
6848 + case CPU_UP_PREPARE:
6849 + case CPU_UP_PREPARE_FROZEN:
6850 + p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
6852 + return NOTIFY_BAD;
6853 + kthread_bind(p, cpu);
6854 + /* Must be high prio: stop_machine expects to yield to it. */
6855 + rq = task_rq_lock(p, &flags);
6856 + __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
6857 + task_rq_unlock(rq, &flags);
6858 + cpu_rq(cpu)->migration_thread = p;
6862 + case CPU_ONLINE_FROZEN:
6863 + /* Strictly unnecessary, as first user will wake it. */
6864 + wake_up_process(cpu_rq(cpu)->migration_thread);
6866 + /* Update our root-domain */
6868 + spin_lock_irqsave(&rq->lock, flags);
6870 + BUG_ON(!cpu_isset(cpu, rq->rd->span));
6872 + set_rq_online(rq);
6874 + spin_unlock_irqrestore(&rq->lock, flags);
6877 +#ifdef CONFIG_HOTPLUG_CPU
6878 + case CPU_UP_CANCELED:
6879 + case CPU_UP_CANCELED_FROZEN:
6880 + if (!cpu_rq(cpu)->migration_thread)
6882 + /* Unbind it from offline cpu so it can run. Fall thru. */
6883 + kthread_bind(cpu_rq(cpu)->migration_thread,
6884 + any_online_cpu(cpu_online_map));
6885 + kthread_stop(cpu_rq(cpu)->migration_thread);
6886 + cpu_rq(cpu)->migration_thread = NULL;
6890 + case CPU_DEAD_FROZEN:
6891 + cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
6892 + migrate_live_tasks(cpu);
6894 + kthread_stop(rq->migration_thread);
6895 + rq->migration_thread = NULL;
6896 + /* Idle task back to normal (off runqueue, low prio) */
6897 + spin_lock_irq(&rq->lock);
6898 + update_rq_clock(rq);
6899 + deactivate_task(rq, rq->idle, 0);
6900 + rq->idle->static_prio = MAX_PRIO;
6901 + __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6902 + rq->idle->sched_class = &idle_sched_class;
6903 + migrate_dead_tasks(cpu);
6904 + spin_unlock_irq(&rq->lock);
6906 + migrate_nr_uninterruptible(rq);
6907 + BUG_ON(rq->nr_running != 0);
6910 + * No need to migrate the tasks: it was best-effort if
6911 + * they didn't take sched_hotcpu_mutex. Just wake up
6914 + spin_lock_irq(&rq->lock);
6915 + while (!list_empty(&rq->migration_queue)) {
6916 + struct migration_req *req;
6918 + req = list_entry(rq->migration_queue.next,
6919 + struct migration_req, list);
6920 + list_del_init(&req->list);
6921 + spin_unlock_irq(&rq->lock);
6922 + complete(&req->done);
6923 + spin_lock_irq(&rq->lock);
6925 + spin_unlock_irq(&rq->lock);
6929 + case CPU_DYING_FROZEN:
6930 + /* Update our root-domain */
6932 + spin_lock_irqsave(&rq->lock, flags);
6934 + BUG_ON(!cpu_isset(cpu, rq->rd->span));
6935 + set_rq_offline(rq);
6937 + spin_unlock_irqrestore(&rq->lock, flags);
6944 +/* Register at highest priority so that task migration (migrate_all_tasks)
6945 + * happens before everything else.
6947 +static struct notifier_block __cpuinitdata migration_notifier = {
6948 + .notifier_call = migration_call,
6952 +static int __init migration_init(void)
6954 + void *cpu = (void *)(long)smp_processor_id();
6957 + /* Start one for the boot CPU: */
6958 + err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6959 + BUG_ON(err == NOTIFY_BAD);
6960 + migration_call(&migration_notifier, CPU_ONLINE, cpu);
6961 + register_cpu_notifier(&migration_notifier);
6965 +early_initcall(migration_init);
6970 +#ifdef CONFIG_SCHED_DEBUG
6972 +static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6977 + case SD_LV_SIBLING:
6985 + case SD_LV_ALLNODES:
6986 + return "ALLNODES";
6994 +static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6995 + cpumask_t *groupmask)
6997 + struct sched_group *group = sd->groups;
7000 + cpulist_scnprintf(str, sizeof(str), sd->span);
7001 + cpus_clear(*groupmask);
7003 + printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
7005 + if (!(sd->flags & SD_LOAD_BALANCE)) {
7006 + printk("does not load-balance\n");
7008 + printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
7013 + printk(KERN_CONT "span %s level %s\n",
7014 + str, sd_level_to_string(sd->level));
7016 + if (!cpu_isset(cpu, sd->span)) {
7017 + printk(KERN_ERR "ERROR: domain->span does not contain "
7020 + if (!cpu_isset(cpu, group->cpumask)) {
7021 + printk(KERN_ERR "ERROR: domain->groups does not contain"
7025 + printk(KERN_DEBUG "%*s groups:", level + 1, "");
7029 + printk(KERN_ERR "ERROR: group is NULL\n");
7033 + if (!group->__cpu_power) {
7034 + printk(KERN_CONT "\n");
7035 + printk(KERN_ERR "ERROR: domain->cpu_power not "
7040 + if (!cpus_weight(group->cpumask)) {
7041 + printk(KERN_CONT "\n");
7042 + printk(KERN_ERR "ERROR: empty group\n");
7046 + if (cpus_intersects(*groupmask, group->cpumask)) {
7047 + printk(KERN_CONT "\n");
7048 + printk(KERN_ERR "ERROR: repeated CPUs\n");
7052 + cpus_or(*groupmask, *groupmask, group->cpumask);
7054 + cpulist_scnprintf(str, sizeof(str), group->cpumask);
7055 + printk(KERN_CONT " %s", str);
7057 + group = group->next;
7058 + } while (group != sd->groups);
7059 + printk(KERN_CONT "\n");
7061 + if (!cpus_equal(sd->span, *groupmask))
7062 + printk(KERN_ERR "ERROR: groups don't span domain->span\n");
7064 + if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
7065 + printk(KERN_ERR "ERROR: parent span is not a superset "
7066 + "of domain->span\n");
7070 +static void sched_domain_debug(struct sched_domain *sd, int cpu)
7072 + cpumask_t *groupmask;
7076 + printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
7080 + printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
7082 + groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7084 + printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
7089 + if (sched_domain_debug_one(sd, cpu, level, groupmask))
7098 +#else /* !CONFIG_SCHED_DEBUG */
7099 +# define sched_domain_debug(sd, cpu) do { } while (0)
7100 +#endif /* CONFIG_SCHED_DEBUG */
7102 +static int sd_degenerate(struct sched_domain *sd)
7104 + if (cpus_weight(sd->span) == 1)
7107 + /* Following flags need at least 2 groups */
7108 + if (sd->flags & (SD_LOAD_BALANCE |
7109 + SD_BALANCE_NEWIDLE |
7112 + SD_SHARE_CPUPOWER |
7113 + SD_SHARE_PKG_RESOURCES)) {
7114 + if (sd->groups != sd->groups->next)
7118 + /* Following flags don't use groups */
7119 + if (sd->flags & (SD_WAKE_IDLE |
7128 +sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
7130 + unsigned long cflags = sd->flags, pflags = parent->flags;
7132 + if (sd_degenerate(parent))
7135 + if (!cpus_equal(sd->span, parent->span))
7138 + /* Does parent contain flags not in child? */
7139 + /* WAKE_BALANCE is a subset of WAKE_AFFINE */
7140 + if (cflags & SD_WAKE_AFFINE)
7141 + pflags &= ~SD_WAKE_BALANCE;
7142 + /* Flags needing groups don't count if only 1 group in parent */
7143 + if (parent->groups == parent->groups->next) {
7144 + pflags &= ~(SD_LOAD_BALANCE |
7145 + SD_BALANCE_NEWIDLE |
7148 + SD_SHARE_CPUPOWER |
7149 + SD_SHARE_PKG_RESOURCES);
7151 + if (~cflags & pflags)
7157 +static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7159 + unsigned long flags;
7161 + spin_lock_irqsave(&rq->lock, flags);
7164 + struct root_domain *old_rd = rq->rd;
7166 + if (cpu_isset(rq->cpu, old_rd->online))
7167 + set_rq_offline(rq);
7169 + cpu_clear(rq->cpu, old_rd->span);
7171 + if (atomic_dec_and_test(&old_rd->refcount))
7175 + atomic_inc(&rd->refcount);
7178 + cpu_set(rq->cpu, rd->span);
7179 + if (cpu_isset(rq->cpu, cpu_online_map))
7180 + set_rq_online(rq);
7182 + spin_unlock_irqrestore(&rq->lock, flags);
7185 +static void init_rootdomain(struct root_domain *rd)
7187 + memset(rd, 0, sizeof(*rd));
7189 + cpus_clear(rd->span);
7190 + cpus_clear(rd->online);
7192 + cpupri_init(&rd->cpupri);
7195 +static void init_defrootdomain(void)
7197 + init_rootdomain(&def_root_domain);
7198 + atomic_set(&def_root_domain.refcount, 1);
7201 +static struct root_domain *alloc_rootdomain(void)
7203 + struct root_domain *rd;
7205 + rd = kmalloc(sizeof(*rd), GFP_KERNEL);
7209 + init_rootdomain(rd);
7215 + * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
7216 + * hold the hotplug lock.
7219 +cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
7221 + struct rq *rq = cpu_rq(cpu);
7222 + struct sched_domain *tmp;
7224 + /* Remove the sched domains which do not contribute to scheduling. */
7225 + for (tmp = sd; tmp; ) {
7226 + struct sched_domain *parent = tmp->parent;
7230 + if (sd_parent_degenerate(tmp, parent)) {
7231 + tmp->parent = parent->parent;
7232 + if (parent->parent)
7233 + parent->parent->child = tmp;
7235 + tmp = tmp->parent;
7238 + if (sd && sd_degenerate(sd)) {
7244 + sched_domain_debug(sd, cpu);
7246 + rq_attach_root(rq, rd);
7247 + rcu_assign_pointer(rq->sd, sd);
7250 +/* cpus with isolated domains */
7251 +static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
7253 +/* Setup the mask of cpus configured for isolated domains */
7254 +static int __init isolated_cpu_setup(char *str)
7256 + static int __initdata ints[NR_CPUS];
7259 + str = get_options(str, ARRAY_SIZE(ints), ints);
7260 + cpus_clear(cpu_isolated_map);
7261 + for (i = 1; i <= ints[0]; i++)
7262 + if (ints[i] < NR_CPUS)
7263 + cpu_set(ints[i], cpu_isolated_map);
7267 +__setup("isolcpus=", isolated_cpu_setup);
7270 + * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7271 + * to a function which identifies what group(along with sched group) a CPU
7272 + * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
7273 + * (due to the fact that we keep track of groups covered with a cpumask_t).
7275 + * init_sched_build_groups will build a circular linked list of the groups
7276 + * covered by the given span, and will set each group's ->cpumask correctly,
7277 + * and ->cpu_power to 0.
7280 +init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
7281 + int (*group_fn)(int cpu, const cpumask_t *cpu_map,
7282 + struct sched_group **sg,
7283 + cpumask_t *tmpmask),
7284 + cpumask_t *covered, cpumask_t *tmpmask)
7286 + struct sched_group *first = NULL, *last = NULL;
7289 + cpus_clear(*covered);
7291 + for_each_cpu_mask_nr(i, *span) {
7292 + struct sched_group *sg;
7293 + int group = group_fn(i, cpu_map, &sg, tmpmask);
7296 + if (cpu_isset(i, *covered))
7299 + cpus_clear(sg->cpumask);
7300 + sg->__cpu_power = 0;
7302 + for_each_cpu_mask_nr(j, *span) {
7303 + if (group_fn(j, cpu_map, NULL, tmpmask) != group)
7306 + cpu_set(j, *covered);
7307 + cpu_set(j, sg->cpumask);
7315 + last->next = first;
7318 +#define SD_NODES_PER_DOMAIN 16
7323 + * find_next_best_node - find the next node to include in a sched_domain
7324 + * @node: node whose sched_domain we're building
7325 + * @used_nodes: nodes already in the sched_domain
7327 + * Find the next node to include in a given scheduling domain. Simply
7328 + * finds the closest node not already in the @used_nodes map.
7330 + * Should use nodemask_t.
7332 +static int find_next_best_node(int node, nodemask_t *used_nodes)
7334 + int i, n, val, min_val, best_node = 0;
7336 + min_val = INT_MAX;
7338 + for (i = 0; i < nr_node_ids; i++) {
7339 + /* Start at @node */
7340 + n = (node + i) % nr_node_ids;
7342 + if (!nr_cpus_node(n))
7345 + /* Skip already used nodes */
7346 + if (node_isset(n, *used_nodes))
7349 + /* Simple min distance search */
7350 + val = node_distance(node, n);
7352 + if (val < min_val) {
7358 + node_set(best_node, *used_nodes);
7363 + * sched_domain_node_span - get a cpumask for a node's sched_domain
7364 + * @node: node whose cpumask we're constructing
7365 + * @span: resulting cpumask
7367 + * Given a node, construct a good cpumask for its sched_domain to span. It
7368 + * should be one that prevents unnecessary balancing, but also spreads tasks
7371 +static void sched_domain_node_span(int node, cpumask_t *span)
7373 + nodemask_t used_nodes;
7374 + node_to_cpumask_ptr(nodemask, node);
7377 + cpus_clear(*span);
7378 + nodes_clear(used_nodes);
7380 + cpus_or(*span, *span, *nodemask);
7381 + node_set(node, used_nodes);
7383 + for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
7384 + int next_node = find_next_best_node(node, &used_nodes);
7386 + node_to_cpumask_ptr_next(nodemask, next_node);
7387 + cpus_or(*span, *span, *nodemask);
7390 +#endif /* CONFIG_NUMA */
7392 +int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
7395 + * SMT sched-domains:
7397 +#ifdef CONFIG_SCHED_SMT
7398 +static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
7399 +static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
7402 +cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7403 + cpumask_t *unused)
7406 + *sg = &per_cpu(sched_group_cpus, cpu);
7409 +#endif /* CONFIG_SCHED_SMT */
7412 + * multi-core sched-domains:
7414 +#ifdef CONFIG_SCHED_MC
7415 +static DEFINE_PER_CPU(struct sched_domain, core_domains);
7416 +static DEFINE_PER_CPU(struct sched_group, sched_group_core);
7417 +#endif /* CONFIG_SCHED_MC */
7419 +#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
7421 +cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7426 + *mask = per_cpu(cpu_sibling_map, cpu);
7427 + cpus_and(*mask, *mask, *cpu_map);
7428 + group = first_cpu(*mask);
7430 + *sg = &per_cpu(sched_group_core, group);
7433 +#elif defined(CONFIG_SCHED_MC)
7435 +cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7436 + cpumask_t *unused)
7439 + *sg = &per_cpu(sched_group_core, cpu);
7444 +static DEFINE_PER_CPU(struct sched_domain, phys_domains);
7445 +static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
7448 +cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7452 +#ifdef CONFIG_SCHED_MC
7453 + *mask = cpu_coregroup_map(cpu);
7454 + cpus_and(*mask, *mask, *cpu_map);
7455 + group = first_cpu(*mask);
7456 +#elif defined(CONFIG_SCHED_SMT)
7457 + *mask = per_cpu(cpu_sibling_map, cpu);
7458 + cpus_and(*mask, *mask, *cpu_map);
7459 + group = first_cpu(*mask);
7464 + *sg = &per_cpu(sched_group_phys, group);
7470 + * The init_sched_build_groups can't handle what we want to do with node
7471 + * groups, so roll our own. Now each node has its own list of groups which
7472 + * gets dynamically allocated.
7474 +static DEFINE_PER_CPU(struct sched_domain, node_domains);
7475 +static struct sched_group ***sched_group_nodes_bycpu;
7477 +static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
7478 +static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
7480 +static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
7481 + struct sched_group **sg, cpumask_t *nodemask)
7485 + *nodemask = node_to_cpumask(cpu_to_node(cpu));
7486 + cpus_and(*nodemask, *nodemask, *cpu_map);
7487 + group = first_cpu(*nodemask);
7490 + *sg = &per_cpu(sched_group_allnodes, group);
7494 +static void init_numa_sched_groups_power(struct sched_group *group_head)
7496 + struct sched_group *sg = group_head;
7502 + for_each_cpu_mask_nr(j, sg->cpumask) {
7503 + struct sched_domain *sd;
7505 + sd = &per_cpu(phys_domains, j);
7506 + if (j != first_cpu(sd->groups->cpumask)) {
7508 + * Only add "power" once for each
7509 + * physical package.
7514 + sg_inc_cpu_power(sg, sd->groups->__cpu_power);
7517 + } while (sg != group_head);
7519 +#endif /* CONFIG_NUMA */
7522 +/* Free memory allocated for various sched_group structures */
7523 +static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
7527 + for_each_cpu_mask_nr(cpu, *cpu_map) {
7528 + struct sched_group **sched_group_nodes
7529 + = sched_group_nodes_bycpu[cpu];
7531 + if (!sched_group_nodes)
7534 + for (i = 0; i < nr_node_ids; i++) {
7535 + struct sched_group *oldsg, *sg = sched_group_nodes[i];
7537 + *nodemask = node_to_cpumask(i);
7538 + cpus_and(*nodemask, *nodemask, *cpu_map);
7539 + if (cpus_empty(*nodemask))
7549 + if (oldsg != sched_group_nodes[i])
7552 + kfree(sched_group_nodes);
7553 + sched_group_nodes_bycpu[cpu] = NULL;
7556 +#else /* !CONFIG_NUMA */
7557 +static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
7560 +#endif /* CONFIG_NUMA */
7563 + * Initialize sched groups cpu_power.
7565 + * cpu_power indicates the capacity of sched group, which is used while
7566 + * distributing the load between different sched groups in a sched domain.
7567 + * Typically cpu_power for all the groups in a sched domain will be same unless
7568 + * there are asymmetries in the topology. If there are asymmetries, group
7569 + * having more cpu_power will pickup more load compared to the group having
7572 + * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7573 + * the maximum number of tasks a group can handle in the presence of other idle
7574 + * or lightly loaded groups in the same sched domain.
7576 +static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7578 + struct sched_domain *child;
7579 + struct sched_group *group;
7581 + WARN_ON(!sd || !sd->groups);
7583 + if (cpu != first_cpu(sd->groups->cpumask))
7586 + child = sd->child;
7588 + sd->groups->__cpu_power = 0;
7591 + * For perf policy, if the groups in child domain share resources
7592 + * (for example cores sharing some portions of the cache hierarchy
7593 + * or SMT), then set this domain groups cpu_power such that each group
7594 + * can handle only one task, when there are other idle groups in the
7595 + * same sched domain.
7597 + if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7599 + (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
7600 + sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
7605 + * add cpu_power of each child group to this groups cpu_power
7607 + group = child->groups;
7609 + sg_inc_cpu_power(sd->groups, group->__cpu_power);
7610 + group = group->next;
7611 + } while (group != child->groups);
7615 + * Initializers for schedule domains
7616 + * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7619 +#define SD_INIT(sd, type) sd_init_##type(sd)
7620 +#define SD_INIT_FUNC(type) \
7621 +static noinline void sd_init_##type(struct sched_domain *sd) \
7623 + memset(sd, 0, sizeof(*sd)); \
7624 + *sd = SD_##type##_INIT; \
7625 + sd->level = SD_LV_##type; \
7630 + SD_INIT_FUNC(ALLNODES)
7631 + SD_INIT_FUNC(NODE)
7633 +#ifdef CONFIG_SCHED_SMT
7634 + SD_INIT_FUNC(SIBLING)
7636 +#ifdef CONFIG_SCHED_MC
7641 + * To minimize stack usage kmalloc room for cpumasks and share the
7642 + * space as the usage in build_sched_domains() dictates. Used only
7643 + * if the amount of space is significant.
7646 + cpumask_t tmpmask; /* make this one first */
7648 + cpumask_t nodemask;
7649 + cpumask_t this_sibling_map;
7650 + cpumask_t this_core_map;
7652 + cpumask_t send_covered;
7655 + cpumask_t domainspan;
7656 + cpumask_t covered;
7657 + cpumask_t notcovered;
7662 +#define SCHED_CPUMASK_ALLOC 1
7663 +#define SCHED_CPUMASK_FREE(v) kfree(v)
7664 +#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7666 +#define SCHED_CPUMASK_ALLOC 0
7667 +#define SCHED_CPUMASK_FREE(v)
7668 +#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7671 +#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7672 + ((unsigned long)(a) + offsetof(struct allmasks, v))
7674 +static int default_relax_domain_level = -1;
7676 +static int __init setup_relax_domain_level(char *str)
7678 + unsigned long val;
7680 + val = simple_strtoul(str, NULL, 0);
7681 + if (val < SD_LV_MAX)
7682 + default_relax_domain_level = val;
7686 +__setup("relax_domain_level=", setup_relax_domain_level);
7688 +static void set_domain_attribute(struct sched_domain *sd,
7689 + struct sched_domain_attr *attr)
7693 + if (!attr || attr->relax_domain_level < 0) {
7694 + if (default_relax_domain_level < 0)
7697 + request = default_relax_domain_level;
7699 + request = attr->relax_domain_level;
7700 + if (request < sd->level) {
7701 + /* turn off idle balance on this domain */
7702 + sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7704 + /* turn on idle balance on this domain */
7705 + sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7710 + * Build sched domains for a given set of cpus and attach the sched domains
7711 + * to the individual cpus
7713 +static int __build_sched_domains(const cpumask_t *cpu_map,
7714 + struct sched_domain_attr *attr)
7717 + struct root_domain *rd;
7718 + SCHED_CPUMASK_DECLARE(allmasks);
7719 + cpumask_t *tmpmask;
7721 + struct sched_group **sched_group_nodes = NULL;
7722 + int sd_allnodes = 0;
7725 + * Allocate the per-node list of sched groups
7727 + sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
7729 + if (!sched_group_nodes) {
7730 + printk(KERN_WARNING "Can not alloc sched group node list\n");
7735 + rd = alloc_rootdomain();
7737 + printk(KERN_WARNING "Cannot alloc root domain\n");
7739 + kfree(sched_group_nodes);
7744 +#if SCHED_CPUMASK_ALLOC
7745 + /* get space for all scratch cpumask variables */
7746 + allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7748 + printk(KERN_WARNING "Cannot alloc cpumask array\n");
7751 + kfree(sched_group_nodes);
7756 + tmpmask = (cpumask_t *)allmasks;
7760 + sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7764 + * Set up domains for cpus specified by the cpu_map.
7766 + for_each_cpu_mask_nr(i, *cpu_map) {
7767 + struct sched_domain *sd = NULL, *p;
7768 + SCHED_CPUMASK_VAR(nodemask, allmasks);
7770 + *nodemask = node_to_cpumask(cpu_to_node(i));
7771 + cpus_and(*nodemask, *nodemask, *cpu_map);
7774 + if (cpus_weight(*cpu_map) >
7775 + SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
7776 + sd = &per_cpu(allnodes_domains, i);
7777 + SD_INIT(sd, ALLNODES);
7778 + set_domain_attribute(sd, attr);
7779 + sd->span = *cpu_map;
7780 + cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
7786 + sd = &per_cpu(node_domains, i);
7787 + SD_INIT(sd, NODE);
7788 + set_domain_attribute(sd, attr);
7789 + sched_domain_node_span(cpu_to_node(i), &sd->span);
7793 + cpus_and(sd->span, sd->span, *cpu_map);
7797 + sd = &per_cpu(phys_domains, i);
7799 + set_domain_attribute(sd, attr);
7800 + sd->span = *nodemask;
7804 + cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
7806 +#ifdef CONFIG_SCHED_MC
7808 + sd = &per_cpu(core_domains, i);
7810 + set_domain_attribute(sd, attr);
7811 + sd->span = cpu_coregroup_map(i);
7812 + cpus_and(sd->span, sd->span, *cpu_map);
7815 + cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
7818 +#ifdef CONFIG_SCHED_SMT
7820 + sd = &per_cpu(cpu_domains, i);
7821 + SD_INIT(sd, SIBLING);
7822 + set_domain_attribute(sd, attr);
7823 + sd->span = per_cpu(cpu_sibling_map, i);
7824 + cpus_and(sd->span, sd->span, *cpu_map);
7827 + cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
7831 +#ifdef CONFIG_SCHED_SMT
7832 + /* Set up CPU (sibling) groups */
7833 + for_each_cpu_mask_nr(i, *cpu_map) {
7834 + SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7835 + SCHED_CPUMASK_VAR(send_covered, allmasks);
7837 + *this_sibling_map = per_cpu(cpu_sibling_map, i);
7838 + cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7839 + if (i != first_cpu(*this_sibling_map))
7842 + init_sched_build_groups(this_sibling_map, cpu_map,
7843 + &cpu_to_cpu_group,
7844 + send_covered, tmpmask);
7848 +#ifdef CONFIG_SCHED_MC
7849 + /* Set up multi-core groups */
7850 + for_each_cpu_mask_nr(i, *cpu_map) {
7851 + SCHED_CPUMASK_VAR(this_core_map, allmasks);
7852 + SCHED_CPUMASK_VAR(send_covered, allmasks);
7854 + *this_core_map = cpu_coregroup_map(i);
7855 + cpus_and(*this_core_map, *this_core_map, *cpu_map);
7856 + if (i != first_cpu(*this_core_map))
7859 + init_sched_build_groups(this_core_map, cpu_map,
7860 + &cpu_to_core_group,
7861 + send_covered, tmpmask);
7865 + /* Set up physical groups */
7866 + for (i = 0; i < nr_node_ids; i++) {
7867 + SCHED_CPUMASK_VAR(nodemask, allmasks);
7868 + SCHED_CPUMASK_VAR(send_covered, allmasks);
7870 + *nodemask = node_to_cpumask(i);
7871 + cpus_and(*nodemask, *nodemask, *cpu_map);
7872 + if (cpus_empty(*nodemask))
7875 + init_sched_build_groups(nodemask, cpu_map,
7876 + &cpu_to_phys_group,
7877 + send_covered, tmpmask);
7881 + /* Set up node groups */
7882 + if (sd_allnodes) {
7883 + SCHED_CPUMASK_VAR(send_covered, allmasks);
7885 + init_sched_build_groups(cpu_map, cpu_map,
7886 + &cpu_to_allnodes_group,
7887 + send_covered, tmpmask);
7890 + for (i = 0; i < nr_node_ids; i++) {
7891 + /* Set up node groups */
7892 + struct sched_group *sg, *prev;
7893 + SCHED_CPUMASK_VAR(nodemask, allmasks);
7894 + SCHED_CPUMASK_VAR(domainspan, allmasks);
7895 + SCHED_CPUMASK_VAR(covered, allmasks);
7898 + *nodemask = node_to_cpumask(i);
7899 + cpus_clear(*covered);
7901 + cpus_and(*nodemask, *nodemask, *cpu_map);
7902 + if (cpus_empty(*nodemask)) {
7903 + sched_group_nodes[i] = NULL;
7907 + sched_domain_node_span(i, domainspan);
7908 + cpus_and(*domainspan, *domainspan, *cpu_map);
7910 + sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
7912 + printk(KERN_WARNING "Can not alloc domain group for "
7916 + sched_group_nodes[i] = sg;
7917 + for_each_cpu_mask_nr(j, *nodemask) {
7918 + struct sched_domain *sd;
7920 + sd = &per_cpu(node_domains, j);
7923 + sg->__cpu_power = 0;
7924 + sg->cpumask = *nodemask;
7926 + cpus_or(*covered, *covered, *nodemask);
7929 + for (j = 0; j < nr_node_ids; j++) {
7930 + SCHED_CPUMASK_VAR(notcovered, allmasks);
7931 + int n = (i + j) % nr_node_ids;
7932 + node_to_cpumask_ptr(pnodemask, n);
7934 + cpus_complement(*notcovered, *covered);
7935 + cpus_and(*tmpmask, *notcovered, *cpu_map);
7936 + cpus_and(*tmpmask, *tmpmask, *domainspan);
7937 + if (cpus_empty(*tmpmask))
7940 + cpus_and(*tmpmask, *tmpmask, *pnodemask);
7941 + if (cpus_empty(*tmpmask))
7944 + sg = kmalloc_node(sizeof(struct sched_group),
7947 + printk(KERN_WARNING
7948 + "Can not alloc domain group for node %d\n", j);
7951 + sg->__cpu_power = 0;
7952 + sg->cpumask = *tmpmask;
7953 + sg->next = prev->next;
7954 + cpus_or(*covered, *covered, *tmpmask);
7961 + /* Calculate CPU power for physical packages and nodes */
7962 +#ifdef CONFIG_SCHED_SMT
7963 + for_each_cpu_mask_nr(i, *cpu_map) {
7964 + struct sched_domain *sd = &per_cpu(cpu_domains, i);
7966 + init_sched_groups_power(i, sd);
7969 +#ifdef CONFIG_SCHED_MC
7970 + for_each_cpu_mask_nr(i, *cpu_map) {
7971 + struct sched_domain *sd = &per_cpu(core_domains, i);
7973 + init_sched_groups_power(i, sd);
7977 + for_each_cpu_mask_nr(i, *cpu_map) {
7978 + struct sched_domain *sd = &per_cpu(phys_domains, i);
7980 + init_sched_groups_power(i, sd);
7984 + for (i = 0; i < nr_node_ids; i++)
7985 + init_numa_sched_groups_power(sched_group_nodes[i]);
7987 + if (sd_allnodes) {
7988 + struct sched_group *sg;
7990 + cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7992 + init_numa_sched_groups_power(sg);
7996 + /* Attach the domains */
7997 + for_each_cpu_mask_nr(i, *cpu_map) {
7998 + struct sched_domain *sd;
7999 +#ifdef CONFIG_SCHED_SMT
8000 + sd = &per_cpu(cpu_domains, i);
8001 +#elif defined(CONFIG_SCHED_MC)
8002 + sd = &per_cpu(core_domains, i);
8004 + sd = &per_cpu(phys_domains, i);
8006 + cpu_attach_domain(sd, rd, i);
8009 + SCHED_CPUMASK_FREE((void *)allmasks);
8014 + free_sched_groups(cpu_map, tmpmask);
8015 + SCHED_CPUMASK_FREE((void *)allmasks);
8020 +static int build_sched_domains(const cpumask_t *cpu_map)
8022 + return __build_sched_domains(cpu_map, NULL);
8025 +static cpumask_t *doms_cur; /* current sched domains */
8026 +static int ndoms_cur; /* number of sched domains in 'doms_cur' */
8027 +static struct sched_domain_attr *dattr_cur;
8028 + /* attribues of custom domains in 'doms_cur' */
8031 + * Special case: If a kmalloc of a doms_cur partition (array of
8032 + * cpumask_t) fails, then fallback to a single sched domain,
8033 + * as determined by the single cpumask_t fallback_doms.
8035 +static cpumask_t fallback_doms;
8037 +void __attribute__((weak)) arch_update_cpu_topology(void)
8042 + * Set up scheduler domains and groups. Callers must hold the hotplug lock.
8043 + * For now this just excludes isolated cpus, but could be used to
8044 + * exclude other special cases in the future.
8046 +static int arch_init_sched_domains(const cpumask_t *cpu_map)
8050 + arch_update_cpu_topology();
8052 + doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
8054 + doms_cur = &fallback_doms;
8055 + cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
8057 + err = build_sched_domains(doms_cur);
8058 + register_sched_domain_sysctl();
8063 +static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
8064 + cpumask_t *tmpmask)
8066 + free_sched_groups(cpu_map, tmpmask);
8070 + * Detach sched domains from a group of cpus specified in cpu_map
8071 + * These cpus will now be attached to the NULL domain
8073 +static void detach_destroy_domains(const cpumask_t *cpu_map)
8075 + cpumask_t tmpmask;
8078 + unregister_sched_domain_sysctl();
8080 + for_each_cpu_mask_nr(i, *cpu_map)
8081 + cpu_attach_domain(NULL, &def_root_domain, i);
8082 + synchronize_sched();
8083 + arch_destroy_sched_domains(cpu_map, &tmpmask);
8086 +/* handle null as "default" */
8087 +static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8088 + struct sched_domain_attr *new, int idx_new)
8090 + struct sched_domain_attr tmp;
8096 + tmp = SD_ATTR_INIT;
8097 + return !memcmp(cur ? (cur + idx_cur) : &tmp,
8098 + new ? (new + idx_new) : &tmp,
8099 + sizeof(struct sched_domain_attr));
8103 + * Partition sched domains as specified by the 'ndoms_new'
8104 + * cpumasks in the array doms_new[] of cpumasks. This compares
8105 + * doms_new[] to the current sched domain partitioning, doms_cur[].
8106 + * It destroys each deleted domain and builds each new domain.
8108 + * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
8109 + * The masks don't intersect (don't overlap.) We should setup one
8110 + * sched domain for each mask. CPUs not in any of the cpumasks will
8111 + * not be load balanced. If the same cpumask appears both in the
8112 + * current 'doms_cur' domains and in the new 'doms_new', we can leave
8115 + * The passed in 'doms_new' should be kmalloc'd. This routine takes
8116 + * ownership of it and will kfree it when done with it. If the caller
8117 + * failed the kmalloc call, then it can pass in doms_new == NULL &&
8118 + * ndoms_new == 1, and partition_sched_domains() will fallback to
8119 + * the single partition 'fallback_doms', it also forces the domains
8122 + * If doms_new == NULL it will be replaced with cpu_online_map.
8123 + * ndoms_new == 0 is a special case for destroying existing domains,
8124 + * and it will not create the default domain.
8126 + * Call with hotplug lock held
8128 +void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
8129 + struct sched_domain_attr *dattr_new)
8133 + mutex_lock(&sched_domains_mutex);
8135 + /* always unregister in case we don't destroy any domains */
8136 + unregister_sched_domain_sysctl();
8138 + n = doms_new ? ndoms_new : 0;
8140 + /* Destroy deleted domains */
8141 + for (i = 0; i < ndoms_cur; i++) {
8142 + for (j = 0; j < n; j++) {
8143 + if (cpus_equal(doms_cur[i], doms_new[j])
8144 + && dattrs_equal(dattr_cur, i, dattr_new, j))
8147 + /* no match - a current sched domain not in new doms_new[] */
8148 + detach_destroy_domains(doms_cur + i);
8153 + if (doms_new == NULL) {
8155 + doms_new = &fallback_doms;
8156 + cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
8160 + /* Build new domains */
8161 + for (i = 0; i < ndoms_new; i++) {
8162 + for (j = 0; j < ndoms_cur; j++) {
8163 + if (cpus_equal(doms_new[i], doms_cur[j])
8164 + && dattrs_equal(dattr_new, i, dattr_cur, j))
8167 + /* no match - add a new doms_new */
8168 + __build_sched_domains(doms_new + i,
8169 + dattr_new ? dattr_new + i : NULL);
8174 + /* Remember the new sched domains */
8175 + if (doms_cur != &fallback_doms)
8177 + kfree(dattr_cur); /* kfree(NULL) is safe */
8178 + doms_cur = doms_new;
8179 + dattr_cur = dattr_new;
8180 + ndoms_cur = ndoms_new;
8182 + register_sched_domain_sysctl();
8184 + mutex_unlock(&sched_domains_mutex);
8187 +#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
8188 +int arch_reinit_sched_domains(void)
8190 + get_online_cpus();
8192 + /* Destroy domains first to force the rebuild */
8193 + partition_sched_domains(0, NULL, NULL);
8195 + rebuild_sched_domains();
8196 + put_online_cpus();
8201 +static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
8205 + if (buf[0] != '0' && buf[0] != '1')
8209 + sched_smt_power_savings = (buf[0] == '1');
8211 + sched_mc_power_savings = (buf[0] == '1');
8213 + ret = arch_reinit_sched_domains();
8215 + return ret ? ret : count;
8218 +#ifdef CONFIG_SCHED_MC
8219 +static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8222 + return sprintf(page, "%u\n", sched_mc_power_savings);
8224 +static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
8225 + const char *buf, size_t count)
8227 + return sched_power_savings_store(buf, count, 0);
8229 +static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8230 + sched_mc_power_savings_show,
8231 + sched_mc_power_savings_store);
8234 +#ifdef CONFIG_SCHED_SMT
8235 +static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8238 + return sprintf(page, "%u\n", sched_smt_power_savings);
8240 +static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
8241 + const char *buf, size_t count)
8243 + return sched_power_savings_store(buf, count, 1);
8245 +static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8246 + sched_smt_power_savings_show,
8247 + sched_smt_power_savings_store);
8250 +int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
8254 +#ifdef CONFIG_SCHED_SMT
8255 + if (smt_capable())
8256 + err = sysfs_create_file(&cls->kset.kobj,
8257 + &attr_sched_smt_power_savings.attr);
8259 +#ifdef CONFIG_SCHED_MC
8260 + if (!err && mc_capable())
8261 + err = sysfs_create_file(&cls->kset.kobj,
8262 + &attr_sched_mc_power_savings.attr);
8266 +#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
8268 +#ifndef CONFIG_CPUSETS
8270 + * Add online and remove offline CPUs from the scheduler domains.
8271 + * When cpusets are enabled they take over this function.
8273 +static int update_sched_domains(struct notifier_block *nfb,
8274 + unsigned long action, void *hcpu)
8278 + case CPU_ONLINE_FROZEN:
8280 + case CPU_DEAD_FROZEN:
8281 + partition_sched_domains(1, NULL, NULL);
8285 + return NOTIFY_DONE;
8290 +static int update_runtime(struct notifier_block *nfb,
8291 + unsigned long action, void *hcpu)
8293 + int cpu = (int)(long)hcpu;
8296 + case CPU_DOWN_PREPARE:
8297 + case CPU_DOWN_PREPARE_FROZEN:
8298 + disable_runtime(cpu_rq(cpu));
8301 + case CPU_DOWN_FAILED:
8302 + case CPU_DOWN_FAILED_FROZEN:
8304 + case CPU_ONLINE_FROZEN:
8305 + enable_runtime(cpu_rq(cpu));
8309 + return NOTIFY_DONE;
8313 +void __init sched_init_smp(void)
8315 + cpumask_t non_isolated_cpus;
8317 +#if defined(CONFIG_NUMA)
8318 + sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8320 + BUG_ON(sched_group_nodes_bycpu == NULL);
8322 + get_online_cpus();
8323 + mutex_lock(&sched_domains_mutex);
8324 + arch_init_sched_domains(&cpu_online_map);
8325 + cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
8326 + if (cpus_empty(non_isolated_cpus))
8327 + cpu_set(smp_processor_id(), non_isolated_cpus);
8328 + mutex_unlock(&sched_domains_mutex);
8329 + put_online_cpus();
8331 +#ifndef CONFIG_CPUSETS
8332 + /* XXX: Theoretical race here - CPU may be hotplugged now */
8333 + hotcpu_notifier(update_sched_domains, 0);
8336 + /* RT runtime code needs to handle some hotplug events */
8337 + hotcpu_notifier(update_runtime, 0);
8341 + /* Move init over to a non-isolated CPU */
8342 + if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
8344 + sched_init_granularity();
8347 +void __init sched_init_smp(void)
8349 + sched_init_granularity();
8351 +#endif /* CONFIG_SMP */
8353 +int in_sched_functions(unsigned long addr)
8355 + return in_lock_functions(addr) ||
8356 + (addr >= (unsigned long)__sched_text_start
8357 + && addr < (unsigned long)__sched_text_end);
8360 +static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
8362 + cfs_rq->tasks_timeline = RB_ROOT;
8363 + INIT_LIST_HEAD(&cfs_rq->tasks);
8364 +#ifdef CONFIG_FAIR_GROUP_SCHED
8367 + cfs_rq->min_vruntime = (u64)(-(1LL << 20));
8370 +static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8372 + struct rt_prio_array *array;
8375 + array = &rt_rq->active;
8376 + for (i = 0; i < MAX_RT_PRIO; i++) {
8377 + INIT_LIST_HEAD(array->queue + i);
8378 + __clear_bit(i, array->bitmap);
8380 + /* delimiter for bitsearch: */
8381 + __set_bit(MAX_RT_PRIO, array->bitmap);
8383 +#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
8384 + rt_rq->highest_prio = MAX_RT_PRIO;
8387 + rt_rq->rt_nr_migratory = 0;
8388 + rt_rq->overloaded = 0;
8391 + rt_rq->rt_time = 0;
8392 + rt_rq->rt_throttled = 0;
8393 + rt_rq->rt_runtime = 0;
8394 + spin_lock_init(&rt_rq->rt_runtime_lock);
8396 +#ifdef CONFIG_RT_GROUP_SCHED
8397 + rt_rq->rt_nr_boosted = 0;
8402 +#ifdef CONFIG_FAIR_GROUP_SCHED
8403 +static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8404 + struct sched_entity *se, int cpu, int add,
8405 + struct sched_entity *parent)
8407 + struct rq *rq = cpu_rq(cpu);
8408 + tg->cfs_rq[cpu] = cfs_rq;
8409 + init_cfs_rq(cfs_rq, rq);
8412 + list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8415 + /* se could be NULL for init_task_group */
8420 + se->cfs_rq = &rq->cfs;
8422 + se->cfs_rq = parent->my_q;
8424 + se->my_q = cfs_rq;
8425 + se->load.weight = tg->shares;
8426 + se->load.inv_weight = 0;
8427 + se->parent = parent;
8431 +#ifdef CONFIG_RT_GROUP_SCHED
8432 +static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8433 + struct sched_rt_entity *rt_se, int cpu, int add,
8434 + struct sched_rt_entity *parent)
8436 + struct rq *rq = cpu_rq(cpu);
8438 + tg->rt_rq[cpu] = rt_rq;
8439 + init_rt_rq(rt_rq, rq);
8441 + rt_rq->rt_se = rt_se;
8442 + rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
8444 + list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8446 + tg->rt_se[cpu] = rt_se;
8451 + rt_se->rt_rq = &rq->rt;
8453 + rt_se->rt_rq = parent->my_q;
8455 + rt_se->my_q = rt_rq;
8456 + rt_se->parent = parent;
8457 + INIT_LIST_HEAD(&rt_se->run_list);
8461 +void __init sched_init(void)
8464 + unsigned long alloc_size = 0, ptr;
8466 +#ifdef CONFIG_FAIR_GROUP_SCHED
8467 + alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8469 +#ifdef CONFIG_RT_GROUP_SCHED
8470 + alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8472 +#ifdef CONFIG_USER_SCHED
8476 + * As sched_init() is called before page_alloc is setup,
8477 + * we use alloc_bootmem().
8480 + ptr = (unsigned long)alloc_bootmem(alloc_size);
8482 +#ifdef CONFIG_FAIR_GROUP_SCHED
8483 + init_task_group.se = (struct sched_entity **)ptr;
8484 + ptr += nr_cpu_ids * sizeof(void **);
8486 + init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8487 + ptr += nr_cpu_ids * sizeof(void **);
8489 +#ifdef CONFIG_USER_SCHED
8490 + root_task_group.se = (struct sched_entity **)ptr;
8491 + ptr += nr_cpu_ids * sizeof(void **);
8493 + root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8494 + ptr += nr_cpu_ids * sizeof(void **);
8495 +#endif /* CONFIG_USER_SCHED */
8496 +#endif /* CONFIG_FAIR_GROUP_SCHED */
8497 +#ifdef CONFIG_RT_GROUP_SCHED
8498 + init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8499 + ptr += nr_cpu_ids * sizeof(void **);
8501 + init_task_group.rt_rq = (struct rt_rq **)ptr;
8502 + ptr += nr_cpu_ids * sizeof(void **);
8504 +#ifdef CONFIG_USER_SCHED
8505 + root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8506 + ptr += nr_cpu_ids * sizeof(void **);
8508 + root_task_group.rt_rq = (struct rt_rq **)ptr;
8509 + ptr += nr_cpu_ids * sizeof(void **);
8510 +#endif /* CONFIG_USER_SCHED */
8511 +#endif /* CONFIG_RT_GROUP_SCHED */
8515 + init_defrootdomain();
8518 + init_rt_bandwidth(&def_rt_bandwidth,
8519 + global_rt_period(), global_rt_runtime());
8521 +#ifdef CONFIG_RT_GROUP_SCHED
8522 + init_rt_bandwidth(&init_task_group.rt_bandwidth,
8523 + global_rt_period(), global_rt_runtime());
8524 +#ifdef CONFIG_USER_SCHED
8525 + init_rt_bandwidth(&root_task_group.rt_bandwidth,
8526 + global_rt_period(), RUNTIME_INF);
8527 +#endif /* CONFIG_USER_SCHED */
8528 +#endif /* CONFIG_RT_GROUP_SCHED */
8530 +#ifdef CONFIG_GROUP_SCHED
8531 + list_add(&init_task_group.list, &task_groups);
8532 + INIT_LIST_HEAD(&init_task_group.children);
8534 +#ifdef CONFIG_USER_SCHED
8535 + INIT_LIST_HEAD(&root_task_group.children);
8536 + init_task_group.parent = &root_task_group;
8537 + list_add(&init_task_group.siblings, &root_task_group.children);
8538 +#endif /* CONFIG_USER_SCHED */
8539 +#endif /* CONFIG_GROUP_SCHED */
8541 + for_each_possible_cpu(i) {
8545 + spin_lock_init(&rq->lock);
8546 + rq->nr_running = 0;
8547 + init_cfs_rq(&rq->cfs, rq);
8548 + init_rt_rq(&rq->rt, rq);
8549 +#ifdef CONFIG_FAIR_GROUP_SCHED
8550 + init_task_group.shares = init_task_group_load;
8551 + INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
8552 +#ifdef CONFIG_CGROUP_SCHED
8554 + * How much cpu bandwidth does init_task_group get?
8556 + * In case of task-groups formed thr' the cgroup filesystem, it
8557 + * gets 100% of the cpu resources in the system. This overall
8558 + * system cpu resource is divided among the tasks of
8559 + * init_task_group and its child task-groups in a fair manner,
8560 + * based on each entity's (task or task-group's) weight
8561 + * (se->load.weight).
8563 + * In other words, if init_task_group has 10 tasks of weight
8564 + * 1024) and two child groups A0 and A1 (of weight 1024 each),
8565 + * then A0's share of the cpu resource is:
8567 + * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8569 + * We achieve this by letting init_task_group's tasks sit
8570 + * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8572 + init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
8573 +#elif defined CONFIG_USER_SCHED
8574 + root_task_group.shares = NICE_0_LOAD;
8575 + init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
8577 + * In case of task-groups formed thr' the user id of tasks,
8578 + * init_task_group represents tasks belonging to root user.
8579 + * Hence it forms a sibling of all subsequent groups formed.
8580 + * In this case, init_task_group gets only a fraction of overall
8581 + * system cpu resource, based on the weight assigned to root
8582 + * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8583 + * by letting tasks of init_task_group sit in a separate cfs_rq
8584 + * (init_cfs_rq) and having one entity represent this group of
8585 + * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8587 + init_tg_cfs_entry(&init_task_group,
8588 + &per_cpu(init_cfs_rq, i),
8589 + &per_cpu(init_sched_entity, i), i, 1,
8590 + root_task_group.se[i]);
8593 +#endif /* CONFIG_FAIR_GROUP_SCHED */
8595 + rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
8596 +#ifdef CONFIG_RT_GROUP_SCHED
8597 + INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
8598 +#ifdef CONFIG_CGROUP_SCHED
8599 + init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
8600 +#elif defined CONFIG_USER_SCHED
8601 + init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
8602 + init_tg_rt_entry(&init_task_group,
8603 + &per_cpu(init_rt_rq, i),
8604 + &per_cpu(init_sched_rt_entity, i), i, 1,
8605 + root_task_group.rt_se[i]);
8609 + for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8610 + rq->cpu_load[j] = 0;
8614 + rq->active_balance = 0;
8615 + rq->next_balance = jiffies;
8619 + rq->migration_thread = NULL;
8620 + INIT_LIST_HEAD(&rq->migration_queue);
8621 + rq_attach_root(rq, &def_root_domain);
8623 + init_rq_hrtick(rq);
8624 + atomic_set(&rq->nr_iowait, 0);
8627 + set_load_weight(&init_task);
8629 +#ifdef CONFIG_PREEMPT_NOTIFIERS
8630 + INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8634 + open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
8637 +#ifdef CONFIG_RT_MUTEXES
8638 + plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8642 + * The boot idle thread does lazy MMU switching as well:
8644 + atomic_inc(&init_mm.mm_count);
8645 + enter_lazy_tlb(&init_mm, current);
8648 + * Make us the idle thread. Technically, schedule() should not be
8649 + * called from this thread, however somewhere below it might be,
8650 + * but because we are the idle thread, we just pick up running again
8651 + * when this runqueue becomes "idle".
8653 + init_idle(current, smp_processor_id());
8655 + * During early bootup we pretend to be a normal task:
8657 + current->sched_class = &fair_sched_class;
8659 + scheduler_running = 1;
8662 +#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8663 +void __might_sleep(char *file, int line)
8666 + static unsigned long prev_jiffy; /* ratelimiting */
8668 + if ((in_atomic() || irqs_disabled()) &&
8669 + system_state == SYSTEM_RUNNING && !oops_in_progress) {
8670 + if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8672 + prev_jiffy = jiffies;
8673 + printk(KERN_ERR "BUG: sleeping function called from invalid"
8674 + " context at %s:%d\n", file, line);
8675 + printk("in_atomic():%d, irqs_disabled():%d\n",
8676 + in_atomic(), irqs_disabled());
8677 + debug_show_held_locks(current);
8678 + if (irqs_disabled())
8679 + print_irqtrace_events(current);
8684 +EXPORT_SYMBOL(__might_sleep);
8687 +#ifdef CONFIG_MAGIC_SYSRQ
8688 +static void normalize_task(struct rq *rq, struct task_struct *p)
8692 + update_rq_clock(rq);
8693 + on_rq = p->se.on_rq;
8695 + deactivate_task(rq, p, 0);
8696 + __setscheduler(rq, p, SCHED_NORMAL, 0);
8698 + activate_task(rq, p, 0);
8699 + resched_task(rq->curr);
8703 +void normalize_rt_tasks(void)
8705 + struct task_struct *g, *p;
8706 + unsigned long flags;
8709 + read_lock_irqsave(&tasklist_lock, flags);
8710 + do_each_thread(g, p) {
8712 + * Only normalize user tasks:
8717 + p->se.exec_start = 0;
8718 +#ifdef CONFIG_SCHEDSTATS
8719 + p->se.wait_start = 0;
8720 + p->se.sleep_start = 0;
8721 + p->se.block_start = 0;
8724 + if (!rt_task(p)) {
8726 + * Renice negative nice level userspace
8727 + * tasks back to 0:
8729 + if (TASK_NICE(p) < 0 && p->mm)
8730 + set_user_nice(p, 0);
8734 + spin_lock(&p->pi_lock);
8735 + rq = __task_rq_lock(p);
8737 + normalize_task(rq, p);
8739 + __task_rq_unlock(rq);
8740 + spin_unlock(&p->pi_lock);
8741 + } while_each_thread(g, p);
8743 + read_unlock_irqrestore(&tasklist_lock, flags);
8746 +#endif /* CONFIG_MAGIC_SYSRQ */
8750 + * These functions are only useful for the IA64 MCA handling.
8752 + * They can only be called when the whole system has been
8753 + * stopped - every CPU needs to be quiescent, and no scheduling
8754 + * activity can take place. Using them for anything else would
8755 + * be a serious bug, and as a result, they aren't even visible
8756 + * under any other configuration.
8760 + * curr_task - return the current task for a given cpu.
8761 + * @cpu: the processor in question.
8763 + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8765 +struct task_struct *curr_task(int cpu)
8767 + return cpu_curr(cpu);
8771 + * set_curr_task - set the current task for a given cpu.
8772 + * @cpu: the processor in question.
8773 + * @p: the task pointer to set.
8775 + * Description: This function must only be used when non-maskable interrupts
8776 + * are serviced on a separate stack. It allows the architecture to switch the
8777 + * notion of the current task on a cpu in a non-blocking manner. This function
8778 + * must be called with all CPU's synchronized, and interrupts disabled, the
8779 + * and caller must save the original value of the current task (see
8780 + * curr_task() above) and restore that value before reenabling interrupts and
8781 + * re-starting the system.
8783 + * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8785 +void set_curr_task(int cpu, struct task_struct *p)
8787 + cpu_curr(cpu) = p;
8792 +#ifdef CONFIG_FAIR_GROUP_SCHED
8793 +static void free_fair_sched_group(struct task_group *tg)
8797 + for_each_possible_cpu(i) {
8799 + kfree(tg->cfs_rq[i]);
8804 + kfree(tg->cfs_rq);
8809 +int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
8811 + struct cfs_rq *cfs_rq;
8812 + struct sched_entity *se, *parent_se;
8816 + tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
8819 + tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
8823 + tg->shares = NICE_0_LOAD;
8825 + for_each_possible_cpu(i) {
8828 + cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8829 + GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8833 + se = kmalloc_node(sizeof(struct sched_entity),
8834 + GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8838 + parent_se = parent ? parent->se[i] : NULL;
8839 + init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
8848 +static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8850 + list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8851 + &cpu_rq(cpu)->leaf_cfs_rq_list);
8854 +static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8856 + list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8858 +#else /* !CONFG_FAIR_GROUP_SCHED */
8859 +static inline void free_fair_sched_group(struct task_group *tg)
8864 +int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
8869 +static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8873 +static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8876 +#endif /* CONFIG_FAIR_GROUP_SCHED */
8878 +#ifdef CONFIG_RT_GROUP_SCHED
8879 +static void free_rt_sched_group(struct task_group *tg)
8883 + destroy_rt_bandwidth(&tg->rt_bandwidth);
8885 + for_each_possible_cpu(i) {
8887 + kfree(tg->rt_rq[i]);
8889 + kfree(tg->rt_se[i]);
8897 +int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
8899 + struct rt_rq *rt_rq;
8900 + struct sched_rt_entity *rt_se, *parent_se;
8904 + tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
8907 + tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
8911 + init_rt_bandwidth(&tg->rt_bandwidth,
8912 + ktime_to_ns(def_rt_bandwidth.rt_period), 0);
8914 + for_each_possible_cpu(i) {
8917 + rt_rq = kmalloc_node(sizeof(struct rt_rq),
8918 + GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8922 + rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8923 + GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8927 + parent_se = parent ? parent->rt_se[i] : NULL;
8928 + init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
8937 +static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8939 + list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8940 + &cpu_rq(cpu)->leaf_rt_rq_list);
8943 +static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8945 + list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8947 +#else /* !CONFIG_RT_GROUP_SCHED */
8948 +static inline void free_rt_sched_group(struct task_group *tg)
8953 +int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
8958 +static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8962 +static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8965 +#endif /* CONFIG_RT_GROUP_SCHED */
8967 +#ifdef CONFIG_GROUP_SCHED
8968 +static void free_sched_group(struct task_group *tg)
8970 + free_fair_sched_group(tg);
8971 + free_rt_sched_group(tg);
8975 +/* allocate runqueue etc for a new task group */
8976 +struct task_group *sched_create_group(struct task_group *parent)
8978 + struct task_group *tg;
8979 + unsigned long flags;
8982 + tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8984 + return ERR_PTR(-ENOMEM);
8986 + if (!alloc_fair_sched_group(tg, parent))
8989 + if (!alloc_rt_sched_group(tg, parent))
8992 + spin_lock_irqsave(&task_group_lock, flags);
8993 + for_each_possible_cpu(i) {
8994 + register_fair_sched_group(tg, i);
8995 + register_rt_sched_group(tg, i);
8997 + list_add_rcu(&tg->list, &task_groups);
8999 + WARN_ON(!parent); /* root should already exist */
9001 + tg->parent = parent;
9002 + INIT_LIST_HEAD(&tg->children);
9003 + list_add_rcu(&tg->siblings, &parent->children);
9004 + spin_unlock_irqrestore(&task_group_lock, flags);
9009 + free_sched_group(tg);
9010 + return ERR_PTR(-ENOMEM);
9013 +/* rcu callback to free various structures associated with a task group */
9014 +static void free_sched_group_rcu(struct rcu_head *rhp)
9016 + /* now it should be safe to free those cfs_rqs */
9017 + free_sched_group(container_of(rhp, struct task_group, rcu));
9020 +/* Destroy runqueue etc associated with a task group */
9021 +void sched_destroy_group(struct task_group *tg)
9023 + unsigned long flags;
9026 + spin_lock_irqsave(&task_group_lock, flags);
9027 + for_each_possible_cpu(i) {
9028 + unregister_fair_sched_group(tg, i);
9029 + unregister_rt_sched_group(tg, i);
9031 + list_del_rcu(&tg->list);
9032 + list_del_rcu(&tg->siblings);
9033 + spin_unlock_irqrestore(&task_group_lock, flags);
9035 + /* wait for possible concurrent references to cfs_rqs complete */
9036 + call_rcu(&tg->rcu, free_sched_group_rcu);
9039 +/* change task's runqueue when it moves between groups.
9040 + * The caller of this function should have put the task in its new group
9041 + * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
9042 + * reflect its new group.
9044 +void sched_move_task(struct task_struct *tsk)
9046 + int on_rq, running;
9047 + unsigned long flags;
9050 + rq = task_rq_lock(tsk, &flags);
9052 + update_rq_clock(rq);
9054 + running = task_current(rq, tsk);
9055 + on_rq = tsk->se.on_rq;
9058 + dequeue_task(rq, tsk, 0);
9059 + if (unlikely(running))
9060 + tsk->sched_class->put_prev_task(rq, tsk);
9062 + set_task_rq(tsk, task_cpu(tsk));
9064 +#ifdef CONFIG_FAIR_GROUP_SCHED
9065 + if (tsk->sched_class->moved_group)
9066 + tsk->sched_class->moved_group(tsk);
9069 + if (unlikely(running))
9070 + tsk->sched_class->set_curr_task(rq);
9072 + enqueue_task(rq, tsk, 0);
9074 + task_rq_unlock(rq, &flags);
9076 +#endif /* CONFIG_GROUP_SCHED */
9078 +#ifdef CONFIG_FAIR_GROUP_SCHED
9079 +static void __set_se_shares(struct sched_entity *se, unsigned long shares)
9081 + struct cfs_rq *cfs_rq = se->cfs_rq;
9084 + on_rq = se->on_rq;
9086 + dequeue_entity(cfs_rq, se, 0);
9088 + se->load.weight = shares;
9089 + se->load.inv_weight = 0;
9092 + enqueue_entity(cfs_rq, se, 0);
9095 +static void set_se_shares(struct sched_entity *se, unsigned long shares)
9097 + struct cfs_rq *cfs_rq = se->cfs_rq;
9098 + struct rq *rq = cfs_rq->rq;
9099 + unsigned long flags;
9101 + spin_lock_irqsave(&rq->lock, flags);
9102 + __set_se_shares(se, shares);
9103 + spin_unlock_irqrestore(&rq->lock, flags);
9106 +static DEFINE_MUTEX(shares_mutex);
9108 +int sched_group_set_shares(struct task_group *tg, unsigned long shares)
9111 + unsigned long flags;
9114 + * We can't change the weight of the root cgroup.
9119 + if (shares < MIN_SHARES)
9120 + shares = MIN_SHARES;
9121 + else if (shares > MAX_SHARES)
9122 + shares = MAX_SHARES;
9124 + mutex_lock(&shares_mutex);
9125 + if (tg->shares == shares)
9128 + spin_lock_irqsave(&task_group_lock, flags);
9129 + for_each_possible_cpu(i)
9130 + unregister_fair_sched_group(tg, i);
9131 + list_del_rcu(&tg->siblings);
9132 + spin_unlock_irqrestore(&task_group_lock, flags);
9134 + /* wait for any ongoing reference to this group to finish */
9135 + synchronize_sched();
9138 + * Now we are free to modify the group's share on each cpu
9139 + * w/o tripping rebalance_share or load_balance_fair.
9141 + tg->shares = shares;
9142 + for_each_possible_cpu(i) {
9144 + * force a rebalance
9146 + cfs_rq_set_shares(tg->cfs_rq[i], 0);
9147 + set_se_shares(tg->se[i], shares);
9151 + * Enable load balance activity on this group, by inserting it back on
9152 + * each cpu's rq->leaf_cfs_rq_list.
9154 + spin_lock_irqsave(&task_group_lock, flags);
9155 + for_each_possible_cpu(i)
9156 + register_fair_sched_group(tg, i);
9157 + list_add_rcu(&tg->siblings, &tg->parent->children);
9158 + spin_unlock_irqrestore(&task_group_lock, flags);
9160 + mutex_unlock(&shares_mutex);
9164 +unsigned long sched_group_shares(struct task_group *tg)
9166 + return tg->shares;
9170 +#ifdef CONFIG_RT_GROUP_SCHED
9172 + * Ensure that the real time constraints are schedulable.
9174 +static DEFINE_MUTEX(rt_constraints_mutex);
9176 +static unsigned long to_ratio(u64 period, u64 runtime)
9178 + if (runtime == RUNTIME_INF)
9179 + return 1ULL << 16;
9181 + return div64_u64(runtime << 16, period);
9184 +#ifdef CONFIG_CGROUP_SCHED
9185 +static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9187 + struct task_group *tgi, *parent = tg->parent;
9188 + unsigned long total = 0;
9191 + if (global_rt_period() < period)
9194 + return to_ratio(period, runtime) <
9195 + to_ratio(global_rt_period(), global_rt_runtime());
9198 + if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
9202 + list_for_each_entry_rcu(tgi, &parent->children, siblings) {
9206 + total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
9207 + tgi->rt_bandwidth.rt_runtime);
9209 + rcu_read_unlock();
9211 + return total + to_ratio(period, runtime) <=
9212 + to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
9213 + parent->rt_bandwidth.rt_runtime);
9215 +#elif defined CONFIG_USER_SCHED
9216 +static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9218 + struct task_group *tgi;
9219 + unsigned long total = 0;
9220 + unsigned long global_ratio =
9221 + to_ratio(global_rt_period(), global_rt_runtime());
9224 + list_for_each_entry_rcu(tgi, &task_groups, list) {
9228 + total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
9229 + tgi->rt_bandwidth.rt_runtime);
9231 + rcu_read_unlock();
9233 + return total + to_ratio(period, runtime) < global_ratio;
9237 +/* Must be called with tasklist_lock held */
9238 +static inline int tg_has_rt_tasks(struct task_group *tg)
9240 + struct task_struct *g, *p;
9241 + do_each_thread(g, p) {
9242 + if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
9244 + } while_each_thread(g, p);
9248 +static int tg_set_bandwidth(struct task_group *tg,
9249 + u64 rt_period, u64 rt_runtime)
9253 + mutex_lock(&rt_constraints_mutex);
9254 + read_lock(&tasklist_lock);
9255 + if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
9259 + if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
9264 + spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
9265 + tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9266 + tg->rt_bandwidth.rt_runtime = rt_runtime;
9268 + for_each_possible_cpu(i) {
9269 + struct rt_rq *rt_rq = tg->rt_rq[i];
9271 + spin_lock(&rt_rq->rt_runtime_lock);
9272 + rt_rq->rt_runtime = rt_runtime;
9273 + spin_unlock(&rt_rq->rt_runtime_lock);
9275 + spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
9277 + read_unlock(&tasklist_lock);
9278 + mutex_unlock(&rt_constraints_mutex);
9283 +int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9285 + u64 rt_runtime, rt_period;
9287 + rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9288 + rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9289 + if (rt_runtime_us < 0)
9290 + rt_runtime = RUNTIME_INF;
9292 + return tg_set_bandwidth(tg, rt_period, rt_runtime);
9295 +long sched_group_rt_runtime(struct task_group *tg)
9297 + u64 rt_runtime_us;
9299 + if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
9302 + rt_runtime_us = tg->rt_bandwidth.rt_runtime;
9303 + do_div(rt_runtime_us, NSEC_PER_USEC);
9304 + return rt_runtime_us;
9307 +int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9309 + u64 rt_runtime, rt_period;
9311 + rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9312 + rt_runtime = tg->rt_bandwidth.rt_runtime;
9314 + if (rt_period == 0)
9317 + return tg_set_bandwidth(tg, rt_period, rt_runtime);
9320 +long sched_group_rt_period(struct task_group *tg)
9324 + rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9325 + do_div(rt_period_us, NSEC_PER_USEC);
9326 + return rt_period_us;
9329 +static int sched_rt_global_constraints(void)
9331 + struct task_group *tg = &root_task_group;
9332 + u64 rt_runtime, rt_period;
9335 + if (sysctl_sched_rt_period <= 0)
9338 + rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9339 + rt_runtime = tg->rt_bandwidth.rt_runtime;
9341 + mutex_lock(&rt_constraints_mutex);
9342 + if (!__rt_schedulable(tg, rt_period, rt_runtime))
9344 + mutex_unlock(&rt_constraints_mutex);
9348 +#else /* !CONFIG_RT_GROUP_SCHED */
9349 +static int sched_rt_global_constraints(void)
9351 + unsigned long flags;
9354 + if (sysctl_sched_rt_period <= 0)
9357 + spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9358 + for_each_possible_cpu(i) {
9359 + struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9361 + spin_lock(&rt_rq->rt_runtime_lock);
9362 + rt_rq->rt_runtime = global_rt_runtime();
9363 + spin_unlock(&rt_rq->rt_runtime_lock);
9365 + spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9369 +#endif /* CONFIG_RT_GROUP_SCHED */
9371 +int sched_rt_handler(struct ctl_table *table, int write,
9372 + struct file *filp, void __user *buffer, size_t *lenp,
9376 + int old_period, old_runtime;
9377 + static DEFINE_MUTEX(mutex);
9379 + mutex_lock(&mutex);
9380 + old_period = sysctl_sched_rt_period;
9381 + old_runtime = sysctl_sched_rt_runtime;
9383 + ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9385 + if (!ret && write) {
9386 + ret = sched_rt_global_constraints();
9388 + sysctl_sched_rt_period = old_period;
9389 + sysctl_sched_rt_runtime = old_runtime;
9391 + def_rt_bandwidth.rt_runtime = global_rt_runtime();
9392 + def_rt_bandwidth.rt_period =
9393 + ns_to_ktime(global_rt_period());
9396 + mutex_unlock(&mutex);
9401 +#ifdef CONFIG_CGROUP_SCHED
9403 +/* return corresponding task_group object of a cgroup */
9404 +static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
9406 + return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9407 + struct task_group, css);
9410 +static struct cgroup_subsys_state *
9411 +cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
9413 + struct task_group *tg, *parent;
9415 + if (!cgrp->parent) {
9416 + /* This is early initialization for the top cgroup */
9417 + init_task_group.css.cgroup = cgrp;
9418 + return &init_task_group.css;
9421 + parent = cgroup_tg(cgrp->parent);
9422 + tg = sched_create_group(parent);
9424 + return ERR_PTR(-ENOMEM);
9426 + /* Bind the cgroup to task_group object we just created */
9427 + tg->css.cgroup = cgrp;
9433 +cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
9435 + struct task_group *tg = cgroup_tg(cgrp);
9437 + sched_destroy_group(tg);
9441 +cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9442 + struct task_struct *tsk)
9444 +#ifdef CONFIG_RT_GROUP_SCHED
9445 + /* Don't accept realtime tasks when there is no way for them to run */
9446 + if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
9449 + /* We don't support RT-tasks being in separate groups */
9450 + if (tsk->sched_class != &fair_sched_class)
9458 +cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9459 + struct cgroup *old_cont, struct task_struct *tsk)
9461 + sched_move_task(tsk);
9464 +#ifdef CONFIG_FAIR_GROUP_SCHED
9465 +static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
9468 + return sched_group_set_shares(cgroup_tg(cgrp), shareval);
9471 +static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
9473 + struct task_group *tg = cgroup_tg(cgrp);
9475 + return (u64) tg->shares;
9477 +#endif /* CONFIG_FAIR_GROUP_SCHED */
9479 +#ifdef CONFIG_RT_GROUP_SCHED
9480 +static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
9483 + return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
9486 +static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
9488 + return sched_group_rt_runtime(cgroup_tg(cgrp));
9491 +static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9494 + return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9497 +static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9499 + return sched_group_rt_period(cgroup_tg(cgrp));
9501 +#endif /* CONFIG_RT_GROUP_SCHED */
9503 +static struct cftype cpu_files[] = {
9504 +#ifdef CONFIG_FAIR_GROUP_SCHED
9507 + .read_u64 = cpu_shares_read_u64,
9508 + .write_u64 = cpu_shares_write_u64,
9511 +#ifdef CONFIG_RT_GROUP_SCHED
9513 + .name = "rt_runtime_us",
9514 + .read_s64 = cpu_rt_runtime_read,
9515 + .write_s64 = cpu_rt_runtime_write,
9518 + .name = "rt_period_us",
9519 + .read_u64 = cpu_rt_period_read_uint,
9520 + .write_u64 = cpu_rt_period_write_uint,
9525 +static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9527 + return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
9530 +struct cgroup_subsys cpu_cgroup_subsys = {
9532 + .create = cpu_cgroup_create,
9533 + .destroy = cpu_cgroup_destroy,
9534 + .can_attach = cpu_cgroup_can_attach,
9535 + .attach = cpu_cgroup_attach,
9536 + .populate = cpu_cgroup_populate,
9537 + .subsys_id = cpu_cgroup_subsys_id,
9541 +#endif /* CONFIG_CGROUP_SCHED */
9543 +#ifdef CONFIG_CGROUP_CPUACCT
9546 + * CPU accounting code for task groups.
9548 + * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9549 + * (balbir@in.ibm.com).
9552 +/* track cpu usage of a group of tasks */
9554 + struct cgroup_subsys_state css;
9555 + /* cpuusage holds pointer to a u64-type object on every cpu */
9559 +struct cgroup_subsys cpuacct_subsys;
9561 +/* return cpu accounting group corresponding to this container */
9562 +static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
9564 + return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
9565 + struct cpuacct, css);
9568 +/* return cpu accounting group to which this task belongs */
9569 +static inline struct cpuacct *task_ca(struct task_struct *tsk)
9571 + return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9572 + struct cpuacct, css);
9575 +/* create a new cpu accounting group */
9576 +static struct cgroup_subsys_state *cpuacct_create(
9577 + struct cgroup_subsys *ss, struct cgroup *cgrp)
9579 + struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9582 + return ERR_PTR(-ENOMEM);
9584 + ca->cpuusage = alloc_percpu(u64);
9585 + if (!ca->cpuusage) {
9587 + return ERR_PTR(-ENOMEM);
9593 +/* destroy an existing cpu accounting group */
9595 +cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
9597 + struct cpuacct *ca = cgroup_ca(cgrp);
9599 + free_percpu(ca->cpuusage);
9603 +/* return total cpu usage (in nanoseconds) of a group */
9604 +static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
9606 + struct cpuacct *ca = cgroup_ca(cgrp);
9607 + u64 totalcpuusage = 0;
9610 + for_each_possible_cpu(i) {
9611 + u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9614 + * Take rq->lock to make 64-bit addition safe on 32-bit
9617 + spin_lock_irq(&cpu_rq(i)->lock);
9618 + totalcpuusage += *cpuusage;
9619 + spin_unlock_irq(&cpu_rq(i)->lock);
9622 + return totalcpuusage;
9625 +static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9628 + struct cpuacct *ca = cgroup_ca(cgrp);
9637 + for_each_possible_cpu(i) {
9638 + u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9640 + spin_lock_irq(&cpu_rq(i)->lock);
9642 + spin_unlock_irq(&cpu_rq(i)->lock);
9648 +static struct cftype files[] = {
9651 + .read_u64 = cpuusage_read,
9652 + .write_u64 = cpuusage_write,
9656 +static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
9658 + return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
9662 + * charge this task's execution time to its accounting group.
9664 + * called with rq->lock held.
9666 +static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9668 + struct cpuacct *ca;
9670 + if (!cpuacct_subsys.active)
9673 + ca = task_ca(tsk);
9675 + u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9677 + *cpuusage += cputime;
9681 +struct cgroup_subsys cpuacct_subsys = {
9682 + .name = "cpuacct",
9683 + .create = cpuacct_create,
9684 + .destroy = cpuacct_destroy,
9685 + .populate = cpuacct_populate,
9686 + .subsys_id = cpuacct_subsys_id,
9688 +#endif /* CONFIG_CGROUP_CPUACCT */
9689 diff -Nurb linux-2.6.27-590/kernel/sched.c.rej linux-2.6.27-591/kernel/sched.c.rej
9690 --- linux-2.6.27-590/kernel/sched.c.rej 1969-12-31 19:00:00.000000000 -0500
9691 +++ linux-2.6.27-591/kernel/sched.c.rej 2010-01-29 16:30:22.000000000 -0500
9695 + #include <linux/nmi.h>
9696 + #include <linux/init.h>
9697 + #include <asm/uaccess.h>
9698 + #include <linux/highmem.h>
9699 + #include <linux/smp_lock.h>
9700 + #include <asm/mmu_context.h>
9702 + #include <linux/nmi.h>
9703 + #include <linux/init.h>
9704 + #include <asm/uaccess.h>
9705 ++ #include <linux/arrays.h>
9706 + #include <linux/highmem.h>
9707 + #include <linux/smp_lock.h>
9708 + #include <asm/mmu_context.h>
9714 + spin_lock(&rq->lock);
9715 + if (unlikely(rq != task_rq(p))) {
9716 + spin_unlock(&rq->lock);
9722 + spin_lock(&rq->lock);
9723 + if (unlikely(rq != task_rq(p))) {
9724 + spin_unlock(&rq->lock);
9727 + * event cannot wake it up and insert it on the runqueue either.
9729 + p->state = TASK_RUNNING;
9732 + * Make sure we do not leak PI boosting priority to the child:
9734 + * event cannot wake it up and insert it on the runqueue either.
9736 + p->state = TASK_RUNNING;
9737 ++ #ifdef CONFIG_CHOPSTIX
9738 ++ /* The jiffy of last interruption */
9739 ++ if (p->state & TASK_UNINTERRUPTIBLE) {
9740 ++ p->last_interrupted=jiffies;
9743 ++ if (p->state & TASK_INTERRUPTIBLE) {
9744 ++ p->last_interrupted=INTERRUPTIBLE;
9747 ++ p->last_interrupted=RUNNING;
9749 ++ /* The jiffy of last execution */
9750 ++ p->last_ran_j=jiffies;
9754 + * Make sure we do not leak PI boosting priority to the child:
9760 + static inline int interactive_sleep(enum sleep_type sleep_type)
9762 + return (sleep_type == SLEEP_INTERACTIVE ||
9768 + static inline int interactive_sleep(enum sleep_type sleep_type)
9770 + return (sleep_type == SLEEP_INTERACTIVE ||
9774 + * schedule() is the main scheduler function.
9776 + asmlinkage void __sched schedule(void)
9778 + struct task_struct *prev, *next;
9779 + struct prio_array *array;
9780 + struct list_head *queue;
9781 + unsigned long long now;
9782 +- unsigned long run_time;
9783 + int cpu, idx, new_prio;
9784 + long *switch_count;
9788 + * Test if we are atomic. Since do_exit() needs to call into
9791 + * schedule() is the main scheduler function.
9794 ++ #ifdef CONFIG_CHOPSTIX
9795 ++ extern void (*rec_event)(void *,unsigned int);
9796 ++ struct event_spec {
9797 ++ unsigned long pc;
9798 ++ unsigned long dcookie;
9799 ++ unsigned int count;
9800 ++ unsigned int reason;
9804 + asmlinkage void __sched schedule(void)
9806 + struct task_struct *prev, *next;
9807 + struct prio_array *array;
9808 + struct list_head *queue;
9809 + unsigned long long now;
9810 ++ unsigned long run_time, diff;
9811 + int cpu, idx, new_prio;
9812 + long *switch_count;
9814 ++ int sampling_reason;
9817 + * Test if we are atomic. Since do_exit() needs to call into
9820 + switch_count = &prev->nivcsw;
9821 + if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
9822 + switch_count = &prev->nvcsw;
9823 + if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
9824 + unlikely(signal_pending(prev))))
9825 + prev->state = TASK_RUNNING;
9827 + switch_count = &prev->nivcsw;
9828 + if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
9829 + switch_count = &prev->nvcsw;
9831 + if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
9832 + unlikely(signal_pending(prev))))
9833 + prev->state = TASK_RUNNING;
9836 + vx_uninterruptible_inc(prev);
9838 + deactivate_task(prev, rq);
9843 + vx_uninterruptible_inc(prev);
9845 + deactivate_task(prev, rq);
9846 ++ #ifdef CONFIG_CHOPSTIX
9847 ++ /* An uninterruptible process just yielded. Record the current jiffie */
9848 ++ if (prev->state & TASK_UNINTERRUPTIBLE) {
9849 ++ prev->last_interrupted=jiffies;
9851 ++ /* An interruptible process just yielded, or it got preempted.
9852 ++ * Mark it as interruptible */
9853 ++ else if (prev->state & TASK_INTERRUPTIBLE) {
9854 ++ prev->last_interrupted=INTERRUPTIBLE;
9862 + prev->sleep_avg = 0;
9863 + prev->timestamp = prev->last_ran = now;
9865 + sched_info_switch(prev, next);
9866 + if (likely(prev != next)) {
9867 + next->timestamp = next->last_ran = now;
9869 + prev->sleep_avg = 0;
9870 + prev->timestamp = prev->last_ran = now;
9872 ++ #ifdef CONFIG_CHOPSTIX
9873 ++ /* Run only if the Chopstix module so decrees it */
9875 ++ prev->last_ran_j = jiffies;
9876 ++ if (next->last_interrupted!=INTERRUPTIBLE) {
9877 ++ if (next->last_interrupted!=RUNNING) {
9878 ++ diff = (jiffies-next->last_interrupted);
9879 ++ sampling_reason = 0;/* BLOCKING */
9882 ++ diff = jiffies-next->last_ran_j;
9883 ++ sampling_reason = 1;/* PREEMPTION */
9886 ++ if (diff >= HZ/10) {
9887 ++ struct event event;
9888 ++ struct event_spec espec;
9889 ++ struct pt_regs *regs;
9890 ++ regs = task_pt_regs(current);
9892 ++ espec.reason = sampling_reason;
9893 ++ event.event_data=&espec;
9895 ++ espec.pc=regs->eip;
9896 ++ event.event_type=2;
9897 ++ /* index in the event array currently set up */
9898 ++ /* make sure the counters are loaded in the order we want them to show up*/
9899 ++ (*rec_event)(&event, diff);
9902 ++ /* next has been elected to run */
9903 ++ next->last_interrupted=0;
9906 + sched_info_switch(prev, next);
9907 + if (likely(prev != next)) {
9908 + next->timestamp = next->last_ran = now;
9911 + jiffies_to_timespec(p->policy == SCHED_FIFO ?
9912 + 0 : task_timeslice(p), &t);
9913 + read_unlock(&tasklist_lock);
9914 + retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
9918 + jiffies_to_timespec(p->policy == SCHED_FIFO ?
9919 + 0 : task_timeslice(p), &t);
9920 + read_unlock(&tasklist_lock);
9922 + retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
9935 ++ #ifdef CONFIG_CHOPSTIX
9936 ++ void (*rec_event)(void *,unsigned int) = NULL;
9938 ++ /* To support safe calling from asm */
9939 ++ asmlinkage void rec_event_asm (struct event *event_signature_in, unsigned int count) {
9940 ++ struct pt_regs *regs;
9941 ++ struct event_spec *es = event_signature_in->event_data;
9942 ++ regs = task_pt_regs(current);
9943 ++ event_signature_in->task=current;
9944 ++ es->pc=regs->eip;
9945 ++ event_signature_in->count=1;
9946 ++ (*rec_event)(event_signature_in, count);
9948 ++ EXPORT_SYMBOL(rec_event);
9949 ++ EXPORT_SYMBOL(in_sched_functions);
9951 diff -Nurb linux-2.6.27-590/mm/memory.c linux-2.6.27-591/mm/memory.c
9952 --- linux-2.6.27-590/mm/memory.c 2010-01-29 16:29:48.000000000 -0500
9953 +++ linux-2.6.27-591/mm/memory.c 2010-01-29 16:30:22.000000000 -0500
9956 #include <linux/swapops.h>
9957 #include <linux/elf.h>
9958 +#include <linux/arrays.h>
9960 #include "internal.h"
9962 @@ -2690,6 +2691,15 @@
9966 +extern void (*rec_event)(void *,unsigned int);
9967 +struct event_spec {
9969 + unsigned long dcookie;
9971 + unsigned char reason;
9976 * By the time we get here, we already hold the mm semaphore
9978 @@ -2719,6 +2729,24 @@
9980 return VM_FAULT_OOM;
9982 +#ifdef CONFIG_CHOPSTIX
9984 + struct event event;
9985 + struct event_spec espec;
9986 + struct pt_regs *regs;
9988 + regs = task_pt_regs(current);
9989 + pc = regs->eip & (unsigned int) ~4095;
9991 + espec.reason = 0; /* alloc */
9992 + event.event_data=&espec;
9993 + event.task = current;
9995 + event.event_type=5;
9996 + (*rec_event)(&event, 1);
10000 return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
10003 diff -Nurb linux-2.6.27-590/mm/slab.c linux-2.6.27-591/mm/slab.c
10004 --- linux-2.6.27-590/mm/slab.c 2010-01-29 16:29:48.000000000 -0500
10005 +++ linux-2.6.27-591/mm/slab.c 2010-01-29 16:30:22.000000000 -0500
10006 @@ -110,6 +110,7 @@
10007 #include <linux/fault-inject.h>
10008 #include <linux/rtmutex.h>
10009 #include <linux/reciprocal_div.h>
10010 +#include <linux/arrays.h>
10011 #include <linux/debugobjects.h>
10013 #include <asm/cacheflush.h>
10014 @@ -248,6 +249,14 @@
10018 +extern void (*rec_event)(void *,unsigned int);
10019 +struct event_spec {
10020 + unsigned long pc;
10021 + unsigned long dcookie;
10023 + unsigned char reason;
10027 * struct array_cache
10029 @@ -3469,6 +3478,19 @@
10030 local_irq_restore(save_flags);
10031 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
10033 +#ifdef CONFIG_CHOPSTIX
10034 + if (rec_event && objp) {
10035 + struct event event;
10036 + struct event_spec espec;
10038 + espec.reason = 0; /* alloc */
10039 + event.event_data=&espec;
10040 + event.task = current;
10042 + event.event_type=5;
10043 + (*rec_event)(&event, cachep->buffer_size);
10047 if (unlikely((flags & __GFP_ZERO) && objp))
10048 memset(objp, 0, obj_size(cachep));
10049 @@ -3578,12 +3600,26 @@
10050 * Release an obj back to its cache. If the obj has a constructed state, it must
10051 * be in this state _before_ it is released. Called with disabled ints.
10053 -static inline void __cache_free(struct kmem_cache *cachep, void *objp)
10054 +static inline void __cache_free(struct kmem_cache *cachep, void *objp, void *caller)
10056 struct array_cache *ac = cpu_cache_get(cachep);
10059 - objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
10060 + objp = cache_free_debugcheck(cachep, objp, caller);
10061 + #ifdef CONFIG_CHOPSTIX
10062 + if (rec_event && objp) {
10063 + struct event event;
10064 + struct event_spec espec;
10066 + espec.reason = 1; /* free */
10067 + event.event_data=&espec;
10068 + event.task = current;
10070 + event.event_type=4;
10071 + (*rec_event)(&event, cachep->buffer_size);
10075 vx_slab_free(cachep);
10078 @@ -3714,6 +3750,7 @@
10081 struct kmem_cache *cachep;
10084 /* If you want to save a few bytes .text space: replace
10086 @@ -3741,10 +3778,17 @@
10087 EXPORT_SYMBOL(__kmalloc_track_caller);
10090 +#ifdef CONFIG_CHOPSTIX
10091 +void *__kmalloc(size_t size, gfp_t flags)
10093 + return __do_kmalloc(size, flags, __builtin_return_address(0));
10096 void *__kmalloc(size_t size, gfp_t flags)
10098 return __do_kmalloc(size, flags, NULL);
10101 EXPORT_SYMBOL(__kmalloc);
10104 @@ -3764,7 +3808,7 @@
10105 debug_check_no_locks_freed(objp, obj_size(cachep));
10106 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
10107 debug_check_no_obj_freed(objp, obj_size(cachep));
10108 - __cache_free(cachep, objp);
10109 + __cache_free(cachep, objp,__builtin_return_address(0));
10110 local_irq_restore(flags);
10112 EXPORT_SYMBOL(kmem_cache_free);
10113 @@ -3790,7 +3834,7 @@
10114 c = virt_to_cache(objp);
10115 debug_check_no_locks_freed(objp, obj_size(c));
10116 debug_check_no_obj_freed(objp, obj_size(c));
10117 - __cache_free(c, (void *)objp);
10118 + __cache_free(c, (void *)objp,__builtin_return_address(0));
10119 local_irq_restore(flags);
10121 EXPORT_SYMBOL(kfree);