xref: /linux/include/linux/sched.h (revision a509a7cd79747074a2c018a45bbbc52d1f4aed44)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_SCHED_H
31da177e4SLinus Torvalds #define _LINUX_SCHED_H
41da177e4SLinus Torvalds 
55eca1c10SIngo Molnar /*
65eca1c10SIngo Molnar  * Define 'struct task_struct' and provide the main scheduler
75eca1c10SIngo Molnar  * APIs (schedule(), wakeup variants, etc.)
85eca1c10SIngo Molnar  */
95eca1c10SIngo Molnar 
10607ca46eSDavid Howells #include <uapi/linux/sched.h>
11b7b3c76aSDavid Woodhouse 
1270b8157eSIngo Molnar #include <asm/current.h>
1370b8157eSIngo Molnar 
145eca1c10SIngo Molnar #include <linux/pid.h>
155eca1c10SIngo Molnar #include <linux/sem.h>
165eca1c10SIngo Molnar #include <linux/shm.h>
175eca1c10SIngo Molnar #include <linux/kcov.h>
185eca1c10SIngo Molnar #include <linux/mutex.h>
195eca1c10SIngo Molnar #include <linux/plist.h>
205eca1c10SIngo Molnar #include <linux/hrtimer.h>
215eca1c10SIngo Molnar #include <linux/seccomp.h>
225eca1c10SIngo Molnar #include <linux/nodemask.h>
235eca1c10SIngo Molnar #include <linux/rcupdate.h>
24ec1d2819SElena Reshetova #include <linux/refcount.h>
255eca1c10SIngo Molnar #include <linux/resource.h>
265eca1c10SIngo Molnar #include <linux/latencytop.h>
275eca1c10SIngo Molnar #include <linux/sched/prio.h>
285eca1c10SIngo Molnar #include <linux/signal_types.h>
295eca1c10SIngo Molnar #include <linux/mm_types_task.h>
305eca1c10SIngo Molnar #include <linux/task_io_accounting.h>
31d7822b1eSMathieu Desnoyers #include <linux/rseq.h>
325eca1c10SIngo Molnar 
335eca1c10SIngo Molnar /* task_struct member predeclarations (sorted alphabetically): */
34c7af7877SIngo Molnar struct audit_context;
35c7af7877SIngo Molnar struct backing_dev_info;
36c7af7877SIngo Molnar struct bio_list;
37c7af7877SIngo Molnar struct blk_plug;
383c93a0c0SQais Yousef struct capture_control;
39c7af7877SIngo Molnar struct cfs_rq;
40c7af7877SIngo Molnar struct fs_struct;
41c7af7877SIngo Molnar struct futex_pi_state;
42c7af7877SIngo Molnar struct io_context;
43c7af7877SIngo Molnar struct mempolicy;
44c7af7877SIngo Molnar struct nameidata;
45c7af7877SIngo Molnar struct nsproxy;
46c7af7877SIngo Molnar struct perf_event_context;
47c7af7877SIngo Molnar struct pid_namespace;
48c7af7877SIngo Molnar struct pipe_inode_info;
49c7af7877SIngo Molnar struct rcu_node;
50c7af7877SIngo Molnar struct reclaim_state;
51c7af7877SIngo Molnar struct robust_list_head;
523c93a0c0SQais Yousef struct root_domain;
533c93a0c0SQais Yousef struct rq;
54e2d1e2aeSIngo Molnar struct sched_attr;
55e2d1e2aeSIngo Molnar struct sched_param;
5643ae34cbSIngo Molnar struct seq_file;
57c7af7877SIngo Molnar struct sighand_struct;
58c7af7877SIngo Molnar struct signal_struct;
59c7af7877SIngo Molnar struct task_delay_info;
604cf86d77SIngo Molnar struct task_group;
611da177e4SLinus Torvalds 
624a8342d2SLinus Torvalds /*
634a8342d2SLinus Torvalds  * Task state bitmask. NOTE! These bits are also
644a8342d2SLinus Torvalds  * encoded in fs/proc/array.c: get_task_state().
654a8342d2SLinus Torvalds  *
664a8342d2SLinus Torvalds  * We have two separate sets of flags: task->state
674a8342d2SLinus Torvalds  * is about runnability, while task->exit_state are
684a8342d2SLinus Torvalds  * about the task exiting. Confusing, but this way
694a8342d2SLinus Torvalds  * modifying one set can't modify the other one by
704a8342d2SLinus Torvalds  * mistake.
714a8342d2SLinus Torvalds  */
725eca1c10SIngo Molnar 
735eca1c10SIngo Molnar /* Used in tsk->state: */
7492c4bc9fSPeter Zijlstra #define TASK_RUNNING			0x0000
7592c4bc9fSPeter Zijlstra #define TASK_INTERRUPTIBLE		0x0001
7692c4bc9fSPeter Zijlstra #define TASK_UNINTERRUPTIBLE		0x0002
7792c4bc9fSPeter Zijlstra #define __TASK_STOPPED			0x0004
7892c4bc9fSPeter Zijlstra #define __TASK_TRACED			0x0008
795eca1c10SIngo Molnar /* Used in tsk->exit_state: */
8092c4bc9fSPeter Zijlstra #define EXIT_DEAD			0x0010
8192c4bc9fSPeter Zijlstra #define EXIT_ZOMBIE			0x0020
82abd50b39SOleg Nesterov #define EXIT_TRACE			(EXIT_ZOMBIE | EXIT_DEAD)
835eca1c10SIngo Molnar /* Used in tsk->state again: */
848ef9925bSPeter Zijlstra #define TASK_PARKED			0x0040
858ef9925bSPeter Zijlstra #define TASK_DEAD			0x0080
868ef9925bSPeter Zijlstra #define TASK_WAKEKILL			0x0100
878ef9925bSPeter Zijlstra #define TASK_WAKING			0x0200
8892c4bc9fSPeter Zijlstra #define TASK_NOLOAD			0x0400
8992c4bc9fSPeter Zijlstra #define TASK_NEW			0x0800
9092c4bc9fSPeter Zijlstra #define TASK_STATE_MAX			0x1000
91f021a3c2SMatthew Wilcox 
925eca1c10SIngo Molnar /* Convenience macros for the sake of set_current_state: */
93f021a3c2SMatthew Wilcox #define TASK_KILLABLE			(TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
94f021a3c2SMatthew Wilcox #define TASK_STOPPED			(TASK_WAKEKILL | __TASK_STOPPED)
95f021a3c2SMatthew Wilcox #define TASK_TRACED			(TASK_WAKEKILL | __TASK_TRACED)
961da177e4SLinus Torvalds 
9780ed87c8SPeter Zijlstra #define TASK_IDLE			(TASK_UNINTERRUPTIBLE | TASK_NOLOAD)
9880ed87c8SPeter Zijlstra 
995eca1c10SIngo Molnar /* Convenience macros for the sake of wake_up(): */
10092a1f4bcSMatthew Wilcox #define TASK_NORMAL			(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
10192a1f4bcSMatthew Wilcox 
1025eca1c10SIngo Molnar /* get_task_state(): */
10392a1f4bcSMatthew Wilcox #define TASK_REPORT			(TASK_RUNNING | TASK_INTERRUPTIBLE | \
104f021a3c2SMatthew Wilcox 					 TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
1058ef9925bSPeter Zijlstra 					 __TASK_TRACED | EXIT_DEAD | EXIT_ZOMBIE | \
1068ef9925bSPeter Zijlstra 					 TASK_PARKED)
10792a1f4bcSMatthew Wilcox 
108f021a3c2SMatthew Wilcox #define task_is_traced(task)		((task->state & __TASK_TRACED) != 0)
1095eca1c10SIngo Molnar 
110f021a3c2SMatthew Wilcox #define task_is_stopped(task)		((task->state & __TASK_STOPPED) != 0)
1115eca1c10SIngo Molnar 
1125eca1c10SIngo Molnar #define task_is_stopped_or_traced(task)	((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
1135eca1c10SIngo Molnar 
1145eca1c10SIngo Molnar #define task_contributes_to_load(task)	((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
11580ed87c8SPeter Zijlstra 					 (task->flags & PF_FROZEN) == 0 && \
11680ed87c8SPeter Zijlstra 					 (task->state & TASK_NOLOAD) == 0)
1171da177e4SLinus Torvalds 
1188eb23b9fSPeter Zijlstra #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
1198eb23b9fSPeter Zijlstra 
120b5bf9a90SPeter Zijlstra /*
121b5bf9a90SPeter Zijlstra  * Special states are those that do not use the normal wait-loop pattern. See
122b5bf9a90SPeter Zijlstra  * the comment with set_special_state().
123b5bf9a90SPeter Zijlstra  */
124b5bf9a90SPeter Zijlstra #define is_special_task_state(state)				\
1251cef1150SPeter Zijlstra 	((state) & (__TASK_STOPPED | __TASK_TRACED | TASK_PARKED | TASK_DEAD))
126b5bf9a90SPeter Zijlstra 
1278eb23b9fSPeter Zijlstra #define __set_current_state(state_value)			\
1288eb23b9fSPeter Zijlstra 	do {							\
129b5bf9a90SPeter Zijlstra 		WARN_ON_ONCE(is_special_task_state(state_value));\
1308eb23b9fSPeter Zijlstra 		current->task_state_change = _THIS_IP_;		\
1318eb23b9fSPeter Zijlstra 		current->state = (state_value);			\
1328eb23b9fSPeter Zijlstra 	} while (0)
133b5bf9a90SPeter Zijlstra 
1348eb23b9fSPeter Zijlstra #define set_current_state(state_value)				\
1358eb23b9fSPeter Zijlstra 	do {							\
136b5bf9a90SPeter Zijlstra 		WARN_ON_ONCE(is_special_task_state(state_value));\
1378eb23b9fSPeter Zijlstra 		current->task_state_change = _THIS_IP_;		\
138b92b8b35SPeter Zijlstra 		smp_store_mb(current->state, (state_value));	\
1398eb23b9fSPeter Zijlstra 	} while (0)
1408eb23b9fSPeter Zijlstra 
141b5bf9a90SPeter Zijlstra #define set_special_state(state_value)					\
142b5bf9a90SPeter Zijlstra 	do {								\
143b5bf9a90SPeter Zijlstra 		unsigned long flags; /* may shadow */			\
144b5bf9a90SPeter Zijlstra 		WARN_ON_ONCE(!is_special_task_state(state_value));	\
145b5bf9a90SPeter Zijlstra 		raw_spin_lock_irqsave(&current->pi_lock, flags);	\
146b5bf9a90SPeter Zijlstra 		current->task_state_change = _THIS_IP_;			\
147b5bf9a90SPeter Zijlstra 		current->state = (state_value);				\
148b5bf9a90SPeter Zijlstra 		raw_spin_unlock_irqrestore(&current->pi_lock, flags);	\
149b5bf9a90SPeter Zijlstra 	} while (0)
1508eb23b9fSPeter Zijlstra #else
151498d0c57SAndrew Morton /*
152498d0c57SAndrew Morton  * set_current_state() includes a barrier so that the write of current->state
153498d0c57SAndrew Morton  * is correctly serialised wrt the caller's subsequent test of whether to
154498d0c57SAndrew Morton  * actually sleep:
155498d0c57SAndrew Morton  *
156a2250238SPeter Zijlstra  *   for (;;) {
157498d0c57SAndrew Morton  *	set_current_state(TASK_UNINTERRUPTIBLE);
158a2250238SPeter Zijlstra  *	if (!need_sleep)
159a2250238SPeter Zijlstra  *		break;
160498d0c57SAndrew Morton  *
161a2250238SPeter Zijlstra  *	schedule();
162a2250238SPeter Zijlstra  *   }
163a2250238SPeter Zijlstra  *   __set_current_state(TASK_RUNNING);
164a2250238SPeter Zijlstra  *
165a2250238SPeter Zijlstra  * If the caller does not need such serialisation (because, for instance, the
166a2250238SPeter Zijlstra  * condition test and condition change and wakeup are under the same lock) then
167a2250238SPeter Zijlstra  * use __set_current_state().
168a2250238SPeter Zijlstra  *
169a2250238SPeter Zijlstra  * The above is typically ordered against the wakeup, which does:
170a2250238SPeter Zijlstra  *
171a2250238SPeter Zijlstra  *   need_sleep = false;
172a2250238SPeter Zijlstra  *   wake_up_state(p, TASK_UNINTERRUPTIBLE);
173a2250238SPeter Zijlstra  *
1747696f991SAndrea Parri  * where wake_up_state() executes a full memory barrier before accessing the
1757696f991SAndrea Parri  * task state.
176a2250238SPeter Zijlstra  *
177a2250238SPeter Zijlstra  * Wakeup will do: if (@state & p->state) p->state = TASK_RUNNING, that is,
178a2250238SPeter Zijlstra  * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a
179a2250238SPeter Zijlstra  * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING).
180a2250238SPeter Zijlstra  *
181b5bf9a90SPeter Zijlstra  * However, with slightly different timing the wakeup TASK_RUNNING store can
182dfcb245eSIngo Molnar  * also collide with the TASK_UNINTERRUPTIBLE store. Losing that store is not
183b5bf9a90SPeter Zijlstra  * a problem either because that will result in one extra go around the loop
184b5bf9a90SPeter Zijlstra  * and our @cond test will save the day.
185a2250238SPeter Zijlstra  *
186a2250238SPeter Zijlstra  * Also see the comments of try_to_wake_up().
187498d0c57SAndrew Morton  */
188b5bf9a90SPeter Zijlstra #define __set_current_state(state_value)				\
189b5bf9a90SPeter Zijlstra 	current->state = (state_value)
190b5bf9a90SPeter Zijlstra 
191b5bf9a90SPeter Zijlstra #define set_current_state(state_value)					\
192b5bf9a90SPeter Zijlstra 	smp_store_mb(current->state, (state_value))
193b5bf9a90SPeter Zijlstra 
194b5bf9a90SPeter Zijlstra /*
195b5bf9a90SPeter Zijlstra  * set_special_state() should be used for those states when the blocking task
196b5bf9a90SPeter Zijlstra  * can not use the regular condition based wait-loop. In that case we must
197b5bf9a90SPeter Zijlstra  * serialize against wakeups such that any possible in-flight TASK_RUNNING stores
198b5bf9a90SPeter Zijlstra  * will not collide with our state change.
199b5bf9a90SPeter Zijlstra  */
200b5bf9a90SPeter Zijlstra #define set_special_state(state_value)					\
201b5bf9a90SPeter Zijlstra 	do {								\
202b5bf9a90SPeter Zijlstra 		unsigned long flags; /* may shadow */			\
203b5bf9a90SPeter Zijlstra 		raw_spin_lock_irqsave(&current->pi_lock, flags);	\
204b5bf9a90SPeter Zijlstra 		current->state = (state_value);				\
205b5bf9a90SPeter Zijlstra 		raw_spin_unlock_irqrestore(&current->pi_lock, flags);	\
206b5bf9a90SPeter Zijlstra 	} while (0)
207b5bf9a90SPeter Zijlstra 
2088eb23b9fSPeter Zijlstra #endif
2098eb23b9fSPeter Zijlstra 
2105eca1c10SIngo Molnar /* Task command name length: */
2111da177e4SLinus Torvalds #define TASK_COMM_LEN			16
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds extern void scheduler_tick(void);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #define	MAX_SCHEDULE_TIMEOUT		LONG_MAX
2165eca1c10SIngo Molnar 
2175eca1c10SIngo Molnar extern long schedule_timeout(long timeout);
2185eca1c10SIngo Molnar extern long schedule_timeout_interruptible(long timeout);
2195eca1c10SIngo Molnar extern long schedule_timeout_killable(long timeout);
2205eca1c10SIngo Molnar extern long schedule_timeout_uninterruptible(long timeout);
2215eca1c10SIngo Molnar extern long schedule_timeout_idle(long timeout);
2221da177e4SLinus Torvalds asmlinkage void schedule(void);
223c5491ea7SThomas Gleixner extern void schedule_preempt_disabled(void);
2241da177e4SLinus Torvalds 
22510ab5643STejun Heo extern int __must_check io_schedule_prepare(void);
22610ab5643STejun Heo extern void io_schedule_finish(int token);
2279cff8adeSNeilBrown extern long io_schedule_timeout(long timeout);
22810ab5643STejun Heo extern void io_schedule(void);
2299cff8adeSNeilBrown 
230f06febc9SFrank Mayhar /**
2310ba42a59SMasanari Iida  * struct prev_cputime - snapshot of system and user cputime
232d37f761dSFrederic Weisbecker  * @utime: time spent in user mode
233d37f761dSFrederic Weisbecker  * @stime: time spent in system mode
2349d7fb042SPeter Zijlstra  * @lock: protects the above two fields
235d37f761dSFrederic Weisbecker  *
2369d7fb042SPeter Zijlstra  * Stores previous user/system time values such that we can guarantee
2379d7fb042SPeter Zijlstra  * monotonicity.
238d37f761dSFrederic Weisbecker  */
2399d7fb042SPeter Zijlstra struct prev_cputime {
2409d7fb042SPeter Zijlstra #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
2415613fda9SFrederic Weisbecker 	u64				utime;
2425613fda9SFrederic Weisbecker 	u64				stime;
2439d7fb042SPeter Zijlstra 	raw_spinlock_t			lock;
2449d7fb042SPeter Zijlstra #endif
245d37f761dSFrederic Weisbecker };
246d37f761dSFrederic Weisbecker 
247d37f761dSFrederic Weisbecker /**
248f06febc9SFrank Mayhar  * struct task_cputime - collected CPU time counts
2495613fda9SFrederic Weisbecker  * @utime:		time spent in user mode, in nanoseconds
2505613fda9SFrederic Weisbecker  * @stime:		time spent in kernel mode, in nanoseconds
251f06febc9SFrank Mayhar  * @sum_exec_runtime:	total time spent on the CPU, in nanoseconds
252f06febc9SFrank Mayhar  *
2539d7fb042SPeter Zijlstra  * This structure groups together three kinds of CPU time that are tracked for
2549d7fb042SPeter Zijlstra  * threads and thread groups.  Most things considering CPU time want to group
2559d7fb042SPeter Zijlstra  * these counts together and treat all three of them in parallel.
256f06febc9SFrank Mayhar  */
257f06febc9SFrank Mayhar struct task_cputime {
2585613fda9SFrederic Weisbecker 	u64				utime;
2595613fda9SFrederic Weisbecker 	u64				stime;
260f06febc9SFrank Mayhar 	unsigned long long		sum_exec_runtime;
261f06febc9SFrank Mayhar };
2629d7fb042SPeter Zijlstra 
2635eca1c10SIngo Molnar /* Alternate field names when used on cache expirations: */
264f06febc9SFrank Mayhar #define virt_exp			utime
2659d7fb042SPeter Zijlstra #define prof_exp			stime
266f06febc9SFrank Mayhar #define sched_exp			sum_exec_runtime
267f06febc9SFrank Mayhar 
268bac5b6b6SFrederic Weisbecker enum vtime_state {
269bac5b6b6SFrederic Weisbecker 	/* Task is sleeping or running in a CPU with VTIME inactive: */
270bac5b6b6SFrederic Weisbecker 	VTIME_INACTIVE = 0,
271bac5b6b6SFrederic Weisbecker 	/* Task runs in userspace in a CPU with VTIME active: */
272bac5b6b6SFrederic Weisbecker 	VTIME_USER,
273bac5b6b6SFrederic Weisbecker 	/* Task runs in kernelspace in a CPU with VTIME active: */
274bac5b6b6SFrederic Weisbecker 	VTIME_SYS,
275bac5b6b6SFrederic Weisbecker };
276bac5b6b6SFrederic Weisbecker 
277bac5b6b6SFrederic Weisbecker struct vtime {
278bac5b6b6SFrederic Weisbecker 	seqcount_t		seqcount;
279bac5b6b6SFrederic Weisbecker 	unsigned long long	starttime;
280bac5b6b6SFrederic Weisbecker 	enum vtime_state	state;
2812a42eb95SWanpeng Li 	u64			utime;
2822a42eb95SWanpeng Li 	u64			stime;
2832a42eb95SWanpeng Li 	u64			gtime;
284bac5b6b6SFrederic Weisbecker };
285bac5b6b6SFrederic Weisbecker 
28669842cbaSPatrick Bellasi /*
28769842cbaSPatrick Bellasi  * Utilization clamp constraints.
28869842cbaSPatrick Bellasi  * @UCLAMP_MIN:	Minimum utilization
28969842cbaSPatrick Bellasi  * @UCLAMP_MAX:	Maximum utilization
29069842cbaSPatrick Bellasi  * @UCLAMP_CNT:	Utilization clamp constraints count
29169842cbaSPatrick Bellasi  */
29269842cbaSPatrick Bellasi enum uclamp_id {
29369842cbaSPatrick Bellasi 	UCLAMP_MIN = 0,
29469842cbaSPatrick Bellasi 	UCLAMP_MAX,
29569842cbaSPatrick Bellasi 	UCLAMP_CNT
29669842cbaSPatrick Bellasi };
29769842cbaSPatrick Bellasi 
2981da177e4SLinus Torvalds struct sched_info {
2997f5f8e8dSIngo Molnar #ifdef CONFIG_SCHED_INFO
3005eca1c10SIngo Molnar 	/* Cumulative counters: */
3011da177e4SLinus Torvalds 
3025eca1c10SIngo Molnar 	/* # of times we have run on this CPU: */
3035eca1c10SIngo Molnar 	unsigned long			pcount;
3045eca1c10SIngo Molnar 
3055eca1c10SIngo Molnar 	/* Time spent waiting on a runqueue: */
3065eca1c10SIngo Molnar 	unsigned long long		run_delay;
3075eca1c10SIngo Molnar 
3085eca1c10SIngo Molnar 	/* Timestamps: */
3095eca1c10SIngo Molnar 
3105eca1c10SIngo Molnar 	/* When did we last run on a CPU? */
3115eca1c10SIngo Molnar 	unsigned long long		last_arrival;
3125eca1c10SIngo Molnar 
3135eca1c10SIngo Molnar 	/* When were we last queued to run? */
3145eca1c10SIngo Molnar 	unsigned long long		last_queued;
3155eca1c10SIngo Molnar 
316f6db8347SNaveen N. Rao #endif /* CONFIG_SCHED_INFO */
3177f5f8e8dSIngo Molnar };
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3206ecdd749SYuyang Du  * Integer metrics need fixed point arithmetic, e.g., sched/fair
3216ecdd749SYuyang Du  * has a few: load, load_avg, util_avg, freq, and capacity.
3226ecdd749SYuyang Du  *
3236ecdd749SYuyang Du  * We define a basic fixed point arithmetic range, and then formalize
3246ecdd749SYuyang Du  * all these metrics based on that basic range.
3256ecdd749SYuyang Du  */
3266ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SHIFT		10
3276ecdd749SYuyang Du # define SCHED_FIXEDPOINT_SCALE		(1L << SCHED_FIXEDPOINT_SHIFT)
3286ecdd749SYuyang Du 
32969842cbaSPatrick Bellasi /* Increase resolution of cpu_capacity calculations */
33069842cbaSPatrick Bellasi # define SCHED_CAPACITY_SHIFT		SCHED_FIXEDPOINT_SHIFT
33169842cbaSPatrick Bellasi # define SCHED_CAPACITY_SCALE		(1L << SCHED_CAPACITY_SHIFT)
33269842cbaSPatrick Bellasi 
33320b8a59fSIngo Molnar struct load_weight {
3349dbdb155SPeter Zijlstra 	unsigned long			weight;
3359dbdb155SPeter Zijlstra 	u32				inv_weight;
33620b8a59fSIngo Molnar };
33720b8a59fSIngo Molnar 
3387f65ea42SPatrick Bellasi /**
3397f65ea42SPatrick Bellasi  * struct util_est - Estimation utilization of FAIR tasks
3407f65ea42SPatrick Bellasi  * @enqueued: instantaneous estimated utilization of a task/cpu
3417f65ea42SPatrick Bellasi  * @ewma:     the Exponential Weighted Moving Average (EWMA)
3427f65ea42SPatrick Bellasi  *            utilization of a task
3437f65ea42SPatrick Bellasi  *
3447f65ea42SPatrick Bellasi  * Support data structure to track an Exponential Weighted Moving Average
3457f65ea42SPatrick Bellasi  * (EWMA) of a FAIR task's utilization. New samples are added to the moving
3467f65ea42SPatrick Bellasi  * average each time a task completes an activation. Sample's weight is chosen
3477f65ea42SPatrick Bellasi  * so that the EWMA will be relatively insensitive to transient changes to the
3487f65ea42SPatrick Bellasi  * task's workload.
3497f65ea42SPatrick Bellasi  *
3507f65ea42SPatrick Bellasi  * The enqueued attribute has a slightly different meaning for tasks and cpus:
3517f65ea42SPatrick Bellasi  * - task:   the task's util_avg at last task dequeue time
3527f65ea42SPatrick Bellasi  * - cfs_rq: the sum of util_est.enqueued for each RUNNABLE task on that CPU
3537f65ea42SPatrick Bellasi  * Thus, the util_est.enqueued of a task represents the contribution on the
3547f65ea42SPatrick Bellasi  * estimated utilization of the CPU where that task is currently enqueued.
3557f65ea42SPatrick Bellasi  *
3567f65ea42SPatrick Bellasi  * Only for tasks we track a moving average of the past instantaneous
3577f65ea42SPatrick Bellasi  * estimated utilization. This allows to absorb sporadic drops in utilization
3587f65ea42SPatrick Bellasi  * of an otherwise almost periodic task.
3597f65ea42SPatrick Bellasi  */
3607f65ea42SPatrick Bellasi struct util_est {
3617f65ea42SPatrick Bellasi 	unsigned int			enqueued;
3627f65ea42SPatrick Bellasi 	unsigned int			ewma;
3637f65ea42SPatrick Bellasi #define UTIL_EST_WEIGHT_SHIFT		2
364317d359dSPeter Zijlstra } __attribute__((__aligned__(sizeof(u64))));
3657f65ea42SPatrick Bellasi 
3669d89c257SYuyang Du /*
3677b595334SYuyang Du  * The load_avg/util_avg accumulates an infinite geometric series
3687b595334SYuyang Du  * (see __update_load_avg() in kernel/sched/fair.c).
3697b595334SYuyang Du  *
3707b595334SYuyang Du  * [load_avg definition]
3717b595334SYuyang Du  *
3727b595334SYuyang Du  *   load_avg = runnable% * scale_load_down(load)
3737b595334SYuyang Du  *
3747b595334SYuyang Du  * where runnable% is the time ratio that a sched_entity is runnable.
3757b595334SYuyang Du  * For cfs_rq, it is the aggregated load_avg of all runnable and
3769d89c257SYuyang Du  * blocked sched_entities.
3777b595334SYuyang Du  *
3787b595334SYuyang Du  * [util_avg definition]
3797b595334SYuyang Du  *
3807b595334SYuyang Du  *   util_avg = running% * SCHED_CAPACITY_SCALE
3817b595334SYuyang Du  *
3827b595334SYuyang Du  * where running% is the time ratio that a sched_entity is running on
3837b595334SYuyang Du  * a CPU. For cfs_rq, it is the aggregated util_avg of all runnable
3847b595334SYuyang Du  * and blocked sched_entities.
3857b595334SYuyang Du  *
38623127296SVincent Guittot  * load_avg and util_avg don't direcly factor frequency scaling and CPU
38723127296SVincent Guittot  * capacity scaling. The scaling is done through the rq_clock_pelt that
38823127296SVincent Guittot  * is used for computing those signals (see update_rq_clock_pelt())
3897b595334SYuyang Du  *
39023127296SVincent Guittot  * N.B., the above ratios (runnable% and running%) themselves are in the
39123127296SVincent Guittot  * range of [0, 1]. To do fixed point arithmetics, we therefore scale them
39223127296SVincent Guittot  * to as large a range as necessary. This is for example reflected by
39323127296SVincent Guittot  * util_avg's SCHED_CAPACITY_SCALE.
3947b595334SYuyang Du  *
3957b595334SYuyang Du  * [Overflow issue]
3967b595334SYuyang Du  *
3977b595334SYuyang Du  * The 64-bit load_sum can have 4353082796 (=2^64/47742/88761) entities
3987b595334SYuyang Du  * with the highest load (=88761), always runnable on a single cfs_rq,
3997b595334SYuyang Du  * and should not overflow as the number already hits PID_MAX_LIMIT.
4007b595334SYuyang Du  *
4017b595334SYuyang Du  * For all other cases (including 32-bit kernels), struct load_weight's
4027b595334SYuyang Du  * weight will overflow first before we do, because:
4037b595334SYuyang Du  *
4047b595334SYuyang Du  *    Max(load_avg) <= Max(load.weight)
4057b595334SYuyang Du  *
4067b595334SYuyang Du  * Then it is the load_weight's responsibility to consider overflow
4077b595334SYuyang Du  * issues.
4089d89c257SYuyang Du  */
4099d85f21cSPaul Turner struct sched_avg {
4105eca1c10SIngo Molnar 	u64				last_update_time;
4115eca1c10SIngo Molnar 	u64				load_sum;
4121ea6c46aSPeter Zijlstra 	u64				runnable_load_sum;
4135eca1c10SIngo Molnar 	u32				util_sum;
4145eca1c10SIngo Molnar 	u32				period_contrib;
4155eca1c10SIngo Molnar 	unsigned long			load_avg;
4161ea6c46aSPeter Zijlstra 	unsigned long			runnable_load_avg;
4175eca1c10SIngo Molnar 	unsigned long			util_avg;
4187f65ea42SPatrick Bellasi 	struct util_est			util_est;
419317d359dSPeter Zijlstra } ____cacheline_aligned;
4209d85f21cSPaul Turner 
42141acab88SLucas De Marchi struct sched_statistics {
4227f5f8e8dSIngo Molnar #ifdef CONFIG_SCHEDSTATS
42394c18227SIngo Molnar 	u64				wait_start;
42494c18227SIngo Molnar 	u64				wait_max;
4256d082592SArjan van de Ven 	u64				wait_count;
4266d082592SArjan van de Ven 	u64				wait_sum;
4278f0dfc34SArjan van de Ven 	u64				iowait_count;
4288f0dfc34SArjan van de Ven 	u64				iowait_sum;
42994c18227SIngo Molnar 
43094c18227SIngo Molnar 	u64				sleep_start;
43120b8a59fSIngo Molnar 	u64				sleep_max;
43294c18227SIngo Molnar 	s64				sum_sleep_runtime;
43394c18227SIngo Molnar 
43494c18227SIngo Molnar 	u64				block_start;
43520b8a59fSIngo Molnar 	u64				block_max;
43620b8a59fSIngo Molnar 	u64				exec_max;
437eba1ed4bSIngo Molnar 	u64				slice_max;
438cc367732SIngo Molnar 
439cc367732SIngo Molnar 	u64				nr_migrations_cold;
440cc367732SIngo Molnar 	u64				nr_failed_migrations_affine;
441cc367732SIngo Molnar 	u64				nr_failed_migrations_running;
442cc367732SIngo Molnar 	u64				nr_failed_migrations_hot;
443cc367732SIngo Molnar 	u64				nr_forced_migrations;
444cc367732SIngo Molnar 
445cc367732SIngo Molnar 	u64				nr_wakeups;
446cc367732SIngo Molnar 	u64				nr_wakeups_sync;
447cc367732SIngo Molnar 	u64				nr_wakeups_migrate;
448cc367732SIngo Molnar 	u64				nr_wakeups_local;
449cc367732SIngo Molnar 	u64				nr_wakeups_remote;
450cc367732SIngo Molnar 	u64				nr_wakeups_affine;
451cc367732SIngo Molnar 	u64				nr_wakeups_affine_attempts;
452cc367732SIngo Molnar 	u64				nr_wakeups_passive;
453cc367732SIngo Molnar 	u64				nr_wakeups_idle;
45441acab88SLucas De Marchi #endif
4557f5f8e8dSIngo Molnar };
45641acab88SLucas De Marchi 
45741acab88SLucas De Marchi struct sched_entity {
4585eca1c10SIngo Molnar 	/* For load-balancing: */
4595eca1c10SIngo Molnar 	struct load_weight		load;
4601ea6c46aSPeter Zijlstra 	unsigned long			runnable_weight;
46141acab88SLucas De Marchi 	struct rb_node			run_node;
46241acab88SLucas De Marchi 	struct list_head		group_node;
46341acab88SLucas De Marchi 	unsigned int			on_rq;
46441acab88SLucas De Marchi 
46541acab88SLucas De Marchi 	u64				exec_start;
46641acab88SLucas De Marchi 	u64				sum_exec_runtime;
46741acab88SLucas De Marchi 	u64				vruntime;
46841acab88SLucas De Marchi 	u64				prev_sum_exec_runtime;
46941acab88SLucas De Marchi 
47041acab88SLucas De Marchi 	u64				nr_migrations;
47141acab88SLucas De Marchi 
47241acab88SLucas De Marchi 	struct sched_statistics		statistics;
47394c18227SIngo Molnar 
47420b8a59fSIngo Molnar #ifdef CONFIG_FAIR_GROUP_SCHED
475fed14d45SPeter Zijlstra 	int				depth;
47620b8a59fSIngo Molnar 	struct sched_entity		*parent;
47720b8a59fSIngo Molnar 	/* rq on which this entity is (to be) queued: */
47820b8a59fSIngo Molnar 	struct cfs_rq			*cfs_rq;
47920b8a59fSIngo Molnar 	/* rq "owned" by this entity/group: */
48020b8a59fSIngo Molnar 	struct cfs_rq			*my_q;
48120b8a59fSIngo Molnar #endif
4828bd75c77SClark Williams 
483141965c7SAlex Shi #ifdef CONFIG_SMP
4845a107804SJiri Olsa 	/*
4855a107804SJiri Olsa 	 * Per entity load average tracking.
4865a107804SJiri Olsa 	 *
4875a107804SJiri Olsa 	 * Put into separate cache line so it does not
4885a107804SJiri Olsa 	 * collide with read-mostly values above.
4895a107804SJiri Olsa 	 */
490317d359dSPeter Zijlstra 	struct sched_avg		avg;
4919d85f21cSPaul Turner #endif
49220b8a59fSIngo Molnar };
49370b97a7fSIngo Molnar 
494fa717060SPeter Zijlstra struct sched_rt_entity {
495fa717060SPeter Zijlstra 	struct list_head		run_list;
49678f2c7dbSPeter Zijlstra 	unsigned long			timeout;
49757d2aa00SYing Xue 	unsigned long			watchdog_stamp;
498bee367edSRichard Kennedy 	unsigned int			time_slice;
499ff77e468SPeter Zijlstra 	unsigned short			on_rq;
500ff77e468SPeter Zijlstra 	unsigned short			on_list;
5016f505b16SPeter Zijlstra 
50258d6c2d7SPeter Zijlstra 	struct sched_rt_entity		*back;
503052f1dc7SPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
5046f505b16SPeter Zijlstra 	struct sched_rt_entity		*parent;
5056f505b16SPeter Zijlstra 	/* rq on which this entity is (to be) queued: */
5066f505b16SPeter Zijlstra 	struct rt_rq			*rt_rq;
5076f505b16SPeter Zijlstra 	/* rq "owned" by this entity/group: */
5086f505b16SPeter Zijlstra 	struct rt_rq			*my_q;
5096f505b16SPeter Zijlstra #endif
5103859a271SKees Cook } __randomize_layout;
511fa717060SPeter Zijlstra 
512aab03e05SDario Faggioli struct sched_dl_entity {
513aab03e05SDario Faggioli 	struct rb_node			rb_node;
514aab03e05SDario Faggioli 
515aab03e05SDario Faggioli 	/*
516aab03e05SDario Faggioli 	 * Original scheduling parameters. Copied here from sched_attr
5174027d080Sxiaofeng.yan 	 * during sched_setattr(), they will remain the same until
5184027d080Sxiaofeng.yan 	 * the next sched_setattr().
519aab03e05SDario Faggioli 	 */
5205eca1c10SIngo Molnar 	u64				dl_runtime;	/* Maximum runtime for each instance	*/
5215eca1c10SIngo Molnar 	u64				dl_deadline;	/* Relative deadline of each instance	*/
5225eca1c10SIngo Molnar 	u64				dl_period;	/* Separation of two instances (period) */
52354d6d303SDaniel Bristot de Oliveira 	u64				dl_bw;		/* dl_runtime / dl_period		*/
5243effcb42SDaniel Bristot de Oliveira 	u64				dl_density;	/* dl_runtime / dl_deadline		*/
525aab03e05SDario Faggioli 
526aab03e05SDario Faggioli 	/*
527aab03e05SDario Faggioli 	 * Actual scheduling parameters. Initialized with the values above,
528dfcb245eSIngo Molnar 	 * they are continuously updated during task execution. Note that
529aab03e05SDario Faggioli 	 * the remaining runtime could be < 0 in case we are in overrun.
530aab03e05SDario Faggioli 	 */
5315eca1c10SIngo Molnar 	s64				runtime;	/* Remaining runtime for this instance	*/
5325eca1c10SIngo Molnar 	u64				deadline;	/* Absolute deadline for this instance	*/
5335eca1c10SIngo Molnar 	unsigned int			flags;		/* Specifying the scheduler behaviour	*/
534aab03e05SDario Faggioli 
535aab03e05SDario Faggioli 	/*
536aab03e05SDario Faggioli 	 * Some bool flags:
537aab03e05SDario Faggioli 	 *
538aab03e05SDario Faggioli 	 * @dl_throttled tells if we exhausted the runtime. If so, the
539aab03e05SDario Faggioli 	 * task has to wait for a replenishment to be performed at the
540aab03e05SDario Faggioli 	 * next firing of dl_timer.
541aab03e05SDario Faggioli 	 *
5422d3d891dSDario Faggioli 	 * @dl_boosted tells if we are boosted due to DI. If so we are
5432d3d891dSDario Faggioli 	 * outside bandwidth enforcement mechanism (but only until we
5445bfd126eSJuri Lelli 	 * exit the critical section);
5455bfd126eSJuri Lelli 	 *
5465eca1c10SIngo Molnar 	 * @dl_yielded tells if task gave up the CPU before consuming
5475bfd126eSJuri Lelli 	 * all its available runtime during the last job.
548209a0cbdSLuca Abeni 	 *
549209a0cbdSLuca Abeni 	 * @dl_non_contending tells if the task is inactive while still
550209a0cbdSLuca Abeni 	 * contributing to the active utilization. In other words, it
551209a0cbdSLuca Abeni 	 * indicates if the inactive timer has been armed and its handler
552209a0cbdSLuca Abeni 	 * has not been executed yet. This flag is useful to avoid race
553209a0cbdSLuca Abeni 	 * conditions between the inactive timer handler and the wakeup
554209a0cbdSLuca Abeni 	 * code.
55534be3930SJuri Lelli 	 *
55634be3930SJuri Lelli 	 * @dl_overrun tells if the task asked to be informed about runtime
55734be3930SJuri Lelli 	 * overruns.
558aab03e05SDario Faggioli 	 */
559aa5222e9SDan Carpenter 	unsigned int			dl_throttled      : 1;
560aa5222e9SDan Carpenter 	unsigned int			dl_boosted        : 1;
561aa5222e9SDan Carpenter 	unsigned int			dl_yielded        : 1;
562aa5222e9SDan Carpenter 	unsigned int			dl_non_contending : 1;
56334be3930SJuri Lelli 	unsigned int			dl_overrun	  : 1;
564aab03e05SDario Faggioli 
565aab03e05SDario Faggioli 	/*
566aab03e05SDario Faggioli 	 * Bandwidth enforcement timer. Each -deadline task has its
567aab03e05SDario Faggioli 	 * own bandwidth to be enforced, thus we need one timer per task.
568aab03e05SDario Faggioli 	 */
569aab03e05SDario Faggioli 	struct hrtimer			dl_timer;
570209a0cbdSLuca Abeni 
571209a0cbdSLuca Abeni 	/*
572209a0cbdSLuca Abeni 	 * Inactive timer, responsible for decreasing the active utilization
573209a0cbdSLuca Abeni 	 * at the "0-lag time". When a -deadline task blocks, it contributes
574209a0cbdSLuca Abeni 	 * to GRUB's active utilization until the "0-lag time", hence a
575209a0cbdSLuca Abeni 	 * timer is needed to decrease the active utilization at the correct
576209a0cbdSLuca Abeni 	 * time.
577209a0cbdSLuca Abeni 	 */
578209a0cbdSLuca Abeni 	struct hrtimer inactive_timer;
579aab03e05SDario Faggioli };
5808bd75c77SClark Williams 
58169842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
58269842cbaSPatrick Bellasi /* Number of utilization clamp buckets (shorter alias) */
58369842cbaSPatrick Bellasi #define UCLAMP_BUCKETS CONFIG_UCLAMP_BUCKETS_COUNT
58469842cbaSPatrick Bellasi 
58569842cbaSPatrick Bellasi /*
58669842cbaSPatrick Bellasi  * Utilization clamp for a scheduling entity
58769842cbaSPatrick Bellasi  * @value:		clamp value "assigned" to a se
58869842cbaSPatrick Bellasi  * @bucket_id:		bucket index corresponding to the "assigned" value
589e8f14172SPatrick Bellasi  * @active:		the se is currently refcounted in a rq's bucket
590*a509a7cdSPatrick Bellasi  * @user_defined:	the requested clamp value comes from user-space
59169842cbaSPatrick Bellasi  *
59269842cbaSPatrick Bellasi  * The bucket_id is the index of the clamp bucket matching the clamp value
59369842cbaSPatrick Bellasi  * which is pre-computed and stored to avoid expensive integer divisions from
59469842cbaSPatrick Bellasi  * the fast path.
595e8f14172SPatrick Bellasi  *
596e8f14172SPatrick Bellasi  * The active bit is set whenever a task has got an "effective" value assigned,
597e8f14172SPatrick Bellasi  * which can be different from the clamp value "requested" from user-space.
598e8f14172SPatrick Bellasi  * This allows to know a task is refcounted in the rq's bucket corresponding
599e8f14172SPatrick Bellasi  * to the "effective" bucket_id.
600*a509a7cdSPatrick Bellasi  *
601*a509a7cdSPatrick Bellasi  * The user_defined bit is set whenever a task has got a task-specific clamp
602*a509a7cdSPatrick Bellasi  * value requested from userspace, i.e. the system defaults apply to this task
603*a509a7cdSPatrick Bellasi  * just as a restriction. This allows to relax default clamps when a less
604*a509a7cdSPatrick Bellasi  * restrictive task-specific value has been requested, thus allowing to
605*a509a7cdSPatrick Bellasi  * implement a "nice" semantic. For example, a task running with a 20%
606*a509a7cdSPatrick Bellasi  * default boost can still drop its own boosting to 0%.
60769842cbaSPatrick Bellasi  */
60869842cbaSPatrick Bellasi struct uclamp_se {
60969842cbaSPatrick Bellasi 	unsigned int value		: bits_per(SCHED_CAPACITY_SCALE);
61069842cbaSPatrick Bellasi 	unsigned int bucket_id		: bits_per(UCLAMP_BUCKETS);
611e8f14172SPatrick Bellasi 	unsigned int active		: 1;
612*a509a7cdSPatrick Bellasi 	unsigned int user_defined	: 1;
61369842cbaSPatrick Bellasi };
61469842cbaSPatrick Bellasi #endif /* CONFIG_UCLAMP_TASK */
61569842cbaSPatrick Bellasi 
6161d082fd0SPaul E. McKenney union rcu_special {
6171d082fd0SPaul E. McKenney 	struct {
6188203d6d0SPaul E. McKenney 		u8			blocked;
6198203d6d0SPaul E. McKenney 		u8			need_qs;
62005f41571SPaul E. McKenney 		u8			exp_hint; /* Hint for performance. */
62105f41571SPaul E. McKenney 		u8			pad; /* No garbage from compiler! */
6228203d6d0SPaul E. McKenney 	} b; /* Bits. */
62305f41571SPaul E. McKenney 	u32 s; /* Set of bits. */
6241d082fd0SPaul E. McKenney };
62586848966SPaul E. McKenney 
6268dc85d54SPeter Zijlstra enum perf_event_task_context {
6278dc85d54SPeter Zijlstra 	perf_invalid_context = -1,
6288dc85d54SPeter Zijlstra 	perf_hw_context = 0,
62989a1e187SPeter Zijlstra 	perf_sw_context,
6308dc85d54SPeter Zijlstra 	perf_nr_task_contexts,
6318dc85d54SPeter Zijlstra };
6328dc85d54SPeter Zijlstra 
633eb61baf6SIngo Molnar struct wake_q_node {
634eb61baf6SIngo Molnar 	struct wake_q_node *next;
635eb61baf6SIngo Molnar };
636eb61baf6SIngo Molnar 
6371da177e4SLinus Torvalds struct task_struct {
638c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
639c65eacbeSAndy Lutomirski 	/*
640c65eacbeSAndy Lutomirski 	 * For reasons of header soup (see current_thread_info()), this
641c65eacbeSAndy Lutomirski 	 * must be the first element of task_struct.
642c65eacbeSAndy Lutomirski 	 */
643c65eacbeSAndy Lutomirski 	struct thread_info		thread_info;
644c65eacbeSAndy Lutomirski #endif
6455eca1c10SIngo Molnar 	/* -1 unrunnable, 0 runnable, >0 stopped: */
6465eca1c10SIngo Molnar 	volatile long			state;
64729e48ce8SKees Cook 
64829e48ce8SKees Cook 	/*
64929e48ce8SKees Cook 	 * This begins the randomizable portion of task_struct. Only
65029e48ce8SKees Cook 	 * scheduling-critical items should be added above here.
65129e48ce8SKees Cook 	 */
65229e48ce8SKees Cook 	randomized_struct_fields_start
65329e48ce8SKees Cook 
654f7e4217bSRoman Zippel 	void				*stack;
655ec1d2819SElena Reshetova 	refcount_t			usage;
6565eca1c10SIngo Molnar 	/* Per task flags (PF_*), defined further below: */
6575eca1c10SIngo Molnar 	unsigned int			flags;
65897dc32cdSWilliam Cohen 	unsigned int			ptrace;
6591da177e4SLinus Torvalds 
6602dd73a4fSPeter Williams #ifdef CONFIG_SMP
661fa14ff4aSPeter Zijlstra 	struct llist_node		wake_entry;
6623ca7a440SPeter Zijlstra 	int				on_cpu;
663c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
6645eca1c10SIngo Molnar 	/* Current CPU: */
6655eca1c10SIngo Molnar 	unsigned int			cpu;
666c65eacbeSAndy Lutomirski #endif
66763b0e9edSMike Galbraith 	unsigned int			wakee_flips;
66862470419SMichael Wang 	unsigned long			wakee_flip_decay_ts;
66963b0e9edSMike Galbraith 	struct task_struct		*last_wakee;
670ac66f547SPeter Zijlstra 
67132e839ddSMel Gorman 	/*
67232e839ddSMel Gorman 	 * recent_used_cpu is initially set as the last CPU used by a task
67332e839ddSMel Gorman 	 * that wakes affine another task. Waker/wakee relationships can
67432e839ddSMel Gorman 	 * push tasks around a CPU where each wakeup moves to the next one.
67532e839ddSMel Gorman 	 * Tracking a recently used CPU allows a quick search for a recently
67632e839ddSMel Gorman 	 * used CPU that may be idle.
67732e839ddSMel Gorman 	 */
67832e839ddSMel Gorman 	int				recent_used_cpu;
679ac66f547SPeter Zijlstra 	int				wake_cpu;
6804866cde0SNick Piggin #endif
681fd2f4419SPeter Zijlstra 	int				on_rq;
68250e645a8SIngo Molnar 
6835eca1c10SIngo Molnar 	int				prio;
6845eca1c10SIngo Molnar 	int				static_prio;
6855eca1c10SIngo Molnar 	int				normal_prio;
686c7aceabaSRichard Kennedy 	unsigned int			rt_priority;
6875eca1c10SIngo Molnar 
6885522d5d5SIngo Molnar 	const struct sched_class	*sched_class;
68920b8a59fSIngo Molnar 	struct sched_entity		se;
690fa717060SPeter Zijlstra 	struct sched_rt_entity		rt;
6918323f26cSPeter Zijlstra #ifdef CONFIG_CGROUP_SCHED
6928323f26cSPeter Zijlstra 	struct task_group		*sched_task_group;
6938323f26cSPeter Zijlstra #endif
694aab03e05SDario Faggioli 	struct sched_dl_entity		dl;
6951da177e4SLinus Torvalds 
69669842cbaSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
697e8f14172SPatrick Bellasi 	/* Clamp values requested for a scheduling entity */
698e8f14172SPatrick Bellasi 	struct uclamp_se		uclamp_req[UCLAMP_CNT];
699e8f14172SPatrick Bellasi 	/* Effective clamp values used for a scheduling entity */
70069842cbaSPatrick Bellasi 	struct uclamp_se		uclamp[UCLAMP_CNT];
70169842cbaSPatrick Bellasi #endif
70269842cbaSPatrick Bellasi 
703e107be36SAvi Kivity #ifdef CONFIG_PREEMPT_NOTIFIERS
7045eca1c10SIngo Molnar 	/* List of struct preempt_notifier: */
705e107be36SAvi Kivity 	struct hlist_head		preempt_notifiers;
706e107be36SAvi Kivity #endif
707e107be36SAvi Kivity 
7086c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
7092056a782SJens Axboe 	unsigned int			btrace_seq;
7106c5c9341SAlexey Dobriyan #endif
7111da177e4SLinus Torvalds 
71297dc32cdSWilliam Cohen 	unsigned int			policy;
71329baa747SPeter Zijlstra 	int				nr_cpus_allowed;
7143bd37062SSebastian Andrzej Siewior 	const cpumask_t			*cpus_ptr;
7153bd37062SSebastian Andrzej Siewior 	cpumask_t			cpus_mask;
7161da177e4SLinus Torvalds 
717a57eb940SPaul E. McKenney #ifdef CONFIG_PREEMPT_RCU
718e260be67SPaul E. McKenney 	int				rcu_read_lock_nesting;
7191d082fd0SPaul E. McKenney 	union rcu_special		rcu_read_unlock_special;
720f41d911fSPaul E. McKenney 	struct list_head		rcu_node_entry;
721a57eb940SPaul E. McKenney 	struct rcu_node			*rcu_blocked_node;
72228f6569aSPranith Kumar #endif /* #ifdef CONFIG_PREEMPT_RCU */
7235eca1c10SIngo Molnar 
7248315f422SPaul E. McKenney #ifdef CONFIG_TASKS_RCU
7258315f422SPaul E. McKenney 	unsigned long			rcu_tasks_nvcsw;
726ccdd29ffSPaul E. McKenney 	u8				rcu_tasks_holdout;
727ccdd29ffSPaul E. McKenney 	u8				rcu_tasks_idx;
728176f8f7aSPaul E. McKenney 	int				rcu_tasks_idle_cpu;
729ccdd29ffSPaul E. McKenney 	struct list_head		rcu_tasks_holdout_list;
7308315f422SPaul E. McKenney #endif /* #ifdef CONFIG_TASKS_RCU */
731e260be67SPaul E. McKenney 
7321da177e4SLinus Torvalds 	struct sched_info		sched_info;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	struct list_head		tasks;
735806c09a7SDario Faggioli #ifdef CONFIG_SMP
736917b627dSGregory Haskins 	struct plist_node		pushable_tasks;
7371baca4ceSJuri Lelli 	struct rb_node			pushable_dl_tasks;
738806c09a7SDario Faggioli #endif
7391da177e4SLinus Torvalds 
7405eca1c10SIngo Molnar 	struct mm_struct		*mm;
7415eca1c10SIngo Molnar 	struct mm_struct		*active_mm;
742314ff785SIngo Molnar 
743314ff785SIngo Molnar 	/* Per-thread vma caching: */
744314ff785SIngo Molnar 	struct vmacache			vmacache;
745314ff785SIngo Molnar 
7465eca1c10SIngo Molnar #ifdef SPLIT_RSS_COUNTING
74734e55232SKAMEZAWA Hiroyuki 	struct task_rss_stat		rss_stat;
74834e55232SKAMEZAWA Hiroyuki #endif
74997dc32cdSWilliam Cohen 	int				exit_state;
7505eca1c10SIngo Molnar 	int				exit_code;
7515eca1c10SIngo Molnar 	int				exit_signal;
7525eca1c10SIngo Molnar 	/* The signal sent when the parent dies: */
7535eca1c10SIngo Molnar 	int				pdeath_signal;
7545eca1c10SIngo Molnar 	/* JOBCTL_*, siglock protected: */
7555eca1c10SIngo Molnar 	unsigned long			jobctl;
7569b89f6baSAndrei Epure 
7575eca1c10SIngo Molnar 	/* Used for emulating ABI behavior of previous Linux versions: */
75897dc32cdSWilliam Cohen 	unsigned int			personality;
7599b89f6baSAndrei Epure 
7605eca1c10SIngo Molnar 	/* Scheduler bits, serialized by scheduler locks: */
761ca94c442SLennart Poettering 	unsigned			sched_reset_on_fork:1;
762a8e4f2eaSPeter Zijlstra 	unsigned			sched_contributes_to_load:1;
763ff303e66SPeter Zijlstra 	unsigned			sched_migrated:1;
764b7e7ade3SPeter Zijlstra 	unsigned			sched_remote_wakeup:1;
765eb414681SJohannes Weiner #ifdef CONFIG_PSI
766eb414681SJohannes Weiner 	unsigned			sched_psi_wake_requeue:1;
767eb414681SJohannes Weiner #endif
768eb414681SJohannes Weiner 
7695eca1c10SIngo Molnar 	/* Force alignment to the next boundary: */
7705eca1c10SIngo Molnar 	unsigned			:0;
771be958bdcSPeter Zijlstra 
7725eca1c10SIngo Molnar 	/* Unserialized, strictly 'current' */
7735eca1c10SIngo Molnar 
7745eca1c10SIngo Molnar 	/* Bit to tell LSMs we're in execve(): */
7755eca1c10SIngo Molnar 	unsigned			in_execve:1;
776be958bdcSPeter Zijlstra 	unsigned			in_iowait:1;
7775eca1c10SIngo Molnar #ifndef TIF_RESTORE_SIGMASK
7787e781418SAndy Lutomirski 	unsigned			restore_sigmask:1;
7797e781418SAndy Lutomirski #endif
780626ebc41STejun Heo #ifdef CONFIG_MEMCG
78129ef680aSMichal Hocko 	unsigned			in_user_fault:1;
782127424c8SJohannes Weiner #endif
783ff303e66SPeter Zijlstra #ifdef CONFIG_COMPAT_BRK
784ff303e66SPeter Zijlstra 	unsigned			brk_randomized:1;
785ff303e66SPeter Zijlstra #endif
78677f88796STejun Heo #ifdef CONFIG_CGROUPS
78777f88796STejun Heo 	/* disallow userland-initiated cgroup migration */
78877f88796STejun Heo 	unsigned			no_cgroup_migration:1;
78976f969e8SRoman Gushchin 	/* task is frozen/stopped (used by the cgroup freezer) */
79076f969e8SRoman Gushchin 	unsigned			frozen:1;
79177f88796STejun Heo #endif
792d09d8df3SJosef Bacik #ifdef CONFIG_BLK_CGROUP
793d09d8df3SJosef Bacik 	/* to be used once the psi infrastructure lands upstream. */
794d09d8df3SJosef Bacik 	unsigned			use_memdelay:1;
795d09d8df3SJosef Bacik #endif
7966f185c29SVladimir Davydov 
7975eca1c10SIngo Molnar 	unsigned long			atomic_flags; /* Flags requiring atomic access. */
7981d4457f9SKees Cook 
799f56141e3SAndy Lutomirski 	struct restart_block		restart_block;
800f56141e3SAndy Lutomirski 
8011da177e4SLinus Torvalds 	pid_t				pid;
8021da177e4SLinus Torvalds 	pid_t				tgid;
8030a425405SArjan van de Ven 
804050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
8055eca1c10SIngo Molnar 	/* Canary value for the -fstack-protector GCC feature: */
8060a425405SArjan van de Ven 	unsigned long			stack_canary;
8071314562aSHiroshi Shimamoto #endif
8081da177e4SLinus Torvalds 	/*
8095eca1c10SIngo Molnar 	 * Pointers to the (original) parent process, youngest child, younger sibling,
8101da177e4SLinus Torvalds 	 * older sibling, respectively.  (p->father can be replaced with
811f470021aSRoland McGrath 	 * p->real_parent->pid)
8121da177e4SLinus Torvalds 	 */
8135eca1c10SIngo Molnar 
8145eca1c10SIngo Molnar 	/* Real parent process: */
8155eca1c10SIngo Molnar 	struct task_struct __rcu	*real_parent;
8165eca1c10SIngo Molnar 
8175eca1c10SIngo Molnar 	/* Recipient of SIGCHLD, wait4() reports: */
8185eca1c10SIngo Molnar 	struct task_struct __rcu	*parent;
8191da177e4SLinus Torvalds 
820f470021aSRoland McGrath 	/*
8215eca1c10SIngo Molnar 	 * Children/sibling form the list of natural children:
8225eca1c10SIngo Molnar 	 */
8235eca1c10SIngo Molnar 	struct list_head		children;
8245eca1c10SIngo Molnar 	struct list_head		sibling;
8255eca1c10SIngo Molnar 	struct task_struct		*group_leader;
8265eca1c10SIngo Molnar 
8275eca1c10SIngo Molnar 	/*
8285eca1c10SIngo Molnar 	 * 'ptraced' is the list of tasks this task is using ptrace() on.
8295eca1c10SIngo Molnar 	 *
830f470021aSRoland McGrath 	 * This includes both natural children and PTRACE_ATTACH targets.
8315eca1c10SIngo Molnar 	 * 'ptrace_entry' is this task's link on the p->parent->ptraced list.
832f470021aSRoland McGrath 	 */
833f470021aSRoland McGrath 	struct list_head		ptraced;
834f470021aSRoland McGrath 	struct list_head		ptrace_entry;
835f470021aSRoland McGrath 
8361da177e4SLinus Torvalds 	/* PID/PID hash table linkage. */
8372c470475SEric W. Biederman 	struct pid			*thread_pid;
8382c470475SEric W. Biederman 	struct hlist_node		pid_links[PIDTYPE_MAX];
83947e65328SOleg Nesterov 	struct list_head		thread_group;
8400c740d0aSOleg Nesterov 	struct list_head		thread_node;
8411da177e4SLinus Torvalds 
8425eca1c10SIngo Molnar 	struct completion		*vfork_done;
8431da177e4SLinus Torvalds 
8445eca1c10SIngo Molnar 	/* CLONE_CHILD_SETTID: */
8455eca1c10SIngo Molnar 	int __user			*set_child_tid;
8465eca1c10SIngo Molnar 
8475eca1c10SIngo Molnar 	/* CLONE_CHILD_CLEARTID: */
8485eca1c10SIngo Molnar 	int __user			*clear_child_tid;
8495eca1c10SIngo Molnar 
8505eca1c10SIngo Molnar 	u64				utime;
8515eca1c10SIngo Molnar 	u64				stime;
85240565b5aSStanislaw Gruszka #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
8535eca1c10SIngo Molnar 	u64				utimescaled;
8545eca1c10SIngo Molnar 	u64				stimescaled;
85540565b5aSStanislaw Gruszka #endif
85616a6d9beSFrederic Weisbecker 	u64				gtime;
8579d7fb042SPeter Zijlstra 	struct prev_cputime		prev_cputime;
8586a61671bSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
859bac5b6b6SFrederic Weisbecker 	struct vtime			vtime;
8606a61671bSFrederic Weisbecker #endif
861d027d45dSFrederic Weisbecker 
862d027d45dSFrederic Weisbecker #ifdef CONFIG_NO_HZ_FULL
863f009a7a7SFrederic Weisbecker 	atomic_t			tick_dep_mask;
864d027d45dSFrederic Weisbecker #endif
8655eca1c10SIngo Molnar 	/* Context switch counts: */
8665eca1c10SIngo Molnar 	unsigned long			nvcsw;
8675eca1c10SIngo Molnar 	unsigned long			nivcsw;
8685eca1c10SIngo Molnar 
8695eca1c10SIngo Molnar 	/* Monotonic time in nsecs: */
8705eca1c10SIngo Molnar 	u64				start_time;
8715eca1c10SIngo Molnar 
8725eca1c10SIngo Molnar 	/* Boot based time in nsecs: */
8735eca1c10SIngo Molnar 	u64				real_start_time;
8745eca1c10SIngo Molnar 
8755eca1c10SIngo Molnar 	/* MM fault and swap info: this can arguably be seen as either mm-specific or thread-specific: */
8765eca1c10SIngo Molnar 	unsigned long			min_flt;
8775eca1c10SIngo Molnar 	unsigned long			maj_flt;
8781da177e4SLinus Torvalds 
879b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
880f06febc9SFrank Mayhar 	struct task_cputime		cputime_expires;
8811da177e4SLinus Torvalds 	struct list_head		cpu_timers[3];
882b18b6a9cSNicolas Pitre #endif
8831da177e4SLinus Torvalds 
8845eca1c10SIngo Molnar 	/* Process credentials: */
8855eca1c10SIngo Molnar 
8865eca1c10SIngo Molnar 	/* Tracer's credentials at attach: */
8875eca1c10SIngo Molnar 	const struct cred __rcu		*ptracer_cred;
8885eca1c10SIngo Molnar 
8895eca1c10SIngo Molnar 	/* Objective and real subjective task credentials (COW): */
8905eca1c10SIngo Molnar 	const struct cred __rcu		*real_cred;
8915eca1c10SIngo Molnar 
8925eca1c10SIngo Molnar 	/* Effective (overridable) subjective task credentials (COW): */
8935eca1c10SIngo Molnar 	const struct cred __rcu		*cred;
8945eca1c10SIngo Molnar 
8955eca1c10SIngo Molnar 	/*
8965eca1c10SIngo Molnar 	 * executable name, excluding path.
8975eca1c10SIngo Molnar 	 *
8985eca1c10SIngo Molnar 	 * - normally initialized setup_new_exec()
8995eca1c10SIngo Molnar 	 * - access it with [gs]et_task_comm()
9005eca1c10SIngo Molnar 	 * - lock it with task_lock()
9015eca1c10SIngo Molnar 	 */
9025eca1c10SIngo Molnar 	char				comm[TASK_COMM_LEN];
9035eca1c10SIngo Molnar 
904756daf26SNeilBrown 	struct nameidata		*nameidata;
9055eca1c10SIngo Molnar 
9063d5b6fccSAlexey Dobriyan #ifdef CONFIG_SYSVIPC
9071da177e4SLinus Torvalds 	struct sysv_sem			sysvsem;
908ab602f79SJack Miller 	struct sysv_shm			sysvshm;
9093d5b6fccSAlexey Dobriyan #endif
910e162b39aSMandeep Singh Baines #ifdef CONFIG_DETECT_HUNG_TASK
91182a1fcb9SIngo Molnar 	unsigned long			last_switch_count;
912a2e51445SDmitry Vyukov 	unsigned long			last_switch_time;
91382a1fcb9SIngo Molnar #endif
9145eca1c10SIngo Molnar 	/* Filesystem information: */
9151da177e4SLinus Torvalds 	struct fs_struct		*fs;
9165eca1c10SIngo Molnar 
9175eca1c10SIngo Molnar 	/* Open file information: */
9181da177e4SLinus Torvalds 	struct files_struct		*files;
9195eca1c10SIngo Molnar 
9205eca1c10SIngo Molnar 	/* Namespaces: */
921ab516013SSerge E. Hallyn 	struct nsproxy			*nsproxy;
9225eca1c10SIngo Molnar 
9235eca1c10SIngo Molnar 	/* Signal handlers: */
9241da177e4SLinus Torvalds 	struct signal_struct		*signal;
9251da177e4SLinus Torvalds 	struct sighand_struct		*sighand;
9265eca1c10SIngo Molnar 	sigset_t			blocked;
9275eca1c10SIngo Molnar 	sigset_t			real_blocked;
9285eca1c10SIngo Molnar 	/* Restored if set_restore_sigmask() was used: */
9295eca1c10SIngo Molnar 	sigset_t			saved_sigmask;
9301da177e4SLinus Torvalds 	struct sigpending		pending;
9311da177e4SLinus Torvalds 	unsigned long			sas_ss_sp;
9321da177e4SLinus Torvalds 	size_t				sas_ss_size;
9335eca1c10SIngo Molnar 	unsigned int			sas_ss_flags;
9342e01fabeSOleg Nesterov 
93567d12145SAl Viro 	struct callback_head		*task_works;
936e73f8959SOleg Nesterov 
9374b7d248bSRichard Guy Briggs #ifdef CONFIG_AUDIT
938bfef93a5SAl Viro #ifdef CONFIG_AUDITSYSCALL
9395f3d544fSRichard Guy Briggs 	struct audit_context		*audit_context;
9405f3d544fSRichard Guy Briggs #endif
941e1760bd5SEric W. Biederman 	kuid_t				loginuid;
9424746ec5bSEric Paris 	unsigned int			sessionid;
943bfef93a5SAl Viro #endif
944932ecebbSWill Drewry 	struct seccomp			seccomp;
9451da177e4SLinus Torvalds 
9465eca1c10SIngo Molnar 	/* Thread group tracking: */
9471da177e4SLinus Torvalds 	u32				parent_exec_id;
9481da177e4SLinus Torvalds 	u32				self_exec_id;
9495eca1c10SIngo Molnar 
9505eca1c10SIngo Molnar 	/* Protection against (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, mempolicy: */
9511da177e4SLinus Torvalds 	spinlock_t			alloc_lock;
9521da177e4SLinus Torvalds 
953b29739f9SIngo Molnar 	/* Protection of the PI data structures: */
9541d615482SThomas Gleixner 	raw_spinlock_t			pi_lock;
955b29739f9SIngo Molnar 
95676751049SPeter Zijlstra 	struct wake_q_node		wake_q;
95776751049SPeter Zijlstra 
95823f78d4aSIngo Molnar #ifdef CONFIG_RT_MUTEXES
9595eca1c10SIngo Molnar 	/* PI waiters blocked on a rt_mutex held by this task: */
960a23ba907SDavidlohr Bueso 	struct rb_root_cached		pi_waiters;
961e96a7705SXunlei Pang 	/* Updated under owner's pi_lock and rq lock */
962e96a7705SXunlei Pang 	struct task_struct		*pi_top_task;
9635eca1c10SIngo Molnar 	/* Deadlock detection and priority inheritance handling: */
96423f78d4aSIngo Molnar 	struct rt_mutex_waiter		*pi_blocked_on;
96523f78d4aSIngo Molnar #endif
96623f78d4aSIngo Molnar 
967408894eeSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
9685eca1c10SIngo Molnar 	/* Mutex deadlock detection: */
969408894eeSIngo Molnar 	struct mutex_waiter		*blocked_on;
970408894eeSIngo Molnar #endif
9715eca1c10SIngo Molnar 
972de30a2b3SIngo Molnar #ifdef CONFIG_TRACE_IRQFLAGS
973de30a2b3SIngo Molnar 	unsigned int			irq_events;
974de30a2b3SIngo Molnar 	unsigned long			hardirq_enable_ip;
975de30a2b3SIngo Molnar 	unsigned long			hardirq_disable_ip;
976fa1452e8SHiroshi Shimamoto 	unsigned int			hardirq_enable_event;
977de30a2b3SIngo Molnar 	unsigned int			hardirq_disable_event;
978fa1452e8SHiroshi Shimamoto 	int				hardirqs_enabled;
979de30a2b3SIngo Molnar 	int				hardirq_context;
980fa1452e8SHiroshi Shimamoto 	unsigned long			softirq_disable_ip;
981fa1452e8SHiroshi Shimamoto 	unsigned long			softirq_enable_ip;
982fa1452e8SHiroshi Shimamoto 	unsigned int			softirq_disable_event;
983fa1452e8SHiroshi Shimamoto 	unsigned int			softirq_enable_event;
984fa1452e8SHiroshi Shimamoto 	int				softirqs_enabled;
985de30a2b3SIngo Molnar 	int				softirq_context;
986de30a2b3SIngo Molnar #endif
9875eca1c10SIngo Molnar 
988fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
989bdb9441eSPeter Zijlstra # define MAX_LOCK_DEPTH			48UL
990fbb9ce95SIngo Molnar 	u64				curr_chain_key;
991fbb9ce95SIngo Molnar 	int				lockdep_depth;
992fbb9ce95SIngo Molnar 	unsigned int			lockdep_recursion;
993c7aceabaSRichard Kennedy 	struct held_lock		held_locks[MAX_LOCK_DEPTH];
994fbb9ce95SIngo Molnar #endif
9955eca1c10SIngo Molnar 
996c6d30853SAndrey Ryabinin #ifdef CONFIG_UBSAN
997c6d30853SAndrey Ryabinin 	unsigned int			in_ubsan;
998c6d30853SAndrey Ryabinin #endif
999408894eeSIngo Molnar 
10005eca1c10SIngo Molnar 	/* Journalling filesystem info: */
10011da177e4SLinus Torvalds 	void				*journal_info;
10021da177e4SLinus Torvalds 
10035eca1c10SIngo Molnar 	/* Stacked block device info: */
1004bddd87c7SAkinobu Mita 	struct bio_list			*bio_list;
1005d89d8796SNeil Brown 
100673c10101SJens Axboe #ifdef CONFIG_BLOCK
10075eca1c10SIngo Molnar 	/* Stack plugging: */
100873c10101SJens Axboe 	struct blk_plug			*plug;
100973c10101SJens Axboe #endif
101073c10101SJens Axboe 
10115eca1c10SIngo Molnar 	/* VM state: */
10121da177e4SLinus Torvalds 	struct reclaim_state		*reclaim_state;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	struct backing_dev_info		*backing_dev_info;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	struct io_context		*io_context;
10171da177e4SLinus Torvalds 
10185e1f0f09SMel Gorman #ifdef CONFIG_COMPACTION
10195e1f0f09SMel Gorman 	struct capture_control		*capture_control;
10205e1f0f09SMel Gorman #endif
10215eca1c10SIngo Molnar 	/* Ptrace state: */
10221da177e4SLinus Torvalds 	unsigned long			ptrace_message;
1023ae7795bcSEric W. Biederman 	kernel_siginfo_t		*last_siginfo;
10245eca1c10SIngo Molnar 
10257c3ab738SAndrew Morton 	struct task_io_accounting	ioac;
1026eb414681SJohannes Weiner #ifdef CONFIG_PSI
1027eb414681SJohannes Weiner 	/* Pressure stall state */
1028eb414681SJohannes Weiner 	unsigned int			psi_flags;
1029eb414681SJohannes Weiner #endif
10305eca1c10SIngo Molnar #ifdef CONFIG_TASK_XACCT
10315eca1c10SIngo Molnar 	/* Accumulated RSS usage: */
10325eca1c10SIngo Molnar 	u64				acct_rss_mem1;
10335eca1c10SIngo Molnar 	/* Accumulated virtual memory usage: */
10345eca1c10SIngo Molnar 	u64				acct_vm_mem1;
10355eca1c10SIngo Molnar 	/* stime + utime since last update: */
10365eca1c10SIngo Molnar 	u64				acct_timexpd;
10371da177e4SLinus Torvalds #endif
10381da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS
10395eca1c10SIngo Molnar 	/* Protected by ->alloc_lock: */
10405eca1c10SIngo Molnar 	nodemask_t			mems_allowed;
10415eca1c10SIngo Molnar 	/* Seqence number to catch updates: */
10425eca1c10SIngo Molnar 	seqcount_t			mems_allowed_seq;
1043825a46afSPaul Jackson 	int				cpuset_mem_spread_rotor;
10446adef3ebSJack Steiner 	int				cpuset_slab_spread_rotor;
10451da177e4SLinus Torvalds #endif
1046ddbcc7e8SPaul Menage #ifdef CONFIG_CGROUPS
10475eca1c10SIngo Molnar 	/* Control Group info protected by css_set_lock: */
10482c392b8cSArnd Bergmann 	struct css_set __rcu		*cgroups;
10495eca1c10SIngo Molnar 	/* cg_list protected by css_set_lock and tsk->alloc_lock: */
1050817929ecSPaul Menage 	struct list_head		cg_list;
1051ddbcc7e8SPaul Menage #endif
1052e6d42931SJohannes Weiner #ifdef CONFIG_X86_CPU_RESCTRL
10530734ded1SVikas Shivappa 	u32				closid;
1054d6aaba61SVikas Shivappa 	u32				rmid;
1055e02737d5SFenghua Yu #endif
105642b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
10570771dfefSIngo Molnar 	struct robust_list_head __user	*robust_list;
105834f192c6SIngo Molnar #ifdef CONFIG_COMPAT
105934f192c6SIngo Molnar 	struct compat_robust_list_head __user *compat_robust_list;
106034f192c6SIngo Molnar #endif
1061c87e2837SIngo Molnar 	struct list_head		pi_state_list;
1062c87e2837SIngo Molnar 	struct futex_pi_state		*pi_state_cache;
106342b2dd0aSAlexey Dobriyan #endif
1064cdd6c482SIngo Molnar #ifdef CONFIG_PERF_EVENTS
10658dc85d54SPeter Zijlstra 	struct perf_event_context	*perf_event_ctxp[perf_nr_task_contexts];
1066cdd6c482SIngo Molnar 	struct mutex			perf_event_mutex;
1067cdd6c482SIngo Molnar 	struct list_head		perf_event_list;
1068a63eaf34SPaul Mackerras #endif
10698f47b187SThomas Gleixner #ifdef CONFIG_DEBUG_PREEMPT
10708f47b187SThomas Gleixner 	unsigned long			preempt_disable_ip;
10718f47b187SThomas Gleixner #endif
1072c7aceabaSRichard Kennedy #ifdef CONFIG_NUMA
10735eca1c10SIngo Molnar 	/* Protected by alloc_lock: */
10745eca1c10SIngo Molnar 	struct mempolicy		*mempolicy;
107545816682SVlastimil Babka 	short				il_prev;
1076207205a2SEric Dumazet 	short				pref_node_fork;
1077c7aceabaSRichard Kennedy #endif
1078cbee9f88SPeter Zijlstra #ifdef CONFIG_NUMA_BALANCING
1079cbee9f88SPeter Zijlstra 	int				numa_scan_seq;
1080cbee9f88SPeter Zijlstra 	unsigned int			numa_scan_period;
1081598f0ec0SMel Gorman 	unsigned int			numa_scan_period_max;
1082de1c9ce6SRik van Riel 	int				numa_preferred_nid;
10836b9a7460SMel Gorman 	unsigned long			numa_migrate_retry;
10845eca1c10SIngo Molnar 	/* Migration stamp: */
10855eca1c10SIngo Molnar 	u64				node_stamp;
10867e2703e6SRik van Riel 	u64				last_task_numa_placement;
10877e2703e6SRik van Riel 	u64				last_sum_exec_runtime;
1088cbee9f88SPeter Zijlstra 	struct callback_head		numa_work;
1089f809ca9aSMel Gorman 
10908c8a743cSPeter Zijlstra 	struct numa_group		*numa_group;
10918c8a743cSPeter Zijlstra 
1092745d6147SMel Gorman 	/*
109344dba3d5SIulia Manda 	 * numa_faults is an array split into four regions:
109444dba3d5SIulia Manda 	 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
109544dba3d5SIulia Manda 	 * in this precise order.
109644dba3d5SIulia Manda 	 *
109744dba3d5SIulia Manda 	 * faults_memory: Exponential decaying average of faults on a per-node
109844dba3d5SIulia Manda 	 * basis. Scheduling placement decisions are made based on these
109944dba3d5SIulia Manda 	 * counts. The values remain static for the duration of a PTE scan.
110044dba3d5SIulia Manda 	 * faults_cpu: Track the nodes the process was running on when a NUMA
110144dba3d5SIulia Manda 	 * hinting fault was incurred.
110244dba3d5SIulia Manda 	 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
110344dba3d5SIulia Manda 	 * during the current scan window. When the scan completes, the counts
110444dba3d5SIulia Manda 	 * in faults_memory and faults_cpu decay and these values are copied.
1105745d6147SMel Gorman 	 */
110644dba3d5SIulia Manda 	unsigned long			*numa_faults;
110783e1d2cdSMel Gorman 	unsigned long			total_numa_faults;
1108745d6147SMel Gorman 
1109745d6147SMel Gorman 	/*
111004bb2f94SRik van Riel 	 * numa_faults_locality tracks if faults recorded during the last
1111074c2381SMel Gorman 	 * scan window were remote/local or failed to migrate. The task scan
1112074c2381SMel Gorman 	 * period is adapted based on the locality of the faults with different
1113074c2381SMel Gorman 	 * weights depending on whether they were shared or private faults
111404bb2f94SRik van Riel 	 */
1115074c2381SMel Gorman 	unsigned long			numa_faults_locality[3];
111604bb2f94SRik van Riel 
1117b32e86b4SIngo Molnar 	unsigned long			numa_pages_migrated;
1118cbee9f88SPeter Zijlstra #endif /* CONFIG_NUMA_BALANCING */
1119cbee9f88SPeter Zijlstra 
1120d7822b1eSMathieu Desnoyers #ifdef CONFIG_RSEQ
1121d7822b1eSMathieu Desnoyers 	struct rseq __user *rseq;
1122d7822b1eSMathieu Desnoyers 	u32 rseq_sig;
1123d7822b1eSMathieu Desnoyers 	/*
1124d7822b1eSMathieu Desnoyers 	 * RmW on rseq_event_mask must be performed atomically
1125d7822b1eSMathieu Desnoyers 	 * with respect to preemption.
1126d7822b1eSMathieu Desnoyers 	 */
1127d7822b1eSMathieu Desnoyers 	unsigned long rseq_event_mask;
1128d7822b1eSMathieu Desnoyers #endif
1129d7822b1eSMathieu Desnoyers 
113072b252aeSMel Gorman 	struct tlbflush_unmap_batch	tlb_ubc;
113172b252aeSMel Gorman 
1132e56d0903SIngo Molnar 	struct rcu_head			rcu;
1133b92ce558SJens Axboe 
11345eca1c10SIngo Molnar 	/* Cache last used pipe for splice(): */
1135b92ce558SJens Axboe 	struct pipe_inode_info		*splice_pipe;
11365640f768SEric Dumazet 
11375640f768SEric Dumazet 	struct page_frag		task_frag;
11385640f768SEric Dumazet 
1139ca74e92bSShailabh Nagar #ifdef CONFIG_TASK_DELAY_ACCT
1140ca74e92bSShailabh Nagar 	struct task_delay_info		*delays;
1141ca74e92bSShailabh Nagar #endif
114247913d4eSIngo Molnar 
1143f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1144f4f154fdSAkinobu Mita 	int				make_it_fail;
11459049f2f6SAkinobu Mita 	unsigned int			fail_nth;
1146f4f154fdSAkinobu Mita #endif
11479d823e8fSWu Fengguang 	/*
11485eca1c10SIngo Molnar 	 * When (nr_dirtied >= nr_dirtied_pause), it's time to call
11495eca1c10SIngo Molnar 	 * balance_dirty_pages() for a dirty throttling pause:
11509d823e8fSWu Fengguang 	 */
11519d823e8fSWu Fengguang 	int				nr_dirtied;
11529d823e8fSWu Fengguang 	int				nr_dirtied_pause;
11535eca1c10SIngo Molnar 	/* Start of a write-and-pause period: */
11545eca1c10SIngo Molnar 	unsigned long			dirty_paused_when;
11559d823e8fSWu Fengguang 
11569745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
11579745512cSArjan van de Ven 	int				latency_record_count;
11589745512cSArjan van de Ven 	struct latency_record		latency_record[LT_SAVECOUNT];
11599745512cSArjan van de Ven #endif
11606976675dSArjan van de Ven 	/*
11615eca1c10SIngo Molnar 	 * Time slack values; these are used to round up poll() and
11626976675dSArjan van de Ven 	 * select() etc timeout values. These are in nanoseconds.
11636976675dSArjan van de Ven 	 */
1164da8b44d5SJohn Stultz 	u64				timer_slack_ns;
1165da8b44d5SJohn Stultz 	u64				default_timer_slack_ns;
1166f8d570a4SDavid Miller 
11670b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
11680b24beccSAndrey Ryabinin 	unsigned int			kasan_depth;
11690b24beccSAndrey Ryabinin #endif
11705eca1c10SIngo Molnar 
1171fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
11725eca1c10SIngo Molnar 	/* Index of current stored address in ret_stack: */
1173f201ae23SFrederic Weisbecker 	int				curr_ret_stack;
117439eb456dSSteven Rostedt (VMware) 	int				curr_ret_depth;
11755eca1c10SIngo Molnar 
11765eca1c10SIngo Molnar 	/* Stack of return addresses for return function tracing: */
1177f201ae23SFrederic Weisbecker 	struct ftrace_ret_stack		*ret_stack;
11785eca1c10SIngo Molnar 
11795eca1c10SIngo Molnar 	/* Timestamp for last schedule: */
11808aef2d28SSteven Rostedt 	unsigned long long		ftrace_timestamp;
11815eca1c10SIngo Molnar 
1182f201ae23SFrederic Weisbecker 	/*
1183f201ae23SFrederic Weisbecker 	 * Number of functions that haven't been traced
11845eca1c10SIngo Molnar 	 * because of depth overrun:
1185f201ae23SFrederic Weisbecker 	 */
1186f201ae23SFrederic Weisbecker 	atomic_t			trace_overrun;
11875eca1c10SIngo Molnar 
11885eca1c10SIngo Molnar 	/* Pause tracing: */
1189380c4b14SFrederic Weisbecker 	atomic_t			tracing_graph_pause;
1190f201ae23SFrederic Weisbecker #endif
11915eca1c10SIngo Molnar 
1192ea4e2bc4SSteven Rostedt #ifdef CONFIG_TRACING
11935eca1c10SIngo Molnar 	/* State flags for use by tracers: */
1194ea4e2bc4SSteven Rostedt 	unsigned long			trace;
11955eca1c10SIngo Molnar 
11965eca1c10SIngo Molnar 	/* Bitmask and counter of trace recursion: */
1197261842b7SSteven Rostedt 	unsigned long			trace_recursion;
1198261842b7SSteven Rostedt #endif /* CONFIG_TRACING */
11995eca1c10SIngo Molnar 
12005c9a8750SDmitry Vyukov #ifdef CONFIG_KCOV
12015eca1c10SIngo Molnar 	/* Coverage collection mode enabled for this task (0 if disabled): */
12020ed557aaSMark Rutland 	unsigned int			kcov_mode;
12035eca1c10SIngo Molnar 
12045eca1c10SIngo Molnar 	/* Size of the kcov_area: */
12055eca1c10SIngo Molnar 	unsigned int			kcov_size;
12065eca1c10SIngo Molnar 
12075eca1c10SIngo Molnar 	/* Buffer for coverage collection: */
12085c9a8750SDmitry Vyukov 	void				*kcov_area;
12095eca1c10SIngo Molnar 
12105eca1c10SIngo Molnar 	/* KCOV descriptor wired with this task or NULL: */
12115c9a8750SDmitry Vyukov 	struct kcov			*kcov;
12125c9a8750SDmitry Vyukov #endif
12135eca1c10SIngo Molnar 
12146f185c29SVladimir Davydov #ifdef CONFIG_MEMCG
1215626ebc41STejun Heo 	struct mem_cgroup		*memcg_in_oom;
1216626ebc41STejun Heo 	gfp_t				memcg_oom_gfp_mask;
1217626ebc41STejun Heo 	int				memcg_oom_order;
1218b23afb93STejun Heo 
12195eca1c10SIngo Molnar 	/* Number of pages to reclaim on returning to userland: */
1220b23afb93STejun Heo 	unsigned int			memcg_nr_pages_over_high;
1221d46eb14bSShakeel Butt 
1222d46eb14bSShakeel Butt 	/* Used by memcontrol for targeted memcg charge: */
1223d46eb14bSShakeel Butt 	struct mem_cgroup		*active_memcg;
1224569b846dSKAMEZAWA Hiroyuki #endif
12255eca1c10SIngo Molnar 
1226d09d8df3SJosef Bacik #ifdef CONFIG_BLK_CGROUP
1227d09d8df3SJosef Bacik 	struct request_queue		*throttle_queue;
1228d09d8df3SJosef Bacik #endif
1229d09d8df3SJosef Bacik 
12300326f5a9SSrikar Dronamraju #ifdef CONFIG_UPROBES
12310326f5a9SSrikar Dronamraju 	struct uprobe_task		*utask;
12320326f5a9SSrikar Dronamraju #endif
1233cafe5635SKent Overstreet #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1234cafe5635SKent Overstreet 	unsigned int			sequential_io;
1235cafe5635SKent Overstreet 	unsigned int			sequential_io_avg;
1236cafe5635SKent Overstreet #endif
12378eb23b9fSPeter Zijlstra #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
12388eb23b9fSPeter Zijlstra 	unsigned long			task_state_change;
12398eb23b9fSPeter Zijlstra #endif
12408bcbde54SDavid Hildenbrand 	int				pagefault_disabled;
124103049269SMichal Hocko #ifdef CONFIG_MMU
124229c696e1SVladimir Davydov 	struct task_struct		*oom_reaper_list;
124303049269SMichal Hocko #endif
1244ba14a194SAndy Lutomirski #ifdef CONFIG_VMAP_STACK
1245ba14a194SAndy Lutomirski 	struct vm_struct		*stack_vm_area;
1246ba14a194SAndy Lutomirski #endif
124768f24b08SAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
12485eca1c10SIngo Molnar 	/* A live task holds one reference: */
1249f0b89d39SElena Reshetova 	refcount_t			stack_refcount;
125068f24b08SAndy Lutomirski #endif
1251d83a7cb3SJosh Poimboeuf #ifdef CONFIG_LIVEPATCH
1252d83a7cb3SJosh Poimboeuf 	int patch_state;
1253d83a7cb3SJosh Poimboeuf #endif
1254e4e55b47STetsuo Handa #ifdef CONFIG_SECURITY
1255e4e55b47STetsuo Handa 	/* Used by LSM modules for access restriction: */
1256e4e55b47STetsuo Handa 	void				*security;
1257e4e55b47STetsuo Handa #endif
125829e48ce8SKees Cook 
1259afaef01cSAlexander Popov #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
1260afaef01cSAlexander Popov 	unsigned long			lowest_stack;
1261c8d12627SAlexander Popov 	unsigned long			prev_lowest_stack;
1262afaef01cSAlexander Popov #endif
1263afaef01cSAlexander Popov 
126429e48ce8SKees Cook 	/*
126529e48ce8SKees Cook 	 * New fields for task_struct should be added above here, so that
126629e48ce8SKees Cook 	 * they are included in the randomized portion of task_struct.
126729e48ce8SKees Cook 	 */
126829e48ce8SKees Cook 	randomized_struct_fields_end
126929e48ce8SKees Cook 
12705eca1c10SIngo Molnar 	/* CPU-specific state of this task: */
12710c8c0f03SDave Hansen 	struct thread_struct		thread;
12725eca1c10SIngo Molnar 
12730c8c0f03SDave Hansen 	/*
12740c8c0f03SDave Hansen 	 * WARNING: on x86, 'thread_struct' contains a variable-sized
12750c8c0f03SDave Hansen 	 * structure.  It *MUST* be at the end of 'task_struct'.
12760c8c0f03SDave Hansen 	 *
12770c8c0f03SDave Hansen 	 * Do not put anything below here!
12780c8c0f03SDave Hansen 	 */
12791da177e4SLinus Torvalds };
12801da177e4SLinus Torvalds 
1281e868171aSAlexey Dobriyan static inline struct pid *task_pid(struct task_struct *task)
128222c935f4SEric W. Biederman {
12832c470475SEric W. Biederman 	return task->thread_pid;
128422c935f4SEric W. Biederman }
128522c935f4SEric W. Biederman 
12867af57294SPavel Emelyanov /*
12877af57294SPavel Emelyanov  * the helpers to get the task's different pids as they are seen
12887af57294SPavel Emelyanov  * from various namespaces
12897af57294SPavel Emelyanov  *
12907af57294SPavel Emelyanov  * task_xid_nr()     : global id, i.e. the id seen from the init namespace;
129144c4e1b2SEric W. Biederman  * task_xid_vnr()    : virtual id, i.e. the id seen from the pid namespace of
129244c4e1b2SEric W. Biederman  *                     current.
12937af57294SPavel Emelyanov  * task_xid_nr_ns()  : id seen from the ns specified;
12947af57294SPavel Emelyanov  *
12957af57294SPavel Emelyanov  * see also pid_nr() etc in include/linux/pid.h
12967af57294SPavel Emelyanov  */
12975eca1c10SIngo Molnar pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type, struct pid_namespace *ns);
12987af57294SPavel Emelyanov 
1299e868171aSAlexey Dobriyan static inline pid_t task_pid_nr(struct task_struct *tsk)
13007af57294SPavel Emelyanov {
13017af57294SPavel Emelyanov 	return tsk->pid;
13027af57294SPavel Emelyanov }
13037af57294SPavel Emelyanov 
13045eca1c10SIngo Molnar static inline pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
130552ee2dfdSOleg Nesterov {
130652ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
130752ee2dfdSOleg Nesterov }
13087af57294SPavel Emelyanov 
13097af57294SPavel Emelyanov static inline pid_t task_pid_vnr(struct task_struct *tsk)
13107af57294SPavel Emelyanov {
131152ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
13127af57294SPavel Emelyanov }
13137af57294SPavel Emelyanov 
13147af57294SPavel Emelyanov 
1315e868171aSAlexey Dobriyan static inline pid_t task_tgid_nr(struct task_struct *tsk)
13167af57294SPavel Emelyanov {
13177af57294SPavel Emelyanov 	return tsk->tgid;
13187af57294SPavel Emelyanov }
13197af57294SPavel Emelyanov 
13205eca1c10SIngo Molnar /**
13215eca1c10SIngo Molnar  * pid_alive - check that a task structure is not stale
13225eca1c10SIngo Molnar  * @p: Task structure to be checked.
13235eca1c10SIngo Molnar  *
13245eca1c10SIngo Molnar  * Test if a process is not yet dead (at most zombie state)
13255eca1c10SIngo Molnar  * If pid_alive fails, then pointers within the task structure
13265eca1c10SIngo Molnar  * can be stale and must not be dereferenced.
13275eca1c10SIngo Molnar  *
13285eca1c10SIngo Molnar  * Return: 1 if the process is alive. 0 otherwise.
13295eca1c10SIngo Molnar  */
13305eca1c10SIngo Molnar static inline int pid_alive(const struct task_struct *p)
13315eca1c10SIngo Molnar {
13322c470475SEric W. Biederman 	return p->thread_pid != NULL;
13335eca1c10SIngo Molnar }
13347af57294SPavel Emelyanov 
13355eca1c10SIngo Molnar static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
13367af57294SPavel Emelyanov {
133752ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
13387af57294SPavel Emelyanov }
13397af57294SPavel Emelyanov 
13407af57294SPavel Emelyanov static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
13417af57294SPavel Emelyanov {
134252ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
13437af57294SPavel Emelyanov }
13447af57294SPavel Emelyanov 
13457af57294SPavel Emelyanov 
13465eca1c10SIngo Molnar static inline pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
13477af57294SPavel Emelyanov {
134852ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
13497af57294SPavel Emelyanov }
13507af57294SPavel Emelyanov 
13517af57294SPavel Emelyanov static inline pid_t task_session_vnr(struct task_struct *tsk)
13527af57294SPavel Emelyanov {
135352ee2dfdSOleg Nesterov 	return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
13547af57294SPavel Emelyanov }
13557af57294SPavel Emelyanov 
1356dd1c1f2fSOleg Nesterov static inline pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
1357dd1c1f2fSOleg Nesterov {
13586883f81aSEric W. Biederman 	return __task_pid_nr_ns(tsk, PIDTYPE_TGID, ns);
1359dd1c1f2fSOleg Nesterov }
1360dd1c1f2fSOleg Nesterov 
1361dd1c1f2fSOleg Nesterov static inline pid_t task_tgid_vnr(struct task_struct *tsk)
1362dd1c1f2fSOleg Nesterov {
13636883f81aSEric W. Biederman 	return __task_pid_nr_ns(tsk, PIDTYPE_TGID, NULL);
1364dd1c1f2fSOleg Nesterov }
1365dd1c1f2fSOleg Nesterov 
1366dd1c1f2fSOleg Nesterov static inline pid_t task_ppid_nr_ns(const struct task_struct *tsk, struct pid_namespace *ns)
1367dd1c1f2fSOleg Nesterov {
1368dd1c1f2fSOleg Nesterov 	pid_t pid = 0;
1369dd1c1f2fSOleg Nesterov 
1370dd1c1f2fSOleg Nesterov 	rcu_read_lock();
1371dd1c1f2fSOleg Nesterov 	if (pid_alive(tsk))
1372dd1c1f2fSOleg Nesterov 		pid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
1373dd1c1f2fSOleg Nesterov 	rcu_read_unlock();
1374dd1c1f2fSOleg Nesterov 
1375dd1c1f2fSOleg Nesterov 	return pid;
1376dd1c1f2fSOleg Nesterov }
1377dd1c1f2fSOleg Nesterov 
1378dd1c1f2fSOleg Nesterov static inline pid_t task_ppid_nr(const struct task_struct *tsk)
1379dd1c1f2fSOleg Nesterov {
1380dd1c1f2fSOleg Nesterov 	return task_ppid_nr_ns(tsk, &init_pid_ns);
1381dd1c1f2fSOleg Nesterov }
1382dd1c1f2fSOleg Nesterov 
13835eca1c10SIngo Molnar /* Obsolete, do not use: */
13841b0f7ffdSOleg Nesterov static inline pid_t task_pgrp_nr(struct task_struct *tsk)
13851b0f7ffdSOleg Nesterov {
13861b0f7ffdSOleg Nesterov 	return task_pgrp_nr_ns(tsk, &init_pid_ns);
13871b0f7ffdSOleg Nesterov }
13887af57294SPavel Emelyanov 
138906eb6184SPeter Zijlstra #define TASK_REPORT_IDLE	(TASK_REPORT + 1)
139006eb6184SPeter Zijlstra #define TASK_REPORT_MAX		(TASK_REPORT_IDLE << 1)
139106eb6184SPeter Zijlstra 
13921d48b080SPeter Zijlstra static inline unsigned int task_state_index(struct task_struct *tsk)
139320435d84SXie XiuQi {
13941593baabSPeter Zijlstra 	unsigned int tsk_state = READ_ONCE(tsk->state);
13951593baabSPeter Zijlstra 	unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
139620435d84SXie XiuQi 
139706eb6184SPeter Zijlstra 	BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
139806eb6184SPeter Zijlstra 
139906eb6184SPeter Zijlstra 	if (tsk_state == TASK_IDLE)
140006eb6184SPeter Zijlstra 		state = TASK_REPORT_IDLE;
140106eb6184SPeter Zijlstra 
14021593baabSPeter Zijlstra 	return fls(state);
14031593baabSPeter Zijlstra }
140420435d84SXie XiuQi 
14051d48b080SPeter Zijlstra static inline char task_index_to_char(unsigned int state)
14061593baabSPeter Zijlstra {
14078ef9925bSPeter Zijlstra 	static const char state_char[] = "RSDTtXZPI";
14081593baabSPeter Zijlstra 
140906eb6184SPeter Zijlstra 	BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
14101593baabSPeter Zijlstra 
14111593baabSPeter Zijlstra 	return state_char[state];
14121593baabSPeter Zijlstra }
14131593baabSPeter Zijlstra 
14141593baabSPeter Zijlstra static inline char task_state_to_char(struct task_struct *tsk)
14151593baabSPeter Zijlstra {
14161d48b080SPeter Zijlstra 	return task_index_to_char(task_state_index(tsk));
141720435d84SXie XiuQi }
141820435d84SXie XiuQi 
14191da177e4SLinus Torvalds /**
1420570f5241SSergey Senozhatsky  * is_global_init - check if a task structure is init. Since init
1421570f5241SSergey Senozhatsky  * is free to have sub-threads we need to check tgid.
14223260259fSHenne  * @tsk: Task structure to be checked.
14233260259fSHenne  *
14243260259fSHenne  * Check if a task structure is the first user space task the kernel created.
1425e69f6186SYacine Belkadi  *
1426e69f6186SYacine Belkadi  * Return: 1 if the task structure is init. 0 otherwise.
1427f400e198SSukadev Bhattiprolu  */
1428e868171aSAlexey Dobriyan static inline int is_global_init(struct task_struct *tsk)
1429b461cc03SPavel Emelyanov {
1430570f5241SSergey Senozhatsky 	return task_tgid_nr(tsk) == 1;
1431b461cc03SPavel Emelyanov }
1432b460cbc5SSerge E. Hallyn 
14339ec52099SCedric Le Goater extern struct pid *cad_pid;
14349ec52099SCedric Le Goater 
14351da177e4SLinus Torvalds /*
14361da177e4SLinus Torvalds  * Per process flags
14371da177e4SLinus Torvalds  */
1438c1de45caSPeter Zijlstra #define PF_IDLE			0x00000002	/* I am an IDLE thread */
14395eca1c10SIngo Molnar #define PF_EXITING		0x00000004	/* Getting shut down */
14405eca1c10SIngo Molnar #define PF_EXITPIDONE		0x00000008	/* PI exit done on shut down */
144194886b84SLaurent Vivier #define PF_VCPU			0x00000010	/* I'm a virtual CPU */
144221aa9af0STejun Heo #define PF_WQ_WORKER		0x00000020	/* I'm a workqueue worker */
14435eca1c10SIngo Molnar #define PF_FORKNOEXEC		0x00000040	/* Forked but didn't exec */
14445eca1c10SIngo Molnar #define PF_MCE_PROCESS		0x00000080      /* Process policy on mce errors */
14455eca1c10SIngo Molnar #define PF_SUPERPRIV		0x00000100	/* Used super-user privileges */
14465eca1c10SIngo Molnar #define PF_DUMPCORE		0x00000200	/* Dumped core */
14475eca1c10SIngo Molnar #define PF_SIGNALED		0x00000400	/* Killed by a signal */
14481da177e4SLinus Torvalds #define PF_MEMALLOC		0x00000800	/* Allocating memory */
14495eca1c10SIngo Molnar #define PF_NPROC_EXCEEDED	0x00001000	/* set_user() noticed that RLIMIT_NPROC was exceeded */
14505eca1c10SIngo Molnar #define PF_USED_MATH		0x00002000	/* If unset the fpu must be initialized before use */
14515eca1c10SIngo Molnar #define PF_USED_ASYNC		0x00004000	/* Used async_schedule*(), used by module init */
14525eca1c10SIngo Molnar #define PF_NOFREEZE		0x00008000	/* This thread should not be frozen */
14535eca1c10SIngo Molnar #define PF_FROZEN		0x00010000	/* Frozen for system suspend */
14547dea19f9SMichal Hocko #define PF_KSWAPD		0x00020000	/* I am kswapd */
14557dea19f9SMichal Hocko #define PF_MEMALLOC_NOFS	0x00040000	/* All allocation requests will inherit GFP_NOFS */
14567dea19f9SMichal Hocko #define PF_MEMALLOC_NOIO	0x00080000	/* All allocation requests will inherit GFP_NOIO */
14571da177e4SLinus Torvalds #define PF_LESS_THROTTLE	0x00100000	/* Throttle me less: I clean memory */
1458246bb0b1SOleg Nesterov #define PF_KTHREAD		0x00200000	/* I am a kernel thread */
14595eca1c10SIngo Molnar #define PF_RANDOMIZE		0x00400000	/* Randomize virtual address space */
1460b31dc66aSJens Axboe #define PF_SWAPWRITE		0x00800000	/* Allowed to write to swap */
1461eb414681SJohannes Weiner #define PF_MEMSTALL		0x01000000	/* Stalled due to lack of memory */
146273ab1cb2STaehee Yoo #define PF_UMH			0x02000000	/* I'm an Usermodehelper process */
14633bd37062SSebastian Andrzej Siewior #define PF_NO_SETAFFINITY	0x04000000	/* Userland is not allowed to meddle with cpus_mask */
14644db96cf0SAndi Kleen #define PF_MCE_EARLY		0x08000000      /* Early kill for mce process policy */
1465d7fefcc8SAneesh Kumar K.V #define PF_MEMALLOC_NOCMA	0x10000000	/* All allocation request will have _GFP_MOVABLE cleared */
146658a69cb4STejun Heo #define PF_FREEZER_SKIP		0x40000000	/* Freezer should not count it as freezable */
14675eca1c10SIngo Molnar #define PF_SUSPEND_TASK		0x80000000      /* This thread called freeze_processes() and should not be frozen */
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds /*
14701da177e4SLinus Torvalds  * Only the _current_ task can read/write to tsk->flags, but other
14711da177e4SLinus Torvalds  * tasks can access tsk->flags in readonly mode for example
14721da177e4SLinus Torvalds  * with tsk_used_math (like during threaded core dumping).
14731da177e4SLinus Torvalds  * There is however an exception to this rule during ptrace
14741da177e4SLinus Torvalds  * or during fork: the ptracer task is allowed to write to the
14751da177e4SLinus Torvalds  * child->flags of its traced child (same goes for fork, the parent
14761da177e4SLinus Torvalds  * can write to the child->flags), because we're guaranteed the
14771da177e4SLinus Torvalds  * child is not running and in turn not changing child->flags
14781da177e4SLinus Torvalds  * at the same time the parent does it.
14791da177e4SLinus Torvalds  */
14801da177e4SLinus Torvalds #define clear_stopped_child_used_math(child)	do { (child)->flags &= ~PF_USED_MATH; } while (0)
14811da177e4SLinus Torvalds #define set_stopped_child_used_math(child)	do { (child)->flags |= PF_USED_MATH; } while (0)
14821da177e4SLinus Torvalds #define clear_used_math()			clear_stopped_child_used_math(current)
14831da177e4SLinus Torvalds #define set_used_math()				set_stopped_child_used_math(current)
14845eca1c10SIngo Molnar 
14851da177e4SLinus Torvalds #define conditional_stopped_child_used_math(condition, child) \
14861da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= (condition) ? PF_USED_MATH : 0; } while (0)
14875eca1c10SIngo Molnar 
14885eca1c10SIngo Molnar #define conditional_used_math(condition)	conditional_stopped_child_used_math(condition, current)
14895eca1c10SIngo Molnar 
14901da177e4SLinus Torvalds #define copy_to_stopped_child_used_math(child) \
14911da177e4SLinus Torvalds 	do { (child)->flags &= ~PF_USED_MATH, (child)->flags |= current->flags & PF_USED_MATH; } while (0)
14925eca1c10SIngo Molnar 
14931da177e4SLinus Torvalds /* NOTE: this will return 0 or PF_USED_MATH, it will never return 1 */
14941da177e4SLinus Torvalds #define tsk_used_math(p)			((p)->flags & PF_USED_MATH)
14951da177e4SLinus Torvalds #define used_math()				tsk_used_math(current)
14961da177e4SLinus Torvalds 
149762ec05ddSThomas Gleixner static inline bool is_percpu_thread(void)
149862ec05ddSThomas Gleixner {
149962ec05ddSThomas Gleixner #ifdef CONFIG_SMP
150062ec05ddSThomas Gleixner 	return (current->flags & PF_NO_SETAFFINITY) &&
150162ec05ddSThomas Gleixner 		(current->nr_cpus_allowed  == 1);
150262ec05ddSThomas Gleixner #else
150362ec05ddSThomas Gleixner 	return true;
150462ec05ddSThomas Gleixner #endif
150562ec05ddSThomas Gleixner }
150662ec05ddSThomas Gleixner 
15071d4457f9SKees Cook /* Per-process atomic flags. */
1508a2b86f77SZefan Li #define PFA_NO_NEW_PRIVS		0	/* May not gain new privileges. */
15092ad654bcSZefan Li #define PFA_SPREAD_PAGE			1	/* Spread page cache over cpuset */
15102ad654bcSZefan Li #define PFA_SPREAD_SLAB			2	/* Spread some slab caches over cpuset */
1511356e4bffSThomas Gleixner #define PFA_SPEC_SSB_DISABLE		3	/* Speculative Store Bypass disabled */
1512356e4bffSThomas Gleixner #define PFA_SPEC_SSB_FORCE_DISABLE	4	/* Speculative Store Bypass force disabled*/
15139137bb27SThomas Gleixner #define PFA_SPEC_IB_DISABLE		5	/* Indirect branch speculation restricted */
15149137bb27SThomas Gleixner #define PFA_SPEC_IB_FORCE_DISABLE	6	/* Indirect branch speculation permanently restricted */
151571368af9SWaiman Long #define PFA_SPEC_SSB_NOEXEC		7	/* Speculative Store Bypass clear on execve() */
15161d4457f9SKees Cook 
1517e0e5070bSZefan Li #define TASK_PFA_TEST(name, func)					\
1518e0e5070bSZefan Li 	static inline bool task_##func(struct task_struct *p)		\
1519e0e5070bSZefan Li 	{ return test_bit(PFA_##name, &p->atomic_flags); }
15205eca1c10SIngo Molnar 
1521e0e5070bSZefan Li #define TASK_PFA_SET(name, func)					\
1522e0e5070bSZefan Li 	static inline void task_set_##func(struct task_struct *p)	\
1523e0e5070bSZefan Li 	{ set_bit(PFA_##name, &p->atomic_flags); }
15245eca1c10SIngo Molnar 
1525e0e5070bSZefan Li #define TASK_PFA_CLEAR(name, func)					\
1526e0e5070bSZefan Li 	static inline void task_clear_##func(struct task_struct *p)	\
1527e0e5070bSZefan Li 	{ clear_bit(PFA_##name, &p->atomic_flags); }
15281d4457f9SKees Cook 
1529e0e5070bSZefan Li TASK_PFA_TEST(NO_NEW_PRIVS, no_new_privs)
1530e0e5070bSZefan Li TASK_PFA_SET(NO_NEW_PRIVS, no_new_privs)
15311d4457f9SKees Cook 
15322ad654bcSZefan Li TASK_PFA_TEST(SPREAD_PAGE, spread_page)
15332ad654bcSZefan Li TASK_PFA_SET(SPREAD_PAGE, spread_page)
15342ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_PAGE, spread_page)
15352ad654bcSZefan Li 
15362ad654bcSZefan Li TASK_PFA_TEST(SPREAD_SLAB, spread_slab)
15372ad654bcSZefan Li TASK_PFA_SET(SPREAD_SLAB, spread_slab)
15382ad654bcSZefan Li TASK_PFA_CLEAR(SPREAD_SLAB, spread_slab)
1539544b2c91STejun Heo 
1540356e4bffSThomas Gleixner TASK_PFA_TEST(SPEC_SSB_DISABLE, spec_ssb_disable)
1541356e4bffSThomas Gleixner TASK_PFA_SET(SPEC_SSB_DISABLE, spec_ssb_disable)
1542356e4bffSThomas Gleixner TASK_PFA_CLEAR(SPEC_SSB_DISABLE, spec_ssb_disable)
1543356e4bffSThomas Gleixner 
154471368af9SWaiman Long TASK_PFA_TEST(SPEC_SSB_NOEXEC, spec_ssb_noexec)
154571368af9SWaiman Long TASK_PFA_SET(SPEC_SSB_NOEXEC, spec_ssb_noexec)
154671368af9SWaiman Long TASK_PFA_CLEAR(SPEC_SSB_NOEXEC, spec_ssb_noexec)
154771368af9SWaiman Long 
1548356e4bffSThomas Gleixner TASK_PFA_TEST(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1549356e4bffSThomas Gleixner TASK_PFA_SET(SPEC_SSB_FORCE_DISABLE, spec_ssb_force_disable)
1550356e4bffSThomas Gleixner 
15519137bb27SThomas Gleixner TASK_PFA_TEST(SPEC_IB_DISABLE, spec_ib_disable)
15529137bb27SThomas Gleixner TASK_PFA_SET(SPEC_IB_DISABLE, spec_ib_disable)
15539137bb27SThomas Gleixner TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable)
15549137bb27SThomas Gleixner 
15559137bb27SThomas Gleixner TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
15569137bb27SThomas Gleixner TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
15579137bb27SThomas Gleixner 
15585eca1c10SIngo Molnar static inline void
1559717a94b5SNeilBrown current_restore_flags(unsigned long orig_flags, unsigned long flags)
1560907aed48SMel Gorman {
1561717a94b5SNeilBrown 	current->flags &= ~flags;
1562717a94b5SNeilBrown 	current->flags |= orig_flags & flags;
1563907aed48SMel Gorman }
1564907aed48SMel Gorman 
15655eca1c10SIngo Molnar extern int cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
15665eca1c10SIngo Molnar extern int task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed);
15671da177e4SLinus Torvalds #ifdef CONFIG_SMP
15685eca1c10SIngo Molnar extern void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask);
15695eca1c10SIngo Molnar extern int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask);
15701da177e4SLinus Torvalds #else
15715eca1c10SIngo Molnar static inline void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
15721e1b6c51SKOSAKI Motohiro {
15731e1b6c51SKOSAKI Motohiro }
15745eca1c10SIngo Molnar static inline int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
15751da177e4SLinus Torvalds {
157696f874e2SRusty Russell 	if (!cpumask_test_cpu(0, new_mask))
15771da177e4SLinus Torvalds 		return -EINVAL;
15781da177e4SLinus Torvalds 	return 0;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds #endif
1581e0ad9556SRusty Russell 
15826d0d2878SChristian Borntraeger #ifndef cpu_relax_yield
15836d0d2878SChristian Borntraeger #define cpu_relax_yield() cpu_relax()
15846d0d2878SChristian Borntraeger #endif
15856d0d2878SChristian Borntraeger 
1586fa93384fSDan Carpenter extern int yield_to(struct task_struct *p, bool preempt);
158736c8b586SIngo Molnar extern void set_user_nice(struct task_struct *p, long nice);
158836c8b586SIngo Molnar extern int task_prio(const struct task_struct *p);
15895eca1c10SIngo Molnar 
1590d0ea0268SDongsheng Yang /**
1591d0ea0268SDongsheng Yang  * task_nice - return the nice value of a given task.
1592d0ea0268SDongsheng Yang  * @p: the task in question.
1593d0ea0268SDongsheng Yang  *
1594d0ea0268SDongsheng Yang  * Return: The nice value [ -20 ... 0 ... 19 ].
1595d0ea0268SDongsheng Yang  */
1596d0ea0268SDongsheng Yang static inline int task_nice(const struct task_struct *p)
1597d0ea0268SDongsheng Yang {
1598d0ea0268SDongsheng Yang 	return PRIO_TO_NICE((p)->static_prio);
1599d0ea0268SDongsheng Yang }
16005eca1c10SIngo Molnar 
160136c8b586SIngo Molnar extern int can_nice(const struct task_struct *p, const int nice);
160236c8b586SIngo Molnar extern int task_curr(const struct task_struct *p);
16031da177e4SLinus Torvalds extern int idle_cpu(int cpu);
1604943d355dSRohit Jain extern int available_idle_cpu(int cpu);
16055eca1c10SIngo Molnar extern int sched_setscheduler(struct task_struct *, int, const struct sched_param *);
16065eca1c10SIngo Molnar extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sched_param *);
16075eca1c10SIngo Molnar extern int sched_setattr(struct task_struct *, const struct sched_attr *);
1608794a56ebSJuri Lelli extern int sched_setattr_nocheck(struct task_struct *, const struct sched_attr *);
160936c8b586SIngo Molnar extern struct task_struct *idle_task(int cpu);
16105eca1c10SIngo Molnar 
1611c4f30608SPaul E. McKenney /**
1612c4f30608SPaul E. McKenney  * is_idle_task - is the specified task an idle task?
1613fa757281SRandy Dunlap  * @p: the task in question.
1614e69f6186SYacine Belkadi  *
1615e69f6186SYacine Belkadi  * Return: 1 if @p is an idle task. 0 otherwise.
1616c4f30608SPaul E. McKenney  */
16177061ca3bSPaul E. McKenney static inline bool is_idle_task(const struct task_struct *p)
1618c4f30608SPaul E. McKenney {
1619c1de45caSPeter Zijlstra 	return !!(p->flags & PF_IDLE);
1620c4f30608SPaul E. McKenney }
16215eca1c10SIngo Molnar 
162236c8b586SIngo Molnar extern struct task_struct *curr_task(int cpu);
1623a458ae2eSPeter Zijlstra extern void ia64_set_curr_task(int cpu, struct task_struct *p);
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds void yield(void);
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds union thread_union {
16280500871fSDavid Howells #ifndef CONFIG_ARCH_TASK_STRUCT_ON_STACK
16290500871fSDavid Howells 	struct task_struct task;
16300500871fSDavid Howells #endif
1631c65eacbeSAndy Lutomirski #ifndef CONFIG_THREAD_INFO_IN_TASK
16321da177e4SLinus Torvalds 	struct thread_info thread_info;
1633c65eacbeSAndy Lutomirski #endif
16341da177e4SLinus Torvalds 	unsigned long stack[THREAD_SIZE/sizeof(long)];
16351da177e4SLinus Torvalds };
16361da177e4SLinus Torvalds 
16370500871fSDavid Howells #ifndef CONFIG_THREAD_INFO_IN_TASK
16380500871fSDavid Howells extern struct thread_info init_thread_info;
16390500871fSDavid Howells #endif
16400500871fSDavid Howells 
16410500871fSDavid Howells extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
16420500871fSDavid Howells 
1643f3ac6067SIngo Molnar #ifdef CONFIG_THREAD_INFO_IN_TASK
1644f3ac6067SIngo Molnar static inline struct thread_info *task_thread_info(struct task_struct *task)
1645f3ac6067SIngo Molnar {
1646f3ac6067SIngo Molnar 	return &task->thread_info;
1647f3ac6067SIngo Molnar }
1648f3ac6067SIngo Molnar #elif !defined(__HAVE_THREAD_FUNCTIONS)
1649f3ac6067SIngo Molnar # define task_thread_info(task)	((struct thread_info *)(task)->stack)
1650f3ac6067SIngo Molnar #endif
1651f3ac6067SIngo Molnar 
1652198fe21bSPavel Emelyanov /*
1653198fe21bSPavel Emelyanov  * find a task by one of its numerical ids
1654198fe21bSPavel Emelyanov  *
1655198fe21bSPavel Emelyanov  * find_task_by_pid_ns():
1656198fe21bSPavel Emelyanov  *      finds a task by its pid in the specified namespace
1657228ebcbeSPavel Emelyanov  * find_task_by_vpid():
1658228ebcbeSPavel Emelyanov  *      finds a task by its virtual pid
1659198fe21bSPavel Emelyanov  *
1660e49859e7SPavel Emelyanov  * see also find_vpid() etc in include/linux/pid.h
1661198fe21bSPavel Emelyanov  */
1662198fe21bSPavel Emelyanov 
1663228ebcbeSPavel Emelyanov extern struct task_struct *find_task_by_vpid(pid_t nr);
16645eca1c10SIngo Molnar extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
1665198fe21bSPavel Emelyanov 
16662ee08260SMike Rapoport /*
16672ee08260SMike Rapoport  * find a task by its virtual pid and get the task struct
16682ee08260SMike Rapoport  */
16692ee08260SMike Rapoport extern struct task_struct *find_get_task_by_vpid(pid_t nr);
16702ee08260SMike Rapoport 
1671b3c97528SHarvey Harrison extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1672b3c97528SHarvey Harrison extern int wake_up_process(struct task_struct *tsk);
16733e51e3edSSamir Bellabes extern void wake_up_new_task(struct task_struct *tsk);
16745eca1c10SIngo Molnar 
16751da177e4SLinus Torvalds #ifdef CONFIG_SMP
16761da177e4SLinus Torvalds extern void kick_process(struct task_struct *tsk);
16771da177e4SLinus Torvalds #else
16781da177e4SLinus Torvalds static inline void kick_process(struct task_struct *tsk) { }
16791da177e4SLinus Torvalds #endif
16801da177e4SLinus Torvalds 
168182b89778SAdrian Hunter extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec);
16825eca1c10SIngo Molnar 
168382b89778SAdrian Hunter static inline void set_task_comm(struct task_struct *tsk, const char *from)
168482b89778SAdrian Hunter {
168582b89778SAdrian Hunter 	__set_task_comm(tsk, from, false);
168682b89778SAdrian Hunter }
16875eca1c10SIngo Molnar 
16883756f640SArnd Bergmann extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
16893756f640SArnd Bergmann #define get_task_comm(buf, tsk) ({			\
16903756f640SArnd Bergmann 	BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN);	\
16913756f640SArnd Bergmann 	__get_task_comm(buf, sizeof(buf), tsk);		\
16923756f640SArnd Bergmann })
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds #ifdef CONFIG_SMP
1695317f3941SPeter Zijlstra void scheduler_ipi(void);
169685ba2d86SRoland McGrath extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
16971da177e4SLinus Torvalds #else
1698184748ccSPeter Zijlstra static inline void scheduler_ipi(void) { }
16995eca1c10SIngo Molnar static inline unsigned long wait_task_inactive(struct task_struct *p, long match_state)
170085ba2d86SRoland McGrath {
170185ba2d86SRoland McGrath 	return 1;
170285ba2d86SRoland McGrath }
17031da177e4SLinus Torvalds #endif
17041da177e4SLinus Torvalds 
17055eca1c10SIngo Molnar /*
17065eca1c10SIngo Molnar  * Set thread flags in other task's structures.
17075eca1c10SIngo Molnar  * See asm/thread_info.h for TIF_xxxx flags available:
17081da177e4SLinus Torvalds  */
17091da177e4SLinus Torvalds static inline void set_tsk_thread_flag(struct task_struct *tsk, int flag)
17101da177e4SLinus Torvalds {
1711a1261f54SAl Viro 	set_ti_thread_flag(task_thread_info(tsk), flag);
17121da177e4SLinus Torvalds }
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds static inline void clear_tsk_thread_flag(struct task_struct *tsk, int flag)
17151da177e4SLinus Torvalds {
1716a1261f54SAl Viro 	clear_ti_thread_flag(task_thread_info(tsk), flag);
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
171993ee37c2SDave Martin static inline void update_tsk_thread_flag(struct task_struct *tsk, int flag,
172093ee37c2SDave Martin 					  bool value)
172193ee37c2SDave Martin {
172293ee37c2SDave Martin 	update_ti_thread_flag(task_thread_info(tsk), flag, value);
172393ee37c2SDave Martin }
172493ee37c2SDave Martin 
17251da177e4SLinus Torvalds static inline int test_and_set_tsk_thread_flag(struct task_struct *tsk, int flag)
17261da177e4SLinus Torvalds {
1727a1261f54SAl Viro 	return test_and_set_ti_thread_flag(task_thread_info(tsk), flag);
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds static inline int test_and_clear_tsk_thread_flag(struct task_struct *tsk, int flag)
17311da177e4SLinus Torvalds {
1732a1261f54SAl Viro 	return test_and_clear_ti_thread_flag(task_thread_info(tsk), flag);
17331da177e4SLinus Torvalds }
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
17361da177e4SLinus Torvalds {
1737a1261f54SAl Viro 	return test_ti_thread_flag(task_thread_info(tsk), flag);
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds static inline void set_tsk_need_resched(struct task_struct *tsk)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds static inline void clear_tsk_need_resched(struct task_struct *tsk)
17461da177e4SLinus Torvalds {
17471da177e4SLinus Torvalds 	clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
17481da177e4SLinus Torvalds }
17491da177e4SLinus Torvalds 
17508ae121acSGregory Haskins static inline int test_tsk_need_resched(struct task_struct *tsk)
17518ae121acSGregory Haskins {
17528ae121acSGregory Haskins 	return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
17538ae121acSGregory Haskins }
17548ae121acSGregory Haskins 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  * cond_resched() and cond_resched_lock(): latency reduction via
17571da177e4SLinus Torvalds  * explicit rescheduling in places that are safe. The return
17581da177e4SLinus Torvalds  * value indicates whether a reschedule was done in fact.
17591da177e4SLinus Torvalds  * cond_resched_lock() will drop the spinlock before scheduling,
17601da177e4SLinus Torvalds  */
176135a773a0SPeter Zijlstra #ifndef CONFIG_PREEMPT
1762c3921ab7SLinus Torvalds extern int _cond_resched(void);
176335a773a0SPeter Zijlstra #else
176435a773a0SPeter Zijlstra static inline int _cond_resched(void) { return 0; }
176535a773a0SPeter Zijlstra #endif
17666f80bd98SFrederic Weisbecker 
1767613afbf8SFrederic Weisbecker #define cond_resched() ({			\
17683427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, 0);	\
1769613afbf8SFrederic Weisbecker 	_cond_resched();			\
1770613afbf8SFrederic Weisbecker })
17716f80bd98SFrederic Weisbecker 
1772613afbf8SFrederic Weisbecker extern int __cond_resched_lock(spinlock_t *lock);
1773613afbf8SFrederic Weisbecker 
1774613afbf8SFrederic Weisbecker #define cond_resched_lock(lock) ({				\
17753427445aSPeter Zijlstra 	___might_sleep(__FILE__, __LINE__, PREEMPT_LOCK_OFFSET);\
1776613afbf8SFrederic Weisbecker 	__cond_resched_lock(lock);				\
1777613afbf8SFrederic Weisbecker })
1778613afbf8SFrederic Weisbecker 
1779f6f3c437SSimon Horman static inline void cond_resched_rcu(void)
1780f6f3c437SSimon Horman {
1781f6f3c437SSimon Horman #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
1782f6f3c437SSimon Horman 	rcu_read_unlock();
1783f6f3c437SSimon Horman 	cond_resched();
1784f6f3c437SSimon Horman 	rcu_read_lock();
1785f6f3c437SSimon Horman #endif
1786f6f3c437SSimon Horman }
1787f6f3c437SSimon Horman 
17881da177e4SLinus Torvalds /*
17891da177e4SLinus Torvalds  * Does a critical section need to be broken due to another
179095c354feSNick Piggin  * task waiting?: (technically does not depend on CONFIG_PREEMPT,
179195c354feSNick Piggin  * but a general need for low latency)
17921da177e4SLinus Torvalds  */
179395c354feSNick Piggin static inline int spin_needbreak(spinlock_t *lock)
17941da177e4SLinus Torvalds {
179595c354feSNick Piggin #ifdef CONFIG_PREEMPT
179695c354feSNick Piggin 	return spin_is_contended(lock);
179795c354feSNick Piggin #else
17981da177e4SLinus Torvalds 	return 0;
179995c354feSNick Piggin #endif
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
180275f93fedSPeter Zijlstra static __always_inline bool need_resched(void)
180375f93fedSPeter Zijlstra {
180475f93fedSPeter Zijlstra 	return unlikely(tif_need_resched());
180575f93fedSPeter Zijlstra }
180675f93fedSPeter Zijlstra 
1807ee761f62SThomas Gleixner /*
18081da177e4SLinus Torvalds  * Wrappers for p->thread_info->cpu access. No-op on UP.
18091da177e4SLinus Torvalds  */
18101da177e4SLinus Torvalds #ifdef CONFIG_SMP
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
18131da177e4SLinus Torvalds {
1814c65eacbeSAndy Lutomirski #ifdef CONFIG_THREAD_INFO_IN_TASK
1815c546951dSAndrea Parri 	return READ_ONCE(p->cpu);
1816c65eacbeSAndy Lutomirski #else
1817c546951dSAndrea Parri 	return READ_ONCE(task_thread_info(p)->cpu);
1818c65eacbeSAndy Lutomirski #endif
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
1821c65cc870SIngo Molnar extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds #else
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds static inline unsigned int task_cpu(const struct task_struct *p)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	return 0;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds #endif /* CONFIG_SMP */
18351da177e4SLinus Torvalds 
1836d9345c65SPan Xinhui /*
1837d9345c65SPan Xinhui  * In order to reduce various lock holder preemption latencies provide an
1838d9345c65SPan Xinhui  * interface to see if a vCPU is currently running or not.
1839d9345c65SPan Xinhui  *
1840d9345c65SPan Xinhui  * This allows us to terminate optimistic spin loops and block, analogous to
1841d9345c65SPan Xinhui  * the native optimistic spin heuristic of testing if the lock owner task is
1842d9345c65SPan Xinhui  * running or not.
1843d9345c65SPan Xinhui  */
1844d9345c65SPan Xinhui #ifndef vcpu_is_preempted
1845d9345c65SPan Xinhui # define vcpu_is_preempted(cpu)	false
1846d9345c65SPan Xinhui #endif
1847d9345c65SPan Xinhui 
184896f874e2SRusty Russell extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
184996f874e2SRusty Russell extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
18505c45bf27SSiddha, Suresh B 
185182455257SDave Hansen #ifndef TASK_SIZE_OF
185282455257SDave Hansen #define TASK_SIZE_OF(tsk)	TASK_SIZE
185382455257SDave Hansen #endif
185482455257SDave Hansen 
1855d7822b1eSMathieu Desnoyers #ifdef CONFIG_RSEQ
1856d7822b1eSMathieu Desnoyers 
1857d7822b1eSMathieu Desnoyers /*
1858d7822b1eSMathieu Desnoyers  * Map the event mask on the user-space ABI enum rseq_cs_flags
1859d7822b1eSMathieu Desnoyers  * for direct mask checks.
1860d7822b1eSMathieu Desnoyers  */
1861d7822b1eSMathieu Desnoyers enum rseq_event_mask_bits {
1862d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_PREEMPT_BIT	= RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT,
1863d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_SIGNAL_BIT	= RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT,
1864d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_MIGRATE_BIT	= RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT,
1865d7822b1eSMathieu Desnoyers };
1866d7822b1eSMathieu Desnoyers 
1867d7822b1eSMathieu Desnoyers enum rseq_event_mask {
1868d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_PREEMPT	= (1U << RSEQ_EVENT_PREEMPT_BIT),
1869d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_SIGNAL	= (1U << RSEQ_EVENT_SIGNAL_BIT),
1870d7822b1eSMathieu Desnoyers 	RSEQ_EVENT_MIGRATE	= (1U << RSEQ_EVENT_MIGRATE_BIT),
1871d7822b1eSMathieu Desnoyers };
1872d7822b1eSMathieu Desnoyers 
1873d7822b1eSMathieu Desnoyers static inline void rseq_set_notify_resume(struct task_struct *t)
1874d7822b1eSMathieu Desnoyers {
1875d7822b1eSMathieu Desnoyers 	if (t->rseq)
1876d7822b1eSMathieu Desnoyers 		set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1877d7822b1eSMathieu Desnoyers }
1878d7822b1eSMathieu Desnoyers 
1879784e0300SWill Deacon void __rseq_handle_notify_resume(struct ksignal *sig, struct pt_regs *regs);
1880d7822b1eSMathieu Desnoyers 
1881784e0300SWill Deacon static inline void rseq_handle_notify_resume(struct ksignal *ksig,
1882784e0300SWill Deacon 					     struct pt_regs *regs)
1883d7822b1eSMathieu Desnoyers {
1884d7822b1eSMathieu Desnoyers 	if (current->rseq)
1885784e0300SWill Deacon 		__rseq_handle_notify_resume(ksig, regs);
1886d7822b1eSMathieu Desnoyers }
1887d7822b1eSMathieu Desnoyers 
1888784e0300SWill Deacon static inline void rseq_signal_deliver(struct ksignal *ksig,
1889784e0300SWill Deacon 				       struct pt_regs *regs)
1890d7822b1eSMathieu Desnoyers {
1891d7822b1eSMathieu Desnoyers 	preempt_disable();
1892d7822b1eSMathieu Desnoyers 	__set_bit(RSEQ_EVENT_SIGNAL_BIT, &current->rseq_event_mask);
1893d7822b1eSMathieu Desnoyers 	preempt_enable();
1894784e0300SWill Deacon 	rseq_handle_notify_resume(ksig, regs);
1895d7822b1eSMathieu Desnoyers }
1896d7822b1eSMathieu Desnoyers 
1897d7822b1eSMathieu Desnoyers /* rseq_preempt() requires preemption to be disabled. */
1898d7822b1eSMathieu Desnoyers static inline void rseq_preempt(struct task_struct *t)
1899d7822b1eSMathieu Desnoyers {
1900d7822b1eSMathieu Desnoyers 	__set_bit(RSEQ_EVENT_PREEMPT_BIT, &t->rseq_event_mask);
1901d7822b1eSMathieu Desnoyers 	rseq_set_notify_resume(t);
1902d7822b1eSMathieu Desnoyers }
1903d7822b1eSMathieu Desnoyers 
1904d7822b1eSMathieu Desnoyers /* rseq_migrate() requires preemption to be disabled. */
1905d7822b1eSMathieu Desnoyers static inline void rseq_migrate(struct task_struct *t)
1906d7822b1eSMathieu Desnoyers {
1907d7822b1eSMathieu Desnoyers 	__set_bit(RSEQ_EVENT_MIGRATE_BIT, &t->rseq_event_mask);
1908d7822b1eSMathieu Desnoyers 	rseq_set_notify_resume(t);
1909d7822b1eSMathieu Desnoyers }
1910d7822b1eSMathieu Desnoyers 
1911d7822b1eSMathieu Desnoyers /*
1912d7822b1eSMathieu Desnoyers  * If parent process has a registered restartable sequences area, the
19139a789fcfSMathieu Desnoyers  * child inherits. Only applies when forking a process, not a thread.
1914d7822b1eSMathieu Desnoyers  */
1915d7822b1eSMathieu Desnoyers static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
1916d7822b1eSMathieu Desnoyers {
1917d7822b1eSMathieu Desnoyers 	if (clone_flags & CLONE_THREAD) {
1918d7822b1eSMathieu Desnoyers 		t->rseq = NULL;
1919d7822b1eSMathieu Desnoyers 		t->rseq_sig = 0;
1920d7822b1eSMathieu Desnoyers 		t->rseq_event_mask = 0;
1921d7822b1eSMathieu Desnoyers 	} else {
1922d7822b1eSMathieu Desnoyers 		t->rseq = current->rseq;
1923d7822b1eSMathieu Desnoyers 		t->rseq_sig = current->rseq_sig;
1924d7822b1eSMathieu Desnoyers 		t->rseq_event_mask = current->rseq_event_mask;
1925d7822b1eSMathieu Desnoyers 	}
1926d7822b1eSMathieu Desnoyers }
1927d7822b1eSMathieu Desnoyers 
1928d7822b1eSMathieu Desnoyers static inline void rseq_execve(struct task_struct *t)
1929d7822b1eSMathieu Desnoyers {
1930d7822b1eSMathieu Desnoyers 	t->rseq = NULL;
1931d7822b1eSMathieu Desnoyers 	t->rseq_sig = 0;
1932d7822b1eSMathieu Desnoyers 	t->rseq_event_mask = 0;
1933d7822b1eSMathieu Desnoyers }
1934d7822b1eSMathieu Desnoyers 
1935d7822b1eSMathieu Desnoyers #else
1936d7822b1eSMathieu Desnoyers 
1937d7822b1eSMathieu Desnoyers static inline void rseq_set_notify_resume(struct task_struct *t)
1938d7822b1eSMathieu Desnoyers {
1939d7822b1eSMathieu Desnoyers }
1940784e0300SWill Deacon static inline void rseq_handle_notify_resume(struct ksignal *ksig,
1941784e0300SWill Deacon 					     struct pt_regs *regs)
1942d7822b1eSMathieu Desnoyers {
1943d7822b1eSMathieu Desnoyers }
1944784e0300SWill Deacon static inline void rseq_signal_deliver(struct ksignal *ksig,
1945784e0300SWill Deacon 				       struct pt_regs *regs)
1946d7822b1eSMathieu Desnoyers {
1947d7822b1eSMathieu Desnoyers }
1948d7822b1eSMathieu Desnoyers static inline void rseq_preempt(struct task_struct *t)
1949d7822b1eSMathieu Desnoyers {
1950d7822b1eSMathieu Desnoyers }
1951d7822b1eSMathieu Desnoyers static inline void rseq_migrate(struct task_struct *t)
1952d7822b1eSMathieu Desnoyers {
1953d7822b1eSMathieu Desnoyers }
1954d7822b1eSMathieu Desnoyers static inline void rseq_fork(struct task_struct *t, unsigned long clone_flags)
1955d7822b1eSMathieu Desnoyers {
1956d7822b1eSMathieu Desnoyers }
1957d7822b1eSMathieu Desnoyers static inline void rseq_execve(struct task_struct *t)
1958d7822b1eSMathieu Desnoyers {
1959d7822b1eSMathieu Desnoyers }
1960d7822b1eSMathieu Desnoyers 
1961d7822b1eSMathieu Desnoyers #endif
1962d7822b1eSMathieu Desnoyers 
196373ab1cb2STaehee Yoo void __exit_umh(struct task_struct *tsk);
196473ab1cb2STaehee Yoo 
196573ab1cb2STaehee Yoo static inline void exit_umh(struct task_struct *tsk)
196673ab1cb2STaehee Yoo {
196773ab1cb2STaehee Yoo 	if (unlikely(tsk->flags & PF_UMH))
196873ab1cb2STaehee Yoo 		__exit_umh(tsk);
196973ab1cb2STaehee Yoo }
197073ab1cb2STaehee Yoo 
1971d7822b1eSMathieu Desnoyers #ifdef CONFIG_DEBUG_RSEQ
1972d7822b1eSMathieu Desnoyers 
1973d7822b1eSMathieu Desnoyers void rseq_syscall(struct pt_regs *regs);
1974d7822b1eSMathieu Desnoyers 
1975d7822b1eSMathieu Desnoyers #else
1976d7822b1eSMathieu Desnoyers 
1977d7822b1eSMathieu Desnoyers static inline void rseq_syscall(struct pt_regs *regs)
1978d7822b1eSMathieu Desnoyers {
1979d7822b1eSMathieu Desnoyers }
1980d7822b1eSMathieu Desnoyers 
1981d7822b1eSMathieu Desnoyers #endif
1982d7822b1eSMathieu Desnoyers 
19833c93a0c0SQais Yousef const struct sched_avg *sched_trace_cfs_rq_avg(struct cfs_rq *cfs_rq);
19843c93a0c0SQais Yousef char *sched_trace_cfs_rq_path(struct cfs_rq *cfs_rq, char *str, int len);
19853c93a0c0SQais Yousef int sched_trace_cfs_rq_cpu(struct cfs_rq *cfs_rq);
19863c93a0c0SQais Yousef 
19873c93a0c0SQais Yousef const struct sched_avg *sched_trace_rq_avg_rt(struct rq *rq);
19883c93a0c0SQais Yousef const struct sched_avg *sched_trace_rq_avg_dl(struct rq *rq);
19893c93a0c0SQais Yousef const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq);
19903c93a0c0SQais Yousef 
19913c93a0c0SQais Yousef int sched_trace_rq_cpu(struct rq *rq);
19923c93a0c0SQais Yousef 
19933c93a0c0SQais Yousef const struct cpumask *sched_trace_rd_span(struct root_domain *rd);
19943c93a0c0SQais Yousef 
19951da177e4SLinus Torvalds #endif
1996