9785 lines · 233.3 KB · C
RawDownload
/* *  kernel/sched.c * *  Kernel scheduler and related syscalls * *  Copyright (C) 1991-2002  Linus Torvalds * *  1996-12-23  Modified by Dave Grothe to fix bugs in semaphores and *		make semaphores SMP safe *  1998-11-19	Implemented schedule_timeout() and related stuff *		by Andrea Arcangeli *  2002-01-04	New ultra-scalable O(1) scheduler by Ingo Molnar: *		hybrid priority-list and round-robin design with *		an array-switch method of distributing timeslices *		and per-CPU runqueues.  Cleanups and useful suggestions *		by Davide Libenzi, preemptible kernel bits by Robert Love. *  2003-09-03	Interactivity tuning by Con Kolivas. *  2004-04-02	Scheduler domains code by Nick Piggin *  2007-04-15  Work begun on replacing all interactivity tuning with a *              fair scheduling design by Con Kolivas. *  2007-05-05  Load balancing (smp-nice) and other improvements *              by Peter Williams *  2007-05-06  Interactivity improvements to CFS by Mike Galbraith *  2007-07-01  Group scheduling enhancements by Srivatsa Vaddagiri *  2007-11-29  RT balancing improvements by Steven Rostedt, Gregory Haskins, *              Thomas Gleixner, Mike Kravetz */ #include <linux/mm.h>#include <linux/module.h>#include <linux/nmi.h>#include <linux/init.h>#include <linux/uaccess.h>#include <linux/highmem.h>#include <asm/mmu_context.h>#include <linux/interrupt.h>#include <linux/capability.h>#include <linux/completion.h>#include <linux/kernel_stat.h>#include <linux/debug_locks.h>#include <linux/perf_event.h>#include <linux/security.h>#include <linux/notifier.h>#include <linux/profile.h>#include <linux/freezer.h>#include <linux/vmalloc.h>#include <linux/blkdev.h>#include <linux/delay.h>#include <linux/pid_namespace.h>#include <linux/smp.h>#include <linux/threads.h>#include <linux/timer.h>#include <linux/rcupdate.h>#include <linux/cpu.h>#include <linux/cpuset.h>#include <linux/percpu.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/stop_machine.h>#include <linux/sysctl.h>#include <linux/syscalls.h>#include <linux/times.h>#include <linux/tsacct_kern.h>#include <linux/kprobes.h>#include <linux/delayacct.h>#include <linux/unistd.h>#include <linux/pagemap.h>#include <linux/hrtimer.h>#include <linux/tick.h>#include <linux/debugfs.h>#include <linux/ctype.h>#include <linux/ftrace.h>#include <linux/slab.h>#include <linux/init_task.h> #include <asm/tlb.h>#include <asm/irq_regs.h>#include <asm/mutex.h>#ifdef CONFIG_PARAVIRT#include <asm/paravirt.h>#endif #include "sched_cpupri.h"#include "workqueue_sched.h"#include "sched_autogroup.h" #define CREATE_TRACE_POINTS#include <trace/events/sched.h> /* * Convert user-nice values [ -20 ... 0 ... 19 ] * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], * and back. */#define NICE_TO_PRIO(nice)	(MAX_RT_PRIO + (nice) + 20)#define PRIO_TO_NICE(prio)	((prio) - MAX_RT_PRIO - 20)#define TASK_NICE(p)		PRIO_TO_NICE((p)->static_prio) /* * 'User priority' is the nice value converted to something we * can work with better when scaling various scheduler parameters, * it's a [ 0 ... 39 ] range. */#define USER_PRIO(p)		((p)-MAX_RT_PRIO)#define TASK_USER_PRIO(p)	USER_PRIO((p)->static_prio)#define MAX_USER_PRIO		(USER_PRIO(MAX_PRIO)) /* * Helpers for converting nanosecond timing to jiffy resolution */#define NS_TO_JIFFIES(TIME)	((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) #define NICE_0_LOAD		SCHED_LOAD_SCALE#define NICE_0_SHIFT		SCHED_LOAD_SHIFT /* * These are the 'tuning knobs' of the scheduler: * * default timeslice is 100 msecs (used only for SCHED_RR tasks). * Timeslices get refilled after they expire. */#define DEF_TIMESLICE		(100 * HZ / 1000) /* * single value that denotes runtime == period, ie unlimited time. */#define RUNTIME_INF	((u64)~0ULL) static inline int rt_policy(int policy){	if (policy == SCHED_FIFO || policy == SCHED_RR)		return 1;	return 0;} static inline int task_has_rt_policy(struct task_struct *p){	return rt_policy(p->policy);} /* * This is the priority-queue data structure of the RT scheduling class: */struct rt_prio_array {	DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */	struct list_head queue[MAX_RT_PRIO];}; struct rt_bandwidth {	/* nests inside the rq lock: */	raw_spinlock_t		rt_runtime_lock;	ktime_t			rt_period;	u64			rt_runtime;	struct hrtimer		rt_period_timer;}; static struct rt_bandwidth def_rt_bandwidth; static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer){	struct rt_bandwidth *rt_b =		container_of(timer, struct rt_bandwidth, rt_period_timer);	ktime_t now;	int overrun;	int idle = 0; 	for (;;) {		now = hrtimer_cb_get_time(timer);		overrun = hrtimer_forward(timer, now, rt_b->rt_period); 		if (!overrun)			break; 		idle = do_sched_rt_period_timer(rt_b, overrun);	} 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;} staticvoid init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime){	rt_b->rt_period = ns_to_ktime(period);	rt_b->rt_runtime = runtime; 	raw_spin_lock_init(&rt_b->rt_runtime_lock); 	hrtimer_init(&rt_b->rt_period_timer,			CLOCK_MONOTONIC, HRTIMER_MODE_REL);	rt_b->rt_period_timer.function = sched_rt_period_timer;} static inline int rt_bandwidth_enabled(void){	return sysctl_sched_rt_runtime >= 0;} static void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period){	unsigned long delta;	ktime_t soft, hard, now; 	for (;;) {		if (hrtimer_active(period_timer))			break; 		now = hrtimer_cb_get_time(period_timer);		hrtimer_forward(period_timer, now, period); 		soft = hrtimer_get_softexpires(period_timer);		hard = hrtimer_get_expires(period_timer);		delta = ktime_to_ns(ktime_sub(hard, soft));		__hrtimer_start_range_ns(period_timer, soft, delta,					 HRTIMER_MODE_ABS_PINNED, 0);	}} static void start_rt_bandwidth(struct rt_bandwidth *rt_b){	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)		return; 	if (hrtimer_active(&rt_b->rt_period_timer))		return; 	raw_spin_lock(&rt_b->rt_runtime_lock);	start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);	raw_spin_unlock(&rt_b->rt_runtime_lock);} #ifdef CONFIG_RT_GROUP_SCHEDstatic void destroy_rt_bandwidth(struct rt_bandwidth *rt_b){	hrtimer_cancel(&rt_b->rt_period_timer);}#endif /* * sched_domains_mutex serializes calls to init_sched_domains, * detach_destroy_domains and partition_sched_domains. */static DEFINE_MUTEX(sched_domains_mutex); #ifdef CONFIG_CGROUP_SCHED #include <linux/cgroup.h> struct cfs_rq; static LIST_HEAD(task_groups); struct cfs_bandwidth {#ifdef CONFIG_CFS_BANDWIDTH	raw_spinlock_t lock;	ktime_t period;	u64 quota, runtime;	s64 hierarchal_quota;	u64 runtime_expires; 	int idle, timer_active;	struct hrtimer period_timer, slack_timer;	struct list_head throttled_cfs_rq; 	/* statistics */	int nr_periods, nr_throttled;	u64 throttled_time;#endif}; /* task group related information */struct task_group {	struct cgroup_subsys_state css; #ifdef CONFIG_FAIR_GROUP_SCHED	/* schedulable entities of this group on each cpu */	struct sched_entity **se;	/* runqueue "owned" by this group on each cpu */	struct cfs_rq **cfs_rq;	unsigned long shares; 	atomic_t load_weight;#endif #ifdef CONFIG_RT_GROUP_SCHED	struct sched_rt_entity **rt_se;	struct rt_rq **rt_rq; 	struct rt_bandwidth rt_bandwidth;#endif 	struct rcu_head rcu;	struct list_head list; 	struct task_group *parent;	struct list_head siblings;	struct list_head children; #ifdef CONFIG_SCHED_AUTOGROUP	struct autogroup *autogroup;#endif 	struct cfs_bandwidth cfs_bandwidth;}; /* task_group_lock serializes the addition/removal of task groups */static DEFINE_SPINLOCK(task_group_lock); #ifdef CONFIG_FAIR_GROUP_SCHED # define ROOT_TASK_GROUP_LOAD	NICE_0_LOAD /* * A weight of 0 or 1 can cause arithmetics problems. * A weight of a cfs_rq is the sum of weights of which entities * are queued on this cfs_rq, so a weight of a entity should not be * too large, so as the shares value of a task group. * (The default weight is 1024 - so there's no practical *  limitation from this.) */#define MIN_SHARES	(1UL <<  1)#define MAX_SHARES	(1UL << 18) static int root_task_group_load = ROOT_TASK_GROUP_LOAD;#endif /* Default task group. *	Every task in system belong to this group at bootup. */struct task_group root_task_group; #endif	/* CONFIG_CGROUP_SCHED */ /* CFS-related fields in a runqueue */struct cfs_rq {	struct load_weight load;	unsigned long nr_running, h_nr_running; 	u64 exec_clock;	u64 min_vruntime;#ifndef CONFIG_64BIT	u64 min_vruntime_copy;#endif 	struct rb_root tasks_timeline;	struct rb_node *rb_leftmost; 	struct list_head tasks;	struct list_head *balance_iterator; 	/*	 * 'curr' points to currently running entity on this cfs_rq.	 * It is set to NULL otherwise (i.e when none are currently running).	 */	struct sched_entity *curr, *next, *last, *skip; #ifdef	CONFIG_SCHED_DEBUG	unsigned int nr_spread_over;#endif #ifdef CONFIG_FAIR_GROUP_SCHED	struct rq *rq;	/* cpu runqueue to which this cfs_rq is attached */ 	/*	 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in	 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities	 * (like users, containers etc.)	 *	 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This	 * list is used during load balance.	 */	int on_list;	struct list_head leaf_cfs_rq_list;	struct task_group *tg;	/* group that "owns" this runqueue */ #ifdef CONFIG_SMP	/*	 * the part of load.weight contributed by tasks	 */	unsigned long task_weight; 	/*	 *   h_load = weight * f(tg)	 *	 * Where f(tg) is the recursive weight fraction assigned to	 * this group.	 */	unsigned long h_load; 	/*	 * Maintaining per-cpu shares distribution for group scheduling	 *	 * load_stamp is the last time we updated the load average	 * load_last is the last time we updated the load average and saw load	 * load_unacc_exec_time is currently unaccounted execution time	 */	u64 load_avg;	u64 load_period;	u64 load_stamp, load_last, load_unacc_exec_time; 	unsigned long load_contribution;#endif#ifdef CONFIG_CFS_BANDWIDTH	int runtime_enabled;	u64 runtime_expires;	s64 runtime_remaining; 	u64 throttled_timestamp;	int throttled, throttle_count;	struct list_head throttled_list;#endif#endif}; #ifdef CONFIG_FAIR_GROUP_SCHED#ifdef CONFIG_CFS_BANDWIDTHstatic inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg){	return &tg->cfs_bandwidth;} static inline u64 default_cfs_period(void);static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b); static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer){	struct cfs_bandwidth *cfs_b =		container_of(timer, struct cfs_bandwidth, slack_timer);	do_sched_cfs_slack_timer(cfs_b); 	return HRTIMER_NORESTART;} static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer){	struct cfs_bandwidth *cfs_b =		container_of(timer, struct cfs_bandwidth, period_timer);	ktime_t now;	int overrun;	int idle = 0; 	for (;;) {		now = hrtimer_cb_get_time(timer);		overrun = hrtimer_forward(timer, now, cfs_b->period); 		if (!overrun)			break; 		idle = do_sched_cfs_period_timer(cfs_b, overrun);	} 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;} static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b){	raw_spin_lock_init(&cfs_b->lock);	cfs_b->runtime = 0;	cfs_b->quota = RUNTIME_INF;	cfs_b->period = ns_to_ktime(default_cfs_period()); 	INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);	hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);	cfs_b->period_timer.function = sched_cfs_period_timer;	hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);	cfs_b->slack_timer.function = sched_cfs_slack_timer;} static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq){	cfs_rq->runtime_enabled = 0;	INIT_LIST_HEAD(&cfs_rq->throttled_list);} /* requires cfs_b->lock, may release to reprogram timer */static void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b){	/*	 * The timer may be active because we're trying to set a new bandwidth	 * period or because we're racing with the tear-down path	 * (timer_active==0 becomes visible before the hrtimer call-back	 * terminates).  In either case we ensure that it's re-programmed	 */	while (unlikely(hrtimer_active(&cfs_b->period_timer))) {		raw_spin_unlock(&cfs_b->lock);		/* ensure cfs_b->lock is available while we wait */		hrtimer_cancel(&cfs_b->period_timer); 		raw_spin_lock(&cfs_b->lock);		/* if someone else restarted the timer then we're done */		if (cfs_b->timer_active)			return;	} 	cfs_b->timer_active = 1;	start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);} 

Lines 1–500 of 9785.