xref: /linux/include/linux/sched.h (revision a225023828038a1aaea876a65313c863ec23fa44)
11da177e4SLinus Torvalds #ifndef _LINUX_SCHED_H
21da177e4SLinus Torvalds #define _LINUX_SCHED_H
31da177e4SLinus Torvalds 
4607ca46eSDavid Howells #include <uapi/linux/sched.h>
5b7b3c76aSDavid Woodhouse 
65c228079SDongsheng Yang #include <linux/sched/prio.h>
75c228079SDongsheng Yang 
8b7b3c76aSDavid Woodhouse 
9b7b3c76aSDavid Woodhouse struct sched_param {
10b7b3c76aSDavid Woodhouse 	int sched_priority;
11b7b3c76aSDavid Woodhouse };
12b7b3c76aSDavid Woodhouse 
131da177e4SLinus Torvalds #include <asm/param.h>	/* for HZ */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/capability.h>
161da177e4SLinus Torvalds #include <linux/threads.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/timex.h>
201da177e4SLinus Torvalds #include <linux/jiffies.h>
21fb00aca4SPeter Zijlstra #include <linux/plist.h>
221da177e4SLinus Torvalds #include <linux/rbtree.h>
231da177e4SLinus Torvalds #include <linux/thread_info.h>
241da177e4SLinus Torvalds #include <linux/cpumask.h>
251da177e4SLinus Torvalds #include <linux/errno.h>
261da177e4SLinus Torvalds #include <linux/nodemask.h>
27c92ff1bdSMartin Schwidefsky #include <linux/mm_types.h>
2892cf2118SFrederic Weisbecker #include <linux/preempt.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <asm/page.h>
311da177e4SLinus Torvalds #include <asm/ptrace.h>
32bfc3f028SFrederic Weisbecker #include <linux/cputime.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/smp.h>
351da177e4SLinus Torvalds #include <linux/sem.h>
36ab602f79SJack Miller #include <linux/shm.h>
371da177e4SLinus Torvalds #include <linux/signal.h>
381da177e4SLinus Torvalds #include <linux/compiler.h>
391da177e4SLinus Torvalds #include <linux/completion.h>
401da177e4SLinus Torvalds #include <linux/pid.h>
411da177e4SLinus Torvalds #include <linux/percpu.h>
421da177e4SLinus Torvalds #include <linux/topology.h>
431da177e4SLinus Torvalds #include <linux/seccomp.h>
44e56d0903SIngo Molnar #include <linux/rcupdate.h>
4505725f7eSJiri Pirko #include <linux/rculist.h>
4623f78d4aSIngo Molnar #include <linux/rtmutex.h>
471da177e4SLinus Torvalds 
48a3b6714eSDavid Woodhouse #include <linux/time.h>
49a3b6714eSDavid Woodhouse #include <linux/param.h>
50a3b6714eSDavid Woodhouse #include <linux/resource.h>
51a3b6714eSDavid Woodhouse #include <linux/timer.h>
52a3b6714eSDavid Woodhouse #include <linux/hrtimer.h>
535c9a8750SDmitry Vyukov #include <linux/kcov.h>
547c3ab738SAndrew Morton #include <linux/task_io_accounting.h>
559745512cSArjan van de Ven #include <linux/latencytop.h>
569e2b2dc4SDavid Howells #include <linux/cred.h>
57fa14ff4aSPeter Zijlstra #include <linux/llist.h>
587b44ab97SEric W. Biederman #include <linux/uidgid.h>
5921caf2fcSMing Lei #include <linux/gfp.h>
60d4311ff1SAaron Tomlin #include <linux/magic.h>
617d7efec3STejun Heo #include <linux/cgroup-defs.h>
62a3b6714eSDavid Woodhouse 
63a3b6714eSDavid Woodhouse #include <asm/processor.h>
6436d57ac4SH. J. Lu 
65d50dde5aSDario Faggioli #define SCHED_ATTR_SIZE_VER0	48	/* sizeof first published struct */
66d50dde5aSDario Faggioli 
67d50dde5aSDario Faggioli /*
68d50dde5aSDario Faggioli  * Extended scheduling parameters data structure.
69d50dde5aSDario Faggioli  *
70d50dde5aSDario Faggioli  * This is needed because the original struct sched_param can not be
71d50dde5aSDario Faggioli  * altered without introducing ABI issues with legacy applications
72d50dde5aSDario Faggioli  * (e.g., in sched_getparam()).
73d50dde5aSDario Faggioli  *
74d50dde5aSDario Faggioli  * However, the possibility of specifying more than just a priority for
75d50dde5aSDario Faggioli  * the tasks may be useful for a wide variety of application fields, e.g.,
76d50dde5aSDario Faggioli  * multimedia, streaming, automation and control, and many others.
77d50dde5aSDario Faggioli  *
78d50dde5aSDario Faggioli  * This variant (sched_attr) is meant at describing a so-called
79d50dde5aSDario Faggioli  * sporadic time-constrained task. In such model a task is specified by:
80d50dde5aSDario Faggioli  *  - the activation period or minimum instance inter-arrival time;
81d50dde5aSDario Faggioli  *  - the maximum (or average, depending on the actual scheduling
82d50dde5aSDario Faggioli  *    discipline) computation time of all instances, a.k.a. runtime;
83d50dde5aSDario Faggioli  *  - the deadline (relative to the actual activation time) of each
84d50dde5aSDario Faggioli  *    instance.
85d50dde5aSDario Faggioli  * Very briefly, a periodic (sporadic) task asks for the execution of
86d50dde5aSDario Faggioli  * some specific computation --which is typically called an instance--
87d50dde5aSDario Faggioli  * (at most) every period. Moreover, each instance typically lasts no more
88d50dde5aSDario Faggioli  * than the runtime and must be completed by time instant t equal to
89d50dde5aSDario Faggioli  * the instance activation time + the deadline.
90d50dde5aSDario Faggioli  *
91d50dde5aSDario Faggioli  * This is reflected by the actual fields of the sched_attr structure:
92d50dde5aSDario Faggioli  *
93d50dde5aSDario Faggioli  *  @size		size of the structure, for fwd/bwd compat.
94d50dde5aSDario Faggioli  *
95d50dde5aSDario Faggioli  *  @sched_policy	task's scheduling policy
96d50dde5aSDario Faggioli  *  @sched_flags	for customizing the scheduler behaviour
97d50dde5aSDario Faggioli  *  @sched_nice		task's nice value      (SCHED_NORMAL/BATCH)
98d50dde5aSDario Faggioli  *  @sched_priority	task's static priority (SCHED_FIFO/RR)
99d50dde5aSDario Faggioli  *  @sched_deadline	representative of the task's deadline
100d50dde5aSDario Faggioli  *  @sched_runtime	representative of the task's runtime
101d50dde5aSDario Faggioli  *  @sched_period	representative of the task's period
102d50dde5aSDario Faggioli  *
103d50dde5aSDario Faggioli  * Given this task model, there are a multiplicity of scheduling algorithms
104d50dde5aSDario Faggioli  * and policies, that can be used to ensure all the tasks will make their
105d50dde5aSDario Faggioli  * timing constraints.
106aab03e05SDario Faggioli  *
107aab03e05SDario Faggioli  * As of now, the SCHED_DEADLINE policy (sched_dl scheduling class) is the
108aab03e05SDario Faggioli  * only user of this new interface. More information about the algorithm
109aab03e05SDario Faggioli  * available in the scheduling class file or in Documentation/.
110d50dde5aSDario Faggioli  */
111d50dde5aSDario Faggioli struct sched_attr {
112d50dde5aSDario Faggioli 	u32 size;
113d50dde5aSDario Faggioli 
114d50dde5aSDario Faggioli 	u32 sched_policy;
115d50dde5aSDario Faggioli 	u64 sched_flags;
116d50dde5aSDario Faggioli 
117d50dde5aSDario Faggioli 	/* SCHED_NORMAL, SCHED_BATCH */
118d50dde5aSDario Faggioli 	s32 sched_nice;
119d50dde5aSDario Faggioli 
120d50dde5aSDario Faggioli 	/* SCHED_FIFO, SCHED_RR */
121d50dde5aSDario Faggioli 	u32 sched_priority;
122d50dde5aSDario Faggioli 
123d50dde5aSDario Faggioli 	/* SCHED_DEADLINE */
124d50dde5aSDario Faggioli 	u64 sched_runtime;
125d50dde5aSDario Faggioli 	u64 sched_deadline;
126d50dde5aSDario Faggioli 	u64 sched_period;
127d50dde5aSDario Faggioli };
128d50dde5aSDario Faggioli 
129c87e2837SIngo Molnar struct futex_pi_state;
130286100a6SAlexey Dobriyan struct robust_list_head;
131bddd87c7SAkinobu Mita struct bio_list;
1325ad4e53bSAl Viro struct fs_struct;
133cdd6c482SIngo Molnar struct perf_event_context;
13473c10101SJens Axboe struct blk_plug;
135c4ad8f98SLinus Torvalds struct filename;
13689076bc3SAl Viro struct nameidata;
1371da177e4SLinus Torvalds 
138615d6e87SDavidlohr Bueso #define VMACACHE_BITS 2
139615d6e87SDavidlohr Bueso #define VMACACHE_SIZE (1U << VMACACHE_BITS)
140615d6e87SDavidlohr Bueso #define VMACACHE_MASK (VMACACHE_SIZE - 1)
141615d6e87SDavidlohr Bueso 
1421da177e4SLinus Torvalds /*
1431da177e4SLinus Torvalds  * These are the constant used to fake the fixed-point load-average
1441da177e4SLinus Torvalds  * counting. Some notes:
1451da177e4SLinus Torvalds  *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
1461da177e4SLinus Torvalds  *    a load-average precision of 10 bits integer + 11 bits fractional
1471da177e4SLinus Torvalds  *  - if you want to count load-averages more often, you need more
1481da177e4SLinus Torvalds  *    precision, or rounding will get you. With 2-second counting freq,
1491da177e4SLinus Torvalds  *    the EXP_n values would be 1981, 2034 and 2043 if still using only
1501da177e4SLinus Torvalds  *    11 bit fractions.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds extern unsigned long avenrun[];		/* Load averages */
1532d02494fSThomas Gleixner extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds #define FSHIFT		11		/* nr of bits of precision */
1561da177e4SLinus Torvalds #define FIXED_1		(1<<FSHIFT)	/* 1.0 as fixed-point */
1570c2043abSLinus Torvalds #define LOAD_FREQ	(5*HZ+1)	/* 5 sec intervals */
1581da177e4SLinus Torvalds #define EXP_1		1884		/* 1/exp(5sec/1min) as fixed-point */
1591da177e4SLinus Torvalds #define EXP_5		2014		/* 1/exp(5sec/5min) */
1601da177e4SLinus Torvalds #define EXP_15		2037		/* 1/exp(5sec/15min) */
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #define CALC_LOAD(load,exp,n) \
1631da177e4SLinus Torvalds 	load *= exp; \
1641da177e4SLinus Torvalds 	load += n*(FIXED_1-exp); \
1651da177e4SLinus Torvalds 	load >>= FSHIFT;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds extern unsigned long total_forks;
1681da177e4SLinus Torvalds extern int nr_threads;
1691da177e4SLinus Torvalds DECLARE_PER_CPU(unsigned long, process_counts);
1701da177e4SLinus Torvalds extern int nr_processes(void);
1711da177e4SLinus Torvalds extern unsigned long nr_running(void);
1722ee507c4STim Chen extern bool single_task_running(void);
1731da177e4SLinus Torvalds extern unsigned long nr_iowait(void);
1748c215bd3SPeter Zijlstra extern unsigned long nr_iowait_cpu(int cpu);
175372ba8cbSMel Gorman extern void get_iowait_load(unsigned long *nr_waiters, unsigned long *load);
17669d25870SArjan van de Ven 
1770f004f5aSPeter Zijlstra extern void calc_global_load(unsigned long ticks);
1783289bdb4SPeter Zijlstra 
1793289bdb4SPeter Zijlstra #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
1801f41906aSFrederic Weisbecker extern void cpu_load_update_nohz_start(void);
1811f41906aSFrederic Weisbecker extern void cpu_load_update_nohz_stop(void);
1823289bdb4SPeter Zijlstra #else
1831f41906aSFrederic Weisbecker static inline void cpu_load_update_nohz_start(void) { }
1841f41906aSFrederic Weisbecker static inline void cpu_load_update_nohz_stop(void) { }
1853289bdb4SPeter Zijlstra #endif
1861da177e4SLinus Torvalds 
187b637a328SPaul E. McKenney extern void dump_cpu_task(int cpu);
188b637a328SPaul E. McKenney 
18943ae34cbSIngo Molnar struct seq_file;
19043ae34cbSIngo Molnar struct cfs_rq;
1914cf86d77SIngo Molnar struct task_group;
19243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
19343ae34cbSIngo Molnar extern void proc_sched_show_task(struct task_struct *p, struct seq_file *m);
19443ae34cbSIngo Molnar extern void proc_sched_set_task(struct task_struct *p);
19543ae34cbSIngo Molnar #endif
1961da177e4SLinus Torvalds 
1974a8342d2SLinus Torvalds /*
1984a8342d2SLinus Torvalds  * Task state bitmask. NOTE! These bits are also
1994a8342d2SLinus Torvalds  * encoded in fs/proc/array.c: get_task_state().
2004a8342d2SLinus Torvalds  *
2014a8342d2SLinus Torvalds  * We have two separate sets of flags: task->state
2024a8342d2SLinus Torvalds  * is about runnability, while task->exit_state are
2034a8342d2SLinus Torvalds  * about the task exiting. Confusing, but this way
2044a8342d2SLinus Torvalds  * modifying one set can't modify the other one by
2054a8342d2SLinus Torvalds  * mistake.
2064a8342d2SLinus Torvalds  */
2071da177e4SLinus Torvalds #define TASK_RUNNING		0
2081da177e4SLinus Torvalds #define TASK_INTERRUPTIBLE	1
2091da177e4SLinus Torvalds #define TASK_UNINTERRUPTIBLE	2
210f021a3c2SMatthew Wilcox #define __TASK_STOPPED		4
211f021a3c2SMatthew Wilcox #define __TASK_TRACED		8
2124a8342d2SLinus Torvalds /* in tsk->exit_state */
213ad86622bSOleg Nesterov #define EXIT_DEAD		16
214ad86622bSOleg Nesterov #define EXIT_ZOMBIE		32
215abd50b39SOleg Nesterov #define EXIT_TRACE		(EXIT_ZOMBIE | EXIT_DEAD)
2164a8342d2SLinus Torvalds /* in tsk->state again */
217af927232SMike Galbraith #define TASK_DEAD		64
218f021a3c2SMatthew Wilcox #define TASK_WAKEKILL		128
219e9c84311SPeter Zijlstra #define TASK_WAKING		256
220f2530dc7SThomas Gleixner #define TASK_PARKED		512
22180ed87c8SPeter Zijlstra #define TASK_NOLOAD		1024
2227dc603c9SPeter Zijlstra #define TASK_NEW		2048
2237dc603c9SPeter Zijlstra #define TASK_STATE_MAX		4096
224f021a3c2SMatthew Wilcox 
2257dc603c9SPeter Zijlstra #define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
22673342151SPeter Zijlstra 
227e1781538SPeter Zijlstra extern char ___assert_task_state[1 - 2*!!(
228e1781538SPeter Zijlstra 		sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
229f021a3c2SMatthew Wilcox 
230f021a3c2SMatthew Wilcox /* Convenience macros for the sake of set_task_state */
231f021a3c2SMatthew Wilcox #define TASK_KILLABLE		(TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
232f021a3c2SMatthew Wilcox #define TASK_STOPPED		(TASK_WAKEKILL | __TASK_STOPPED)
233f021a3c2SMatthew Wilcox #define TASK_TRACED		(TASK_WAKEKILL | __TASK_TRACED)
2341da177e4SLinus Torvalds 
23580ed87c8SPeter Zijlstra #define TASK_IDLE		(TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
23680ed87c8SPeter Zijlstra 
23792a1f4bcSMatthew Wilcox /* Convenience macros for the sake of wake_up */
23892a1f4bcSMatthew Wilcox #define TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
239f021a3c2SMatthew Wilcox #define TASK_ALL		(TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
24092a1f4bcSMatthew Wilcox 
24192a1f4bcSMatthew Wilcox /* get_task_state() */
24292a1f4bcSMatthew Wilcox #define TASK_REPORT		(TASK_RUNNING | TASK_INTERRUPTIBLE | \
243f021a3c2SMatthew Wilcox 				 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
24474e37200SOleg Nesterov 				 __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
24592a1f4bcSMatthew Wilcox 
246f021a3c2SMatthew Wilcox #define task_is_traced(task)	((task->state & __TASK_TRACED) != 0)
247f021a3c2SMatthew Wilcox #define task_is_stopped(task)	((task->state & __TASK_STOPPED) != 0)
24892a1f4bcSMatthew Wilcox #define task_is_stopped_or_traced(task)	\
249f021a3c2SMatthew Wilcox 			((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
25092a1f4bcSMatthew Wilcox #define task_contributes_to_load(task)	\
251e3c8ca83SNathan Lynch 				((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
25280ed87c8SPeter Zijlstra 				 (task->flags & PF_FROZEN) == 0 && \
25380ed87c8SPeter Zijlstra 				 (task->state & TASK_NOLOAD) == 0)
2541da177e4SLinus Torvalds 
2558eb23b9fSPeter Zijlstra #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
2568eb23b9fSPeter Zijlstra 
2578eb23b9fSPeter Zijlstra #define __set_task_state(tsk, state_value)			\
2588eb23b9fSPeter Zijlstra 	do {							\
2598eb23b9fSPeter Zijlstra 		(tsk)->task_state_change = _THIS_IP_;		\
2608eb23b9fSPeter Zijlstra 		(tsk)->state = (state_value);			\
2618eb23b9fSPeter Zijlstra 	} while (0)
2628eb23b9fSPeter Zijlstra #define set_task_state(tsk, state_value)			\
2638eb23b9fSPeter Zijlstra 	do {							\
2648eb23b9fSPeter Zijlstra 		(tsk)->task_state_change = _THIS_IP_;		\
265b92b8b35SPeter Zijlstra 		smp_store_mb((tsk)->state, (state_value));	\
2668eb23b9fSPeter Zijlstra 	} while (0)
2678eb23b9fSPeter Zijlstra 
2688eb23b9fSPeter Zijlstra #define __set_current_state(state_value)			\
2698eb23b9fSPeter Zijlstra 	do {							\
2708eb23b9fSPeter Zijlstra 		current->task_state_change = _THIS_IP_;		\
2718eb23b9fSPeter Zijlstra 		current->state = (state_value);			\
2728eb23b9fSPeter Zijlstra 	} while (0)
2738eb23b9fSPeter Zijlstra #define set_current_state(state_value)				\
2748eb23b9fSPeter Zijlstra 	do {							\
2758eb23b9fSPeter Zijlstra 		current->task_state_change = _THIS_IP_;		\
276b92b8b35SPeter Zijlstra 		smp_store_mb(current->state, (state_value));	\
2778eb23b9fSPeter Zijlstra 	} while (0)
2788eb23b9fSPeter Zijlstra 
2798eb23b9fSPeter Zijlstra #else
2808eb23b9fSPeter Zijlstra 
281*a2250238SPeter Zijlstra /*
282*a2250238SPeter Zijlstra  * @tsk had better be current, or you get to keep the pieces.
283*a2250238SPeter Zijlstra  *
284*a2250238SPeter Zijlstra  * The only reason is that computing current can be more expensive than
285*a2250238SPeter Zijlstra  * using a pointer that's already available.
286*a2250238SPeter Zijlstra  *
287*a2250238SPeter Zijlstra  * Therefore, see set_current_state().
288*a2250238SPeter Zijlstra  */
2891da177e4SLinus Torvalds #define __set_task_state(tsk, state_value)		\
2901da177e4SLinus Torvalds 	do { (tsk)->state = (state_value); } while (0)
2911da177e4SLinus Torvalds #define set_task_state(tsk, state_value)		\
292b92b8b35SPeter Zijlstra 	smp_store_mb((tsk)->state, (state_value))
2931da177e4SLinus Torvalds 
294498d0c57SAndrew Morton /*
295498d0c57SAndrew Morton  * set_current_state() includes a barrier so that the write of current->state
296498d0c57SAndrew Morton  * is correctly serialised wrt the caller's subsequent test of whether to
297498d0c57SAndrew Morton  * actually sleep:
298498d0c57SAndrew Morton  *
299*a2250238SPeter Zijlstra  *   for (;;) {
300498d0c57SAndrew Morton  *	set_current_state(TASK_UNINTERRUPTIBLE);
301*a2250238SPeter Zijlstra  *	if (!need_sleep)
302*a2250238SPeter Zijlstra  *		break;
303498d0c57SAndrew Morton  *
304*a2250238SPeter Zijlstra  *	schedule();
305*a2250238SPeter Zijlstra  *   }
306*a2250238SPeter Zijlstra  *   __set_current_state(TASK_RUNNING);
307*a2250238SPeter Zijlstra  *
308*a2250238SPeter Zijlstra  * If the caller does not need such serialisation (because, for instance, the
309*a2250238SPeter Zijlstra  * condition test and condition change and wakeup are under the same lock) then
310*a2250238SPeter Zijlstra  * use __set_current_state().
311*a2250238SPeter Zijlstra  *
312*a2250238SPeter Zijlstra  * The above is typically ordered against the wakeup, which does:
313*a2250238SPeter Zijlstra  *
314*a2250238SPeter Zijlstra  *	need_sleep = false;
315*a2250238SPeter Zijlstra  *	wake_up_state(p, TASK_UNINTERRUPTIBLE);
316*a2250238SPeter Zijlstra  *
317*a2250238SPeter Zijlstra  * Where wake_up_state() (and all other wakeup primitives) imply enough
318*a2250238SPeter Zijlstra  * barriers to order the store of the variable against wakeup.
319*a2250238SPeter Zijlstra  *
320*a2250238SPeter Zijlstra  * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
321*a2250238SPeter Zijlstra  * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
322*a2250238SPeter Zijlstra  * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
323*a2250238SPeter Zijlstra  *
324*a2250238SPeter Zijlstra  * This is obviously fine, since they both store the exact same value.
325*a2250238SPeter Zijlstra  *
326*a2250238SPeter Zijlstra  * Also see the comments of try_to_wake_up().
327498d0c57SAndrew Morton  */
3281da177e4SLinus Torvalds #define __set_current_state(state_value)		\
3291da177e4SLinus Torvalds 	do { current->state = (state_value); } while (0)
3301da177e4SLinus Torvalds #define set_current_state(state_value)			\
331b92b8b35SPeter Zijlstra 	smp_store_mb(current->state, (state_value))
3321da177e4SLinus Torvalds 
3338eb23b9fSPeter Zijlstra #endif
3348eb23b9fSPeter Zijlstra 
3351da177e4SLinus Torvalds /* Task command name length */
3361da177e4SLinus Torvalds #define TASK_COMM_LEN 16
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds #include <linux/spinlock.h>
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * This serializes "schedule()" and also protects
3421da177e4SLinus Torvalds  * the run-queue from deletions/modifications (but
3431da177e4SLinus Torvalds  * _adding_ to the beginning of the run-queue has
3441da177e4SLinus Torvalds  * a separate lock).
3451da177e4SLinus Torvalds  */
3461da177e4SLinus Torvalds extern rwlock_t tasklist_lock;
3471da177e4SLinus Torvalds extern spinlock_t mmlist_lock;
3481da177e4SLinus Torvalds 
34936c8b586SIngo Molnar struct task_struct;
3501da177e4SLinus Torvalds 
351db1466b3SPaul E. McKenney #ifdef CONFIG_PROVE_RCU
352db1466b3SPaul E. McKenney extern int lockdep_tasklist_lock_is_held(void);
353db1466b3SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_RCU */
354db1466b3SPaul E. McKenney 
3551da177e4SLinus Torvalds extern void sched_init(void);
3561da177e4SLinus Torvalds extern void sched_init_smp(void);
3572d07b255SHarvey Harrison extern asmlinkage void schedule_tail(struct task_struct *prev);
35836c8b586SIngo Molnar extern void init_idle(struct task_struct *idle, int cpu);
3591df21055SIngo Molnar extern void init_idle_bootup_task(struct task_struct *idle);
3601da177e4SLinus Torvalds 
3613fa0818bSRik van Riel extern cpumask_var_t cpu_isolated_map;
3623fa0818bSRik van Riel 
36389f19f04SAndrew Morton extern int runqueue_is_locked(int cpu);
364017730c1SIngo Molnar 
3653451d024SFrederic Weisbecker #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
366c1cc017cSAlex Shi extern void nohz_balance_enter_idle(int cpu);
36769e1e811SSuresh Siddha extern void set_cpu_sd_state_idle(void);
368bc7a34b8SThomas Gleixner extern int get_nohz_timer_target(void);
36946cb4b7cSSiddha, Suresh B #else
370c1cc017cSAlex Shi static inline void nohz_balance_enter_idle(int cpu) { }
371fdaabd80SPeter Zijlstra static inline void set_cpu_sd_state_idle(void) { }
37246cb4b7cSSiddha, Suresh B #endif
3731da177e4SLinus Torvalds 
374e59e2ae2SIngo Molnar /*
37539bc89fdSIngo Molnar  * Only dump TASK_* tasks. (0 for all tasks)
376e59e2ae2SIngo Molnar  */
377e59e2ae2SIngo Molnar extern void show_state_filter(unsigned long state_filter);
378e59e2ae2SIngo Molnar 
379e59e2ae2SIngo Molnar static inline void show_state(void)
380e59e2ae2SIngo Molnar {
38139bc89fdSIngo Molnar 	show_state_filter(0);
382e59e2ae2SIngo Molnar }
383e59e2ae2SIngo Molnar 
3841da177e4SLinus Torvalds extern void show_regs(struct pt_regs *);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /*
3871da177e4SLinus Torvalds  * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
3881da177e4SLinus Torvalds  * task), SP is the stack pointer of the first frame that should be shown in the back
3891da177e4SLinus Torvalds  * trace (or NULL if the entire call-chain of the task should be shown).
3901da177e4SLinus Torvalds  */
3911da177e4SLinus Torvalds extern void show_stack(struct task_struct *task, unsigned long *sp);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds extern void cpu_init (void);
3941da177e4SLinus Torvalds extern void trap_init(void);
3951da177e4SLinus Torvalds extern void update_process_times(int user);
3961da177e4SLinus Torvalds extern void scheduler_tick(void);
3979cf7243dSThomas Gleixner extern int sched_cpu_starting(unsigned int cpu);
39840190a78SThomas Gleixner extern int sched_cpu_activate(unsigned int cpu);
39940190a78SThomas Gleixner extern int sched_cpu_deactivate(unsigned int cpu);
4001da177e4SLinus Torvalds 
401f2785ddbSThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
402f2785ddbSThomas Gleixner extern int sched_cpu_dying(unsigned int cpu);
403f2785ddbSThomas Gleixner #else
404f2785ddbSThomas Gleixner # define sched_cpu_dying	NULL
405f2785ddbSThomas Gleixner #endif
4061da177e4SLinus Torvalds 
40782a1fcb9SIngo Molnar extern void sched_show_task(struct task_struct *p);
40882a1fcb9SIngo Molnar 
40919cc36c0SFrederic Weisbecker #ifdef CONFIG_LOCKUP_DETECTOR
41003e0d461STejun Heo extern void touch_softlockup_watchdog_sched(void);
4118446f1d3SIngo Molnar extern void touch_softlockup_watchdog(void);
412d6ad3e28SJason Wessel extern void touch_softlockup_watchdog_sync(void);
41304c9167fSJeremy Fitzhardinge extern void touch_all_softlockup_watchdogs(void);
414332fbdbcSDon Zickus extern int proc_dowatchdog_thresh(struct ctl_table *table, int write,
4158d65af78SAlexey Dobriyan 				  void __user *buffer,
416baf48f65SMandeep Singh Baines 				  size_t *lenp, loff_t *ppos);
4179c44bc03SIngo Molnar extern unsigned int  softlockup_panic;
418ac1f5912SDon Zickus extern unsigned int  hardlockup_panic;
419004417a6SPeter Zijlstra void lockup_detector_init(void);
4208446f1d3SIngo Molnar #else
42103e0d461STejun Heo static inline void touch_softlockup_watchdog_sched(void)
42203e0d461STejun Heo {
42303e0d461STejun Heo }
4248446f1d3SIngo Molnar static inline void touch_softlockup_watchdog(void)
4258446f1d3SIngo Molnar {
4268446f1d3SIngo Molnar }
427d6ad3e28SJason Wessel static inline void touch_softlockup_watchdog_sync(void)
428d6ad3e28SJason Wessel {
429d6ad3e28SJason Wessel }
43004c9167fSJeremy Fitzhardinge static inline void touch_all_softlockup_watchdogs(void)
43104c9167fSJeremy Fitzhardinge {
43204c9167fSJeremy Fitzhardinge }
433004417a6SPeter Zijlstra static inline void lockup_detector_init(void)
434004417a6SPeter Zijlstra {
435004417a6SPeter Zijlstra }
4368446f1d3SIngo Molnar #endif
4378446f1d3SIngo Molnar 
4388b414521SMarcelo Tosatti #ifdef CONFIG_DETECT_HUNG_TASK
4398b414521SMarcelo Tosatti void reset_hung_task_detector(void);
4408b414521SMarcelo Tosatti #else
4418b414521SMarcelo Tosatti static inline void reset_hung_task_detector(void)
4428b414521SMarcelo Tosatti {
4438b414521SMarcelo Tosatti }
4448b414521SMarcelo Tosatti #endif
4458b414521SMarcelo Tosatti 
4461da177e4SLinus Torvalds /* Attach to any functions which should be ignored in wchan output. */
4471da177e4SLinus Torvalds #define __sched		__attribute__((__section__(".sched.text")))
448deaf2227SIngo Molnar 
449deaf2227SIngo Molnar /* Linker adds these: start and end of __sched functions */
450deaf2227SIngo Molnar extern char __sched_text_start[], __sched_text_end[];
451deaf2227SIngo Molnar 
4521da177e4SLinus Torvalds /* Is this address in the __sched functions? */
4531da177e4SLinus Torvalds extern int in_sched_functions(unsigned long addr);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds #define	MAX_SCHEDULE_TIMEOUT	LONG_MAX
456b3c97528SHarvey Harrison extern signed long schedule_timeout(signed long timeout);
45764ed93a2SNishanth Aravamudan extern signed long schedule_timeout_interruptible(signed long timeout);
458294d5cc2SMatthew Wilcox extern signed long schedule_timeout_killable(signed long timeout);
45964ed93a2SNishanth Aravamudan extern signed long schedule_timeout_uninterruptible(signed long timeout);
46069b27bafSAndrew Morton extern signed long schedule_timeout_idle(signed long timeout);
4611da177e4SLinus Torvalds asmlinkage void schedule(void);
462c5491ea7SThomas Gleixner extern void schedule_preempt_disabled(void);
4631da177e4SLinus Torvalds 
4649cff8adeSNeilBrown extern long io_schedule_timeout(long timeout);
4659cff8adeSNeilBrown 
4669cff8adeSNeilBrown static inline void io_schedule(void)
4679cff8adeSNeilBrown {
4689cff8adeSNeilBrown 	io_schedule_timeout(MAX_SCHEDULE_TIMEOUT);
4699cff8adeSNeilBrown }
4709cff8adeSNeilBrown 
4719af6528eSPeter Zijlstra void __noreturn do_task_dead(void);
4729af6528eSPeter Zijlstra 
473ab516013SSerge E. Hallyn struct nsproxy;
474acce292cSCedric Le Goater struct user_namespace;
4751da177e4SLinus Torvalds 
476efc1a3b1SDavid Howells #ifdef CONFIG_MMU
477efc1a3b1SDavid Howells extern void arch_pick_mmap_layout(struct mm_struct *mm);
4781da177e4SLinus Torvalds extern unsigned long
4791da177e4SLinus Torvalds arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
4801da177e4SLinus Torvalds 		       unsigned long, unsigned long);
4811da177e4SLinus Torvalds extern unsigned long
4821da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
4831da177e4SLinus Torvalds 			  unsigned long len, unsigned long pgoff,
4841da177e4SLinus Torvalds 			  unsigned long flags);
485efc1a3b1SDavid Howells #else
486efc1a3b1SDavid Howells static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
487efc1a3b1SDavid Howells #endif
4881da177e4SLinus Torvalds 
489d049f74fSKees Cook #define SUID_DUMP_DISABLE	0	/* No setuid dumping */
490d049f74fSKees Cook #define SUID_DUMP_USER		1	/* Dump as user of process */
491d049f74fSKees Cook #define SUID_DUMP_ROOT		2	/* Dump as root */
492d049f74fSKees Cook 
4936c5d5238SKawai, Hidehiro /* mm flags */
494f8af4da3SHugh Dickins 
4957288e118SOleg Nesterov /* for SUID_DUMP_* above */
4963cb4a0bbSKawai, Hidehiro #define MMF_DUMPABLE_BITS 2
497f8af4da3SHugh Dickins #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
4983cb4a0bbSKawai, Hidehiro 
499942be387SOleg Nesterov extern void set_dumpable(struct mm_struct *mm, int value);
500942be387SOleg Nesterov /*
501942be387SOleg Nesterov  * This returns the actual value of the suid_dumpable flag. For things
502942be387SOleg Nesterov  * that are using this for checking for privilege transitions, it must
503942be387SOleg Nesterov  * test against SUID_DUMP_USER rather than treating it as a boolean
504942be387SOleg Nesterov  * value.
505942be387SOleg Nesterov  */
506942be387SOleg Nesterov static inline int __get_dumpable(unsigned long mm_flags)
507942be387SOleg Nesterov {
508942be387SOleg Nesterov 	return mm_flags & MMF_DUMPABLE_MASK;
509942be387SOleg Nesterov }
510942be387SOleg Nesterov 
511942be387SOleg Nesterov static inline int get_dumpable(struct mm_struct *mm)
512942be387SOleg Nesterov {
513942be387SOleg Nesterov 	return __get_dumpable(mm->flags);
514942be387SOleg Nesterov }
515942be387SOleg Nesterov 
5163cb4a0bbSKawai, Hidehiro /* coredump filter bits */
5173cb4a0bbSKawai, Hidehiro #define MMF_DUMP_ANON_PRIVATE	2
5183cb4a0bbSKawai, Hidehiro #define MMF_DUMP_ANON_SHARED	3
5193cb4a0bbSKawai, Hidehiro #define MMF_DUMP_MAPPED_PRIVATE	4
5203cb4a0bbSKawai, Hidehiro #define MMF_DUMP_MAPPED_SHARED	5
52182df3973SRoland McGrath #define MMF_DUMP_ELF_HEADERS	6
522e575f111SKOSAKI Motohiro #define MMF_DUMP_HUGETLB_PRIVATE 7
523e575f111SKOSAKI Motohiro #define MMF_DUMP_HUGETLB_SHARED  8
5245037835cSRoss Zwisler #define MMF_DUMP_DAX_PRIVATE	9
5255037835cSRoss Zwisler #define MMF_DUMP_DAX_SHARED	10
526f8af4da3SHugh Dickins 
5273cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_SHIFT	MMF_DUMPABLE_BITS
5285037835cSRoss Zwisler #define MMF_DUMP_FILTER_BITS	9
5293cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_MASK \
5303cb4a0bbSKawai, Hidehiro 	(((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
5313cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_DEFAULT \
532e575f111SKOSAKI Motohiro 	((1 << MMF_DUMP_ANON_PRIVATE) |	(1 << MMF_DUMP_ANON_SHARED) |\
533656eb2cdSRoland McGrath 	 (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
534656eb2cdSRoland McGrath 
535656eb2cdSRoland McGrath #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
536656eb2cdSRoland McGrath # define MMF_DUMP_MASK_DEFAULT_ELF	(1 << MMF_DUMP_ELF_HEADERS)
537656eb2cdSRoland McGrath #else
538656eb2cdSRoland McGrath # define MMF_DUMP_MASK_DEFAULT_ELF	0
539656eb2cdSRoland McGrath #endif
540f8af4da3SHugh Dickins 					/* leave room for more dump flags */
541f8af4da3SHugh Dickins #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
542ba76149fSAndrea Arcangeli #define MMF_VM_HUGEPAGE		17	/* set when VM_HUGEPAGE is set on vma */
543bafb282dSKonstantin Khlebnikov #define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
544f8af4da3SHugh Dickins 
5459f68f672SOleg Nesterov #define MMF_HAS_UPROBES		19	/* has uprobes */
5469f68f672SOleg Nesterov #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
547862e3073SMichal Hocko #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
5483f70dc38SMichal Hocko #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
5496fcb52a5SAaron Lu #define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
550f8ac4ec9SOleg Nesterov 
551f8af4da3SHugh Dickins #define MMF_INIT_MASK		(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK)
5526c5d5238SKawai, Hidehiro 
5531da177e4SLinus Torvalds struct sighand_struct {
5541da177e4SLinus Torvalds 	atomic_t		count;
5551da177e4SLinus Torvalds 	struct k_sigaction	action[_NSIG];
5561da177e4SLinus Torvalds 	spinlock_t		siglock;
557b8fceee1SDavide Libenzi 	wait_queue_head_t	signalfd_wqh;
5581da177e4SLinus Torvalds };
5591da177e4SLinus Torvalds 
5600e464814SKaiGai Kohei struct pacct_struct {
561f6ec29a4SKaiGai Kohei 	int			ac_flag;
562f6ec29a4SKaiGai Kohei 	long			ac_exitcode;
5630e464814SKaiGai Kohei 	unsigned long		ac_mem;
56477787bfbSKaiGai Kohei 	cputime_t		ac_utime, ac_stime;
56577787bfbSKaiGai Kohei 	unsigned long		ac_minflt, ac_majflt;
5660e464814SKaiGai Kohei };
5670e464814SKaiGai Kohei 
56842c4ab41SStanislaw Gruszka struct cpu_itimer {
56942c4ab41SStanislaw Gruszka 	cputime_t expires;
57042c4ab41SStanislaw Gruszka 	cputime_t incr;
5718356b5f9SStanislaw Gruszka 	u32 error;
5728356b5f9SStanislaw Gruszka 	u32 incr_error;
57342c4ab41SStanislaw Gruszka };
57442c4ab41SStanislaw Gruszka 
575f06febc9SFrank Mayhar /**
5769d7fb042SPeter Zijlstra  * struct prev_cputime - snaphsot of system and user cputime
577d37f761dSFrederic Weisbecker  * @utime: time spent in user mode
578d37f761dSFrederic Weisbecker  * @stime: time spent in system mode
5799d7fb042SPeter Zijlstra  * @lock: protects the above two fields
580d37f761dSFrederic Weisbecker  *
5819d7fb042SPeter Zijlstra  * Stores previous user/system time values such that we can guarantee
5829d7fb042SPeter Zijlstra  * monotonicity.
583d37f761dSFrederic Weisbecker  */
5849d7fb042SPeter Zijlstra struct prev_cputime {
5859d7fb042SPeter Zijlstra #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
586d37f761dSFrederic Weisbecker 	cputime_t utime;
587d37f761dSFrederic Weisbecker 	cputime_t stime;
5889d7fb042SPeter Zijlstra 	raw_spinlock_t lock;
5899d7fb042SPeter Zijlstra #endif
590d37f761dSFrederic Weisbecker };
591d37f761dSFrederic Weisbecker 
5929d7fb042SPeter Zijlstra static inline void prev_cputime_init(struct prev_cputime *prev)
5939d7fb042SPeter Zijlstra {
5949d7fb042SPeter Zijlstra #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
5959d7fb042SPeter Zijlstra 	prev->utime = prev->stime = 0;
5969d7fb042SPeter Zijlstra 	raw_spin_lock_init(&prev->lock);
5979d7fb042SPeter Zijlstra #endif
5989d7fb042SPeter Zijlstra }
5999d7fb042SPeter Zijlstra 
600d37f761dSFrederic Weisbecker /**
601f06febc9SFrank Mayhar  * struct task_cputime - collected CPU time counts
602f06febc9SFrank Mayhar  * @utime:		time spent in user mode, in &cputime_t units
603f06febc9SFrank Mayhar  * @stime:		time spent in kernel mode, in &cputime_t units
604f06febc9SFrank Mayhar  * @sum_exec_runtime:	total time spent on the CPU, in nanoseconds
605f06febc9SFrank Mayhar  *
6069d7fb042SPeter Zijlstra  * This structure groups together three kinds of CPU time that are tracked for
6079d7fb042SPeter Zijlstra  * threads and thread groups.  Most things considering CPU time want to group
6089d7fb042SPeter Zijlstra  * these counts together and treat all three of them in parallel.
609f06febc9SFrank Mayhar  */
610f06febc9SFrank Mayhar struct task_cputime {
611f06febc9SFrank Mayhar 	cputime_t utime;
612f06febc9SFrank Mayhar 	cputime_t stime;
613f06febc9SFrank Mayhar 	unsigned long long sum_exec_runtime;
614f06febc9SFrank Mayhar };
6159d7fb042SPeter Zijlstra 
616f06febc9SFrank Mayhar /* Alternate field names when used to cache expirations. */
617f06febc9SFrank Mayhar #define virt_exp	utime
6189d7fb042SPeter Zijlstra #define prof_exp	stime
619f06febc9SFrank Mayhar #define sched_exp	sum_exec_runtime
620f06febc9SFrank Mayhar 
6214cd4c1b4SPeter Zijlstra #define INIT_CPUTIME	\
6224cd4c1b4SPeter Zijlstra 	(struct task_cputime) {					\
62364861634SMartin Schwidefsky 		.utime = 0,					\
62464861634SMartin Schwidefsky 		.stime = 0,					\
6254cd4c1b4SPeter Zijlstra 		.sum_exec_runtime = 0,				\
6264cd4c1b4SPeter Zijlstra 	}
6274cd4c1b4SPeter Zijlstra 
628971e8a98SJason Low /*
629971e8a98SJason Low  * This is the atomic variant of task_cputime, which can be used for
630971e8a98SJason Low  * storing and updating task_cputime statistics without locking.
631971e8a98SJason Low  */
632971e8a98SJason Low struct task_cputime_atomic {
633971e8a98SJason Low 	atomic64_t utime;
634971e8a98SJason Low 	atomic64_t stime;
635971e8a98SJason Low 	atomic64_t sum_exec_runtime;
636971e8a98SJason Low };
637971e8a98SJason Low 
638971e8a98SJason Low #define INIT_CPUTIME_ATOMIC \
639971e8a98SJason Low 	(struct task_cputime_atomic) {				\
640971e8a98SJason Low 		.utime = ATOMIC64_INIT(0),			\
641971e8a98SJason Low 		.stime = ATOMIC64_INIT(0),			\
642971e8a98SJason Low 		.sum_exec_runtime = ATOMIC64_INIT(0),		\
643971e8a98SJason Low 	}
644971e8a98SJason Low 
645609ca066SPeter Zijlstra #define PREEMPT_DISABLED	(PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
646a233f112SPeter Zijlstra 
647c99e6efeSPeter Zijlstra /*
64887dcbc06SPeter Zijlstra  * Disable preemption until the scheduler is running -- use an unconditional
64987dcbc06SPeter Zijlstra  * value so that it also works on !PREEMPT_COUNT kernels.
650d86ee480SPeter Zijlstra  *
65187dcbc06SPeter Zijlstra  * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
652c99e6efeSPeter Zijlstra  */
65387dcbc06SPeter Zijlstra #define INIT_PREEMPT_COUNT	PREEMPT_OFFSET
654c99e6efeSPeter Zijlstra 
655609ca066SPeter Zijlstra /*
656609ca066SPeter Zijlstra  * Initial preempt_count value; reflects the preempt_count schedule invariant
657609ca066SPeter Zijlstra  * which states that during context switches:
658609ca066SPeter Zijlstra  *
659609ca066SPeter Zijlstra  *    preempt_count() == 2*PREEMPT_DISABLE_OFFSET
660609ca066SPeter Zijlstra  *
661609ca066SPeter Zijlstra  * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
662609ca066SPeter Zijlstra  * Note: See finish_task_switch().
663609ca066SPeter Zijlstra  */
664609ca066SPeter Zijlstra #define FORK_PREEMPT_COUNT	(2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
6654cd4c1b4SPeter Zijlstra 
666f06febc9SFrank Mayhar /**
667f06febc9SFrank Mayhar  * struct thread_group_cputimer - thread group interval timer counts
668920ce39fSJason Low  * @cputime_atomic:	atomic thread group interval timers.
669d5c373ebSJason Low  * @running:		true when there are timers running and
670d5c373ebSJason Low  *			@cputime_atomic receives updates.
671c8d75aa4SJason Low  * @checking_timer:	true when a thread in the group is in the
672c8d75aa4SJason Low  *			process of checking for thread group timers.
673f06febc9SFrank Mayhar  *
674f06febc9SFrank Mayhar  * This structure contains the version of task_cputime, above, that is
6754cd4c1b4SPeter Zijlstra  * used for thread group CPU timer calculations.
676f06febc9SFrank Mayhar  */
6774cd4c1b4SPeter Zijlstra struct thread_group_cputimer {
67871107445SJason Low 	struct task_cputime_atomic cputime_atomic;
679d5c373ebSJason Low 	bool running;
680c8d75aa4SJason Low 	bool checking_timer;
681f06febc9SFrank Mayhar };
682f06febc9SFrank Mayhar 
6834714d1d3SBen Blum #include <linux/rwsem.h>
6845091faa4SMike Galbraith struct autogroup;
6855091faa4SMike Galbraith 
6861da177e4SLinus Torvalds /*
687e815f0a8SJonathan Neuschäfer  * NOTE! "signal_struct" does not have its own
6881da177e4SLinus Torvalds  * locking, because a shared signal_struct always
6891da177e4SLinus Torvalds  * implies a shared sighand_struct, so locking
6901da177e4SLinus Torvalds  * sighand_struct is always a proper superset of
6911da177e4SLinus Torvalds  * the locking of signal_struct.
6921da177e4SLinus Torvalds  */
6931da177e4SLinus Torvalds struct signal_struct {
694ea6d290cSOleg Nesterov 	atomic_t		sigcnt;
6951da177e4SLinus Torvalds 	atomic_t		live;
696b3ac022cSOleg Nesterov 	int			nr_threads;
6970c740d0aSOleg Nesterov 	struct list_head	thread_head;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	wait_queue_head_t	wait_chldexit;	/* for wait4() */
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* current thread group signal load-balancing target: */
70236c8b586SIngo Molnar 	struct task_struct	*curr_target;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	/* shared signal handling: */
7051da177e4SLinus Torvalds 	struct sigpending	shared_pending;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	/* thread group exit support */
7081da177e4SLinus Torvalds 	int			group_exit_code;
7091da177e4SLinus Torvalds 	/* overloaded:
7101da177e4SLinus Torvalds 	 * - notify group_exit_task when ->count is equal to notify_count
7111da177e4SLinus Torvalds 	 * - everyone except group_exit_task is stopped during signal delivery
7121da177e4SLinus Torvalds 	 *   of fatal signals, group_exit_task processes the signal.
7131da177e4SLinus Torvalds 	 */
7141da177e4SLinus Torvalds 	int			notify_count;
71507dd20e0SRichard Kennedy 	struct task_struct	*group_exit_task;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* thread group stop support, overloads group_exit_code too */
7181da177e4SLinus Torvalds 	int			group_stop_count;
7191da177e4SLinus Torvalds 	unsigned int		flags; /* see SIGNAL_* flags below */
7201da177e4SLinus Torvalds 
721ebec18a6SLennart Poettering 	/*
722ebec18a6SLennart Poettering 	 * PR_SET_CHILD_SUBREAPER marks a process, like a service
723ebec18a6SLennart Poettering 	 * manager, to re-parent orphan (double-forking) child processes
724ebec18a6SLennart Poettering 	 * to this process instead of 'init'. The service manager is
725ebec18a6SLennart Poettering 	 * able to receive SIGCHLD signals and is able to investigate
726ebec18a6SLennart Poettering 	 * the process until it calls wait(). All children of this
727ebec18a6SLennart Poettering 	 * process will inherit a flag if they should look for a
728ebec18a6SLennart Poettering 	 * child_subreaper process at exit.
729ebec18a6SLennart Poettering 	 */
730ebec18a6SLennart Poettering 	unsigned int		is_child_subreaper:1;
731ebec18a6SLennart Poettering 	unsigned int		has_child_subreaper:1;
732ebec18a6SLennart Poettering 
7331da177e4SLinus Torvalds 	/* POSIX.1b Interval Timers */
7345ed67f05SPavel Emelyanov 	int			posix_timer_id;
7351da177e4SLinus Torvalds 	struct list_head	posix_timers;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	/* ITIMER_REAL timer for the process */
7382ff678b8SThomas Gleixner 	struct hrtimer real_timer;
739fea9d175SOleg Nesterov 	struct pid *leader_pid;
7402ff678b8SThomas Gleixner 	ktime_t it_real_incr;
7411da177e4SLinus Torvalds 
74242c4ab41SStanislaw Gruszka 	/*
74342c4ab41SStanislaw Gruszka 	 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
74442c4ab41SStanislaw Gruszka 	 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
74542c4ab41SStanislaw Gruszka 	 * values are defined to 0 and 1 respectively
74642c4ab41SStanislaw Gruszka 	 */
74742c4ab41SStanislaw Gruszka 	struct cpu_itimer it[2];
7481da177e4SLinus Torvalds 
749f06febc9SFrank Mayhar 	/*
7504cd4c1b4SPeter Zijlstra 	 * Thread group totals for process CPU timers.
7514cd4c1b4SPeter Zijlstra 	 * See thread_group_cputimer(), et al, for details.
752f06febc9SFrank Mayhar 	 */
7534cd4c1b4SPeter Zijlstra 	struct thread_group_cputimer cputimer;
754f06febc9SFrank Mayhar 
755f06febc9SFrank Mayhar 	/* Earliest-expiration cache. */
756f06febc9SFrank Mayhar 	struct task_cputime cputime_expires;
757f06febc9SFrank Mayhar 
758d027d45dSFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
759f009a7a7SFrederic Weisbecker 	atomic_t tick_dep_mask;
760d027d45dSFrederic Weisbecker #endif
761d027d45dSFrederic Weisbecker 
762f06febc9SFrank Mayhar 	struct list_head cpu_timers[3];
763f06febc9SFrank Mayhar 
764ab521dc0SEric W. Biederman 	struct pid *tty_old_pgrp;
7651ec320afSCedric Le Goater 
7661da177e4SLinus Torvalds 	/* boolean value for session group leader */
7671da177e4SLinus Torvalds 	int leader;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	struct tty_struct *tty; /* NULL if no tty */
7701da177e4SLinus Torvalds 
7715091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
7725091faa4SMike Galbraith 	struct autogroup *autogroup;
7735091faa4SMike Galbraith #endif
7741da177e4SLinus Torvalds 	/*
7751da177e4SLinus Torvalds 	 * Cumulative resource counters for dead threads in the group,
7761da177e4SLinus Torvalds 	 * and for reaped dead child processes forked by this group.
7771da177e4SLinus Torvalds 	 * Live threads maintain their own counters and add to these
7781da177e4SLinus Torvalds 	 * in __exit_signal, except for the group leader.
7791da177e4SLinus Torvalds 	 */
780e78c3496SRik van Riel 	seqlock_t stats_lock;
78132bd671dSPeter Zijlstra 	cputime_t utime, stime, cutime, cstime;
7829ac52315SLaurent Vivier 	cputime_t gtime;
7839ac52315SLaurent Vivier 	cputime_t cgtime;
7849d7fb042SPeter Zijlstra 	struct prev_cputime prev_cputime;
7851da177e4SLinus Torvalds 	unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
7861da177e4SLinus Torvalds 	unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
7876eaeeabaSEric Dumazet 	unsigned long inblock, oublock, cinblock, coublock;
7881f10206cSJiri Pirko 	unsigned long maxrss, cmaxrss;
789940389b8SAndrea Righi 	struct task_io_accounting ioac;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/*
79232bd671dSPeter Zijlstra 	 * Cumulative ns of schedule CPU time fo dead threads in the
79332bd671dSPeter Zijlstra 	 * group, not including a zombie group leader, (This only differs
79432bd671dSPeter Zijlstra 	 * from jiffies_to_ns(utime + stime) if sched_clock uses something
79532bd671dSPeter Zijlstra 	 * other than jiffies.)
79632bd671dSPeter Zijlstra 	 */
79732bd671dSPeter Zijlstra 	unsigned long long sum_sched_runtime;
79832bd671dSPeter Zijlstra 
79932bd671dSPeter Zijlstra 	/*
8001da177e4SLinus Torvalds 	 * We don't bother to synchronize most readers of this at all,
8011da177e4SLinus Torvalds 	 * because there is no reader checking a limit that actually needs
8021da177e4SLinus Torvalds 	 * to get both rlim_cur and rlim_max atomically, and either one
8031da177e4SLinus Torvalds 	 * alone is a single word that can safely be read normally.
8041da177e4SLinus Torvalds 	 * getrlimit/setrlimit use task_lock(current->group_leader) to
8051da177e4SLinus Torvalds 	 * protect this instead of the siglock, because they really
8061da177e4SLinus Torvalds 	 * have no need to disable irqs.
8071da177e4SLinus Torvalds 	 */
8081da177e4SLinus Torvalds 	struct rlimit rlim[RLIM_NLIMITS];
8091da177e4SLinus Torvalds 
8100e464814SKaiGai Kohei #ifdef CONFIG_BSD_PROCESS_ACCT
8110e464814SKaiGai Kohei 	struct pacct_struct pacct;	/* per-process accounting information */
8120e464814SKaiGai Kohei #endif
813ad4ecbcbSShailabh Nagar #ifdef CONFIG_TASKSTATS
814ad4ecbcbSShailabh Nagar 	struct taskstats *stats;
815ad4ecbcbSShailabh Nagar #endif
816522ed776SMiloslav Trmac #ifdef CONFIG_AUDIT
817522ed776SMiloslav Trmac 	unsigned audit_tty;
818522ed776SMiloslav Trmac 	struct tty_audit_buf *tty_audit_buf;
819522ed776SMiloslav Trmac #endif
82028b83c51SKOSAKI Motohiro 
821c96fc2d8STetsuo Handa 	/*
822c96fc2d8STetsuo Handa 	 * Thread is the potential origin of an oom condition; kill first on
823c96fc2d8STetsuo Handa 	 * oom
824c96fc2d8STetsuo Handa 	 */
825c96fc2d8STetsuo Handa 	bool oom_flag_origin;
826a9c58b90SDavid Rientjes 	short oom_score_adj;		/* OOM kill score adjustment */
827a9c58b90SDavid Rientjes 	short oom_score_adj_min;	/* OOM kill score adjustment min value.
828dabb16f6SMandeep Singh Baines 					 * Only settable by CAP_SYS_RESOURCE. */
82926db62f1SMichal Hocko 	struct mm_struct *oom_mm;	/* recorded mm when the thread group got
83026db62f1SMichal Hocko 					 * killed by the oom killer */
8319b1bf12dSKOSAKI Motohiro 
8329b1bf12dSKOSAKI Motohiro 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
8339b1bf12dSKOSAKI Motohiro 					 * credential calculations
8349b1bf12dSKOSAKI Motohiro 					 * (notably. ptrace) */
8351da177e4SLinus Torvalds };
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds /*
8381da177e4SLinus Torvalds  * Bits in flags field of signal_struct.
8391da177e4SLinus Torvalds  */
8401da177e4SLinus Torvalds #define SIGNAL_STOP_STOPPED	0x00000001 /* job control stop in effect */
841ee77f075SOleg Nesterov #define SIGNAL_STOP_CONTINUED	0x00000002 /* SIGCONT since WCONTINUED reap */
842ee77f075SOleg Nesterov #define SIGNAL_GROUP_EXIT	0x00000004 /* group exit in progress */
843403bad72SOleg Nesterov #define SIGNAL_GROUP_COREDUMP	0x00000008 /* coredump in progress */
844e4420551SOleg Nesterov /*
845e4420551SOleg Nesterov  * Pending notifications to parent.
846e4420551SOleg Nesterov  */
847e4420551SOleg Nesterov #define SIGNAL_CLD_STOPPED	0x00000010
848e4420551SOleg Nesterov #define SIGNAL_CLD_CONTINUED	0x00000020
849e4420551SOleg Nesterov #define SIGNAL_CLD_MASK		(SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
8501da177e4SLinus Torvalds 
851fae5fa44SOleg Nesterov #define SIGNAL_UNKILLABLE	0x00000040 /* for init: ignore fatal signals */
852fae5fa44SOleg Nesterov 
853ed5d2cacSOleg Nesterov /* If true, all threads except ->group_exit_task have pending SIGKILL */
854ed5d2cacSOleg Nesterov static inline int signal_group_exit(const struct signal_struct *sig)
855ed5d2cacSOleg Nesterov {
856ed5d2cacSOleg Nesterov 	return	(sig->flags & SIGNAL_GROUP_EXIT) ||
857ed5d2cacSOleg Nesterov 		(sig->group_exit_task != NULL);
858ed5d2cacSOleg Nesterov }
859ed5d2cacSOleg Nesterov 
8601da177e4SLinus Torvalds /*
8611da177e4SLinus Torvalds  * Some day this will be a full-fledged user tracking system..
8621da177e4SLinus Torvalds  */
8631da177e4SLinus Torvalds struct user_struct {
8641da177e4SLinus Torvalds 	atomic_t __count;	/* reference count */
8651da177e4SLinus Torvalds 	atomic_t processes;	/* How many processes does this user have? */
8661da177e4SLinus Torvalds 	atomic_t sigpending;	/* How many pending signals does this user have? */
8672d9048e2SAmy Griffis #ifdef CONFIG_INOTIFY_USER
8680eeca283SRobert Love 	atomic_t inotify_watches; /* How many inotify watches does this user have? */
8690eeca283SRobert Love 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
8700eeca283SRobert Love #endif
8714afeff85SEric Paris #ifdef CONFIG_FANOTIFY
8724afeff85SEric Paris 	atomic_t fanotify_listeners;
8734afeff85SEric Paris #endif
8747ef9964eSDavide Libenzi #ifdef CONFIG_EPOLL
87552bd19f7SRobin Holt 	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
8767ef9964eSDavide Libenzi #endif
877970a8645SAlexey Dobriyan #ifdef CONFIG_POSIX_MQUEUE
8781da177e4SLinus Torvalds 	/* protected by mq_lock	*/
8791da177e4SLinus Torvalds 	unsigned long mq_bytes;	/* How many bytes can be allocated to mqueue? */
880970a8645SAlexey Dobriyan #endif
8811da177e4SLinus Torvalds 	unsigned long locked_shm; /* How many pages of mlocked shm ? */
882712f4aadSwilly tarreau 	unsigned long unix_inflight;	/* How many files in flight in unix sockets */
883759c0114SWilly Tarreau 	atomic_long_t pipe_bufs;  /* how many pages are allocated in pipe buffers */
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds #ifdef CONFIG_KEYS
8861da177e4SLinus Torvalds 	struct key *uid_keyring;	/* UID specific keyring */
8871da177e4SLinus Torvalds 	struct key *session_keyring;	/* UID's default session keyring */
8881da177e4SLinus Torvalds #endif
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* Hash table maintenance information */
891735de223SPavel Emelyanov 	struct hlist_node uidhash_node;
8927b44ab97SEric W. Biederman 	kuid_t uid;
89324e377a8SSrivatsa Vaddagiri 
894aaac3ba9SAlexei Starovoitov #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL)
895789f90fcSPeter Zijlstra 	atomic_long_t locked_vm;
896789f90fcSPeter Zijlstra #endif
8971da177e4SLinus Torvalds };
8981da177e4SLinus Torvalds 
899eb41d946SKay Sievers extern int uids_sysfs_init(void);
9005cb350baSDhaval Giani 
9017b44ab97SEric W. Biederman extern struct user_struct *find_user(kuid_t);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds extern struct user_struct root_user;
9041da177e4SLinus Torvalds #define INIT_USER (&root_user)
9051da177e4SLinus Torvalds 
906b6dff3ecSDavid Howells 
9071da177e4SLinus Torvalds struct backing_dev_info;
9081da177e4SLinus Torvalds struct reclaim_state;
9091da177e4SLinus Torvalds 
910f6db8347SNaveen N. Rao #ifdef CONFIG_SCHED_INFO
9111da177e4SLinus Torvalds struct sched_info {
9121da177e4SLinus Torvalds 	/* cumulative counters */
9132d72376bSIngo Molnar 	unsigned long pcount;	      /* # of times run on this cpu */
9149c2c4802SKen Chen 	unsigned long long run_delay; /* time spent waiting on a runqueue */
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	/* timestamps */
917172ba844SBalbir Singh 	unsigned long long last_arrival,/* when we last ran on a cpu */
9181da177e4SLinus Torvalds 			   last_queued;	/* when we were last queued to run */
9191da177e4SLinus Torvalds };
920f6db8347SNaveen N. Rao #endif /* CONFIG_SCHED_INFO */
9211da177e4SLinus Torvalds 
922ca74e92bSShailabh Nagar #ifdef CONFIG_TASK_DELAY_ACCT
923ca74e92bSShailabh Nagar struct task_delay_info {
924ca74e92bSShailabh Nagar 	spinlock_t	lock;
925ca74e92bSShailabh Nagar 	unsigned int	flags;	/* Private per-task flags */
926ca74e92bSShailabh Nagar 
927ca74e92bSShailabh Nagar 	/* For each stat XXX, add following, aligned appropriately
928ca74e92bSShailabh Nagar 	 *
929ca74e92bSShailabh Nagar 	 * struct timespec XXX_start, XXX_end;
930ca74e92bSShailabh Nagar 	 * u64 XXX_delay;
931ca74e92bSShailabh Nagar 	 * u32 XXX_count;
932ca74e92bSShailabh Nagar 	 *
933ca74e92bSShailabh Nagar 	 * Atomicity of updates to XXX_delay, XXX_count protected by
934ca74e92bSShailabh Nagar 	 * single lock above (split into XXX_lock if contention is an issue).
935ca74e92bSShailabh Nagar 	 */
9360ff92245SShailabh Nagar 
9370ff92245SShailabh Nagar 	/*
9380ff92245SShailabh Nagar 	 * XXX_count is incremented on every XXX operation, the delay
9390ff92245SShailabh Nagar 	 * associated with the operation is added to XXX_delay.
9400ff92245SShailabh Nagar 	 * XXX_delay contains the accumulated delay time in nanoseconds.
9410ff92245SShailabh Nagar 	 */
9429667a23dSThomas Gleixner 	u64 blkio_start;	/* Shared by blkio, swapin */
9430ff92245SShailabh Nagar 	u64 blkio_delay;	/* wait for sync block io completion */
9440ff92245SShailabh Nagar 	u64 swapin_delay;	/* wait for swapin block io completion */
9450ff92245SShailabh Nagar 	u32 blkio_count;	/* total count of the number of sync block */
9460ff92245SShailabh Nagar 				/* io operations performed */
9470ff92245SShailabh Nagar 	u32 swapin_count;	/* total count of the number of swapin block */
9480ff92245SShailabh Nagar 				/* io operations performed */
949873b4771SKeika Kobayashi 
9509667a23dSThomas Gleixner 	u64 freepages_start;
951873b4771SKeika Kobayashi 	u64 freepages_delay;	/* wait for memory reclaim */
952873b4771SKeika Kobayashi 	u32 freepages_count;	/* total count of memory reclaim */
953ca74e92bSShailabh Nagar };
95452f17b6cSChandra Seetharaman #endif	/* CONFIG_TASK_DELAY_ACCT */
95552f17b6cSChandra Seetharaman 
95652f17b6cSChandra Seetharaman static inline int sched_info_on(void)
95752f17b6cSChandra Seetharaman {
95852f17b6cSChandra Seetharaman #ifdef CONFIG_SCHEDSTATS
95952f17b6cSChandra Seetharaman 	return 1;
96052f17b6cSChandra Seetharaman #elif defined(CONFIG_TASK_DELAY_ACCT)
96152f17b6cSChandra Seetharaman 	extern int delayacct_on;
96252f17b6cSChandra Seetharaman 	return delayacct_on;
96352f17b6cSChandra Seetharaman #else
96452f17b6cSChandra Seetharaman 	return 0;
965ca74e92bSShailabh Nagar #endif
96652f17b6cSChandra Seetharaman }
967ca74e92bSShailabh Nagar 
968cb251765SMel Gorman #ifdef CONFIG_SCHEDSTATS
969cb251765SMel Gorman void force_schedstat_enabled(void);
970cb251765SMel Gorman #endif
971cb251765SMel Gorman 
972d15bcfdbSIngo Molnar enum cpu_idle_type {
973d15bcfdbSIngo Molnar 	CPU_IDLE,
974d15bcfdbSIngo Molnar 	CPU_NOT_IDLE,
975d15bcfdbSIngo Molnar 	CPU_NEWLY_IDLE,
976d15bcfdbSIngo Molnar 	CPU_MAX_IDLE_TYPES
9771da177e4SLinus Torvalds };
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds /*
9806ecdd749SYuyang Du  * Integer metrics need fixed point arithmetic, e.g., sched/fair
9816ecdd749SYuyang Du  * has a few: load, load_avg, util_avg, freq, and capacity.
9826ecdd749SYuyang Du  *
9836ecdd749SYuyang Du  * We define a basic fixed point arithmetic range, and then formalize
9846ecdd749SYuyang Du  * all these metrics based on that basic range.
9856ecdd749SYuyang Du  */
9866ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SHIFT	10
9876ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SCALE	(1L << SCHED_FIXEDPOINT_SHIFT)
9886ecdd749SYuyang Du 
9896ecdd749SYuyang Du /*
990ca8ce3d0SNicolas Pitre  * Increase resolution of cpu_capacity calculations
9911399fa78SNikhil Rao  */
9926ecdd749SYuyang Du #define SCHED_CAPACITY_SHIFT	SCHED_FIXEDPOINT_SHIFT
993ca8ce3d0SNicolas Pitre #define SCHED_CAPACITY_SCALE	(1L << SCHED_CAPACITY_SHIFT)
9941da177e4SLinus Torvalds 
9951399fa78SNikhil Rao /*
99676751049SPeter Zijlstra  * Wake-queues are lists of tasks with a pending wakeup, whose
99776751049SPeter Zijlstra  * callers have already marked the task as woken internally,
99876751049SPeter Zijlstra  * and can thus carry on. A common use case is being able to
99976751049SPeter Zijlstra  * do the wakeups once the corresponding user lock as been
100076751049SPeter Zijlstra  * released.
100176751049SPeter Zijlstra  *
100276751049SPeter Zijlstra  * We hold reference to each task in the list across the wakeup,
100376751049SPeter Zijlstra  * thus guaranteeing that the memory is still valid by the time
100476751049SPeter Zijlstra  * the actual wakeups are performed in wake_up_q().
100576751049SPeter Zijlstra  *
100676751049SPeter Zijlstra  * One per task suffices, because there's never a need for a task to be
100776751049SPeter Zijlstra  * in two wake queues simultaneously; it is forbidden to abandon a task
100876751049SPeter Zijlstra  * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
100976751049SPeter Zijlstra  * already in a wake queue, the wakeup will happen soon and the second
101076751049SPeter Zijlstra  * waker can just skip it.
101176751049SPeter Zijlstra  *
101276751049SPeter Zijlstra  * The WAKE_Q macro declares and initializes the list head.
101376751049SPeter Zijlstra  * wake_up_q() does NOT reinitialize the list; it's expected to be
101476751049SPeter Zijlstra  * called near the end of a function, where the fact that the queue is
101576751049SPeter Zijlstra  * not used again will be easy to see by inspection.
101676751049SPeter Zijlstra  *
101776751049SPeter Zijlstra  * Note that this can cause spurious wakeups. schedule() callers
101876751049SPeter Zijlstra  * must ensure the call is done inside a loop, confirming that the
101976751049SPeter Zijlstra  * wakeup condition has in fact occurred.
102076751049SPeter Zijlstra  */
102176751049SPeter Zijlstra struct wake_q_node {
102276751049SPeter Zijlstra 	struct wake_q_node *next;
102376751049SPeter Zijlstra };
102476751049SPeter Zijlstra 
102576751049SPeter Zijlstra struct wake_q_head {
102676751049SPeter Zijlstra 	struct wake_q_node *first;
102776751049SPeter Zijlstra 	struct wake_q_node **lastp;
102876751049SPeter Zijlstra };
102976751049SPeter Zijlstra 
103076751049SPeter Zijlstra #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
103176751049SPeter Zijlstra 
103276751049SPeter Zijlstra #define WAKE_Q(name)					\
103376751049SPeter Zijlstra 	struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
103476751049SPeter Zijlstra 
103576751049SPeter Zijlstra extern void wake_q_add(struct wake_q_head *head,
103676751049SPeter Zijlstra 		       struct task_struct *task);
103776751049SPeter Zijlstra extern void wake_up_q(struct wake_q_head *head);
103876751049SPeter Zijlstra 
103976751049SPeter Zijlstra /*
10401399fa78SNikhil Rao  * sched-domains (multiprocessor balancing) declarations:
10411399fa78SNikhil Rao  */
10422dd73a4fSPeter Williams #ifdef CONFIG_SMP
1043b5d978e0SPeter Zijlstra #define SD_LOAD_BALANCE		0x0001	/* Do load balancing on this domain. */
1044b5d978e0SPeter Zijlstra #define SD_BALANCE_NEWIDLE	0x0002	/* Balance when about to become idle */
1045b5d978e0SPeter Zijlstra #define SD_BALANCE_EXEC		0x0004	/* Balance on exec */
1046b5d978e0SPeter Zijlstra #define SD_BALANCE_FORK		0x0008	/* Balance on fork, clone */
1047c88d5910SPeter Zijlstra #define SD_BALANCE_WAKE		0x0010  /* Balance on wakeup */
1048b5d978e0SPeter Zijlstra #define SD_WAKE_AFFINE		0x0020	/* Wake task to waking CPU */
10491f6e6c7cSMorten Rasmussen #define SD_ASYM_CPUCAPACITY	0x0040  /* Groups have different max cpu capacities */
1050bd425d4bSMorten Rasmussen #define SD_SHARE_CPUCAPACITY	0x0080	/* Domain members share cpu capacity */
1051d77b3ed5SVincent Guittot #define SD_SHARE_POWERDOMAIN	0x0100	/* Domain members share power domain */
1052b5d978e0SPeter Zijlstra #define SD_SHARE_PKG_RESOURCES	0x0200	/* Domain members share cpu pkg resources */
1053b5d978e0SPeter Zijlstra #define SD_SERIALIZE		0x0400	/* Only a single load balancing instance */
1054532cb4c4SMichael Neuling #define SD_ASYM_PACKING		0x0800  /* Place busy groups earlier in the domain */
1055b5d978e0SPeter Zijlstra #define SD_PREFER_SIBLING	0x1000	/* Prefer to place tasks in a sibling domain */
1056e3589f6cSPeter Zijlstra #define SD_OVERLAP		0x2000	/* sched_domains of this level overlap */
10573a7053b3SMel Gorman #define SD_NUMA			0x4000	/* cross-node balancing */
10585c45bf27SSiddha, Suresh B 
1059143e1e28SVincent Guittot #ifdef CONFIG_SCHED_SMT
1060b6220ad6SGuenter Roeck static inline int cpu_smt_flags(void)
1061143e1e28SVincent Guittot {
10625d4dfdddSNicolas Pitre 	return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
1063143e1e28SVincent Guittot }
1064143e1e28SVincent Guittot #endif
1065143e1e28SVincent Guittot 
1066143e1e28SVincent Guittot #ifdef CONFIG_SCHED_MC
1067b6220ad6SGuenter Roeck static inline int cpu_core_flags(void)
1068143e1e28SVincent Guittot {
1069143e1e28SVincent Guittot 	return SD_SHARE_PKG_RESOURCES;
1070143e1e28SVincent Guittot }
1071143e1e28SVincent Guittot #endif
1072143e1e28SVincent Guittot 
1073143e1e28SVincent Guittot #ifdef CONFIG_NUMA
1074b6220ad6SGuenter Roeck static inline int cpu_numa_flags(void)
1075143e1e28SVincent Guittot {
1076143e1e28SVincent Guittot 	return SD_NUMA;
1077143e1e28SVincent Guittot }
1078143e1e28SVincent Guittot #endif
1079532cb4c4SMichael Neuling 
10801d3504fcSHidetoshi Seto struct sched_domain_attr {
10811d3504fcSHidetoshi Seto 	int relax_domain_level;
10821d3504fcSHidetoshi Seto };
10831d3504fcSHidetoshi Seto 
10841d3504fcSHidetoshi Seto #define SD_ATTR_INIT	(struct sched_domain_attr) {	\
10851d3504fcSHidetoshi Seto 	.relax_domain_level = -1,			\
10861d3504fcSHidetoshi Seto }
10871d3504fcSHidetoshi Seto 
108860495e77SPeter Zijlstra extern int sched_domain_level_max;
108960495e77SPeter Zijlstra 
10905e6521eaSLi Zefan struct sched_group;
10915e6521eaSLi Zefan 
109224fc7edbSPeter Zijlstra struct sched_domain_shared {
109324fc7edbSPeter Zijlstra 	atomic_t	ref;
10940e369d75SPeter Zijlstra 	atomic_t	nr_busy_cpus;
109510e2f1acSPeter Zijlstra 	int		has_idle_cores;
109624fc7edbSPeter Zijlstra };
109724fc7edbSPeter Zijlstra 
10981da177e4SLinus Torvalds struct sched_domain {
10991da177e4SLinus Torvalds 	/* These fields must be setup */
11001da177e4SLinus Torvalds 	struct sched_domain *parent;	/* top domain must be null terminated */
11011a848870SSiddha, Suresh B 	struct sched_domain *child;	/* bottom domain must be null terminated */
11021da177e4SLinus Torvalds 	struct sched_group *groups;	/* the balancing groups of the domain */
11031da177e4SLinus Torvalds 	unsigned long min_interval;	/* Minimum balance interval ms */
11041da177e4SLinus Torvalds 	unsigned long max_interval;	/* Maximum balance interval ms */
11051da177e4SLinus Torvalds 	unsigned int busy_factor;	/* less balancing by factor if busy */
11061da177e4SLinus Torvalds 	unsigned int imbalance_pct;	/* No balance until over watermark */
11071da177e4SLinus Torvalds 	unsigned int cache_nice_tries;	/* Leave cache hot tasks for # tries */
11087897986bSNick Piggin 	unsigned int busy_idx;
11097897986bSNick Piggin 	unsigned int idle_idx;
11107897986bSNick Piggin 	unsigned int newidle_idx;
11117897986bSNick Piggin 	unsigned int wake_idx;
1112147cbb4bSNick Piggin 	unsigned int forkexec_idx;
1113a52bfd73SPeter Zijlstra 	unsigned int smt_gain;
111425f55d9dSVincent Guittot 
111525f55d9dSVincent Guittot 	int nohz_idle;			/* NOHZ IDLE status */
11161da177e4SLinus Torvalds 	int flags;			/* See SD_* */
111760495e77SPeter Zijlstra 	int level;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	/* Runtime fields. */
11201da177e4SLinus Torvalds 	unsigned long last_balance;	/* init to jiffies. units in jiffies */
11211da177e4SLinus Torvalds 	unsigned int balance_interval;	/* initialise to 1. units in ms. */
11221da177e4SLinus Torvalds 	unsigned int nr_balance_failed; /* initialise to 0 */
11231da177e4SLinus Torvalds 
1124f48627e6SJason Low 	/* idle_balance() stats */
11259bd721c5SJason Low 	u64 max_newidle_lb_cost;
1126f48627e6SJason Low 	unsigned long next_decay_max_lb_cost;
11272398f2c6SPeter Zijlstra 
112810e2f1acSPeter Zijlstra 	u64 avg_scan_cost;		/* select_idle_sibling */
112910e2f1acSPeter Zijlstra 
11301da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
11311da177e4SLinus Torvalds 	/* load_balance() stats */
1132480b9434SKen Chen 	unsigned int lb_count[CPU_MAX_IDLE_TYPES];
1133480b9434SKen Chen 	unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
1134480b9434SKen Chen 	unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
1135480b9434SKen Chen 	unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
1136480b9434SKen Chen 	unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
1137480b9434SKen Chen 	unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
1138480b9434SKen Chen 	unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
1139480b9434SKen Chen 	unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* Active load balancing */
1142480b9434SKen Chen 	unsigned int alb_count;
1143480b9434SKen Chen 	unsigned int alb_failed;
1144480b9434SKen Chen 	unsigned int alb_pushed;
11451da177e4SLinus Torvalds 
114668767a0aSNick Piggin 	/* SD_BALANCE_EXEC stats */
1147480b9434SKen Chen 	unsigned int sbe_count;
1148480b9434SKen Chen 	unsigned int sbe_balanced;
1149480b9434SKen Chen 	unsigned int sbe_pushed;
11501da177e4SLinus Torvalds 
115168767a0aSNick Piggin 	/* SD_BALANCE_FORK stats */
1152480b9434SKen Chen 	unsigned int sbf_count;
1153480b9434SKen Chen 	unsigned int sbf_balanced;
1154480b9434SKen Chen 	unsigned int sbf_pushed;
115568767a0aSNick Piggin 
11561da177e4SLinus Torvalds 	/* try_to_wake_up() stats */
1157480b9434SKen Chen 	unsigned int ttwu_wake_remote;
1158480b9434SKen Chen 	unsigned int ttwu_move_affine;
1159480b9434SKen Chen 	unsigned int ttwu_move_balance;
11601da177e4SLinus Torvalds #endif
1161a5d8c348SIngo Molnar #ifdef CONFIG_SCHED_DEBUG
1162a5d8c348SIngo Molnar 	char *name;
1163a5d8c348SIngo Molnar #endif
1164dce840a0SPeter Zijlstra 	union {
1165dce840a0SPeter Zijlstra 		void *private;		/* used during construction */
1166dce840a0SPeter Zijlstra 		struct rcu_head rcu;	/* used during destruction */
1167dce840a0SPeter Zijlstra 	};
116824fc7edbSPeter Zijlstra 	struct sched_domain_shared *shared;
11696c99e9adSRusty Russell 
1170669c55e9SPeter Zijlstra 	unsigned int span_weight;
11714200efd9SIngo Molnar 	/*
11724200efd9SIngo Molnar 	 * Span of all CPUs in this domain.
11734200efd9SIngo Molnar 	 *
11744200efd9SIngo Molnar 	 * NOTE: this field is variable length. (Allocated dynamically
11754200efd9SIngo Molnar 	 * by attaching extra space to the end of the structure,
11764200efd9SIngo Molnar 	 * depending on how many CPUs the kernel has booted up with)
11774200efd9SIngo Molnar 	 */
11784200efd9SIngo Molnar 	unsigned long span[0];
11791da177e4SLinus Torvalds };
11801da177e4SLinus Torvalds 
1181758b2cdcSRusty Russell static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
1182758b2cdcSRusty Russell {
11836c99e9adSRusty Russell 	return to_cpumask(sd->span);
1184758b2cdcSRusty Russell }
1185758b2cdcSRusty Russell 
1186acc3f5d7SRusty Russell extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
11871d3504fcSHidetoshi Seto 				    struct sched_domain_attr *dattr_new);
1188029190c5SPaul Jackson 
1189acc3f5d7SRusty Russell /* Allocate an array of sched domains, for partition_sched_domains(). */
1190acc3f5d7SRusty Russell cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
1191acc3f5d7SRusty Russell void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
1192acc3f5d7SRusty Russell 
119339be3501SPeter Zijlstra bool cpus_share_cache(int this_cpu, int that_cpu);
119439be3501SPeter Zijlstra 
1195143e1e28SVincent Guittot typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
1196b6220ad6SGuenter Roeck typedef int (*sched_domain_flags_f)(void);
1197143e1e28SVincent Guittot 
1198143e1e28SVincent Guittot #define SDTL_OVERLAP	0x01
1199143e1e28SVincent Guittot 
1200143e1e28SVincent Guittot struct sd_data {
1201143e1e28SVincent Guittot 	struct sched_domain **__percpu sd;
120224fc7edbSPeter Zijlstra 	struct sched_domain_shared **__percpu sds;
1203143e1e28SVincent Guittot 	struct sched_group **__percpu sg;
120463b2ca30SNicolas Pitre 	struct sched_group_capacity **__percpu sgc;
1205143e1e28SVincent Guittot };
1206143e1e28SVincent Guittot 
1207143e1e28SVincent Guittot struct sched_domain_topology_level {
1208143e1e28SVincent Guittot 	sched_domain_mask_f mask;
1209143e1e28SVincent Guittot 	sched_domain_flags_f sd_flags;
1210143e1e28SVincent Guittot 	int		    flags;
1211143e1e28SVincent Guittot 	int		    numa_level;
1212143e1e28SVincent Guittot 	struct sd_data      data;
1213143e1e28SVincent Guittot #ifdef CONFIG_SCHED_DEBUG
1214143e1e28SVincent Guittot 	char                *name;
1215143e1e28SVincent Guittot #endif
1216143e1e28SVincent Guittot };
1217143e1e28SVincent Guittot 
1218143e1e28SVincent Guittot extern void set_sched_topology(struct sched_domain_topology_level *tl);
1219f6be8af1SChuansheng Liu extern void wake_up_if_idle(int cpu);
1220143e1e28SVincent Guittot 
1221143e1e28SVincent Guittot #ifdef CONFIG_SCHED_DEBUG
1222143e1e28SVincent Guittot # define SD_INIT_NAME(type)		.name = #type
1223143e1e28SVincent Guittot #else
1224143e1e28SVincent Guittot # define SD_INIT_NAME(type)
1225143e1e28SVincent Guittot #endif
1226143e1e28SVincent Guittot 
12271b427c15SIngo Molnar #else /* CONFIG_SMP */
12281da177e4SLinus Torvalds 
12291b427c15SIngo Molnar struct sched_domain_attr;
12301b427c15SIngo Molnar 
12311b427c15SIngo Molnar static inline void
1232acc3f5d7SRusty Russell partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
12331b427c15SIngo Molnar 			struct sched_domain_attr *dattr_new)
1234d02c7a8cSCon Kolivas {
1235d02c7a8cSCon Kolivas }
123639be3501SPeter Zijlstra 
123739be3501SPeter Zijlstra static inline bool cpus_share_cache(int this_cpu, int that_cpu)
123839be3501SPeter Zijlstra {
123939be3501SPeter Zijlstra 	return true;
124039be3501SPeter Zijlstra }
124139be3501SPeter Zijlstra 
12421b427c15SIngo Molnar #endif	/* !CONFIG_SMP */
12431da177e4SLinus Torvalds 
124447fe38fcSPeter Zijlstra 
12451da177e4SLinus Torvalds struct io_context;			/* See blkdev.h */
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 
1248383f2835SChen, Kenneth W #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
124936c8b586SIngo Molnar extern void prefetch_stack(struct task_struct *t);
1250383f2835SChen, Kenneth W #else
1251383f2835SChen, Kenneth W static inline void prefetch_stack(struct task_struct *t) { }
1252383f2835SChen, Kenneth W #endif
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds struct audit_context;		/* See audit.c */
12551da177e4SLinus Torvalds struct mempolicy;
1256b92ce558SJens Axboe struct pipe_inode_info;
12574865ecf1SSerge E. Hallyn struct uts_namespace;
12581da177e4SLinus Torvalds 
125920b8a59fSIngo Molnar struct load_weight {
12609dbdb155SPeter Zijlstra 	unsigned long weight;
12619dbdb155SPeter Zijlstra 	u32 inv_weight;
126220b8a59fSIngo Molnar };
126320b8a59fSIngo Molnar 
12649d89c257SYuyang Du /*
12657b595334SYuyang Du  * The load_avg/util_avg accumulates an infinite geometric series
12667b595334SYuyang Du  * (see __update_load_avg() in kernel/sched/fair.c).
12677b595334SYuyang Du  *
12687b595334SYuyang Du  * [load_avg definition]
12697b595334SYuyang Du  *
12707b595334SYuyang Du  *   load_avg = runnable% * scale_load_down(load)
12717b595334SYuyang Du  *
12727b595334SYuyang Du  * where runnable% is the time ratio that a sched_entity is runnable.
12737b595334SYuyang Du  * For cfs_rq, it is the aggregated load_avg of all runnable and
12749d89c257SYuyang Du  * blocked sched_entities.
12757b595334SYuyang Du  *
12767b595334SYuyang Du  * load_avg may also take frequency scaling into account:
12777b595334SYuyang Du  *
12787b595334SYuyang Du  *   load_avg = runnable% * scale_load_down(load) * freq%
12797b595334SYuyang Du  *
12807b595334SYuyang Du  * where freq% is the CPU frequency normalized to the highest frequency.
12817b595334SYuyang Du  *
12827b595334SYuyang Du  * [util_avg definition]
12837b595334SYuyang Du  *
12847b595334SYuyang Du  *   util_avg = running% * SCHED_CAPACITY_SCALE
12857b595334SYuyang Du  *
12867b595334SYuyang Du  * where running% is the time ratio that a sched_entity is running on
12877b595334SYuyang Du  * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
12887b595334SYuyang Du  * and blocked sched_entities.
12897b595334SYuyang Du  *
12907b595334SYuyang Du  * util_avg may also factor frequency scaling and CPU capacity scaling:
12917b595334SYuyang Du  *
12927b595334SYuyang Du  *   util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
12937b595334SYuyang Du  *
12947b595334SYuyang Du  * where freq% is the same as above, and capacity% is the CPU capacity
12957b595334SYuyang Du  * normalized to the greatest capacity (due to uarch differences, etc).
12967b595334SYuyang Du  *
12977b595334SYuyang Du  * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
12987b595334SYuyang Du  * themselves are in the range of [0, 1]. To do fixed point arithmetics,
12997b595334SYuyang Du  * we therefore scale them to as large a range as necessary. This is for
13007b595334SYuyang Du  * example reflected by util_avg's SCHED_CAPACITY_SCALE.
13017b595334SYuyang Du  *
13027b595334SYuyang Du  * [Overflow issue]
13037b595334SYuyang Du  *
13047b595334SYuyang Du  * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
13057b595334SYuyang Du  * with the highest load (=88761), always runnable on a single cfs_rq,
13067b595334SYuyang Du  * and should not overflow as the number already hits PID_MAX_LIMIT.
13077b595334SYuyang Du  *
13087b595334SYuyang Du  * For all other cases (including 32-bit kernels), struct load_weight's
13097b595334SYuyang Du  * weight will overflow first before we do, because:
13107b595334SYuyang Du  *
13117b595334SYuyang Du  *    Max(load_avg) <= Max(load.weight)
13127b595334SYuyang Du  *
13137b595334SYuyang Du  * Then it is the load_weight's responsibility to consider overflow
13147b595334SYuyang Du  * issues.
13159d89c257SYuyang Du  */
13169d85f21cSPaul Turner struct sched_avg {
13179d89c257SYuyang Du 	u64 last_update_time, load_sum;
13189d89c257SYuyang Du 	u32 util_sum, period_contrib;
13199d89c257SYuyang Du 	unsigned long load_avg, util_avg;
13209d85f21cSPaul Turner };
13219d85f21cSPaul Turner 
132294c18227SIngo Molnar #ifdef CONFIG_SCHEDSTATS
132341acab88SLucas De Marchi struct sched_statistics {
132494c18227SIngo Molnar 	u64			wait_start;
132594c18227SIngo Molnar 	u64			wait_max;
13266d082592SArjan van de Ven 	u64			wait_count;
13276d082592SArjan van de Ven 	u64			wait_sum;
13288f0dfc34SArjan van de Ven 	u64			iowait_count;
13298f0dfc34SArjan van de Ven 	u64			iowait_sum;
133094c18227SIngo Molnar 
133194c18227SIngo Molnar 	u64			sleep_start;
133220b8a59fSIngo Molnar 	u64			sleep_max;
133394c18227SIngo Molnar 	s64			sum_sleep_runtime;
133494c18227SIngo Molnar 
133594c18227SIngo Molnar 	u64			block_start;
133620b8a59fSIngo Molnar 	u64			block_max;
133720b8a59fSIngo Molnar 	u64			exec_max;
1338eba1ed4bSIngo Molnar 	u64			slice_max;
1339cc367732SIngo Molnar 
1340cc367732SIngo Molnar 	u64			nr_migrations_cold;
1341cc367732SIngo Molnar 	u64			nr_failed_migrations_affine;
1342cc367732SIngo Molnar 	u64			nr_failed_migrations_running;
1343cc367732SIngo Molnar 	u64			nr_failed_migrations_hot;
1344cc367732SIngo Molnar 	u64			nr_forced_migrations;
1345cc367732SIngo Molnar 
1346cc367732SIngo Molnar 	u64			nr_wakeups;
1347cc367732SIngo Molnar 	u64			nr_wakeups_sync;
1348cc367732SIngo Molnar 	u64			nr_wakeups_migrate;
1349cc367732SIngo Molnar 	u64			nr_wakeups_local;
1350cc367732SIngo Molnar 	u64			nr_wakeups_remote;
1351cc367732SIngo Molnar 	u64			nr_wakeups_affine;
1352cc367732SIngo Molnar 	u64			nr_wakeups_affine_attempts;
1353cc367732SIngo Molnar 	u64			nr_wakeups_passive;
1354cc367732SIngo Molnar 	u64			nr_wakeups_idle;
135541acab88SLucas De Marchi };
135641acab88SLucas De Marchi #endif
135741acab88SLucas De Marchi 
135841acab88SLucas De Marchi struct sched_entity {
135941acab88SLucas De Marchi 	struct load_weight	load;		/* for load-balancing */
136041acab88SLucas De Marchi 	struct rb_node		run_node;
136141acab88SLucas De Marchi 	struct list_head	group_node;
136241acab88SLucas De Marchi 	unsigned int		on_rq;
136341acab88SLucas De Marchi 
136441acab88SLucas De Marchi 	u64			exec_start;
136541acab88SLucas De Marchi 	u64			sum_exec_runtime;
136641acab88SLucas De Marchi 	u64			vruntime;
136741acab88SLucas De Marchi 	u64			prev_sum_exec_runtime;
136841acab88SLucas De Marchi 
136941acab88SLucas De Marchi 	u64			nr_migrations;
137041acab88SLucas De Marchi 
137141acab88SLucas De Marchi #ifdef CONFIG_SCHEDSTATS
137241acab88SLucas De Marchi 	struct sched_statistics statistics;
137394c18227SIngo Molnar #endif
137494c18227SIngo Molnar 
137520b8a59fSIngo Molnar #ifdef CONFIG_FAIR_GROUP_SCHED
1376fed14d45SPeter Zijlstra 	int			depth;
137720b8a59fSIngo Molnar 	struct sched_entity	*parent;
137820b8a59fSIngo Molnar 	/* rq on which this entity is (to be) queued: */
137920b8a59fSIngo Molnar 	struct cfs_rq		*cfs_rq;
138020b8a59fSIngo Molnar 	/* rq "owned" by this entity/group: */
138120b8a59fSIngo Molnar 	struct cfs_rq		*my_q;
138220b8a59fSIngo Molnar #endif
13838bd75c77SClark Williams 
1384141965c7SAlex Shi #ifdef CONFIG_SMP
13855a107804SJiri Olsa 	/*
13865a107804SJiri Olsa 	 * Per entity load average tracking.
13875a107804SJiri Olsa 	 *
13885a107804SJiri Olsa 	 * Put into separate cache line so it does not
13895a107804SJiri Olsa 	 * collide with read-mostly values above.
13905a107804SJiri Olsa 	 */
13915a107804SJiri Olsa 	struct sched_avg	avg ____cacheline_aligned_in_smp;
13929d85f21cSPaul Turner #endif
139320b8a59fSIngo Molnar };
139470b97a7fSIngo Molnar 
1395fa717060SPeter Zijlstra struct sched_rt_entity {
1396fa717060SPeter Zijlstra 	struct list_head run_list;
139778f2c7dbSPeter Zijlstra 	unsigned long timeout;
139857d2aa00SYing Xue 	unsigned long watchdog_stamp;
1399bee367edSRichard Kennedy 	unsigned int time_slice;
1400ff77e468SPeter Zijlstra 	unsigned short on_rq;
1401ff77e468SPeter Zijlstra 	unsigned short on_list;
14026f505b16SPeter Zijlstra 
140358d6c2d7SPeter Zijlstra 	struct sched_rt_entity *back;
1404052f1dc7SPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
14056f505b16SPeter Zijlstra 	struct sched_rt_entity	*parent;
14066f505b16SPeter Zijlstra 	/* rq on which this entity is (to be) queued: */
14076f505b16SPeter Zijlstra 	struct rt_rq		*rt_rq;
14086f505b16SPeter Zijlstra 	/* rq "owned" by this entity/group: */
14096f505b16SPeter Zijlstra 	struct rt_rq		*my_q;
14106f505b16SPeter Zijlstra #endif
1411fa717060SPeter Zijlstra };
1412fa717060SPeter Zijlstra 
1413aab03e05SDario Faggioli struct sched_dl_entity {
1414aab03e05SDario Faggioli 	struct rb_node	rb_node;
1415aab03e05SDario Faggioli 
1416aab03e05SDario Faggioli 	/*
1417aab03e05SDario Faggioli 	 * Original scheduling parameters. Copied here from sched_attr
14184027d080Sxiaofeng.yan 	 * during sched_setattr(), they will remain the same until
14194027d080Sxiaofeng.yan 	 * the next sched_setattr().
1420aab03e05SDario Faggioli 	 */
1421aab03e05SDario Faggioli 	u64 dl_runtime;		/* maximum runtime for each instance	*/
1422aab03e05SDario Faggioli 	u64 dl_deadline;	/* relative deadline of each instance	*/
1423755378a4SHarald Gustafsson 	u64 dl_period;		/* separation of two instances (period) */
1424332ac17eSDario Faggioli 	u64 dl_bw;		/* dl_runtime / dl_deadline		*/
1425aab03e05SDario Faggioli 
1426aab03e05SDario Faggioli 	/*
1427aab03e05SDario Faggioli 	 * Actual scheduling parameters. Initialized with the values above,
1428aab03e05SDario Faggioli 	 * they are continously updated during task execution. Note that
1429aab03e05SDario Faggioli 	 * the remaining runtime could be < 0 in case we are in overrun.
1430aab03e05SDario Faggioli 	 */
1431aab03e05SDario Faggioli 	s64 runtime;		/* remaining runtime for this instance	*/
1432aab03e05SDario Faggioli 	u64 deadline;		/* absolute deadline for this instance	*/
1433aab03e05SDario Faggioli 	unsigned int flags;	/* specifying the scheduler behaviour	*/
1434aab03e05SDario Faggioli 
1435aab03e05SDario Faggioli 	/*
1436aab03e05SDario Faggioli 	 * Some bool flags:
1437aab03e05SDario Faggioli 	 *
1438aab03e05SDario Faggioli 	 * @dl_throttled tells if we exhausted the runtime. If so, the
1439aab03e05SDario Faggioli 	 * task has to wait for a replenishment to be performed at the
1440aab03e05SDario Faggioli 	 * next firing of dl_timer.
1441aab03e05SDario Faggioli 	 *
14422d3d891dSDario Faggioli 	 * @dl_boosted tells if we are boosted due to DI. If so we are
14432d3d891dSDario Faggioli 	 * outside bandwidth enforcement mechanism (but only until we
14445bfd126eSJuri Lelli 	 * exit the critical section);
14455bfd126eSJuri Lelli 	 *
14465bfd126eSJuri Lelli 	 * @dl_yielded tells if task gave up the cpu before consuming
14475bfd126eSJuri Lelli 	 * all its available runtime during the last job.
1448aab03e05SDario Faggioli 	 */
144972f9f3fdSLuca Abeni 	int dl_throttled, dl_boosted, dl_yielded;
1450aab03e05SDario Faggioli 
1451aab03e05SDario Faggioli 	/*
1452aab03e05SDario Faggioli 	 * Bandwidth enforcement timer. Each -deadline task has its
1453aab03e05SDario Faggioli 	 * own bandwidth to be enforced, thus we need one timer per task.
1454aab03e05SDario Faggioli 	 */
1455aab03e05SDario Faggioli 	struct hrtimer dl_timer;
1456aab03e05SDario Faggioli };
14578bd75c77SClark Williams 
14581d082fd0SPaul E. McKenney union rcu_special {
14591d082fd0SPaul E. McKenney 	struct {
14608203d6d0SPaul E. McKenney 		u8 blocked;
14618203d6d0SPaul E. McKenney 		u8 need_qs;
14628203d6d0SPaul E. McKenney 		u8 exp_need_qs;
14638203d6d0SPaul E. McKenney 		u8 pad;	/* Otherwise the compiler can store garbage here. */
14648203d6d0SPaul E. McKenney 	} b; /* Bits. */
14658203d6d0SPaul E. McKenney 	u32 s; /* Set of bits. */
14661d082fd0SPaul E. McKenney };
146786848966SPaul E. McKenney struct rcu_node;
146886848966SPaul E. McKenney 
14698dc85d54SPeter Zijlstra enum perf_event_task_context {
14708dc85d54SPeter Zijlstra 	perf_invalid_context = -1,
14718dc85d54SPeter Zijlstra 	perf_hw_context = 0,
147289a1e187SPeter Zijlstra 	perf_sw_context,
14738dc85d54SPeter Zijlstra 	perf_nr_task_contexts,
14748dc85d54SPeter Zijlstra };
14758dc85d54SPeter Zijlstra 
147672b252aeSMel Gorman /* Track pages that require TLB flushes */
147772b252aeSMel Gorman struct tlbflush_unmap_batch {
147872b252aeSMel Gorman 	/*
147972b252aeSMel Gorman 	 * Each bit set is a CPU that potentially has a TLB entry for one of
148072b252aeSMel Gorman 	 * the PFNs being flushed. See set_tlb_ubc_flush_pending().
148172b252aeSMel Gorman 	 */
148272b252aeSMel Gorman 	struct cpumask cpumask;
148372b252aeSMel Gorman 
148472b252aeSMel Gorman 	/* True if any bit in cpumask is set */
148572b252aeSMel Gorman 	bool flush_required;
1486d950c947SMel Gorman 
1487d950c947SMel Gorman 	/*
1488d950c947SMel Gorman 	 * If true then the PTE was dirty when unmapped. The entry must be
1489d950c947SMel Gorman 	 * flushed before IO is initiated or a stale TLB entry potentially
1490d950c947SMel Gorman 	 * allows an update without redirtying the page.
1491d950c947SMel Gorman 	 */
1492d950c947SMel Gorman 	bool writable;
149372b252aeSMel Gorman };
149472b252aeSMel Gorman 
14951da177e4SLinus Torvalds struct task_struct {
1496c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1497c65eacbeSAndy Lutomirski 	/*
1498c65eacbeSAndy Lutomirski 	 * For reasons of header soup (see current_thread_info()), this
1499c65eacbeSAndy Lutomirski 	 * must be the first element of task_struct.
1500c65eacbeSAndy Lutomirski 	 */
1501c65eacbeSAndy Lutomirski 	struct thread_info thread_info;
1502c65eacbeSAndy Lutomirski #endif
15031da177e4SLinus Torvalds 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
1504f7e4217bSRoman Zippel 	void *stack;
15051da177e4SLinus Torvalds 	atomic_t usage;
150697dc32cdSWilliam Cohen 	unsigned int flags;	/* per process flags, defined below */
150797dc32cdSWilliam Cohen 	unsigned int ptrace;
15081da177e4SLinus Torvalds 
15092dd73a4fSPeter Williams #ifdef CONFIG_SMP
1510fa14ff4aSPeter Zijlstra 	struct llist_node wake_entry;
15113ca7a440SPeter Zijlstra 	int on_cpu;
1512c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1513c65eacbeSAndy Lutomirski 	unsigned int cpu;	/* current CPU */
1514c65eacbeSAndy Lutomirski #endif
151563b0e9edSMike Galbraith 	unsigned int wakee_flips;
151662470419SMichael Wang 	unsigned long wakee_flip_decay_ts;
151763b0e9edSMike Galbraith 	struct task_struct *last_wakee;
1518ac66f547SPeter Zijlstra 
1519ac66f547SPeter Zijlstra 	int wake_cpu;
15204866cde0SNick Piggin #endif
1521fd2f4419SPeter Zijlstra 	int on_rq;
152250e645a8SIngo Molnar 
1523b29739f9SIngo Molnar 	int prio, static_prio, normal_prio;
1524c7aceabaSRichard Kennedy 	unsigned int rt_priority;
15255522d5d5SIngo Molnar 	const struct sched_class *sched_class;
152620b8a59fSIngo Molnar 	struct sched_entity se;
1527fa717060SPeter Zijlstra 	struct sched_rt_entity rt;
15288323f26cSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED
15298323f26cSPeter Zijlstra 	struct task_group *sched_task_group;
15308323f26cSPeter Zijlstra #endif
1531aab03e05SDario Faggioli 	struct sched_dl_entity dl;
15321da177e4SLinus Torvalds 
1533e107be36SAvi Kivity #ifdef CONFIG_PREEMPT_NOTIFIERS
1534e107be36SAvi Kivity 	/* list of struct preempt_notifier: */
1535e107be36SAvi Kivity 	struct hlist_head preempt_notifiers;
1536e107be36SAvi Kivity #endif
1537e107be36SAvi Kivity 
15386c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
15392056a782SJens Axboe 	unsigned int btrace_seq;
15406c5c9341SAlexey Dobriyan #endif
15411da177e4SLinus Torvalds 
154297dc32cdSWilliam Cohen 	unsigned int policy;
154329baa747SPeter Zijlstra 	int nr_cpus_allowed;
15441da177e4SLinus Torvalds 	cpumask_t cpus_allowed;
15451da177e4SLinus Torvalds 
1546a57eb940SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
1547e260be67SPaul E. McKenney 	int rcu_read_lock_nesting;
15481d082fd0SPaul E. McKenney 	union rcu_special rcu_read_unlock_special;
1549f41d911fSPaul E. McKenney 	struct list_head rcu_node_entry;
1550a57eb940SPaul E. McKenney 	struct rcu_node *rcu_blocked_node;
155128f6569aSPranith Kumar #endif /* #ifdef CONFIG_PREEMPT_RCU */
15528315f422SPaul E. McKenney #ifdef CONFIG_TASKS_RCU
15538315f422SPaul E. McKenney 	unsigned long rcu_tasks_nvcsw;
15548315f422SPaul E. McKenney 	bool rcu_tasks_holdout;
15558315f422SPaul E. McKenney 	struct list_head rcu_tasks_holdout_list;
1556176f8f7aSPaul E. McKenney 	int rcu_tasks_idle_cpu;
15578315f422SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_RCU */
1558e260be67SPaul E. McKenney 
1559f6db8347SNaveen N. Rao #ifdef CONFIG_SCHED_INFO
15601da177e4SLinus Torvalds 	struct sched_info sched_info;
15611da177e4SLinus Torvalds #endif
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	struct list_head tasks;
1564806c09a7SDario Faggioli #ifdef CONFIG_SMP
1565917b627dSGregory Haskins 	struct plist_node pushable_tasks;
15661baca4ceSJuri Lelli 	struct rb_node pushable_dl_tasks;
1567806c09a7SDario Faggioli #endif
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	struct mm_struct *mm, *active_mm;
1570615d6e87SDavidlohr Bueso 	/* per-thread vma caching */
1571615d6e87SDavidlohr Bueso 	u32 vmacache_seqnum;
1572615d6e87SDavidlohr Bueso 	struct vm_area_struct *vmacache[VMACACHE_SIZE];
157334e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
157434e55232SKAMEZAWA Hiroyuki 	struct task_rss_stat	rss_stat;
157534e55232SKAMEZAWA Hiroyuki #endif
15761da177e4SLinus Torvalds /* task state */
157797dc32cdSWilliam Cohen 	int exit_state;
15781da177e4SLinus Torvalds 	int exit_code, exit_signal;
15791da177e4SLinus Torvalds 	int pdeath_signal;  /*  The signal sent when the parent dies  */
1580e7cc4173SPalmer Dabbelt 	unsigned long jobctl;	/* JOBCTL_*, siglock protected */
15819b89f6baSAndrei Epure 
15829b89f6baSAndrei Epure 	/* Used for emulating ABI behavior of previous Linux versions */
158397dc32cdSWilliam Cohen 	unsigned int personality;
15849b89f6baSAndrei Epure 
1585be958bdcSPeter Zijlstra 	/* scheduler bits, serialized by scheduler locks */
1586ca94c442SLennart Poettering 	unsigned sched_reset_on_fork:1;
1587a8e4f2eaSPeter Zijlstra 	unsigned sched_contributes_to_load:1;
1588ff303e66SPeter Zijlstra 	unsigned sched_migrated:1;
1589b7e7ade3SPeter Zijlstra 	unsigned sched_remote_wakeup:1;
1590be958bdcSPeter Zijlstra 	unsigned :0; /* force alignment to the next boundary */
1591be958bdcSPeter Zijlstra 
1592be958bdcSPeter Zijlstra 	/* unserialized, strictly 'current' */
1593be958bdcSPeter Zijlstra 	unsigned in_execve:1; /* bit to tell LSMs we're in execve */
1594be958bdcSPeter Zijlstra 	unsigned in_iowait:1;
15957e781418SAndy Lutomirski #if !defined(TIF_RESTORE_SIGMASK)
15967e781418SAndy Lutomirski 	unsigned restore_sigmask:1;
15977e781418SAndy Lutomirski #endif
1598626ebc41STejun Heo #ifdef CONFIG_MEMCG
1599626ebc41STejun Heo 	unsigned memcg_may_oom:1;
1600127424c8SJohannes Weiner #ifndef CONFIG_SLOB
16016f185c29SVladimir Davydov 	unsigned memcg_kmem_skip_account:1;
16026f185c29SVladimir Davydov #endif
1603127424c8SJohannes Weiner #endif
1604ff303e66SPeter Zijlstra #ifdef CONFIG_COMPAT_BRK
1605ff303e66SPeter Zijlstra 	unsigned brk_randomized:1;
1606ff303e66SPeter Zijlstra #endif
16076f185c29SVladimir Davydov 
16081d4457f9SKees Cook 	unsigned long atomic_flags; /* Flags needing atomic access. */
16091d4457f9SKees Cook 
1610f56141e3SAndy Lutomirski 	struct restart_block restart_block;
1611f56141e3SAndy Lutomirski 
16121da177e4SLinus Torvalds 	pid_t pid;
16131da177e4SLinus Torvalds 	pid_t tgid;
16140a425405SArjan van de Ven 
16151314562aSHiroshi Shimamoto #ifdef CONFIG_CC_STACKPROTECTOR
16160a425405SArjan van de Ven 	/* Canary value for the -fstack-protector gcc feature */
16170a425405SArjan van de Ven 	unsigned long stack_canary;
16181314562aSHiroshi Shimamoto #endif
16191da177e4SLinus Torvalds 	/*
16201da177e4SLinus Torvalds 	 * pointers to (original) parent process, youngest child, younger sibling,
16211da177e4SLinus Torvalds 	 * older sibling, respectively.  (p->father can be replaced with
1622f470021aSRoland McGrath 	 * p->real_parent->pid)
16231da177e4SLinus Torvalds 	 */
1624abd63bc3SKees Cook 	struct task_struct __rcu *real_parent; /* real parent process */
1625abd63bc3SKees Cook 	struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
16261da177e4SLinus Torvalds 	/*
1627f470021aSRoland McGrath 	 * children/sibling forms the list of my natural children
16281da177e4SLinus Torvalds 	 */
16291da177e4SLinus Torvalds 	struct list_head children;	/* list of my children */
16301da177e4SLinus Torvalds 	struct list_head sibling;	/* linkage in my parent's children list */
16311da177e4SLinus Torvalds 	struct task_struct *group_leader;	/* threadgroup leader */
16321da177e4SLinus Torvalds 
1633f470021aSRoland McGrath 	/*
1634f470021aSRoland McGrath 	 * ptraced is the list of tasks this task is using ptrace on.
1635f470021aSRoland McGrath 	 * This includes both natural children and PTRACE_ATTACH targets.
1636f470021aSRoland McGrath 	 * p->ptrace_entry is p's link on the p->parent->ptraced list.
1637f470021aSRoland McGrath 	 */
1638f470021aSRoland McGrath 	struct list_head ptraced;
1639f470021aSRoland McGrath 	struct list_head ptrace_entry;
1640f470021aSRoland McGrath 
16411da177e4SLinus Torvalds 	/* PID/PID hash table linkage. */
164292476d7fSEric W. Biederman 	struct pid_link pids[PIDTYPE_MAX];
164347e65328SOleg Nesterov 	struct list_head thread_group;
16440c740d0aSOleg Nesterov 	struct list_head thread_node;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	struct completion *vfork_done;		/* for vfork() */
16471da177e4SLinus Torvalds 	int __user *set_child_tid;		/* CLONE_CHILD_SETTID */
16481da177e4SLinus Torvalds 	int __user *clear_child_tid;		/* CLONE_CHILD_CLEARTID */
16491da177e4SLinus Torvalds 
1650c66f08beSMichael Neuling 	cputime_t utime, stime, utimescaled, stimescaled;
16519ac52315SLaurent Vivier 	cputime_t gtime;
16529d7fb042SPeter Zijlstra 	struct prev_cputime prev_cputime;
16536a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1654b7ce2277SFrederic Weisbecker 	seqcount_t vtime_seqcount;
16556a61671bSFrederic Weisbecker 	unsigned long long vtime_snap;
16566a61671bSFrederic Weisbecker 	enum {
16577098c1eaSFrederic Weisbecker 		/* Task is sleeping or running in a CPU with VTIME inactive */
16587098c1eaSFrederic Weisbecker 		VTIME_INACTIVE = 0,
16597098c1eaSFrederic Weisbecker 		/* Task runs in userspace in a CPU with VTIME active */
16606a61671bSFrederic Weisbecker 		VTIME_USER,
16617098c1eaSFrederic Weisbecker 		/* Task runs in kernelspace in a CPU with VTIME active */
16626a61671bSFrederic Weisbecker 		VTIME_SYS,
16636a61671bSFrederic Weisbecker 	} vtime_snap_whence;
16646a61671bSFrederic Weisbecker #endif
1665d027d45dSFrederic Weisbecker 
1666d027d45dSFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
1667f009a7a7SFrederic Weisbecker 	atomic_t tick_dep_mask;
1668d027d45dSFrederic Weisbecker #endif
16691da177e4SLinus Torvalds 	unsigned long nvcsw, nivcsw; /* context switch counts */
1670ccbf62d8SThomas Gleixner 	u64 start_time;		/* monotonic time in nsec */
167157e0be04SThomas Gleixner 	u64 real_start_time;	/* boot based time in nsec */
16721da177e4SLinus Torvalds /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
16731da177e4SLinus Torvalds 	unsigned long min_flt, maj_flt;
16741da177e4SLinus Torvalds 
1675f06febc9SFrank Mayhar 	struct task_cputime cputime_expires;
16761da177e4SLinus Torvalds 	struct list_head cpu_timers[3];
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds /* process credentials */
16791b0ba1c9SArnd Bergmann 	const struct cred __rcu *real_cred; /* objective and real subjective task
16803b11a1deSDavid Howells 					 * credentials (COW) */
16811b0ba1c9SArnd Bergmann 	const struct cred __rcu *cred;	/* effective (overridable) subjective task
16823b11a1deSDavid Howells 					 * credentials (COW) */
168336772092SPaolo 'Blaisorblade' Giarrusso 	char comm[TASK_COMM_LEN]; /* executable name excluding path
168436772092SPaolo 'Blaisorblade' Giarrusso 				     - access with [gs]et_task_comm (which lock
168536772092SPaolo 'Blaisorblade' Giarrusso 				       it with task_lock())
1686221af7f8SLinus Torvalds 				     - initialized normally by setup_new_exec */
16871da177e4SLinus Torvalds /* file system info */
1688756daf26SNeilBrown 	struct nameidata *nameidata;
16893d5b6fccSAlexey Dobriyan #ifdef CONFIG_SYSVIPC
16901da177e4SLinus Torvalds /* ipc stuff */
16911da177e4SLinus Torvalds 	struct sysv_sem sysvsem;
1692ab602f79SJack Miller 	struct sysv_shm sysvshm;
16933d5b6fccSAlexey Dobriyan #endif
1694e162b39aSMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
169582a1fcb9SIngo Molnar /* hung task detection */
169682a1fcb9SIngo Molnar 	unsigned long last_switch_count;
169782a1fcb9SIngo Molnar #endif
16981da177e4SLinus Torvalds /* filesystem information */
16991da177e4SLinus Torvalds 	struct fs_struct *fs;
17001da177e4SLinus Torvalds /* open file information */
17011da177e4SLinus Torvalds 	struct files_struct *files;
17021651e14eSSerge E. Hallyn /* namespaces */
1703ab516013SSerge E. Hallyn 	struct nsproxy *nsproxy;
17041da177e4SLinus Torvalds /* signal handlers */
17051da177e4SLinus Torvalds 	struct signal_struct *signal;
17061da177e4SLinus Torvalds 	struct sighand_struct *sighand;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	sigset_t blocked, real_blocked;
1709f3de272bSRoland McGrath 	sigset_t saved_sigmask;	/* restored if set_restore_sigmask() was used */
17101da177e4SLinus Torvalds 	struct sigpending pending;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	unsigned long sas_ss_sp;
17131da177e4SLinus Torvalds 	size_t sas_ss_size;
17142a742138SStas Sergeev 	unsigned sas_ss_flags;
17152e01fabeSOleg Nesterov 
171667d12145SAl Viro 	struct callback_head *task_works;
1717e73f8959SOleg Nesterov 
17181da177e4SLinus Torvalds 	struct audit_context *audit_context;
1719bfef93a5SAl Viro #ifdef CONFIG_AUDITSYSCALL
1720e1760bd5SEric W. Biederman 	kuid_t loginuid;
17214746ec5bSEric Paris 	unsigned int sessionid;
1722bfef93a5SAl Viro #endif
1723932ecebbSWill Drewry 	struct seccomp seccomp;
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds /* Thread group tracking */
17261da177e4SLinus Torvalds    	u32 parent_exec_id;
17271da177e4SLinus Torvalds    	u32 self_exec_id;
172858568d2aSMiao Xie /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
172958568d2aSMiao Xie  * mempolicy */
17301da177e4SLinus Torvalds 	spinlock_t alloc_lock;
17311da177e4SLinus Torvalds 
1732b29739f9SIngo Molnar 	/* Protection of the PI data structures: */
17331d615482SThomas Gleixner 	raw_spinlock_t pi_lock;
1734b29739f9SIngo Molnar 
173576751049SPeter Zijlstra 	struct wake_q_node wake_q;
173676751049SPeter Zijlstra 
173723f78d4aSIngo Molnar #ifdef CONFIG_RT_MUTEXES
173823f78d4aSIngo Molnar 	/* PI waiters blocked on a rt_mutex held by this task */
1739fb00aca4SPeter Zijlstra 	struct rb_root pi_waiters;
1740fb00aca4SPeter Zijlstra 	struct rb_node *pi_waiters_leftmost;
174123f78d4aSIngo Molnar 	/* Deadlock detection and priority inheritance handling */
174223f78d4aSIngo Molnar 	struct rt_mutex_waiter *pi_blocked_on;
174323f78d4aSIngo Molnar #endif
174423f78d4aSIngo Molnar 
1745408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1746408894eeSIngo Molnar 	/* mutex deadlock detection */
1747408894eeSIngo Molnar 	struct mutex_waiter *blocked_on;
1748408894eeSIngo Molnar #endif
1749de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1750de30a2b3SIngo Molnar 	unsigned int irq_events;
1751de30a2b3SIngo Molnar 	unsigned long hardirq_enable_ip;
1752de30a2b3SIngo Molnar 	unsigned long hardirq_disable_ip;
1753fa1452e8SHiroshi Shimamoto 	unsigned int hardirq_enable_event;
1754de30a2b3SIngo Molnar 	unsigned int hardirq_disable_event;
1755fa1452e8SHiroshi Shimamoto 	int hardirqs_enabled;
1756de30a2b3SIngo Molnar 	int hardirq_context;
1757fa1452e8SHiroshi Shimamoto 	unsigned long softirq_disable_ip;
1758fa1452e8SHiroshi Shimamoto 	unsigned long softirq_enable_ip;
1759fa1452e8SHiroshi Shimamoto 	unsigned int softirq_disable_event;
1760fa1452e8SHiroshi Shimamoto 	unsigned int softirq_enable_event;
1761fa1452e8SHiroshi Shimamoto 	int softirqs_enabled;
1762de30a2b3SIngo Molnar 	int softirq_context;
1763de30a2b3SIngo Molnar #endif
1764fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1765bdb9441eSPeter Zijlstra # define MAX_LOCK_DEPTH 48UL
1766fbb9ce95SIngo Molnar 	u64 curr_chain_key;
1767fbb9ce95SIngo Molnar 	int lockdep_depth;
1768fbb9ce95SIngo Molnar 	unsigned int lockdep_recursion;
1769c7aceabaSRichard Kennedy 	struct held_lock held_locks[MAX_LOCK_DEPTH];
1770cf40bd16SNick Piggin 	gfp_t lockdep_reclaim_gfp;
1771fbb9ce95SIngo Molnar #endif
1772c6d30853SAndrey Ryabinin #ifdef CONFIG_UBSAN
1773c6d30853SAndrey Ryabinin 	unsigned int in_ubsan;
1774c6d30853SAndrey Ryabinin #endif
1775408894eeSIngo Molnar 
17761da177e4SLinus Torvalds /* journalling filesystem info */
17771da177e4SLinus Torvalds 	void *journal_info;
17781da177e4SLinus Torvalds 
1779d89d8796SNeil Brown /* stacked block device info */
1780bddd87c7SAkinobu Mita 	struct bio_list *bio_list;
1781d89d8796SNeil Brown 
178273c10101SJens Axboe #ifdef CONFIG_BLOCK
178373c10101SJens Axboe /* stack plugging */
178473c10101SJens Axboe 	struct blk_plug *plug;
178573c10101SJens Axboe #endif
178673c10101SJens Axboe 
17871da177e4SLinus Torvalds /* VM state */
17881da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	struct backing_dev_info *backing_dev_info;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	struct io_context *io_context;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	unsigned long ptrace_message;
17951da177e4SLinus Torvalds 	siginfo_t *last_siginfo; /* For ptrace use.  */
17967c3ab738SAndrew Morton 	struct task_io_accounting ioac;
17978f0ab514SJay Lan #if defined(CONFIG_TASK_XACCT)
17981da177e4SLinus Torvalds 	u64 acct_rss_mem1;	/* accumulated rss usage */
17991da177e4SLinus Torvalds 	u64 acct_vm_mem1;	/* accumulated virtual memory usage */
180049b5cf34SJonathan Lim 	cputime_t acct_timexpd;	/* stime + utime since last update */
18011da177e4SLinus Torvalds #endif
18021da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS
180358568d2aSMiao Xie 	nodemask_t mems_allowed;	/* Protected by alloc_lock */
1804cc9a6c87SMel Gorman 	seqcount_t mems_allowed_seq;	/* Seqence no to catch updates */
1805825a46afSPaul Jackson 	int cpuset_mem_spread_rotor;
18066adef3ebSJack Steiner 	int cpuset_slab_spread_rotor;
18071da177e4SLinus Torvalds #endif
1808ddbcc7e8SPaul Menage #ifdef CONFIG_CGROUPS
1809817929ecSPaul Menage 	/* Control Group info protected by css_set_lock */
18102c392b8cSArnd Bergmann 	struct css_set __rcu *cgroups;
1811817929ecSPaul Menage 	/* cg_list protected by css_set_lock and tsk->alloc_lock */
1812817929ecSPaul Menage 	struct list_head cg_list;
1813ddbcc7e8SPaul Menage #endif
181442b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
18150771dfefSIngo Molnar 	struct robust_list_head __user *robust_list;
181634f192c6SIngo Molnar #ifdef CONFIG_COMPAT
181734f192c6SIngo Molnar 	struct compat_robust_list_head __user *compat_robust_list;
181834f192c6SIngo Molnar #endif
1819c87e2837SIngo Molnar 	struct list_head pi_state_list;
1820c87e2837SIngo Molnar 	struct futex_pi_state *pi_state_cache;
182142b2dd0aSAlexey Dobriyan #endif
1822cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
18238dc85d54SPeter Zijlstra 	struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1824cdd6c482SIngo Molnar 	struct mutex perf_event_mutex;
1825cdd6c482SIngo Molnar 	struct list_head perf_event_list;
1826a63eaf34SPaul Mackerras #endif
18278f47b187SThomas Gleixner #ifdef CONFIG_DEBUG_PREEMPT
18288f47b187SThomas Gleixner 	unsigned long preempt_disable_ip;
18298f47b187SThomas Gleixner #endif
1830c7aceabaSRichard Kennedy #ifdef CONFIG_NUMA
183158568d2aSMiao Xie 	struct mempolicy *mempolicy;	/* Protected by alloc_lock */
1832c7aceabaSRichard Kennedy 	short il_next;
1833207205a2SEric Dumazet 	short pref_node_fork;
1834c7aceabaSRichard Kennedy #endif
1835cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
1836cbee9f88SPeter Zijlstra 	int numa_scan_seq;
1837cbee9f88SPeter Zijlstra 	unsigned int numa_scan_period;
1838598f0ec0SMel Gorman 	unsigned int numa_scan_period_max;
1839de1c9ce6SRik van Riel 	int numa_preferred_nid;
18406b9a7460SMel Gorman 	unsigned long numa_migrate_retry;
1841cbee9f88SPeter Zijlstra 	u64 node_stamp;			/* migration stamp  */
18427e2703e6SRik van Riel 	u64 last_task_numa_placement;
18437e2703e6SRik van Riel 	u64 last_sum_exec_runtime;
1844cbee9f88SPeter Zijlstra 	struct callback_head numa_work;
1845f809ca9aSMel Gorman 
18468c8a743cSPeter Zijlstra 	struct list_head numa_entry;
18478c8a743cSPeter Zijlstra 	struct numa_group *numa_group;
18488c8a743cSPeter Zijlstra 
1849745d6147SMel Gorman 	/*
185044dba3d5SIulia Manda 	 * numa_faults is an array split into four regions:
185144dba3d5SIulia Manda 	 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
185244dba3d5SIulia Manda 	 * in this precise order.
185344dba3d5SIulia Manda 	 *
185444dba3d5SIulia Manda 	 * faults_memory: Exponential decaying average of faults on a per-node
185544dba3d5SIulia Manda 	 * basis. Scheduling placement decisions are made based on these
185644dba3d5SIulia Manda 	 * counts. The values remain static for the duration of a PTE scan.
185744dba3d5SIulia Manda 	 * faults_cpu: Track the nodes the process was running on when a NUMA
185844dba3d5SIulia Manda 	 * hinting fault was incurred.
185944dba3d5SIulia Manda 	 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
186044dba3d5SIulia Manda 	 * during the current scan window. When the scan completes, the counts
186144dba3d5SIulia Manda 	 * in faults_memory and faults_cpu decay and these values are copied.
1862745d6147SMel Gorman 	 */
186344dba3d5SIulia Manda 	unsigned long *numa_faults;
186483e1d2cdSMel Gorman 	unsigned long total_numa_faults;
1865745d6147SMel Gorman 
1866745d6147SMel Gorman 	/*
186704bb2f94SRik van Riel 	 * numa_faults_locality tracks if faults recorded during the last
1868074c2381SMel Gorman 	 * scan window were remote/local or failed to migrate. The task scan
1869074c2381SMel Gorman 	 * period is adapted based on the locality of the faults with different
1870074c2381SMel Gorman 	 * weights depending on whether they were shared or private faults
187104bb2f94SRik van Riel 	 */
1872074c2381SMel Gorman 	unsigned long numa_faults_locality[3];
187304bb2f94SRik van Riel 
1874b32e86b4SIngo Molnar 	unsigned long numa_pages_migrated;
1875cbee9f88SPeter Zijlstra #endif /* CONFIG_NUMA_BALANCING */
1876cbee9f88SPeter Zijlstra 
187772b252aeSMel Gorman #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
187872b252aeSMel Gorman 	struct tlbflush_unmap_batch tlb_ubc;
187972b252aeSMel Gorman #endif
188072b252aeSMel Gorman 
1881e56d0903SIngo Molnar 	struct rcu_head rcu;
1882b92ce558SJens Axboe 
1883b92ce558SJens Axboe 	/*
1884b92ce558SJens Axboe 	 * cache last used pipe for splice
1885b92ce558SJens Axboe 	 */
1886b92ce558SJens Axboe 	struct pipe_inode_info *splice_pipe;
18875640f768SEric Dumazet 
18885640f768SEric Dumazet 	struct page_frag task_frag;
18895640f768SEric Dumazet 
1890ca74e92bSShailabh Nagar #ifdef	CONFIG_TASK_DELAY_ACCT
1891ca74e92bSShailabh Nagar 	struct task_delay_info *delays;
1892ca74e92bSShailabh Nagar #endif
1893f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1894f4f154fdSAkinobu Mita 	int make_it_fail;
1895f4f154fdSAkinobu Mita #endif
18969d823e8fSWu Fengguang 	/*
18979d823e8fSWu Fengguang 	 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
18989d823e8fSWu Fengguang 	 * balance_dirty_pages() for some dirty throttling pause
18999d823e8fSWu Fengguang 	 */
19009d823e8fSWu Fengguang 	int nr_dirtied;
19019d823e8fSWu Fengguang 	int nr_dirtied_pause;
190283712358SWu Fengguang 	unsigned long dirty_paused_when; /* start of a write-and-pause period */
19039d823e8fSWu Fengguang 
19049745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
19059745512cSArjan van de Ven 	int latency_record_count;
19069745512cSArjan van de Ven 	struct latency_record latency_record[LT_SAVECOUNT];
19079745512cSArjan van de Ven #endif
19086976675dSArjan van de Ven 	/*
19096976675dSArjan van de Ven 	 * time slack values; these are used to round up poll() and
19106976675dSArjan van de Ven 	 * select() etc timeout values. These are in nanoseconds.
19116976675dSArjan van de Ven 	 */
1912da8b44d5SJohn Stultz 	u64 timer_slack_ns;
1913da8b44d5SJohn Stultz 	u64 default_timer_slack_ns;
1914f8d570a4SDavid Miller 
19150b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
19160b24beccSAndrey Ryabinin 	unsigned int kasan_depth;
19170b24beccSAndrey Ryabinin #endif
1918fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
19193ad2f3fbSDaniel Mack 	/* Index of current stored address in ret_stack */
1920f201ae23SFrederic Weisbecker 	int curr_ret_stack;
1921f201ae23SFrederic Weisbecker 	/* Stack of return addresses for return function tracing */
1922f201ae23SFrederic Weisbecker 	struct ftrace_ret_stack	*ret_stack;
19238aef2d28SSteven Rostedt 	/* time stamp for last schedule */
19248aef2d28SSteven Rostedt 	unsigned long long ftrace_timestamp;
1925f201ae23SFrederic Weisbecker 	/*
1926f201ae23SFrederic Weisbecker 	 * Number of functions that haven't been traced
1927f201ae23SFrederic Weisbecker 	 * because of depth overrun.
1928f201ae23SFrederic Weisbecker 	 */
1929f201ae23SFrederic Weisbecker 	atomic_t trace_overrun;
1930380c4b14SFrederic Weisbecker 	/* Pause for the tracing */
1931380c4b14SFrederic Weisbecker 	atomic_t tracing_graph_pause;
1932f201ae23SFrederic Weisbecker #endif
1933ea4e2bc4SSteven Rostedt #ifdef CONFIG_TRACING
1934ea4e2bc4SSteven Rostedt 	/* state flags for use by tracers */
1935ea4e2bc4SSteven Rostedt 	unsigned long trace;
1936b1cff0adSSteven Rostedt 	/* bitmask and counter of trace recursion */
1937261842b7SSteven Rostedt 	unsigned long trace_recursion;
1938261842b7SSteven Rostedt #endif /* CONFIG_TRACING */
19395c9a8750SDmitry Vyukov #ifdef CONFIG_KCOV
19405c9a8750SDmitry Vyukov 	/* Coverage collection mode enabled for this task (0 if disabled). */
19415c9a8750SDmitry Vyukov 	enum kcov_mode kcov_mode;
19425c9a8750SDmitry Vyukov 	/* Size of the kcov_area. */
19435c9a8750SDmitry Vyukov 	unsigned	kcov_size;
19445c9a8750SDmitry Vyukov 	/* Buffer for coverage collection. */
19455c9a8750SDmitry Vyukov 	void		*kcov_area;
19465c9a8750SDmitry Vyukov 	/* kcov desciptor wired with this task or NULL. */
19475c9a8750SDmitry Vyukov 	struct kcov	*kcov;
19485c9a8750SDmitry Vyukov #endif
19496f185c29SVladimir Davydov #ifdef CONFIG_MEMCG
1950626ebc41STejun Heo 	struct mem_cgroup *memcg_in_oom;
1951626ebc41STejun Heo 	gfp_t memcg_oom_gfp_mask;
1952626ebc41STejun Heo 	int memcg_oom_order;
1953b23afb93STejun Heo 
1954b23afb93STejun Heo 	/* number of pages to reclaim on returning to userland */
1955b23afb93STejun Heo 	unsigned int memcg_nr_pages_over_high;
1956569b846dSKAMEZAWA Hiroyuki #endif
19570326f5a9SSrikar Dronamraju #ifdef CONFIG_UPROBES
19580326f5a9SSrikar Dronamraju 	struct uprobe_task *utask;
19590326f5a9SSrikar Dronamraju #endif
1960cafe5635SKent Overstreet #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1961cafe5635SKent Overstreet 	unsigned int	sequential_io;
1962cafe5635SKent Overstreet 	unsigned int	sequential_io_avg;
1963cafe5635SKent Overstreet #endif
19648eb23b9fSPeter Zijlstra #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
19658eb23b9fSPeter Zijlstra 	unsigned long	task_state_change;
19668eb23b9fSPeter Zijlstra #endif
19678bcbde54SDavid Hildenbrand 	int pagefault_disabled;
196803049269SMichal Hocko #ifdef CONFIG_MMU
196929c696e1SVladimir Davydov 	struct task_struct *oom_reaper_list;
197003049269SMichal Hocko #endif
1971ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
1972ba14a194SAndy Lutomirski 	struct vm_struct *stack_vm_area;
1973ba14a194SAndy Lutomirski #endif
197468f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
197568f24b08SAndy Lutomirski 	/* A live task holds one reference. */
197668f24b08SAndy Lutomirski 	atomic_t stack_refcount;
197768f24b08SAndy Lutomirski #endif
19780c8c0f03SDave Hansen /* CPU-specific state of this task */
19790c8c0f03SDave Hansen 	struct thread_struct thread;
19800c8c0f03SDave Hansen /*
19810c8c0f03SDave Hansen  * WARNING: on x86, 'thread_struct' contains a variable-sized
19820c8c0f03SDave Hansen  * structure.  It *MUST* be at the end of 'task_struct'.
19830c8c0f03SDave Hansen  *
19840c8c0f03SDave Hansen  * Do not put anything below here!
19850c8c0f03SDave Hansen  */
19861da177e4SLinus Torvalds };
19871da177e4SLinus Torvalds 
19885aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
19895aaeb5c0SIngo Molnar extern int arch_task_struct_size __read_mostly;
19905aaeb5c0SIngo Molnar #else
19915aaeb5c0SIngo Molnar # define arch_task_struct_size (sizeof(struct task_struct))
19925aaeb5c0SIngo Molnar #endif
19930c8c0f03SDave Hansen 
1994ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
1995ba14a194SAndy Lutomirski static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
1996ba14a194SAndy Lutomirski {
1997ba14a194SAndy Lutomirski 	return t->stack_vm_area;
1998ba14a194SAndy Lutomirski }
1999ba14a194SAndy Lutomirski #else
2000ba14a194SAndy Lutomirski static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
2001ba14a194SAndy Lutomirski {
2002ba14a194SAndy Lutomirski 	return NULL;
2003ba14a194SAndy Lutomirski }
2004ba14a194SAndy Lutomirski #endif
2005ba14a194SAndy Lutomirski 
200676e6eee0SRusty Russell /* Future-safe accessor for struct task_struct's cpus_allowed. */
2007a4636818SRusty Russell #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
200876e6eee0SRusty Russell 
200950605ffbSThomas Gleixner static inline int tsk_nr_cpus_allowed(struct task_struct *p)
201050605ffbSThomas Gleixner {
201150605ffbSThomas Gleixner 	return p->nr_cpus_allowed;
201250605ffbSThomas Gleixner }
201350605ffbSThomas Gleixner 
20146688cc05SPeter Zijlstra #define TNF_MIGRATED	0x01
20156688cc05SPeter Zijlstra #define TNF_NO_GROUP	0x02
2016dabe1d99SRik van Riel #define TNF_SHARED	0x04
201704bb2f94SRik van Riel #define TNF_FAULT_LOCAL	0x08
2018074c2381SMel Gorman #define TNF_MIGRATE_FAIL 0x10
20196688cc05SPeter Zijlstra 
2020b18dc5f2SMichal Hocko static inline bool in_vfork(struct task_struct *tsk)
2021b18dc5f2SMichal Hocko {
2022b18dc5f2SMichal Hocko 	bool ret;
2023b18dc5f2SMichal Hocko 
2024b18dc5f2SMichal Hocko 	/*
2025b18dc5f2SMichal Hocko 	 * need RCU to access ->real_parent if CLONE_VM was used along with
2026b18dc5f2SMichal Hocko 	 * CLONE_PARENT.
2027b18dc5f2SMichal Hocko 	 *
2028b18dc5f2SMichal Hocko 	 * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
2029b18dc5f2SMichal Hocko 	 * imply CLONE_VM
2030b18dc5f2SMichal Hocko 	 *
2031b18dc5f2SMichal Hocko 	 * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
2032b18dc5f2SMichal Hocko 	 * ->real_parent is not necessarily the task doing vfork(), so in
2033b18dc5f2SMichal Hocko 	 * theory we can't rely on task_lock() if we want to dereference it.
2034b18dc5f2SMichal Hocko 	 *
2035b18dc5f2SMichal Hocko 	 * And in this case we can't trust the real_parent->mm == tsk->mm
2036b18dc5f2SMichal Hocko 	 * check, it can be false negative. But we do not care, if init or
2037b18dc5f2SMichal Hocko 	 * another oom-unkillable task does this it should blame itself.
2038b18dc5f2SMichal Hocko 	 */
2039b18dc5f2SMichal Hocko 	rcu_read_lock();
2040b18dc5f2SMichal Hocko 	ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
2041b18dc5f2SMichal Hocko 	rcu_read_unlock();
2042b18dc5f2SMichal Hocko 
2043b18dc5f2SMichal Hocko 	return ret;
2044b18dc5f2SMichal Hocko }
2045b18dc5f2SMichal Hocko 
2046cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
20476688cc05SPeter Zijlstra extern void task_numa_fault(int last_node, int node, int pages, int flags);
2048e29cf08bSMel Gorman extern pid_t task_numa_group_id(struct task_struct *p);
20491a687c2eSMel Gorman extern void set_numabalancing_state(bool enabled);
205082727018SRik van Riel extern void task_numa_free(struct task_struct *p);
205110f39042SRik van Riel extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
205210f39042SRik van Riel 					int src_nid, int dst_cpu);
2053cbee9f88SPeter Zijlstra #else
2054ac8e895bSMel Gorman static inline void task_numa_fault(int last_node, int node, int pages,
20556688cc05SPeter Zijlstra 				   int flags)
2056cbee9f88SPeter Zijlstra {
2057cbee9f88SPeter Zijlstra }
2058e29cf08bSMel Gorman static inline pid_t task_numa_group_id(struct task_struct *p)
2059e29cf08bSMel Gorman {
2060e29cf08bSMel Gorman 	return 0;
2061e29cf08bSMel Gorman }
20621a687c2eSMel Gorman static inline void set_numabalancing_state(bool enabled)
20631a687c2eSMel Gorman {
20641a687c2eSMel Gorman }
206582727018SRik van Riel static inline void task_numa_free(struct task_struct *p)
206682727018SRik van Riel {
206782727018SRik van Riel }
206810f39042SRik van Riel static inline bool should_numa_migrate_memory(struct task_struct *p,
206910f39042SRik van Riel 				struct page *page, int src_nid, int dst_cpu)
207010f39042SRik van Riel {
207110f39042SRik van Riel 	return true;
207210f39042SRik van Riel }
2073cbee9f88SPeter Zijlstra #endif
2074cbee9f88SPeter Zijlstra 
2075e868171aSAlexey Dobriyan static inline struct pid *task_pid(struct task_struct *task)
207622c935f4SEric W. Biederman {
207722c935f4SEric W. Biederman 	return task->pids[PIDTYPE_PID].pid;
207822c935f4SEric W. Biederman }
207922c935f4SEric W. Biederman 
2080e868171aSAlexey Dobriyan static inline struct pid *task_tgid(struct task_struct *task)
208122c935f4SEric W. Biederman {
208222c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_PID].pid;
208322c935f4SEric W. Biederman }
208422c935f4SEric W. Biederman 
20856dda81f4SOleg Nesterov /*
20866dda81f4SOleg Nesterov  * Without tasklist or rcu lock it is not safe to dereference
20876dda81f4SOleg Nesterov  * the result of task_pgrp/task_session even if task == current,
20886dda81f4SOleg Nesterov  * we can race with another thread doing sys_setsid/sys_setpgid.
20896dda81f4SOleg Nesterov  */
2090e868171aSAlexey Dobriyan static inline struct pid *task_pgrp(struct task_struct *task)
209122c935f4SEric W. Biederman {
209222c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_PGID].pid;
209322c935f4SEric W. Biederman }
209422c935f4SEric W. Biederman 
2095e868171aSAlexey Dobriyan static inline struct pid *task_session(struct task_struct *task)
209622c935f4SEric W. Biederman {
209722c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_SID].pid;
209822c935f4SEric W. Biederman }
209922c935f4SEric W. Biederman 
21007af57294SPavel Emelyanov struct pid_namespace;
21017af57294SPavel Emelyanov 
21027af57294SPavel Emelyanov /*
21037af57294SPavel Emelyanov  * the helpers to get the task's different pids as they are seen
21047af57294SPavel Emelyanov  * from various namespaces
21057af57294SPavel Emelyanov  *
21067af57294SPavel Emelyanov  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
210744c4e1b2SEric W. Biederman  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
210844c4e1b2SEric W. Biederman  *                     current.
21097af57294SPavel Emelyanov  * task_xid_nr_ns()  : id seen from the ns specified;
21107af57294SPavel Emelyanov  *
21117af57294SPavel Emelyanov  * set_task_vxid()   : assigns a virtual id to a task;
21127af57294SPavel Emelyanov  *
21137af57294SPavel Emelyanov  * see also pid_nr() etc in include/linux/pid.h
21147af57294SPavel Emelyanov  */
211552ee2dfdSOleg Nesterov pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
211652ee2dfdSOleg Nesterov 			struct pid_namespace *ns);
21177af57294SPavel Emelyanov 
2118e868171aSAlexey Dobriyan static inline pid_t task_pid_nr(struct task_struct *tsk)
21197af57294SPavel Emelyanov {
21207af57294SPavel Emelyanov 	return tsk->pid;
21217af57294SPavel Emelyanov }
21227af57294SPavel Emelyanov 
212352ee2dfdSOleg Nesterov static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
212452ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
212552ee2dfdSOleg Nesterov {
212652ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
212752ee2dfdSOleg Nesterov }
21287af57294SPavel Emelyanov 
21297af57294SPavel Emelyanov static inline pid_t task_pid_vnr(struct task_struct *tsk)
21307af57294SPavel Emelyanov {
213152ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
21327af57294SPavel Emelyanov }
21337af57294SPavel Emelyanov 
21347af57294SPavel Emelyanov 
2135e868171aSAlexey Dobriyan static inline pid_t task_tgid_nr(struct task_struct *tsk)
21367af57294SPavel Emelyanov {
21377af57294SPavel Emelyanov 	return tsk->tgid;
21387af57294SPavel Emelyanov }
21397af57294SPavel Emelyanov 
21402f2a3a46SPavel Emelyanov pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
21417af57294SPavel Emelyanov 
21427af57294SPavel Emelyanov static inline pid_t task_tgid_vnr(struct task_struct *tsk)
21437af57294SPavel Emelyanov {
21447af57294SPavel Emelyanov 	return pid_vnr(task_tgid(tsk));
21457af57294SPavel Emelyanov }
21467af57294SPavel Emelyanov 
21477af57294SPavel Emelyanov 
214880e0b6e8SRichard Guy Briggs static inline int pid_alive(const struct task_struct *p);
2149ad36d282SRichard Guy Briggs static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
2150ad36d282SRichard Guy Briggs {
2151ad36d282SRichard Guy Briggs 	pid_t pid = 0;
2152ad36d282SRichard Guy Briggs 
2153ad36d282SRichard Guy Briggs 	rcu_read_lock();
2154ad36d282SRichard Guy Briggs 	if (pid_alive(tsk))
2155ad36d282SRichard Guy Briggs 		pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
2156ad36d282SRichard Guy Briggs 	rcu_read_unlock();
2157ad36d282SRichard Guy Briggs 
2158ad36d282SRichard Guy Briggs 	return pid;
2159ad36d282SRichard Guy Briggs }
2160ad36d282SRichard Guy Briggs 
2161ad36d282SRichard Guy Briggs static inline pid_t task_ppid_nr(const struct task_struct *tsk)
2162ad36d282SRichard Guy Briggs {
2163ad36d282SRichard Guy Briggs 	return task_ppid_nr_ns(tsk, &init_pid_ns);
2164ad36d282SRichard Guy Briggs }
2165ad36d282SRichard Guy Briggs 
216652ee2dfdSOleg Nesterov static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
216752ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
21687af57294SPavel Emelyanov {
216952ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
21707af57294SPavel Emelyanov }
21717af57294SPavel Emelyanov 
21727af57294SPavel Emelyanov static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
21737af57294SPavel Emelyanov {
217452ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
21757af57294SPavel Emelyanov }
21767af57294SPavel Emelyanov 
21777af57294SPavel Emelyanov 
217852ee2dfdSOleg Nesterov static inline pid_t task_session_nr_ns(struct task_struct *tsk,
217952ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
21807af57294SPavel Emelyanov {
218152ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
21827af57294SPavel Emelyanov }
21837af57294SPavel Emelyanov 
21847af57294SPavel Emelyanov static inline pid_t task_session_vnr(struct task_struct *tsk)
21857af57294SPavel Emelyanov {
218652ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
21877af57294SPavel Emelyanov }
21887af57294SPavel Emelyanov 
21891b0f7ffdSOleg Nesterov /* obsolete, do not use */
21901b0f7ffdSOleg Nesterov static inline pid_t task_pgrp_nr(struct task_struct *tsk)
21911b0f7ffdSOleg Nesterov {
21921b0f7ffdSOleg Nesterov 	return task_pgrp_nr_ns(tsk, &init_pid_ns);
21931b0f7ffdSOleg Nesterov }
21947af57294SPavel Emelyanov 
21951da177e4SLinus Torvalds /**
21961da177e4SLinus Torvalds  * pid_alive - check that a task structure is not stale
21971da177e4SLinus Torvalds  * @p: Task structure to be checked.
21981da177e4SLinus Torvalds  *
21991da177e4SLinus Torvalds  * Test if a process is not yet dead (at most zombie state)
22001da177e4SLinus Torvalds  * If pid_alive fails, then pointers within the task structure
22011da177e4SLinus Torvalds  * can be stale and must not be dereferenced.
2202e69f6186SYacine Belkadi  *
2203e69f6186SYacine Belkadi  * Return: 1 if the process is alive. 0 otherwise.
22041da177e4SLinus Torvalds  */
2205ad36d282SRichard Guy Briggs static inline int pid_alive(const struct task_struct *p)
22061da177e4SLinus Torvalds {
220792476d7fSEric W. Biederman 	return p->pids[PIDTYPE_PID].pid != NULL;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
2210f400e198SSukadev Bhattiprolu /**
2211570f5241SSergey Senozhatsky  * is_global_init - check if a task structure is init. Since init
2212570f5241SSergey Senozhatsky  * is free to have sub-threads we need to check tgid.
22133260259fSHenne  * @tsk: Task structure to be checked.
22143260259fSHenne  *
22153260259fSHenne  * Check if a task structure is the first user space task the kernel created.
2216e69f6186SYacine Belkadi  *
2217e69f6186SYacine Belkadi  * Return: 1 if the task structure is init. 0 otherwise.
2218f400e198SSukadev Bhattiprolu  */
2219e868171aSAlexey Dobriyan static inline int is_global_init(struct task_struct *tsk)
2220b461cc03SPavel Emelyanov {
2221570f5241SSergey Senozhatsky 	return task_tgid_nr(tsk) == 1;
2222b461cc03SPavel Emelyanov }
2223b460cbc5SSerge E. Hallyn 
22249ec52099SCedric Le Goater extern struct pid *cad_pid;
22259ec52099SCedric Le Goater 
22261da177e4SLinus Torvalds extern void free_task(struct task_struct *tsk);
22271da177e4SLinus Torvalds #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
2228e56d0903SIngo Molnar 
2229158d9ebdSAndrew Morton extern void __put_task_struct(struct task_struct *t);
2230e56d0903SIngo Molnar 
2231e56d0903SIngo Molnar static inline void put_task_struct(struct task_struct *t)
2232e56d0903SIngo Molnar {
2233e56d0903SIngo Molnar 	if (atomic_dec_and_test(&t->usage))
22348c7904a0SEric W. Biederman 		__put_task_struct(t);
2235e56d0903SIngo Molnar }
22361da177e4SLinus Torvalds 
2237150593bfSOleg Nesterov struct task_struct *task_rcu_dereference(struct task_struct **ptask);
2238150593bfSOleg Nesterov struct task_struct *try_get_task_struct(struct task_struct **ptask);
2239150593bfSOleg Nesterov 
22406a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
22416a61671bSFrederic Weisbecker extern void task_cputime(struct task_struct *t,
22426a61671bSFrederic Weisbecker 			 cputime_t *utime, cputime_t *stime);
22436a61671bSFrederic Weisbecker extern void task_cputime_scaled(struct task_struct *t,
22446a61671bSFrederic Weisbecker 				cputime_t *utimescaled, cputime_t *stimescaled);
22456a61671bSFrederic Weisbecker extern cputime_t task_gtime(struct task_struct *t);
22466a61671bSFrederic Weisbecker #else
22476fac4829SFrederic Weisbecker static inline void task_cputime(struct task_struct *t,
22486fac4829SFrederic Weisbecker 				cputime_t *utime, cputime_t *stime)
22496fac4829SFrederic Weisbecker {
22506fac4829SFrederic Weisbecker 	if (utime)
22516fac4829SFrederic Weisbecker 		*utime = t->utime;
22526fac4829SFrederic Weisbecker 	if (stime)
22536fac4829SFrederic Weisbecker 		*stime = t->stime;
22546fac4829SFrederic Weisbecker }
22556fac4829SFrederic Weisbecker 
22566fac4829SFrederic Weisbecker static inline void task_cputime_scaled(struct task_struct *t,
22576fac4829SFrederic Weisbecker 				       cputime_t *utimescaled,
22586fac4829SFrederic Weisbecker 				       cputime_t *stimescaled)
22596fac4829SFrederic Weisbecker {
22606fac4829SFrederic Weisbecker 	if (utimescaled)
22616fac4829SFrederic Weisbecker 		*utimescaled = t->utimescaled;
22626fac4829SFrederic Weisbecker 	if (stimescaled)
22636fac4829SFrederic Weisbecker 		*stimescaled = t->stimescaled;
22646fac4829SFrederic Weisbecker }
22656a61671bSFrederic Weisbecker 
22666a61671bSFrederic Weisbecker static inline cputime_t task_gtime(struct task_struct *t)
22676a61671bSFrederic Weisbecker {
22686a61671bSFrederic Weisbecker 	return t->gtime;
22696a61671bSFrederic Weisbecker }
22706a61671bSFrederic Weisbecker #endif
2271e80d0a1aSFrederic Weisbecker extern void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
2272e80d0a1aSFrederic Weisbecker extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
227349048622SBalbir Singh 
22741da177e4SLinus Torvalds /*
22751da177e4SLinus Torvalds  * Per process flags
22761da177e4SLinus Torvalds  */
22771da177e4SLinus Torvalds #define PF_EXITING	0x00000004	/* getting shut down */
2278778e9a9cSAlexey Kuznetsov #define PF_EXITPIDONE	0x00000008	/* pi exit done on shut down */
227994886b84SLaurent Vivier #define PF_VCPU		0x00000010	/* I'm a virtual CPU */
228021aa9af0STejun Heo #define PF_WQ_WORKER	0x00000020	/* I'm a workqueue worker */
22811da177e4SLinus Torvalds #define PF_FORKNOEXEC	0x00000040	/* forked but didn't exec */
22824db96cf0SAndi Kleen #define PF_MCE_PROCESS  0x00000080      /* process policy on mce errors */
22831da177e4SLinus Torvalds #define PF_SUPERPRIV	0x00000100	/* used super-user privileges */
22841da177e4SLinus Torvalds #define PF_DUMPCORE	0x00000200	/* dumped core */
22851da177e4SLinus Torvalds #define PF_SIGNALED	0x00000400	/* killed by a signal */
22861da177e4SLinus Torvalds #define PF_MEMALLOC	0x00000800	/* Allocating memory */
228772fa5997SVasiliy Kulikov #define PF_NPROC_EXCEEDED 0x00001000	/* set_user noticed that RLIMIT_NPROC was exceeded */
22881da177e4SLinus Torvalds #define PF_USED_MATH	0x00002000	/* if unset the fpu must be initialized before use */
2289774a1221STejun Heo #define PF_USED_ASYNC	0x00004000	/* used async_schedule*(), used by module init */
22901da177e4SLinus Torvalds #define PF_NOFREEZE	0x00008000	/* this thread should not be frozen */
22911da177e4SLinus Torvalds #define PF_FROZEN	0x00010000	/* frozen for system suspend */
22921da177e4SLinus Torvalds #define PF_FSTRANS	0x00020000	/* inside a filesystem transaction */
22931da177e4SLinus Torvalds #define PF_KSWAPD	0x00040000	/* I am kswapd */
229421caf2fcSMing Lei #define PF_MEMALLOC_NOIO 0x00080000	/* Allocating memory without IO involved */
22951da177e4SLinus Torvalds #define PF_LESS_THROTTLE 0x00100000	/* Throttle me less: I clean memory */
2296246bb0b1SOleg Nesterov #define PF_KTHREAD	0x00200000	/* I am a kernel thread */
2297b31dc66aSJens Axboe #define PF_RANDOMIZE	0x00400000	/* randomize virtual address space */
2298b31dc66aSJens Axboe #define PF_SWAPWRITE	0x00800000	/* Allowed to write to swap */
229914a40ffcSTejun Heo #define PF_NO_SETAFFINITY 0x04000000	/* Userland is not allowed to meddle with cpus_allowed */
23004db96cf0SAndi Kleen #define PF_MCE_EARLY    0x08000000      /* Early kill for mce process policy */
230161a87122SThomas Gleixner #define PF_MUTEX_TESTER	0x20000000	/* Thread belongs to the rt mutex tester */
230258a69cb4STejun Heo #define PF_FREEZER_SKIP	0x40000000	/* Freezer should not count it as freezable */
23032b44c4dbSColin Cross #define PF_SUSPEND_TASK 0x80000000      /* this thread called freeze_processes and should not be frozen */
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds /*
23061da177e4SLinus Torvalds  * Only the _current_ task can read/write to tsk->flags, but other
23071da177e4SLinus Torvalds  * tasks can access tsk->flags in readonly mode for example
23081da177e4SLinus Torvalds  * with tsk_used_math (like during threaded core dumping).
23091da177e4SLinus Torvalds  * There is however an exception to this rule during ptrace
23101da177e4SLinus Torvalds  * or during fork: the ptracer task is allowed to write to the
23111da177e4SLinus Torvalds  * child->flags of its traced child (same goes for fork, the parent
23121da177e4SLinus Torvalds  * can write to the child->flags), because we're guaranteed the
23131da177e4SLinus Torvalds  * child is not running and in turn not changing child->flags
23141da177e4SLinus Torvalds  * at the same time the parent does it.
23151da177e4SLinus Torvalds  */
23161da177e4SLinus Torvalds #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
23171da177e4SLinus Torvalds #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
23181da177e4SLinus Torvalds #define clear_used_math() clear_stopped_child_used_math(current)
23191da177e4SLinus Torvalds #define set_used_math() set_stopped_child_used_math(current)
23201da177e4SLinus Torvalds #define conditional_stopped_child_used_math(condition, child) \
23211da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
23221da177e4SLinus Torvalds #define conditional_used_math(condition) \
23231da177e4SLinus Torvalds 	conditional_stopped_child_used_math(condition, current)
23241da177e4SLinus Torvalds #define copy_to_stopped_child_used_math(child) \
23251da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
23261da177e4SLinus Torvalds /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
23271da177e4SLinus Torvalds #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
23281da177e4SLinus Torvalds #define used_math() tsk_used_math(current)
23291da177e4SLinus Torvalds 
2330934f3072SJunxiao Bi /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
2331934f3072SJunxiao Bi  * __GFP_FS is also cleared as it implies __GFP_IO.
2332934f3072SJunxiao Bi  */
233321caf2fcSMing Lei static inline gfp_t memalloc_noio_flags(gfp_t flags)
233421caf2fcSMing Lei {
233521caf2fcSMing Lei 	if (unlikely(current->flags & PF_MEMALLOC_NOIO))
2336934f3072SJunxiao Bi 		flags &= ~(__GFP_IO | __GFP_FS);
233721caf2fcSMing Lei 	return flags;
233821caf2fcSMing Lei }
233921caf2fcSMing Lei 
234021caf2fcSMing Lei static inline unsigned int memalloc_noio_save(void)
234121caf2fcSMing Lei {
234221caf2fcSMing Lei 	unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
234321caf2fcSMing Lei 	current->flags |= PF_MEMALLOC_NOIO;
234421caf2fcSMing Lei 	return flags;
234521caf2fcSMing Lei }
234621caf2fcSMing Lei 
234721caf2fcSMing Lei static inline void memalloc_noio_restore(unsigned int flags)
234821caf2fcSMing Lei {
234921caf2fcSMing Lei 	current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
235021caf2fcSMing Lei }
235121caf2fcSMing Lei 
23521d4457f9SKees Cook /* Per-process atomic flags. */
2353a2b86f77SZefan Li #define PFA_NO_NEW_PRIVS 0	/* May not gain new privileges. */
23542ad654bcSZefan Li #define PFA_SPREAD_PAGE  1      /* Spread page cache over cpuset */
23552ad654bcSZefan Li #define PFA_SPREAD_SLAB  2      /* Spread some slab caches over cpuset */
235677ed2c57STetsuo Handa #define PFA_LMK_WAITING  3      /* Lowmemorykiller is waiting */
23571d4457f9SKees Cook 
23581d4457f9SKees Cook 
2359e0e5070bSZefan Li #define TASK_PFA_TEST(name, func)					\
2360e0e5070bSZefan Li 	static inline bool task_##func(struct task_struct *p)		\
2361e0e5070bSZefan Li 	{ return test_bit(PFA_##name, &p->atomic_flags); }
2362e0e5070bSZefan Li #define TASK_PFA_SET(name, func)					\
2363e0e5070bSZefan Li 	static inline void task_set_##func(struct task_struct *p)	\
2364e0e5070bSZefan Li 	{ set_bit(PFA_##name, &p->atomic_flags); }
2365e0e5070bSZefan Li #define TASK_PFA_CLEAR(name, func)					\
2366e0e5070bSZefan Li 	static inline void task_clear_##func(struct task_struct *p)	\
2367e0e5070bSZefan Li 	{ clear_bit(PFA_##name, &p->atomic_flags); }
23681d4457f9SKees Cook 
2369e0e5070bSZefan Li TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
2370e0e5070bSZefan Li TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
23711d4457f9SKees Cook 
23722ad654bcSZefan Li TASK_PFA_TEST(SPREAD_PAGE, spread_page)
23732ad654bcSZefan Li TASK_PFA_SET(SPREAD_PAGE, spread_page)
23742ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
23752ad654bcSZefan Li 
23762ad654bcSZefan Li TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
23772ad654bcSZefan Li TASK_PFA_SET(SPREAD_SLAB, spread_slab)
23782ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
2379544b2c91STejun Heo 
238077ed2c57STetsuo Handa TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
238177ed2c57STetsuo Handa TASK_PFA_SET(LMK_WAITING, lmk_waiting)
238277ed2c57STetsuo Handa 
238339efa3efSTejun Heo /*
2384e5c1902eSTejun Heo  * task->jobctl flags
2385d79fdd6dSTejun Heo  */
2386ee77f075SOleg Nesterov #define JOBCTL_STOP_SIGMASK	0xffff	/* signr of the last group stop */
2387e5c1902eSTejun Heo 
2388f41d911fSPaul E. McKenney #define JOBCTL_STOP_DEQUEUED_BIT 16	/* stop signal dequeued */
2389a8f072c1STejun Heo #define JOBCTL_STOP_PENDING_BIT	17	/* task should stop for group stop */
2390a8f072c1STejun Heo #define JOBCTL_STOP_CONSUME_BIT	18	/* consume group stop count */
2391a8f072c1STejun Heo #define JOBCTL_TRAP_STOP_BIT	19	/* trap for STOP */
239273ddff2bSTejun Heo #define JOBCTL_TRAP_NOTIFY_BIT	20	/* trap for NOTIFY */
2393fb1d910cSTejun Heo #define JOBCTL_TRAPPING_BIT	21	/* switching to TRACED */
2394a8f072c1STejun Heo #define JOBCTL_LISTENING_BIT	22	/* ptracer is listening for events */
2395a8f072c1STejun Heo 
2396b76808e6SPalmer Dabbelt #define JOBCTL_STOP_DEQUEUED	(1UL << JOBCTL_STOP_DEQUEUED_BIT)
2397b76808e6SPalmer Dabbelt #define JOBCTL_STOP_PENDING	(1UL << JOBCTL_STOP_PENDING_BIT)
2398b76808e6SPalmer Dabbelt #define JOBCTL_STOP_CONSUME	(1UL << JOBCTL_STOP_CONSUME_BIT)
2399b76808e6SPalmer Dabbelt #define JOBCTL_TRAP_STOP	(1UL << JOBCTL_TRAP_STOP_BIT)
2400b76808e6SPalmer Dabbelt #define JOBCTL_TRAP_NOTIFY	(1UL << JOBCTL_TRAP_NOTIFY_BIT)
2401b76808e6SPalmer Dabbelt #define JOBCTL_TRAPPING		(1UL << JOBCTL_TRAPPING_BIT)
2402b76808e6SPalmer Dabbelt #define JOBCTL_LISTENING	(1UL << JOBCTL_LISTENING_BIT)
2403a8f072c1STejun Heo 
2404fb1d910cSTejun Heo #define JOBCTL_TRAP_MASK	(JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
240573ddff2bSTejun Heo #define JOBCTL_PENDING_MASK	(JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
24063759a0d9STejun Heo 
24077dd3db54STejun Heo extern bool task_set_jobctl_pending(struct task_struct *task,
2408b76808e6SPalmer Dabbelt 				    unsigned long mask);
240973ddff2bSTejun Heo extern void task_clear_jobctl_trapping(struct task_struct *task);
24103759a0d9STejun Heo extern void task_clear_jobctl_pending(struct task_struct *task,
2411b76808e6SPalmer Dabbelt 				      unsigned long mask);
241239efa3efSTejun Heo 
2413f41d911fSPaul E. McKenney static inline void rcu_copy_process(struct task_struct *p)
2414f41d911fSPaul E. McKenney {
24151da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT_RCU
2416f41d911fSPaul E. McKenney 	p->rcu_read_lock_nesting = 0;
24171d082fd0SPaul E. McKenney 	p->rcu_read_unlock_special.s = 0;
2418dd5d19baSPaul E. McKenney 	p->rcu_blocked_node = NULL;
2419f41d911fSPaul E. McKenney 	INIT_LIST_HEAD(&p->rcu_node_entry);
24208315f422SPaul E. McKenney #endif /* #ifdef CONFIG_PREEMPT_RCU */
24218315f422SPaul E. McKenney #ifdef CONFIG_TASKS_RCU
24228315f422SPaul E. McKenney 	p->rcu_tasks_holdout = false;
24238315f422SPaul E. McKenney 	INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
2424176f8f7aSPaul E. McKenney 	p->rcu_tasks_idle_cpu = -1;
24258315f422SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_RCU */
2426f41d911fSPaul E. McKenney }
2427f41d911fSPaul E. McKenney 
2428907aed48SMel Gorman static inline void tsk_restore_flags(struct task_struct *task,
2429907aed48SMel Gorman 				unsigned long orig_flags, unsigned long flags)
2430907aed48SMel Gorman {
2431907aed48SMel Gorman 	task->flags &= ~flags;
2432907aed48SMel Gorman 	task->flags |= orig_flags & flags;
2433907aed48SMel Gorman }
2434907aed48SMel Gorman 
2435f82f8042SJuri Lelli extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
2436f82f8042SJuri Lelli 				     const struct cpumask *trial);
24377f51412aSJuri Lelli extern int task_can_attach(struct task_struct *p,
24387f51412aSJuri Lelli 			   const struct cpumask *cs_cpus_allowed);
24391da177e4SLinus Torvalds #ifdef CONFIG_SMP
24401e1b6c51SKOSAKI Motohiro extern void do_set_cpus_allowed(struct task_struct *p,
24411e1b6c51SKOSAKI Motohiro 			       const struct cpumask *new_mask);
24421e1b6c51SKOSAKI Motohiro 
2443cd8ba7cdSMike Travis extern int set_cpus_allowed_ptr(struct task_struct *p,
244496f874e2SRusty Russell 				const struct cpumask *new_mask);
24451da177e4SLinus Torvalds #else
24461e1b6c51SKOSAKI Motohiro static inline void do_set_cpus_allowed(struct task_struct *p,
24471e1b6c51SKOSAKI Motohiro 				      const struct cpumask *new_mask)
24481e1b6c51SKOSAKI Motohiro {
24491e1b6c51SKOSAKI Motohiro }
2450cd8ba7cdSMike Travis static inline int set_cpus_allowed_ptr(struct task_struct *p,
245196f874e2SRusty Russell 				       const struct cpumask *new_mask)
24521da177e4SLinus Torvalds {
245396f874e2SRusty Russell 	if (!cpumask_test_cpu(0, new_mask))
24541da177e4SLinus Torvalds 		return -EINVAL;
24551da177e4SLinus Torvalds 	return 0;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds #endif
2458e0ad9556SRusty Russell 
24593451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON
24605167e8d5SPeter Zijlstra void calc_load_enter_idle(void);
24615167e8d5SPeter Zijlstra void calc_load_exit_idle(void);
24625167e8d5SPeter Zijlstra #else
24635167e8d5SPeter Zijlstra static inline void calc_load_enter_idle(void) { }
24645167e8d5SPeter Zijlstra static inline void calc_load_exit_idle(void) { }
24653451d024SFrederic Weisbecker #endif /* CONFIG_NO_HZ_COMMON */
24665167e8d5SPeter Zijlstra 
2467b342501cSIngo Molnar /*
2468c676329aSPeter Zijlstra  * Do not use outside of architecture code which knows its limitations.
2469c676329aSPeter Zijlstra  *
2470c676329aSPeter Zijlstra  * sched_clock() has no promise of monotonicity or bounded drift between
2471c676329aSPeter Zijlstra  * CPUs, use (which you should not) requires disabling IRQs.
2472c676329aSPeter Zijlstra  *
2473c676329aSPeter Zijlstra  * Please use one of the three interfaces below.
2474b342501cSIngo Molnar  */
24751bbfa6f2SMike Frysinger extern unsigned long long notrace sched_clock(void);
2476c676329aSPeter Zijlstra /*
2477489a71b0SHiroshi Shimamoto  * See the comment in kernel/sched/clock.c
2478c676329aSPeter Zijlstra  */
2479545a2bf7SCyril Bur extern u64 running_clock(void);
2480c676329aSPeter Zijlstra extern u64 sched_clock_cpu(int cpu);
2481c676329aSPeter Zijlstra 
2482e436d800SIngo Molnar 
2483c1955a3dSPeter Zijlstra extern void sched_clock_init(void);
2484c1955a3dSPeter Zijlstra 
24853e51f33fSPeter Zijlstra #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
24863e51f33fSPeter Zijlstra static inline void sched_clock_tick(void)
24873e51f33fSPeter Zijlstra {
24883e51f33fSPeter Zijlstra }
24893e51f33fSPeter Zijlstra 
24903e51f33fSPeter Zijlstra static inline void sched_clock_idle_sleep_event(void)
24913e51f33fSPeter Zijlstra {
24923e51f33fSPeter Zijlstra }
24933e51f33fSPeter Zijlstra 
24943e51f33fSPeter Zijlstra static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
24953e51f33fSPeter Zijlstra {
24963e51f33fSPeter Zijlstra }
24972c923e94SDaniel Lezcano 
24982c923e94SDaniel Lezcano static inline u64 cpu_clock(int cpu)
24992c923e94SDaniel Lezcano {
25002c923e94SDaniel Lezcano 	return sched_clock();
25012c923e94SDaniel Lezcano }
25022c923e94SDaniel Lezcano 
25032c923e94SDaniel Lezcano static inline u64 local_clock(void)
25042c923e94SDaniel Lezcano {
25052c923e94SDaniel Lezcano 	return sched_clock();
25062c923e94SDaniel Lezcano }
25073e51f33fSPeter Zijlstra #else
2508c676329aSPeter Zijlstra /*
2509c676329aSPeter Zijlstra  * Architectures can set this to 1 if they have specified
2510c676329aSPeter Zijlstra  * CONFIG_HAVE_UNSTABLE_SCHED_CLOCK in their arch Kconfig,
2511c676329aSPeter Zijlstra  * but then during bootup it turns out that sched_clock()
2512c676329aSPeter Zijlstra  * is reliable after all:
2513c676329aSPeter Zijlstra  */
251435af99e6SPeter Zijlstra extern int sched_clock_stable(void);
251535af99e6SPeter Zijlstra extern void set_sched_clock_stable(void);
251635af99e6SPeter Zijlstra extern void clear_sched_clock_stable(void);
2517c676329aSPeter Zijlstra 
25183e51f33fSPeter Zijlstra extern void sched_clock_tick(void);
25193e51f33fSPeter Zijlstra extern void sched_clock_idle_sleep_event(void);
25203e51f33fSPeter Zijlstra extern void sched_clock_idle_wakeup_event(u64 delta_ns);
25212c923e94SDaniel Lezcano 
25222c923e94SDaniel Lezcano /*
25232c923e94SDaniel Lezcano  * As outlined in clock.c, provides a fast, high resolution, nanosecond
25242c923e94SDaniel Lezcano  * time source that is monotonic per cpu argument and has bounded drift
25252c923e94SDaniel Lezcano  * between cpus.
25262c923e94SDaniel Lezcano  *
25272c923e94SDaniel Lezcano  * ######################### BIG FAT WARNING ##########################
25282c923e94SDaniel Lezcano  * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can #
25292c923e94SDaniel Lezcano  * # go backwards !!                                                  #
25302c923e94SDaniel Lezcano  * ####################################################################
25312c923e94SDaniel Lezcano  */
25322c923e94SDaniel Lezcano static inline u64 cpu_clock(int cpu)
25332c923e94SDaniel Lezcano {
25342c923e94SDaniel Lezcano 	return sched_clock_cpu(cpu);
25352c923e94SDaniel Lezcano }
25362c923e94SDaniel Lezcano 
25372c923e94SDaniel Lezcano static inline u64 local_clock(void)
25382c923e94SDaniel Lezcano {
25392c923e94SDaniel Lezcano 	return sched_clock_cpu(raw_smp_processor_id());
25402c923e94SDaniel Lezcano }
25413e51f33fSPeter Zijlstra #endif
25423e51f33fSPeter Zijlstra 
2543b52bfee4SVenkatesh Pallipadi #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2544b52bfee4SVenkatesh Pallipadi /*
2545b52bfee4SVenkatesh Pallipadi  * An i/f to runtime opt-in for irq time accounting based off of sched_clock.
2546b52bfee4SVenkatesh Pallipadi  * The reason for this explicit opt-in is not to have perf penalty with
2547b52bfee4SVenkatesh Pallipadi  * slow sched_clocks.
2548b52bfee4SVenkatesh Pallipadi  */
2549b52bfee4SVenkatesh Pallipadi extern void enable_sched_clock_irqtime(void);
2550b52bfee4SVenkatesh Pallipadi extern void disable_sched_clock_irqtime(void);
2551b52bfee4SVenkatesh Pallipadi #else
2552b52bfee4SVenkatesh Pallipadi static inline void enable_sched_clock_irqtime(void) {}
2553b52bfee4SVenkatesh Pallipadi static inline void disable_sched_clock_irqtime(void) {}
2554b52bfee4SVenkatesh Pallipadi #endif
2555b52bfee4SVenkatesh Pallipadi 
255636c8b586SIngo Molnar extern unsigned long long
255741b86e9cSIngo Molnar task_sched_runtime(struct task_struct *task);
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds /* sched_exec is called by processes performing an exec */
25601da177e4SLinus Torvalds #ifdef CONFIG_SMP
25611da177e4SLinus Torvalds extern void sched_exec(void);
25621da177e4SLinus Torvalds #else
25631da177e4SLinus Torvalds #define sched_exec()   {}
25641da177e4SLinus Torvalds #endif
25651da177e4SLinus Torvalds 
25662aa44d05SIngo Molnar extern void sched_clock_idle_sleep_event(void);
25672aa44d05SIngo Molnar extern void sched_clock_idle_wakeup_event(u64 delta_ns);
2568bb29ab26SIngo Molnar 
25691da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
25701da177e4SLinus Torvalds extern void idle_task_exit(void);
25711da177e4SLinus Torvalds #else
25721da177e4SLinus Torvalds static inline void idle_task_exit(void) {}
25731da177e4SLinus Torvalds #endif
25741da177e4SLinus Torvalds 
25753451d024SFrederic Weisbecker #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
25761c20091eSFrederic Weisbecker extern void wake_up_nohz_cpu(int cpu);
257706d8308cSThomas Gleixner #else
25781c20091eSFrederic Weisbecker static inline void wake_up_nohz_cpu(int cpu) { }
257906d8308cSThomas Gleixner #endif
258006d8308cSThomas Gleixner 
2581ce831b38SFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
2582265f22a9SFrederic Weisbecker extern u64 scheduler_tick_max_deferment(void);
2583bf0f6f24SIngo Molnar #endif
2584bf0f6f24SIngo Molnar 
25855091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
25865091faa4SMike Galbraith extern void sched_autogroup_create_attach(struct task_struct *p);
25875091faa4SMike Galbraith extern void sched_autogroup_detach(struct task_struct *p);
25885091faa4SMike Galbraith extern void sched_autogroup_fork(struct signal_struct *sig);
25895091faa4SMike Galbraith extern void sched_autogroup_exit(struct signal_struct *sig);
25905091faa4SMike Galbraith #ifdef CONFIG_PROC_FS
25915091faa4SMike Galbraith extern void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m);
25922e5b5b3aSHiroshi Shimamoto extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
25935091faa4SMike Galbraith #endif
25945091faa4SMike Galbraith #else
25955091faa4SMike Galbraith static inline void sched_autogroup_create_attach(struct task_struct *p) { }
25965091faa4SMike Galbraith static inline void sched_autogroup_detach(struct task_struct *p) { }
25975091faa4SMike Galbraith static inline void sched_autogroup_fork(struct signal_struct *sig) { }
25985091faa4SMike Galbraith static inline void sched_autogroup_exit(struct signal_struct *sig) { }
25995091faa4SMike Galbraith #endif
26005091faa4SMike Galbraith 
2601fa93384fSDan Carpenter extern int yield_to(struct task_struct *p, bool preempt);
260236c8b586SIngo Molnar extern void set_user_nice(struct task_struct *p, long nice);
260336c8b586SIngo Molnar extern int task_prio(const struct task_struct *p);
2604d0ea0268SDongsheng Yang /**
2605d0ea0268SDongsheng Yang  * task_nice - return the nice value of a given task.
2606d0ea0268SDongsheng Yang  * @p: the task in question.
2607d0ea0268SDongsheng Yang  *
2608d0ea0268SDongsheng Yang  * Return: The nice value [ -20 ... 0 ... 19 ].
2609d0ea0268SDongsheng Yang  */
2610d0ea0268SDongsheng Yang static inline int task_nice(const struct task_struct *p)
2611d0ea0268SDongsheng Yang {
2612d0ea0268SDongsheng Yang 	return PRIO_TO_NICE((p)->static_prio);
2613d0ea0268SDongsheng Yang }
261436c8b586SIngo Molnar extern int can_nice(const struct task_struct *p, const int nice);
261536c8b586SIngo Molnar extern int task_curr(const struct task_struct *p);
26161da177e4SLinus Torvalds extern int idle_cpu(int cpu);
2617fe7de49fSKOSAKI Motohiro extern int sched_setscheduler(struct task_struct *, int,
2618fe7de49fSKOSAKI Motohiro 			      const struct sched_param *);
2619961ccdddSRusty Russell extern int sched_setscheduler_nocheck(struct task_struct *, int,
2620fe7de49fSKOSAKI Motohiro 				      const struct sched_param *);
2621d50dde5aSDario Faggioli extern int sched_setattr(struct task_struct *,
2622d50dde5aSDario Faggioli 			 const struct sched_attr *);
262336c8b586SIngo Molnar extern struct task_struct *idle_task(int cpu);
2624c4f30608SPaul E. McKenney /**
2625c4f30608SPaul E. McKenney  * is_idle_task - is the specified task an idle task?
2626fa757281SRandy Dunlap  * @p: the task in question.
2627e69f6186SYacine Belkadi  *
2628e69f6186SYacine Belkadi  * Return: 1 if @p is an idle task. 0 otherwise.
2629c4f30608SPaul E. McKenney  */
26307061ca3bSPaul E. McKenney static inline bool is_idle_task(const struct task_struct *p)
2631c4f30608SPaul E. McKenney {
2632c4f30608SPaul E. McKenney 	return p->pid == 0;
2633c4f30608SPaul E. McKenney }
263436c8b586SIngo Molnar extern struct task_struct *curr_task(int cpu);
2635a458ae2eSPeter Zijlstra extern void ia64_set_curr_task(int cpu, struct task_struct *p);
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds void yield(void);
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds union thread_union {
2640c65eacbeSAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK
26411da177e4SLinus Torvalds 	struct thread_info thread_info;
2642c65eacbeSAndy Lutomirski #endif
26431da177e4SLinus Torvalds 	unsigned long stack[THREAD_SIZE/sizeof(long)];
26441da177e4SLinus Torvalds };
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds #ifndef __HAVE_ARCH_KSTACK_END
26471da177e4SLinus Torvalds static inline int kstack_end(void *addr)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds 	/* Reliable end of stack detection:
26501da177e4SLinus Torvalds 	 * Some APM bios versions misalign the stack
26511da177e4SLinus Torvalds 	 */
26521da177e4SLinus Torvalds 	return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
26531da177e4SLinus Torvalds }
26541da177e4SLinus Torvalds #endif
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds extern union thread_union init_thread_union;
26571da177e4SLinus Torvalds extern struct task_struct init_task;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds extern struct   mm_struct init_mm;
26601da177e4SLinus Torvalds 
2661198fe21bSPavel Emelyanov extern struct pid_namespace init_pid_ns;
2662198fe21bSPavel Emelyanov 
2663198fe21bSPavel Emelyanov /*
2664198fe21bSPavel Emelyanov  * find a task by one of its numerical ids
2665198fe21bSPavel Emelyanov  *
2666198fe21bSPavel Emelyanov  * find_task_by_pid_ns():
2667198fe21bSPavel Emelyanov  *      finds a task by its pid in the specified namespace
2668228ebcbeSPavel Emelyanov  * find_task_by_vpid():
2669228ebcbeSPavel Emelyanov  *      finds a task by its virtual pid
2670198fe21bSPavel Emelyanov  *
2671e49859e7SPavel Emelyanov  * see also find_vpid() etc in include/linux/pid.h
2672198fe21bSPavel Emelyanov  */
2673198fe21bSPavel Emelyanov 
2674228ebcbeSPavel Emelyanov extern struct task_struct *find_task_by_vpid(pid_t nr);
2675228ebcbeSPavel Emelyanov extern struct task_struct *find_task_by_pid_ns(pid_t nr,
2676228ebcbeSPavel Emelyanov 		struct pid_namespace *ns);
2677198fe21bSPavel Emelyanov 
26781da177e4SLinus Torvalds /* per-UID process charging. */
26797b44ab97SEric W. Biederman extern struct user_struct * alloc_uid(kuid_t);
26801da177e4SLinus Torvalds static inline struct user_struct *get_uid(struct user_struct *u)
26811da177e4SLinus Torvalds {
26821da177e4SLinus Torvalds 	atomic_inc(&u->__count);
26831da177e4SLinus Torvalds 	return u;
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds extern void free_uid(struct user_struct *);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds #include <asm/current.h>
26881da177e4SLinus Torvalds 
2689f0af911aSTorben Hohn extern void xtime_update(unsigned long ticks);
26901da177e4SLinus Torvalds 
2691b3c97528SHarvey Harrison extern int wake_up_state(struct task_struct *tsk, unsigned int state);
2692b3c97528SHarvey Harrison extern int wake_up_process(struct task_struct *tsk);
26933e51e3edSSamir Bellabes extern void wake_up_new_task(struct task_struct *tsk);
26941da177e4SLinus Torvalds #ifdef CONFIG_SMP
26951da177e4SLinus Torvalds  extern void kick_process(struct task_struct *tsk);
26961da177e4SLinus Torvalds #else
26971da177e4SLinus Torvalds  static inline void kick_process(struct task_struct *tsk) { }
26981da177e4SLinus Torvalds #endif
2699aab03e05SDario Faggioli extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
2700ad46c2c4SIngo Molnar extern void sched_dead(struct task_struct *p);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds extern void proc_caches_init(void);
27031da177e4SLinus Torvalds extern void flush_signals(struct task_struct *);
270410ab825bSOleg Nesterov extern void ignore_signals(struct task_struct *);
27051da177e4SLinus Torvalds extern void flush_signal_handlers(struct task_struct *, int force_default);
27061da177e4SLinus Torvalds extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
27071da177e4SLinus Torvalds 
2708be0e6f29SOleg Nesterov static inline int kernel_dequeue_signal(siginfo_t *info)
27091da177e4SLinus Torvalds {
2710be0e6f29SOleg Nesterov 	struct task_struct *tsk = current;
2711be0e6f29SOleg Nesterov 	siginfo_t __info;
27121da177e4SLinus Torvalds 	int ret;
27131da177e4SLinus Torvalds 
2714be0e6f29SOleg Nesterov 	spin_lock_irq(&tsk->sighand->siglock);
2715be0e6f29SOleg Nesterov 	ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
2716be0e6f29SOleg Nesterov 	spin_unlock_irq(&tsk->sighand->siglock);
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	return ret;
27191da177e4SLinus Torvalds }
27201da177e4SLinus Torvalds 
27219a13049eSOleg Nesterov static inline void kernel_signal_stop(void)
27229a13049eSOleg Nesterov {
27239a13049eSOleg Nesterov 	spin_lock_irq(&current->sighand->siglock);
27249a13049eSOleg Nesterov 	if (current->jobctl & JOBCTL_STOP_DEQUEUED)
27259a13049eSOleg Nesterov 		__set_current_state(TASK_STOPPED);
27269a13049eSOleg Nesterov 	spin_unlock_irq(&current->sighand->siglock);
27279a13049eSOleg Nesterov 
27289a13049eSOleg Nesterov 	schedule();
27299a13049eSOleg Nesterov }
27309a13049eSOleg Nesterov 
27311da177e4SLinus Torvalds extern void release_task(struct task_struct * p);
27321da177e4SLinus Torvalds extern int send_sig_info(int, struct siginfo *, struct task_struct *);
27331da177e4SLinus Torvalds extern int force_sigsegv(int, struct task_struct *);
27341da177e4SLinus Torvalds extern int force_sig_info(int, struct siginfo *, struct task_struct *);
2735c4b92fc1SEric W. Biederman extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
2736c4b92fc1SEric W. Biederman extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
2737d178bc3aSSerge Hallyn extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
2738d178bc3aSSerge Hallyn 				const struct cred *, u32);
2739c4b92fc1SEric W. Biederman extern int kill_pgrp(struct pid *pid, int sig, int priv);
2740c4b92fc1SEric W. Biederman extern int kill_pid(struct pid *pid, int sig, int priv);
2741c3de4b38SMatthew Wilcox extern int kill_proc_info(int, struct siginfo *, pid_t);
274286773473SOleg Nesterov extern __must_check bool do_notify_parent(struct task_struct *, int);
2743a7f0765eSOleg Nesterov extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
27441da177e4SLinus Torvalds extern void force_sig(int, struct task_struct *);
27451da177e4SLinus Torvalds extern int send_sig(int, struct task_struct *, int);
274609faef11SOleg Nesterov extern int zap_other_threads(struct task_struct *p);
27471da177e4SLinus Torvalds extern struct sigqueue *sigqueue_alloc(void);
27481da177e4SLinus Torvalds extern void sigqueue_free(struct sigqueue *);
2749ac5c2153SOleg Nesterov extern int send_sigqueue(struct sigqueue *,  struct task_struct *, int group);
27509ac95f2fSOleg Nesterov extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
27511da177e4SLinus Torvalds 
27527e781418SAndy Lutomirski #ifdef TIF_RESTORE_SIGMASK
27537e781418SAndy Lutomirski /*
27547e781418SAndy Lutomirski  * Legacy restore_sigmask accessors.  These are inefficient on
27557e781418SAndy Lutomirski  * SMP architectures because they require atomic operations.
27567e781418SAndy Lutomirski  */
27577e781418SAndy Lutomirski 
27587e781418SAndy Lutomirski /**
27597e781418SAndy Lutomirski  * set_restore_sigmask() - make sure saved_sigmask processing gets done
27607e781418SAndy Lutomirski  *
27617e781418SAndy Lutomirski  * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
27627e781418SAndy Lutomirski  * will run before returning to user mode, to process the flag.  For
27637e781418SAndy Lutomirski  * all callers, TIF_SIGPENDING is already set or it's no harm to set
27647e781418SAndy Lutomirski  * it.  TIF_RESTORE_SIGMASK need not be in the set of bits that the
27657e781418SAndy Lutomirski  * arch code will notice on return to user mode, in case those bits
27667e781418SAndy Lutomirski  * are scarce.  We set TIF_SIGPENDING here to ensure that the arch
27677e781418SAndy Lutomirski  * signal code always gets run when TIF_RESTORE_SIGMASK is set.
27687e781418SAndy Lutomirski  */
27697e781418SAndy Lutomirski static inline void set_restore_sigmask(void)
27707e781418SAndy Lutomirski {
27717e781418SAndy Lutomirski 	set_thread_flag(TIF_RESTORE_SIGMASK);
27727e781418SAndy Lutomirski 	WARN_ON(!test_thread_flag(TIF_SIGPENDING));
27737e781418SAndy Lutomirski }
27747e781418SAndy Lutomirski static inline void clear_restore_sigmask(void)
27757e781418SAndy Lutomirski {
27767e781418SAndy Lutomirski 	clear_thread_flag(TIF_RESTORE_SIGMASK);
27777e781418SAndy Lutomirski }
27787e781418SAndy Lutomirski static inline bool test_restore_sigmask(void)
27797e781418SAndy Lutomirski {
27807e781418SAndy Lutomirski 	return test_thread_flag(TIF_RESTORE_SIGMASK);
27817e781418SAndy Lutomirski }
27827e781418SAndy Lutomirski static inline bool test_and_clear_restore_sigmask(void)
27837e781418SAndy Lutomirski {
27847e781418SAndy Lutomirski 	return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
27857e781418SAndy Lutomirski }
27867e781418SAndy Lutomirski 
27877e781418SAndy Lutomirski #else	/* TIF_RESTORE_SIGMASK */
27887e781418SAndy Lutomirski 
27897e781418SAndy Lutomirski /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
27907e781418SAndy Lutomirski static inline void set_restore_sigmask(void)
27917e781418SAndy Lutomirski {
27927e781418SAndy Lutomirski 	current->restore_sigmask = true;
27937e781418SAndy Lutomirski 	WARN_ON(!test_thread_flag(TIF_SIGPENDING));
27947e781418SAndy Lutomirski }
27957e781418SAndy Lutomirski static inline void clear_restore_sigmask(void)
27967e781418SAndy Lutomirski {
27977e781418SAndy Lutomirski 	current->restore_sigmask = false;
27987e781418SAndy Lutomirski }
27997e781418SAndy Lutomirski static inline bool test_restore_sigmask(void)
28007e781418SAndy Lutomirski {
28017e781418SAndy Lutomirski 	return current->restore_sigmask;
28027e781418SAndy Lutomirski }
28037e781418SAndy Lutomirski static inline bool test_and_clear_restore_sigmask(void)
28047e781418SAndy Lutomirski {
28057e781418SAndy Lutomirski 	if (!current->restore_sigmask)
28067e781418SAndy Lutomirski 		return false;
28077e781418SAndy Lutomirski 	current->restore_sigmask = false;
28087e781418SAndy Lutomirski 	return true;
28097e781418SAndy Lutomirski }
28107e781418SAndy Lutomirski #endif
28117e781418SAndy Lutomirski 
281251a7b448SAl Viro static inline void restore_saved_sigmask(void)
281351a7b448SAl Viro {
281451a7b448SAl Viro 	if (test_and_clear_restore_sigmask())
281577097ae5SAl Viro 		__set_current_blocked(&current->saved_sigmask);
281651a7b448SAl Viro }
281751a7b448SAl Viro 
2818b7f9a11aSAl Viro static inline sigset_t *sigmask_to_save(void)
2819b7f9a11aSAl Viro {
2820b7f9a11aSAl Viro 	sigset_t *res = &current->blocked;
2821b7f9a11aSAl Viro 	if (unlikely(test_restore_sigmask()))
2822b7f9a11aSAl Viro 		res = &current->saved_sigmask;
2823b7f9a11aSAl Viro 	return res;
2824b7f9a11aSAl Viro }
2825b7f9a11aSAl Viro 
28269ec52099SCedric Le Goater static inline int kill_cad_pid(int sig, int priv)
28279ec52099SCedric Le Goater {
28289ec52099SCedric Le Goater 	return kill_pid(cad_pid, sig, priv);
28299ec52099SCedric Le Goater }
28309ec52099SCedric Le Goater 
28311da177e4SLinus Torvalds /* These can be the second arg to send_sig_info/send_group_sig_info.  */
28321da177e4SLinus Torvalds #define SEND_SIG_NOINFO ((struct siginfo *) 0)
28331da177e4SLinus Torvalds #define SEND_SIG_PRIV	((struct siginfo *) 1)
28341da177e4SLinus Torvalds #define SEND_SIG_FORCED	((struct siginfo *) 2)
28351da177e4SLinus Torvalds 
28362a855dd0SSebastian Andrzej Siewior /*
28372a855dd0SSebastian Andrzej Siewior  * True if we are on the alternate signal stack.
28382a855dd0SSebastian Andrzej Siewior  */
28391da177e4SLinus Torvalds static inline int on_sig_stack(unsigned long sp)
28401da177e4SLinus Torvalds {
2841c876eeabSAndy Lutomirski 	/*
2842c876eeabSAndy Lutomirski 	 * If the signal stack is SS_AUTODISARM then, by construction, we
2843c876eeabSAndy Lutomirski 	 * can't be on the signal stack unless user code deliberately set
2844c876eeabSAndy Lutomirski 	 * SS_AUTODISARM when we were already on it.
2845c876eeabSAndy Lutomirski 	 *
2846c876eeabSAndy Lutomirski 	 * This improves reliability: if user state gets corrupted such that
2847c876eeabSAndy Lutomirski 	 * the stack pointer points very close to the end of the signal stack,
2848c876eeabSAndy Lutomirski 	 * then this check will enable the signal to be handled anyway.
2849c876eeabSAndy Lutomirski 	 */
2850c876eeabSAndy Lutomirski 	if (current->sas_ss_flags & SS_AUTODISARM)
2851c876eeabSAndy Lutomirski 		return 0;
2852c876eeabSAndy Lutomirski 
28532a855dd0SSebastian Andrzej Siewior #ifdef CONFIG_STACK_GROWSUP
28542a855dd0SSebastian Andrzej Siewior 	return sp >= current->sas_ss_sp &&
28552a855dd0SSebastian Andrzej Siewior 		sp - current->sas_ss_sp < current->sas_ss_size;
28562a855dd0SSebastian Andrzej Siewior #else
28572a855dd0SSebastian Andrzej Siewior 	return sp > current->sas_ss_sp &&
28582a855dd0SSebastian Andrzej Siewior 		sp - current->sas_ss_sp <= current->sas_ss_size;
28592a855dd0SSebastian Andrzej Siewior #endif
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds static inline int sas_ss_flags(unsigned long sp)
28631da177e4SLinus Torvalds {
286472f15c03SRichard Weinberger 	if (!current->sas_ss_size)
286572f15c03SRichard Weinberger 		return SS_DISABLE;
286672f15c03SRichard Weinberger 
286772f15c03SRichard Weinberger 	return on_sig_stack(sp) ? SS_ONSTACK : 0;
28681da177e4SLinus Torvalds }
28691da177e4SLinus Torvalds 
28702a742138SStas Sergeev static inline void sas_ss_reset(struct task_struct *p)
28712a742138SStas Sergeev {
28722a742138SStas Sergeev 	p->sas_ss_sp = 0;
28732a742138SStas Sergeev 	p->sas_ss_size = 0;
28742a742138SStas Sergeev 	p->sas_ss_flags = SS_DISABLE;
28752a742138SStas Sergeev }
28762a742138SStas Sergeev 
28775a1b98d3SAl Viro static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
28785a1b98d3SAl Viro {
28795a1b98d3SAl Viro 	if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
28805a1b98d3SAl Viro #ifdef CONFIG_STACK_GROWSUP
28815a1b98d3SAl Viro 		return current->sas_ss_sp;
28825a1b98d3SAl Viro #else
28835a1b98d3SAl Viro 		return current->sas_ss_sp + current->sas_ss_size;
28845a1b98d3SAl Viro #endif
28855a1b98d3SAl Viro 	return sp;
28865a1b98d3SAl Viro }
28875a1b98d3SAl Viro 
28881da177e4SLinus Torvalds /*
28891da177e4SLinus Torvalds  * Routines for handling mm_structs
28901da177e4SLinus Torvalds  */
28911da177e4SLinus Torvalds extern struct mm_struct * mm_alloc(void);
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds /* mmdrop drops the mm and the page tables */
2894b3c97528SHarvey Harrison extern void __mmdrop(struct mm_struct *);
28951da177e4SLinus Torvalds static inline void mmdrop(struct mm_struct *mm)
28961da177e4SLinus Torvalds {
28976fb43d7bSIngo Molnar 	if (unlikely(atomic_dec_and_test(&mm->mm_count)))
28981da177e4SLinus Torvalds 		__mmdrop(mm);
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29017283094eSMichal Hocko static inline void mmdrop_async_fn(struct work_struct *work)
29027283094eSMichal Hocko {
29037283094eSMichal Hocko 	struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work);
29047283094eSMichal Hocko 	__mmdrop(mm);
29057283094eSMichal Hocko }
29067283094eSMichal Hocko 
29077283094eSMichal Hocko static inline void mmdrop_async(struct mm_struct *mm)
29087283094eSMichal Hocko {
29097283094eSMichal Hocko 	if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
29107283094eSMichal Hocko 		INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
29117283094eSMichal Hocko 		schedule_work(&mm->async_put_work);
29127283094eSMichal Hocko 	}
29137283094eSMichal Hocko }
29147283094eSMichal Hocko 
2915d2005e3fSOleg Nesterov static inline bool mmget_not_zero(struct mm_struct *mm)
2916d2005e3fSOleg Nesterov {
2917d2005e3fSOleg Nesterov 	return atomic_inc_not_zero(&mm->mm_users);
29181da177e4SLinus Torvalds }
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds /* mmput gets rid of the mappings and all user-space */
29211da177e4SLinus Torvalds extern void mmput(struct mm_struct *);
29227ef949d7SMichal Hocko #ifdef CONFIG_MMU
29237ef949d7SMichal Hocko /* same as above but performs the slow path from the async context. Can
2924ec8d7c14SMichal Hocko  * be called from the atomic context as well
2925ec8d7c14SMichal Hocko  */
2926ec8d7c14SMichal Hocko extern void mmput_async(struct mm_struct *);
29277ef949d7SMichal Hocko #endif
2928ec8d7c14SMichal Hocko 
29291da177e4SLinus Torvalds /* Grab a reference to a task's mm, if it is not already going away */
29301da177e4SLinus Torvalds extern struct mm_struct *get_task_mm(struct task_struct *task);
29318cdb878dSChristopher Yeoh /*
29328cdb878dSChristopher Yeoh  * Grab a reference to a task's mm, if it is not already going away
29338cdb878dSChristopher Yeoh  * and ptrace_may_access with the mode parameter passed to it
29348cdb878dSChristopher Yeoh  * succeeds.
29358cdb878dSChristopher Yeoh  */
29368cdb878dSChristopher Yeoh extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
29371da177e4SLinus Torvalds /* Remove the current tasks stale references to the old mm_struct */
29381da177e4SLinus Torvalds extern void mm_release(struct task_struct *, struct mm_struct *);
29391da177e4SLinus Torvalds 
29403033f14aSJosh Triplett #ifdef CONFIG_HAVE_COPY_THREAD_TLS
29413033f14aSJosh Triplett extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
29423033f14aSJosh Triplett 			struct task_struct *, unsigned long);
29433033f14aSJosh Triplett #else
29446f2c55b8SAlexey Dobriyan extern int copy_thread(unsigned long, unsigned long, unsigned long,
2945afa86fc4SAl Viro 			struct task_struct *);
29463033f14aSJosh Triplett 
29473033f14aSJosh Triplett /* Architectures that haven't opted into copy_thread_tls get the tls argument
29483033f14aSJosh Triplett  * via pt_regs, so ignore the tls argument passed via C. */
29493033f14aSJosh Triplett static inline int copy_thread_tls(
29503033f14aSJosh Triplett 		unsigned long clone_flags, unsigned long sp, unsigned long arg,
29513033f14aSJosh Triplett 		struct task_struct *p, unsigned long tls)
29523033f14aSJosh Triplett {
29533033f14aSJosh Triplett 	return copy_thread(clone_flags, sp, arg, p);
29543033f14aSJosh Triplett }
29553033f14aSJosh Triplett #endif
29561da177e4SLinus Torvalds extern void flush_thread(void);
29575f56a5dfSJiri Slaby 
29585f56a5dfSJiri Slaby #ifdef CONFIG_HAVE_EXIT_THREAD
2959e6464694SJiri Slaby extern void exit_thread(struct task_struct *tsk);
29605f56a5dfSJiri Slaby #else
2961e6464694SJiri Slaby static inline void exit_thread(struct task_struct *tsk)
29625f56a5dfSJiri Slaby {
29635f56a5dfSJiri Slaby }
29645f56a5dfSJiri Slaby #endif
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds extern void exit_files(struct task_struct *);
2967a7e5328aSOleg Nesterov extern void __cleanup_sighand(struct sighand_struct *);
2968cbaffba1SOleg Nesterov 
29691da177e4SLinus Torvalds extern void exit_itimers(struct signal_struct *);
2970cbaffba1SOleg Nesterov extern void flush_itimer_signals(void);
29711da177e4SLinus Torvalds 
29729402c95fSJoe Perches extern void do_group_exit(int);
29731da177e4SLinus Torvalds 
2974c4ad8f98SLinus Torvalds extern int do_execve(struct filename *,
2975d7627467SDavid Howells 		     const char __user * const __user *,
2976da3d4c5fSAl Viro 		     const char __user * const __user *);
297751f39a1fSDavid Drysdale extern int do_execveat(int, struct filename *,
297851f39a1fSDavid Drysdale 		       const char __user * const __user *,
297951f39a1fSDavid Drysdale 		       const char __user * const __user *,
298051f39a1fSDavid Drysdale 		       int);
29813033f14aSJosh Triplett extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
2982e80d6661SAl Viro extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
298336c8b586SIngo Molnar struct task_struct *fork_idle(int);
29842aa3a7f8SAl Viro extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
29851da177e4SLinus Torvalds 
298682b89778SAdrian Hunter extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
298782b89778SAdrian Hunter static inline void set_task_comm(struct task_struct *tsk, const char *from)
298882b89778SAdrian Hunter {
298982b89778SAdrian Hunter 	__set_task_comm(tsk, from, false);
299082b89778SAdrian Hunter }
299159714d65SAndrew Morton extern char *get_task_comm(char *to, struct task_struct *tsk);
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds #ifdef CONFIG_SMP
2994317f3941SPeter Zijlstra void scheduler_ipi(void);
299585ba2d86SRoland McGrath extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
29961da177e4SLinus Torvalds #else
2997184748ccSPeter Zijlstra static inline void scheduler_ipi(void) { }
299885ba2d86SRoland McGrath static inline unsigned long wait_task_inactive(struct task_struct *p,
299985ba2d86SRoland McGrath 					       long match_state)
300085ba2d86SRoland McGrath {
300185ba2d86SRoland McGrath 	return 1;
300285ba2d86SRoland McGrath }
30031da177e4SLinus Torvalds #endif
30041da177e4SLinus Torvalds 
3005fafe870fSFrederic Weisbecker #define tasklist_empty() \
3006fafe870fSFrederic Weisbecker 	list_empty(&init_task.tasks)
3007fafe870fSFrederic Weisbecker 
300805725f7eSJiri Pirko #define next_task(p) \
300905725f7eSJiri Pirko 	list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds #define for_each_process(p) \
30121da177e4SLinus Torvalds 	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
30131da177e4SLinus Torvalds 
30145bb459bbSOleg Nesterov extern bool current_is_single_threaded(void);
3015d84f4f99SDavid Howells 
30161da177e4SLinus Torvalds /*
30171da177e4SLinus Torvalds  * Careful: do_each_thread/while_each_thread is a double loop so
30181da177e4SLinus Torvalds  *          'break' will not work as expected - use goto instead.
30191da177e4SLinus Torvalds  */
30201da177e4SLinus Torvalds #define do_each_thread(g, t) \
30211da177e4SLinus Torvalds 	for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds #define while_each_thread(g, t) \
30241da177e4SLinus Torvalds 	while ((t = next_thread(t)) != g)
30251da177e4SLinus Torvalds 
30260c740d0aSOleg Nesterov #define __for_each_thread(signal, t)	\
30270c740d0aSOleg Nesterov 	list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
30280c740d0aSOleg Nesterov 
30290c740d0aSOleg Nesterov #define for_each_thread(p, t)		\
30300c740d0aSOleg Nesterov 	__for_each_thread((p)->signal, t)
30310c740d0aSOleg Nesterov 
30320c740d0aSOleg Nesterov /* Careful: this is a double loop, 'break' won't work as expected. */
30330c740d0aSOleg Nesterov #define for_each_process_thread(p, t)	\
30340c740d0aSOleg Nesterov 	for_each_process(p) for_each_thread(p, t)
30350c740d0aSOleg Nesterov 
30367e49827cSOleg Nesterov static inline int get_nr_threads(struct task_struct *tsk)
30377e49827cSOleg Nesterov {
3038b3ac022cSOleg Nesterov 	return tsk->signal->nr_threads;
30397e49827cSOleg Nesterov }
30407e49827cSOleg Nesterov 
3041087806b1SOleg Nesterov static inline bool thread_group_leader(struct task_struct *p)
3042087806b1SOleg Nesterov {
3043087806b1SOleg Nesterov 	return p->exit_signal >= 0;
3044087806b1SOleg Nesterov }
30451da177e4SLinus Torvalds 
30460804ef4bSEric W. Biederman /* Do to the insanities of de_thread it is possible for a process
30470804ef4bSEric W. Biederman  * to have the pid of the thread group leader without actually being
30480804ef4bSEric W. Biederman  * the thread group leader.  For iteration through the pids in proc
30490804ef4bSEric W. Biederman  * all we care about is that we have a task with the appropriate
30500804ef4bSEric W. Biederman  * pid, we don't actually care if we have the right task.
30510804ef4bSEric W. Biederman  */
3052e1403b8eSOleg Nesterov static inline bool has_group_leader_pid(struct task_struct *p)
30530804ef4bSEric W. Biederman {
3054e1403b8eSOleg Nesterov 	return task_pid(p) == p->signal->leader_pid;
30550804ef4bSEric W. Biederman }
30560804ef4bSEric W. Biederman 
3057bac0abd6SPavel Emelyanov static inline
3058e1403b8eSOleg Nesterov bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
3059bac0abd6SPavel Emelyanov {
3060e1403b8eSOleg Nesterov 	return p1->signal == p2->signal;
3061bac0abd6SPavel Emelyanov }
3062bac0abd6SPavel Emelyanov 
306336c8b586SIngo Molnar static inline struct task_struct *next_thread(const struct task_struct *p)
306447e65328SOleg Nesterov {
306505725f7eSJiri Pirko 	return list_entry_rcu(p->thread_group.next,
306636c8b586SIngo Molnar 			      struct task_struct, thread_group);
306747e65328SOleg Nesterov }
306847e65328SOleg Nesterov 
3069e868171aSAlexey Dobriyan static inline int thread_group_empty(struct task_struct *p)
30701da177e4SLinus Torvalds {
307147e65328SOleg Nesterov 	return list_empty(&p->thread_group);
30721da177e4SLinus Torvalds }
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds #define delay_group_leader(p) \
30751da177e4SLinus Torvalds 		(thread_group_leader(p) && !thread_group_empty(p))
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds /*
3078260ea101SEric W. Biederman  * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
307922e2c507SJens Axboe  * subscriptions and synchronises with wait4().  Also used in procfs.  Also
3080ddbcc7e8SPaul Menage  * pins the final release of task.io_context.  Also protects ->cpuset and
3081d68b46feSOleg Nesterov  * ->cgroup.subsys[]. And ->vfork_done.
30821da177e4SLinus Torvalds  *
30831da177e4SLinus Torvalds  * Nests both inside and outside of read_lock(&tasklist_lock).
30841da177e4SLinus Torvalds  * It must not be nested with write_lock_irq(&tasklist_lock),
30851da177e4SLinus Torvalds  * neither inside nor outside.
30861da177e4SLinus Torvalds  */
30871da177e4SLinus Torvalds static inline void task_lock(struct task_struct *p)
30881da177e4SLinus Torvalds {
30891da177e4SLinus Torvalds 	spin_lock(&p->alloc_lock);
30901da177e4SLinus Torvalds }
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds static inline void task_unlock(struct task_struct *p)
30931da177e4SLinus Torvalds {
30941da177e4SLinus Torvalds 	spin_unlock(&p->alloc_lock);
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
3097b8ed374eSNamhyung Kim extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
3098f63ee72eSOleg Nesterov 							unsigned long *flags);
3099f63ee72eSOleg Nesterov 
31009388dc30SAnton Vorontsov static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
31019388dc30SAnton Vorontsov 						       unsigned long *flags)
31029388dc30SAnton Vorontsov {
31039388dc30SAnton Vorontsov 	struct sighand_struct *ret;
31049388dc30SAnton Vorontsov 
31059388dc30SAnton Vorontsov 	ret = __lock_task_sighand(tsk, flags);
31069388dc30SAnton Vorontsov 	(void)__cond_lock(&tsk->sighand->siglock, ret);
31079388dc30SAnton Vorontsov 	return ret;
31089388dc30SAnton Vorontsov }
3109b8ed374eSNamhyung Kim 
3110f63ee72eSOleg Nesterov static inline void unlock_task_sighand(struct task_struct *tsk,
3111f63ee72eSOleg Nesterov 						unsigned long *flags)
3112f63ee72eSOleg Nesterov {
3113f63ee72eSOleg Nesterov 	spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
3114f63ee72eSOleg Nesterov }
3115f63ee72eSOleg Nesterov 
31167d7efec3STejun Heo /**
31177d7efec3STejun Heo  * threadgroup_change_begin - mark the beginning of changes to a threadgroup
31187d7efec3STejun Heo  * @tsk: task causing the changes
31197d7efec3STejun Heo  *
31207d7efec3STejun Heo  * All operations which modify a threadgroup - a new thread joining the
31217d7efec3STejun Heo  * group, death of a member thread (the assertion of PF_EXITING) and
31227d7efec3STejun Heo  * exec(2) dethreading the process and replacing the leader - are wrapped
31237d7efec3STejun Heo  * by threadgroup_change_{begin|end}().  This is to provide a place which
31247d7efec3STejun Heo  * subsystems needing threadgroup stability can hook into for
31257d7efec3STejun Heo  * synchronization.
31267d7efec3STejun Heo  */
3127257058aeSTejun Heo static inline void threadgroup_change_begin(struct task_struct *tsk)
31284714d1d3SBen Blum {
31297d7efec3STejun Heo 	might_sleep();
31307d7efec3STejun Heo 	cgroup_threadgroup_change_begin(tsk);
31314714d1d3SBen Blum }
31327d7efec3STejun Heo 
31337d7efec3STejun Heo /**
31347d7efec3STejun Heo  * threadgroup_change_end - mark the end of changes to a threadgroup
31357d7efec3STejun Heo  * @tsk: task causing the changes
31367d7efec3STejun Heo  *
31377d7efec3STejun Heo  * See threadgroup_change_begin().
31387d7efec3STejun Heo  */
3139257058aeSTejun Heo static inline void threadgroup_change_end(struct task_struct *tsk)
31404714d1d3SBen Blum {
31417d7efec3STejun Heo 	cgroup_threadgroup_change_end(tsk);
31424714d1d3SBen Blum }
314377e4ef99STejun Heo 
3144c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
3145c65eacbeSAndy Lutomirski 
3146c65eacbeSAndy Lutomirski static inline struct thread_info *task_thread_info(struct task_struct *task)
3147c65eacbeSAndy Lutomirski {
3148c65eacbeSAndy Lutomirski 	return &task->thread_info;
3149c65eacbeSAndy Lutomirski }
3150c6c314a6SAndy Lutomirski 
3151c6c314a6SAndy Lutomirski /*
3152c6c314a6SAndy Lutomirski  * When accessing the stack of a non-current task that might exit, use
3153c6c314a6SAndy Lutomirski  * try_get_task_stack() instead.  task_stack_page will return a pointer
3154c6c314a6SAndy Lutomirski  * that could get freed out from under you.
3155c6c314a6SAndy Lutomirski  */
3156c65eacbeSAndy Lutomirski static inline void *task_stack_page(const struct task_struct *task)
3157c65eacbeSAndy Lutomirski {
3158c65eacbeSAndy Lutomirski 	return task->stack;
3159c65eacbeSAndy Lutomirski }
3160c6c314a6SAndy Lutomirski 
3161c65eacbeSAndy Lutomirski #define setup_thread_stack(new,old)	do { } while(0)
3162c6c314a6SAndy Lutomirski 
3163c65eacbeSAndy Lutomirski static inline unsigned long *end_of_stack(const struct task_struct *task)
3164c65eacbeSAndy Lutomirski {
3165c65eacbeSAndy Lutomirski 	return task->stack;
3166c65eacbeSAndy Lutomirski }
3167c65eacbeSAndy Lutomirski 
3168c65eacbeSAndy Lutomirski #elif !defined(__HAVE_THREAD_FUNCTIONS)
3169f037360fSAl Viro 
3170f7e4217bSRoman Zippel #define task_thread_info(task)	((struct thread_info *)(task)->stack)
3171c65eacbeSAndy Lutomirski #define task_stack_page(task)	((void *)(task)->stack)
3172a1261f54SAl Viro 
317310ebffdeSAl Viro static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
317410ebffdeSAl Viro {
317510ebffdeSAl Viro 	*task_thread_info(p) = *task_thread_info(org);
317610ebffdeSAl Viro 	task_thread_info(p)->task = p;
317710ebffdeSAl Viro }
317810ebffdeSAl Viro 
31796a40281aSChuck Ebbert /*
31806a40281aSChuck Ebbert  * Return the address of the last usable long on the stack.
31816a40281aSChuck Ebbert  *
31826a40281aSChuck Ebbert  * When the stack grows down, this is just above the thread
31836a40281aSChuck Ebbert  * info struct. Going any lower will corrupt the threadinfo.
31846a40281aSChuck Ebbert  *
31856a40281aSChuck Ebbert  * When the stack grows up, this is the highest address.
31866a40281aSChuck Ebbert  * Beyond that position, we corrupt data on the next page.
31876a40281aSChuck Ebbert  */
318810ebffdeSAl Viro static inline unsigned long *end_of_stack(struct task_struct *p)
318910ebffdeSAl Viro {
31906a40281aSChuck Ebbert #ifdef CONFIG_STACK_GROWSUP
31916a40281aSChuck Ebbert 	return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
31926a40281aSChuck Ebbert #else
3193f7e4217bSRoman Zippel 	return (unsigned long *)(task_thread_info(p) + 1);
31946a40281aSChuck Ebbert #endif
319510ebffdeSAl Viro }
319610ebffdeSAl Viro 
3197f037360fSAl Viro #endif
3198c6c314a6SAndy Lutomirski 
319968f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
320068f24b08SAndy Lutomirski static inline void *try_get_task_stack(struct task_struct *tsk)
320168f24b08SAndy Lutomirski {
320268f24b08SAndy Lutomirski 	return atomic_inc_not_zero(&tsk->stack_refcount) ?
320368f24b08SAndy Lutomirski 		task_stack_page(tsk) : NULL;
320468f24b08SAndy Lutomirski }
320568f24b08SAndy Lutomirski 
320668f24b08SAndy Lutomirski extern void put_task_stack(struct task_struct *tsk);
320768f24b08SAndy Lutomirski #else
3208c6c314a6SAndy Lutomirski static inline void *try_get_task_stack(struct task_struct *tsk)
3209c6c314a6SAndy Lutomirski {
3210c6c314a6SAndy Lutomirski 	return task_stack_page(tsk);
3211c6c314a6SAndy Lutomirski }
3212c6c314a6SAndy Lutomirski 
3213c6c314a6SAndy Lutomirski static inline void put_task_stack(struct task_struct *tsk) {}
321468f24b08SAndy Lutomirski #endif
3215c6c314a6SAndy Lutomirski 
3216a70857e4SAaron Tomlin #define task_stack_end_corrupted(task) \
3217a70857e4SAaron Tomlin 		(*(end_of_stack(task)) != STACK_END_MAGIC)
3218f037360fSAl Viro 
32198b05c7e6SFUJITA Tomonori static inline int object_is_on_stack(void *obj)
32208b05c7e6SFUJITA Tomonori {
32218b05c7e6SFUJITA Tomonori 	void *stack = task_stack_page(current);
32228b05c7e6SFUJITA Tomonori 
32238b05c7e6SFUJITA Tomonori 	return (obj >= stack) && (obj < (stack + THREAD_SIZE));
32248b05c7e6SFUJITA Tomonori }
32258b05c7e6SFUJITA Tomonori 
3226b235beeaSLinus Torvalds extern void thread_stack_cache_init(void);
32278c9843e5SBenjamin Herrenschmidt 
32287c9f8861SEric Sandeen #ifdef CONFIG_DEBUG_STACK_USAGE
32297c9f8861SEric Sandeen static inline unsigned long stack_not_used(struct task_struct *p)
32307c9f8861SEric Sandeen {
32317c9f8861SEric Sandeen 	unsigned long *n = end_of_stack(p);
32327c9f8861SEric Sandeen 
32337c9f8861SEric Sandeen 	do { 	/* Skip over canary */
32346c31da34SHelge Deller # ifdef CONFIG_STACK_GROWSUP
32356c31da34SHelge Deller 		n--;
32366c31da34SHelge Deller # else
32377c9f8861SEric Sandeen 		n++;
32386c31da34SHelge Deller # endif
32397c9f8861SEric Sandeen 	} while (!*n);
32407c9f8861SEric Sandeen 
32416c31da34SHelge Deller # ifdef CONFIG_STACK_GROWSUP
32426c31da34SHelge Deller 	return (unsigned long)end_of_stack(p) - (unsigned long)n;
32436c31da34SHelge Deller # else
32447c9f8861SEric Sandeen 	return (unsigned long)n - (unsigned long)end_of_stack(p);
32456c31da34SHelge Deller # endif
32467c9f8861SEric Sandeen }
32477c9f8861SEric Sandeen #endif
3248d4311ff1SAaron Tomlin extern void set_task_stack_end_magic(struct task_struct *tsk);
32497c9f8861SEric Sandeen 
32501da177e4SLinus Torvalds /* set thread flags in other task's structures
32511da177e4SLinus Torvalds  * - see asm/thread_info.h for TIF_xxxx flags available
32521da177e4SLinus Torvalds  */
32531da177e4SLinus Torvalds static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
32541da177e4SLinus Torvalds {
3255a1261f54SAl Viro 	set_ti_thread_flag(task_thread_info(tsk), flag);
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
32591da177e4SLinus Torvalds {
3260a1261f54SAl Viro 	clear_ti_thread_flag(task_thread_info(tsk), flag);
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
32641da177e4SLinus Torvalds {
3265a1261f54SAl Viro 	return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
32661da177e4SLinus Torvalds }
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
32691da177e4SLinus Torvalds {
3270a1261f54SAl Viro 	return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
32711da177e4SLinus Torvalds }
32721da177e4SLinus Torvalds 
32731da177e4SLinus Torvalds static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
32741da177e4SLinus Torvalds {
3275a1261f54SAl Viro 	return test_ti_thread_flag(task_thread_info(tsk), flag);
32761da177e4SLinus Torvalds }
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds static inline void set_tsk_need_resched(struct task_struct *tsk)
32791da177e4SLinus Torvalds {
32801da177e4SLinus Torvalds 	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
32811da177e4SLinus Torvalds }
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds static inline void clear_tsk_need_resched(struct task_struct *tsk)
32841da177e4SLinus Torvalds {
32851da177e4SLinus Torvalds 	clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds 
32888ae121acSGregory Haskins static inline int test_tsk_need_resched(struct task_struct *tsk)
32898ae121acSGregory Haskins {
32908ae121acSGregory Haskins 	return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
32918ae121acSGregory Haskins }
32928ae121acSGregory Haskins 
3293690cc3ffSEric W. Biederman static inline int restart_syscall(void)
3294690cc3ffSEric W. Biederman {
3295690cc3ffSEric W. Biederman 	set_tsk_thread_flag(current, TIF_SIGPENDING);
3296690cc3ffSEric W. Biederman 	return -ERESTARTNOINTR;
3297690cc3ffSEric W. Biederman }
3298690cc3ffSEric W. Biederman 
32991da177e4SLinus Torvalds static inline int signal_pending(struct task_struct *p)
33001da177e4SLinus Torvalds {
33011da177e4SLinus Torvalds 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
33021da177e4SLinus Torvalds }
33031da177e4SLinus Torvalds 
3304d9588725SRoland McGrath static inline int __fatal_signal_pending(struct task_struct *p)
3305d9588725SRoland McGrath {
3306d9588725SRoland McGrath 	return unlikely(sigismember(&p->pending.signal, SIGKILL));
3307d9588725SRoland McGrath }
3308f776d12dSMatthew Wilcox 
3309f776d12dSMatthew Wilcox static inline int fatal_signal_pending(struct task_struct *p)
3310f776d12dSMatthew Wilcox {
3311f776d12dSMatthew Wilcox 	return signal_pending(p) && __fatal_signal_pending(p);
3312f776d12dSMatthew Wilcox }
3313f776d12dSMatthew Wilcox 
331416882c1eSOleg Nesterov static inline int signal_pending_state(long state, struct task_struct *p)
331516882c1eSOleg Nesterov {
331616882c1eSOleg Nesterov 	if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
331716882c1eSOleg Nesterov 		return 0;
331816882c1eSOleg Nesterov 	if (!signal_pending(p))
331916882c1eSOleg Nesterov 		return 0;
332016882c1eSOleg Nesterov 
332116882c1eSOleg Nesterov 	return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
332216882c1eSOleg Nesterov }
332316882c1eSOleg Nesterov 
33241da177e4SLinus Torvalds /*
33251da177e4SLinus Torvalds  * cond_resched() and cond_resched_lock(): latency reduction via
33261da177e4SLinus Torvalds  * explicit rescheduling in places that are safe. The return
33271da177e4SLinus Torvalds  * value indicates whether a reschedule was done in fact.
33281da177e4SLinus Torvalds  * cond_resched_lock() will drop the spinlock before scheduling,
33291da177e4SLinus Torvalds  * cond_resched_softirq() will enable bhs before scheduling.
33301da177e4SLinus Torvalds  */
333135a773a0SPeter Zijlstra #ifndef CONFIG_PREEMPT
3332c3921ab7SLinus Torvalds extern int _cond_resched(void);
333335a773a0SPeter Zijlstra #else
333435a773a0SPeter Zijlstra static inline int _cond_resched(void) { return 0; }
333535a773a0SPeter Zijlstra #endif
33366f80bd98SFrederic Weisbecker 
3337613afbf8SFrederic Weisbecker #define cond_resched() ({			\
33383427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, 0);	\
3339613afbf8SFrederic Weisbecker 	_cond_resched();			\
3340613afbf8SFrederic Weisbecker })
33416f80bd98SFrederic Weisbecker 
3342613afbf8SFrederic Weisbecker extern int __cond_resched_lock(spinlock_t *lock);
3343613afbf8SFrederic Weisbecker 
3344613afbf8SFrederic Weisbecker #define cond_resched_lock(lock) ({				\
33453427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
3346613afbf8SFrederic Weisbecker 	__cond_resched_lock(lock);				\
3347613afbf8SFrederic Weisbecker })
3348613afbf8SFrederic Weisbecker 
3349613afbf8SFrederic Weisbecker extern int __cond_resched_softirq(void);
3350613afbf8SFrederic Weisbecker 
3351613afbf8SFrederic Weisbecker #define cond_resched_softirq() ({					\
33523427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET);	\
3353613afbf8SFrederic Weisbecker 	__cond_resched_softirq();					\
3354613afbf8SFrederic Weisbecker })
33551da177e4SLinus Torvalds 
3356f6f3c437SSimon Horman static inline void cond_resched_rcu(void)
3357f6f3c437SSimon Horman {
3358f6f3c437SSimon Horman #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
3359f6f3c437SSimon Horman 	rcu_read_unlock();
3360f6f3c437SSimon Horman 	cond_resched();
3361f6f3c437SSimon Horman 	rcu_read_lock();
3362f6f3c437SSimon Horman #endif
3363f6f3c437SSimon Horman }
3364f6f3c437SSimon Horman 
3365d1c6d149SVegard Nossum static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
3366d1c6d149SVegard Nossum {
3367d1c6d149SVegard Nossum #ifdef CONFIG_DEBUG_PREEMPT
3368d1c6d149SVegard Nossum 	return p->preempt_disable_ip;
3369d1c6d149SVegard Nossum #else
3370d1c6d149SVegard Nossum 	return 0;
3371d1c6d149SVegard Nossum #endif
3372d1c6d149SVegard Nossum }
3373d1c6d149SVegard Nossum 
33741da177e4SLinus Torvalds /*
33751da177e4SLinus Torvalds  * Does a critical section need to be broken due to another
337695c354feSNick Piggin  * task waiting?: (technically does not depend on CONFIG_PREEMPT,
337795c354feSNick Piggin  * but a general need for low latency)
33781da177e4SLinus Torvalds  */
337995c354feSNick Piggin static inline int spin_needbreak(spinlock_t *lock)
33801da177e4SLinus Torvalds {
338195c354feSNick Piggin #ifdef CONFIG_PREEMPT
338295c354feSNick Piggin 	return spin_is_contended(lock);
338395c354feSNick Piggin #else
33841da177e4SLinus Torvalds 	return 0;
338595c354feSNick Piggin #endif
33861da177e4SLinus Torvalds }
33871da177e4SLinus Torvalds 
33887bb44adeSRoland McGrath /*
3389ee761f62SThomas Gleixner  * Idle thread specific functions to determine the need_resched
339069dd0f84SPeter Zijlstra  * polling state.
3391ee761f62SThomas Gleixner  */
339269dd0f84SPeter Zijlstra #ifdef TIF_POLLING_NRFLAG
3393ee761f62SThomas Gleixner static inline int tsk_is_polling(struct task_struct *p)
3394ee761f62SThomas Gleixner {
3395ee761f62SThomas Gleixner 	return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
3396ee761f62SThomas Gleixner }
3397ea811747SPeter Zijlstra 
3398ea811747SPeter Zijlstra static inline void __current_set_polling(void)
33993a98f871SThomas Gleixner {
34003a98f871SThomas Gleixner 	set_thread_flag(TIF_POLLING_NRFLAG);
34013a98f871SThomas Gleixner }
34023a98f871SThomas Gleixner 
3403ea811747SPeter Zijlstra static inline bool __must_check current_set_polling_and_test(void)
3404ea811747SPeter Zijlstra {
3405ea811747SPeter Zijlstra 	__current_set_polling();
3406ea811747SPeter Zijlstra 
3407ea811747SPeter Zijlstra 	/*
3408ea811747SPeter Zijlstra 	 * Polling state must be visible before we test NEED_RESCHED,
34098875125eSKirill Tkhai 	 * paired by resched_curr()
3410ea811747SPeter Zijlstra 	 */
34114e857c58SPeter Zijlstra 	smp_mb__after_atomic();
3412ea811747SPeter Zijlstra 
3413ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3414ea811747SPeter Zijlstra }
3415ea811747SPeter Zijlstra 
3416ea811747SPeter Zijlstra static inline void __current_clr_polling(void)
34173a98f871SThomas Gleixner {
34183a98f871SThomas Gleixner 	clear_thread_flag(TIF_POLLING_NRFLAG);
34193a98f871SThomas Gleixner }
3420ea811747SPeter Zijlstra 
3421ea811747SPeter Zijlstra static inline bool __must_check current_clr_polling_and_test(void)
3422ea811747SPeter Zijlstra {
3423ea811747SPeter Zijlstra 	__current_clr_polling();
3424ea811747SPeter Zijlstra 
3425ea811747SPeter Zijlstra 	/*
3426ea811747SPeter Zijlstra 	 * Polling state must be visible before we test NEED_RESCHED,
34278875125eSKirill Tkhai 	 * paired by resched_curr()
3428ea811747SPeter Zijlstra 	 */
34294e857c58SPeter Zijlstra 	smp_mb__after_atomic();
3430ea811747SPeter Zijlstra 
3431ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3432ea811747SPeter Zijlstra }
3433ea811747SPeter Zijlstra 
3434ee761f62SThomas Gleixner #else
3435ee761f62SThomas Gleixner static inline int tsk_is_polling(struct task_struct *p) { return 0; }
3436ea811747SPeter Zijlstra static inline void __current_set_polling(void) { }
3437ea811747SPeter Zijlstra static inline void __current_clr_polling(void) { }
3438ea811747SPeter Zijlstra 
3439ea811747SPeter Zijlstra static inline bool __must_check current_set_polling_and_test(void)
3440ea811747SPeter Zijlstra {
3441ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3442ea811747SPeter Zijlstra }
3443ea811747SPeter Zijlstra static inline bool __must_check current_clr_polling_and_test(void)
3444ea811747SPeter Zijlstra {
3445ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3446ea811747SPeter Zijlstra }
3447ee761f62SThomas Gleixner #endif
3448ee761f62SThomas Gleixner 
34498cb75e0cSPeter Zijlstra static inline void current_clr_polling(void)
34508cb75e0cSPeter Zijlstra {
34518cb75e0cSPeter Zijlstra 	__current_clr_polling();
34528cb75e0cSPeter Zijlstra 
34538cb75e0cSPeter Zijlstra 	/*
34548cb75e0cSPeter Zijlstra 	 * Ensure we check TIF_NEED_RESCHED after we clear the polling bit.
34558cb75e0cSPeter Zijlstra 	 * Once the bit is cleared, we'll get IPIs with every new
34568cb75e0cSPeter Zijlstra 	 * TIF_NEED_RESCHED and the IPI handler, scheduler_ipi(), will also
34578cb75e0cSPeter Zijlstra 	 * fold.
34588cb75e0cSPeter Zijlstra 	 */
34598875125eSKirill Tkhai 	smp_mb(); /* paired with resched_curr() */
34608cb75e0cSPeter Zijlstra 
34618cb75e0cSPeter Zijlstra 	preempt_fold_need_resched();
34628cb75e0cSPeter Zijlstra }
34638cb75e0cSPeter Zijlstra 
346475f93fedSPeter Zijlstra static __always_inline bool need_resched(void)
346575f93fedSPeter Zijlstra {
346675f93fedSPeter Zijlstra 	return unlikely(tif_need_resched());
346775f93fedSPeter Zijlstra }
346875f93fedSPeter Zijlstra 
3469ee761f62SThomas Gleixner /*
3470f06febc9SFrank Mayhar  * Thread group CPU time accounting.
3471f06febc9SFrank Mayhar  */
34724cd4c1b4SPeter Zijlstra void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
34734da94d49SPeter Zijlstra void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
3474f06febc9SFrank Mayhar 
3475f06febc9SFrank Mayhar /*
34767bb44adeSRoland McGrath  * Reevaluate whether the task has signals pending delivery.
34777bb44adeSRoland McGrath  * Wake the task if so.
34787bb44adeSRoland McGrath  * This is required every time the blocked sigset_t changes.
34797bb44adeSRoland McGrath  * callers must hold sighand->siglock.
34807bb44adeSRoland McGrath  */
34817bb44adeSRoland McGrath extern void recalc_sigpending_and_wake(struct task_struct *t);
34821da177e4SLinus Torvalds extern void recalc_sigpending(void);
34831da177e4SLinus Torvalds 
3484910ffdb1SOleg Nesterov extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
3485910ffdb1SOleg Nesterov 
3486910ffdb1SOleg Nesterov static inline void signal_wake_up(struct task_struct *t, bool resume)
3487910ffdb1SOleg Nesterov {
3488910ffdb1SOleg Nesterov 	signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
3489910ffdb1SOleg Nesterov }
3490910ffdb1SOleg Nesterov static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
3491910ffdb1SOleg Nesterov {
3492910ffdb1SOleg Nesterov 	signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
3493910ffdb1SOleg Nesterov }
34941da177e4SLinus Torvalds 
34951da177e4SLinus Torvalds /*
34961da177e4SLinus Torvalds  * Wrappers for p->thread_info->cpu access. No-op on UP.
34971da177e4SLinus Torvalds  */
34981da177e4SLinus Torvalds #ifdef CONFIG_SMP
34991da177e4SLinus Torvalds 
35001da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
35011da177e4SLinus Torvalds {
3502c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
3503c65eacbeSAndy Lutomirski 	return p->cpu;
3504c65eacbeSAndy Lutomirski #else
3505a1261f54SAl Viro 	return task_thread_info(p)->cpu;
3506c65eacbeSAndy Lutomirski #endif
35071da177e4SLinus Torvalds }
35081da177e4SLinus Torvalds 
3509b32e86b4SIngo Molnar static inline int task_node(const struct task_struct *p)
3510b32e86b4SIngo Molnar {
3511b32e86b4SIngo Molnar 	return cpu_to_node(task_cpu(p));
3512b32e86b4SIngo Molnar }
3513b32e86b4SIngo Molnar 
3514c65cc870SIngo Molnar extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds #else
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
35191da177e4SLinus Torvalds {
35201da177e4SLinus Torvalds 	return 0;
35211da177e4SLinus Torvalds }
35221da177e4SLinus Torvalds 
35231da177e4SLinus Torvalds static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
35241da177e4SLinus Torvalds {
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds #endif /* CONFIG_SMP */
35281da177e4SLinus Torvalds 
352996f874e2SRusty Russell extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
353096f874e2SRusty Russell extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
35315c45bf27SSiddha, Suresh B 
35327c941438SDhaval Giani #ifdef CONFIG_CGROUP_SCHED
353307e06b01SYong Zhang extern struct task_group root_task_group;
35348323f26cSPeter Zijlstra #endif /* CONFIG_CGROUP_SCHED */
35359b5b7751SSrivatsa Vaddagiri 
353654e99124SDhaval Giani extern int task_can_switch_user(struct user_struct *up,
353754e99124SDhaval Giani 					struct task_struct *tsk);
353854e99124SDhaval Giani 
35394b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
35404b98d11bSAlexey Dobriyan static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
35414b98d11bSAlexey Dobriyan {
3542940389b8SAndrea Righi 	tsk->ioac.rchar += amt;
35434b98d11bSAlexey Dobriyan }
35444b98d11bSAlexey Dobriyan 
35454b98d11bSAlexey Dobriyan static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
35464b98d11bSAlexey Dobriyan {
3547940389b8SAndrea Righi 	tsk->ioac.wchar += amt;
35484b98d11bSAlexey Dobriyan }
35494b98d11bSAlexey Dobriyan 
35504b98d11bSAlexey Dobriyan static inline void inc_syscr(struct task_struct *tsk)
35514b98d11bSAlexey Dobriyan {
3552940389b8SAndrea Righi 	tsk->ioac.syscr++;
35534b98d11bSAlexey Dobriyan }
35544b98d11bSAlexey Dobriyan 
35554b98d11bSAlexey Dobriyan static inline void inc_syscw(struct task_struct *tsk)
35564b98d11bSAlexey Dobriyan {
3557940389b8SAndrea Righi 	tsk->ioac.syscw++;
35584b98d11bSAlexey Dobriyan }
35594b98d11bSAlexey Dobriyan #else
35604b98d11bSAlexey Dobriyan static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
35614b98d11bSAlexey Dobriyan {
35624b98d11bSAlexey Dobriyan }
35634b98d11bSAlexey Dobriyan 
35644b98d11bSAlexey Dobriyan static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
35654b98d11bSAlexey Dobriyan {
35664b98d11bSAlexey Dobriyan }
35674b98d11bSAlexey Dobriyan 
35684b98d11bSAlexey Dobriyan static inline void inc_syscr(struct task_struct *tsk)
35694b98d11bSAlexey Dobriyan {
35704b98d11bSAlexey Dobriyan }
35714b98d11bSAlexey Dobriyan 
35724b98d11bSAlexey Dobriyan static inline void inc_syscw(struct task_struct *tsk)
35734b98d11bSAlexey Dobriyan {
35744b98d11bSAlexey Dobriyan }
35754b98d11bSAlexey Dobriyan #endif
35764b98d11bSAlexey Dobriyan 
357782455257SDave Hansen #ifndef TASK_SIZE_OF
357882455257SDave Hansen #define TASK_SIZE_OF(tsk)	TASK_SIZE
357982455257SDave Hansen #endif
358082455257SDave Hansen 
3581f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG
3582cf475ad2SBalbir Singh extern void mm_update_next_owner(struct mm_struct *mm);
3583cf475ad2SBalbir Singh #else
3584cf475ad2SBalbir Singh static inline void mm_update_next_owner(struct mm_struct *mm)
3585cf475ad2SBalbir Singh {
3586cf475ad2SBalbir Singh }
3587f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */
3588cf475ad2SBalbir Singh 
35893e10e716SJiri Slaby static inline unsigned long task_rlimit(const struct task_struct *tsk,
35903e10e716SJiri Slaby 		unsigned int limit)
35913e10e716SJiri Slaby {
3592316c1608SJason Low 	return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
35933e10e716SJiri Slaby }
35943e10e716SJiri Slaby 
35953e10e716SJiri Slaby static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
35963e10e716SJiri Slaby 		unsigned int limit)
35973e10e716SJiri Slaby {
3598316c1608SJason Low 	return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
35993e10e716SJiri Slaby }
36003e10e716SJiri Slaby 
36013e10e716SJiri Slaby static inline unsigned long rlimit(unsigned int limit)
36023e10e716SJiri Slaby {
36033e10e716SJiri Slaby 	return task_rlimit(current, limit);
36043e10e716SJiri Slaby }
36053e10e716SJiri Slaby 
36063e10e716SJiri Slaby static inline unsigned long rlimit_max(unsigned int limit)
36073e10e716SJiri Slaby {
36083e10e716SJiri Slaby 	return task_rlimit_max(current, limit);
36093e10e716SJiri Slaby }
36103e10e716SJiri Slaby 
361158919e83SRafael J. Wysocki #define SCHED_CPUFREQ_RT	(1U << 0)
361258919e83SRafael J. Wysocki #define SCHED_CPUFREQ_DL	(1U << 1)
36138c34ab19SRafael J. Wysocki #define SCHED_CPUFREQ_IOWAIT	(1U << 2)
361458919e83SRafael J. Wysocki 
361558919e83SRafael J. Wysocki #define SCHED_CPUFREQ_RT_DL	(SCHED_CPUFREQ_RT | SCHED_CPUFREQ_DL)
361658919e83SRafael J. Wysocki 
3617adaf9fcdSRafael J. Wysocki #ifdef CONFIG_CPU_FREQ
3618adaf9fcdSRafael J. Wysocki struct update_util_data {
361958919e83SRafael J. Wysocki        void (*func)(struct update_util_data *data, u64 time, unsigned int flags);
3620adaf9fcdSRafael J. Wysocki };
3621adaf9fcdSRafael J. Wysocki 
36220bed612bSRafael J. Wysocki void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
36230bed612bSRafael J. Wysocki                        void (*func)(struct update_util_data *data, u64 time,
362458919e83SRafael J. Wysocki 				    unsigned int flags));
36250bed612bSRafael J. Wysocki void cpufreq_remove_update_util_hook(int cpu);
3626adaf9fcdSRafael J. Wysocki #endif /* CONFIG_CPU_FREQ */
3627adaf9fcdSRafael J. Wysocki 
36281da177e4SLinus Torvalds #endif
3629