xref: /linux/include/linux/sched.h (revision 16a6d9be90373fb0b521850cd0185a4d460dd152)
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 
281a2250238SPeter Zijlstra /*
282a2250238SPeter Zijlstra  * @tsk had better be current, or you get to keep the pieces.
283a2250238SPeter Zijlstra  *
284a2250238SPeter Zijlstra  * The only reason is that computing current can be more expensive than
285a2250238SPeter Zijlstra  * using a pointer that's already available.
286a2250238SPeter Zijlstra  *
287a2250238SPeter Zijlstra  * Therefore, see set_current_state().
288a2250238SPeter 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  *
299a2250238SPeter Zijlstra  *   for (;;) {
300498d0c57SAndrew Morton  *	set_current_state(TASK_UNINTERRUPTIBLE);
301a2250238SPeter Zijlstra  *	if (!need_sleep)
302a2250238SPeter Zijlstra  *		break;
303498d0c57SAndrew Morton  *
304a2250238SPeter Zijlstra  *	schedule();
305a2250238SPeter Zijlstra  *   }
306a2250238SPeter Zijlstra  *   __set_current_state(TASK_RUNNING);
307a2250238SPeter Zijlstra  *
308a2250238SPeter Zijlstra  * If the caller does not need such serialisation (because, for instance, the
309a2250238SPeter Zijlstra  * condition test and condition change and wakeup are under the same lock) then
310a2250238SPeter Zijlstra  * use __set_current_state().
311a2250238SPeter Zijlstra  *
312a2250238SPeter Zijlstra  * The above is typically ordered against the wakeup, which does:
313a2250238SPeter Zijlstra  *
314a2250238SPeter Zijlstra  *	need_sleep = false;
315a2250238SPeter Zijlstra  *	wake_up_state(p, TASK_UNINTERRUPTIBLE);
316a2250238SPeter Zijlstra  *
317a2250238SPeter Zijlstra  * Where wake_up_state() (and all other wakeup primitives) imply enough
318a2250238SPeter Zijlstra  * barriers to order the store of the variable against wakeup.
319a2250238SPeter Zijlstra  *
320a2250238SPeter Zijlstra  * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
321a2250238SPeter Zijlstra  * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
322a2250238SPeter Zijlstra  * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
323a2250238SPeter Zijlstra  *
324a2250238SPeter Zijlstra  * This is obviously fine, since they both store the exact same value.
325a2250238SPeter Zijlstra  *
326a2250238SPeter 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 
46410ab5643STejun Heo extern int __must_check io_schedule_prepare(void);
46510ab5643STejun Heo extern void io_schedule_finish(int token);
4669cff8adeSNeilBrown extern long io_schedule_timeout(long timeout);
46710ab5643STejun Heo extern void io_schedule(void);
4689cff8adeSNeilBrown 
4699af6528eSPeter Zijlstra void __noreturn do_task_dead(void);
4709af6528eSPeter Zijlstra 
471ab516013SSerge E. Hallyn struct nsproxy;
472acce292cSCedric Le Goater struct user_namespace;
4731da177e4SLinus Torvalds 
474efc1a3b1SDavid Howells #ifdef CONFIG_MMU
475efc1a3b1SDavid Howells extern void arch_pick_mmap_layout(struct mm_struct *mm);
4761da177e4SLinus Torvalds extern unsigned long
4771da177e4SLinus Torvalds arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
4781da177e4SLinus Torvalds 		       unsigned long, unsigned long);
4791da177e4SLinus Torvalds extern unsigned long
4801da177e4SLinus Torvalds arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
4811da177e4SLinus Torvalds 			  unsigned long len, unsigned long pgoff,
4821da177e4SLinus Torvalds 			  unsigned long flags);
483efc1a3b1SDavid Howells #else
484efc1a3b1SDavid Howells static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
485efc1a3b1SDavid Howells #endif
4861da177e4SLinus Torvalds 
487d049f74fSKees Cook #define SUID_DUMP_DISABLE	0	/* No setuid dumping */
488d049f74fSKees Cook #define SUID_DUMP_USER		1	/* Dump as user of process */
489d049f74fSKees Cook #define SUID_DUMP_ROOT		2	/* Dump as root */
490d049f74fSKees Cook 
4916c5d5238SKawai, Hidehiro /* mm flags */
492f8af4da3SHugh Dickins 
4937288e118SOleg Nesterov /* for SUID_DUMP_* above */
4943cb4a0bbSKawai, Hidehiro #define MMF_DUMPABLE_BITS 2
495f8af4da3SHugh Dickins #define MMF_DUMPABLE_MASK ((1 << MMF_DUMPABLE_BITS) - 1)
4963cb4a0bbSKawai, Hidehiro 
497942be387SOleg Nesterov extern void set_dumpable(struct mm_struct *mm, int value);
498942be387SOleg Nesterov /*
499942be387SOleg Nesterov  * This returns the actual value of the suid_dumpable flag. For things
500942be387SOleg Nesterov  * that are using this for checking for privilege transitions, it must
501942be387SOleg Nesterov  * test against SUID_DUMP_USER rather than treating it as a boolean
502942be387SOleg Nesterov  * value.
503942be387SOleg Nesterov  */
504942be387SOleg Nesterov static inline int __get_dumpable(unsigned long mm_flags)
505942be387SOleg Nesterov {
506942be387SOleg Nesterov 	return mm_flags & MMF_DUMPABLE_MASK;
507942be387SOleg Nesterov }
508942be387SOleg Nesterov 
509942be387SOleg Nesterov static inline int get_dumpable(struct mm_struct *mm)
510942be387SOleg Nesterov {
511942be387SOleg Nesterov 	return __get_dumpable(mm->flags);
512942be387SOleg Nesterov }
513942be387SOleg Nesterov 
5143cb4a0bbSKawai, Hidehiro /* coredump filter bits */
5153cb4a0bbSKawai, Hidehiro #define MMF_DUMP_ANON_PRIVATE	2
5163cb4a0bbSKawai, Hidehiro #define MMF_DUMP_ANON_SHARED	3
5173cb4a0bbSKawai, Hidehiro #define MMF_DUMP_MAPPED_PRIVATE	4
5183cb4a0bbSKawai, Hidehiro #define MMF_DUMP_MAPPED_SHARED	5
51982df3973SRoland McGrath #define MMF_DUMP_ELF_HEADERS	6
520e575f111SKOSAKI Motohiro #define MMF_DUMP_HUGETLB_PRIVATE 7
521e575f111SKOSAKI Motohiro #define MMF_DUMP_HUGETLB_SHARED  8
5225037835cSRoss Zwisler #define MMF_DUMP_DAX_PRIVATE	9
5235037835cSRoss Zwisler #define MMF_DUMP_DAX_SHARED	10
524f8af4da3SHugh Dickins 
5253cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_SHIFT	MMF_DUMPABLE_BITS
5265037835cSRoss Zwisler #define MMF_DUMP_FILTER_BITS	9
5273cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_MASK \
5283cb4a0bbSKawai, Hidehiro 	(((1 << MMF_DUMP_FILTER_BITS) - 1) << MMF_DUMP_FILTER_SHIFT)
5293cb4a0bbSKawai, Hidehiro #define MMF_DUMP_FILTER_DEFAULT \
530e575f111SKOSAKI Motohiro 	((1 << MMF_DUMP_ANON_PRIVATE) |	(1 << MMF_DUMP_ANON_SHARED) |\
531656eb2cdSRoland McGrath 	 (1 << MMF_DUMP_HUGETLB_PRIVATE) | MMF_DUMP_MASK_DEFAULT_ELF)
532656eb2cdSRoland McGrath 
533656eb2cdSRoland McGrath #ifdef CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS
534656eb2cdSRoland McGrath # define MMF_DUMP_MASK_DEFAULT_ELF	(1 << MMF_DUMP_ELF_HEADERS)
535656eb2cdSRoland McGrath #else
536656eb2cdSRoland McGrath # define MMF_DUMP_MASK_DEFAULT_ELF	0
537656eb2cdSRoland McGrath #endif
538f8af4da3SHugh Dickins 					/* leave room for more dump flags */
539f8af4da3SHugh Dickins #define MMF_VM_MERGEABLE	16	/* KSM may merge identical pages */
540ba76149fSAndrea Arcangeli #define MMF_VM_HUGEPAGE		17	/* set when VM_HUGEPAGE is set on vma */
5413fb4afd9SStanislav Kinsburskiy /*
5423fb4afd9SStanislav Kinsburskiy  * This one-shot flag is dropped due to necessity of changing exe once again
5433fb4afd9SStanislav Kinsburskiy  * on NFS restore
5443fb4afd9SStanislav Kinsburskiy  */
5453fb4afd9SStanislav Kinsburskiy //#define MMF_EXE_FILE_CHANGED	18	/* see prctl_set_mm_exe_file() */
546f8af4da3SHugh Dickins 
5479f68f672SOleg Nesterov #define MMF_HAS_UPROBES		19	/* has uprobes */
5489f68f672SOleg Nesterov #define MMF_RECALC_UPROBES	20	/* MMF_HAS_UPROBES can be wrong */
549862e3073SMichal Hocko #define MMF_OOM_SKIP		21	/* mm is of no interest for the OOM killer */
5503f70dc38SMichal Hocko #define MMF_UNSTABLE		22	/* mm is unstable for copy_from_user */
5516fcb52a5SAaron Lu #define MMF_HUGE_ZERO_PAGE	23      /* mm has ever used the global huge zero page */
552f8ac4ec9SOleg Nesterov 
553f8af4da3SHugh Dickins #define MMF_INIT_MASK		(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK)
5546c5d5238SKawai, Hidehiro 
5551da177e4SLinus Torvalds struct sighand_struct {
5561da177e4SLinus Torvalds 	atomic_t		count;
5571da177e4SLinus Torvalds 	struct k_sigaction	action[_NSIG];
5581da177e4SLinus Torvalds 	spinlock_t		siglock;
559b8fceee1SDavide Libenzi 	wait_queue_head_t	signalfd_wqh;
5601da177e4SLinus Torvalds };
5611da177e4SLinus Torvalds 
5620e464814SKaiGai Kohei struct pacct_struct {
563f6ec29a4SKaiGai Kohei 	int			ac_flag;
564f6ec29a4SKaiGai Kohei 	long			ac_exitcode;
5650e464814SKaiGai Kohei 	unsigned long		ac_mem;
56677787bfbSKaiGai Kohei 	cputime_t		ac_utime, ac_stime;
56777787bfbSKaiGai Kohei 	unsigned long		ac_minflt, ac_majflt;
5680e464814SKaiGai Kohei };
5690e464814SKaiGai Kohei 
57042c4ab41SStanislaw Gruszka struct cpu_itimer {
57142c4ab41SStanislaw Gruszka 	cputime_t expires;
57242c4ab41SStanislaw Gruszka 	cputime_t incr;
5738356b5f9SStanislaw Gruszka 	u32 error;
5748356b5f9SStanislaw Gruszka 	u32 incr_error;
57542c4ab41SStanislaw Gruszka };
57642c4ab41SStanislaw Gruszka 
577f06febc9SFrank Mayhar /**
5789d7fb042SPeter Zijlstra  * struct prev_cputime - snaphsot of system and user cputime
579d37f761dSFrederic Weisbecker  * @utime: time spent in user mode
580d37f761dSFrederic Weisbecker  * @stime: time spent in system mode
5819d7fb042SPeter Zijlstra  * @lock: protects the above two fields
582d37f761dSFrederic Weisbecker  *
5839d7fb042SPeter Zijlstra  * Stores previous user/system time values such that we can guarantee
5849d7fb042SPeter Zijlstra  * monotonicity.
585d37f761dSFrederic Weisbecker  */
5869d7fb042SPeter Zijlstra struct prev_cputime {
5879d7fb042SPeter Zijlstra #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
588d37f761dSFrederic Weisbecker 	cputime_t utime;
589d37f761dSFrederic Weisbecker 	cputime_t stime;
5909d7fb042SPeter Zijlstra 	raw_spinlock_t lock;
5919d7fb042SPeter Zijlstra #endif
592d37f761dSFrederic Weisbecker };
593d37f761dSFrederic Weisbecker 
5949d7fb042SPeter Zijlstra static inline void prev_cputime_init(struct prev_cputime *prev)
5959d7fb042SPeter Zijlstra {
5969d7fb042SPeter Zijlstra #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
5979d7fb042SPeter Zijlstra 	prev->utime = prev->stime = 0;
5989d7fb042SPeter Zijlstra 	raw_spin_lock_init(&prev->lock);
5999d7fb042SPeter Zijlstra #endif
6009d7fb042SPeter Zijlstra }
6019d7fb042SPeter Zijlstra 
602d37f761dSFrederic Weisbecker /**
603f06febc9SFrank Mayhar  * struct task_cputime - collected CPU time counts
604f06febc9SFrank Mayhar  * @utime:		time spent in user mode, in &cputime_t units
605f06febc9SFrank Mayhar  * @stime:		time spent in kernel mode, in &cputime_t units
606f06febc9SFrank Mayhar  * @sum_exec_runtime:	total time spent on the CPU, in nanoseconds
607f06febc9SFrank Mayhar  *
6089d7fb042SPeter Zijlstra  * This structure groups together three kinds of CPU time that are tracked for
6099d7fb042SPeter Zijlstra  * threads and thread groups.  Most things considering CPU time want to group
6109d7fb042SPeter Zijlstra  * these counts together and treat all three of them in parallel.
611f06febc9SFrank Mayhar  */
612f06febc9SFrank Mayhar struct task_cputime {
613f06febc9SFrank Mayhar 	cputime_t utime;
614f06febc9SFrank Mayhar 	cputime_t stime;
615f06febc9SFrank Mayhar 	unsigned long long sum_exec_runtime;
616f06febc9SFrank Mayhar };
6179d7fb042SPeter Zijlstra 
618f06febc9SFrank Mayhar /* Alternate field names when used to cache expirations. */
619f06febc9SFrank Mayhar #define virt_exp	utime
6209d7fb042SPeter Zijlstra #define prof_exp	stime
621f06febc9SFrank Mayhar #define sched_exp	sum_exec_runtime
622f06febc9SFrank Mayhar 
623971e8a98SJason Low /*
624971e8a98SJason Low  * This is the atomic variant of task_cputime, which can be used for
625971e8a98SJason Low  * storing and updating task_cputime statistics without locking.
626971e8a98SJason Low  */
627971e8a98SJason Low struct task_cputime_atomic {
628971e8a98SJason Low 	atomic64_t utime;
629971e8a98SJason Low 	atomic64_t stime;
630971e8a98SJason Low 	atomic64_t sum_exec_runtime;
631971e8a98SJason Low };
632971e8a98SJason Low 
633971e8a98SJason Low #define INIT_CPUTIME_ATOMIC \
634971e8a98SJason Low 	(struct task_cputime_atomic) {				\
635971e8a98SJason Low 		.utime = ATOMIC64_INIT(0),			\
636971e8a98SJason Low 		.stime = ATOMIC64_INIT(0),			\
637971e8a98SJason Low 		.sum_exec_runtime = ATOMIC64_INIT(0),		\
638971e8a98SJason Low 	}
639971e8a98SJason Low 
640609ca066SPeter Zijlstra #define PREEMPT_DISABLED	(PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
641a233f112SPeter Zijlstra 
642c99e6efeSPeter Zijlstra /*
64387dcbc06SPeter Zijlstra  * Disable preemption until the scheduler is running -- use an unconditional
64487dcbc06SPeter Zijlstra  * value so that it also works on !PREEMPT_COUNT kernels.
645d86ee480SPeter Zijlstra  *
64687dcbc06SPeter Zijlstra  * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
647c99e6efeSPeter Zijlstra  */
64887dcbc06SPeter Zijlstra #define INIT_PREEMPT_COUNT	PREEMPT_OFFSET
649c99e6efeSPeter Zijlstra 
650609ca066SPeter Zijlstra /*
651609ca066SPeter Zijlstra  * Initial preempt_count value; reflects the preempt_count schedule invariant
652609ca066SPeter Zijlstra  * which states that during context switches:
653609ca066SPeter Zijlstra  *
654609ca066SPeter Zijlstra  *    preempt_count() == 2*PREEMPT_DISABLE_OFFSET
655609ca066SPeter Zijlstra  *
656609ca066SPeter Zijlstra  * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
657609ca066SPeter Zijlstra  * Note: See finish_task_switch().
658609ca066SPeter Zijlstra  */
659609ca066SPeter Zijlstra #define FORK_PREEMPT_COUNT	(2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
6604cd4c1b4SPeter Zijlstra 
661f06febc9SFrank Mayhar /**
662f06febc9SFrank Mayhar  * struct thread_group_cputimer - thread group interval timer counts
663920ce39fSJason Low  * @cputime_atomic:	atomic thread group interval timers.
664d5c373ebSJason Low  * @running:		true when there are timers running and
665d5c373ebSJason Low  *			@cputime_atomic receives updates.
666c8d75aa4SJason Low  * @checking_timer:	true when a thread in the group is in the
667c8d75aa4SJason Low  *			process of checking for thread group timers.
668f06febc9SFrank Mayhar  *
669f06febc9SFrank Mayhar  * This structure contains the version of task_cputime, above, that is
6704cd4c1b4SPeter Zijlstra  * used for thread group CPU timer calculations.
671f06febc9SFrank Mayhar  */
6724cd4c1b4SPeter Zijlstra struct thread_group_cputimer {
67371107445SJason Low 	struct task_cputime_atomic cputime_atomic;
674d5c373ebSJason Low 	bool running;
675c8d75aa4SJason Low 	bool checking_timer;
676f06febc9SFrank Mayhar };
677f06febc9SFrank Mayhar 
6784714d1d3SBen Blum #include <linux/rwsem.h>
6795091faa4SMike Galbraith struct autogroup;
6805091faa4SMike Galbraith 
6811da177e4SLinus Torvalds /*
682e815f0a8SJonathan Neuschäfer  * NOTE! "signal_struct" does not have its own
6831da177e4SLinus Torvalds  * locking, because a shared signal_struct always
6841da177e4SLinus Torvalds  * implies a shared sighand_struct, so locking
6851da177e4SLinus Torvalds  * sighand_struct is always a proper superset of
6861da177e4SLinus Torvalds  * the locking of signal_struct.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds struct signal_struct {
689ea6d290cSOleg Nesterov 	atomic_t		sigcnt;
6901da177e4SLinus Torvalds 	atomic_t		live;
691b3ac022cSOleg Nesterov 	int			nr_threads;
6920c740d0aSOleg Nesterov 	struct list_head	thread_head;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	wait_queue_head_t	wait_chldexit;	/* for wait4() */
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/* current thread group signal load-balancing target: */
69736c8b586SIngo Molnar 	struct task_struct	*curr_target;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	/* shared signal handling: */
7001da177e4SLinus Torvalds 	struct sigpending	shared_pending;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/* thread group exit support */
7031da177e4SLinus Torvalds 	int			group_exit_code;
7041da177e4SLinus Torvalds 	/* overloaded:
7051da177e4SLinus Torvalds 	 * - notify group_exit_task when ->count is equal to notify_count
7061da177e4SLinus Torvalds 	 * - everyone except group_exit_task is stopped during signal delivery
7071da177e4SLinus Torvalds 	 *   of fatal signals, group_exit_task processes the signal.
7081da177e4SLinus Torvalds 	 */
7091da177e4SLinus Torvalds 	int			notify_count;
71007dd20e0SRichard Kennedy 	struct task_struct	*group_exit_task;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* thread group stop support, overloads group_exit_code too */
7131da177e4SLinus Torvalds 	int			group_stop_count;
7141da177e4SLinus Torvalds 	unsigned int		flags; /* see SIGNAL_* flags below */
7151da177e4SLinus Torvalds 
716ebec18a6SLennart Poettering 	/*
717ebec18a6SLennart Poettering 	 * PR_SET_CHILD_SUBREAPER marks a process, like a service
718ebec18a6SLennart Poettering 	 * manager, to re-parent orphan (double-forking) child processes
719ebec18a6SLennart Poettering 	 * to this process instead of 'init'. The service manager is
720ebec18a6SLennart Poettering 	 * able to receive SIGCHLD signals and is able to investigate
721ebec18a6SLennart Poettering 	 * the process until it calls wait(). All children of this
722ebec18a6SLennart Poettering 	 * process will inherit a flag if they should look for a
723ebec18a6SLennart Poettering 	 * child_subreaper process at exit.
724ebec18a6SLennart Poettering 	 */
725ebec18a6SLennart Poettering 	unsigned int		is_child_subreaper:1;
726ebec18a6SLennart Poettering 	unsigned int		has_child_subreaper:1;
727ebec18a6SLennart Poettering 
7281da177e4SLinus Torvalds 	/* POSIX.1b Interval Timers */
7295ed67f05SPavel Emelyanov 	int			posix_timer_id;
7301da177e4SLinus Torvalds 	struct list_head	posix_timers;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	/* ITIMER_REAL timer for the process */
7332ff678b8SThomas Gleixner 	struct hrtimer real_timer;
734fea9d175SOleg Nesterov 	struct pid *leader_pid;
7352ff678b8SThomas Gleixner 	ktime_t it_real_incr;
7361da177e4SLinus Torvalds 
73742c4ab41SStanislaw Gruszka 	/*
73842c4ab41SStanislaw Gruszka 	 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
73942c4ab41SStanislaw Gruszka 	 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
74042c4ab41SStanislaw Gruszka 	 * values are defined to 0 and 1 respectively
74142c4ab41SStanislaw Gruszka 	 */
74242c4ab41SStanislaw Gruszka 	struct cpu_itimer it[2];
7431da177e4SLinus Torvalds 
744f06febc9SFrank Mayhar 	/*
7454cd4c1b4SPeter Zijlstra 	 * Thread group totals for process CPU timers.
7464cd4c1b4SPeter Zijlstra 	 * See thread_group_cputimer(), et al, for details.
747f06febc9SFrank Mayhar 	 */
7484cd4c1b4SPeter Zijlstra 	struct thread_group_cputimer cputimer;
749f06febc9SFrank Mayhar 
750f06febc9SFrank Mayhar 	/* Earliest-expiration cache. */
751f06febc9SFrank Mayhar 	struct task_cputime cputime_expires;
752f06febc9SFrank Mayhar 
753d027d45dSFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
754f009a7a7SFrederic Weisbecker 	atomic_t tick_dep_mask;
755d027d45dSFrederic Weisbecker #endif
756d027d45dSFrederic Weisbecker 
757f06febc9SFrank Mayhar 	struct list_head cpu_timers[3];
758f06febc9SFrank Mayhar 
759ab521dc0SEric W. Biederman 	struct pid *tty_old_pgrp;
7601ec320afSCedric Le Goater 
7611da177e4SLinus Torvalds 	/* boolean value for session group leader */
7621da177e4SLinus Torvalds 	int leader;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	struct tty_struct *tty; /* NULL if no tty */
7651da177e4SLinus Torvalds 
7665091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
7675091faa4SMike Galbraith 	struct autogroup *autogroup;
7685091faa4SMike Galbraith #endif
7691da177e4SLinus Torvalds 	/*
7701da177e4SLinus Torvalds 	 * Cumulative resource counters for dead threads in the group,
7711da177e4SLinus Torvalds 	 * and for reaped dead child processes forked by this group.
7721da177e4SLinus Torvalds 	 * Live threads maintain their own counters and add to these
7731da177e4SLinus Torvalds 	 * in __exit_signal, except for the group leader.
7741da177e4SLinus Torvalds 	 */
775e78c3496SRik van Riel 	seqlock_t stats_lock;
77632bd671dSPeter Zijlstra 	cputime_t utime, stime, cutime, cstime;
777*16a6d9beSFrederic Weisbecker 	u64 gtime;
778*16a6d9beSFrederic Weisbecker 	u64 cgtime;
7799d7fb042SPeter Zijlstra 	struct prev_cputime prev_cputime;
7801da177e4SLinus Torvalds 	unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
7811da177e4SLinus Torvalds 	unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
7826eaeeabaSEric Dumazet 	unsigned long inblock, oublock, cinblock, coublock;
7831f10206cSJiri Pirko 	unsigned long maxrss, cmaxrss;
784940389b8SAndrea Righi 	struct task_io_accounting ioac;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/*
78732bd671dSPeter Zijlstra 	 * Cumulative ns of schedule CPU time fo dead threads in the
78832bd671dSPeter Zijlstra 	 * group, not including a zombie group leader, (This only differs
78932bd671dSPeter Zijlstra 	 * from jiffies_to_ns(utime + stime) if sched_clock uses something
79032bd671dSPeter Zijlstra 	 * other than jiffies.)
79132bd671dSPeter Zijlstra 	 */
79232bd671dSPeter Zijlstra 	unsigned long long sum_sched_runtime;
79332bd671dSPeter Zijlstra 
79432bd671dSPeter Zijlstra 	/*
7951da177e4SLinus Torvalds 	 * We don't bother to synchronize most readers of this at all,
7961da177e4SLinus Torvalds 	 * because there is no reader checking a limit that actually needs
7971da177e4SLinus Torvalds 	 * to get both rlim_cur and rlim_max atomically, and either one
7981da177e4SLinus Torvalds 	 * alone is a single word that can safely be read normally.
7991da177e4SLinus Torvalds 	 * getrlimit/setrlimit use task_lock(current->group_leader) to
8001da177e4SLinus Torvalds 	 * protect this instead of the siglock, because they really
8011da177e4SLinus Torvalds 	 * have no need to disable irqs.
8021da177e4SLinus Torvalds 	 */
8031da177e4SLinus Torvalds 	struct rlimit rlim[RLIM_NLIMITS];
8041da177e4SLinus Torvalds 
8050e464814SKaiGai Kohei #ifdef CONFIG_BSD_PROCESS_ACCT
8060e464814SKaiGai Kohei 	struct pacct_struct pacct;	/* per-process accounting information */
8070e464814SKaiGai Kohei #endif
808ad4ecbcbSShailabh Nagar #ifdef CONFIG_TASKSTATS
809ad4ecbcbSShailabh Nagar 	struct taskstats *stats;
810ad4ecbcbSShailabh Nagar #endif
811522ed776SMiloslav Trmac #ifdef CONFIG_AUDIT
812522ed776SMiloslav Trmac 	unsigned audit_tty;
813522ed776SMiloslav Trmac 	struct tty_audit_buf *tty_audit_buf;
814522ed776SMiloslav Trmac #endif
81528b83c51SKOSAKI Motohiro 
816c96fc2d8STetsuo Handa 	/*
817c96fc2d8STetsuo Handa 	 * Thread is the potential origin of an oom condition; kill first on
818c96fc2d8STetsuo Handa 	 * oom
819c96fc2d8STetsuo Handa 	 */
820c96fc2d8STetsuo Handa 	bool oom_flag_origin;
821a9c58b90SDavid Rientjes 	short oom_score_adj;		/* OOM kill score adjustment */
822a9c58b90SDavid Rientjes 	short oom_score_adj_min;	/* OOM kill score adjustment min value.
823dabb16f6SMandeep Singh Baines 					 * Only settable by CAP_SYS_RESOURCE. */
82426db62f1SMichal Hocko 	struct mm_struct *oom_mm;	/* recorded mm when the thread group got
82526db62f1SMichal Hocko 					 * killed by the oom killer */
8269b1bf12dSKOSAKI Motohiro 
8279b1bf12dSKOSAKI Motohiro 	struct mutex cred_guard_mutex;	/* guard against foreign influences on
8289b1bf12dSKOSAKI Motohiro 					 * credential calculations
8299b1bf12dSKOSAKI Motohiro 					 * (notably. ptrace) */
8301da177e4SLinus Torvalds };
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds /*
8331da177e4SLinus Torvalds  * Bits in flags field of signal_struct.
8341da177e4SLinus Torvalds  */
8351da177e4SLinus Torvalds #define SIGNAL_STOP_STOPPED	0x00000001 /* job control stop in effect */
836ee77f075SOleg Nesterov #define SIGNAL_STOP_CONTINUED	0x00000002 /* SIGCONT since WCONTINUED reap */
837ee77f075SOleg Nesterov #define SIGNAL_GROUP_EXIT	0x00000004 /* group exit in progress */
838403bad72SOleg Nesterov #define SIGNAL_GROUP_COREDUMP	0x00000008 /* coredump in progress */
839e4420551SOleg Nesterov /*
840e4420551SOleg Nesterov  * Pending notifications to parent.
841e4420551SOleg Nesterov  */
842e4420551SOleg Nesterov #define SIGNAL_CLD_STOPPED	0x00000010
843e4420551SOleg Nesterov #define SIGNAL_CLD_CONTINUED	0x00000020
844e4420551SOleg Nesterov #define SIGNAL_CLD_MASK		(SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
8451da177e4SLinus Torvalds 
846fae5fa44SOleg Nesterov #define SIGNAL_UNKILLABLE	0x00000040 /* for init: ignore fatal signals */
847fae5fa44SOleg Nesterov 
8482d39b3cdSJamie Iles #define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
8492d39b3cdSJamie Iles 			  SIGNAL_STOP_CONTINUED)
8502d39b3cdSJamie Iles 
8512d39b3cdSJamie Iles static inline void signal_set_stop_flags(struct signal_struct *sig,
8522d39b3cdSJamie Iles 					 unsigned int flags)
8532d39b3cdSJamie Iles {
8542d39b3cdSJamie Iles 	WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
8552d39b3cdSJamie Iles 	sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
8562d39b3cdSJamie Iles }
8572d39b3cdSJamie Iles 
858ed5d2cacSOleg Nesterov /* If true, all threads except ->group_exit_task have pending SIGKILL */
859ed5d2cacSOleg Nesterov static inline int signal_group_exit(const struct signal_struct *sig)
860ed5d2cacSOleg Nesterov {
861ed5d2cacSOleg Nesterov 	return	(sig->flags & SIGNAL_GROUP_EXIT) ||
862ed5d2cacSOleg Nesterov 		(sig->group_exit_task != NULL);
863ed5d2cacSOleg Nesterov }
864ed5d2cacSOleg Nesterov 
8651da177e4SLinus Torvalds /*
8661da177e4SLinus Torvalds  * Some day this will be a full-fledged user tracking system..
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds struct user_struct {
8691da177e4SLinus Torvalds 	atomic_t __count;	/* reference count */
8701da177e4SLinus Torvalds 	atomic_t processes;	/* How many processes does this user have? */
8711da177e4SLinus Torvalds 	atomic_t sigpending;	/* How many pending signals does this user have? */
8722d9048e2SAmy Griffis #ifdef CONFIG_INOTIFY_USER
8730eeca283SRobert Love 	atomic_t inotify_watches; /* How many inotify watches does this user have? */
8740eeca283SRobert Love 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
8750eeca283SRobert Love #endif
8764afeff85SEric Paris #ifdef CONFIG_FANOTIFY
8774afeff85SEric Paris 	atomic_t fanotify_listeners;
8784afeff85SEric Paris #endif
8797ef9964eSDavide Libenzi #ifdef CONFIG_EPOLL
88052bd19f7SRobin Holt 	atomic_long_t epoll_watches; /* The number of file descriptors currently watched */
8817ef9964eSDavide Libenzi #endif
882970a8645SAlexey Dobriyan #ifdef CONFIG_POSIX_MQUEUE
8831da177e4SLinus Torvalds 	/* protected by mq_lock	*/
8841da177e4SLinus Torvalds 	unsigned long mq_bytes;	/* How many bytes can be allocated to mqueue? */
885970a8645SAlexey Dobriyan #endif
8861da177e4SLinus Torvalds 	unsigned long locked_shm; /* How many pages of mlocked shm ? */
887712f4aadSwilly tarreau 	unsigned long unix_inflight;	/* How many files in flight in unix sockets */
888759c0114SWilly Tarreau 	atomic_long_t pipe_bufs;  /* how many pages are allocated in pipe buffers */
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds #ifdef CONFIG_KEYS
8911da177e4SLinus Torvalds 	struct key *uid_keyring;	/* UID specific keyring */
8921da177e4SLinus Torvalds 	struct key *session_keyring;	/* UID's default session keyring */
8931da177e4SLinus Torvalds #endif
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	/* Hash table maintenance information */
896735de223SPavel Emelyanov 	struct hlist_node uidhash_node;
8977b44ab97SEric W. Biederman 	kuid_t uid;
89824e377a8SSrivatsa Vaddagiri 
899aaac3ba9SAlexei Starovoitov #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL)
900789f90fcSPeter Zijlstra 	atomic_long_t locked_vm;
901789f90fcSPeter Zijlstra #endif
9021da177e4SLinus Torvalds };
9031da177e4SLinus Torvalds 
904eb41d946SKay Sievers extern int uids_sysfs_init(void);
9055cb350baSDhaval Giani 
9067b44ab97SEric W. Biederman extern struct user_struct *find_user(kuid_t);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds extern struct user_struct root_user;
9091da177e4SLinus Torvalds #define INIT_USER (&root_user)
9101da177e4SLinus Torvalds 
911b6dff3ecSDavid Howells 
9121da177e4SLinus Torvalds struct backing_dev_info;
9131da177e4SLinus Torvalds struct reclaim_state;
9141da177e4SLinus Torvalds 
915f6db8347SNaveen N. Rao #ifdef CONFIG_SCHED_INFO
9161da177e4SLinus Torvalds struct sched_info {
9171da177e4SLinus Torvalds 	/* cumulative counters */
9182d72376bSIngo Molnar 	unsigned long pcount;	      /* # of times run on this cpu */
9199c2c4802SKen Chen 	unsigned long long run_delay; /* time spent waiting on a runqueue */
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	/* timestamps */
922172ba844SBalbir Singh 	unsigned long long last_arrival,/* when we last ran on a cpu */
9231da177e4SLinus Torvalds 			   last_queued;	/* when we were last queued to run */
9241da177e4SLinus Torvalds };
925f6db8347SNaveen N. Rao #endif /* CONFIG_SCHED_INFO */
9261da177e4SLinus Torvalds 
927ca74e92bSShailabh Nagar #ifdef CONFIG_TASK_DELAY_ACCT
928ca74e92bSShailabh Nagar struct task_delay_info {
929ca74e92bSShailabh Nagar 	spinlock_t	lock;
930ca74e92bSShailabh Nagar 	unsigned int	flags;	/* Private per-task flags */
931ca74e92bSShailabh Nagar 
932ca74e92bSShailabh Nagar 	/* For each stat XXX, add following, aligned appropriately
933ca74e92bSShailabh Nagar 	 *
934ca74e92bSShailabh Nagar 	 * struct timespec XXX_start, XXX_end;
935ca74e92bSShailabh Nagar 	 * u64 XXX_delay;
936ca74e92bSShailabh Nagar 	 * u32 XXX_count;
937ca74e92bSShailabh Nagar 	 *
938ca74e92bSShailabh Nagar 	 * Atomicity of updates to XXX_delay, XXX_count protected by
939ca74e92bSShailabh Nagar 	 * single lock above (split into XXX_lock if contention is an issue).
940ca74e92bSShailabh Nagar 	 */
9410ff92245SShailabh Nagar 
9420ff92245SShailabh Nagar 	/*
9430ff92245SShailabh Nagar 	 * XXX_count is incremented on every XXX operation, the delay
9440ff92245SShailabh Nagar 	 * associated with the operation is added to XXX_delay.
9450ff92245SShailabh Nagar 	 * XXX_delay contains the accumulated delay time in nanoseconds.
9460ff92245SShailabh Nagar 	 */
9479667a23dSThomas Gleixner 	u64 blkio_start;	/* Shared by blkio, swapin */
9480ff92245SShailabh Nagar 	u64 blkio_delay;	/* wait for sync block io completion */
9490ff92245SShailabh Nagar 	u64 swapin_delay;	/* wait for swapin block io completion */
9500ff92245SShailabh Nagar 	u32 blkio_count;	/* total count of the number of sync block */
9510ff92245SShailabh Nagar 				/* io operations performed */
9520ff92245SShailabh Nagar 	u32 swapin_count;	/* total count of the number of swapin block */
9530ff92245SShailabh Nagar 				/* io operations performed */
954873b4771SKeika Kobayashi 
9559667a23dSThomas Gleixner 	u64 freepages_start;
956873b4771SKeika Kobayashi 	u64 freepages_delay;	/* wait for memory reclaim */
957873b4771SKeika Kobayashi 	u32 freepages_count;	/* total count of memory reclaim */
958ca74e92bSShailabh Nagar };
95952f17b6cSChandra Seetharaman #endif	/* CONFIG_TASK_DELAY_ACCT */
96052f17b6cSChandra Seetharaman 
96152f17b6cSChandra Seetharaman static inline int sched_info_on(void)
96252f17b6cSChandra Seetharaman {
96352f17b6cSChandra Seetharaman #ifdef CONFIG_SCHEDSTATS
96452f17b6cSChandra Seetharaman 	return 1;
96552f17b6cSChandra Seetharaman #elif defined(CONFIG_TASK_DELAY_ACCT)
96652f17b6cSChandra Seetharaman 	extern int delayacct_on;
96752f17b6cSChandra Seetharaman 	return delayacct_on;
96852f17b6cSChandra Seetharaman #else
96952f17b6cSChandra Seetharaman 	return 0;
970ca74e92bSShailabh Nagar #endif
97152f17b6cSChandra Seetharaman }
972ca74e92bSShailabh Nagar 
973cb251765SMel Gorman #ifdef CONFIG_SCHEDSTATS
974cb251765SMel Gorman void force_schedstat_enabled(void);
975cb251765SMel Gorman #endif
976cb251765SMel Gorman 
977d15bcfdbSIngo Molnar enum cpu_idle_type {
978d15bcfdbSIngo Molnar 	CPU_IDLE,
979d15bcfdbSIngo Molnar 	CPU_NOT_IDLE,
980d15bcfdbSIngo Molnar 	CPU_NEWLY_IDLE,
981d15bcfdbSIngo Molnar 	CPU_MAX_IDLE_TYPES
9821da177e4SLinus Torvalds };
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds /*
9856ecdd749SYuyang Du  * Integer metrics need fixed point arithmetic, e.g., sched/fair
9866ecdd749SYuyang Du  * has a few: load, load_avg, util_avg, freq, and capacity.
9876ecdd749SYuyang Du  *
9886ecdd749SYuyang Du  * We define a basic fixed point arithmetic range, and then formalize
9896ecdd749SYuyang Du  * all these metrics based on that basic range.
9906ecdd749SYuyang Du  */
9916ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SHIFT	10
9926ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SCALE	(1L << SCHED_FIXEDPOINT_SHIFT)
9936ecdd749SYuyang Du 
9946ecdd749SYuyang Du /*
995ca8ce3d0SNicolas Pitre  * Increase resolution of cpu_capacity calculations
9961399fa78SNikhil Rao  */
9976ecdd749SYuyang Du #define SCHED_CAPACITY_SHIFT	SCHED_FIXEDPOINT_SHIFT
998ca8ce3d0SNicolas Pitre #define SCHED_CAPACITY_SCALE	(1L << SCHED_CAPACITY_SHIFT)
9991da177e4SLinus Torvalds 
10001399fa78SNikhil Rao /*
100176751049SPeter Zijlstra  * Wake-queues are lists of tasks with a pending wakeup, whose
100276751049SPeter Zijlstra  * callers have already marked the task as woken internally,
100376751049SPeter Zijlstra  * and can thus carry on. A common use case is being able to
100476751049SPeter Zijlstra  * do the wakeups once the corresponding user lock as been
100576751049SPeter Zijlstra  * released.
100676751049SPeter Zijlstra  *
100776751049SPeter Zijlstra  * We hold reference to each task in the list across the wakeup,
100876751049SPeter Zijlstra  * thus guaranteeing that the memory is still valid by the time
100976751049SPeter Zijlstra  * the actual wakeups are performed in wake_up_q().
101076751049SPeter Zijlstra  *
101176751049SPeter Zijlstra  * One per task suffices, because there's never a need for a task to be
101276751049SPeter Zijlstra  * in two wake queues simultaneously; it is forbidden to abandon a task
101376751049SPeter Zijlstra  * in a wake queue (a call to wake_up_q() _must_ follow), so if a task is
101476751049SPeter Zijlstra  * already in a wake queue, the wakeup will happen soon and the second
101576751049SPeter Zijlstra  * waker can just skip it.
101676751049SPeter Zijlstra  *
1017194a6b5bSWaiman Long  * The DEFINE_WAKE_Q macro declares and initializes the list head.
101876751049SPeter Zijlstra  * wake_up_q() does NOT reinitialize the list; it's expected to be
101976751049SPeter Zijlstra  * called near the end of a function, where the fact that the queue is
102076751049SPeter Zijlstra  * not used again will be easy to see by inspection.
102176751049SPeter Zijlstra  *
102276751049SPeter Zijlstra  * Note that this can cause spurious wakeups. schedule() callers
102376751049SPeter Zijlstra  * must ensure the call is done inside a loop, confirming that the
102476751049SPeter Zijlstra  * wakeup condition has in fact occurred.
102576751049SPeter Zijlstra  */
102676751049SPeter Zijlstra struct wake_q_node {
102776751049SPeter Zijlstra 	struct wake_q_node *next;
102876751049SPeter Zijlstra };
102976751049SPeter Zijlstra 
103076751049SPeter Zijlstra struct wake_q_head {
103176751049SPeter Zijlstra 	struct wake_q_node *first;
103276751049SPeter Zijlstra 	struct wake_q_node **lastp;
103376751049SPeter Zijlstra };
103476751049SPeter Zijlstra 
103576751049SPeter Zijlstra #define WAKE_Q_TAIL ((struct wake_q_node *) 0x01)
103676751049SPeter Zijlstra 
1037194a6b5bSWaiman Long #define DEFINE_WAKE_Q(name)				\
103876751049SPeter Zijlstra 	struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
103976751049SPeter Zijlstra 
104076751049SPeter Zijlstra extern void wake_q_add(struct wake_q_head *head,
104176751049SPeter Zijlstra 		       struct task_struct *task);
104276751049SPeter Zijlstra extern void wake_up_q(struct wake_q_head *head);
104376751049SPeter Zijlstra 
104476751049SPeter Zijlstra /*
10451399fa78SNikhil Rao  * sched-domains (multiprocessor balancing) declarations:
10461399fa78SNikhil Rao  */
10472dd73a4fSPeter Williams #ifdef CONFIG_SMP
1048b5d978e0SPeter Zijlstra #define SD_LOAD_BALANCE		0x0001	/* Do load balancing on this domain. */
1049b5d978e0SPeter Zijlstra #define SD_BALANCE_NEWIDLE	0x0002	/* Balance when about to become idle */
1050b5d978e0SPeter Zijlstra #define SD_BALANCE_EXEC		0x0004	/* Balance on exec */
1051b5d978e0SPeter Zijlstra #define SD_BALANCE_FORK		0x0008	/* Balance on fork, clone */
1052c88d5910SPeter Zijlstra #define SD_BALANCE_WAKE		0x0010  /* Balance on wakeup */
1053b5d978e0SPeter Zijlstra #define SD_WAKE_AFFINE		0x0020	/* Wake task to waking CPU */
10541f6e6c7cSMorten Rasmussen #define SD_ASYM_CPUCAPACITY	0x0040  /* Groups have different max cpu capacities */
1055bd425d4bSMorten Rasmussen #define SD_SHARE_CPUCAPACITY	0x0080	/* Domain members share cpu capacity */
1056d77b3ed5SVincent Guittot #define SD_SHARE_POWERDOMAIN	0x0100	/* Domain members share power domain */
1057b5d978e0SPeter Zijlstra #define SD_SHARE_PKG_RESOURCES	0x0200	/* Domain members share cpu pkg resources */
1058b5d978e0SPeter Zijlstra #define SD_SERIALIZE		0x0400	/* Only a single load balancing instance */
1059532cb4c4SMichael Neuling #define SD_ASYM_PACKING		0x0800  /* Place busy groups earlier in the domain */
1060b5d978e0SPeter Zijlstra #define SD_PREFER_SIBLING	0x1000	/* Prefer to place tasks in a sibling domain */
1061e3589f6cSPeter Zijlstra #define SD_OVERLAP		0x2000	/* sched_domains of this level overlap */
10623a7053b3SMel Gorman #define SD_NUMA			0x4000	/* cross-node balancing */
10635c45bf27SSiddha, Suresh B 
1064143e1e28SVincent Guittot #ifdef CONFIG_SCHED_SMT
1065b6220ad6SGuenter Roeck static inline int cpu_smt_flags(void)
1066143e1e28SVincent Guittot {
10675d4dfdddSNicolas Pitre 	return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
1068143e1e28SVincent Guittot }
1069143e1e28SVincent Guittot #endif
1070143e1e28SVincent Guittot 
1071143e1e28SVincent Guittot #ifdef CONFIG_SCHED_MC
1072b6220ad6SGuenter Roeck static inline int cpu_core_flags(void)
1073143e1e28SVincent Guittot {
1074143e1e28SVincent Guittot 	return SD_SHARE_PKG_RESOURCES;
1075143e1e28SVincent Guittot }
1076143e1e28SVincent Guittot #endif
1077143e1e28SVincent Guittot 
1078143e1e28SVincent Guittot #ifdef CONFIG_NUMA
1079b6220ad6SGuenter Roeck static inline int cpu_numa_flags(void)
1080143e1e28SVincent Guittot {
1081143e1e28SVincent Guittot 	return SD_NUMA;
1082143e1e28SVincent Guittot }
1083143e1e28SVincent Guittot #endif
1084532cb4c4SMichael Neuling 
1085afe06efdSTim Chen extern int arch_asym_cpu_priority(int cpu);
1086afe06efdSTim Chen 
10871d3504fcSHidetoshi Seto struct sched_domain_attr {
10881d3504fcSHidetoshi Seto 	int relax_domain_level;
10891d3504fcSHidetoshi Seto };
10901d3504fcSHidetoshi Seto 
10911d3504fcSHidetoshi Seto #define SD_ATTR_INIT	(struct sched_domain_attr) {	\
10921d3504fcSHidetoshi Seto 	.relax_domain_level = -1,			\
10931d3504fcSHidetoshi Seto }
10941d3504fcSHidetoshi Seto 
109560495e77SPeter Zijlstra extern int sched_domain_level_max;
109660495e77SPeter Zijlstra 
10975e6521eaSLi Zefan struct sched_group;
10985e6521eaSLi Zefan 
109924fc7edbSPeter Zijlstra struct sched_domain_shared {
110024fc7edbSPeter Zijlstra 	atomic_t	ref;
11010e369d75SPeter Zijlstra 	atomic_t	nr_busy_cpus;
110210e2f1acSPeter Zijlstra 	int		has_idle_cores;
110324fc7edbSPeter Zijlstra };
110424fc7edbSPeter Zijlstra 
11051da177e4SLinus Torvalds struct sched_domain {
11061da177e4SLinus Torvalds 	/* These fields must be setup */
11071da177e4SLinus Torvalds 	struct sched_domain *parent;	/* top domain must be null terminated */
11081a848870SSiddha, Suresh B 	struct sched_domain *child;	/* bottom domain must be null terminated */
11091da177e4SLinus Torvalds 	struct sched_group *groups;	/* the balancing groups of the domain */
11101da177e4SLinus Torvalds 	unsigned long min_interval;	/* Minimum balance interval ms */
11111da177e4SLinus Torvalds 	unsigned long max_interval;	/* Maximum balance interval ms */
11121da177e4SLinus Torvalds 	unsigned int busy_factor;	/* less balancing by factor if busy */
11131da177e4SLinus Torvalds 	unsigned int imbalance_pct;	/* No balance until over watermark */
11141da177e4SLinus Torvalds 	unsigned int cache_nice_tries;	/* Leave cache hot tasks for # tries */
11157897986bSNick Piggin 	unsigned int busy_idx;
11167897986bSNick Piggin 	unsigned int idle_idx;
11177897986bSNick Piggin 	unsigned int newidle_idx;
11187897986bSNick Piggin 	unsigned int wake_idx;
1119147cbb4bSNick Piggin 	unsigned int forkexec_idx;
1120a52bfd73SPeter Zijlstra 	unsigned int smt_gain;
112125f55d9dSVincent Guittot 
112225f55d9dSVincent Guittot 	int nohz_idle;			/* NOHZ IDLE status */
11231da177e4SLinus Torvalds 	int flags;			/* See SD_* */
112460495e77SPeter Zijlstra 	int level;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	/* Runtime fields. */
11271da177e4SLinus Torvalds 	unsigned long last_balance;	/* init to jiffies. units in jiffies */
11281da177e4SLinus Torvalds 	unsigned int balance_interval;	/* initialise to 1. units in ms. */
11291da177e4SLinus Torvalds 	unsigned int nr_balance_failed; /* initialise to 0 */
11301da177e4SLinus Torvalds 
1131f48627e6SJason Low 	/* idle_balance() stats */
11329bd721c5SJason Low 	u64 max_newidle_lb_cost;
1133f48627e6SJason Low 	unsigned long next_decay_max_lb_cost;
11342398f2c6SPeter Zijlstra 
113510e2f1acSPeter Zijlstra 	u64 avg_scan_cost;		/* select_idle_sibling */
113610e2f1acSPeter Zijlstra 
11371da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
11381da177e4SLinus Torvalds 	/* load_balance() stats */
1139480b9434SKen Chen 	unsigned int lb_count[CPU_MAX_IDLE_TYPES];
1140480b9434SKen Chen 	unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
1141480b9434SKen Chen 	unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
1142480b9434SKen Chen 	unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
1143480b9434SKen Chen 	unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
1144480b9434SKen Chen 	unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
1145480b9434SKen Chen 	unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
1146480b9434SKen Chen 	unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	/* Active load balancing */
1149480b9434SKen Chen 	unsigned int alb_count;
1150480b9434SKen Chen 	unsigned int alb_failed;
1151480b9434SKen Chen 	unsigned int alb_pushed;
11521da177e4SLinus Torvalds 
115368767a0aSNick Piggin 	/* SD_BALANCE_EXEC stats */
1154480b9434SKen Chen 	unsigned int sbe_count;
1155480b9434SKen Chen 	unsigned int sbe_balanced;
1156480b9434SKen Chen 	unsigned int sbe_pushed;
11571da177e4SLinus Torvalds 
115868767a0aSNick Piggin 	/* SD_BALANCE_FORK stats */
1159480b9434SKen Chen 	unsigned int sbf_count;
1160480b9434SKen Chen 	unsigned int sbf_balanced;
1161480b9434SKen Chen 	unsigned int sbf_pushed;
116268767a0aSNick Piggin 
11631da177e4SLinus Torvalds 	/* try_to_wake_up() stats */
1164480b9434SKen Chen 	unsigned int ttwu_wake_remote;
1165480b9434SKen Chen 	unsigned int ttwu_move_affine;
1166480b9434SKen Chen 	unsigned int ttwu_move_balance;
11671da177e4SLinus Torvalds #endif
1168a5d8c348SIngo Molnar #ifdef CONFIG_SCHED_DEBUG
1169a5d8c348SIngo Molnar 	char *name;
1170a5d8c348SIngo Molnar #endif
1171dce840a0SPeter Zijlstra 	union {
1172dce840a0SPeter Zijlstra 		void *private;		/* used during construction */
1173dce840a0SPeter Zijlstra 		struct rcu_head rcu;	/* used during destruction */
1174dce840a0SPeter Zijlstra 	};
117524fc7edbSPeter Zijlstra 	struct sched_domain_shared *shared;
11766c99e9adSRusty Russell 
1177669c55e9SPeter Zijlstra 	unsigned int span_weight;
11784200efd9SIngo Molnar 	/*
11794200efd9SIngo Molnar 	 * Span of all CPUs in this domain.
11804200efd9SIngo Molnar 	 *
11814200efd9SIngo Molnar 	 * NOTE: this field is variable length. (Allocated dynamically
11824200efd9SIngo Molnar 	 * by attaching extra space to the end of the structure,
11834200efd9SIngo Molnar 	 * depending on how many CPUs the kernel has booted up with)
11844200efd9SIngo Molnar 	 */
11854200efd9SIngo Molnar 	unsigned long span[0];
11861da177e4SLinus Torvalds };
11871da177e4SLinus Torvalds 
1188758b2cdcSRusty Russell static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
1189758b2cdcSRusty Russell {
11906c99e9adSRusty Russell 	return to_cpumask(sd->span);
1191758b2cdcSRusty Russell }
1192758b2cdcSRusty Russell 
1193acc3f5d7SRusty Russell extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
11941d3504fcSHidetoshi Seto 				    struct sched_domain_attr *dattr_new);
1195029190c5SPaul Jackson 
1196acc3f5d7SRusty Russell /* Allocate an array of sched domains, for partition_sched_domains(). */
1197acc3f5d7SRusty Russell cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
1198acc3f5d7SRusty Russell void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
1199acc3f5d7SRusty Russell 
120039be3501SPeter Zijlstra bool cpus_share_cache(int this_cpu, int that_cpu);
120139be3501SPeter Zijlstra 
1202143e1e28SVincent Guittot typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
1203b6220ad6SGuenter Roeck typedef int (*sched_domain_flags_f)(void);
1204143e1e28SVincent Guittot 
1205143e1e28SVincent Guittot #define SDTL_OVERLAP	0x01
1206143e1e28SVincent Guittot 
1207143e1e28SVincent Guittot struct sd_data {
1208143e1e28SVincent Guittot 	struct sched_domain **__percpu sd;
120924fc7edbSPeter Zijlstra 	struct sched_domain_shared **__percpu sds;
1210143e1e28SVincent Guittot 	struct sched_group **__percpu sg;
121163b2ca30SNicolas Pitre 	struct sched_group_capacity **__percpu sgc;
1212143e1e28SVincent Guittot };
1213143e1e28SVincent Guittot 
1214143e1e28SVincent Guittot struct sched_domain_topology_level {
1215143e1e28SVincent Guittot 	sched_domain_mask_f mask;
1216143e1e28SVincent Guittot 	sched_domain_flags_f sd_flags;
1217143e1e28SVincent Guittot 	int		    flags;
1218143e1e28SVincent Guittot 	int		    numa_level;
1219143e1e28SVincent Guittot 	struct sd_data      data;
1220143e1e28SVincent Guittot #ifdef CONFIG_SCHED_DEBUG
1221143e1e28SVincent Guittot 	char                *name;
1222143e1e28SVincent Guittot #endif
1223143e1e28SVincent Guittot };
1224143e1e28SVincent Guittot 
1225143e1e28SVincent Guittot extern void set_sched_topology(struct sched_domain_topology_level *tl);
1226f6be8af1SChuansheng Liu extern void wake_up_if_idle(int cpu);
1227143e1e28SVincent Guittot 
1228143e1e28SVincent Guittot #ifdef CONFIG_SCHED_DEBUG
1229143e1e28SVincent Guittot # define SD_INIT_NAME(type)		.name = #type
1230143e1e28SVincent Guittot #else
1231143e1e28SVincent Guittot # define SD_INIT_NAME(type)
1232143e1e28SVincent Guittot #endif
1233143e1e28SVincent Guittot 
12341b427c15SIngo Molnar #else /* CONFIG_SMP */
12351da177e4SLinus Torvalds 
12361b427c15SIngo Molnar struct sched_domain_attr;
12371b427c15SIngo Molnar 
12381b427c15SIngo Molnar static inline void
1239acc3f5d7SRusty Russell partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
12401b427c15SIngo Molnar 			struct sched_domain_attr *dattr_new)
1241d02c7a8cSCon Kolivas {
1242d02c7a8cSCon Kolivas }
124339be3501SPeter Zijlstra 
124439be3501SPeter Zijlstra static inline bool cpus_share_cache(int this_cpu, int that_cpu)
124539be3501SPeter Zijlstra {
124639be3501SPeter Zijlstra 	return true;
124739be3501SPeter Zijlstra }
124839be3501SPeter Zijlstra 
12491b427c15SIngo Molnar #endif	/* !CONFIG_SMP */
12501da177e4SLinus Torvalds 
125147fe38fcSPeter Zijlstra 
12521da177e4SLinus Torvalds struct io_context;			/* See blkdev.h */
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 
1255383f2835SChen, Kenneth W #ifdef ARCH_HAS_PREFETCH_SWITCH_STACK
125636c8b586SIngo Molnar extern void prefetch_stack(struct task_struct *t);
1257383f2835SChen, Kenneth W #else
1258383f2835SChen, Kenneth W static inline void prefetch_stack(struct task_struct *t) { }
1259383f2835SChen, Kenneth W #endif
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds struct audit_context;		/* See audit.c */
12621da177e4SLinus Torvalds struct mempolicy;
1263b92ce558SJens Axboe struct pipe_inode_info;
12644865ecf1SSerge E. Hallyn struct uts_namespace;
12651da177e4SLinus Torvalds 
126620b8a59fSIngo Molnar struct load_weight {
12679dbdb155SPeter Zijlstra 	unsigned long weight;
12689dbdb155SPeter Zijlstra 	u32 inv_weight;
126920b8a59fSIngo Molnar };
127020b8a59fSIngo Molnar 
12719d89c257SYuyang Du /*
12727b595334SYuyang Du  * The load_avg/util_avg accumulates an infinite geometric series
12737b595334SYuyang Du  * (see __update_load_avg() in kernel/sched/fair.c).
12747b595334SYuyang Du  *
12757b595334SYuyang Du  * [load_avg definition]
12767b595334SYuyang Du  *
12777b595334SYuyang Du  *   load_avg = runnable% * scale_load_down(load)
12787b595334SYuyang Du  *
12797b595334SYuyang Du  * where runnable% is the time ratio that a sched_entity is runnable.
12807b595334SYuyang Du  * For cfs_rq, it is the aggregated load_avg of all runnable and
12819d89c257SYuyang Du  * blocked sched_entities.
12827b595334SYuyang Du  *
12837b595334SYuyang Du  * load_avg may also take frequency scaling into account:
12847b595334SYuyang Du  *
12857b595334SYuyang Du  *   load_avg = runnable% * scale_load_down(load) * freq%
12867b595334SYuyang Du  *
12877b595334SYuyang Du  * where freq% is the CPU frequency normalized to the highest frequency.
12887b595334SYuyang Du  *
12897b595334SYuyang Du  * [util_avg definition]
12907b595334SYuyang Du  *
12917b595334SYuyang Du  *   util_avg = running% * SCHED_CAPACITY_SCALE
12927b595334SYuyang Du  *
12937b595334SYuyang Du  * where running% is the time ratio that a sched_entity is running on
12947b595334SYuyang Du  * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
12957b595334SYuyang Du  * and blocked sched_entities.
12967b595334SYuyang Du  *
12977b595334SYuyang Du  * util_avg may also factor frequency scaling and CPU capacity scaling:
12987b595334SYuyang Du  *
12997b595334SYuyang Du  *   util_avg = running% * SCHED_CAPACITY_SCALE * freq% * capacity%
13007b595334SYuyang Du  *
13017b595334SYuyang Du  * where freq% is the same as above, and capacity% is the CPU capacity
13027b595334SYuyang Du  * normalized to the greatest capacity (due to uarch differences, etc).
13037b595334SYuyang Du  *
13047b595334SYuyang Du  * N.B., the above ratios (runnable%, running%, freq%, and capacity%)
13057b595334SYuyang Du  * themselves are in the range of [0, 1]. To do fixed point arithmetics,
13067b595334SYuyang Du  * we therefore scale them to as large a range as necessary. This is for
13077b595334SYuyang Du  * example reflected by util_avg's SCHED_CAPACITY_SCALE.
13087b595334SYuyang Du  *
13097b595334SYuyang Du  * [Overflow issue]
13107b595334SYuyang Du  *
13117b595334SYuyang Du  * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
13127b595334SYuyang Du  * with the highest load (=88761), always runnable on a single cfs_rq,
13137b595334SYuyang Du  * and should not overflow as the number already hits PID_MAX_LIMIT.
13147b595334SYuyang Du  *
13157b595334SYuyang Du  * For all other cases (including 32-bit kernels), struct load_weight's
13167b595334SYuyang Du  * weight will overflow first before we do, because:
13177b595334SYuyang Du  *
13187b595334SYuyang Du  *    Max(load_avg) <= Max(load.weight)
13197b595334SYuyang Du  *
13207b595334SYuyang Du  * Then it is the load_weight's responsibility to consider overflow
13217b595334SYuyang Du  * issues.
13229d89c257SYuyang Du  */
13239d85f21cSPaul Turner struct sched_avg {
13249d89c257SYuyang Du 	u64 last_update_time, load_sum;
13259d89c257SYuyang Du 	u32 util_sum, period_contrib;
13269d89c257SYuyang Du 	unsigned long load_avg, util_avg;
13279d85f21cSPaul Turner };
13289d85f21cSPaul Turner 
132994c18227SIngo Molnar #ifdef CONFIG_SCHEDSTATS
133041acab88SLucas De Marchi struct sched_statistics {
133194c18227SIngo Molnar 	u64			wait_start;
133294c18227SIngo Molnar 	u64			wait_max;
13336d082592SArjan van de Ven 	u64			wait_count;
13346d082592SArjan van de Ven 	u64			wait_sum;
13358f0dfc34SArjan van de Ven 	u64			iowait_count;
13368f0dfc34SArjan van de Ven 	u64			iowait_sum;
133794c18227SIngo Molnar 
133894c18227SIngo Molnar 	u64			sleep_start;
133920b8a59fSIngo Molnar 	u64			sleep_max;
134094c18227SIngo Molnar 	s64			sum_sleep_runtime;
134194c18227SIngo Molnar 
134294c18227SIngo Molnar 	u64			block_start;
134320b8a59fSIngo Molnar 	u64			block_max;
134420b8a59fSIngo Molnar 	u64			exec_max;
1345eba1ed4bSIngo Molnar 	u64			slice_max;
1346cc367732SIngo Molnar 
1347cc367732SIngo Molnar 	u64			nr_migrations_cold;
1348cc367732SIngo Molnar 	u64			nr_failed_migrations_affine;
1349cc367732SIngo Molnar 	u64			nr_failed_migrations_running;
1350cc367732SIngo Molnar 	u64			nr_failed_migrations_hot;
1351cc367732SIngo Molnar 	u64			nr_forced_migrations;
1352cc367732SIngo Molnar 
1353cc367732SIngo Molnar 	u64			nr_wakeups;
1354cc367732SIngo Molnar 	u64			nr_wakeups_sync;
1355cc367732SIngo Molnar 	u64			nr_wakeups_migrate;
1356cc367732SIngo Molnar 	u64			nr_wakeups_local;
1357cc367732SIngo Molnar 	u64			nr_wakeups_remote;
1358cc367732SIngo Molnar 	u64			nr_wakeups_affine;
1359cc367732SIngo Molnar 	u64			nr_wakeups_affine_attempts;
1360cc367732SIngo Molnar 	u64			nr_wakeups_passive;
1361cc367732SIngo Molnar 	u64			nr_wakeups_idle;
136241acab88SLucas De Marchi };
136341acab88SLucas De Marchi #endif
136441acab88SLucas De Marchi 
136541acab88SLucas De Marchi struct sched_entity {
136641acab88SLucas De Marchi 	struct load_weight	load;		/* for load-balancing */
136741acab88SLucas De Marchi 	struct rb_node		run_node;
136841acab88SLucas De Marchi 	struct list_head	group_node;
136941acab88SLucas De Marchi 	unsigned int		on_rq;
137041acab88SLucas De Marchi 
137141acab88SLucas De Marchi 	u64			exec_start;
137241acab88SLucas De Marchi 	u64			sum_exec_runtime;
137341acab88SLucas De Marchi 	u64			vruntime;
137441acab88SLucas De Marchi 	u64			prev_sum_exec_runtime;
137541acab88SLucas De Marchi 
137641acab88SLucas De Marchi 	u64			nr_migrations;
137741acab88SLucas De Marchi 
137841acab88SLucas De Marchi #ifdef CONFIG_SCHEDSTATS
137941acab88SLucas De Marchi 	struct sched_statistics statistics;
138094c18227SIngo Molnar #endif
138194c18227SIngo Molnar 
138220b8a59fSIngo Molnar #ifdef CONFIG_FAIR_GROUP_SCHED
1383fed14d45SPeter Zijlstra 	int			depth;
138420b8a59fSIngo Molnar 	struct sched_entity	*parent;
138520b8a59fSIngo Molnar 	/* rq on which this entity is (to be) queued: */
138620b8a59fSIngo Molnar 	struct cfs_rq		*cfs_rq;
138720b8a59fSIngo Molnar 	/* rq "owned" by this entity/group: */
138820b8a59fSIngo Molnar 	struct cfs_rq		*my_q;
138920b8a59fSIngo Molnar #endif
13908bd75c77SClark Williams 
1391141965c7SAlex Shi #ifdef CONFIG_SMP
13925a107804SJiri Olsa 	/*
13935a107804SJiri Olsa 	 * Per entity load average tracking.
13945a107804SJiri Olsa 	 *
13955a107804SJiri Olsa 	 * Put into separate cache line so it does not
13965a107804SJiri Olsa 	 * collide with read-mostly values above.
13975a107804SJiri Olsa 	 */
13985a107804SJiri Olsa 	struct sched_avg	avg ____cacheline_aligned_in_smp;
13999d85f21cSPaul Turner #endif
140020b8a59fSIngo Molnar };
140170b97a7fSIngo Molnar 
1402fa717060SPeter Zijlstra struct sched_rt_entity {
1403fa717060SPeter Zijlstra 	struct list_head run_list;
140478f2c7dbSPeter Zijlstra 	unsigned long timeout;
140557d2aa00SYing Xue 	unsigned long watchdog_stamp;
1406bee367edSRichard Kennedy 	unsigned int time_slice;
1407ff77e468SPeter Zijlstra 	unsigned short on_rq;
1408ff77e468SPeter Zijlstra 	unsigned short on_list;
14096f505b16SPeter Zijlstra 
141058d6c2d7SPeter Zijlstra 	struct sched_rt_entity *back;
1411052f1dc7SPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
14126f505b16SPeter Zijlstra 	struct sched_rt_entity	*parent;
14136f505b16SPeter Zijlstra 	/* rq on which this entity is (to be) queued: */
14146f505b16SPeter Zijlstra 	struct rt_rq		*rt_rq;
14156f505b16SPeter Zijlstra 	/* rq "owned" by this entity/group: */
14166f505b16SPeter Zijlstra 	struct rt_rq		*my_q;
14176f505b16SPeter Zijlstra #endif
1418fa717060SPeter Zijlstra };
1419fa717060SPeter Zijlstra 
1420aab03e05SDario Faggioli struct sched_dl_entity {
1421aab03e05SDario Faggioli 	struct rb_node	rb_node;
1422aab03e05SDario Faggioli 
1423aab03e05SDario Faggioli 	/*
1424aab03e05SDario Faggioli 	 * Original scheduling parameters. Copied here from sched_attr
14254027d080Sxiaofeng.yan 	 * during sched_setattr(), they will remain the same until
14264027d080Sxiaofeng.yan 	 * the next sched_setattr().
1427aab03e05SDario Faggioli 	 */
1428aab03e05SDario Faggioli 	u64 dl_runtime;		/* maximum runtime for each instance	*/
1429aab03e05SDario Faggioli 	u64 dl_deadline;	/* relative deadline of each instance	*/
1430755378a4SHarald Gustafsson 	u64 dl_period;		/* separation of two instances (period) */
1431332ac17eSDario Faggioli 	u64 dl_bw;		/* dl_runtime / dl_deadline		*/
1432aab03e05SDario Faggioli 
1433aab03e05SDario Faggioli 	/*
1434aab03e05SDario Faggioli 	 * Actual scheduling parameters. Initialized with the values above,
1435aab03e05SDario Faggioli 	 * they are continously updated during task execution. Note that
1436aab03e05SDario Faggioli 	 * the remaining runtime could be < 0 in case we are in overrun.
1437aab03e05SDario Faggioli 	 */
1438aab03e05SDario Faggioli 	s64 runtime;		/* remaining runtime for this instance	*/
1439aab03e05SDario Faggioli 	u64 deadline;		/* absolute deadline for this instance	*/
1440aab03e05SDario Faggioli 	unsigned int flags;	/* specifying the scheduler behaviour	*/
1441aab03e05SDario Faggioli 
1442aab03e05SDario Faggioli 	/*
1443aab03e05SDario Faggioli 	 * Some bool flags:
1444aab03e05SDario Faggioli 	 *
1445aab03e05SDario Faggioli 	 * @dl_throttled tells if we exhausted the runtime. If so, the
1446aab03e05SDario Faggioli 	 * task has to wait for a replenishment to be performed at the
1447aab03e05SDario Faggioli 	 * next firing of dl_timer.
1448aab03e05SDario Faggioli 	 *
14492d3d891dSDario Faggioli 	 * @dl_boosted tells if we are boosted due to DI. If so we are
14502d3d891dSDario Faggioli 	 * outside bandwidth enforcement mechanism (but only until we
14515bfd126eSJuri Lelli 	 * exit the critical section);
14525bfd126eSJuri Lelli 	 *
14535bfd126eSJuri Lelli 	 * @dl_yielded tells if task gave up the cpu before consuming
14545bfd126eSJuri Lelli 	 * all its available runtime during the last job.
1455aab03e05SDario Faggioli 	 */
145672f9f3fdSLuca Abeni 	int dl_throttled, dl_boosted, dl_yielded;
1457aab03e05SDario Faggioli 
1458aab03e05SDario Faggioli 	/*
1459aab03e05SDario Faggioli 	 * Bandwidth enforcement timer. Each -deadline task has its
1460aab03e05SDario Faggioli 	 * own bandwidth to be enforced, thus we need one timer per task.
1461aab03e05SDario Faggioli 	 */
1462aab03e05SDario Faggioli 	struct hrtimer dl_timer;
1463aab03e05SDario Faggioli };
14648bd75c77SClark Williams 
14651d082fd0SPaul E. McKenney union rcu_special {
14661d082fd0SPaul E. McKenney 	struct {
14678203d6d0SPaul E. McKenney 		u8 blocked;
14688203d6d0SPaul E. McKenney 		u8 need_qs;
14698203d6d0SPaul E. McKenney 		u8 exp_need_qs;
14708203d6d0SPaul E. McKenney 		u8 pad;	/* Otherwise the compiler can store garbage here. */
14718203d6d0SPaul E. McKenney 	} b; /* Bits. */
14728203d6d0SPaul E. McKenney 	u32 s; /* Set of bits. */
14731d082fd0SPaul E. McKenney };
147486848966SPaul E. McKenney struct rcu_node;
147586848966SPaul E. McKenney 
14768dc85d54SPeter Zijlstra enum perf_event_task_context {
14778dc85d54SPeter Zijlstra 	perf_invalid_context = -1,
14788dc85d54SPeter Zijlstra 	perf_hw_context = 0,
147989a1e187SPeter Zijlstra 	perf_sw_context,
14808dc85d54SPeter Zijlstra 	perf_nr_task_contexts,
14818dc85d54SPeter Zijlstra };
14828dc85d54SPeter Zijlstra 
148372b252aeSMel Gorman /* Track pages that require TLB flushes */
148472b252aeSMel Gorman struct tlbflush_unmap_batch {
148572b252aeSMel Gorman 	/*
148672b252aeSMel Gorman 	 * Each bit set is a CPU that potentially has a TLB entry for one of
148772b252aeSMel Gorman 	 * the PFNs being flushed. See set_tlb_ubc_flush_pending().
148872b252aeSMel Gorman 	 */
148972b252aeSMel Gorman 	struct cpumask cpumask;
149072b252aeSMel Gorman 
149172b252aeSMel Gorman 	/* True if any bit in cpumask is set */
149272b252aeSMel Gorman 	bool flush_required;
1493d950c947SMel Gorman 
1494d950c947SMel Gorman 	/*
1495d950c947SMel Gorman 	 * If true then the PTE was dirty when unmapped. The entry must be
1496d950c947SMel Gorman 	 * flushed before IO is initiated or a stale TLB entry potentially
1497d950c947SMel Gorman 	 * allows an update without redirtying the page.
1498d950c947SMel Gorman 	 */
1499d950c947SMel Gorman 	bool writable;
150072b252aeSMel Gorman };
150172b252aeSMel Gorman 
15021da177e4SLinus Torvalds struct task_struct {
1503c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1504c65eacbeSAndy Lutomirski 	/*
1505c65eacbeSAndy Lutomirski 	 * For reasons of header soup (see current_thread_info()), this
1506c65eacbeSAndy Lutomirski 	 * must be the first element of task_struct.
1507c65eacbeSAndy Lutomirski 	 */
1508c65eacbeSAndy Lutomirski 	struct thread_info thread_info;
1509c65eacbeSAndy Lutomirski #endif
15101da177e4SLinus Torvalds 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
1511f7e4217bSRoman Zippel 	void *stack;
15121da177e4SLinus Torvalds 	atomic_t usage;
151397dc32cdSWilliam Cohen 	unsigned int flags;	/* per process flags, defined below */
151497dc32cdSWilliam Cohen 	unsigned int ptrace;
15151da177e4SLinus Torvalds 
15162dd73a4fSPeter Williams #ifdef CONFIG_SMP
1517fa14ff4aSPeter Zijlstra 	struct llist_node wake_entry;
15183ca7a440SPeter Zijlstra 	int on_cpu;
1519c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1520c65eacbeSAndy Lutomirski 	unsigned int cpu;	/* current CPU */
1521c65eacbeSAndy Lutomirski #endif
152263b0e9edSMike Galbraith 	unsigned int wakee_flips;
152362470419SMichael Wang 	unsigned long wakee_flip_decay_ts;
152463b0e9edSMike Galbraith 	struct task_struct *last_wakee;
1525ac66f547SPeter Zijlstra 
1526ac66f547SPeter Zijlstra 	int wake_cpu;
15274866cde0SNick Piggin #endif
1528fd2f4419SPeter Zijlstra 	int on_rq;
152950e645a8SIngo Molnar 
1530b29739f9SIngo Molnar 	int prio, static_prio, normal_prio;
1531c7aceabaSRichard Kennedy 	unsigned int rt_priority;
15325522d5d5SIngo Molnar 	const struct sched_class *sched_class;
153320b8a59fSIngo Molnar 	struct sched_entity se;
1534fa717060SPeter Zijlstra 	struct sched_rt_entity rt;
15358323f26cSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED
15368323f26cSPeter Zijlstra 	struct task_group *sched_task_group;
15378323f26cSPeter Zijlstra #endif
1538aab03e05SDario Faggioli 	struct sched_dl_entity dl;
15391da177e4SLinus Torvalds 
1540e107be36SAvi Kivity #ifdef CONFIG_PREEMPT_NOTIFIERS
1541e107be36SAvi Kivity 	/* list of struct preempt_notifier: */
1542e107be36SAvi Kivity 	struct hlist_head preempt_notifiers;
1543e107be36SAvi Kivity #endif
1544e107be36SAvi Kivity 
15456c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
15462056a782SJens Axboe 	unsigned int btrace_seq;
15476c5c9341SAlexey Dobriyan #endif
15481da177e4SLinus Torvalds 
154997dc32cdSWilliam Cohen 	unsigned int policy;
155029baa747SPeter Zijlstra 	int nr_cpus_allowed;
15511da177e4SLinus Torvalds 	cpumask_t cpus_allowed;
15521da177e4SLinus Torvalds 
1553a57eb940SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
1554e260be67SPaul E. McKenney 	int rcu_read_lock_nesting;
15551d082fd0SPaul E. McKenney 	union rcu_special rcu_read_unlock_special;
1556f41d911fSPaul E. McKenney 	struct list_head rcu_node_entry;
1557a57eb940SPaul E. McKenney 	struct rcu_node *rcu_blocked_node;
155828f6569aSPranith Kumar #endif /* #ifdef CONFIG_PREEMPT_RCU */
15598315f422SPaul E. McKenney #ifdef CONFIG_TASKS_RCU
15608315f422SPaul E. McKenney 	unsigned long rcu_tasks_nvcsw;
15618315f422SPaul E. McKenney 	bool rcu_tasks_holdout;
15628315f422SPaul E. McKenney 	struct list_head rcu_tasks_holdout_list;
1563176f8f7aSPaul E. McKenney 	int rcu_tasks_idle_cpu;
15648315f422SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_RCU */
1565e260be67SPaul E. McKenney 
1566f6db8347SNaveen N. Rao #ifdef CONFIG_SCHED_INFO
15671da177e4SLinus Torvalds 	struct sched_info sched_info;
15681da177e4SLinus Torvalds #endif
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	struct list_head tasks;
1571806c09a7SDario Faggioli #ifdef CONFIG_SMP
1572917b627dSGregory Haskins 	struct plist_node pushable_tasks;
15731baca4ceSJuri Lelli 	struct rb_node pushable_dl_tasks;
1574806c09a7SDario Faggioli #endif
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	struct mm_struct *mm, *active_mm;
1577615d6e87SDavidlohr Bueso 	/* per-thread vma caching */
1578615d6e87SDavidlohr Bueso 	u32 vmacache_seqnum;
1579615d6e87SDavidlohr Bueso 	struct vm_area_struct *vmacache[VMACACHE_SIZE];
158034e55232SKAMEZAWA Hiroyuki #if defined(SPLIT_RSS_COUNTING)
158134e55232SKAMEZAWA Hiroyuki 	struct task_rss_stat	rss_stat;
158234e55232SKAMEZAWA Hiroyuki #endif
15831da177e4SLinus Torvalds /* task state */
158497dc32cdSWilliam Cohen 	int exit_state;
15851da177e4SLinus Torvalds 	int exit_code, exit_signal;
15861da177e4SLinus Torvalds 	int pdeath_signal;  /*  The signal sent when the parent dies  */
1587e7cc4173SPalmer Dabbelt 	unsigned long jobctl;	/* JOBCTL_*, siglock protected */
15889b89f6baSAndrei Epure 
15899b89f6baSAndrei Epure 	/* Used for emulating ABI behavior of previous Linux versions */
159097dc32cdSWilliam Cohen 	unsigned int personality;
15919b89f6baSAndrei Epure 
1592be958bdcSPeter Zijlstra 	/* scheduler bits, serialized by scheduler locks */
1593ca94c442SLennart Poettering 	unsigned sched_reset_on_fork:1;
1594a8e4f2eaSPeter Zijlstra 	unsigned sched_contributes_to_load:1;
1595ff303e66SPeter Zijlstra 	unsigned sched_migrated:1;
1596b7e7ade3SPeter Zijlstra 	unsigned sched_remote_wakeup:1;
1597be958bdcSPeter Zijlstra 	unsigned :0; /* force alignment to the next boundary */
1598be958bdcSPeter Zijlstra 
1599be958bdcSPeter Zijlstra 	/* unserialized, strictly 'current' */
1600be958bdcSPeter Zijlstra 	unsigned in_execve:1; /* bit to tell LSMs we're in execve */
1601be958bdcSPeter Zijlstra 	unsigned in_iowait:1;
16027e781418SAndy Lutomirski #if !defined(TIF_RESTORE_SIGMASK)
16037e781418SAndy Lutomirski 	unsigned restore_sigmask:1;
16047e781418SAndy Lutomirski #endif
1605626ebc41STejun Heo #ifdef CONFIG_MEMCG
1606626ebc41STejun Heo 	unsigned memcg_may_oom:1;
1607127424c8SJohannes Weiner #ifndef CONFIG_SLOB
16086f185c29SVladimir Davydov 	unsigned memcg_kmem_skip_account:1;
16096f185c29SVladimir Davydov #endif
1610127424c8SJohannes Weiner #endif
1611ff303e66SPeter Zijlstra #ifdef CONFIG_COMPAT_BRK
1612ff303e66SPeter Zijlstra 	unsigned brk_randomized:1;
1613ff303e66SPeter Zijlstra #endif
16146f185c29SVladimir Davydov 
16151d4457f9SKees Cook 	unsigned long atomic_flags; /* Flags needing atomic access. */
16161d4457f9SKees Cook 
1617f56141e3SAndy Lutomirski 	struct restart_block restart_block;
1618f56141e3SAndy Lutomirski 
16191da177e4SLinus Torvalds 	pid_t pid;
16201da177e4SLinus Torvalds 	pid_t tgid;
16210a425405SArjan van de Ven 
16221314562aSHiroshi Shimamoto #ifdef CONFIG_CC_STACKPROTECTOR
16230a425405SArjan van de Ven 	/* Canary value for the -fstack-protector gcc feature */
16240a425405SArjan van de Ven 	unsigned long stack_canary;
16251314562aSHiroshi Shimamoto #endif
16261da177e4SLinus Torvalds 	/*
16271da177e4SLinus Torvalds 	 * pointers to (original) parent process, youngest child, younger sibling,
16281da177e4SLinus Torvalds 	 * older sibling, respectively.  (p->father can be replaced with
1629f470021aSRoland McGrath 	 * p->real_parent->pid)
16301da177e4SLinus Torvalds 	 */
1631abd63bc3SKees Cook 	struct task_struct __rcu *real_parent; /* real parent process */
1632abd63bc3SKees Cook 	struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
16331da177e4SLinus Torvalds 	/*
1634f470021aSRoland McGrath 	 * children/sibling forms the list of my natural children
16351da177e4SLinus Torvalds 	 */
16361da177e4SLinus Torvalds 	struct list_head children;	/* list of my children */
16371da177e4SLinus Torvalds 	struct list_head sibling;	/* linkage in my parent's children list */
16381da177e4SLinus Torvalds 	struct task_struct *group_leader;	/* threadgroup leader */
16391da177e4SLinus Torvalds 
1640f470021aSRoland McGrath 	/*
1641f470021aSRoland McGrath 	 * ptraced is the list of tasks this task is using ptrace on.
1642f470021aSRoland McGrath 	 * This includes both natural children and PTRACE_ATTACH targets.
1643f470021aSRoland McGrath 	 * p->ptrace_entry is p's link on the p->parent->ptraced list.
1644f470021aSRoland McGrath 	 */
1645f470021aSRoland McGrath 	struct list_head ptraced;
1646f470021aSRoland McGrath 	struct list_head ptrace_entry;
1647f470021aSRoland McGrath 
16481da177e4SLinus Torvalds 	/* PID/PID hash table linkage. */
164992476d7fSEric W. Biederman 	struct pid_link pids[PIDTYPE_MAX];
165047e65328SOleg Nesterov 	struct list_head thread_group;
16510c740d0aSOleg Nesterov 	struct list_head thread_node;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	struct completion *vfork_done;		/* for vfork() */
16541da177e4SLinus Torvalds 	int __user *set_child_tid;		/* CLONE_CHILD_SETTID */
16551da177e4SLinus Torvalds 	int __user *clear_child_tid;		/* CLONE_CHILD_CLEARTID */
16561da177e4SLinus Torvalds 
165740565b5aSStanislaw Gruszka 	cputime_t utime, stime;
165840565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
165940565b5aSStanislaw Gruszka 	cputime_t utimescaled, stimescaled;
166040565b5aSStanislaw Gruszka #endif
1661*16a6d9beSFrederic Weisbecker 	u64 gtime;
16629d7fb042SPeter Zijlstra 	struct prev_cputime prev_cputime;
16636a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1664b7ce2277SFrederic Weisbecker 	seqcount_t vtime_seqcount;
16656a61671bSFrederic Weisbecker 	unsigned long long vtime_snap;
16666a61671bSFrederic Weisbecker 	enum {
16677098c1eaSFrederic Weisbecker 		/* Task is sleeping or running in a CPU with VTIME inactive */
16687098c1eaSFrederic Weisbecker 		VTIME_INACTIVE = 0,
16697098c1eaSFrederic Weisbecker 		/* Task runs in userspace in a CPU with VTIME active */
16706a61671bSFrederic Weisbecker 		VTIME_USER,
16717098c1eaSFrederic Weisbecker 		/* Task runs in kernelspace in a CPU with VTIME active */
16726a61671bSFrederic Weisbecker 		VTIME_SYS,
16736a61671bSFrederic Weisbecker 	} vtime_snap_whence;
16746a61671bSFrederic Weisbecker #endif
1675d027d45dSFrederic Weisbecker 
1676d027d45dSFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
1677f009a7a7SFrederic Weisbecker 	atomic_t tick_dep_mask;
1678d027d45dSFrederic Weisbecker #endif
16791da177e4SLinus Torvalds 	unsigned long nvcsw, nivcsw; /* context switch counts */
1680ccbf62d8SThomas Gleixner 	u64 start_time;		/* monotonic time in nsec */
168157e0be04SThomas Gleixner 	u64 real_start_time;	/* boot based time in nsec */
16821da177e4SLinus Torvalds /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
16831da177e4SLinus Torvalds 	unsigned long min_flt, maj_flt;
16841da177e4SLinus Torvalds 
1685f06febc9SFrank Mayhar 	struct task_cputime cputime_expires;
16861da177e4SLinus Torvalds 	struct list_head cpu_timers[3];
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds /* process credentials */
168964b875f7SEric W. Biederman 	const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */
16901b0ba1c9SArnd Bergmann 	const struct cred __rcu *real_cred; /* objective and real subjective task
16913b11a1deSDavid Howells 					 * credentials (COW) */
16921b0ba1c9SArnd Bergmann 	const struct cred __rcu *cred;	/* effective (overridable) subjective task
16933b11a1deSDavid Howells 					 * credentials (COW) */
169436772092SPaolo 'Blaisorblade' Giarrusso 	char comm[TASK_COMM_LEN]; /* executable name excluding path
169536772092SPaolo 'Blaisorblade' Giarrusso 				     - access with [gs]et_task_comm (which lock
169636772092SPaolo 'Blaisorblade' Giarrusso 				       it with task_lock())
1697221af7f8SLinus Torvalds 				     - initialized normally by setup_new_exec */
16981da177e4SLinus Torvalds /* file system info */
1699756daf26SNeilBrown 	struct nameidata *nameidata;
17003d5b6fccSAlexey Dobriyan #ifdef CONFIG_SYSVIPC
17011da177e4SLinus Torvalds /* ipc stuff */
17021da177e4SLinus Torvalds 	struct sysv_sem sysvsem;
1703ab602f79SJack Miller 	struct sysv_shm sysvshm;
17043d5b6fccSAlexey Dobriyan #endif
1705e162b39aSMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
170682a1fcb9SIngo Molnar /* hung task detection */
170782a1fcb9SIngo Molnar 	unsigned long last_switch_count;
170882a1fcb9SIngo Molnar #endif
17091da177e4SLinus Torvalds /* filesystem information */
17101da177e4SLinus Torvalds 	struct fs_struct *fs;
17111da177e4SLinus Torvalds /* open file information */
17121da177e4SLinus Torvalds 	struct files_struct *files;
17131651e14eSSerge E. Hallyn /* namespaces */
1714ab516013SSerge E. Hallyn 	struct nsproxy *nsproxy;
17151da177e4SLinus Torvalds /* signal handlers */
17161da177e4SLinus Torvalds 	struct signal_struct *signal;
17171da177e4SLinus Torvalds 	struct sighand_struct *sighand;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 	sigset_t blocked, real_blocked;
1720f3de272bSRoland McGrath 	sigset_t saved_sigmask;	/* restored if set_restore_sigmask() was used */
17211da177e4SLinus Torvalds 	struct sigpending pending;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	unsigned long sas_ss_sp;
17241da177e4SLinus Torvalds 	size_t sas_ss_size;
17252a742138SStas Sergeev 	unsigned sas_ss_flags;
17262e01fabeSOleg Nesterov 
172767d12145SAl Viro 	struct callback_head *task_works;
1728e73f8959SOleg Nesterov 
17291da177e4SLinus Torvalds 	struct audit_context *audit_context;
1730bfef93a5SAl Viro #ifdef CONFIG_AUDITSYSCALL
1731e1760bd5SEric W. Biederman 	kuid_t loginuid;
17324746ec5bSEric Paris 	unsigned int sessionid;
1733bfef93a5SAl Viro #endif
1734932ecebbSWill Drewry 	struct seccomp seccomp;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds /* Thread group tracking */
17371da177e4SLinus Torvalds    	u32 parent_exec_id;
17381da177e4SLinus Torvalds    	u32 self_exec_id;
173958568d2aSMiao Xie /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
174058568d2aSMiao Xie  * mempolicy */
17411da177e4SLinus Torvalds 	spinlock_t alloc_lock;
17421da177e4SLinus Torvalds 
1743b29739f9SIngo Molnar 	/* Protection of the PI data structures: */
17441d615482SThomas Gleixner 	raw_spinlock_t pi_lock;
1745b29739f9SIngo Molnar 
174676751049SPeter Zijlstra 	struct wake_q_node wake_q;
174776751049SPeter Zijlstra 
174823f78d4aSIngo Molnar #ifdef CONFIG_RT_MUTEXES
174923f78d4aSIngo Molnar 	/* PI waiters blocked on a rt_mutex held by this task */
1750fb00aca4SPeter Zijlstra 	struct rb_root pi_waiters;
1751fb00aca4SPeter Zijlstra 	struct rb_node *pi_waiters_leftmost;
175223f78d4aSIngo Molnar 	/* Deadlock detection and priority inheritance handling */
175323f78d4aSIngo Molnar 	struct rt_mutex_waiter *pi_blocked_on;
175423f78d4aSIngo Molnar #endif
175523f78d4aSIngo Molnar 
1756408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
1757408894eeSIngo Molnar 	/* mutex deadlock detection */
1758408894eeSIngo Molnar 	struct mutex_waiter *blocked_on;
1759408894eeSIngo Molnar #endif
1760de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
1761de30a2b3SIngo Molnar 	unsigned int irq_events;
1762de30a2b3SIngo Molnar 	unsigned long hardirq_enable_ip;
1763de30a2b3SIngo Molnar 	unsigned long hardirq_disable_ip;
1764fa1452e8SHiroshi Shimamoto 	unsigned int hardirq_enable_event;
1765de30a2b3SIngo Molnar 	unsigned int hardirq_disable_event;
1766fa1452e8SHiroshi Shimamoto 	int hardirqs_enabled;
1767de30a2b3SIngo Molnar 	int hardirq_context;
1768fa1452e8SHiroshi Shimamoto 	unsigned long softirq_disable_ip;
1769fa1452e8SHiroshi Shimamoto 	unsigned long softirq_enable_ip;
1770fa1452e8SHiroshi Shimamoto 	unsigned int softirq_disable_event;
1771fa1452e8SHiroshi Shimamoto 	unsigned int softirq_enable_event;
1772fa1452e8SHiroshi Shimamoto 	int softirqs_enabled;
1773de30a2b3SIngo Molnar 	int softirq_context;
1774de30a2b3SIngo Molnar #endif
1775fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1776bdb9441eSPeter Zijlstra # define MAX_LOCK_DEPTH 48UL
1777fbb9ce95SIngo Molnar 	u64 curr_chain_key;
1778fbb9ce95SIngo Molnar 	int lockdep_depth;
1779fbb9ce95SIngo Molnar 	unsigned int lockdep_recursion;
1780c7aceabaSRichard Kennedy 	struct held_lock held_locks[MAX_LOCK_DEPTH];
1781cf40bd16SNick Piggin 	gfp_t lockdep_reclaim_gfp;
1782fbb9ce95SIngo Molnar #endif
1783c6d30853SAndrey Ryabinin #ifdef CONFIG_UBSAN
1784c6d30853SAndrey Ryabinin 	unsigned int in_ubsan;
1785c6d30853SAndrey Ryabinin #endif
1786408894eeSIngo Molnar 
17871da177e4SLinus Torvalds /* journalling filesystem info */
17881da177e4SLinus Torvalds 	void *journal_info;
17891da177e4SLinus Torvalds 
1790d89d8796SNeil Brown /* stacked block device info */
1791bddd87c7SAkinobu Mita 	struct bio_list *bio_list;
1792d89d8796SNeil Brown 
179373c10101SJens Axboe #ifdef CONFIG_BLOCK
179473c10101SJens Axboe /* stack plugging */
179573c10101SJens Axboe 	struct blk_plug *plug;
179673c10101SJens Axboe #endif
179773c10101SJens Axboe 
17981da177e4SLinus Torvalds /* VM state */
17991da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	struct backing_dev_info *backing_dev_info;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	struct io_context *io_context;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	unsigned long ptrace_message;
18061da177e4SLinus Torvalds 	siginfo_t *last_siginfo; /* For ptrace use.  */
18077c3ab738SAndrew Morton 	struct task_io_accounting ioac;
18088f0ab514SJay Lan #if defined(CONFIG_TASK_XACCT)
18091da177e4SLinus Torvalds 	u64 acct_rss_mem1;	/* accumulated rss usage */
18101da177e4SLinus Torvalds 	u64 acct_vm_mem1;	/* accumulated virtual memory usage */
181149b5cf34SJonathan Lim 	cputime_t acct_timexpd;	/* stime + utime since last update */
18121da177e4SLinus Torvalds #endif
18131da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS
181458568d2aSMiao Xie 	nodemask_t mems_allowed;	/* Protected by alloc_lock */
1815cc9a6c87SMel Gorman 	seqcount_t mems_allowed_seq;	/* Seqence no to catch updates */
1816825a46afSPaul Jackson 	int cpuset_mem_spread_rotor;
18176adef3ebSJack Steiner 	int cpuset_slab_spread_rotor;
18181da177e4SLinus Torvalds #endif
1819ddbcc7e8SPaul Menage #ifdef CONFIG_CGROUPS
1820817929ecSPaul Menage 	/* Control Group info protected by css_set_lock */
18212c392b8cSArnd Bergmann 	struct css_set __rcu *cgroups;
1822817929ecSPaul Menage 	/* cg_list protected by css_set_lock and tsk->alloc_lock */
1823817929ecSPaul Menage 	struct list_head cg_list;
1824ddbcc7e8SPaul Menage #endif
1825e02737d5SFenghua Yu #ifdef CONFIG_INTEL_RDT_A
1826e02737d5SFenghua Yu 	int closid;
1827e02737d5SFenghua Yu #endif
182842b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
18290771dfefSIngo Molnar 	struct robust_list_head __user *robust_list;
183034f192c6SIngo Molnar #ifdef CONFIG_COMPAT
183134f192c6SIngo Molnar 	struct compat_robust_list_head __user *compat_robust_list;
183234f192c6SIngo Molnar #endif
1833c87e2837SIngo Molnar 	struct list_head pi_state_list;
1834c87e2837SIngo Molnar 	struct futex_pi_state *pi_state_cache;
183542b2dd0aSAlexey Dobriyan #endif
1836cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
18378dc85d54SPeter Zijlstra 	struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1838cdd6c482SIngo Molnar 	struct mutex perf_event_mutex;
1839cdd6c482SIngo Molnar 	struct list_head perf_event_list;
1840a63eaf34SPaul Mackerras #endif
18418f47b187SThomas Gleixner #ifdef CONFIG_DEBUG_PREEMPT
18428f47b187SThomas Gleixner 	unsigned long preempt_disable_ip;
18438f47b187SThomas Gleixner #endif
1844c7aceabaSRichard Kennedy #ifdef CONFIG_NUMA
184558568d2aSMiao Xie 	struct mempolicy *mempolicy;	/* Protected by alloc_lock */
1846c7aceabaSRichard Kennedy 	short il_next;
1847207205a2SEric Dumazet 	short pref_node_fork;
1848c7aceabaSRichard Kennedy #endif
1849cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
1850cbee9f88SPeter Zijlstra 	int numa_scan_seq;
1851cbee9f88SPeter Zijlstra 	unsigned int numa_scan_period;
1852598f0ec0SMel Gorman 	unsigned int numa_scan_period_max;
1853de1c9ce6SRik van Riel 	int numa_preferred_nid;
18546b9a7460SMel Gorman 	unsigned long numa_migrate_retry;
1855cbee9f88SPeter Zijlstra 	u64 node_stamp;			/* migration stamp  */
18567e2703e6SRik van Riel 	u64 last_task_numa_placement;
18577e2703e6SRik van Riel 	u64 last_sum_exec_runtime;
1858cbee9f88SPeter Zijlstra 	struct callback_head numa_work;
1859f809ca9aSMel Gorman 
18608c8a743cSPeter Zijlstra 	struct list_head numa_entry;
18618c8a743cSPeter Zijlstra 	struct numa_group *numa_group;
18628c8a743cSPeter Zijlstra 
1863745d6147SMel Gorman 	/*
186444dba3d5SIulia Manda 	 * numa_faults is an array split into four regions:
186544dba3d5SIulia Manda 	 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
186644dba3d5SIulia Manda 	 * in this precise order.
186744dba3d5SIulia Manda 	 *
186844dba3d5SIulia Manda 	 * faults_memory: Exponential decaying average of faults on a per-node
186944dba3d5SIulia Manda 	 * basis. Scheduling placement decisions are made based on these
187044dba3d5SIulia Manda 	 * counts. The values remain static for the duration of a PTE scan.
187144dba3d5SIulia Manda 	 * faults_cpu: Track the nodes the process was running on when a NUMA
187244dba3d5SIulia Manda 	 * hinting fault was incurred.
187344dba3d5SIulia Manda 	 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
187444dba3d5SIulia Manda 	 * during the current scan window. When the scan completes, the counts
187544dba3d5SIulia Manda 	 * in faults_memory and faults_cpu decay and these values are copied.
1876745d6147SMel Gorman 	 */
187744dba3d5SIulia Manda 	unsigned long *numa_faults;
187883e1d2cdSMel Gorman 	unsigned long total_numa_faults;
1879745d6147SMel Gorman 
1880745d6147SMel Gorman 	/*
188104bb2f94SRik van Riel 	 * numa_faults_locality tracks if faults recorded during the last
1882074c2381SMel Gorman 	 * scan window were remote/local or failed to migrate. The task scan
1883074c2381SMel Gorman 	 * period is adapted based on the locality of the faults with different
1884074c2381SMel Gorman 	 * weights depending on whether they were shared or private faults
188504bb2f94SRik van Riel 	 */
1886074c2381SMel Gorman 	unsigned long numa_faults_locality[3];
188704bb2f94SRik van Riel 
1888b32e86b4SIngo Molnar 	unsigned long numa_pages_migrated;
1889cbee9f88SPeter Zijlstra #endif /* CONFIG_NUMA_BALANCING */
1890cbee9f88SPeter Zijlstra 
189172b252aeSMel Gorman #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
189272b252aeSMel Gorman 	struct tlbflush_unmap_batch tlb_ubc;
189372b252aeSMel Gorman #endif
189472b252aeSMel Gorman 
1895e56d0903SIngo Molnar 	struct rcu_head rcu;
1896b92ce558SJens Axboe 
1897b92ce558SJens Axboe 	/*
1898b92ce558SJens Axboe 	 * cache last used pipe for splice
1899b92ce558SJens Axboe 	 */
1900b92ce558SJens Axboe 	struct pipe_inode_info *splice_pipe;
19015640f768SEric Dumazet 
19025640f768SEric Dumazet 	struct page_frag task_frag;
19035640f768SEric Dumazet 
1904ca74e92bSShailabh Nagar #ifdef	CONFIG_TASK_DELAY_ACCT
1905ca74e92bSShailabh Nagar 	struct task_delay_info *delays;
1906ca74e92bSShailabh Nagar #endif
1907f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1908f4f154fdSAkinobu Mita 	int make_it_fail;
1909f4f154fdSAkinobu Mita #endif
19109d823e8fSWu Fengguang 	/*
19119d823e8fSWu Fengguang 	 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
19129d823e8fSWu Fengguang 	 * balance_dirty_pages() for some dirty throttling pause
19139d823e8fSWu Fengguang 	 */
19149d823e8fSWu Fengguang 	int nr_dirtied;
19159d823e8fSWu Fengguang 	int nr_dirtied_pause;
191683712358SWu Fengguang 	unsigned long dirty_paused_when; /* start of a write-and-pause period */
19179d823e8fSWu Fengguang 
19189745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
19199745512cSArjan van de Ven 	int latency_record_count;
19209745512cSArjan van de Ven 	struct latency_record latency_record[LT_SAVECOUNT];
19219745512cSArjan van de Ven #endif
19226976675dSArjan van de Ven 	/*
19236976675dSArjan van de Ven 	 * time slack values; these are used to round up poll() and
19246976675dSArjan van de Ven 	 * select() etc timeout values. These are in nanoseconds.
19256976675dSArjan van de Ven 	 */
1926da8b44d5SJohn Stultz 	u64 timer_slack_ns;
1927da8b44d5SJohn Stultz 	u64 default_timer_slack_ns;
1928f8d570a4SDavid Miller 
19290b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
19300b24beccSAndrey Ryabinin 	unsigned int kasan_depth;
19310b24beccSAndrey Ryabinin #endif
1932fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
19333ad2f3fbSDaniel Mack 	/* Index of current stored address in ret_stack */
1934f201ae23SFrederic Weisbecker 	int curr_ret_stack;
1935f201ae23SFrederic Weisbecker 	/* Stack of return addresses for return function tracing */
1936f201ae23SFrederic Weisbecker 	struct ftrace_ret_stack	*ret_stack;
19378aef2d28SSteven Rostedt 	/* time stamp for last schedule */
19388aef2d28SSteven Rostedt 	unsigned long long ftrace_timestamp;
1939f201ae23SFrederic Weisbecker 	/*
1940f201ae23SFrederic Weisbecker 	 * Number of functions that haven't been traced
1941f201ae23SFrederic Weisbecker 	 * because of depth overrun.
1942f201ae23SFrederic Weisbecker 	 */
1943f201ae23SFrederic Weisbecker 	atomic_t trace_overrun;
1944380c4b14SFrederic Weisbecker 	/* Pause for the tracing */
1945380c4b14SFrederic Weisbecker 	atomic_t tracing_graph_pause;
1946f201ae23SFrederic Weisbecker #endif
1947ea4e2bc4SSteven Rostedt #ifdef CONFIG_TRACING
1948ea4e2bc4SSteven Rostedt 	/* state flags for use by tracers */
1949ea4e2bc4SSteven Rostedt 	unsigned long trace;
1950b1cff0adSSteven Rostedt 	/* bitmask and counter of trace recursion */
1951261842b7SSteven Rostedt 	unsigned long trace_recursion;
1952261842b7SSteven Rostedt #endif /* CONFIG_TRACING */
19535c9a8750SDmitry Vyukov #ifdef CONFIG_KCOV
19545c9a8750SDmitry Vyukov 	/* Coverage collection mode enabled for this task (0 if disabled). */
19555c9a8750SDmitry Vyukov 	enum kcov_mode kcov_mode;
19565c9a8750SDmitry Vyukov 	/* Size of the kcov_area. */
19575c9a8750SDmitry Vyukov 	unsigned	kcov_size;
19585c9a8750SDmitry Vyukov 	/* Buffer for coverage collection. */
19595c9a8750SDmitry Vyukov 	void		*kcov_area;
19605c9a8750SDmitry Vyukov 	/* kcov desciptor wired with this task or NULL. */
19615c9a8750SDmitry Vyukov 	struct kcov	*kcov;
19625c9a8750SDmitry Vyukov #endif
19636f185c29SVladimir Davydov #ifdef CONFIG_MEMCG
1964626ebc41STejun Heo 	struct mem_cgroup *memcg_in_oom;
1965626ebc41STejun Heo 	gfp_t memcg_oom_gfp_mask;
1966626ebc41STejun Heo 	int memcg_oom_order;
1967b23afb93STejun Heo 
1968b23afb93STejun Heo 	/* number of pages to reclaim on returning to userland */
1969b23afb93STejun Heo 	unsigned int memcg_nr_pages_over_high;
1970569b846dSKAMEZAWA Hiroyuki #endif
19710326f5a9SSrikar Dronamraju #ifdef CONFIG_UPROBES
19720326f5a9SSrikar Dronamraju 	struct uprobe_task *utask;
19730326f5a9SSrikar Dronamraju #endif
1974cafe5635SKent Overstreet #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1975cafe5635SKent Overstreet 	unsigned int	sequential_io;
1976cafe5635SKent Overstreet 	unsigned int	sequential_io_avg;
1977cafe5635SKent Overstreet #endif
19788eb23b9fSPeter Zijlstra #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
19798eb23b9fSPeter Zijlstra 	unsigned long	task_state_change;
19808eb23b9fSPeter Zijlstra #endif
19818bcbde54SDavid Hildenbrand 	int pagefault_disabled;
198203049269SMichal Hocko #ifdef CONFIG_MMU
198329c696e1SVladimir Davydov 	struct task_struct *oom_reaper_list;
198403049269SMichal Hocko #endif
1985ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
1986ba14a194SAndy Lutomirski 	struct vm_struct *stack_vm_area;
1987ba14a194SAndy Lutomirski #endif
198868f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
198968f24b08SAndy Lutomirski 	/* A live task holds one reference. */
199068f24b08SAndy Lutomirski 	atomic_t stack_refcount;
199168f24b08SAndy Lutomirski #endif
19920c8c0f03SDave Hansen /* CPU-specific state of this task */
19930c8c0f03SDave Hansen 	struct thread_struct thread;
19940c8c0f03SDave Hansen /*
19950c8c0f03SDave Hansen  * WARNING: on x86, 'thread_struct' contains a variable-sized
19960c8c0f03SDave Hansen  * structure.  It *MUST* be at the end of 'task_struct'.
19970c8c0f03SDave Hansen  *
19980c8c0f03SDave Hansen  * Do not put anything below here!
19990c8c0f03SDave Hansen  */
20001da177e4SLinus Torvalds };
20011da177e4SLinus Torvalds 
20025aaeb5c0SIngo Molnar #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
20035aaeb5c0SIngo Molnar extern int arch_task_struct_size __read_mostly;
20045aaeb5c0SIngo Molnar #else
20055aaeb5c0SIngo Molnar # define arch_task_struct_size (sizeof(struct task_struct))
20065aaeb5c0SIngo Molnar #endif
20070c8c0f03SDave Hansen 
2008ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
2009ba14a194SAndy Lutomirski static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
2010ba14a194SAndy Lutomirski {
2011ba14a194SAndy Lutomirski 	return t->stack_vm_area;
2012ba14a194SAndy Lutomirski }
2013ba14a194SAndy Lutomirski #else
2014ba14a194SAndy Lutomirski static inline struct vm_struct *task_stack_vm_area(const struct task_struct *t)
2015ba14a194SAndy Lutomirski {
2016ba14a194SAndy Lutomirski 	return NULL;
2017ba14a194SAndy Lutomirski }
2018ba14a194SAndy Lutomirski #endif
2019ba14a194SAndy Lutomirski 
202076e6eee0SRusty Russell /* Future-safe accessor for struct task_struct's cpus_allowed. */
2021a4636818SRusty Russell #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
202276e6eee0SRusty Russell 
202350605ffbSThomas Gleixner static inline int tsk_nr_cpus_allowed(struct task_struct *p)
202450605ffbSThomas Gleixner {
202550605ffbSThomas Gleixner 	return p->nr_cpus_allowed;
202650605ffbSThomas Gleixner }
202750605ffbSThomas Gleixner 
20286688cc05SPeter Zijlstra #define TNF_MIGRATED	0x01
20296688cc05SPeter Zijlstra #define TNF_NO_GROUP	0x02
2030dabe1d99SRik van Riel #define TNF_SHARED	0x04
203104bb2f94SRik van Riel #define TNF_FAULT_LOCAL	0x08
2032074c2381SMel Gorman #define TNF_MIGRATE_FAIL 0x10
20336688cc05SPeter Zijlstra 
2034b18dc5f2SMichal Hocko static inline bool in_vfork(struct task_struct *tsk)
2035b18dc5f2SMichal Hocko {
2036b18dc5f2SMichal Hocko 	bool ret;
2037b18dc5f2SMichal Hocko 
2038b18dc5f2SMichal Hocko 	/*
2039b18dc5f2SMichal Hocko 	 * need RCU to access ->real_parent if CLONE_VM was used along with
2040b18dc5f2SMichal Hocko 	 * CLONE_PARENT.
2041b18dc5f2SMichal Hocko 	 *
2042b18dc5f2SMichal Hocko 	 * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
2043b18dc5f2SMichal Hocko 	 * imply CLONE_VM
2044b18dc5f2SMichal Hocko 	 *
2045b18dc5f2SMichal Hocko 	 * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
2046b18dc5f2SMichal Hocko 	 * ->real_parent is not necessarily the task doing vfork(), so in
2047b18dc5f2SMichal Hocko 	 * theory we can't rely on task_lock() if we want to dereference it.
2048b18dc5f2SMichal Hocko 	 *
2049b18dc5f2SMichal Hocko 	 * And in this case we can't trust the real_parent->mm == tsk->mm
2050b18dc5f2SMichal Hocko 	 * check, it can be false negative. But we do not care, if init or
2051b18dc5f2SMichal Hocko 	 * another oom-unkillable task does this it should blame itself.
2052b18dc5f2SMichal Hocko 	 */
2053b18dc5f2SMichal Hocko 	rcu_read_lock();
2054b18dc5f2SMichal Hocko 	ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
2055b18dc5f2SMichal Hocko 	rcu_read_unlock();
2056b18dc5f2SMichal Hocko 
2057b18dc5f2SMichal Hocko 	return ret;
2058b18dc5f2SMichal Hocko }
2059b18dc5f2SMichal Hocko 
2060cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
20616688cc05SPeter Zijlstra extern void task_numa_fault(int last_node, int node, int pages, int flags);
2062e29cf08bSMel Gorman extern pid_t task_numa_group_id(struct task_struct *p);
20631a687c2eSMel Gorman extern void set_numabalancing_state(bool enabled);
206482727018SRik van Riel extern void task_numa_free(struct task_struct *p);
206510f39042SRik van Riel extern bool should_numa_migrate_memory(struct task_struct *p, struct page *page,
206610f39042SRik van Riel 					int src_nid, int dst_cpu);
2067cbee9f88SPeter Zijlstra #else
2068ac8e895bSMel Gorman static inline void task_numa_fault(int last_node, int node, int pages,
20696688cc05SPeter Zijlstra 				   int flags)
2070cbee9f88SPeter Zijlstra {
2071cbee9f88SPeter Zijlstra }
2072e29cf08bSMel Gorman static inline pid_t task_numa_group_id(struct task_struct *p)
2073e29cf08bSMel Gorman {
2074e29cf08bSMel Gorman 	return 0;
2075e29cf08bSMel Gorman }
20761a687c2eSMel Gorman static inline void set_numabalancing_state(bool enabled)
20771a687c2eSMel Gorman {
20781a687c2eSMel Gorman }
207982727018SRik van Riel static inline void task_numa_free(struct task_struct *p)
208082727018SRik van Riel {
208182727018SRik van Riel }
208210f39042SRik van Riel static inline bool should_numa_migrate_memory(struct task_struct *p,
208310f39042SRik van Riel 				struct page *page, int src_nid, int dst_cpu)
208410f39042SRik van Riel {
208510f39042SRik van Riel 	return true;
208610f39042SRik van Riel }
2087cbee9f88SPeter Zijlstra #endif
2088cbee9f88SPeter Zijlstra 
2089e868171aSAlexey Dobriyan static inline struct pid *task_pid(struct task_struct *task)
209022c935f4SEric W. Biederman {
209122c935f4SEric W. Biederman 	return task->pids[PIDTYPE_PID].pid;
209222c935f4SEric W. Biederman }
209322c935f4SEric W. Biederman 
2094e868171aSAlexey Dobriyan static inline struct pid *task_tgid(struct task_struct *task)
209522c935f4SEric W. Biederman {
209622c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_PID].pid;
209722c935f4SEric W. Biederman }
209822c935f4SEric W. Biederman 
20996dda81f4SOleg Nesterov /*
21006dda81f4SOleg Nesterov  * Without tasklist or rcu lock it is not safe to dereference
21016dda81f4SOleg Nesterov  * the result of task_pgrp/task_session even if task == current,
21026dda81f4SOleg Nesterov  * we can race with another thread doing sys_setsid/sys_setpgid.
21036dda81f4SOleg Nesterov  */
2104e868171aSAlexey Dobriyan static inline struct pid *task_pgrp(struct task_struct *task)
210522c935f4SEric W. Biederman {
210622c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_PGID].pid;
210722c935f4SEric W. Biederman }
210822c935f4SEric W. Biederman 
2109e868171aSAlexey Dobriyan static inline struct pid *task_session(struct task_struct *task)
211022c935f4SEric W. Biederman {
211122c935f4SEric W. Biederman 	return task->group_leader->pids[PIDTYPE_SID].pid;
211222c935f4SEric W. Biederman }
211322c935f4SEric W. Biederman 
21147af57294SPavel Emelyanov struct pid_namespace;
21157af57294SPavel Emelyanov 
21167af57294SPavel Emelyanov /*
21177af57294SPavel Emelyanov  * the helpers to get the task's different pids as they are seen
21187af57294SPavel Emelyanov  * from various namespaces
21197af57294SPavel Emelyanov  *
21207af57294SPavel Emelyanov  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
212144c4e1b2SEric W. Biederman  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
212244c4e1b2SEric W. Biederman  *                     current.
21237af57294SPavel Emelyanov  * task_xid_nr_ns()  : id seen from the ns specified;
21247af57294SPavel Emelyanov  *
21257af57294SPavel Emelyanov  * set_task_vxid()   : assigns a virtual id to a task;
21267af57294SPavel Emelyanov  *
21277af57294SPavel Emelyanov  * see also pid_nr() etc in include/linux/pid.h
21287af57294SPavel Emelyanov  */
212952ee2dfdSOleg Nesterov pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
213052ee2dfdSOleg Nesterov 			struct pid_namespace *ns);
21317af57294SPavel Emelyanov 
2132e868171aSAlexey Dobriyan static inline pid_t task_pid_nr(struct task_struct *tsk)
21337af57294SPavel Emelyanov {
21347af57294SPavel Emelyanov 	return tsk->pid;
21357af57294SPavel Emelyanov }
21367af57294SPavel Emelyanov 
213752ee2dfdSOleg Nesterov static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
213852ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
213952ee2dfdSOleg Nesterov {
214052ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
214152ee2dfdSOleg Nesterov }
21427af57294SPavel Emelyanov 
21437af57294SPavel Emelyanov static inline pid_t task_pid_vnr(struct task_struct *tsk)
21447af57294SPavel Emelyanov {
214552ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
21467af57294SPavel Emelyanov }
21477af57294SPavel Emelyanov 
21487af57294SPavel Emelyanov 
2149e868171aSAlexey Dobriyan static inline pid_t task_tgid_nr(struct task_struct *tsk)
21507af57294SPavel Emelyanov {
21517af57294SPavel Emelyanov 	return tsk->tgid;
21527af57294SPavel Emelyanov }
21537af57294SPavel Emelyanov 
21542f2a3a46SPavel Emelyanov pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
21557af57294SPavel Emelyanov 
21567af57294SPavel Emelyanov static inline pid_t task_tgid_vnr(struct task_struct *tsk)
21577af57294SPavel Emelyanov {
21587af57294SPavel Emelyanov 	return pid_vnr(task_tgid(tsk));
21597af57294SPavel Emelyanov }
21607af57294SPavel Emelyanov 
21617af57294SPavel Emelyanov 
216280e0b6e8SRichard Guy Briggs static inline int pid_alive(const struct task_struct *p);
2163ad36d282SRichard Guy Briggs static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
2164ad36d282SRichard Guy Briggs {
2165ad36d282SRichard Guy Briggs 	pid_t pid = 0;
2166ad36d282SRichard Guy Briggs 
2167ad36d282SRichard Guy Briggs 	rcu_read_lock();
2168ad36d282SRichard Guy Briggs 	if (pid_alive(tsk))
2169ad36d282SRichard Guy Briggs 		pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
2170ad36d282SRichard Guy Briggs 	rcu_read_unlock();
2171ad36d282SRichard Guy Briggs 
2172ad36d282SRichard Guy Briggs 	return pid;
2173ad36d282SRichard Guy Briggs }
2174ad36d282SRichard Guy Briggs 
2175ad36d282SRichard Guy Briggs static inline pid_t task_ppid_nr(const struct task_struct *tsk)
2176ad36d282SRichard Guy Briggs {
2177ad36d282SRichard Guy Briggs 	return task_ppid_nr_ns(tsk, &init_pid_ns);
2178ad36d282SRichard Guy Briggs }
2179ad36d282SRichard Guy Briggs 
218052ee2dfdSOleg Nesterov static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
218152ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
21827af57294SPavel Emelyanov {
218352ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
21847af57294SPavel Emelyanov }
21857af57294SPavel Emelyanov 
21867af57294SPavel Emelyanov static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
21877af57294SPavel Emelyanov {
218852ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
21897af57294SPavel Emelyanov }
21907af57294SPavel Emelyanov 
21917af57294SPavel Emelyanov 
219252ee2dfdSOleg Nesterov static inline pid_t task_session_nr_ns(struct task_struct *tsk,
219352ee2dfdSOleg Nesterov 					struct pid_namespace *ns)
21947af57294SPavel Emelyanov {
219552ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
21967af57294SPavel Emelyanov }
21977af57294SPavel Emelyanov 
21987af57294SPavel Emelyanov static inline pid_t task_session_vnr(struct task_struct *tsk)
21997af57294SPavel Emelyanov {
220052ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
22017af57294SPavel Emelyanov }
22027af57294SPavel Emelyanov 
22031b0f7ffdSOleg Nesterov /* obsolete, do not use */
22041b0f7ffdSOleg Nesterov static inline pid_t task_pgrp_nr(struct task_struct *tsk)
22051b0f7ffdSOleg Nesterov {
22061b0f7ffdSOleg Nesterov 	return task_pgrp_nr_ns(tsk, &init_pid_ns);
22071b0f7ffdSOleg Nesterov }
22087af57294SPavel Emelyanov 
22091da177e4SLinus Torvalds /**
22101da177e4SLinus Torvalds  * pid_alive - check that a task structure is not stale
22111da177e4SLinus Torvalds  * @p: Task structure to be checked.
22121da177e4SLinus Torvalds  *
22131da177e4SLinus Torvalds  * Test if a process is not yet dead (at most zombie state)
22141da177e4SLinus Torvalds  * If pid_alive fails, then pointers within the task structure
22151da177e4SLinus Torvalds  * can be stale and must not be dereferenced.
2216e69f6186SYacine Belkadi  *
2217e69f6186SYacine Belkadi  * Return: 1 if the process is alive. 0 otherwise.
22181da177e4SLinus Torvalds  */
2219ad36d282SRichard Guy Briggs static inline int pid_alive(const struct task_struct *p)
22201da177e4SLinus Torvalds {
222192476d7fSEric W. Biederman 	return p->pids[PIDTYPE_PID].pid != NULL;
22221da177e4SLinus Torvalds }
22231da177e4SLinus Torvalds 
2224f400e198SSukadev Bhattiprolu /**
2225570f5241SSergey Senozhatsky  * is_global_init - check if a task structure is init. Since init
2226570f5241SSergey Senozhatsky  * is free to have sub-threads we need to check tgid.
22273260259fSHenne  * @tsk: Task structure to be checked.
22283260259fSHenne  *
22293260259fSHenne  * Check if a task structure is the first user space task the kernel created.
2230e69f6186SYacine Belkadi  *
2231e69f6186SYacine Belkadi  * Return: 1 if the task structure is init. 0 otherwise.
2232f400e198SSukadev Bhattiprolu  */
2233e868171aSAlexey Dobriyan static inline int is_global_init(struct task_struct *tsk)
2234b461cc03SPavel Emelyanov {
2235570f5241SSergey Senozhatsky 	return task_tgid_nr(tsk) == 1;
2236b461cc03SPavel Emelyanov }
2237b460cbc5SSerge E. Hallyn 
22389ec52099SCedric Le Goater extern struct pid *cad_pid;
22399ec52099SCedric Le Goater 
22401da177e4SLinus Torvalds extern void free_task(struct task_struct *tsk);
22411da177e4SLinus Torvalds #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
2242e56d0903SIngo Molnar 
2243158d9ebdSAndrew Morton extern void __put_task_struct(struct task_struct *t);
2244e56d0903SIngo Molnar 
2245e56d0903SIngo Molnar static inline void put_task_struct(struct task_struct *t)
2246e56d0903SIngo Molnar {
2247e56d0903SIngo Molnar 	if (atomic_dec_and_test(&t->usage))
22488c7904a0SEric W. Biederman 		__put_task_struct(t);
2249e56d0903SIngo Molnar }
22501da177e4SLinus Torvalds 
2251150593bfSOleg Nesterov struct task_struct *task_rcu_dereference(struct task_struct **ptask);
2252150593bfSOleg Nesterov struct task_struct *try_get_task_struct(struct task_struct **ptask);
2253150593bfSOleg Nesterov 
22546a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
22556a61671bSFrederic Weisbecker extern void task_cputime(struct task_struct *t,
22566a61671bSFrederic Weisbecker 			 cputime_t *utime, cputime_t *stime);
2257*16a6d9beSFrederic Weisbecker extern u64 task_gtime(struct task_struct *t);
22586a61671bSFrederic Weisbecker #else
22596fac4829SFrederic Weisbecker static inline void task_cputime(struct task_struct *t,
22606fac4829SFrederic Weisbecker 				cputime_t *utime, cputime_t *stime)
22616fac4829SFrederic Weisbecker {
22626fac4829SFrederic Weisbecker 	*utime = t->utime;
22636fac4829SFrederic Weisbecker 	*stime = t->stime;
22646fac4829SFrederic Weisbecker }
22656fac4829SFrederic Weisbecker 
2266*16a6d9beSFrederic Weisbecker static inline u64 task_gtime(struct task_struct *t)
22676a61671bSFrederic Weisbecker {
22686a61671bSFrederic Weisbecker 	return t->gtime;
22696a61671bSFrederic Weisbecker }
22706a61671bSFrederic Weisbecker #endif
227140565b5aSStanislaw Gruszka 
227240565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
2273b31dc66aSJens Axboe static inline void task_cputime_scaled(struct task_struct *t,
2274b31dc66aSJens Axboe 				       cputime_t *utimescaled,
2275b31dc66aSJens Axboe 				       cputime_t *stimescaled)
2276b31dc66aSJens Axboe {
22774db96cf0SAndi Kleen 	*utimescaled = t->utimescaled;
227861a87122SThomas Gleixner 	*stimescaled = t->stimescaled;
227958a69cb4STejun Heo }
228040565b5aSStanislaw Gruszka #else
228140565b5aSStanislaw Gruszka static inline void task_cputime_scaled(struct task_struct *t,
228240565b5aSStanislaw Gruszka 				       cputime_t *utimescaled,
228340565b5aSStanislaw Gruszka 				       cputime_t *stimescaled)
22841da177e4SLinus Torvalds {
228540565b5aSStanislaw Gruszka 	task_cputime(t, utimescaled, stimescaled);
22861da177e4SLinus Torvalds }
22871da177e4SLinus Torvalds #endif
228840565b5aSStanislaw Gruszka 
22891da177e4SLinus Torvalds extern void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
22901da177e4SLinus Torvalds extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds /*
22931da177e4SLinus Torvalds  * Per process flags
22941da177e4SLinus Torvalds  */
2295c1de45caSPeter Zijlstra #define PF_IDLE		0x00000002	/* I am an IDLE thread */
22961da177e4SLinus Torvalds #define PF_EXITING	0x00000004	/* getting shut down */
22971da177e4SLinus Torvalds #define PF_EXITPIDONE	0x00000008	/* pi exit done on shut down */
22981da177e4SLinus Torvalds #define PF_VCPU		0x00000010	/* I'm a virtual CPU */
22991da177e4SLinus Torvalds #define PF_WQ_WORKER	0x00000020	/* I'm a workqueue worker */
23001da177e4SLinus Torvalds #define PF_FORKNOEXEC	0x00000040	/* forked but didn't exec */
23011da177e4SLinus Torvalds #define PF_MCE_PROCESS  0x00000080      /* process policy on mce errors */
23021da177e4SLinus Torvalds #define PF_SUPERPRIV	0x00000100	/* used super-user privileges */
23031da177e4SLinus Torvalds #define PF_DUMPCORE	0x00000200	/* dumped core */
23041da177e4SLinus Torvalds #define PF_SIGNALED	0x00000400	/* killed by a signal */
23051da177e4SLinus Torvalds #define PF_MEMALLOC	0x00000800	/* Allocating memory */
23061da177e4SLinus Torvalds #define PF_NPROC_EXCEEDED 0x00001000	/* set_user noticed that RLIMIT_NPROC was exceeded */
23071da177e4SLinus Torvalds #define PF_USED_MATH	0x00002000	/* if unset the fpu must be initialized before use */
23081da177e4SLinus Torvalds #define PF_USED_ASYNC	0x00004000	/* used async_schedule*(), used by module init */
23091da177e4SLinus Torvalds #define PF_NOFREEZE	0x00008000	/* this thread should not be frozen */
23101da177e4SLinus Torvalds #define PF_FROZEN	0x00010000	/* frozen for system suspend */
23111da177e4SLinus Torvalds #define PF_FSTRANS	0x00020000	/* inside a filesystem transaction */
23121da177e4SLinus Torvalds #define PF_KSWAPD	0x00040000	/* I am kswapd */
231321caf2fcSMing Lei #define PF_MEMALLOC_NOIO 0x00080000	/* Allocating memory without IO involved */
23141da177e4SLinus Torvalds #define PF_LESS_THROTTLE 0x00100000	/* Throttle me less: I clean memory */
23151da177e4SLinus Torvalds #define PF_KTHREAD	0x00200000	/* I am a kernel thread */
23161da177e4SLinus Torvalds #define PF_RANDOMIZE	0x00400000	/* randomize virtual address space */
23171da177e4SLinus Torvalds #define PF_SWAPWRITE	0x00800000	/* Allowed to write to swap */
231814a40ffcSTejun Heo #define PF_NO_SETAFFINITY 0x04000000	/* Userland is not allowed to meddle with cpus_allowed */
23191da177e4SLinus Torvalds #define PF_MCE_EARLY    0x08000000      /* Early kill for mce process policy */
23201da177e4SLinus Torvalds #define PF_MUTEX_TESTER	0x20000000	/* Thread belongs to the rt mutex tester */
23211da177e4SLinus Torvalds #define PF_FREEZER_SKIP	0x40000000	/* Freezer should not count it as freezable */
23222b44c4dbSColin Cross #define PF_SUSPEND_TASK 0x80000000      /* this thread called freeze_processes and should not be frozen */
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds /*
23251da177e4SLinus Torvalds  * Only the _current_ task can read/write to tsk->flags, but other
23261da177e4SLinus Torvalds  * tasks can access tsk->flags in readonly mode for example
23271da177e4SLinus Torvalds  * with tsk_used_math (like during threaded core dumping).
23281da177e4SLinus Torvalds  * There is however an exception to this rule during ptrace
23291da177e4SLinus Torvalds  * or during fork: the ptracer task is allowed to write to the
23301da177e4SLinus Torvalds  * child->flags of its traced child (same goes for fork, the parent
23311da177e4SLinus Torvalds  * can write to the child->flags), because we're guaranteed the
23321da177e4SLinus Torvalds  * child is not running and in turn not changing child->flags
23331da177e4SLinus Torvalds  * at the same time the parent does it.
23341da177e4SLinus Torvalds  */
23351da177e4SLinus Torvalds #define clear_stopped_child_used_math(child) do { (child)->flags &= ~PF_USED_MATH; } while (0)
23361da177e4SLinus Torvalds #define set_stopped_child_used_math(child) do { (child)->flags |= PF_USED_MATH; } while (0)
23371da177e4SLinus Torvalds #define clear_used_math() clear_stopped_child_used_math(current)
23381da177e4SLinus Torvalds #define set_used_math() set_stopped_child_used_math(current)
23391da177e4SLinus Torvalds #define conditional_stopped_child_used_math(condition, child) \
23401da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
23411da177e4SLinus Torvalds #define conditional_used_math(condition) \
23421da177e4SLinus Torvalds 	conditional_stopped_child_used_math(condition, current)
23431da177e4SLinus Torvalds #define copy_to_stopped_child_used_math(child) \
23441da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
23451da177e4SLinus Torvalds /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
23461da177e4SLinus Torvalds #define tsk_used_math(p) ((p)->flags & PF_USED_MATH)
23471da177e4SLinus Torvalds #define used_math() tsk_used_math(current)
23481da177e4SLinus Torvalds 
2349934f3072SJunxiao Bi /* __GFP_IO isn't allowed if PF_MEMALLOC_NOIO is set in current->flags
2350934f3072SJunxiao Bi  * __GFP_FS is also cleared as it implies __GFP_IO.
2351934f3072SJunxiao Bi  */
235221caf2fcSMing Lei static inline gfp_t memalloc_noio_flags(gfp_t flags)
235321caf2fcSMing Lei {
235421caf2fcSMing Lei 	if (unlikely(current->flags & PF_MEMALLOC_NOIO))
2355934f3072SJunxiao Bi 		flags &= ~(__GFP_IO | __GFP_FS);
235621caf2fcSMing Lei 	return flags;
235721caf2fcSMing Lei }
235821caf2fcSMing Lei 
235921caf2fcSMing Lei static inline unsigned int memalloc_noio_save(void)
236021caf2fcSMing Lei {
236121caf2fcSMing Lei 	unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
236221caf2fcSMing Lei 	current->flags |= PF_MEMALLOC_NOIO;
236321caf2fcSMing Lei 	return flags;
236421caf2fcSMing Lei }
236521caf2fcSMing Lei 
236621caf2fcSMing Lei static inline void memalloc_noio_restore(unsigned int flags)
236721caf2fcSMing Lei {
236821caf2fcSMing Lei 	current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
236921caf2fcSMing Lei }
237021caf2fcSMing Lei 
23711d4457f9SKees Cook /* Per-process atomic flags. */
2372a2b86f77SZefan Li #define PFA_NO_NEW_PRIVS 0	/* May not gain new privileges. */
23732ad654bcSZefan Li #define PFA_SPREAD_PAGE  1      /* Spread page cache over cpuset */
23742ad654bcSZefan Li #define PFA_SPREAD_SLAB  2      /* Spread some slab caches over cpuset */
237577ed2c57STetsuo Handa #define PFA_LMK_WAITING  3      /* Lowmemorykiller is waiting */
23761d4457f9SKees Cook 
23771d4457f9SKees Cook 
2378e0e5070bSZefan Li #define TASK_PFA_TEST(name, func)					\
2379e0e5070bSZefan Li 	static inline bool task_##func(struct task_struct *p)		\
2380e0e5070bSZefan Li 	{ return test_bit(PFA_##name, &p->atomic_flags); }
2381e0e5070bSZefan Li #define TASK_PFA_SET(name, func)					\
2382e0e5070bSZefan Li 	static inline void task_set_##func(struct task_struct *p)	\
2383e0e5070bSZefan Li 	{ set_bit(PFA_##name, &p->atomic_flags); }
2384e0e5070bSZefan Li #define TASK_PFA_CLEAR(name, func)					\
2385e0e5070bSZefan Li 	static inline void task_clear_##func(struct task_struct *p)	\
2386e0e5070bSZefan Li 	{ clear_bit(PFA_##name, &p->atomic_flags); }
23871d4457f9SKees Cook 
2388e0e5070bSZefan Li TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
2389e0e5070bSZefan Li TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
23901d4457f9SKees Cook 
23912ad654bcSZefan Li TASK_PFA_TEST(SPREAD_PAGE, spread_page)
23922ad654bcSZefan Li TASK_PFA_SET(SPREAD_PAGE, spread_page)
23932ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
23942ad654bcSZefan Li 
23952ad654bcSZefan Li TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
23962ad654bcSZefan Li TASK_PFA_SET(SPREAD_SLAB, spread_slab)
23972ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
2398544b2c91STejun Heo 
239977ed2c57STetsuo Handa TASK_PFA_TEST(LMK_WAITING, lmk_waiting)
240077ed2c57STetsuo Handa TASK_PFA_SET(LMK_WAITING, lmk_waiting)
240177ed2c57STetsuo Handa 
240239efa3efSTejun Heo /*
2403e5c1902eSTejun Heo  * task->jobctl flags
2404d79fdd6dSTejun Heo  */
2405ee77f075SOleg Nesterov #define JOBCTL_STOP_SIGMASK	0xffff	/* signr of the last group stop */
2406e5c1902eSTejun Heo 
2407f41d911fSPaul E. McKenney #define JOBCTL_STOP_DEQUEUED_BIT 16	/* stop signal dequeued */
2408a8f072c1STejun Heo #define JOBCTL_STOP_PENDING_BIT	17	/* task should stop for group stop */
2409a8f072c1STejun Heo #define JOBCTL_STOP_CONSUME_BIT	18	/* consume group stop count */
2410a8f072c1STejun Heo #define JOBCTL_TRAP_STOP_BIT	19	/* trap for STOP */
241173ddff2bSTejun Heo #define JOBCTL_TRAP_NOTIFY_BIT	20	/* trap for NOTIFY */
2412fb1d910cSTejun Heo #define JOBCTL_TRAPPING_BIT	21	/* switching to TRACED */
2413a8f072c1STejun Heo #define JOBCTL_LISTENING_BIT	22	/* ptracer is listening for events */
2414a8f072c1STejun Heo 
2415b76808e6SPalmer Dabbelt #define JOBCTL_STOP_DEQUEUED	(1UL << JOBCTL_STOP_DEQUEUED_BIT)
2416b76808e6SPalmer Dabbelt #define JOBCTL_STOP_PENDING	(1UL << JOBCTL_STOP_PENDING_BIT)
2417b76808e6SPalmer Dabbelt #define JOBCTL_STOP_CONSUME	(1UL << JOBCTL_STOP_CONSUME_BIT)
2418b76808e6SPalmer Dabbelt #define JOBCTL_TRAP_STOP	(1UL << JOBCTL_TRAP_STOP_BIT)
2419b76808e6SPalmer Dabbelt #define JOBCTL_TRAP_NOTIFY	(1UL << JOBCTL_TRAP_NOTIFY_BIT)
2420b76808e6SPalmer Dabbelt #define JOBCTL_TRAPPING		(1UL << JOBCTL_TRAPPING_BIT)
2421b76808e6SPalmer Dabbelt #define JOBCTL_LISTENING	(1UL << JOBCTL_LISTENING_BIT)
2422a8f072c1STejun Heo 
2423fb1d910cSTejun Heo #define JOBCTL_TRAP_MASK	(JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
242473ddff2bSTejun Heo #define JOBCTL_PENDING_MASK	(JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
24253759a0d9STejun Heo 
24267dd3db54STejun Heo extern bool task_set_jobctl_pending(struct task_struct *task,
2427b76808e6SPalmer Dabbelt 				    unsigned long mask);
242873ddff2bSTejun Heo extern void task_clear_jobctl_trapping(struct task_struct *task);
24293759a0d9STejun Heo extern void task_clear_jobctl_pending(struct task_struct *task,
2430b76808e6SPalmer Dabbelt 				      unsigned long mask);
243139efa3efSTejun Heo 
2432f41d911fSPaul E. McKenney static inline void rcu_copy_process(struct task_struct *p)
2433f41d911fSPaul E. McKenney {
24341da177e4SLinus Torvalds #ifdef CONFIG_PREEMPT_RCU
2435f41d911fSPaul E. McKenney 	p->rcu_read_lock_nesting = 0;
24361d082fd0SPaul E. McKenney 	p->rcu_read_unlock_special.s = 0;
2437dd5d19baSPaul E. McKenney 	p->rcu_blocked_node = NULL;
2438f41d911fSPaul E. McKenney 	INIT_LIST_HEAD(&p->rcu_node_entry);
24398315f422SPaul E. McKenney #endif /* #ifdef CONFIG_PREEMPT_RCU */
24408315f422SPaul E. McKenney #ifdef CONFIG_TASKS_RCU
24418315f422SPaul E. McKenney 	p->rcu_tasks_holdout = false;
24428315f422SPaul E. McKenney 	INIT_LIST_HEAD(&p->rcu_tasks_holdout_list);
2443176f8f7aSPaul E. McKenney 	p->rcu_tasks_idle_cpu = -1;
24448315f422SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_RCU */
2445f41d911fSPaul E. McKenney }
2446f41d911fSPaul E. McKenney 
2447907aed48SMel Gorman static inline void tsk_restore_flags(struct task_struct *task,
2448907aed48SMel Gorman 				unsigned long orig_flags, unsigned long flags)
2449907aed48SMel Gorman {
2450907aed48SMel Gorman 	task->flags &= ~flags;
2451907aed48SMel Gorman 	task->flags |= orig_flags & flags;
2452907aed48SMel Gorman }
2453907aed48SMel Gorman 
2454f82f8042SJuri Lelli extern int cpuset_cpumask_can_shrink(const struct cpumask *cur,
2455f82f8042SJuri Lelli 				     const struct cpumask *trial);
24567f51412aSJuri Lelli extern int task_can_attach(struct task_struct *p,
24577f51412aSJuri Lelli 			   const struct cpumask *cs_cpus_allowed);
24581da177e4SLinus Torvalds #ifdef CONFIG_SMP
24591e1b6c51SKOSAKI Motohiro extern void do_set_cpus_allowed(struct task_struct *p,
24601e1b6c51SKOSAKI Motohiro 			       const struct cpumask *new_mask);
24611e1b6c51SKOSAKI Motohiro 
2462cd8ba7cdSMike Travis extern int set_cpus_allowed_ptr(struct task_struct *p,
246396f874e2SRusty Russell 				const struct cpumask *new_mask);
24641da177e4SLinus Torvalds #else
24651e1b6c51SKOSAKI Motohiro static inline void do_set_cpus_allowed(struct task_struct *p,
24661e1b6c51SKOSAKI Motohiro 				      const struct cpumask *new_mask)
24671e1b6c51SKOSAKI Motohiro {
24681e1b6c51SKOSAKI Motohiro }
2469cd8ba7cdSMike Travis static inline int set_cpus_allowed_ptr(struct task_struct *p,
247096f874e2SRusty Russell 				       const struct cpumask *new_mask)
24711da177e4SLinus Torvalds {
247296f874e2SRusty Russell 	if (!cpumask_test_cpu(0, new_mask))
24731da177e4SLinus Torvalds 		return -EINVAL;
24741da177e4SLinus Torvalds 	return 0;
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds #endif
2477e0ad9556SRusty Russell 
24783451d024SFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON
24795167e8d5SPeter Zijlstra void calc_load_enter_idle(void);
24805167e8d5SPeter Zijlstra void calc_load_exit_idle(void);
24815167e8d5SPeter Zijlstra #else
24825167e8d5SPeter Zijlstra static inline void calc_load_enter_idle(void) { }
24835167e8d5SPeter Zijlstra static inline void calc_load_exit_idle(void) { }
24843451d024SFrederic Weisbecker #endif /* CONFIG_NO_HZ_COMMON */
24855167e8d5SPeter Zijlstra 
24866d0d2878SChristian Borntraeger #ifndef cpu_relax_yield
24876d0d2878SChristian Borntraeger #define cpu_relax_yield() cpu_relax()
24886d0d2878SChristian Borntraeger #endif
24896d0d2878SChristian Borntraeger 
2490b342501cSIngo Molnar /*
2491c676329aSPeter Zijlstra  * Do not use outside of architecture code which knows its limitations.
2492c676329aSPeter Zijlstra  *
2493c676329aSPeter Zijlstra  * sched_clock() has no promise of monotonicity or bounded drift between
2494c676329aSPeter Zijlstra  * CPUs, use (which you should not) requires disabling IRQs.
2495c676329aSPeter Zijlstra  *
2496c676329aSPeter Zijlstra  * Please use one of the three interfaces below.
2497b342501cSIngo Molnar  */
24981bbfa6f2SMike Frysinger extern unsigned long long notrace sched_clock(void);
2499c676329aSPeter Zijlstra /*
2500489a71b0SHiroshi Shimamoto  * See the comment in kernel/sched/clock.c
2501c676329aSPeter Zijlstra  */
2502545a2bf7SCyril Bur extern u64 running_clock(void);
2503c676329aSPeter Zijlstra extern u64 sched_clock_cpu(int cpu);
2504c676329aSPeter Zijlstra 
2505e436d800SIngo Molnar 
2506c1955a3dSPeter Zijlstra extern void sched_clock_init(void);
2507c1955a3dSPeter Zijlstra 
25083e51f33fSPeter Zijlstra #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
25099881b024SPeter Zijlstra static inline void sched_clock_init_late(void)
25109881b024SPeter Zijlstra {
25119881b024SPeter Zijlstra }
25129881b024SPeter Zijlstra 
25133e51f33fSPeter Zijlstra static inline void sched_clock_tick(void)
25143e51f33fSPeter Zijlstra {
25153e51f33fSPeter Zijlstra }
25163e51f33fSPeter Zijlstra 
25173e51f33fSPeter Zijlstra static inline void sched_clock_idle_sleep_event(void)
25183e51f33fSPeter Zijlstra {
25193e51f33fSPeter Zijlstra }
25203e51f33fSPeter Zijlstra 
25213e51f33fSPeter Zijlstra static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
25223e51f33fSPeter Zijlstra {
25233e51f33fSPeter Zijlstra }
25242c923e94SDaniel Lezcano 
25252c923e94SDaniel Lezcano static inline u64 cpu_clock(int cpu)
25262c923e94SDaniel Lezcano {
25272c923e94SDaniel Lezcano 	return sched_clock();
25282c923e94SDaniel Lezcano }
25292c923e94SDaniel Lezcano 
25302c923e94SDaniel Lezcano static inline u64 local_clock(void)
25312c923e94SDaniel Lezcano {
25322c923e94SDaniel Lezcano 	return sched_clock();
25332c923e94SDaniel Lezcano }
25343e51f33fSPeter Zijlstra #else
25359881b024SPeter Zijlstra extern void sched_clock_init_late(void);
2536c676329aSPeter Zijlstra /*
2537c676329aSPeter Zijlstra  * Architectures can set this to 1 if they have specified
2538c676329aSPeter Zijlstra  * CONFIG_HAVE_UNSTABLE_SCHED_CLOCK in their arch Kconfig,
2539c676329aSPeter Zijlstra  * but then during bootup it turns out that sched_clock()
2540c676329aSPeter Zijlstra  * is reliable after all:
2541c676329aSPeter Zijlstra  */
254235af99e6SPeter Zijlstra extern int sched_clock_stable(void);
254335af99e6SPeter Zijlstra extern void clear_sched_clock_stable(void);
2544c676329aSPeter Zijlstra 
25453e51f33fSPeter Zijlstra extern void sched_clock_tick(void);
25463e51f33fSPeter Zijlstra extern void sched_clock_idle_sleep_event(void);
25473e51f33fSPeter Zijlstra extern void sched_clock_idle_wakeup_event(u64 delta_ns);
25482c923e94SDaniel Lezcano 
25492c923e94SDaniel Lezcano /*
25502c923e94SDaniel Lezcano  * As outlined in clock.c, provides a fast, high resolution, nanosecond
25512c923e94SDaniel Lezcano  * time source that is monotonic per cpu argument and has bounded drift
25522c923e94SDaniel Lezcano  * between cpus.
25532c923e94SDaniel Lezcano  *
25542c923e94SDaniel Lezcano  * ######################### BIG FAT WARNING ##########################
25552c923e94SDaniel Lezcano  * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can #
25562c923e94SDaniel Lezcano  * # go backwards !!                                                  #
25572c923e94SDaniel Lezcano  * ####################################################################
25582c923e94SDaniel Lezcano  */
25592c923e94SDaniel Lezcano static inline u64 cpu_clock(int cpu)
25602c923e94SDaniel Lezcano {
25612c923e94SDaniel Lezcano 	return sched_clock_cpu(cpu);
25622c923e94SDaniel Lezcano }
25632c923e94SDaniel Lezcano 
25642c923e94SDaniel Lezcano static inline u64 local_clock(void)
25652c923e94SDaniel Lezcano {
25662c923e94SDaniel Lezcano 	return sched_clock_cpu(raw_smp_processor_id());
25672c923e94SDaniel Lezcano }
25683e51f33fSPeter Zijlstra #endif
25693e51f33fSPeter Zijlstra 
2570b52bfee4SVenkatesh Pallipadi #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2571b52bfee4SVenkatesh Pallipadi /*
2572b52bfee4SVenkatesh Pallipadi  * An i/f to runtime opt-in for irq time accounting based off of sched_clock.
2573b52bfee4SVenkatesh Pallipadi  * The reason for this explicit opt-in is not to have perf penalty with
2574b52bfee4SVenkatesh Pallipadi  * slow sched_clocks.
2575b52bfee4SVenkatesh Pallipadi  */
2576b52bfee4SVenkatesh Pallipadi extern void enable_sched_clock_irqtime(void);
2577b52bfee4SVenkatesh Pallipadi extern void disable_sched_clock_irqtime(void);
2578b52bfee4SVenkatesh Pallipadi #else
2579b52bfee4SVenkatesh Pallipadi static inline void enable_sched_clock_irqtime(void) {}
2580b52bfee4SVenkatesh Pallipadi static inline void disable_sched_clock_irqtime(void) {}
2581b52bfee4SVenkatesh Pallipadi #endif
2582b52bfee4SVenkatesh Pallipadi 
258336c8b586SIngo Molnar extern unsigned long long
258441b86e9cSIngo Molnar task_sched_runtime(struct task_struct *task);
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds /* sched_exec is called by processes performing an exec */
25871da177e4SLinus Torvalds #ifdef CONFIG_SMP
25881da177e4SLinus Torvalds extern void sched_exec(void);
25891da177e4SLinus Torvalds #else
25901da177e4SLinus Torvalds #define sched_exec()   {}
25911da177e4SLinus Torvalds #endif
25921da177e4SLinus Torvalds 
25932aa44d05SIngo Molnar extern void sched_clock_idle_sleep_event(void);
25942aa44d05SIngo Molnar extern void sched_clock_idle_wakeup_event(u64 delta_ns);
2595bb29ab26SIngo Molnar 
25961da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
25971da177e4SLinus Torvalds extern void idle_task_exit(void);
25981da177e4SLinus Torvalds #else
25991da177e4SLinus Torvalds static inline void idle_task_exit(void) {}
26001da177e4SLinus Torvalds #endif
26011da177e4SLinus Torvalds 
26023451d024SFrederic Weisbecker #if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)
26031c20091eSFrederic Weisbecker extern void wake_up_nohz_cpu(int cpu);
260406d8308cSThomas Gleixner #else
26051c20091eSFrederic Weisbecker static inline void wake_up_nohz_cpu(int cpu) { }
260606d8308cSThomas Gleixner #endif
260706d8308cSThomas Gleixner 
2608ce831b38SFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
2609265f22a9SFrederic Weisbecker extern u64 scheduler_tick_max_deferment(void);
2610bf0f6f24SIngo Molnar #endif
2611bf0f6f24SIngo Molnar 
26125091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP
26135091faa4SMike Galbraith extern void sched_autogroup_create_attach(struct task_struct *p);
26145091faa4SMike Galbraith extern void sched_autogroup_detach(struct task_struct *p);
26155091faa4SMike Galbraith extern void sched_autogroup_fork(struct signal_struct *sig);
26165091faa4SMike Galbraith extern void sched_autogroup_exit(struct signal_struct *sig);
26178e5bfa8cSOleg Nesterov extern void sched_autogroup_exit_task(struct task_struct *p);
26185091faa4SMike Galbraith #ifdef CONFIG_PROC_FS
26195091faa4SMike Galbraith extern void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m);
26202e5b5b3aSHiroshi Shimamoto extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
26215091faa4SMike Galbraith #endif
26225091faa4SMike Galbraith #else
26235091faa4SMike Galbraith static inline void sched_autogroup_create_attach(struct task_struct *p) { }
26245091faa4SMike Galbraith static inline void sched_autogroup_detach(struct task_struct *p) { }
26255091faa4SMike Galbraith static inline void sched_autogroup_fork(struct signal_struct *sig) { }
26265091faa4SMike Galbraith static inline void sched_autogroup_exit(struct signal_struct *sig) { }
26278e5bfa8cSOleg Nesterov static inline void sched_autogroup_exit_task(struct task_struct *p) { }
26285091faa4SMike Galbraith #endif
26295091faa4SMike Galbraith 
2630fa93384fSDan Carpenter extern int yield_to(struct task_struct *p, bool preempt);
263136c8b586SIngo Molnar extern void set_user_nice(struct task_struct *p, long nice);
263236c8b586SIngo Molnar extern int task_prio(const struct task_struct *p);
2633d0ea0268SDongsheng Yang /**
2634d0ea0268SDongsheng Yang  * task_nice - return the nice value of a given task.
2635d0ea0268SDongsheng Yang  * @p: the task in question.
2636d0ea0268SDongsheng Yang  *
2637d0ea0268SDongsheng Yang  * Return: The nice value [ -20 ... 0 ... 19 ].
2638d0ea0268SDongsheng Yang  */
2639d0ea0268SDongsheng Yang static inline int task_nice(const struct task_struct *p)
2640d0ea0268SDongsheng Yang {
2641d0ea0268SDongsheng Yang 	return PRIO_TO_NICE((p)->static_prio);
2642d0ea0268SDongsheng Yang }
264336c8b586SIngo Molnar extern int can_nice(const struct task_struct *p, const int nice);
264436c8b586SIngo Molnar extern int task_curr(const struct task_struct *p);
26451da177e4SLinus Torvalds extern int idle_cpu(int cpu);
2646fe7de49fSKOSAKI Motohiro extern int sched_setscheduler(struct task_struct *, int,
2647fe7de49fSKOSAKI Motohiro 			      const struct sched_param *);
2648961ccdddSRusty Russell extern int sched_setscheduler_nocheck(struct task_struct *, int,
2649fe7de49fSKOSAKI Motohiro 				      const struct sched_param *);
2650d50dde5aSDario Faggioli extern int sched_setattr(struct task_struct *,
2651d50dde5aSDario Faggioli 			 const struct sched_attr *);
265236c8b586SIngo Molnar extern struct task_struct *idle_task(int cpu);
2653c4f30608SPaul E. McKenney /**
2654c4f30608SPaul E. McKenney  * is_idle_task - is the specified task an idle task?
2655fa757281SRandy Dunlap  * @p: the task in question.
2656e69f6186SYacine Belkadi  *
2657e69f6186SYacine Belkadi  * Return: 1 if @p is an idle task. 0 otherwise.
2658c4f30608SPaul E. McKenney  */
26597061ca3bSPaul E. McKenney static inline bool is_idle_task(const struct task_struct *p)
2660c4f30608SPaul E. McKenney {
2661c1de45caSPeter Zijlstra 	return !!(p->flags & PF_IDLE);
2662c4f30608SPaul E. McKenney }
266336c8b586SIngo Molnar extern struct task_struct *curr_task(int cpu);
2664a458ae2eSPeter Zijlstra extern void ia64_set_curr_task(int cpu, struct task_struct *p);
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds void yield(void);
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds union thread_union {
2669c65eacbeSAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK
26701da177e4SLinus Torvalds 	struct thread_info thread_info;
2671c65eacbeSAndy Lutomirski #endif
26721da177e4SLinus Torvalds 	unsigned long stack[THREAD_SIZE/sizeof(long)];
26731da177e4SLinus Torvalds };
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds #ifndef __HAVE_ARCH_KSTACK_END
26761da177e4SLinus Torvalds static inline int kstack_end(void *addr)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	/* Reliable end of stack detection:
26791da177e4SLinus Torvalds 	 * Some APM bios versions misalign the stack
26801da177e4SLinus Torvalds 	 */
26811da177e4SLinus Torvalds 	return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds #endif
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds extern union thread_union init_thread_union;
26861da177e4SLinus Torvalds extern struct task_struct init_task;
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds extern struct   mm_struct init_mm;
26891da177e4SLinus Torvalds 
2690198fe21bSPavel Emelyanov extern struct pid_namespace init_pid_ns;
2691198fe21bSPavel Emelyanov 
2692198fe21bSPavel Emelyanov /*
2693198fe21bSPavel Emelyanov  * find a task by one of its numerical ids
2694198fe21bSPavel Emelyanov  *
2695198fe21bSPavel Emelyanov  * find_task_by_pid_ns():
2696198fe21bSPavel Emelyanov  *      finds a task by its pid in the specified namespace
2697228ebcbeSPavel Emelyanov  * find_task_by_vpid():
2698228ebcbeSPavel Emelyanov  *      finds a task by its virtual pid
2699198fe21bSPavel Emelyanov  *
2700e49859e7SPavel Emelyanov  * see also find_vpid() etc in include/linux/pid.h
2701198fe21bSPavel Emelyanov  */
2702198fe21bSPavel Emelyanov 
2703228ebcbeSPavel Emelyanov extern struct task_struct *find_task_by_vpid(pid_t nr);
2704228ebcbeSPavel Emelyanov extern struct task_struct *find_task_by_pid_ns(pid_t nr,
2705228ebcbeSPavel Emelyanov 		struct pid_namespace *ns);
2706198fe21bSPavel Emelyanov 
27071da177e4SLinus Torvalds /* per-UID process charging. */
27087b44ab97SEric W. Biederman extern struct user_struct * alloc_uid(kuid_t);
27091da177e4SLinus Torvalds static inline struct user_struct *get_uid(struct user_struct *u)
27101da177e4SLinus Torvalds {
27111da177e4SLinus Torvalds 	atomic_inc(&u->__count);
27121da177e4SLinus Torvalds 	return u;
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds extern void free_uid(struct user_struct *);
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds #include <asm/current.h>
27171da177e4SLinus Torvalds 
2718f0af911aSTorben Hohn extern void xtime_update(unsigned long ticks);
27191da177e4SLinus Torvalds 
2720b3c97528SHarvey Harrison extern int wake_up_state(struct task_struct *tsk, unsigned int state);
2721b3c97528SHarvey Harrison extern int wake_up_process(struct task_struct *tsk);
27223e51e3edSSamir Bellabes extern void wake_up_new_task(struct task_struct *tsk);
27231da177e4SLinus Torvalds #ifdef CONFIG_SMP
27241da177e4SLinus Torvalds  extern void kick_process(struct task_struct *tsk);
27251da177e4SLinus Torvalds #else
27261da177e4SLinus Torvalds  static inline void kick_process(struct task_struct *tsk) { }
27271da177e4SLinus Torvalds #endif
2728aab03e05SDario Faggioli extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
2729ad46c2c4SIngo Molnar extern void sched_dead(struct task_struct *p);
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds extern void proc_caches_init(void);
27321da177e4SLinus Torvalds extern void flush_signals(struct task_struct *);
273310ab825bSOleg Nesterov extern void ignore_signals(struct task_struct *);
27341da177e4SLinus Torvalds extern void flush_signal_handlers(struct task_struct *, int force_default);
27351da177e4SLinus Torvalds extern int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info);
27361da177e4SLinus Torvalds 
2737be0e6f29SOleg Nesterov static inline int kernel_dequeue_signal(siginfo_t *info)
27381da177e4SLinus Torvalds {
2739be0e6f29SOleg Nesterov 	struct task_struct *tsk = current;
2740be0e6f29SOleg Nesterov 	siginfo_t __info;
27411da177e4SLinus Torvalds 	int ret;
27421da177e4SLinus Torvalds 
2743be0e6f29SOleg Nesterov 	spin_lock_irq(&tsk->sighand->siglock);
2744be0e6f29SOleg Nesterov 	ret = dequeue_signal(tsk, &tsk->blocked, info ?: &__info);
2745be0e6f29SOleg Nesterov 	spin_unlock_irq(&tsk->sighand->siglock);
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds 	return ret;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
27509a13049eSOleg Nesterov static inline void kernel_signal_stop(void)
27519a13049eSOleg Nesterov {
27529a13049eSOleg Nesterov 	spin_lock_irq(&current->sighand->siglock);
27539a13049eSOleg Nesterov 	if (current->jobctl & JOBCTL_STOP_DEQUEUED)
27549a13049eSOleg Nesterov 		__set_current_state(TASK_STOPPED);
27559a13049eSOleg Nesterov 	spin_unlock_irq(&current->sighand->siglock);
27569a13049eSOleg Nesterov 
27579a13049eSOleg Nesterov 	schedule();
27589a13049eSOleg Nesterov }
27599a13049eSOleg Nesterov 
27601da177e4SLinus Torvalds extern void release_task(struct task_struct * p);
27611da177e4SLinus Torvalds extern int send_sig_info(int, struct siginfo *, struct task_struct *);
27621da177e4SLinus Torvalds extern int force_sigsegv(int, struct task_struct *);
27631da177e4SLinus Torvalds extern int force_sig_info(int, struct siginfo *, struct task_struct *);
2764c4b92fc1SEric W. Biederman extern int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp);
2765c4b92fc1SEric W. Biederman extern int kill_pid_info(int sig, struct siginfo *info, struct pid *pid);
2766d178bc3aSSerge Hallyn extern int kill_pid_info_as_cred(int, struct siginfo *, struct pid *,
2767d178bc3aSSerge Hallyn 				const struct cred *, u32);
2768c4b92fc1SEric W. Biederman extern int kill_pgrp(struct pid *pid, int sig, int priv);
2769c4b92fc1SEric W. Biederman extern int kill_pid(struct pid *pid, int sig, int priv);
2770c3de4b38SMatthew Wilcox extern int kill_proc_info(int, struct siginfo *, pid_t);
277186773473SOleg Nesterov extern __must_check bool do_notify_parent(struct task_struct *, int);
2772a7f0765eSOleg Nesterov extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
27731da177e4SLinus Torvalds extern void force_sig(int, struct task_struct *);
27741da177e4SLinus Torvalds extern int send_sig(int, struct task_struct *, int);
277509faef11SOleg Nesterov extern int zap_other_threads(struct task_struct *p);
27761da177e4SLinus Torvalds extern struct sigqueue *sigqueue_alloc(void);
27771da177e4SLinus Torvalds extern void sigqueue_free(struct sigqueue *);
2778ac5c2153SOleg Nesterov extern int send_sigqueue(struct sigqueue *,  struct task_struct *, int group);
27799ac95f2fSOleg Nesterov extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
27801da177e4SLinus Torvalds 
27817e781418SAndy Lutomirski #ifdef TIF_RESTORE_SIGMASK
27827e781418SAndy Lutomirski /*
27837e781418SAndy Lutomirski  * Legacy restore_sigmask accessors.  These are inefficient on
27847e781418SAndy Lutomirski  * SMP architectures because they require atomic operations.
27857e781418SAndy Lutomirski  */
27867e781418SAndy Lutomirski 
27877e781418SAndy Lutomirski /**
27887e781418SAndy Lutomirski  * set_restore_sigmask() - make sure saved_sigmask processing gets done
27897e781418SAndy Lutomirski  *
27907e781418SAndy Lutomirski  * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
27917e781418SAndy Lutomirski  * will run before returning to user mode, to process the flag.  For
27927e781418SAndy Lutomirski  * all callers, TIF_SIGPENDING is already set or it's no harm to set
27937e781418SAndy Lutomirski  * it.  TIF_RESTORE_SIGMASK need not be in the set of bits that the
27947e781418SAndy Lutomirski  * arch code will notice on return to user mode, in case those bits
27957e781418SAndy Lutomirski  * are scarce.  We set TIF_SIGPENDING here to ensure that the arch
27967e781418SAndy Lutomirski  * signal code always gets run when TIF_RESTORE_SIGMASK is set.
27977e781418SAndy Lutomirski  */
27987e781418SAndy Lutomirski static inline void set_restore_sigmask(void)
27997e781418SAndy Lutomirski {
28007e781418SAndy Lutomirski 	set_thread_flag(TIF_RESTORE_SIGMASK);
28017e781418SAndy Lutomirski 	WARN_ON(!test_thread_flag(TIF_SIGPENDING));
28027e781418SAndy Lutomirski }
28037e781418SAndy Lutomirski static inline void clear_restore_sigmask(void)
28047e781418SAndy Lutomirski {
28057e781418SAndy Lutomirski 	clear_thread_flag(TIF_RESTORE_SIGMASK);
28067e781418SAndy Lutomirski }
28077e781418SAndy Lutomirski static inline bool test_restore_sigmask(void)
28087e781418SAndy Lutomirski {
28097e781418SAndy Lutomirski 	return test_thread_flag(TIF_RESTORE_SIGMASK);
28107e781418SAndy Lutomirski }
28117e781418SAndy Lutomirski static inline bool test_and_clear_restore_sigmask(void)
28127e781418SAndy Lutomirski {
28137e781418SAndy Lutomirski 	return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
28147e781418SAndy Lutomirski }
28157e781418SAndy Lutomirski 
28167e781418SAndy Lutomirski #else	/* TIF_RESTORE_SIGMASK */
28177e781418SAndy Lutomirski 
28187e781418SAndy Lutomirski /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
28197e781418SAndy Lutomirski static inline void set_restore_sigmask(void)
28207e781418SAndy Lutomirski {
28217e781418SAndy Lutomirski 	current->restore_sigmask = true;
28227e781418SAndy Lutomirski 	WARN_ON(!test_thread_flag(TIF_SIGPENDING));
28237e781418SAndy Lutomirski }
28247e781418SAndy Lutomirski static inline void clear_restore_sigmask(void)
28257e781418SAndy Lutomirski {
28267e781418SAndy Lutomirski 	current->restore_sigmask = false;
28277e781418SAndy Lutomirski }
28287e781418SAndy Lutomirski static inline bool test_restore_sigmask(void)
28297e781418SAndy Lutomirski {
28307e781418SAndy Lutomirski 	return current->restore_sigmask;
28317e781418SAndy Lutomirski }
28327e781418SAndy Lutomirski static inline bool test_and_clear_restore_sigmask(void)
28337e781418SAndy Lutomirski {
28347e781418SAndy Lutomirski 	if (!current->restore_sigmask)
28357e781418SAndy Lutomirski 		return false;
28367e781418SAndy Lutomirski 	current->restore_sigmask = false;
28377e781418SAndy Lutomirski 	return true;
28387e781418SAndy Lutomirski }
28397e781418SAndy Lutomirski #endif
28407e781418SAndy Lutomirski 
284151a7b448SAl Viro static inline void restore_saved_sigmask(void)
284251a7b448SAl Viro {
284351a7b448SAl Viro 	if (test_and_clear_restore_sigmask())
284477097ae5SAl Viro 		__set_current_blocked(&current->saved_sigmask);
284551a7b448SAl Viro }
284651a7b448SAl Viro 
2847b7f9a11aSAl Viro static inline sigset_t *sigmask_to_save(void)
2848b7f9a11aSAl Viro {
2849b7f9a11aSAl Viro 	sigset_t *res = &current->blocked;
2850b7f9a11aSAl Viro 	if (unlikely(test_restore_sigmask()))
2851b7f9a11aSAl Viro 		res = &current->saved_sigmask;
2852b7f9a11aSAl Viro 	return res;
2853b7f9a11aSAl Viro }
2854b7f9a11aSAl Viro 
28559ec52099SCedric Le Goater static inline int kill_cad_pid(int sig, int priv)
28569ec52099SCedric Le Goater {
28579ec52099SCedric Le Goater 	return kill_pid(cad_pid, sig, priv);
28589ec52099SCedric Le Goater }
28599ec52099SCedric Le Goater 
28601da177e4SLinus Torvalds /* These can be the second arg to send_sig_info/send_group_sig_info.  */
28611da177e4SLinus Torvalds #define SEND_SIG_NOINFO ((struct siginfo *) 0)
28621da177e4SLinus Torvalds #define SEND_SIG_PRIV	((struct siginfo *) 1)
28631da177e4SLinus Torvalds #define SEND_SIG_FORCED	((struct siginfo *) 2)
28641da177e4SLinus Torvalds 
28652a855dd0SSebastian Andrzej Siewior /*
28662a855dd0SSebastian Andrzej Siewior  * True if we are on the alternate signal stack.
28672a855dd0SSebastian Andrzej Siewior  */
28681da177e4SLinus Torvalds static inline int on_sig_stack(unsigned long sp)
28691da177e4SLinus Torvalds {
2870c876eeabSAndy Lutomirski 	/*
2871c876eeabSAndy Lutomirski 	 * If the signal stack is SS_AUTODISARM then, by construction, we
2872c876eeabSAndy Lutomirski 	 * can't be on the signal stack unless user code deliberately set
2873c876eeabSAndy Lutomirski 	 * SS_AUTODISARM when we were already on it.
2874c876eeabSAndy Lutomirski 	 *
2875c876eeabSAndy Lutomirski 	 * This improves reliability: if user state gets corrupted such that
2876c876eeabSAndy Lutomirski 	 * the stack pointer points very close to the end of the signal stack,
2877c876eeabSAndy Lutomirski 	 * then this check will enable the signal to be handled anyway.
2878c876eeabSAndy Lutomirski 	 */
2879c876eeabSAndy Lutomirski 	if (current->sas_ss_flags & SS_AUTODISARM)
2880c876eeabSAndy Lutomirski 		return 0;
2881c876eeabSAndy Lutomirski 
28822a855dd0SSebastian Andrzej Siewior #ifdef CONFIG_STACK_GROWSUP
28832a855dd0SSebastian Andrzej Siewior 	return sp >= current->sas_ss_sp &&
28842a855dd0SSebastian Andrzej Siewior 		sp - current->sas_ss_sp < current->sas_ss_size;
28852a855dd0SSebastian Andrzej Siewior #else
28862a855dd0SSebastian Andrzej Siewior 	return sp > current->sas_ss_sp &&
28872a855dd0SSebastian Andrzej Siewior 		sp - current->sas_ss_sp <= current->sas_ss_size;
28882a855dd0SSebastian Andrzej Siewior #endif
28891da177e4SLinus Torvalds }
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds static inline int sas_ss_flags(unsigned long sp)
28921da177e4SLinus Torvalds {
289372f15c03SRichard Weinberger 	if (!current->sas_ss_size)
289472f15c03SRichard Weinberger 		return SS_DISABLE;
289572f15c03SRichard Weinberger 
289672f15c03SRichard Weinberger 	return on_sig_stack(sp) ? SS_ONSTACK : 0;
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds 
28992a742138SStas Sergeev static inline void sas_ss_reset(struct task_struct *p)
29002a742138SStas Sergeev {
29012a742138SStas Sergeev 	p->sas_ss_sp = 0;
29022a742138SStas Sergeev 	p->sas_ss_size = 0;
29032a742138SStas Sergeev 	p->sas_ss_flags = SS_DISABLE;
29042a742138SStas Sergeev }
29052a742138SStas Sergeev 
29065a1b98d3SAl Viro static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
29075a1b98d3SAl Viro {
29085a1b98d3SAl Viro 	if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
29095a1b98d3SAl Viro #ifdef CONFIG_STACK_GROWSUP
29105a1b98d3SAl Viro 		return current->sas_ss_sp;
29115a1b98d3SAl Viro #else
29125a1b98d3SAl Viro 		return current->sas_ss_sp + current->sas_ss_size;
29135a1b98d3SAl Viro #endif
29145a1b98d3SAl Viro 	return sp;
29155a1b98d3SAl Viro }
29165a1b98d3SAl Viro 
29171da177e4SLinus Torvalds /*
29181da177e4SLinus Torvalds  * Routines for handling mm_structs
29191da177e4SLinus Torvalds  */
29201da177e4SLinus Torvalds extern struct mm_struct * mm_alloc(void);
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds /* mmdrop drops the mm and the page tables */
2923b3c97528SHarvey Harrison extern void __mmdrop(struct mm_struct *);
29241da177e4SLinus Torvalds static inline void mmdrop(struct mm_struct *mm)
29251da177e4SLinus Torvalds {
29266fb43d7bSIngo Molnar 	if (unlikely(atomic_dec_and_test(&mm->mm_count)))
29271da177e4SLinus Torvalds 		__mmdrop(mm);
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29307283094eSMichal Hocko static inline void mmdrop_async_fn(struct work_struct *work)
29317283094eSMichal Hocko {
29327283094eSMichal Hocko 	struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work);
29337283094eSMichal Hocko 	__mmdrop(mm);
29347283094eSMichal Hocko }
29357283094eSMichal Hocko 
29367283094eSMichal Hocko static inline void mmdrop_async(struct mm_struct *mm)
29377283094eSMichal Hocko {
29387283094eSMichal Hocko 	if (unlikely(atomic_dec_and_test(&mm->mm_count))) {
29397283094eSMichal Hocko 		INIT_WORK(&mm->async_put_work, mmdrop_async_fn);
29407283094eSMichal Hocko 		schedule_work(&mm->async_put_work);
29417283094eSMichal Hocko 	}
29427283094eSMichal Hocko }
29437283094eSMichal Hocko 
2944d2005e3fSOleg Nesterov static inline bool mmget_not_zero(struct mm_struct *mm)
2945d2005e3fSOleg Nesterov {
2946d2005e3fSOleg Nesterov 	return atomic_inc_not_zero(&mm->mm_users);
29471da177e4SLinus Torvalds }
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds /* mmput gets rid of the mappings and all user-space */
29501da177e4SLinus Torvalds extern void mmput(struct mm_struct *);
29517ef949d7SMichal Hocko #ifdef CONFIG_MMU
29527ef949d7SMichal Hocko /* same as above but performs the slow path from the async context. Can
2953ec8d7c14SMichal Hocko  * be called from the atomic context as well
2954ec8d7c14SMichal Hocko  */
2955ec8d7c14SMichal Hocko extern void mmput_async(struct mm_struct *);
29567ef949d7SMichal Hocko #endif
2957ec8d7c14SMichal Hocko 
29581da177e4SLinus Torvalds /* Grab a reference to a task's mm, if it is not already going away */
29591da177e4SLinus Torvalds extern struct mm_struct *get_task_mm(struct task_struct *task);
29608cdb878dSChristopher Yeoh /*
29618cdb878dSChristopher Yeoh  * Grab a reference to a task's mm, if it is not already going away
29628cdb878dSChristopher Yeoh  * and ptrace_may_access with the mode parameter passed to it
29638cdb878dSChristopher Yeoh  * succeeds.
29648cdb878dSChristopher Yeoh  */
29658cdb878dSChristopher Yeoh extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
29661da177e4SLinus Torvalds /* Remove the current tasks stale references to the old mm_struct */
29671da177e4SLinus Torvalds extern void mm_release(struct task_struct *, struct mm_struct *);
29681da177e4SLinus Torvalds 
29693033f14aSJosh Triplett #ifdef CONFIG_HAVE_COPY_THREAD_TLS
29703033f14aSJosh Triplett extern int copy_thread_tls(unsigned long, unsigned long, unsigned long,
29713033f14aSJosh Triplett 			struct task_struct *, unsigned long);
29723033f14aSJosh Triplett #else
29736f2c55b8SAlexey Dobriyan extern int copy_thread(unsigned long, unsigned long, unsigned long,
2974afa86fc4SAl Viro 			struct task_struct *);
29753033f14aSJosh Triplett 
29763033f14aSJosh Triplett /* Architectures that haven't opted into copy_thread_tls get the tls argument
29773033f14aSJosh Triplett  * via pt_regs, so ignore the tls argument passed via C. */
29783033f14aSJosh Triplett static inline int copy_thread_tls(
29793033f14aSJosh Triplett 		unsigned long clone_flags, unsigned long sp, unsigned long arg,
29803033f14aSJosh Triplett 		struct task_struct *p, unsigned long tls)
29813033f14aSJosh Triplett {
29823033f14aSJosh Triplett 	return copy_thread(clone_flags, sp, arg, p);
29833033f14aSJosh Triplett }
29843033f14aSJosh Triplett #endif
29851da177e4SLinus Torvalds extern void flush_thread(void);
29865f56a5dfSJiri Slaby 
29875f56a5dfSJiri Slaby #ifdef CONFIG_HAVE_EXIT_THREAD
2988e6464694SJiri Slaby extern void exit_thread(struct task_struct *tsk);
29895f56a5dfSJiri Slaby #else
2990e6464694SJiri Slaby static inline void exit_thread(struct task_struct *tsk)
29915f56a5dfSJiri Slaby {
29925f56a5dfSJiri Slaby }
29935f56a5dfSJiri Slaby #endif
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds extern void exit_files(struct task_struct *);
2996a7e5328aSOleg Nesterov extern void __cleanup_sighand(struct sighand_struct *);
2997cbaffba1SOleg Nesterov 
29981da177e4SLinus Torvalds extern void exit_itimers(struct signal_struct *);
2999cbaffba1SOleg Nesterov extern void flush_itimer_signals(void);
30001da177e4SLinus Torvalds 
30019402c95fSJoe Perches extern void do_group_exit(int);
30021da177e4SLinus Torvalds 
3003c4ad8f98SLinus Torvalds extern int do_execve(struct filename *,
3004d7627467SDavid Howells 		     const char __user * const __user *,
3005da3d4c5fSAl Viro 		     const char __user * const __user *);
300651f39a1fSDavid Drysdale extern int do_execveat(int, struct filename *,
300751f39a1fSDavid Drysdale 		       const char __user * const __user *,
300851f39a1fSDavid Drysdale 		       const char __user * const __user *,
300951f39a1fSDavid Drysdale 		       int);
30103033f14aSJosh Triplett extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
3011e80d6661SAl Viro extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
301236c8b586SIngo Molnar struct task_struct *fork_idle(int);
30132aa3a7f8SAl Viro extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
30141da177e4SLinus Torvalds 
301582b89778SAdrian Hunter extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
301682b89778SAdrian Hunter static inline void set_task_comm(struct task_struct *tsk, const char *from)
301782b89778SAdrian Hunter {
301882b89778SAdrian Hunter 	__set_task_comm(tsk, from, false);
301982b89778SAdrian Hunter }
302059714d65SAndrew Morton extern char *get_task_comm(char *to, struct task_struct *tsk);
30211da177e4SLinus Torvalds 
30221da177e4SLinus Torvalds #ifdef CONFIG_SMP
3023317f3941SPeter Zijlstra void scheduler_ipi(void);
302485ba2d86SRoland McGrath extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
30251da177e4SLinus Torvalds #else
3026184748ccSPeter Zijlstra static inline void scheduler_ipi(void) { }
302785ba2d86SRoland McGrath static inline unsigned long wait_task_inactive(struct task_struct *p,
302885ba2d86SRoland McGrath 					       long match_state)
302985ba2d86SRoland McGrath {
303085ba2d86SRoland McGrath 	return 1;
303185ba2d86SRoland McGrath }
30321da177e4SLinus Torvalds #endif
30331da177e4SLinus Torvalds 
3034fafe870fSFrederic Weisbecker #define tasklist_empty() \
3035fafe870fSFrederic Weisbecker 	list_empty(&init_task.tasks)
3036fafe870fSFrederic Weisbecker 
303705725f7eSJiri Pirko #define next_task(p) \
303805725f7eSJiri Pirko 	list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds #define for_each_process(p) \
30411da177e4SLinus Torvalds 	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
30421da177e4SLinus Torvalds 
30435bb459bbSOleg Nesterov extern bool current_is_single_threaded(void);
3044d84f4f99SDavid Howells 
30451da177e4SLinus Torvalds /*
30461da177e4SLinus Torvalds  * Careful: do_each_thread/while_each_thread is a double loop so
30471da177e4SLinus Torvalds  *          'break' will not work as expected - use goto instead.
30481da177e4SLinus Torvalds  */
30491da177e4SLinus Torvalds #define do_each_thread(g, t) \
30501da177e4SLinus Torvalds 	for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds #define while_each_thread(g, t) \
30531da177e4SLinus Torvalds 	while ((t = next_thread(t)) != g)
30541da177e4SLinus Torvalds 
30550c740d0aSOleg Nesterov #define __for_each_thread(signal, t)	\
30560c740d0aSOleg Nesterov 	list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
30570c740d0aSOleg Nesterov 
30580c740d0aSOleg Nesterov #define for_each_thread(p, t)		\
30590c740d0aSOleg Nesterov 	__for_each_thread((p)->signal, t)
30600c740d0aSOleg Nesterov 
30610c740d0aSOleg Nesterov /* Careful: this is a double loop, 'break' won't work as expected. */
30620c740d0aSOleg Nesterov #define for_each_process_thread(p, t)	\
30630c740d0aSOleg Nesterov 	for_each_process(p) for_each_thread(p, t)
30640c740d0aSOleg Nesterov 
30657e49827cSOleg Nesterov static inline int get_nr_threads(struct task_struct *tsk)
30667e49827cSOleg Nesterov {
3067b3ac022cSOleg Nesterov 	return tsk->signal->nr_threads;
30687e49827cSOleg Nesterov }
30697e49827cSOleg Nesterov 
3070087806b1SOleg Nesterov static inline bool thread_group_leader(struct task_struct *p)
3071087806b1SOleg Nesterov {
3072087806b1SOleg Nesterov 	return p->exit_signal >= 0;
3073087806b1SOleg Nesterov }
30741da177e4SLinus Torvalds 
30750804ef4bSEric W. Biederman /* Do to the insanities of de_thread it is possible for a process
30760804ef4bSEric W. Biederman  * to have the pid of the thread group leader without actually being
30770804ef4bSEric W. Biederman  * the thread group leader.  For iteration through the pids in proc
30780804ef4bSEric W. Biederman  * all we care about is that we have a task with the appropriate
30790804ef4bSEric W. Biederman  * pid, we don't actually care if we have the right task.
30800804ef4bSEric W. Biederman  */
3081e1403b8eSOleg Nesterov static inline bool has_group_leader_pid(struct task_struct *p)
30820804ef4bSEric W. Biederman {
3083e1403b8eSOleg Nesterov 	return task_pid(p) == p->signal->leader_pid;
30840804ef4bSEric W. Biederman }
30850804ef4bSEric W. Biederman 
3086bac0abd6SPavel Emelyanov static inline
3087e1403b8eSOleg Nesterov bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
3088bac0abd6SPavel Emelyanov {
3089e1403b8eSOleg Nesterov 	return p1->signal == p2->signal;
3090bac0abd6SPavel Emelyanov }
3091bac0abd6SPavel Emelyanov 
309236c8b586SIngo Molnar static inline struct task_struct *next_thread(const struct task_struct *p)
309347e65328SOleg Nesterov {
309405725f7eSJiri Pirko 	return list_entry_rcu(p->thread_group.next,
309536c8b586SIngo Molnar 			      struct task_struct, thread_group);
309647e65328SOleg Nesterov }
309747e65328SOleg Nesterov 
3098e868171aSAlexey Dobriyan static inline int thread_group_empty(struct task_struct *p)
30991da177e4SLinus Torvalds {
310047e65328SOleg Nesterov 	return list_empty(&p->thread_group);
31011da177e4SLinus Torvalds }
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds #define delay_group_leader(p) \
31041da177e4SLinus Torvalds 		(thread_group_leader(p) && !thread_group_empty(p))
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds /*
3107260ea101SEric W. Biederman  * Protects ->fs, ->files, ->mm, ->group_info, ->comm, keyring
310822e2c507SJens Axboe  * subscriptions and synchronises with wait4().  Also used in procfs.  Also
3109ddbcc7e8SPaul Menage  * pins the final release of task.io_context.  Also protects ->cpuset and
3110d68b46feSOleg Nesterov  * ->cgroup.subsys[]. And ->vfork_done.
31111da177e4SLinus Torvalds  *
31121da177e4SLinus Torvalds  * Nests both inside and outside of read_lock(&tasklist_lock).
31131da177e4SLinus Torvalds  * It must not be nested with write_lock_irq(&tasklist_lock),
31141da177e4SLinus Torvalds  * neither inside nor outside.
31151da177e4SLinus Torvalds  */
31161da177e4SLinus Torvalds static inline void task_lock(struct task_struct *p)
31171da177e4SLinus Torvalds {
31181da177e4SLinus Torvalds 	spin_lock(&p->alloc_lock);
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds static inline void task_unlock(struct task_struct *p)
31221da177e4SLinus Torvalds {
31231da177e4SLinus Torvalds 	spin_unlock(&p->alloc_lock);
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds 
3126b8ed374eSNamhyung Kim extern struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
3127f63ee72eSOleg Nesterov 							unsigned long *flags);
3128f63ee72eSOleg Nesterov 
31299388dc30SAnton Vorontsov static inline struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
31309388dc30SAnton Vorontsov 						       unsigned long *flags)
31319388dc30SAnton Vorontsov {
31329388dc30SAnton Vorontsov 	struct sighand_struct *ret;
31339388dc30SAnton Vorontsov 
31349388dc30SAnton Vorontsov 	ret = __lock_task_sighand(tsk, flags);
31359388dc30SAnton Vorontsov 	(void)__cond_lock(&tsk->sighand->siglock, ret);
31369388dc30SAnton Vorontsov 	return ret;
31379388dc30SAnton Vorontsov }
3138b8ed374eSNamhyung Kim 
3139f63ee72eSOleg Nesterov static inline void unlock_task_sighand(struct task_struct *tsk,
3140f63ee72eSOleg Nesterov 						unsigned long *flags)
3141f63ee72eSOleg Nesterov {
3142f63ee72eSOleg Nesterov 	spin_unlock_irqrestore(&tsk->sighand->siglock, *flags);
3143f63ee72eSOleg Nesterov }
3144f63ee72eSOleg Nesterov 
31457d7efec3STejun Heo /**
31467d7efec3STejun Heo  * threadgroup_change_begin - mark the beginning of changes to a threadgroup
31477d7efec3STejun Heo  * @tsk: task causing the changes
31487d7efec3STejun Heo  *
31497d7efec3STejun Heo  * All operations which modify a threadgroup - a new thread joining the
31507d7efec3STejun Heo  * group, death of a member thread (the assertion of PF_EXITING) and
31517d7efec3STejun Heo  * exec(2) dethreading the process and replacing the leader - are wrapped
31527d7efec3STejun Heo  * by threadgroup_change_{begin|end}().  This is to provide a place which
31537d7efec3STejun Heo  * subsystems needing threadgroup stability can hook into for
31547d7efec3STejun Heo  * synchronization.
31557d7efec3STejun Heo  */
3156257058aeSTejun Heo static inline void threadgroup_change_begin(struct task_struct *tsk)
31574714d1d3SBen Blum {
31587d7efec3STejun Heo 	might_sleep();
31597d7efec3STejun Heo 	cgroup_threadgroup_change_begin(tsk);
31604714d1d3SBen Blum }
31617d7efec3STejun Heo 
31627d7efec3STejun Heo /**
31637d7efec3STejun Heo  * threadgroup_change_end - mark the end of changes to a threadgroup
31647d7efec3STejun Heo  * @tsk: task causing the changes
31657d7efec3STejun Heo  *
31667d7efec3STejun Heo  * See threadgroup_change_begin().
31677d7efec3STejun Heo  */
3168257058aeSTejun Heo static inline void threadgroup_change_end(struct task_struct *tsk)
31694714d1d3SBen Blum {
31707d7efec3STejun Heo 	cgroup_threadgroup_change_end(tsk);
31714714d1d3SBen Blum }
317277e4ef99STejun Heo 
3173c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
3174c65eacbeSAndy Lutomirski 
3175c65eacbeSAndy Lutomirski static inline struct thread_info *task_thread_info(struct task_struct *task)
3176c65eacbeSAndy Lutomirski {
3177c65eacbeSAndy Lutomirski 	return &task->thread_info;
3178c65eacbeSAndy Lutomirski }
3179c6c314a6SAndy Lutomirski 
3180c6c314a6SAndy Lutomirski /*
3181c6c314a6SAndy Lutomirski  * When accessing the stack of a non-current task that might exit, use
3182c6c314a6SAndy Lutomirski  * try_get_task_stack() instead.  task_stack_page will return a pointer
3183c6c314a6SAndy Lutomirski  * that could get freed out from under you.
3184c6c314a6SAndy Lutomirski  */
3185c65eacbeSAndy Lutomirski static inline void *task_stack_page(const struct task_struct *task)
3186c65eacbeSAndy Lutomirski {
3187c65eacbeSAndy Lutomirski 	return task->stack;
3188c65eacbeSAndy Lutomirski }
3189c6c314a6SAndy Lutomirski 
3190c65eacbeSAndy Lutomirski #define setup_thread_stack(new,old)	do { } while(0)
3191c6c314a6SAndy Lutomirski 
3192c65eacbeSAndy Lutomirski static inline unsigned long *end_of_stack(const struct task_struct *task)
3193c65eacbeSAndy Lutomirski {
3194c65eacbeSAndy Lutomirski 	return task->stack;
3195c65eacbeSAndy Lutomirski }
3196c65eacbeSAndy Lutomirski 
3197c65eacbeSAndy Lutomirski #elif !defined(__HAVE_THREAD_FUNCTIONS)
3198f037360fSAl Viro 
3199f7e4217bSRoman Zippel #define task_thread_info(task)	((struct thread_info *)(task)->stack)
3200c65eacbeSAndy Lutomirski #define task_stack_page(task)	((void *)(task)->stack)
3201a1261f54SAl Viro 
320210ebffdeSAl Viro static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
320310ebffdeSAl Viro {
320410ebffdeSAl Viro 	*task_thread_info(p) = *task_thread_info(org);
320510ebffdeSAl Viro 	task_thread_info(p)->task = p;
320610ebffdeSAl Viro }
320710ebffdeSAl Viro 
32086a40281aSChuck Ebbert /*
32096a40281aSChuck Ebbert  * Return the address of the last usable long on the stack.
32106a40281aSChuck Ebbert  *
32116a40281aSChuck Ebbert  * When the stack grows down, this is just above the thread
32126a40281aSChuck Ebbert  * info struct. Going any lower will corrupt the threadinfo.
32136a40281aSChuck Ebbert  *
32146a40281aSChuck Ebbert  * When the stack grows up, this is the highest address.
32156a40281aSChuck Ebbert  * Beyond that position, we corrupt data on the next page.
32166a40281aSChuck Ebbert  */
321710ebffdeSAl Viro static inline unsigned long *end_of_stack(struct task_struct *p)
321810ebffdeSAl Viro {
32196a40281aSChuck Ebbert #ifdef CONFIG_STACK_GROWSUP
32206a40281aSChuck Ebbert 	return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
32216a40281aSChuck Ebbert #else
3222f7e4217bSRoman Zippel 	return (unsigned long *)(task_thread_info(p) + 1);
32236a40281aSChuck Ebbert #endif
322410ebffdeSAl Viro }
322510ebffdeSAl Viro 
3226f037360fSAl Viro #endif
3227c6c314a6SAndy Lutomirski 
322868f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
322968f24b08SAndy Lutomirski static inline void *try_get_task_stack(struct task_struct *tsk)
323068f24b08SAndy Lutomirski {
323168f24b08SAndy Lutomirski 	return atomic_inc_not_zero(&tsk->stack_refcount) ?
323268f24b08SAndy Lutomirski 		task_stack_page(tsk) : NULL;
323368f24b08SAndy Lutomirski }
323468f24b08SAndy Lutomirski 
323568f24b08SAndy Lutomirski extern void put_task_stack(struct task_struct *tsk);
323668f24b08SAndy Lutomirski #else
3237c6c314a6SAndy Lutomirski static inline void *try_get_task_stack(struct task_struct *tsk)
3238c6c314a6SAndy Lutomirski {
3239c6c314a6SAndy Lutomirski 	return task_stack_page(tsk);
3240c6c314a6SAndy Lutomirski }
3241c6c314a6SAndy Lutomirski 
3242c6c314a6SAndy Lutomirski static inline void put_task_stack(struct task_struct *tsk) {}
324368f24b08SAndy Lutomirski #endif
3244c6c314a6SAndy Lutomirski 
3245a70857e4SAaron Tomlin #define task_stack_end_corrupted(task) \
3246a70857e4SAaron Tomlin 		(*(end_of_stack(task)) != STACK_END_MAGIC)
3247f037360fSAl Viro 
32488b05c7e6SFUJITA Tomonori static inline int object_is_on_stack(void *obj)
32498b05c7e6SFUJITA Tomonori {
32508b05c7e6SFUJITA Tomonori 	void *stack = task_stack_page(current);
32518b05c7e6SFUJITA Tomonori 
32528b05c7e6SFUJITA Tomonori 	return (obj >= stack) && (obj < (stack + THREAD_SIZE));
32538b05c7e6SFUJITA Tomonori }
32548b05c7e6SFUJITA Tomonori 
3255b235beeaSLinus Torvalds extern void thread_stack_cache_init(void);
32568c9843e5SBenjamin Herrenschmidt 
32577c9f8861SEric Sandeen #ifdef CONFIG_DEBUG_STACK_USAGE
32587c9f8861SEric Sandeen static inline unsigned long stack_not_used(struct task_struct *p)
32597c9f8861SEric Sandeen {
32607c9f8861SEric Sandeen 	unsigned long *n = end_of_stack(p);
32617c9f8861SEric Sandeen 
32627c9f8861SEric Sandeen 	do { 	/* Skip over canary */
32636c31da34SHelge Deller # ifdef CONFIG_STACK_GROWSUP
32646c31da34SHelge Deller 		n--;
32656c31da34SHelge Deller # else
32667c9f8861SEric Sandeen 		n++;
32676c31da34SHelge Deller # endif
32687c9f8861SEric Sandeen 	} while (!*n);
32697c9f8861SEric Sandeen 
32706c31da34SHelge Deller # ifdef CONFIG_STACK_GROWSUP
32716c31da34SHelge Deller 	return (unsigned long)end_of_stack(p) - (unsigned long)n;
32726c31da34SHelge Deller # else
32737c9f8861SEric Sandeen 	return (unsigned long)n - (unsigned long)end_of_stack(p);
32746c31da34SHelge Deller # endif
32757c9f8861SEric Sandeen }
32767c9f8861SEric Sandeen #endif
3277d4311ff1SAaron Tomlin extern void set_task_stack_end_magic(struct task_struct *tsk);
32787c9f8861SEric Sandeen 
32791da177e4SLinus Torvalds /* set thread flags in other task's structures
32801da177e4SLinus Torvalds  * - see asm/thread_info.h for TIF_xxxx flags available
32811da177e4SLinus Torvalds  */
32821da177e4SLinus Torvalds static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
32831da177e4SLinus Torvalds {
3284a1261f54SAl Viro 	set_ti_thread_flag(task_thread_info(tsk), flag);
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
32881da177e4SLinus Torvalds {
3289a1261f54SAl Viro 	clear_ti_thread_flag(task_thread_info(tsk), flag);
32901da177e4SLinus Torvalds }
32911da177e4SLinus Torvalds 
32921da177e4SLinus Torvalds static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
32931da177e4SLinus Torvalds {
3294a1261f54SAl Viro 	return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
32981da177e4SLinus Torvalds {
3299a1261f54SAl Viro 	return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
33001da177e4SLinus Torvalds }
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
33031da177e4SLinus Torvalds {
3304a1261f54SAl Viro 	return test_ti_thread_flag(task_thread_info(tsk), flag);
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds static inline void set_tsk_need_resched(struct task_struct *tsk)
33081da177e4SLinus Torvalds {
33091da177e4SLinus Torvalds 	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
33101da177e4SLinus Torvalds }
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds static inline void clear_tsk_need_resched(struct task_struct *tsk)
33131da177e4SLinus Torvalds {
33141da177e4SLinus Torvalds 	clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
33151da177e4SLinus Torvalds }
33161da177e4SLinus Torvalds 
33178ae121acSGregory Haskins static inline int test_tsk_need_resched(struct task_struct *tsk)
33188ae121acSGregory Haskins {
33198ae121acSGregory Haskins 	return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
33208ae121acSGregory Haskins }
33218ae121acSGregory Haskins 
3322690cc3ffSEric W. Biederman static inline int restart_syscall(void)
3323690cc3ffSEric W. Biederman {
3324690cc3ffSEric W. Biederman 	set_tsk_thread_flag(current, TIF_SIGPENDING);
3325690cc3ffSEric W. Biederman 	return -ERESTARTNOINTR;
3326690cc3ffSEric W. Biederman }
3327690cc3ffSEric W. Biederman 
33281da177e4SLinus Torvalds static inline int signal_pending(struct task_struct *p)
33291da177e4SLinus Torvalds {
33301da177e4SLinus Torvalds 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
33311da177e4SLinus Torvalds }
33321da177e4SLinus Torvalds 
3333d9588725SRoland McGrath static inline int __fatal_signal_pending(struct task_struct *p)
3334d9588725SRoland McGrath {
3335d9588725SRoland McGrath 	return unlikely(sigismember(&p->pending.signal, SIGKILL));
3336d9588725SRoland McGrath }
3337f776d12dSMatthew Wilcox 
3338f776d12dSMatthew Wilcox static inline int fatal_signal_pending(struct task_struct *p)
3339f776d12dSMatthew Wilcox {
3340f776d12dSMatthew Wilcox 	return signal_pending(p) && __fatal_signal_pending(p);
3341f776d12dSMatthew Wilcox }
3342f776d12dSMatthew Wilcox 
334316882c1eSOleg Nesterov static inline int signal_pending_state(long state, struct task_struct *p)
334416882c1eSOleg Nesterov {
334516882c1eSOleg Nesterov 	if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
334616882c1eSOleg Nesterov 		return 0;
334716882c1eSOleg Nesterov 	if (!signal_pending(p))
334816882c1eSOleg Nesterov 		return 0;
334916882c1eSOleg Nesterov 
335016882c1eSOleg Nesterov 	return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
335116882c1eSOleg Nesterov }
335216882c1eSOleg Nesterov 
33531da177e4SLinus Torvalds /*
33541da177e4SLinus Torvalds  * cond_resched() and cond_resched_lock(): latency reduction via
33551da177e4SLinus Torvalds  * explicit rescheduling in places that are safe. The return
33561da177e4SLinus Torvalds  * value indicates whether a reschedule was done in fact.
33571da177e4SLinus Torvalds  * cond_resched_lock() will drop the spinlock before scheduling,
33581da177e4SLinus Torvalds  * cond_resched_softirq() will enable bhs before scheduling.
33591da177e4SLinus Torvalds  */
336035a773a0SPeter Zijlstra #ifndef CONFIG_PREEMPT
3361c3921ab7SLinus Torvalds extern int _cond_resched(void);
336235a773a0SPeter Zijlstra #else
336335a773a0SPeter Zijlstra static inline int _cond_resched(void) { return 0; }
336435a773a0SPeter Zijlstra #endif
33656f80bd98SFrederic Weisbecker 
3366613afbf8SFrederic Weisbecker #define cond_resched() ({			\
33673427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, 0);	\
3368613afbf8SFrederic Weisbecker 	_cond_resched();			\
3369613afbf8SFrederic Weisbecker })
33706f80bd98SFrederic Weisbecker 
3371613afbf8SFrederic Weisbecker extern int __cond_resched_lock(spinlock_t *lock);
3372613afbf8SFrederic Weisbecker 
3373613afbf8SFrederic Weisbecker #define cond_resched_lock(lock) ({				\
33743427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
3375613afbf8SFrederic Weisbecker 	__cond_resched_lock(lock);				\
3376613afbf8SFrederic Weisbecker })
3377613afbf8SFrederic Weisbecker 
3378613afbf8SFrederic Weisbecker extern int __cond_resched_softirq(void);
3379613afbf8SFrederic Weisbecker 
3380613afbf8SFrederic Weisbecker #define cond_resched_softirq() ({					\
33813427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET);	\
3382613afbf8SFrederic Weisbecker 	__cond_resched_softirq();					\
3383613afbf8SFrederic Weisbecker })
33841da177e4SLinus Torvalds 
3385f6f3c437SSimon Horman static inline void cond_resched_rcu(void)
3386f6f3c437SSimon Horman {
3387f6f3c437SSimon Horman #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
3388f6f3c437SSimon Horman 	rcu_read_unlock();
3389f6f3c437SSimon Horman 	cond_resched();
3390f6f3c437SSimon Horman 	rcu_read_lock();
3391f6f3c437SSimon Horman #endif
3392f6f3c437SSimon Horman }
3393f6f3c437SSimon Horman 
3394d1c6d149SVegard Nossum static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
3395d1c6d149SVegard Nossum {
3396d1c6d149SVegard Nossum #ifdef CONFIG_DEBUG_PREEMPT
3397d1c6d149SVegard Nossum 	return p->preempt_disable_ip;
3398d1c6d149SVegard Nossum #else
3399d1c6d149SVegard Nossum 	return 0;
3400d1c6d149SVegard Nossum #endif
3401d1c6d149SVegard Nossum }
3402d1c6d149SVegard Nossum 
34031da177e4SLinus Torvalds /*
34041da177e4SLinus Torvalds  * Does a critical section need to be broken due to another
340595c354feSNick Piggin  * task waiting?: (technically does not depend on CONFIG_PREEMPT,
340695c354feSNick Piggin  * but a general need for low latency)
34071da177e4SLinus Torvalds  */
340895c354feSNick Piggin static inline int spin_needbreak(spinlock_t *lock)
34091da177e4SLinus Torvalds {
341095c354feSNick Piggin #ifdef CONFIG_PREEMPT
341195c354feSNick Piggin 	return spin_is_contended(lock);
341295c354feSNick Piggin #else
34131da177e4SLinus Torvalds 	return 0;
341495c354feSNick Piggin #endif
34151da177e4SLinus Torvalds }
34161da177e4SLinus Torvalds 
34177bb44adeSRoland McGrath /*
3418ee761f62SThomas Gleixner  * Idle thread specific functions to determine the need_resched
341969dd0f84SPeter Zijlstra  * polling state.
3420ee761f62SThomas Gleixner  */
342169dd0f84SPeter Zijlstra #ifdef TIF_POLLING_NRFLAG
3422ee761f62SThomas Gleixner static inline int tsk_is_polling(struct task_struct *p)
3423ee761f62SThomas Gleixner {
3424ee761f62SThomas Gleixner 	return test_tsk_thread_flag(p, TIF_POLLING_NRFLAG);
3425ee761f62SThomas Gleixner }
3426ea811747SPeter Zijlstra 
3427ea811747SPeter Zijlstra static inline void __current_set_polling(void)
34283a98f871SThomas Gleixner {
34293a98f871SThomas Gleixner 	set_thread_flag(TIF_POLLING_NRFLAG);
34303a98f871SThomas Gleixner }
34313a98f871SThomas Gleixner 
3432ea811747SPeter Zijlstra static inline bool __must_check current_set_polling_and_test(void)
3433ea811747SPeter Zijlstra {
3434ea811747SPeter Zijlstra 	__current_set_polling();
3435ea811747SPeter Zijlstra 
3436ea811747SPeter Zijlstra 	/*
3437ea811747SPeter Zijlstra 	 * Polling state must be visible before we test NEED_RESCHED,
34388875125eSKirill Tkhai 	 * paired by resched_curr()
3439ea811747SPeter Zijlstra 	 */
34404e857c58SPeter Zijlstra 	smp_mb__after_atomic();
3441ea811747SPeter Zijlstra 
3442ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3443ea811747SPeter Zijlstra }
3444ea811747SPeter Zijlstra 
3445ea811747SPeter Zijlstra static inline void __current_clr_polling(void)
34463a98f871SThomas Gleixner {
34473a98f871SThomas Gleixner 	clear_thread_flag(TIF_POLLING_NRFLAG);
34483a98f871SThomas Gleixner }
3449ea811747SPeter Zijlstra 
3450ea811747SPeter Zijlstra static inline bool __must_check current_clr_polling_and_test(void)
3451ea811747SPeter Zijlstra {
3452ea811747SPeter Zijlstra 	__current_clr_polling();
3453ea811747SPeter Zijlstra 
3454ea811747SPeter Zijlstra 	/*
3455ea811747SPeter Zijlstra 	 * Polling state must be visible before we test NEED_RESCHED,
34568875125eSKirill Tkhai 	 * paired by resched_curr()
3457ea811747SPeter Zijlstra 	 */
34584e857c58SPeter Zijlstra 	smp_mb__after_atomic();
3459ea811747SPeter Zijlstra 
3460ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3461ea811747SPeter Zijlstra }
3462ea811747SPeter Zijlstra 
3463ee761f62SThomas Gleixner #else
3464ee761f62SThomas Gleixner static inline int tsk_is_polling(struct task_struct *p) { return 0; }
3465ea811747SPeter Zijlstra static inline void __current_set_polling(void) { }
3466ea811747SPeter Zijlstra static inline void __current_clr_polling(void) { }
3467ea811747SPeter Zijlstra 
3468ea811747SPeter Zijlstra static inline bool __must_check current_set_polling_and_test(void)
3469ea811747SPeter Zijlstra {
3470ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3471ea811747SPeter Zijlstra }
3472ea811747SPeter Zijlstra static inline bool __must_check current_clr_polling_and_test(void)
3473ea811747SPeter Zijlstra {
3474ea811747SPeter Zijlstra 	return unlikely(tif_need_resched());
3475ea811747SPeter Zijlstra }
3476ee761f62SThomas Gleixner #endif
3477ee761f62SThomas Gleixner 
34788cb75e0cSPeter Zijlstra static inline void current_clr_polling(void)
34798cb75e0cSPeter Zijlstra {
34808cb75e0cSPeter Zijlstra 	__current_clr_polling();
34818cb75e0cSPeter Zijlstra 
34828cb75e0cSPeter Zijlstra 	/*
34838cb75e0cSPeter Zijlstra 	 * Ensure we check TIF_NEED_RESCHED after we clear the polling bit.
34848cb75e0cSPeter Zijlstra 	 * Once the bit is cleared, we'll get IPIs with every new
34858cb75e0cSPeter Zijlstra 	 * TIF_NEED_RESCHED and the IPI handler, scheduler_ipi(), will also
34868cb75e0cSPeter Zijlstra 	 * fold.
34878cb75e0cSPeter Zijlstra 	 */
34888875125eSKirill Tkhai 	smp_mb(); /* paired with resched_curr() */
34898cb75e0cSPeter Zijlstra 
34908cb75e0cSPeter Zijlstra 	preempt_fold_need_resched();
34918cb75e0cSPeter Zijlstra }
34928cb75e0cSPeter Zijlstra 
349375f93fedSPeter Zijlstra static __always_inline bool need_resched(void)
349475f93fedSPeter Zijlstra {
349575f93fedSPeter Zijlstra 	return unlikely(tif_need_resched());
349675f93fedSPeter Zijlstra }
349775f93fedSPeter Zijlstra 
3498ee761f62SThomas Gleixner /*
3499f06febc9SFrank Mayhar  * Thread group CPU time accounting.
3500f06febc9SFrank Mayhar  */
35014cd4c1b4SPeter Zijlstra void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
35024da94d49SPeter Zijlstra void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
3503f06febc9SFrank Mayhar 
3504f06febc9SFrank Mayhar /*
35057bb44adeSRoland McGrath  * Reevaluate whether the task has signals pending delivery.
35067bb44adeSRoland McGrath  * Wake the task if so.
35077bb44adeSRoland McGrath  * This is required every time the blocked sigset_t changes.
35087bb44adeSRoland McGrath  * callers must hold sighand->siglock.
35097bb44adeSRoland McGrath  */
35107bb44adeSRoland McGrath extern void recalc_sigpending_and_wake(struct task_struct *t);
35111da177e4SLinus Torvalds extern void recalc_sigpending(void);
35121da177e4SLinus Torvalds 
3513910ffdb1SOleg Nesterov extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
3514910ffdb1SOleg Nesterov 
3515910ffdb1SOleg Nesterov static inline void signal_wake_up(struct task_struct *t, bool resume)
3516910ffdb1SOleg Nesterov {
3517910ffdb1SOleg Nesterov 	signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
3518910ffdb1SOleg Nesterov }
3519910ffdb1SOleg Nesterov static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
3520910ffdb1SOleg Nesterov {
3521910ffdb1SOleg Nesterov 	signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
3522910ffdb1SOleg Nesterov }
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds /*
35251da177e4SLinus Torvalds  * Wrappers for p->thread_info->cpu access. No-op on UP.
35261da177e4SLinus Torvalds  */
35271da177e4SLinus Torvalds #ifdef CONFIG_SMP
35281da177e4SLinus Torvalds 
35291da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
35301da177e4SLinus Torvalds {
3531c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
3532c65eacbeSAndy Lutomirski 	return p->cpu;
3533c65eacbeSAndy Lutomirski #else
3534a1261f54SAl Viro 	return task_thread_info(p)->cpu;
3535c65eacbeSAndy Lutomirski #endif
35361da177e4SLinus Torvalds }
35371da177e4SLinus Torvalds 
3538b32e86b4SIngo Molnar static inline int task_node(const struct task_struct *p)
3539b32e86b4SIngo Molnar {
3540b32e86b4SIngo Molnar 	return cpu_to_node(task_cpu(p));
3541b32e86b4SIngo Molnar }
3542b32e86b4SIngo Molnar 
3543c65cc870SIngo Molnar extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds #else
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
35481da177e4SLinus Torvalds {
35491da177e4SLinus Torvalds 	return 0;
35501da177e4SLinus Torvalds }
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
35531da177e4SLinus Torvalds {
35541da177e4SLinus Torvalds }
35551da177e4SLinus Torvalds 
35561da177e4SLinus Torvalds #endif /* CONFIG_SMP */
35571da177e4SLinus Torvalds 
3558d9345c65SPan Xinhui /*
3559d9345c65SPan Xinhui  * In order to reduce various lock holder preemption latencies provide an
3560d9345c65SPan Xinhui  * interface to see if a vCPU is currently running or not.
3561d9345c65SPan Xinhui  *
3562d9345c65SPan Xinhui  * This allows us to terminate optimistic spin loops and block, analogous to
3563d9345c65SPan Xinhui  * the native optimistic spin heuristic of testing if the lock owner task is
3564d9345c65SPan Xinhui  * running or not.
3565d9345c65SPan Xinhui  */
3566d9345c65SPan Xinhui #ifndef vcpu_is_preempted
3567d9345c65SPan Xinhui # define vcpu_is_preempted(cpu)	false
3568d9345c65SPan Xinhui #endif
3569d9345c65SPan Xinhui 
357096f874e2SRusty Russell extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
357196f874e2SRusty Russell extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
35725c45bf27SSiddha, Suresh B 
35737c941438SDhaval Giani #ifdef CONFIG_CGROUP_SCHED
357407e06b01SYong Zhang extern struct task_group root_task_group;
35758323f26cSPeter Zijlstra #endif /* CONFIG_CGROUP_SCHED */
35769b5b7751SSrivatsa Vaddagiri 
357754e99124SDhaval Giani extern int task_can_switch_user(struct user_struct *up,
357854e99124SDhaval Giani 					struct task_struct *tsk);
357954e99124SDhaval Giani 
35804b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
35814b98d11bSAlexey Dobriyan static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
35824b98d11bSAlexey Dobriyan {
3583940389b8SAndrea Righi 	tsk->ioac.rchar += amt;
35844b98d11bSAlexey Dobriyan }
35854b98d11bSAlexey Dobriyan 
35864b98d11bSAlexey Dobriyan static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
35874b98d11bSAlexey Dobriyan {
3588940389b8SAndrea Righi 	tsk->ioac.wchar += amt;
35894b98d11bSAlexey Dobriyan }
35904b98d11bSAlexey Dobriyan 
35914b98d11bSAlexey Dobriyan static inline void inc_syscr(struct task_struct *tsk)
35924b98d11bSAlexey Dobriyan {
3593940389b8SAndrea Righi 	tsk->ioac.syscr++;
35944b98d11bSAlexey Dobriyan }
35954b98d11bSAlexey Dobriyan 
35964b98d11bSAlexey Dobriyan static inline void inc_syscw(struct task_struct *tsk)
35974b98d11bSAlexey Dobriyan {
3598940389b8SAndrea Righi 	tsk->ioac.syscw++;
35994b98d11bSAlexey Dobriyan }
36004b98d11bSAlexey Dobriyan #else
36014b98d11bSAlexey Dobriyan static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
36024b98d11bSAlexey Dobriyan {
36034b98d11bSAlexey Dobriyan }
36044b98d11bSAlexey Dobriyan 
36054b98d11bSAlexey Dobriyan static inline void add_wchar(struct task_struct *tsk, ssize_t amt)
36064b98d11bSAlexey Dobriyan {
36074b98d11bSAlexey Dobriyan }
36084b98d11bSAlexey Dobriyan 
36094b98d11bSAlexey Dobriyan static inline void inc_syscr(struct task_struct *tsk)
36104b98d11bSAlexey Dobriyan {
36114b98d11bSAlexey Dobriyan }
36124b98d11bSAlexey Dobriyan 
36134b98d11bSAlexey Dobriyan static inline void inc_syscw(struct task_struct *tsk)
36144b98d11bSAlexey Dobriyan {
36154b98d11bSAlexey Dobriyan }
36164b98d11bSAlexey Dobriyan #endif
36174b98d11bSAlexey Dobriyan 
361882455257SDave Hansen #ifndef TASK_SIZE_OF
361982455257SDave Hansen #define TASK_SIZE_OF(tsk)	TASK_SIZE
362082455257SDave Hansen #endif
362182455257SDave Hansen 
3622f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG
3623cf475ad2SBalbir Singh extern void mm_update_next_owner(struct mm_struct *mm);
3624cf475ad2SBalbir Singh #else
3625cf475ad2SBalbir Singh static inline void mm_update_next_owner(struct mm_struct *mm)
3626cf475ad2SBalbir Singh {
3627cf475ad2SBalbir Singh }
3628f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */
3629cf475ad2SBalbir Singh 
36303e10e716SJiri Slaby static inline unsigned long task_rlimit(const struct task_struct *tsk,
36313e10e716SJiri Slaby 		unsigned int limit)
36323e10e716SJiri Slaby {
3633316c1608SJason Low 	return READ_ONCE(tsk->signal->rlim[limit].rlim_cur);
36343e10e716SJiri Slaby }
36353e10e716SJiri Slaby 
36363e10e716SJiri Slaby static inline unsigned long task_rlimit_max(const struct task_struct *tsk,
36373e10e716SJiri Slaby 		unsigned int limit)
36383e10e716SJiri Slaby {
3639316c1608SJason Low 	return READ_ONCE(tsk->signal->rlim[limit].rlim_max);
36403e10e716SJiri Slaby }
36413e10e716SJiri Slaby 
36423e10e716SJiri Slaby static inline unsigned long rlimit(unsigned int limit)
36433e10e716SJiri Slaby {
36443e10e716SJiri Slaby 	return task_rlimit(current, limit);
36453e10e716SJiri Slaby }
36463e10e716SJiri Slaby 
36473e10e716SJiri Slaby static inline unsigned long rlimit_max(unsigned int limit)
36483e10e716SJiri Slaby {
36493e10e716SJiri Slaby 	return task_rlimit_max(current, limit);
36503e10e716SJiri Slaby }
36513e10e716SJiri Slaby 
365258919e83SRafael J. Wysocki #define SCHED_CPUFREQ_RT	(1U << 0)
365358919e83SRafael J. Wysocki #define SCHED_CPUFREQ_DL	(1U << 1)
36548c34ab19SRafael J. Wysocki #define SCHED_CPUFREQ_IOWAIT	(1U << 2)
365558919e83SRafael J. Wysocki 
365658919e83SRafael J. Wysocki #define SCHED_CPUFREQ_RT_DL	(SCHED_CPUFREQ_RT | SCHED_CPUFREQ_DL)
365758919e83SRafael J. Wysocki 
3658adaf9fcdSRafael J. Wysocki #ifdef CONFIG_CPU_FREQ
3659adaf9fcdSRafael J. Wysocki struct update_util_data {
366058919e83SRafael J. Wysocki        void (*func)(struct update_util_data *data, u64 time, unsigned int flags);
3661adaf9fcdSRafael J. Wysocki };
3662adaf9fcdSRafael J. Wysocki 
36630bed612bSRafael J. Wysocki void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
36640bed612bSRafael J. Wysocki                        void (*func)(struct update_util_data *data, u64 time,
366558919e83SRafael J. Wysocki 				    unsigned int flags));
36660bed612bSRafael J. Wysocki void cpufreq_remove_update_util_hook(int cpu);
3667adaf9fcdSRafael J. Wysocki #endif /* CONFIG_CPU_FREQ */
3668adaf9fcdSRafael J. Wysocki 
36691da177e4SLinus Torvalds #endif
3670